home *** CD-ROM | disk | FTP | other *** search
/ Computer Buyer 1998 October / dpcb1098.iso / Business / Maxim / MAX5 / data.z / OwnComboBox.cpp < prev    next >
C/C++ Source or Header  |  1998-05-15  |  8KB  |  238 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // NAME.......: OwnComboBox.CPP                                               
  3. // PURPOSE....: Ownerdrawn combo (bmp and text) box for displaying 
  4. //              appointment types and assoc. bmps  (page 3)
  5. // WRITTEN....: 96/09/27 by Darko Juvan
  6. // DESCRIPTION: owner drawn combo box
  7. //
  8. // This code and information is provided "as is" without warranty of any
  9. // kind, either expressed or implied, including but not limited to the
  10. // implied warranties of merchantability and/or fitness for a particular
  11. // purpose..
  12. //
  13. // Copyright (c) 1998  Multiactive Software Inc.  All Rights Reserved.
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16.  
  17. #include "stdafx.h"
  18. #include "resource.h"
  19. #include "OwnComboBox.h"
  20. #include "TransBlt.h"
  21.  
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. BEGIN_MESSAGE_MAP(CBmpCombo, CComboBox)
  28.     //{{AFX_MSG_MAP(CBmpCombo)
  29.     //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31.  
  32.  
  33. #define new DEBUG_NEW
  34.  
  35.  
  36. //////////////////////////////////////////////////////////////////////////////
  37. //                        
  38. // FUNCTION...: LoadBitmap()
  39. //                                                  
  40. // DESCRIPTION: load item bmp
  41. //
  42. //////////////////////////////////////////////////////////////////////////////
  43. void CBmpComboData::LoadBitmap()
  44. {
  45.     VERIFY (m_bmp.LoadBitmap(m_nBitmapID));
  46.     BITMAP bmpStruct;
  47.     m_bmp.GetObject (sizeof(BITMAP), &bmpStruct);
  48.  
  49.     m_size.cx = bmpStruct.bmWidth;
  50.     m_size.cy = bmpStruct.bmHeight;
  51. }
  52.  
  53. //////////////////////////////////////////////////////////////////////////////
  54. //                        
  55. // FUNCTION...: GetSize()
  56. //                                                  
  57. // DESCRIPTION: return size of bmp
  58. //
  59. //////////////////////////////////////////////////////////////////////////////
  60. CSize CBmpComboData::GetSize()
  61. {
  62.     ASSERT (m_nBitmapID != 0);
  63.  
  64.     if (m_bmp.m_hObject == NULL)
  65.         LoadBitmap();
  66.  
  67.     return m_size;
  68. }
  69.  
  70. //////////////////////////////////////////////////////////////////////////////
  71. //                        
  72. // FUNCTION...: Focus ()
  73. //                                                  
  74. // DESCRIPTION: draw focus state
  75. //
  76. //////////////////////////////////////////////////////////////////////////////
  77. void CBmpComboData::Focus (CDC* pDC, const CRect& rcDraw, BOOL bAddFocus)
  78. {
  79.     // We can ignore bAddFocus since DrawFocusRect uses XOR
  80. }
  81.  
  82. //////////////////////////////////////////////////////////////////////////////
  83. //                        
  84. // FUNCTION...: DrawItem ()
  85. //                                                  
  86. // DESCRIPTION: draw item (bmp and text)
  87. //
  88. //////////////////////////////////////////////////////////////////////////////
  89. void CBmpComboData::DrawItem (CDC* pDC, const CRect& rcDraw, BOOL bSelected)
  90. {
  91.     CRect rc(rcDraw);
  92.  
  93.     if (m_bmp.m_hObject == NULL)
  94.         LoadBitmap();
  95.  
  96.     CBrush br;
  97.     CRect rc2(rcDraw);
  98.     rc2.InflateRect(-1,-1);
  99.     if (bSelected)
  100.     {
  101.         br.CreateSolidBrush(GetSysColor (COLOR_HIGHLIGHT));
  102.         pDC->FrameRect(&rc2, &br);    
  103.     } 
  104.     else 
  105.     {
  106.         br.CreateSolidBrush(GetSysColor (COLOR_WINDOW));
  107.         pDC->FrameRect(&rc2, &br);    
  108.     }
  109.  
  110.     // Draw the bitmap
  111.     TransparentBlt(pDC->GetSafeHdc(), (HBITMAP) m_bmp.GetSafeHandle(), 
  112.         rc.left+5, rc.top+4);
  113.  
  114.     // Shift the rectangle to the left to account for the space
  115.     // used by blitting the bitmap
  116.     rc.left += m_size.cx + 15;
  117.  
  118.     // Draw the text
  119.     pDC->DrawText (m_sText, m_sText.GetLength(), rc, 
  120.         DT_SINGLELINE | DT_VCENTER | DT_LEFT);
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CBmpCombo
  125.  
  126. //////////////////////////////////////////////////////////////////////////////
  127. //                        
  128. // FUNCTION...: CBmpCombo()
  129. //                                                  
  130. // DESCRIPTION: constructor
  131. //
  132. //////////////////////////////////////////////////////////////////////////////
  133. CBmpCombo::CBmpCombo()
  134. {
  135. }
  136.  
  137. //////////////////////////////////////////////////////////////////////////////
  138. //                        
  139. // FUNCTION...: ~CBmpCombo()
  140. //                                                  
  141. // DESCRIPTION: destructor
  142. //
  143. //////////////////////////////////////////////////////////////////////////////
  144. CBmpCombo::~CBmpCombo()
  145. {
  146. }
  147.  
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CBmpCombo message handlers
  151.  
  152. //////////////////////////////////////////////////////////////////////////////
  153. //                        
  154. // FUNCTION...: DrawItem()
  155. //                                                  
  156. // DESCRIPTION: draw item in particular state
  157. //
  158. //////////////////////////////////////////////////////////////////////////////
  159. void CBmpCombo::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
  160. {
  161.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  162.     CBmpComboData *pData = (CBmpComboData *) (lpDIS->itemData);
  163.     ASSERT(pData);
  164.     CRect rc (lpDIS->rcItem);
  165.  
  166.     if (lpDIS->itemID == LB_ERR)
  167.         return;
  168.  
  169.     if (lpDIS->itemID == -1){
  170.         /* We have a request to draw an item in the combo box, yet there
  171.          * are no combo box items. This is sent when the user TABS into
  172.          * an empty combo box or an empty combo box gets the focus. We
  173.          * have to indicate (somehow) that this owner-draw combo box has
  174.          * the focus. We do it in response to this message. Note that
  175.          * lpdis->itemData field would be invalid in this instance so
  176.          * we can't allow it to fall into our standard routines.
  177.          */
  178.         pData->Focus(pDC, rc, lpDIS->itemState & ODS_FOCUS);
  179.         return;
  180.     }
  181.  
  182.     if (lpDIS->itemAction & (ODA_DRAWENTIRE | ODA_SELECT) )
  183.         pData->DrawItem (pDC, rc, lpDIS->itemState & ODS_SELECTED);
  184.  
  185.       if (lpDIS->itemAction & ODA_FOCUS)
  186.         pData->Focus(pDC, rc, lpDIS->itemState & ODS_FOCUS);
  187. }
  188.  
  189. //////////////////////////////////////////////////////////////////////////////
  190. //                        
  191. // FUNCTION...: MeasureItem()
  192. //                                                  
  193. // DESCRIPTION: set the measure of item
  194. //
  195. //////////////////////////////////////////////////////////////////////////////
  196. void CBmpCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
  197. {
  198.     CBmpComboData *pData = (CBmpComboData *) (lpMeasureItemStruct->itemData);
  199.  
  200.     // MeasureItem is called only once for fixed sized list boxes, before
  201.     // they have any data!  So we return 40 pixels just in case, otherwise
  202.     // we return the height of each item's bitmap.
  203.     lpMeasureItemStruct->itemHeight = 21;    // a reasonable default
  204. }
  205.  
  206. //////////////////////////////////////////////////////////////////////////////
  207. //                        
  208. // FUNCTION...: DeleteItem()
  209. //                                                  
  210. // DESCRIPTION: delete item
  211. //
  212. //////////////////////////////////////////////////////////////////////////////
  213. void CBmpCombo::DeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct) 
  214. {
  215.     CBmpComboData *pData = (CBmpComboData *) (lpDeleteItemStruct->itemData);
  216.     ASSERT(pData);
  217.     delete pData;
  218. }
  219.  
  220. //////////////////////////////////////////////////////////////////////////////
  221. //                        
  222. // FUNCTION...: AddItem ()
  223. //                                                  
  224. // DESCRIPTION: add item
  225. //
  226. //////////////////////////////////////////////////////////////////////////////
  227. int CBmpCombo::AddItem (const char* pszText, UINT nBitmapID)
  228. {
  229.     CBmpComboData* pData = new CBmpComboData (pszText, nBitmapID);
  230.  
  231.     int nRet = AddString ((LPCSTR) pData);
  232.     if (nRet == LB_ERR)
  233.         delete pData;
  234.  
  235.     return nRet;
  236. }
  237.  
  238.