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