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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// ListBoxEx.cpp : implementation file
//
#include "stdafx.h"
#include "hammer.h"
#include "ListBoxEx.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
/////////////////////////////////////////////////////////////////////////////
// CListBoxEx
CListBoxEx::CListBoxEx()
{
Items.SetSize(16);
nItems = 0;
iItemHeight = -1;
dwStyle = 0;
bControlActive = FALSE;
bIgnoreChange = FALSE;
}
CListBoxEx::~CListBoxEx()
{
}
BEGIN_MESSAGE_MAP(CListBoxEx, CListBox)
//{{AFX_MSG_MAP(CListBoxEx)
ON_WM_LBUTTONDOWN()
ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
ON_WM_LBUTTONUP()
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListBoxEx message handlers
void CListBoxEx::SetStyle(DWORD dwStyle_)
{
this->dwStyle = dwStyle_;
}
void CListBoxEx::AddItem(char *pszCaption, int iEditType, PVOID pData,
int iRangeMin, int iRangeMax, const char * pszHelp)
{
LBEXTITEMSTRUCT lbis;
memset(&lbis, 0, sizeof lbis);
strcpy(lbis.szCaption, pszCaption);
lbis.pszSaveCaption = pszCaption;
lbis.iEditType = iEditType;
switch(iEditType)
{
case lbeYesNo:
case lbeOnOff:
lbis.iDataType = lbdBool;
lbis.iDataValue = PINT(pData)[0];
break;
case lbeInteger:
lbis.iDataType = lbdInteger;
lbis.iDataValue = PINT(pData)[0];
break;
case lbeTexture:
case lbeString:
lbis.iDataType = lbdString;
strcpy(lbis.szDataString, LPCTSTR(pData));
break;
case lbeChoices:
lbis.iDataType = lbdString;
lbis.pChoices = NULL;
break;
}
lbis.pSaveTo = pData;
lbis.iRangeMin = iRangeMin;
lbis.iRangeMax = iRangeMax;
lbis.pszHelp = pszHelp;
Items[nItems++] = lbis;
AddString(""); // trick windows! muahaha
}
void CListBoxEx::SetItemChoices(int iItem, CStringArray * pChoices,
int iDefaultChoice)
{
LBEXTITEMSTRUCT& lbis = Items[iItem];
lbis.pChoices = pChoices;
lbis.iDataValue = iDefaultChoice;
V_strcpy_safe( lbis.szDataString, pChoices->GetAt( iDefaultChoice ) );
}
void CListBoxEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if(iItemHeight == -1)
{
CDC *pDC = GetDC();
TEXTMETRIC tm;
pDC->GetOutputTextMetrics(&tm);
iItemHeight = tm.tmHeight + 4;
CRect r;
GetClientRect(r);
iCaptionWidthPixels = r.Width() / 2;
ReleaseDC(pDC);
}
lpMeasureItemStruct->itemHeight = iItemHeight;
}
void CListBoxEx::GetItemText(int iItem, char *pszText)
{
LBEXTITEMSTRUCT& lbis = Items[iItem];
switch(lbis.iDataType)
{
case lbdBool:
if(lbis.iEditType == lbeYesNo)
strcpy(pszText, lbis.iDataValue ? "Yes" : "No");
else
strcpy(pszText, lbis.iDataValue ? "On" : "Off");
break;
case lbdString:
strcpy(pszText, lbis.szDataString);
break;
case lbdInteger:
ltoa(lbis.iDataValue, pszText, 10);
break;
}
}
void CListBoxEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.SaveDC();
RECT& r = lpDrawItemStruct->rcItem;
if(lpDrawItemStruct->itemID != -1 &&
(lpDrawItemStruct->itemAction == ODA_DRAWENTIRE ||
lpDrawItemStruct->itemAction == ODA_SELECT))
{
LBEXTITEMSTRUCT& item = Items[lpDrawItemStruct->itemID];
dc.SetROP2(R2_COPYPEN);
int iBackIndex = COLOR_WINDOW;
int iForeIndex = COLOR_WINDOWTEXT;
BOOL bDrawCaptionOnly = FALSE;
if(lpDrawItemStruct->itemAction == ODA_SELECT &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
iBackIndex = COLOR_HIGHLIGHT;
iForeIndex = COLOR_HIGHLIGHTTEXT;
bDrawCaptionOnly = item.iDataType != lbdBool ? TRUE : FALSE;
}
// draw background
CBrush brush;
brush.CreateSolidBrush(GetSysColor(iBackIndex));
if(0)//!bDrawCaptionOnly)
dc.FillRect(&r, &brush);
else
{
CRect r2(&r);
r2.right = iCaptionWidthPixels;
dc.FillRect(r2, &brush);
}
// first, draw text
dc.SetTextColor(GetSysColor(iForeIndex));
dc.SetBkColor(GetSysColor(iBackIndex));
dc.TextOut(r.left + 1, r.top+ 1, item.szCaption,
strlen(item.szCaption));
if(!bDrawCaptionOnly)
{
// draw value ..
char szText[128];
GetItemText(lpDrawItemStruct->itemID, szText);
dc.TextOut(r.left + iCaptionWidthPixels + 1, r.top + 1,
szText, strlen(szText));
}
// draw border.
CPen pen(PS_SOLID, 1, RGB(200, 200, 200));
dc.SelectObject(pen);
dc.MoveTo(r.left, r.bottom-1);
dc.LineTo(r.right, r.bottom-1);
dc.MoveTo(r.left + iCaptionWidthPixels, r.top);
dc.LineTo(r.left + iCaptionWidthPixels, r.bottom-1);
}
else if(lpDrawItemStruct->itemAction == ODA_FOCUS)
{
dc.DrawFocusRect(&r);
}
dc.RestoreDC(-1);
}
void CListBoxEx::OnLButtonDown(UINT nFlags, CPoint point)
{
BOOL bOutside;
int iItem = ItemFromPoint(point, bOutside);
LBEXTITEMSTRUCT& lbis = Items[iItem];
if(lbis.iDataType == lbdBool)
{
// toggle bool field
lbis.iDataValue = !lbis.iDataValue;
}
CListBox::OnLButtonDown(nFlags, point);
}
int CListBoxEx::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
return 0;
}
void CListBoxEx::CreateEditControl()
{
if(IsWindow(EditCtrl.m_hWnd))
return;
// create edit control
int iItem = GetCurSel();
if(iItem == LB_ERR)
return;
LBEXTITEMSTRUCT& lbis = Items[iItem];
if(lbis.iEditType != lbeString &&
lbis.iEditType != lbeInteger &&
lbis.iEditType != lbeTexture)
return;
CRect r;
GetItemRect(iItem, r);
r.InflateRect(-1, -1);
r.left += iCaptionWidthPixels;
// create edit ctrl
EditCtrl.Create(ES_LEFT | ES_LOWERCASE | WS_VISIBLE | WS_TABSTOP, r, this,
IDC_EDITPARAMETER);
// set font
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == NULL)
hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
EditCtrl.SendMessage(WM_SETFONT, (WPARAM)hFont);
// set current text in edit ctrl
char szBuf[128];
GetItemText(iItem, szBuf);
EditCtrl.SetWindowText(szBuf);
EditCtrl.SetForegroundWindow();
EditCtrl.SetSel(0, -1);
bControlActive = TRUE;
iControlItem = iItem;
}
void CListBoxEx::CreateComboControl()
{
if(IsWindow(ComboCtrl.m_hWnd))
return;
// create edit control
int iItem = GetCurSel();
if(iItem == LB_ERR)
return;
LBEXTITEMSTRUCT& lbis = Items[iItem];
if(lbis.iEditType != lbeChoices)
return;
CRect r;
GetItemRect(iItem, r);
r.left += iCaptionWidthPixels;
r.bottom += 80;
// create combo ctrl
ComboCtrl.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST |
WS_TABSTOP, r, this, IDC_EDITPARAMETER);
// set font
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == NULL)
hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
ComboCtrl.SendMessage(WM_SETFONT, (WPARAM)hFont);
// add strings to combo ctrl
CStringArray * pChoices = lbis.pChoices;
Assert(pChoices);
for(int i = 0; i < pChoices->GetSize(); i++)
ComboCtrl.AddString(pChoices->GetAt(i));
// set current selection in combo ctrl
ComboCtrl.SetCurSel(lbis.iDataValue);
ComboCtrl.SetForegroundWindow();
bControlActive = TRUE;
iControlItem = iItem;
}
void CListBoxEx::DestroyControls()
{
// get rid of window if there is one
if(::IsWindow(EditCtrl.m_hWnd))
{
EditCtrl.DestroyWindow();
}
if(::IsWindow(ComboCtrl.m_hWnd))
{
ComboCtrl.DestroyWindow();
}
bControlActive = FALSE;
}
void CListBoxEx::OnSelchange()
{
if(bControlActive)
{
// on combobox/edit controls, save string back to data
LBEXTITEMSTRUCT& lbis = Items[iControlItem];
if(lbis.iEditType == lbeChoices)
{
ComboCtrl.GetLBText(ComboCtrl.GetCurSel(), lbis.szDataString);
lbis.iDataValue = ComboCtrl.GetCurSel();
}
else if(lbis.iDataType == lbdString)
{
EditCtrl.GetWindowText(lbis.szDataString, 128);
}
else if(lbis.iDataType == lbdInteger)
{
EditCtrl.GetWindowText(lbis.szDataString, 128);
lbis.iDataValue = atoi(lbis.szDataString);
}
}
DestroyControls();
int iCurItem = GetCurSel();
LBEXTITEMSTRUCT& lbis = Items[iCurItem];
if(lbis.iEditType == lbeChoices)
{
CreateComboControl();
}
else
{
CreateEditControl();
}
}
void CListBoxEx::OnLButtonUp(UINT nFlags, CPoint point)
{
CListBox::OnLButtonUp(nFlags, point);
int iItem = GetCurSel();
if(iItem == LB_ERR)
return;
LBEXTITEMSTRUCT& lbis = Items[iItem];
if(lbis.iDataType == lbdBool)
{
lbis.iDataValue = !lbis.iDataValue;
Invalidate();
}
}
void CListBoxEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CListBox::OnChar(nChar, nRepCnt, nFlags);
return;
int iItem = GetCurSel();
if(iItem == LB_ERR)
return;
LBEXTITEMSTRUCT& lbis = Items[iItem];
switch(nChar)
{
case VK_RETURN:
if(!(nFlags & 0x8000))
break;
if(lbis.iDataType == lbdBool)
{
// toggle bool field
lbis.iDataValue = !lbis.iDataValue;
Invalidate();
}
else if(lbis.iEditType == lbeChoices)
{
CreateComboControl();
}
else if(lbis.iEditType == lbeString ||
lbis.iEditType == lbeInteger ||
lbis.iEditType == lbeTexture)
{
CreateEditControl();
}
break;
}
}
|