-
Notifications
You must be signed in to change notification settings - Fork 0
/
CJFlatComboBox.cpp
executable file
·362 lines (316 loc) · 10.3 KB
/
CJFlatComboBox.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// CJFlatComboBox.cpp : implementation file
//
// Copyright © 1998-99 Kirk W. Stowell
// mailto:kstowell@codejockeys.com
// http://www.codejockeys.com/kstowell/
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name and all copyright
// notices remains intact. If the source code in this file is used in
// any commercial application then a statement along the lines of
// "Portions Copyright © 1998 Kirk Stowell" must be included in
// the startup banner, "About" box or printed documentation. An email
// letting me know that you are using it would be nice as well. That's
// not much to ask considering the amount of work that went into this.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// Expect bugs!
//
// Please use and enjoy, and let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into
// this file.
//
/////////////////////////////////////////////////////////////////////////////
/****************************************************************************
*
* $Date: 5/17/99 9:24p $
* $Revision: 9 $
* $Archive: /CodeJockey/CJ60Lib/CJFlatComboBox.cpp $
*
* $History: CJFlatComboBox.cpp $
*
* ***************** Version 9 *****************
* User: Kirk Stowell Date: 5/17/99 Time: 9:24p
* Updated in $/CodeJockey/CJ60Lib
* Gilberto Botaro [seafox@techno.com.br] - Added code for auto
* completion.
*
* ***************** Version 8 *****************
* User: Kirk Stowell Date: 5/03/99 Time: 10:44p
* Updated in $/CodeJockey/CJ60Lib
* Fixed code ommisions.
*
* ***************** Version 7 *****************
* User: Kirk Stowell Date: 5/03/99 Time: 8:42p
* Updated in $/CodeJockey/CJ60Lib
* Danilo Lax [Danilo_Lax@www2.alpeng.com] - The control is painted flat
* when the mouse leaves the box, even if the control has the focus, now
* checking for the focus in OnTimer(.
*
* Removed, unhandled msgs are processed by
* CComboBox::PreTranslateMessage(pMsg). Used reflected msgs
* BN_GETFOCUS/CBN_KILLFOCUS instead of WM_GETFOCUS/WM_KILLFOCUS.
*
* ::GetFocus() doesn't return CFlatComboBox::m_hWnd, even if the control
* has the focus (don't know why, maybe ::GetFocus() only returns the
* handle to the EditField part of the ComboBox), used flag m_bHasFocus
* instead.
*
* ***************** Version 6 *****************
* User: Kirk Stowell Date: 4/03/99 Time: 4:26p
* Updated in $/CodeJockey/CJ60Lib
* Added comments and cleaned up code.
*
* ***************** Version 5 *****************
* User: Kirk Stowell Date: 1/22/99 Time: 4:26p
* Updated in $/CodeJockey/CJ60Lib
* Fixed accelerator problem reported by YOSH YAMASHITA
* (yosh@muj.biglobe.ne.jp) with Matt Weagle's solution of overloading
* PreTranslateMessage().
*
* ***************** Version 4 *****************
* User: Kirk Stowell Date: 1/02/99 Time: 4:25p
* Updated in $/CodeJockey/CJ60Lib
* Renamed class from CJComboBox to CCJFlatComboBox, changed DRAWSTATE to
* enum, removed method Offset(), and replaced with a direct call to
* ::GetSystemMetrics().
*
* ***************** Version 3 *****************
* User: Kirk Stowell Date: 11/02/98 Time: 4:24p
* Updated in $/CodeJockey/CJ60Lib
* Added OnSetFocus(), OnKillFocus() and modified OnPaint() so the control
* remains beveled until it actually looses focus - (Vachik
* Hovhannissian).
*
* ***************** Version 2 *****************
* User: Kirk Stowell Date: 10/05/98 Time: 4:24p
* Updated in $/CodeJockey/CJ60Lib
* Fixed offset bug to use GetSystemMetrics(), instead of hard coded
* values -
* Todd Brannam (tbrannam@msn.com).
*
* ***************** Version 1 *****************
* User: Kirk Stowell Date: 10/02/98 Time: 4:23p
* Created in $/CodeJockey/CJ60Lib
* First release version.
*
***************************************************************************/
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CJFlatComboBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCJFlatComboBox
CCJFlatComboBox::CCJFlatComboBox()
{
m_bLBtnDown = FALSE;
m_bPainted = FALSE;
m_bHasFocus = FALSE;
m_bAutoComp = TRUE;
m_clrBtnHilite = ::GetSysColor(COLOR_BTNHILIGHT);
m_clrBtnShadow = ::GetSysColor(COLOR_BTNSHADOW);
m_clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
m_nOffset = ::GetSystemMetrics(SM_CXHTHUMB);
}
CCJFlatComboBox::~CCJFlatComboBox()
{
}
IMPLEMENT_DYNAMIC(CCJFlatComboBox, CComboBox)
BEGIN_MESSAGE_MAP(CCJFlatComboBox, CComboBox)
//{{AFX_MSG_MAP(CCJFlatComboBox)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
ON_WM_PAINT()
ON_CONTROL_REFLECT(CBN_SETFOCUS, OnSetFocus)
ON_CONTROL_REFLECT(CBN_KILLFOCUS, OnKillFocus)
ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
ON_WM_SYSCOLORCHANGE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCJFlatComboBox message handlers
void CCJFlatComboBox::OnMouseMove(UINT nFlags, CPoint point)
{
// SetTimer(1,10,NULL);
// OnTimer(1);
CComboBox::OnMouseMove(nFlags, point);
}
void CCJFlatComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bLBtnDown = TRUE;
CComboBox::OnLButtonDown(nFlags, point);
}
void CCJFlatComboBox::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bLBtnDown = FALSE;
Invalidate();
CComboBox::OnLButtonUp(nFlags, point);
}
void CCJFlatComboBox::OnTimer(UINT nIDEvent)
{
POINT pt;
GetCursorPos(&pt);
CRect rcItem;
GetWindowRect(&rcItem);
// OnLButtonDown, show pressed.
if (m_bLBtnDown==TRUE) {
KillTimer (1);
if (m_bPainted == TRUE) {
DrawCombo( pressed, m_clrBtnShadow, m_clrBtnHilite );
m_bPainted = FALSE;
}
return;
}
// If mouse leaves, show flat.
if (!rcItem.PtInRect(pt) && !m_bHasFocus) {
KillTimer (1);
if (m_bPainted == TRUE) {
DrawCombo( normal, m_clrBtnFace, m_clrBtnFace );
m_bPainted = FALSE;
}
return;
}
// On mouse over, show raised.
else {
if (m_bPainted == TRUE)
return;
else {
m_bPainted = TRUE;
DrawCombo( raised, m_clrBtnShadow, m_clrBtnHilite );
}
}
CComboBox::OnTimer(nIDEvent);
}
void CCJFlatComboBox::OnPaint()
{
ModifyStyleEx (WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE,
0, SWP_FRAMECHANGED);
Default();
CPoint pt;
GetCursorPos(&pt);
CRect rcItem;
GetWindowRect(&rcItem);
/*
if ((rcItem.PtInRect(pt)) || m_bHasFocus)
DrawCombo( raised, m_clrBtnShadow, m_clrBtnHilite );
else
DrawCombo( normal, m_clrBtnFace, m_clrBtnFace );
*/
if (IsWindowEnabled ())
DrawCombo (raised, m_clrBtnShadow, m_clrBtnHilite);
else
DrawCombo (normal, m_clrBtnFace, m_clrBtnFace);
}
void CCJFlatComboBox::DrawCombo(STATE eState, COLORREF clrTopLeft, COLORREF clrBottomRight)
{
CRect rcItem;
GetClientRect(&rcItem);
CDC* pDC = GetDC();
// Cover up dark 3D shadow.
pDC->Draw3dRect(rcItem, clrTopLeft, clrBottomRight);
rcItem.DeflateRect(1,1);
if (!IsWindowEnabled()) {
pDC->Draw3dRect( rcItem, m_clrBtnHilite, m_clrBtnHilite );
}
else {
pDC->Draw3dRect( rcItem, m_clrBtnFace, m_clrBtnFace );
}
// Cover up dark 3D shadow on drop arrow.
rcItem.DeflateRect(1,1);
rcItem.left = rcItem.right-m_nOffset;
pDC->Draw3dRect( rcItem, m_clrBtnFace, m_clrBtnFace );
// Cover up normal 3D shadow on drop arrow.
rcItem.DeflateRect(1,1);
pDC->Draw3dRect( rcItem, m_clrBtnFace, m_clrBtnFace );
if (!IsWindowEnabled()) {
ReleaseDC(pDC); // mch
return;
}
rcItem.right++; // mch 6.2.02: too large border on right side -> increment right
switch (eState)
{
case normal:
rcItem.top -= 1;
rcItem.bottom += 1;
pDC->Draw3dRect( rcItem, m_clrBtnHilite, m_clrBtnHilite );
rcItem.left -= 1;
pDC->Draw3dRect( rcItem, m_clrBtnHilite, m_clrBtnHilite );
break;
case raised:
rcItem.top -= 1;
rcItem.bottom += 1;
pDC->Draw3dRect( rcItem, m_clrBtnHilite, m_clrBtnShadow);
break;
case pressed:
rcItem.top -= 1;
rcItem.bottom += 1;
rcItem.OffsetRect(1,1);
pDC->Draw3dRect( rcItem, m_clrBtnShadow, m_clrBtnHilite );
break;
}
ReleaseDC(pDC);
}
void CCJFlatComboBox::OnSetFocus()
{
m_bHasFocus = TRUE;
DrawCombo( raised, m_clrBtnShadow, m_clrBtnHilite );
}
void CCJFlatComboBox::OnKillFocus()
{
m_bHasFocus = FALSE;
DrawCombo( /*normal*/raised, m_clrBtnFace, m_clrBtnFace );
}
BOOL CCJFlatComboBox::PreTranslateMessage(MSG* pMsg)
{
// Catch the Alt key so we don't choke if focus
// is going to an owner drawn button
if (pMsg->message == WM_SYSCHAR)
return TRUE;
return CComboBox::PreTranslateMessage(pMsg);
}
void CCJFlatComboBox::OnEditUpdate()
{
// if we are not to auto update the text, get outta here
if (!m_bAutoComp)
return;
// Get the text in the edit box
CString str;
GetWindowText(str);
int nLength = str.GetLength();
// Currently selected range
DWORD dwCurSel = GetEditSel();
WORD dStart = LOWORD(dwCurSel);
WORD dEnd = HIWORD(dwCurSel);
// Search for, and select in, and string in the combo box that is prefixed
// by the text in the edit box
if (SelectString(-1, str) == CB_ERR)
{
SetWindowText(str); // No text selected, so restore what was there before
if (dwCurSel != CB_ERR)
SetEditSel(dStart, dEnd); //restore cursor postion
}
// Set the text selection as the additional text that we have added
if (dEnd < nLength && dwCurSel != CB_ERR)
SetEditSel(dStart, dEnd);
else
SetEditSel(nLength, -1);
}
void CCJFlatComboBox::OnSysColorChange()
{
CComboBox::OnSysColorChange();
// re-initialize system colors.
m_clrBtnHilite = ::GetSysColor(COLOR_BTNHILIGHT);
m_clrBtnShadow = ::GetSysColor(COLOR_BTNSHADOW);
m_clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
}