BitmapPickerCombo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. }
  56. //----------------------------------------------------------------------------
  57. // Function DrawBitmap
  58. // @func draws bitmap centered in specified rectangle in specified device context
  59. // @rdesc nothing
  60. // @parm const CBitmap | *bitmap | bitmap to draw
  61. // @parm const CDC | *pDC | device context to draw in
  62. // @parm const CRect | &rect | rectangle to center in
  63. //----------------------------------------------------------------------------
  64. // @prog
  65. // Anneke Sicherer-Roetman
  66. // @revs
  67. // 06-10-1999 - First implementation
  68. //----------------------------------------------------------------------------
  69. // @todo
  70. //----------------------------------------------------------------------------
  71. static void DrawBitmap(const CBitmap *bitmap, const CDC *pDC, const CRect &rect)
  72. {
  73. BITMAP bm; ((CBitmap*)bitmap)->GetBitmap(&bm);
  74. int w = bm.bmWidth;
  75. int h = bm.bmHeight;
  76. CPoint point;
  77. point.x = rect.left + ((rect.right - rect.left) / 2) - (w / 2);
  78. point.y = rect.top + ((rect.bottom - rect.top) / 2) - (h / 2);
  79. DrawBitmap(bitmap, pDC, point);
  80. }
  81. //----------------------------------------------------------------------------
  82. // Function CBitmapPickerCombo::CBitmapPickerCombo
  83. // @mfunc constructor
  84. // @xref <c CBitmapPickerCombo>
  85. //----------------------------------------------------------------------------
  86. // @prog
  87. // Anneke Sicherer-Roetman
  88. // @revs
  89. // 06-10-1999 - First implementation
  90. //----------------------------------------------------------------------------
  91. // @todo
  92. //----------------------------------------------------------------------------
  93. CBitmapPickerCombo::CBitmapPickerCombo():
  94. CComboBox(),
  95. m_nItemWidth(0),
  96. m_nItemHeight(0)
  97. {
  98. }
  99. //----------------------------------------------------------------------------
  100. // Function CBitmapPickerCombo::AddBitmap
  101. // @mfunc adds bitmap (and string) item to combobox
  102. // @rdesc index of item (-1 on failure) (int)
  103. // @parm const CBitmap | *bitmap | bitmap to add
  104. // @parm const char | *string | string to add (default NULL)
  105. // @xref <c CBitmapPickerCombo>
  106. //----------------------------------------------------------------------------
  107. // @prog
  108. // Anneke Sicherer-Roetman
  109. // @revs
  110. // 06-10-1999 - First implementation
  111. //----------------------------------------------------------------------------
  112. // @todo
  113. //----------------------------------------------------------------------------
  114. int CBitmapPickerCombo::AddBitmap(const CBitmap *bitmap, const char *string)
  115. {
  116. return InsertBitmap(GetCount(), bitmap, string);
  117. }
  118. //----------------------------------------------------------------------------
  119. // Function CBitmapPickerCombo::InsertBitmap
  120. // @mfunc adds bitmap (and string) item to combobox at specified index
  121. // @rdesc index of item (-1 on failure) (int)
  122. // @parm int | nIndex | index at which to insert
  123. // @parm const CBitmap | *bitmap | bitmap to add
  124. // @parm const char | *string | string to add (default NULL)
  125. // @xref <c CBitmapPickerCombo>
  126. //----------------------------------------------------------------------------
  127. // @prog
  128. // Anneke Sicherer-Roetman <nl>
  129. // after: Icon Picker Combo Box by Joel Wahlberg <nl>
  130. // http://www.codeguru.com/combobox/icon_combobox.shtml
  131. // @revs
  132. // 06-10-1999 - First implementation
  133. //----------------------------------------------------------------------------
  134. // @todo
  135. //----------------------------------------------------------------------------
  136. int CBitmapPickerCombo::InsertBitmap(int nIndex, const CBitmap *bitmap, const char *string)
  137. {
  138. int n = CComboBox::InsertString(nIndex, string ? string : "");
  139. if (n != CB_ERR && n != CB_ERRSPACE) {
  140. SetItemData(n, (DWORD)bitmap);
  141. BITMAP bm;
  142. ((CBitmap*)bitmap)->GetBitmap(&bm);
  143. SetSize(bm.bmWidth, bm.bmHeight);
  144. }
  145. return n;
  146. }
  147. //----------------------------------------------------------------------------
  148. // Function CBitmapPickerCombo::MeasureItem
  149. // @mfunc Called by MFC when combo box is created
  150. // @rdesc nothing
  151. // @parm LPMEASUREITEMSTRUCT | lpMIS | standard parameter
  152. // @xref <c CBitmapPickerCombo>
  153. //----------------------------------------------------------------------------
  154. // @prog
  155. // Anneke Sicherer-Roetman
  156. // @revs
  157. // 06-10-1999 - First implementation
  158. //----------------------------------------------------------------------------
  159. // @todo
  160. //----------------------------------------------------------------------------
  161. void CBitmapPickerCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  162. {
  163. lpMIS->itemWidth = (m_nItemWidth + 1);
  164. lpMIS->itemHeight = (m_nItemHeight + 1);
  165. }
  166. //----------------------------------------------------------------------------
  167. // Function CBitmapPickerCombo::DrawItem
  168. // @mfunc Called by MFC when visual aspect of combo box changes
  169. // @rdesc nothing
  170. // @parm LPDRAWITEMSTRUCT | lpDIS | standard parameter
  171. // @xref <c CBitmapPickerCombo>
  172. //----------------------------------------------------------------------------
  173. // @prog
  174. // Anneke Sicherer-Roetman <nl>
  175. // after: Icon Picker Combo Box by Joel Wahlberg <nl>
  176. // http://www.codeguru.com/combobox/icon_combobox.shtml
  177. // @revs
  178. // 06-10-1999 - First implementation
  179. //----------------------------------------------------------------------------
  180. // @todo
  181. //----------------------------------------------------------------------------
  182. void CBitmapPickerCombo::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  183. {
  184. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  185. if (!IsWindowEnabled()) {
  186. CBrush brDisabled(RGB(192,192,192)); // light gray
  187. CBrush* pOldBrush = pDC->SelectObject(&brDisabled);
  188. CPen penDisabled(PS_SOLID, 1, RGB(192,192,192));
  189. CPen* pOldPen = pDC->SelectObject(&penDisabled);
  190. OutputBitmap(lpDIS, false);
  191. pDC->SelectObject(pOldBrush);
  192. pDC->SelectObject(pOldPen);
  193. return;
  194. }
  195. // Selected
  196. if ((lpDIS->itemState & ODS_SELECTED)
  197. && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) {
  198. CBrush brHighlight(::GetSysColor(COLOR_HIGHLIGHT));
  199. CBrush* pOldBrush = pDC->SelectObject(&brHighlight);
  200. CPen penHighlight(PS_SOLID, 1, ::GetSysColor(COLOR_HIGHLIGHT));
  201. CPen* pOldPen = pDC->SelectObject(&penHighlight);
  202. pDC->Rectangle(&lpDIS->rcItem);
  203. pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  204. pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  205. OutputBitmap(lpDIS, true);
  206. pDC->SelectObject(pOldBrush);
  207. pDC->SelectObject(pOldPen);
  208. }
  209. // De-Selected
  210. if (!(lpDIS->itemState & ODS_SELECTED)
  211. && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) {
  212. CBrush brWindow(::GetSysColor(COLOR_WINDOW));
  213. CBrush* pOldBrush = pDC->SelectObject(&brWindow);
  214. CPen penHighlight(PS_SOLID, 1, ::GetSysColor(COLOR_WINDOW));
  215. CPen* pOldPen = pDC->SelectObject(&penHighlight);
  216. pDC->Rectangle(&lpDIS->rcItem);
  217. pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  218. pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
  219. OutputBitmap(lpDIS, false);
  220. pDC->SelectObject(pOldBrush);
  221. pDC->SelectObject(pOldPen);
  222. }
  223. // Focus
  224. if (lpDIS->itemAction & ODA_FOCUS)
  225. pDC->DrawFocusRect(&lpDIS->rcItem);
  226. }
  227. //----------------------------------------------------------------------------
  228. // Function CBitmapPickerCombo::OutputBitmap
  229. // @mfunc draws bitmap (and string) in item
  230. // @rdesc nothing
  231. // @parm LPDRAWITEMSTRUCT | lpDIS | item data
  232. // @parm bool | selected | is the item selected?
  233. // @xref <c CBitmapPickerCombo>
  234. //----------------------------------------------------------------------------
  235. // @prog
  236. // Anneke Sicherer-Roetman <nl>
  237. // after: Icon Picker Combo Box by Joel Wahlberg <nl>
  238. // http://www.codeguru.com/combobox/icon_combobox.shtml
  239. // @revs
  240. // 06-10-1999 - First implementation
  241. //----------------------------------------------------------------------------
  242. // @todo
  243. //----------------------------------------------------------------------------
  244. void CBitmapPickerCombo::OutputBitmap(LPDRAWITEMSTRUCT lpDIS, bool selected)
  245. {
  246. const CBitmap *bitmap = (const CBitmap*)(lpDIS->itemData);
  247. if (bitmap && bitmap != (const CBitmap *)(0xffffffff)) {
  248. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  249. CString string;
  250. if (lpDIS->itemID != -1)
  251. GetLBText(lpDIS->itemID, string);
  252. if (string.IsEmpty())
  253. DrawBitmap(bitmap, pDC, lpDIS->rcItem);
  254. else {
  255. CPoint point;
  256. point.x = lpDIS->rcItem.left + 1;
  257. point.y = lpDIS->rcItem.top + ((lpDIS->rcItem.bottom - lpDIS->rcItem.top) / 2) - (m_nItemHeight / 2);
  258. DrawBitmap(bitmap, pDC, point);
  259. CRect rcText(lpDIS->rcItem);
  260. rcText.DeflateRect(m_nItemWidth + 2, 0, 0, 0);
  261. pDC->DrawText(string, rcText, DT_SINGLELINE |DT_VCENTER );
  262. }
  263. }
  264. }
  265. //----------------------------------------------------------------------------
  266. // Function CBitmapPickerCombo::SetSize
  267. // @mfunc sets overall item size
  268. // @rdesc nothing
  269. // @parm int | width | width of item
  270. // @parm int | height | height of item
  271. // @xref <c CBitmapPickerCombo>
  272. //----------------------------------------------------------------------------
  273. // @prog
  274. // Anneke Sicherer-Roetman
  275. // @revs
  276. // 06-10-1999 - First implementation
  277. //----------------------------------------------------------------------------
  278. // @todo
  279. //----------------------------------------------------------------------------
  280. void CBitmapPickerCombo::SetSize(int width, int height)
  281. {
  282. if (width > m_nItemWidth)
  283. m_nItemWidth = width;
  284. if (height > m_nItemHeight)
  285. m_nItemHeight = height;
  286. for (int i = -1; i < GetCount(); i++)
  287. SetItemHeight(i, m_nItemHeight + 2);
  288. }
  289. //----------------------------------------------------------------------------
  290. #ifdef _DEBUG
  291. void CBitmapPickerCombo::PreSubclassWindow()
  292. {
  293. CComboBox::PreSubclassWindow();
  294. // ensure some styles are set
  295. // modifying style here has NO effect!?!
  296. ASSERT(GetStyle() & CBS_DROPDOWNLIST);
  297. ASSERT(GetStyle() & CBS_OWNERDRAWVARIABLE);
  298. ASSERT(GetStyle() & CBS_HASSTRINGS);
  299. }
  300. #endif
  301. //----------------------------------------------------------------------------