BitmapPickerCombo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //----------------------------------------------------------------------------
  2. // N O L D U S I N F O R M A T I O N T E C H N O L O G Y B . V .
  3. //----------------------------------------------------------------------------
  4. // Filename: BitmapPickerCombo.cpp
  5. // Project: EthoVision
  6. // Module: BitmapPicker
  7. // Programmer: Anneke Sicherer-Roetman
  8. // Version: 1.00
  9. // Revision Date: 06-10-1999
  10. //----------------------------------------------------------------------------
  11. // Description: Definition of class CBitmapPickerCombo
  12. // See CBitmapPickerCombo.h
  13. //----------------------------------------------------------------------------
  14. // Acknowledgements: based on Joel Wahlberg's CIconComboBox
  15. // (joel.wahlberg@enator.se)
  16. //----------------------------------------------------------------------------
  17. // Revision history:
  18. // 06-10-1999 - First implementation
  19. //----------------------------------------------------------------------------
  20. // Bugs: ........
  21. //----------------------------------------------------------------------------
  22. // @doc
  23. //----------------------------------------------------------------------------
  24. #include "stdafx.h"
  25. #include "BitmapPickerCombo.h"
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. //----------------------------------------------------------------------------
  32. // Function DrawBitmap
  33. // @func draws bitmap at specified point in specified device context
  34. // @rdesc nothing
  35. // @parm const CBitmap | *bitmap | bitmap to draw
  36. // @parm const CDC | *pDC | device context to draw in
  37. // @parm const CPoint | &point | top left point of bitmap
  38. //----------------------------------------------------------------------------
  39. // @prog
  40. // Anneke Sicherer-Roetman
  41. // @revs
  42. // 06-10-1999 - First implementation
  43. //----------------------------------------------------------------------------
  44. // @todo
  45. //----------------------------------------------------------------------------
  46. static void DrawBitmap(const CBitmap *bitmap, const CDC *pDC, const CPoint &point)
  47. {
  48. BITMAP bm; ((CBitmap*)bitmap)->GetBitmap(&bm);
  49. int w = bm.bmWidth;
  50. int h = bm.bmHeight;
  51. CDC memDC; memDC.CreateCompatibleDC((CDC*)pDC);
  52. CBitmap *pBmp = memDC.SelectObject((CBitmap*)bitmap);
  53. ((CDC*)pDC)->BitBlt(point.x, point.y, w, h, &memDC, 0, 0, SRCCOPY);
  54. memDC.SelectObject(pBmp);
  55. //2011-10-31 add
  56. DeleteObject( memDC );
  57. }
  58. //----------------------------------------------------------------------------
  59. // Function DrawBitmap
  60. // @func draws bitmap centered in specified rectangle in specified device context
  61. // @rdesc nothing
  62. // @parm const CBitmap | *bitmap | bitmap to draw
  63. // @parm const CDC | *pDC | device context to draw in
  64. // @parm const CRect | &rect | rectangle to center in
  65. //----------------------------------------------------------------------------
  66. // @prog
  67. // Anneke Sicherer-Roetman
  68. // @revs
  69. // 06-10-1999 - First implementation
  70. //----------------------------------------------------------------------------
  71. // @todo
  72. //----------------------------------------------------------------------------
  73. static void DrawBitmap(const CBitmap *bitmap, const CDC *pDC, const CRect &rect)
  74. {
  75. BITMAP bm; ((CBitmap*)bitmap)->GetBitmap(&bm);
  76. int w = bm.bmWidth;
  77. int h = bm.bmHeight;
  78. CPoint point;
  79. point.x = rect.left + ((rect.right - rect.left) / 2) - (w / 2);
  80. point.y = rect.top + ((rect.bottom - rect.top) / 2) - (h / 2);
  81. DrawBitmap(bitmap, pDC, point);
  82. }
  83. //----------------------------------------------------------------------------
  84. // Function CBitmapPickerCombo::CBitmapPickerCombo
  85. // @mfunc constructor
  86. // @xref <c CBitmapPickerCombo>
  87. //----------------------------------------------------------------------------
  88. // @prog
  89. // Anneke Sicherer-Roetman
  90. // @revs
  91. // 06-10-1999 - First implementation
  92. //----------------------------------------------------------------------------
  93. // @todo
  94. //----------------------------------------------------------------------------
  95. CBitmapPickerCombo::CBitmapPickerCombo():
  96. CComboBox(),
  97. m_nItemWidth(0),
  98. m_nItemHeight(0)
  99. {
  100. }
  101. //----------------------------------------------------------------------------
  102. // Function CBitmapPickerCombo::AddBitmap
  103. // @mfunc adds bitmap (and string) item to combobox
  104. // @rdesc index of item (-1 on failure) (int)
  105. // @parm const CBitmap | *bitmap | bitmap to add
  106. // @parm const char | *string | string to add (default NULL)
  107. // @xref <c CBitmapPickerCombo>
  108. //----------------------------------------------------------------------------
  109. // @prog
  110. // Anneke Sicherer-Roetman
  111. // @revs
  112. // 06-10-1999 - First implementation
  113. //----------------------------------------------------------------------------
  114. // @todo
  115. //----------------------------------------------------------------------------
  116. int CBitmapPickerCombo::AddBitmap(const CBitmap *bitmap, const char *string)
  117. {
  118. return InsertBitmap(GetCount(), bitmap, string);
  119. }
  120. //----------------------------------------------------------------------------
  121. // Function CBitmapPickerCombo::InsertBitmap
  122. // @mfunc adds bitmap (and string) item to combobox at specified index
  123. // @rdesc index of item (-1 on failure) (int)
  124. // @parm int | nIndex | index at which to insert
  125. // @parm const CBitmap | *bitmap | bitmap to add
  126. // @parm const char | *string | string to add (default NULL)
  127. // @xref <c CBitmapPickerCombo>
  128. //----------------------------------------------------------------------------
  129. // @prog
  130. // Anneke Sicherer-Roetman <nl>
  131. // after: Icon Picker Combo Box by Joel Wahlberg <nl>
  132. // http://www.codeguru.com/combobox/icon_combobox.shtml
  133. // @revs
  134. // 06-10-1999 - First implementation
  135. //----------------------------------------------------------------------------
  136. // @todo
  137. //----------------------------------------------------------------------------
  138. int CBitmapPickerCombo::InsertBitmap(int nIndex, const CBitmap *bitmap, const char *string)
  139. {
  140. int n = CComboBox::InsertString(nIndex, string ? string : "");
  141. if (n != CB_ERR && n != CB_ERRSPACE) {
  142. SetItemData(n, (DWORD)bitmap);
  143. BITMAP bm;
  144. ((CBitmap*)bitmap)->GetBitmap(&bm);
  145. SetSize(bm.bmWidth, bm.bmHeight);
  146. }
  147. return n;
  148. }
  149. //----------------------------------------------------------------------------
  150. // Function CBitmapPickerCombo::MeasureItem
  151. // @mfunc Called by MFC when combo box is created
  152. // @rdesc nothing
  153. // @parm LPMEASUREITEMSTRUCT | lpMIS | standard parameter
  154. // @xref <c CBitmapPickerCombo>
  155. //----------------------------------------------------------------------------
  156. // @prog
  157. // Anneke Sicherer-Roetman
  158. // @revs
  159. // 06-10-1999 - First implementation
  160. //----------------------------------------------------------------------------
  161. // @todo
  162. //----------------------------------------------------------------------------
  163. void CBitmapPickerCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  164. {
  165. lpMIS->itemWidth = (m_nItemWidth + 1);
  166. lpMIS->itemHeight = (m_nItemHeight + 1);
  167. }
  168. //----------------------------------------------------------------------------
  169. // Function CBitmapPickerCombo::DrawItem
  170. // @mfunc Called by MFC when visual aspect of combo box changes
  171. // @rdesc nothing
  172. // @parm LPDRAWITEMSTRUCT | lpDIS | standard parameter
  173. // @xref <c CBitmapPickerCombo>
  174. //----------------------------------------------------------------------------
  175. // @prog
  176. // Anneke Sicherer-Roetman <nl>
  177. // after: Icon Picker Combo Box by Joel Wahlberg <nl>
  178. // http://www.codeguru.com/combobox/icon_combobox.shtml
  179. // @revs
  180. // 06-10-1999 - First implementation
  181. //----------------------------------------------------------------------------
  182. // @todo
  183. //----------------------------------------------------------------------------
  184. void CBitmapPickerCombo::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  185. {
  186. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  187. if (!IsWindowEnabled()) {
  188. CBrush brDisabled(RGB(192,192,192)); // light gray
  189. CBrush* pOldBrush = pDC->SelectObject(&brDisabled);
  190. CPen penDisabled(PS_SOLID, 1, RGB(192,192,192));
  191. CPen* pOldPen = pDC->SelectObject(&penDisabled);
  192. OutputBitmap(lpDIS, false);
  193. pDC->SelectObject(pOldBrush);
  194. pDC->SelectObject(pOldPen);
  195. DeleteObject( brDisabled );
  196. DeleteObject( penDisabled );
  197. return;
  198. }
  199. // Selected
  200. if ((lpDIS->itemState & ODS_SELECTED)
  201. && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) {
  202. CBrush brHighlight(::GetSysColor(COLOR_HIGHLIGHT));
  203. CBrush* pOldBrush = pDC->SelectObject(&brHighlight);
  204. CPen penHighlight(PS_SOLID, 1, ::GetSysColor(COLOR_HIGHLIGHT));
  205. CPen* pOldPen = pDC->SelectObject(&penHighlight);
  206. pDC->Rectangle(&lpDIS->rcItem);
  207. pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  208. pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  209. OutputBitmap(lpDIS, true);
  210. pDC->SelectObject(pOldBrush);
  211. pDC->SelectObject(pOldPen);
  212. DeleteObject( brHighlight );
  213. DeleteObject( penHighlight );
  214. }
  215. // De-Selected
  216. if (!(lpDIS->itemState & ODS_SELECTED)
  217. && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) {
  218. CBrush brWindow(::GetSysColor(COLOR_WINDOW));
  219. CBrush* pOldBrush = pDC->SelectObject(&brWindow);
  220. CPen penHighlight(PS_SOLID, 1, ::GetSysColor(COLOR_WINDOW));
  221. CPen* pOldPen = pDC->SelectObject(&penHighlight);
  222. pDC->Rectangle(&lpDIS->rcItem);
  223. pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  224. pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
  225. OutputBitmap(lpDIS, false);
  226. pDC->SelectObject(pOldBrush);
  227. pDC->SelectObject(pOldPen);
  228. DeleteObject( brWindow );
  229. DeleteObject( penHighlight );
  230. }
  231. // Focus
  232. if (lpDIS->itemAction & ODA_FOCUS)
  233. pDC->DrawFocusRect(&lpDIS->rcItem);
  234. }
  235. //----------------------------------------------------------------------------
  236. // Function CBitmapPickerCombo::OutputBitmap
  237. // @mfunc draws bitmap (and string) in item
  238. // @rdesc nothing
  239. // @parm LPDRAWITEMSTRUCT | lpDIS | item data
  240. // @parm bool | selected | is the item selected?
  241. // @xref <c CBitmapPickerCombo>
  242. //----------------------------------------------------------------------------
  243. // @prog
  244. // Anneke Sicherer-Roetman <nl>
  245. // after: Icon Picker Combo Box by Joel Wahlberg <nl>
  246. // http://www.codeguru.com/combobox/icon_combobox.shtml
  247. // @revs
  248. // 06-10-1999 - First implementation
  249. //----------------------------------------------------------------------------
  250. // @todo
  251. //----------------------------------------------------------------------------
  252. void CBitmapPickerCombo::OutputBitmap(LPDRAWITEMSTRUCT lpDIS, bool selected)
  253. {
  254. const CBitmap *bitmap = (const CBitmap*)(lpDIS->itemData);
  255. if (bitmap && bitmap != (const CBitmap *)(0xffffffff)) {
  256. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  257. CString string;
  258. if (lpDIS->itemID != -1)
  259. GetLBText(lpDIS->itemID, string);
  260. if (string.IsEmpty())
  261. DrawBitmap(bitmap, pDC, lpDIS->rcItem);
  262. else {
  263. CPoint point;
  264. point.x = lpDIS->rcItem.left + 1;
  265. point.y = lpDIS->rcItem.top + ((lpDIS->rcItem.bottom - lpDIS->rcItem.top) / 2) - (m_nItemHeight / 2);
  266. DrawBitmap(bitmap, pDC, point);
  267. CRect rcText(lpDIS->rcItem);
  268. rcText.DeflateRect(m_nItemWidth + 2, 0, 0, 0);
  269. pDC->DrawText(string, rcText, DT_SINGLELINE |DT_VCENTER );
  270. }
  271. }
  272. }
  273. //----------------------------------------------------------------------------
  274. // Function CBitmapPickerCombo::SetSize
  275. // @mfunc sets overall item size
  276. // @rdesc nothing
  277. // @parm int | width | width of item
  278. // @parm int | height | height of item
  279. // @xref <c CBitmapPickerCombo>
  280. //----------------------------------------------------------------------------
  281. // @prog
  282. // Anneke Sicherer-Roetman
  283. // @revs
  284. // 06-10-1999 - First implementation
  285. //----------------------------------------------------------------------------
  286. // @todo
  287. //----------------------------------------------------------------------------
  288. void CBitmapPickerCombo::SetSize(int width, int height)
  289. {
  290. if (width > m_nItemWidth)
  291. m_nItemWidth = width;
  292. if (height > m_nItemHeight)
  293. m_nItemHeight = height;
  294. for (int i = -1; i < GetCount(); i++)
  295. SetItemHeight(i, m_nItemHeight + 2);
  296. }
  297. //----------------------------------------------------------------------------
  298. #ifdef _DEBUG
  299. void CBitmapPickerCombo::PreSubclassWindow()
  300. {
  301. CComboBox::PreSubclassWindow();
  302. // ensure some styles are set
  303. // modifying style here has NO effect!?!
  304. ASSERT(GetStyle() & CBS_DROPDOWNLIST);
  305. ASSERT(GetStyle() & CBS_OWNERDRAWVARIABLE);
  306. ASSERT(GetStyle() & CBS_HASSTRINGS);
  307. }
  308. #endif
  309. //----------------------------------------------------------------------------