Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
11 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdafx.h>
  8. #include "IconComboBox.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include <tier0/memdbgon.h>
  11. //-----------------------------------------------------------------------------
  12. // Purpose: Constructor
  13. //-----------------------------------------------------------------------------
  14. CIconComboBox::CIconComboBox()
  15. {
  16. }
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Deconstructor
  19. //-----------------------------------------------------------------------------
  20. CIconComboBox::~CIconComboBox()
  21. {
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. void CIconComboBox::Init( void )
  27. {
  28. // initialize the icon size
  29. m_IconSize.cx = GetSystemMetrics( SM_CXICON );
  30. m_IconSize.cy = GetSystemMetrics( SM_CYICON );
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose:
  34. //-----------------------------------------------------------------------------
  35. int CIconComboBox::AddIcon( LPCTSTR pIconName )
  36. {
  37. //
  38. // create/load an icon from file
  39. //
  40. // NULL - no icons in file
  41. // 1 - not a proper icon file
  42. //
  43. HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pIconName, 0 );
  44. if( ( hIcon == ( HICON )1 ) || !hIcon )
  45. return CB_ERR;
  46. //
  47. // add the icon to the combo box - returning the index
  48. //
  49. // CB_ERR - general error adding icon
  50. // CB_ERRSPACE - insufficient space necessary to add icon
  51. //
  52. int ndx = CComboBox::AddString( pIconName );
  53. if( ( ndx == CB_ERR ) || ( ndx == CB_ERRSPACE ) )
  54. return ndx;
  55. //
  56. // associate the icon with the index
  57. //
  58. // CB_ERR - general error
  59. //
  60. int result = SetItemData( ndx, ( DWORD )hIcon );
  61. if( result == CB_ERR )
  62. return result;
  63. // return the icon index
  64. return ndx;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose:
  68. //-----------------------------------------------------------------------------
  69. int CIconComboBox::InsertIcon( LPCTSTR pIconName, int ndx )
  70. {
  71. //
  72. // create an icon from file
  73. //
  74. // NULL - no icons in file
  75. // 1 - not a proper icon file
  76. //
  77. HICON hIcon = ExtractIcon( AfxGetInstanceHandle(), pIconName, 0 );
  78. if( ( hIcon == ( HICON )1 ) || !hIcon )
  79. return CB_ERR;
  80. //
  81. // insert the icon into the combo box -- returning the index
  82. //
  83. // CB_ERR - general error adding icon
  84. // CB_ERRSPACE - insufficient space necessary to add icon
  85. //
  86. int result = CComboBox::InsertString( ndx, pIconName );
  87. if( ( result == CB_ERR ) || ( result == CB_ERRSPACE ) )
  88. return result;
  89. //
  90. // associate the icon with the index
  91. //
  92. // CB_ERR - general error
  93. //
  94. result = SetItemData( ndx, ( DWORD )hIcon );
  95. if( result == CB_ERR )
  96. return result;
  97. // return the icon index
  98. return ndx;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose:
  102. //-----------------------------------------------------------------------------
  103. int CIconComboBox::SelectIcon( LPCTSTR pIconName )
  104. {
  105. //
  106. // search the combo box list for the given string, -1 = search the whole list
  107. //
  108. // CB_ERR - unsuccessful search
  109. //
  110. int ndx = CComboBox::FindStringExact( -1, pIconName );
  111. if( ndx == CB_ERR )
  112. return CB_ERR;
  113. // set the selection current
  114. return CComboBox::SetCurSel( ndx );
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose:
  118. //-----------------------------------------------------------------------------
  119. int CIconComboBox::SelectIcon( int ndx )
  120. {
  121. // set the selection current
  122. return CComboBox::SetCurSel( ndx );
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose:
  126. //-----------------------------------------------------------------------------
  127. int CIconComboBox::DeleteIcon( LPCTSTR pIconName )
  128. {
  129. //
  130. // search the combo box list for the given string, -1 = search the whole list
  131. //
  132. // CB_ERR - unsuccessful search
  133. //
  134. int ndx = CComboBox::FindStringExact( -1, pIconName );
  135. if( ndx == CB_ERR )
  136. return CB_ERR;
  137. // remove the icon from the combo box
  138. return CComboBox::DeleteString( ndx );
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. int CIconComboBox::DeleteIcon( int ndx )
  144. {
  145. // remove the icon from the combo box
  146. return CComboBox::DeleteString( ndx );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose: don't allow the icon combo box to "AddString"
  150. //-----------------------------------------------------------------------------
  151. int CIconComboBox::AddString( LPCTSTR lpszString )
  152. {
  153. assert( FALSE );
  154. return CB_ERR;
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Purpose: don't allow the icon combo box to "InsertString"
  158. //-----------------------------------------------------------------------------
  159. int CIconComboBox::InsertString( int nIndex, LPCTSTR lpszString )
  160. {
  161. assert( FALSE );
  162. return CB_ERR;
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose: don't allow the icon combo box to "DeleteString"
  166. //-----------------------------------------------------------------------------
  167. int CIconComboBox::DeleteString( int nIndex )
  168. {
  169. assert( FALSE );
  170. return CB_ERR;
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Purpose:
  174. //-----------------------------------------------------------------------------
  175. void CIconComboBox::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
  176. {
  177. lpMeasureItemStruct->itemWidth = m_IconSize.cx;
  178. lpMeasureItemStruct->itemHeight = m_IconSize.cy + 1;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose:
  182. //-----------------------------------------------------------------------------
  183. void CIconComboBox::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
  184. {
  185. CBrush *pOldBrush = NULL;
  186. CPen *pOldPen = NULL;
  187. //
  188. // the icon is "disabled"
  189. //
  190. if( !IsWindowEnabled() )
  191. {
  192. SetDisabledBrushAndPen( lpDrawItemStruct, &pOldBrush, &pOldPen );
  193. OnDrawIcon( lpDrawItemStruct );
  194. ResetBrushAndPen( lpDrawItemStruct, pOldBrush, pOldPen );
  195. return;
  196. }
  197. //
  198. // the icon is "selected"
  199. //
  200. if( ( lpDrawItemStruct->itemState & ODS_SELECTED ) &&
  201. ( lpDrawItemStruct->itemAction & ( ODA_SELECT | ODA_DRAWENTIRE ) ) )
  202. {
  203. SetSelectedBrushAndPen( lpDrawItemStruct, &pOldBrush, &pOldPen );
  204. OnDrawIcon( lpDrawItemStruct );
  205. ResetBrushAndPen( lpDrawItemStruct, pOldBrush, pOldPen );
  206. }
  207. //
  208. // the icon is "un-selected"
  209. //
  210. if( !( lpDrawItemStruct->itemState & ODS_SELECTED ) &&
  211. ( lpDrawItemStruct->itemAction & ( ODA_SELECT | ODA_DRAWENTIRE ) ) )
  212. {
  213. SetUnSelectedBrushAndPen( lpDrawItemStruct, &pOldBrush, &pOldPen );
  214. OnDrawIcon( lpDrawItemStruct );
  215. ResetBrushAndPen( lpDrawItemStruct, pOldBrush, pOldPen );
  216. }
  217. //
  218. // icon gains focus
  219. //
  220. if( lpDrawItemStruct->itemAction & ODA_FOCUS )
  221. {
  222. // get the device context
  223. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  224. // render the focus rectangle
  225. pDC->DrawFocusRect( &lpDrawItemStruct->rcItem );
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. //-----------------------------------------------------------------------------
  230. void CIconComboBox::OnDrawIcon( LPDRAWITEMSTRUCT lpDrawItemStruct )
  231. {
  232. // any items to draw?
  233. if( GetCount() == 0 )
  234. return;
  235. // get the device context - to draw
  236. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  237. // get the current icon to render
  238. HICON hIcon = ( HICON )lpDrawItemStruct->itemData;
  239. if( !hIcon )
  240. return;
  241. // calculate the icon's upper left corner
  242. int UpperLeftX = lpDrawItemStruct->rcItem.left +
  243. ( ( lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left ) / 2 ) -
  244. ( m_IconSize.cx / 2 );
  245. int UpperLeftY = lpDrawItemStruct->rcItem.top +
  246. ( ( lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top ) / 2 ) -
  247. ( m_IconSize.cy / 2 );
  248. // render the icon
  249. pDC->DrawIcon( UpperLeftX, UpperLeftY, hIcon );
  250. }
  251. //-----------------------------------------------------------------------------
  252. //-----------------------------------------------------------------------------
  253. void CIconComboBox::ResetBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct,
  254. CBrush *pBrush, CPen *pPen )
  255. {
  256. // get the device context
  257. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  258. // reset brush and pen
  259. pDC->SelectObject( pBrush );
  260. pDC->SelectObject( pPen );
  261. }
  262. //-----------------------------------------------------------------------------
  263. //-----------------------------------------------------------------------------
  264. void CIconComboBox::SetDisabledBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct,
  265. CBrush **ppOldBrush, CPen **ppOldPen )
  266. {
  267. // get the device context
  268. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  269. // set brush and pen to light gray
  270. CBrush brushDisabled( RGB( 192, 192, 192 ) );
  271. CPen penDisabled( PS_SOLID, 1, RGB( 192, 192, 192 ) );
  272. // set the brush and pen current -- saving the old brush and pen state
  273. *ppOldBrush = pDC->SelectObject( &brushDisabled );
  274. *ppOldPen = pDC->SelectObject( &penDisabled );
  275. }
  276. //-----------------------------------------------------------------------------
  277. //-----------------------------------------------------------------------------
  278. void CIconComboBox::SetUnSelectedBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct,
  279. CBrush **ppOldBrush, CPen **ppOldPen )
  280. {
  281. // get the device context
  282. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  283. // set the brush and pen "un-highlighted"
  284. CBrush brushUnSelected( GetSysColor( COLOR_WINDOW ) );
  285. CPen penUnSelected( PS_SOLID, 1, GetSysColor( COLOR_WINDOW ) );
  286. // set the brush and pen current -- saving the old brush and pen state
  287. *ppOldBrush = pDC->SelectObject( &brushUnSelected );
  288. *ppOldPen = pDC->SelectObject( &penUnSelected );
  289. //
  290. // set some addition render state - background and text color
  291. //
  292. pDC->Rectangle( &lpDrawItemStruct->rcItem );
  293. pDC->SetBkColor( GetSysColor( COLOR_WINDOW ) );
  294. pDC->SetTextColor( GetSysColor( COLOR_WINDOWTEXT ) );
  295. }
  296. //-----------------------------------------------------------------------------
  297. //-----------------------------------------------------------------------------
  298. void CIconComboBox::SetSelectedBrushAndPen( LPDRAWITEMSTRUCT lpDrawItemStruct,
  299. CBrush **ppOldBrush, CPen **ppOldPen )
  300. {
  301. // get the device context
  302. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  303. // set the brush and pen "highlighted"
  304. CBrush brushSelected( GetSysColor( COLOR_HIGHLIGHT ) );
  305. CPen penSelected( PS_SOLID, 1, GetSysColor( COLOR_HIGHLIGHT ) );
  306. // set the brush and pen current -- saving the old brush and pen state
  307. *ppOldBrush = pDC->SelectObject( &brushSelected );
  308. *ppOldPen = pDC->SelectObject( &penSelected );
  309. //
  310. // set some addition render state - background and text color
  311. //
  312. pDC->Rectangle( &lpDrawItemStruct->rcItem );
  313. pDC->SetBkColor( GetSysColor( COLOR_HIGHLIGHT ) );
  314. pDC->SetTextColor( GetSysColor( COLOR_HIGHLIGHTTEXT ) );
  315. }