summaryrefslogtreecommitdiff
path: root/hammer/iconcombobox.cpp
blob: 3c980db2511e743685da9049ab2edbe4ad815724 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include <stdafx.h>
#include "IconComboBox.h"

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CIconComboBox::CIconComboBox()
{
}


//-----------------------------------------------------------------------------
// Purpose: Deconstructor
//-----------------------------------------------------------------------------
CIconComboBox::~CIconComboBox()
{
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CIconComboBox::Init( void )
{
	// initialize the icon size
	m_IconSize.cx = GetSystemMetrics( SM_CXICON );
	m_IconSize.cy = GetSystemMetrics( SM_CYICON );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CIconComboBox::AddIcon( LPCTSTR pIconName )
{
	//
	// create/load an icon from file
	//
	// NULL - no icons in file
	// 1 - not a proper icon file
	//
	HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pIconName, 0 );
	if( ( hIcon == ( HICON )1 ) || !hIcon )
		return CB_ERR;

	//
	// add the icon to the combo box - returning the index
	//
	// CB_ERR - general error adding icon
	// CB_ERRSPACE - insufficient space necessary to add icon
	//
	int ndx = CComboBox::AddString( pIconName );
	if( ( ndx == CB_ERR ) || ( ndx == CB_ERRSPACE ) )
		return ndx;

	//
	// associate the icon with the index
	//
	// CB_ERR - general error
	//
	int result = SetItemData( ndx, ( DWORD )hIcon );
	if( result == CB_ERR )
		return result;

	// return the icon index
	return ndx;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CIconComboBox::InsertIcon( LPCTSTR pIconName, int ndx )
{
	//
	// create an icon from file
	// 
	// NULL - no icons in file
	// 1 - not a proper icon file
	//
	HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pIconName, 0 );
	if( ( hIcon == ( HICON )1 ) || !hIcon )
		return CB_ERR;

	//
	// insert the icon into the combo box -- returning the index
	//
	// CB_ERR - general error adding icon
	// CB_ERRSPACE - insufficient space necessary to add icon
	//
	int result = CComboBox::InsertString( ndx, pIconName );
	if( ( result == CB_ERR ) || ( result == CB_ERRSPACE ) )
		return result;

	//
	// associate the icon with the index
	//
	// CB_ERR - general error
	//
	result = SetItemData( ndx, ( DWORD )hIcon );
	if( result == CB_ERR )
		return result;
	
	// return the icon index
	return ndx;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CIconComboBox::SelectIcon( LPCTSTR pIconName )
{
	//
	// search the combo box list for the given string, -1 = search the whole list
	//
	// CB_ERR - unsuccessful search
	//
	int ndx = CComboBox::FindStringExact( -1, pIconName );
	if( ndx == CB_ERR )
		return CB_ERR;

	// set the selection current
	return CComboBox::SetCurSel( ndx );
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CIconComboBox::SelectIcon( int ndx )
{
	// set the selection current
	return CComboBox::SetCurSel( ndx );
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CIconComboBox::DeleteIcon( LPCTSTR pIconName )
{
	//
	// search the combo box list for the given string, -1 = search the whole list
	//
	// CB_ERR - unsuccessful search
	//
	int ndx = CComboBox::FindStringExact( -1, pIconName );
	if( ndx == CB_ERR )
		return CB_ERR;

	// remove the icon from the combo box
	return CComboBox::DeleteString( ndx );
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CIconComboBox::DeleteIcon( int ndx )
{
	// remove the icon from the combo box
	return CComboBox::DeleteString( ndx );
}


//-----------------------------------------------------------------------------
// Purpose: don't allow the icon combo box to "AddString"
//-----------------------------------------------------------------------------
int CIconComboBox::AddString( LPCTSTR lpszString )
{
	assert( FALSE ); 
	return CB_ERR;
}


//-----------------------------------------------------------------------------
// Purpose: don't allow the icon combo box to "InsertString"
//-----------------------------------------------------------------------------
int CIconComboBox::InsertString( int nIndex, LPCTSTR lpszString )
{
	assert( FALSE );
	return CB_ERR;
}


//-----------------------------------------------------------------------------
// Purpose: don't allow the icon combo box to "DeleteString"
//-----------------------------------------------------------------------------
int CIconComboBox::DeleteString( int nIndex )
{
	assert( FALSE );
	return CB_ERR;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CIconComboBox::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{ 
	lpMeasureItemStruct->itemWidth = m_IconSize.cx;
	lpMeasureItemStruct->itemHeight = m_IconSize.cy + 1;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CIconComboBox::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
	CBrush *pOldBrush = NULL;
	CPen *pOldPen = NULL;

	//
	// the icon is "disabled"
	//
	if( !IsWindowEnabled() )
	{
		SetDisabledBrushAndPen( lpDrawItemStruct, &pOldBrush, &pOldPen );
        OnDrawIcon( lpDrawItemStruct );
		ResetBrushAndPen( lpDrawItemStruct, pOldBrush, pOldPen );
		return;
	}

	//
	// the icon is "selected"
	//
	if( ( lpDrawItemStruct->itemState & ODS_SELECTED ) && 
		( lpDrawItemStruct->itemAction & ( ODA_SELECT | ODA_DRAWENTIRE ) ) ) 
	{
		SetSelectedBrushAndPen( lpDrawItemStruct, &pOldBrush, &pOldPen );
		OnDrawIcon( lpDrawItemStruct );
		ResetBrushAndPen( lpDrawItemStruct, pOldBrush, pOldPen );
	}

	//
	// the icon is "un-selected"
	//
	if( !( lpDrawItemStruct->itemState & ODS_SELECTED ) && 
		 ( lpDrawItemStruct->itemAction & ( ODA_SELECT | ODA_DRAWENTIRE ) ) ) 
	{
		SetUnSelectedBrushAndPen( lpDrawItemStruct, &pOldBrush, &pOldPen );
		OnDrawIcon( lpDrawItemStruct );
		ResetBrushAndPen( lpDrawItemStruct, pOldBrush, pOldPen );
	}

	//
    // icon gains focus
	//
    if( lpDrawItemStruct->itemAction & ODA_FOCUS ) 
	{ 
		// get the device context
		CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

		// render the focus rectangle
        pDC->DrawFocusRect( &lpDrawItemStruct->rcItem );
    }
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CIconComboBox::OnDrawIcon( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
	// any items to draw?
	if( GetCount() == 0 )
		return;

	// get the device context - to draw
	CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

	// get the current icon to render
	HICON hIcon = ( HICON )lpDrawItemStruct->itemData;
	if( !hIcon )
		return;

	// calculate the icon's upper left corner
	int UpperLeftX = lpDrawItemStruct->rcItem.left + 
		             ( ( lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left ) / 2 ) - 
			         ( m_IconSize.cx / 2 );
	int UpperLeftY = lpDrawItemStruct->rcItem.top + 
		             ( ( lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top ) / 2 ) - 
					 ( m_IconSize.cy / 2 );

	// render the icon
	pDC->DrawIcon( UpperLeftX, UpperLeftY, hIcon );
}               


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CIconComboBox::ResetBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct, 
									  CBrush *pBrush, CPen *pPen )
{
	// get the device context
    CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

	// reset brush and pen
	pDC->SelectObject( pBrush );
	pDC->SelectObject( pPen );
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CIconComboBox::SetDisabledBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct, 
										    CBrush **ppOldBrush, CPen **ppOldPen )
{
	// get the device context
    CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

	// set brush and pen to light gray
	CBrush brushDisabled( RGB( 192, 192, 192 ) );
	CPen penDisabled( PS_SOLID, 1, RGB( 192, 192, 192 ) );

	// set the brush and pen current -- saving the old brush and pen state
	*ppOldBrush = pDC->SelectObject( &brushDisabled );
	*ppOldPen = pDC->SelectObject( &penDisabled );
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CIconComboBox::SetUnSelectedBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct, 
											  CBrush **ppOldBrush, CPen **ppOldPen )
{
	// get the device context
    CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

	// set the brush and pen "un-highlighted"
	CBrush brushUnSelected( GetSysColor( COLOR_WINDOW ) );
	CPen penUnSelected( PS_SOLID, 1, GetSysColor( COLOR_WINDOW ) );

	// set the brush and pen current -- saving the old brush and pen state
	*ppOldBrush = pDC->SelectObject( &brushUnSelected );
	*ppOldPen = pDC->SelectObject( &penUnSelected );

	//
	// set some addition render state - background  and text color
	//
	pDC->Rectangle( &lpDrawItemStruct->rcItem );
	pDC->SetBkColor( GetSysColor( COLOR_WINDOW ) );
	pDC->SetTextColor( GetSysColor( COLOR_WINDOWTEXT ) );
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CIconComboBox::SetSelectedBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct, 
										    CBrush **ppOldBrush, CPen **ppOldPen )
{
	// get the device context
    CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

	// set the brush and pen "highlighted"
	CBrush brushSelected( GetSysColor( COLOR_HIGHLIGHT ) );
	CPen penSelected( PS_SOLID, 1, GetSysColor( COLOR_HIGHLIGHT ) );

	// set the brush and pen current -- saving the old brush and pen state
	*ppOldBrush = pDC->SelectObject( &brushSelected );
	*ppOldPen = pDC->SelectObject( &penSelected );
	
	//
	// set some addition render state - background  and text color
	//
	pDC->Rectangle( &lpDrawItemStruct->rcItem );
	pDC->SetBkColor( GetSysColor( COLOR_HIGHLIGHT ) );
	pDC->SetTextColor( GetSysColor( COLOR_HIGHLIGHTTEXT ) );
}