home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c12 / lab01 / ex03 / palviewctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  8.5 KB  |  345 lines

  1. // PalViewCtl.cpp : Implementation of the CPalViewCtrl ActiveX Control class.
  2.  
  3. #include "stdafx.h"
  4. #include "PalView.h"
  5.  
  6. #include "dib.h"
  7. #include "dibpal.h"
  8.  
  9. #include "PalViewCtl.h"
  10. #include "PalViewPpg.h"
  11.  
  12. #include "DibFilePropPage.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20.  
  21. IMPLEMENT_DYNCREATE(CPalViewCtrl, COleControl)
  22.  
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Message map
  26.  
  27. BEGIN_MESSAGE_MAP(CPalViewCtrl, COleControl)
  28.     //{{AFX_MSG_MAP(CPalViewCtrl)
  29.     ON_WM_LBUTTONDOWN()
  30.     //}}AFX_MSG_MAP
  31.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  32. END_MESSAGE_MAP()
  33.  
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // Dispatch map
  37.  
  38. BEGIN_DISPATCH_MAP(CPalViewCtrl, COleControl)
  39.     //{{AFX_DISPATCH_MAP(CPalViewCtrl)
  40.     DISP_PROPERTY_NOTIFY(CPalViewCtrl, "DibFileName", m_dibFileName, OnDibFileNameChanged, VT_BSTR)
  41.     DISP_PROPERTY_NOTIFY(CPalViewCtrl, "ShowColorSelection", m_showColorSelection, OnShowColorSelectionChanged, VT_BOOL)
  42.     DISP_PROPERTY_NOTIFY(CPalViewCtrl, "Show3D", m_show3D, OnShow3DChanged, VT_BOOL)
  43.     DISP_PROPERTY_EX(CPalViewCtrl, "SelectionIndex", GetSelectionIndex, SetSelectionIndex, VT_I2)
  44.     DISP_FUNCTION(CPalViewCtrl, "GetColorFromPalette", GetColorFromPalette, VT_I4, VTS_I2)
  45.     //}}AFX_DISPATCH_MAP
  46.     DISP_FUNCTION_ID(CPalViewCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  47. END_DISPATCH_MAP()
  48.  
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // Event map
  52.  
  53. BEGIN_EVENT_MAP(CPalViewCtrl, COleControl)
  54.     //{{AFX_EVENT_MAP(CPalViewCtrl)
  55.     EVENT_CUSTOM("SelChange", FireSelChange, VTS_I2)
  56.     EVENT_STOCK_DBLCLICK()
  57.     //}}AFX_EVENT_MAP
  58. END_EVENT_MAP()
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Property pages
  63.  
  64. // TODO: Add more property pages as needed.  Remember to increase the count!
  65. BEGIN_PROPPAGEIDS(CPalViewCtrl, 2)
  66.     PROPPAGEID(CPalViewPropPage::guid)
  67.     PROPPAGEID(CDibFilePropPage::guid)
  68. END_PROPPAGEIDS(CPalViewCtrl)
  69.  
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // Initialize class factory and guid
  73.  
  74. IMPLEMENT_OLECREATE_EX(CPalViewCtrl, "PALVIEW.PalViewCtrl.1",
  75.     0x7f7767c3, 0x365b, 0x11d0, 0x9c, 0x9d, 0, 0xa0, 0xd1, 0, 0xe3, 0xc8)
  76.  
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // Type library ID and version
  80.  
  81. IMPLEMENT_OLETYPELIB(CPalViewCtrl, _tlid, _wVerMajor, _wVerMinor)
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Interface IDs
  86.  
  87. const IID BASED_CODE IID_DPalView =
  88.         { 0x7f7767c1, 0x365b, 0x11d0, 
  89.         { 0x9c, 0x9d, 0, 0xa0, 0xd1, 0, 0xe3, 0xc8 } };
  90. const IID BASED_CODE IID_DPalViewEvents =
  91.         { 0x7f7767c2, 0x365b, 0x11d0, 
  92.         { 0x9c, 0x9d, 0, 0xa0, 0xd1, 0, 0xe3, 0xc8 } };
  93.  
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // Control type information
  97.  
  98. static const DWORD BASED_CODE _dwPalViewOleMisc =
  99.     OLEMISC_ACTIVATEWHENVISIBLE |
  100.     OLEMISC_SETCLIENTSITEFIRST |
  101.     OLEMISC_INSIDEOUT |
  102.     OLEMISC_CANTLINKINSIDE |
  103.     OLEMISC_RECOMPOSEONRESIZE;
  104.  
  105. IMPLEMENT_OLECTLTYPE(CPalViewCtrl, IDS_PALVIEW, _dwPalViewOleMisc)
  106.  
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109. // CPalViewCtrl::CPalViewCtrlFactory::UpdateRegistry -
  110. // Adds or removes system registry entries for CPalViewCtrl
  111.  
  112. BOOL CPalViewCtrl::CPalViewCtrlFactory::UpdateRegistry(BOOL bRegister)
  113. {
  114.     // TODO: Verify that your control follows apartment-model threading rules.
  115.     // Refer to MFC TechNote 64 for more information.
  116.     // If your control does not conform to the apartment-model rules, then
  117.     // you must modify the code below, changing the 6th parameter from
  118.     // afxRegApartmentThreading to 0.
  119.  
  120.     if (bRegister)
  121.         return AfxOleRegisterControlClass(
  122.             AfxGetInstanceHandle(),
  123.             m_clsid,
  124.             m_lpszProgID,
  125.             IDS_PALVIEW,
  126.             IDB_PALVIEW,
  127.             afxRegApartmentThreading,
  128.             _dwPalViewOleMisc,
  129.             _tlid,
  130.             _wVerMajor,
  131.             _wVerMinor);
  132.     else
  133.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  134. }
  135.  
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CPalViewCtrl::CPalViewCtrl - Constructor
  139.  
  140. CPalViewCtrl::CPalViewCtrl()
  141. {
  142.     InitializeIIDs(&IID_DPalView, &IID_DPalViewEvents);
  143.  
  144.     m_pDibPal = 0;
  145.     m_dibFileName = _T("");
  146.  
  147.     m_showColorSelection = TRUE;
  148.     m_show3D = FALSE;
  149. }
  150.  
  151.  
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CPalViewCtrl::~CPalViewCtrl - Destructor
  154.  
  155. CPalViewCtrl::~CPalViewCtrl()
  156. {
  157.     if (0 != m_pDibPal)
  158.     {
  159.         delete m_pDibPal;
  160.         m_pDibPal = 0;
  161.     }
  162. }
  163.  
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CPalViewCtrl::OnDraw - Drawing function
  167.  
  168. void CPalViewCtrl::OnDraw(
  169.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  170. {
  171.     if(0 != m_pDibPal)
  172.     {
  173.         m_pDibPal->Draw(pdc, rcBounds, m_showColorSelection); 
  174.     }
  175.     else
  176.     {
  177.         pdc->FillRect(rcBounds, 
  178.             CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
  179.     }
  180. }
  181.  
  182.  
  183. /////////////////////////////////////////////////////////////////////////////
  184. // CPalViewCtrl::DoPropExchange - Persistence support
  185.  
  186. void CPalViewCtrl::DoPropExchange(CPropExchange* pPX)
  187. {
  188.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  189.     COleControl::DoPropExchange(pPX);
  190.  
  191.     // TODO: Call PX_ functions for each persistent custom property.
  192.     PX_Bool(pPX, _T("ShowColorSelection"), m_showColorSelection, TRUE);
  193.     PX_Bool(pPX, _T("Show3D"), m_show3D, FALSE);
  194.     PX_String(pPX, _T("DibFileName"), m_dibFileName, _T(""));
  195.  
  196.     if (pPX->IsLoading())
  197.     {
  198.         ExtractPalFromDIBFile(m_dibFileName);
  199.     }
  200. }
  201.  
  202.  
  203. /////////////////////////////////////////////////////////////////////////////
  204. // CPalViewCtrl::OnResetState - Reset control to default state
  205.  
  206. void CPalViewCtrl::OnResetState()
  207. {
  208.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  209.  
  210.     // TODO: Reset any other control state here.
  211. }
  212.  
  213.  
  214. /////////////////////////////////////////////////////////////////////////////
  215. // CPalViewCtrl::AboutBox - Display an "About" box to the user
  216.  
  217. void CPalViewCtrl::AboutBox()
  218. {
  219.     CDialog dlgAbout(IDD_ABOUTBOX_PALVIEW);
  220.     dlgAbout.DoModal();
  221. }
  222.  
  223.  
  224. /////////////////////////////////////////////////////////////////////////////
  225. // CPalViewCtrl message handlers
  226. void CPalViewCtrl::ExtractPalFromDIBFile(LPCTSTR lpszFileSpec)
  227. {
  228.     CDIB    Dib;
  229.     CFile    DibFile;
  230.     
  231.     if(DibFile.Open(lpszFileSpec, CFile::modeRead))
  232.     {
  233.         // Load the bitmap file into a CDIB object
  234.         if(Dib.Load(&DibFile))
  235.         {
  236.             CDIBPal* pTemp = new CDIBPal;
  237.             ASSERT(pTemp != NULL);
  238.         
  239.             //    This will extract the color table fron the CDIB object
  240.             if(pTemp->Create(&Dib))
  241.             {
  242.                 //    Get rid of any existing CDIBPal object
  243.                 if(0 != m_pDibPal)
  244.                 {
  245.                     delete m_pDibPal;
  246.                     m_pDibPal = 0;
  247.                 }
  248.                 //    Ready to go
  249.                 m_pDibPal = pTemp;
  250.         
  251.                 //    Make sure the new palette reflects our current state
  252.                 m_pDibPal->SetDraw3D(m_show3D);
  253.             }
  254.             else
  255.             {
  256.                 delete pTemp;
  257.             }
  258.         }
  259.     }
  260. }
  261.  
  262. void CPalViewCtrl::OnDibFileNameChanged() 
  263. {
  264.     ExtractPalFromDIBFile(m_dibFileName);
  265.     InvalidateControl();
  266.  
  267.     SetModifiedFlag();
  268. }
  269.  
  270. void CPalViewCtrl::OnShowColorSelectionChanged() 
  271. {
  272.     InvalidateControl();
  273.     SetModifiedFlag();
  274. }
  275.  
  276. void CPalViewCtrl::OnShow3DChanged() 
  277. {
  278.     if(0 != m_pDibPal)
  279.     {
  280.         m_pDibPal->SetDraw3D(m_show3D);
  281.         InvalidateControl();
  282.     }
  283.     SetModifiedFlag();
  284. }
  285.  
  286.  
  287. short CPalViewCtrl::GetSelectionIndex() 
  288. {
  289.     short nSelectionIndex = -1;
  290.     if(0 != m_pDibPal)
  291.     {
  292.         nSelectionIndex = m_pDibPal->GetSelectionIndex();
  293.     }
  294.     return nSelectionIndex;
  295. }
  296.  
  297. void CPalViewCtrl::SetSelectionIndex(short nNewValue) 
  298. {
  299.     if(nNewValue >= 0 && nNewValue < 256)
  300.     {
  301.         if(0 != m_pDibPal)
  302.         {
  303.             m_pDibPal->SetSelectionIndex(nNewValue);
  304.             FireSelChange(nNewValue);
  305.             InvalidateControl();
  306.         }
  307.     }
  308.     SetModifiedFlag();
  309. }
  310.  
  311. void CPalViewCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
  312. {
  313.     if(0 != m_pDibPal)
  314.     {
  315.         int x, y;
  316.         GetControlSize(&x, &y);
  317.         CRect rcControl(0, 0, x, y);
  318.  
  319.         //    Ask the DibPal object which palette index would
  320.         //    be hit by the mouse click
  321.         int nPalIndex = m_pDibPal->HitTest(rcControl, point);
  322.         if(-1 != nPalIndex)
  323.         {
  324.             //    Change the selection property
  325.             SetSelectionIndex(nPalIndex);
  326.         }
  327.     }
  328.  
  329.     COleControl::OnLButtonDown(nFlags, point);
  330. }
  331.  
  332. long CPalViewCtrl::GetColorFromPalette(short nPalIndex) 
  333. {
  334.     COLORREF    crRet = 0; // Default to BLACK
  335.     
  336.     if(nPalIndex >= 0 && nPalIndex < 256)
  337.     {
  338.         if(0 != m_pDibPal)
  339.         {
  340.             crRet = m_pDibPal->GetColorFromIndex(nPalIndex);
  341.         }
  342.     }
  343.     return crRet;
  344. }
  345.