home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / WINBTN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  5.4 KB  |  182 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CBitmapButton
  26.  
  27. // LoadBitmaps will load in one, two, three or all four bitmaps
  28. // returns TRUE if all specified images are loaded
  29. BOOL CBitmapButton::LoadBitmaps(LPCTSTR lpszBitmapResource,
  30.     LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus,
  31.     LPCTSTR lpszBitmapResourceDisabled)
  32. {
  33.     // delete old bitmaps (if present)
  34.     m_bitmap.DeleteObject();
  35.     m_bitmapSel.DeleteObject();
  36.     m_bitmapFocus.DeleteObject();
  37.     m_bitmapDisabled.DeleteObject();
  38.  
  39.     if (!m_bitmap.LoadBitmap(lpszBitmapResource))
  40.     {
  41.         TRACE0("Failed to load bitmap for normal image.\n");
  42.         return FALSE;   // need this one image
  43.     }
  44.     BOOL bAllLoaded = TRUE;
  45.     if (lpszBitmapResourceSel != NULL)
  46.     {
  47.         if (!m_bitmapSel.LoadBitmap(lpszBitmapResourceSel))
  48.         {
  49.             TRACE0("Failed to load bitmap for selected image.\n");
  50.             bAllLoaded = FALSE;
  51.         }
  52.     }
  53.     if (lpszBitmapResourceFocus != NULL)
  54.     {
  55.         if (!m_bitmapFocus.LoadBitmap(lpszBitmapResourceFocus))
  56.             bAllLoaded = FALSE;
  57.     }
  58.     if (lpszBitmapResourceDisabled != NULL)
  59.     {
  60.         if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))
  61.             bAllLoaded = FALSE;
  62.     }
  63. #ifdef _MAC
  64.     ASSERT(m_bitmap.m_hObject != NULL);
  65.     ::SetBitmapReadOnly((HBITMAP)m_bitmap.m_hObject, BRO_READONLY);
  66.     if (m_bitmapSel.m_hObject != NULL)
  67.         ::SetBitmapReadOnly((HBITMAP)m_bitmapSel.m_hObject, BRO_READONLY);
  68.     if (m_bitmapFocus.m_hObject != NULL)
  69.         ::SetBitmapReadOnly((HBITMAP)m_bitmapFocus.m_hObject, BRO_READONLY);
  70.     if (m_bitmapDisabled.m_hObject != NULL)
  71.         ::SetBitmapReadOnly((HBITMAP)m_bitmapDisabled.m_hObject, BRO_READONLY);
  72. #endif
  73.     return bAllLoaded;
  74. }
  75.  
  76. // SizeToContent will resize the button to the size of the bitmap
  77. void CBitmapButton::SizeToContent()
  78. {
  79.     ASSERT(m_bitmap.m_hObject != NULL);
  80.     CSize bitmapSize;
  81.     BITMAP bmInfo;
  82.     VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));
  83.     VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight,
  84.         SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
  85. }
  86.  
  87. // Autoload will load the bitmap resources based on the text of
  88. //  the button
  89. // Using suffices "U", "D", "F" and "X" for up/down/focus/disabled
  90. BOOL CBitmapButton::AutoLoad(UINT nID, CWnd* pParent)
  91. {
  92.     // first attach the CBitmapButton to the dialog control
  93.     if (!SubclassDlgItem(nID, pParent))
  94.         return FALSE;
  95.  
  96.     CString buttonName;
  97.     GetWindowText(buttonName);
  98.     ASSERT(!buttonName.IsEmpty());      // must provide a title
  99.  
  100.     LoadBitmaps(buttonName + _T("U"), buttonName + _T("D"),
  101.       buttonName + _T("F"), buttonName + _T("X"));
  102.  
  103.     // we need at least the primary
  104.     if (m_bitmap.m_hObject == NULL)
  105.         return FALSE;
  106.  
  107.     // size to content
  108.     SizeToContent();
  109.     return TRUE;
  110. }
  111.  
  112. // Draw the appropriate bitmap
  113. void CBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  114. {
  115.     ASSERT(lpDIS != NULL);
  116.     // must have at least the first bitmap loaded before calling DrawItem
  117.     ASSERT(m_bitmap.m_hObject != NULL);     // required
  118.  
  119.     // use the main bitmap for up, the selected bitmap for down
  120.     CBitmap* pBitmap = &m_bitmap;
  121.     UINT state = lpDIS->itemState;
  122.     if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
  123.         pBitmap = &m_bitmapSel;
  124. #ifndef _MAC
  125.     else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
  126. #else
  127.     else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL &&
  128.             (GetParent()->GetStyle() & DS_WINDOWSUI))
  129. #endif
  130.         pBitmap = &m_bitmapFocus;   // third image for focused
  131.     else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
  132.         pBitmap = &m_bitmapDisabled;   // last image for disabled
  133.  
  134.     // draw the whole button
  135.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  136.     CDC memDC;
  137.     memDC.CreateCompatibleDC(pDC);
  138.     CBitmap* pOld = memDC.SelectObject(pBitmap);
  139.     if (pOld == NULL)
  140.         return;     // destructors will clean up
  141.  
  142.     CRect rect;
  143.     rect.CopyRect(&lpDIS->rcItem);
  144.     pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  145.         &memDC, 0, 0, SRCCOPY);
  146.     memDC.SelectObject(pOld);
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CBitmapButton diagnostics
  151. #ifdef _DEBUG
  152. void CBitmapButton::AssertValid() const
  153. {
  154.     CButton::AssertValid();
  155.  
  156.     m_bitmap.AssertValid();
  157.     m_bitmapSel.AssertValid();
  158.     m_bitmapFocus.AssertValid();
  159.     m_bitmapDisabled.AssertValid();
  160. }
  161.  
  162. void CBitmapButton::Dump(CDumpContext& dc) const
  163. {
  164.     CButton::Dump(dc);
  165.  
  166.     dc << "m_bitmap = " << (UINT)m_bitmap.m_hObject;
  167.     dc << "\nm_bitmapSel = " << (UINT)m_bitmapSel.m_hObject;
  168.     dc << "\nm_bitmapFocus = " << (UINT)m_bitmapFocus.m_hObject;
  169.     dc << "\nm_bitmapDisabled = " << (UINT)m_bitmapDisabled.m_hObject;
  170.  
  171.     dc << "\n";
  172. }
  173. #endif
  174.  
  175. #ifdef AFX_INIT_SEG
  176. #pragma code_seg(AFX_INIT_SEG)
  177. #endif
  178.  
  179. IMPLEMENT_DYNAMIC(CBitmapButton, CButton)
  180.  
  181. /////////////////////////////////////////////////////////////////////////////
  182.