home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / PUSH.PAK / PUSHCTL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.0 KB  |  309 lines

  1. // pushctl.cpp : Implementation of the CPushCtrl OLE control class.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "push.h"
  16. #include "pushctl.h"
  17. #include "pushppg.h"
  18.  
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25.  
  26. IMPLEMENT_DYNCREATE(CPushCtrl, COleControl)
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Message map
  31.  
  32. BEGIN_MESSAGE_MAP(CPushCtrl, COleControl)
  33.     //{{AFX_MSG_MAP(CPushCtrl)
  34.     ON_MESSAGE(OCM_COMMAND, OnOcmCommand)
  35.     ON_MESSAGE(OCM_DRAWITEM, OnOcmDrawItem)
  36.     ON_WM_ERASEBKGND()
  37.     //}}AFX_MSG_MAP
  38.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  39. END_MESSAGE_MAP()
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Dispatch map
  44.  
  45. BEGIN_DISPATCH_MAP(CPushCtrl, COleControl)
  46.     //{{AFX_DISPATCH_MAP(CPushCtrl)
  47.     DISP_STOCKPROP_BORDERSTYLE()
  48.     DISP_STOCKPROP_CAPTION()
  49.     DISP_STOCKPROP_ENABLED()
  50.     //}}AFX_DISPATCH_MAP
  51.     DISP_FUNCTION_ID(CPushCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  52. END_DISPATCH_MAP()
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Event map
  57.  
  58. BEGIN_EVENT_MAP(CPushCtrl, COleControl)
  59.     //{{AFX_EVENT_MAP(CPushCtrl)
  60.     EVENT_CUSTOM("CustomClick", FireCustomClick, VTS_BSTR  VTS_I2)
  61.     //}}AFX_EVENT_MAP
  62. END_EVENT_MAP()
  63.  
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Property pages
  67.  
  68. // TODO: Add more property pages as needed.  Remember to increase the count!
  69. BEGIN_PROPPAGEIDS(CPushCtrl, 1)
  70.     PROPPAGEID(CPushPropPage::guid)
  71. END_PROPPAGEIDS(CPushCtrl)
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Initialize class factory and guid
  76.  
  77. IMPLEMENT_OLECREATE_EX(CPushCtrl, "PUSH.PushCtrl.1",
  78.     0x75303863, 0xb925, 0x101a, 0xb5, 0x7a, 0x0, 0x0, 0xc0, 0xc3, 0xed, 0x5f)
  79.  
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // Type library ID and version
  83.  
  84. IMPLEMENT_OLETYPELIB(CPushCtrl, _tlid, _wVerMajor, _wVerMinor)
  85.  
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // Interface IDs
  89.  
  90. const IID BASED_CODE IID_DPush =
  91.         { 0x37446b86, 0x5870, 0x101b, { 0xb5, 0x7b, 0x0, 0x60, 0x8c, 0xc9, 0x6a, 0xfa } };
  92. const IID BASED_CODE IID_DPushEvents =
  93.         { 0x37446b87, 0x5870, 0x101b, { 0xb5, 0x7b, 0x0, 0x60, 0x8c, 0xc9, 0x6a, 0xfa } };
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // Control type information
  98.  
  99. static const DWORD BASED_CODE _dwPushOleMisc =
  100.     OLEMISC_ACTIVATEWHENVISIBLE |
  101.     OLEMISC_SETCLIENTSITEFIRST |
  102.     OLEMISC_INSIDEOUT |
  103.     OLEMISC_CANTLINKINSIDE |
  104.     OLEMISC_RECOMPOSEONRESIZE;
  105.  
  106. IMPLEMENT_OLECTLTYPE(CPushCtrl, IDS_PUSH, _dwPushOleMisc)
  107.  
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CPushCtrl::CPushCtrlFactory::UpdateRegistry -
  111. // Adds or removes system registry entries for CPushCtrl
  112.  
  113. BOOL CPushCtrl::CPushCtrlFactory::UpdateRegistry(BOOL bRegister)
  114. {
  115.     if (bRegister)
  116.         return AfxOleRegisterControlClass(
  117.             AfxGetInstanceHandle(),
  118.             m_clsid,
  119.             m_lpszProgID,
  120.             IDS_PUSH,
  121.             IDB_PUSH,
  122.             FALSE,                      //  Not insertable
  123.             _dwPushOleMisc,
  124.             _tlid,
  125.             _wVerMajor,
  126.             _wVerMinor);
  127.     else
  128.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  129. }
  130.  
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CPushCtrl::CPushCtrl - Constructor
  134.  
  135. CPushCtrl::CPushCtrl()
  136. {
  137.     InitializeIIDs(&IID_DPush, &IID_DPushEvents);
  138.  
  139.     SetInitialSize(28, 28);
  140. }
  141.  
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CPushCtrl::~CPushCtrl - Destructor
  145.  
  146. CPushCtrl::~CPushCtrl()
  147. {
  148. }
  149.  
  150.  
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CPushCtrl::OnDraw - Drawing function
  153.  
  154. void CPushCtrl::OnDraw(
  155.             CDC* pdc, const CRect& rcBounds, const CRect&)
  156. {
  157.     DoSuperclassPaint(pdc, rcBounds);
  158. }
  159.  
  160.  
  161. /////////////////////////////////////////////////////////////////////////////
  162. // CPushCtrl::DoPropExchange - Persistence support
  163.  
  164. void CPushCtrl::DoPropExchange(CPropExchange* pPX)
  165. {
  166.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  167.     COleControl::DoPropExchange(pPX);
  168. }
  169.  
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. // CPushCtrl::OnResetState - Reset control to default state
  173.  
  174. void CPushCtrl::OnResetState()
  175. {
  176.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  177. }
  178.  
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CPushCtrl::AboutBox - Display an "About" box to the user
  182.  
  183. void CPushCtrl::AboutBox()
  184. {
  185.     CDialog dlgAbout(IDD_ABOUTBOX_PUSH);
  186.     dlgAbout.DoModal();
  187. }
  188.  
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191. // CPushCtrl::PreCreateWindow - Modify parameters for CreateWindowEx
  192.  
  193. BOOL CPushCtrl::PreCreateWindow(CREATESTRUCT& cs)
  194. {
  195.     cs.lpszClass = _T("BUTTON");
  196.     cs.style |= BS_PUSHBUTTON | BS_OWNERDRAW;
  197.     return COleControl::PreCreateWindow(cs);
  198. }
  199.  
  200.  
  201. /////////////////////////////////////////////////////////////////////////////
  202. // CPushCtrl::IsSubclassedControl - This is a subclassed control
  203.  
  204. BOOL CPushCtrl::IsSubclassedControl()
  205. {
  206.     return TRUE;
  207. }
  208.  
  209.  
  210. /////////////////////////////////////////////////////////////////////////////
  211. // CPushCtrl::OnOcmCommand - Handle command messages
  212.  
  213. LRESULT CPushCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
  214. {
  215. #ifdef _WIN32
  216.     lParam;
  217.     WORD wNotifyCode = HIWORD(wParam);
  218. #else
  219.     wParam;
  220.     WORD wNotifyCode = HIWORD(lParam);
  221. #endif
  222.  
  223.     switch (wNotifyCode)
  224.     {
  225.     case BN_CLICKED:
  226.  
  227.         // Fire custom click event, sending caption
  228.         CString strCaption = InternalGetText();
  229.         FireCustomClick(strCaption, (short) strCaption.GetLength());
  230.  
  231.         break;
  232.     }
  233.  
  234.     return 0;
  235. }
  236.  
  237.  
  238. /////////////////////////////////////////////////////////////////////////////
  239. // CPushCtrl::OnEraseBkgnd - Simply return TRUE so background is not erased.
  240.  
  241. BOOL CPushCtrl::OnEraseBkgnd(CDC*)
  242. {
  243.     return TRUE;
  244. }
  245.  
  246.  
  247. ////////////////////////////////////////////////////////////////////////////
  248. // CPushCtrl::OnOcmDrawItem - Draw an item.
  249.  
  250. LRESULT CPushCtrl::OnOcmDrawItem(WPARAM, LPARAM lParam)
  251. {
  252.     DrawItem((LPDRAWITEMSTRUCT)lParam);
  253.  
  254.     return 1;
  255. }
  256.  
  257.  
  258. /////////////////////////////////////////////////////////////////////////////
  259. // CPushCtrl::DrawItem - Draw an item.
  260.  
  261. void CPushCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  262. {
  263.     CDC *pdc;
  264.     int bmpId;
  265.     CBitmap bitmap;
  266.     BITMAP  bmp;
  267.     CPictureHolder picHolder;
  268.     CRect rcSrcBounds;
  269.  
  270.     CPen* pOldPen;
  271.     RECT    rect;
  272.     SHORT   inflate;
  273.  
  274.     pdc = CDC::FromHandle(lpDrawItemStruct->hDC);
  275.  
  276.     switch (lpDrawItemStruct->itemAction)
  277.     {
  278.         case ODA_DRAWENTIRE:
  279.         case ODA_SELECT:
  280.             // Load "up" or "down" bitmap depending on selection state
  281.             bmpId = (lpDrawItemStruct->itemState & ODS_SELECTED) ? IDB_DOWNBITMAP
  282.                 : IDB_UPBITMAP;
  283.             bitmap.LoadBitmap(bmpId);
  284.             bitmap.GetObject(sizeof(BITMAP), &bmp);
  285.             rcSrcBounds.right = bmp.bmWidth;
  286.             rcSrcBounds.bottom = bmp.bmHeight;
  287.  
  288.             // Create picture and render
  289.             picHolder.CreateFromBitmap((HBITMAP)bitmap.m_hObject, NULL, FALSE);
  290.             picHolder.Render(pdc, lpDrawItemStruct->rcItem, rcSrcBounds);
  291.  
  292.             break;
  293.  
  294.         case ODA_FOCUS:
  295.             // Just draw focus rect
  296.             pOldPen = (CPen*)pdc->SelectStockObject(BLACK_PEN);
  297.             if (lpDrawItemStruct->itemState & ODS_FOCUS)
  298.             {
  299.                 CopyRect((LPRECT)&rect, (LPRECT)&lpDrawItemStruct->rcItem);
  300.                 inflate = (SHORT)min(3,min(rect.right  - rect.left + 1,
  301.                     rect.bottom - rect.top  + 1) / 5);
  302.                 InflateRect(&rect, -inflate, -inflate);
  303.                 pdc->DrawFocusRect(&rect);
  304.             }
  305.             pdc->SelectObject(pOldPen);
  306.             break;
  307.     }
  308. }
  309.