forked from SomeSupport/eMule
-
Notifications
You must be signed in to change notification settings - Fork 97
/
ComboBoxEx2.cpp
193 lines (175 loc) · 5.18 KB
/
ComboBoxEx2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//this file is part of eMule
//Copyright (C)2002-2008 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
#include "ComboBoxEx2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////////
// CComboBoxEx2
IMPLEMENT_DYNAMIC(CComboBoxEx2, CComboBoxEx)
BEGIN_MESSAGE_MAP(CComboBoxEx2, CComboBoxEx)
END_MESSAGE_MAP()
CComboBoxEx2::CComboBoxEx2()
{
}
CComboBoxEx2::~CComboBoxEx2()
{
}
int CComboBoxEx2::AddItem(LPCTSTR pszText, int iImage)
{
COMBOBOXEXITEM cbi = {0};
cbi.mask = CBEIF_TEXT;
cbi.iItem = -1;
cbi.pszText = (LPTSTR)pszText;
if (iImage != -1)
{
cbi.mask |= CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
cbi.iImage = iImage;
cbi.iSelectedImage = iImage;
}
return InsertItem(&cbi);
}
BOOL CComboBoxEx2::PreTranslateMessage(MSG* pMsg)
{
// there seems to be no way that we get the WM_CHARTOITEM for this control
ASSERT( pMsg->message != WM_CHARTOITEM );
if (pMsg->message == WM_KEYDOWN)
{
UINT uChar = MapVirtualKey(pMsg->wParam, 2);
if (uChar != 0)
{
// CComboBox::SelectString seems also not to work
CComboBox* pctrlCB = GetComboBoxCtrl();
if (pctrlCB != NULL)
{
int iCount = pctrlCB->GetCount();
for (int i = 0; i < iCount; i++)
{
CString strItem;
pctrlCB->GetLBText(i, strItem);
if (strItem.IsEmpty())
continue;
//those casts are indeed all(!) needed to get that thing (at least!) running correctly for ANSI code pages,
//if that will also work for MBCS code pages has to be tested..
UINT uFirstChar = (UINT)(_TUCHAR)strItem[0];
UINT uFirstCharLower = (UINT)(_TUCHAR)_totlower((_TINT)(uFirstChar));
UINT uTheChar = (UINT)(_TUCHAR)_totlower((_TINT)((UINT)uChar));
if (uFirstCharLower == uTheChar){
SetCurSel(i);
GetParent()->SendMessage(WM_COMMAND, MAKELONG((WORD)GetWindowLong(m_hWnd, GWL_ID), CBN_SELCHANGE), (LPARAM)m_hWnd);
return TRUE;
}
}
}
}
}
return CComboBoxEx::PreTranslateMessage(pMsg);
}
// Win98: This function does not work under Win98 ?
BOOL CComboBoxEx2::SelectString(LPCTSTR pszText)
{
// CComboBox::SelectString seems also not to work
CComboBox* pctrlCB = GetComboBoxCtrl();
if (pctrlCB != NULL)
{
int iCount = pctrlCB->GetCount();
for (int i = 0; i < iCount; i++)
{
CString strItem;
pctrlCB->GetLBText(i, strItem);
if (strItem == pszText)
{
SetCurSel(i);
GetParent()->SendMessage(WM_COMMAND, MAKELONG((WORD)GetWindowLong(m_hWnd, GWL_ID), CBN_SELCHANGE), (LPARAM)m_hWnd);
return TRUE;
}
}
}
return FALSE;
}
BOOL CComboBoxEx2::SelectItemDataStringA(LPCSTR pszText)
{
CComboBox* pctrlCB = GetComboBoxCtrl();
if (pctrlCB != NULL)
{
int iCount = pctrlCB->GetCount();
for (int i = 0; i < iCount; i++)
{
void* pvItemData = GetItemDataPtr(i);
if (pvItemData && strcmp((LPCSTR)pvItemData, pszText) == 0)
{
SetCurSel(i);
GetParent()->SendMessage(WM_COMMAND, MAKELONG((WORD)GetWindowLong(m_hWnd, GWL_ID), CBN_SELCHANGE), (LPARAM)m_hWnd);
return TRUE;
}
}
}
return FALSE;
}
void UpdateHorzExtent(CComboBox &rctlComboBox, int iIconWidth)
{
int iItemCount = rctlComboBox.GetCount();
if (iItemCount > 0)
{
CDC *pDC = rctlComboBox.GetDC();
if (pDC != NULL)
{
// *** To get *ACCURATE* results from 'GetOutputTextExtent' one *MUST*
// *** explicitly set the font!
CFont *pOldFont = pDC->SelectObject(rctlComboBox.GetFont());
CString strItem;
int iMaxWidth = 0;
for (int i = 0; i < iItemCount; i++)
{
rctlComboBox.GetLBText(i, strItem);
int iItemWidth = pDC->GetOutputTextExtent(strItem, strItem.GetLength()).cx;
if (iItemWidth > iMaxWidth)
iMaxWidth = iItemWidth;
}
pDC->SelectObject(pOldFont);
rctlComboBox.ReleaseDC(pDC);
// Depending on the string (lot of "M" or lot of "i") sometime the
// width is just a few pixels too small!
iMaxWidth += 4;
if (iIconWidth)
iMaxWidth += 2 + iIconWidth + 2;
rctlComboBox.SetHorizontalExtent(iMaxWidth);
if (rctlComboBox.GetDroppedWidth() < iMaxWidth)
rctlComboBox.SetDroppedWidth(iMaxWidth);
}
}
else
rctlComboBox.SetHorizontalExtent(0);
}
HWND GetComboBoxEditCtrl(CComboBox& cb)
{
CWnd* pWnd = cb.GetWindow(GW_CHILD);
while (pWnd)
{
CHAR szClassName[MAX_PATH];
if (::GetClassNameA(*pWnd, szClassName, ARRSIZE(szClassName)))
{
if (__ascii_stricmp(szClassName, "EDIT") == 0)
return pWnd->m_hWnd;
}
pWnd = pWnd->GetNextWindow();
}
return NULL;
}