home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atlfire / firetabctrl.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  9KB  |  384 lines

  1. // FireTabCtrl.cpp : Implementation of CFireTabCtrl
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "stdafx.h"
  14. #include "ATLFire.h"
  15. #include "FireTabCtrl.h"
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CFireTabCtrl
  19.  
  20. STDMETHODIMP CFireTabCtrl::InterfaceSupportsErrorInfo(REFIID riid)
  21. {
  22.     static const IID* arr[] =
  23.     {
  24.         &IID_IFireTabCtrl,
  25.     };
  26.     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  27.     {
  28.         if (InlineIsEqualGUID(*arr[i],riid))
  29.             return S_OK;
  30.     }
  31.     return S_FALSE;
  32. }
  33.  
  34. HRESULT CFireTabCtrl::OnDraw(ATL_DRAWINFO& di)
  35. {
  36.     return S_OK;
  37. }
  38.  
  39. LRESULT CFireTabCtrl::OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
  40. {
  41.     if(m_ctlSysTabControl32.m_hWnd != NULL)
  42.         TabCtrl_DeleteAllItems(m_ctlSysTabControl32);
  43.     return 0;
  44. }
  45.  
  46. CFireTabCtrl::CFireTabCtrl() :
  47.     m_ctlSysTabControl32(_T("SysTabControl32"), this, 1)
  48. {
  49.     m_bWindowOnly = TRUE;
  50. }
  51.  
  52. CFireTabCtrl::~CFireTabCtrl()
  53. {
  54.     TCHAR szBuff[128];
  55.     wsprintf(szBuff, _T("ATL:%s"), m_ctlSysTabControl32.m_lpszClassName);
  56.     UnregisterClass(szBuff, _Module.GetModuleInstance());
  57. }
  58.  
  59. LRESULT CFireTabCtrl::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  60. {
  61.     ATLTRACE(_T("CFireTabCtrl::OnSize()\n"));
  62.     int cx, cy;
  63.     cx = LOWORD(lParam);
  64.     cy = HIWORD(lParam);
  65.     ::SetWindowPos(m_ctlSysTabControl32.m_hWnd, NULL, 0,
  66.         0, cx, cy, SWP_NOZORDER | SWP_NOACTIVATE);
  67.  
  68.     int cxFire = cx - 12;
  69.     int cyFire = cy - 40;
  70.     ::SetWindowPos(m_Display.m_hWnd, NULL, 5,
  71.         30, cxFire, cyFire, SWP_NOZORDER | SWP_NOACTIVATE);
  72.  
  73.     m_Display.bmSize.cx = cxFire - 5;
  74.     m_Display.bmSize.cy = cyFire - 30;
  75.     switch(m_Display.m_curColor)
  76.     {
  77.     case 0:
  78.         m_Display.InitFire(CFireWnd::red);
  79.         break;
  80.     case 1:
  81.         m_Display.InitFire(CFireWnd::blue);
  82.         break;
  83.     case 2:
  84.         m_Display.InitFire(CFireWnd::green);
  85.         break;
  86.     }
  87.     FireOnChanged(5);
  88.  
  89.     return 0;
  90. }
  91.  
  92. LRESULT CFireTabCtrl::OnSelChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
  93. {
  94.     ATLTRACE(_T("CTabDlg::OnSelChanged()\n"));
  95.     m_Display.m_curColor = TabCtrl_GetCurSel(m_ctlSysTabControl32.m_hWnd);
  96.     switch(m_Display.m_curColor)
  97.     {
  98.     case 0:
  99.         m_Display.InitFire(CFireWnd::red);
  100.         break;
  101.     case 1:
  102.         m_Display.InitFire(CFireWnd::blue);
  103.         break;
  104.     case 2:
  105.         m_Display.InitFire(CFireWnd::green);
  106.         break;
  107.     }
  108.  
  109.     // force a repaint
  110.     m_Display.Invalidate();
  111.  
  112.     return 0;
  113. }
  114.  
  115. LRESULT CFireTabCtrl::OnEraseBackground(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  116. {
  117.     ATLTRACE(_T("CTabDlg::OnEraseBackground()\n"));
  118.     return 0;
  119. }
  120.  
  121. LRESULT CFireTabCtrl::OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  122. {
  123.     ATLTRACE(_T("CTabDlg::OnActivate()\n"));
  124.     m_Display.SendMessage(WM_QUERYNEWPALETTE);  // redo the palette if necessary
  125.     return 0;
  126. }
  127.  
  128. LRESULT CFireTabCtrl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  129. {
  130.     ATLTRACE(_T("CFireTabDlg::OnCreate()\n"));
  131.  
  132.     // Setup and create the tab control
  133.     int nTabs = 3;
  134.     _TCHAR *pszNames[] = { _T("Wood"), _T("Natural Gas"), _T("Kryptonite") };
  135.     RECT rc;
  136.     GetWindowRect(&rc);
  137.     rc.right -= rc.left;
  138.     rc.bottom -= rc.top;
  139.     rc.top = rc.left = 0;
  140.     m_ctlSysTabControl32.Create(m_hWnd, rc,_T(""), WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE | TCS_TABS );
  141.     TC_ITEM tie;
  142.     for( int i = 0; i < nTabs; i++ )
  143.     {
  144.         tie.mask = TCIF_TEXT | TCIF_IMAGE;
  145.         tie.iImage = -1;
  146.         tie.pszText = pszNames[i];
  147.         tie.cchTextMax = _tcslen(pszNames[i]);
  148.         if (TabCtrl_InsertItem (m_ctlSysTabControl32.m_hWnd, i, &tie) == -1)
  149.         {
  150.             // The insert failed; display an error box.
  151.             ::MessageBox (NULL, _T("TabCtrl_InsertItem failed!"), NULL, MB_OK);
  152.             return E_FAIL;
  153.         }
  154.     }
  155.  
  156.     m_Display.Create(m_ctlSysTabControl32.m_hWnd);
  157.     TabCtrl_SetCurSel(m_ctlSysTabControl32.m_hWnd, m_Display.m_curColor);
  158.     m_Display.ShowWindow(TRUE);
  159.     return 0;
  160. }
  161.  
  162. HRESULT CFireTabCtrl::IOleInPlaceObject_UIDeactivate()
  163. {
  164.     ATLTRACE(_T("CFireTabDlg::IOleInPlaceObject_UIDeactivate()\n"));
  165.  
  166.     if(m_Display.m_hWnd != NULL && ::IsWindow(m_Display.m_hWnd))
  167.     {
  168.         ATLTRACE(_T("Destroying m_Display.m_hWnd\n"));
  169.         m_Display.DestroyWindow();
  170.     }
  171.  
  172.     return CComControlBase::IOleInPlaceObject_UIDeactivate();
  173. }
  174.  
  175. STDMETHODIMP CFireTabCtrl::IOleInPlaceObject_SetObjectRects(LPCRECT prcPos,LPCRECT prcClip)
  176. {
  177.     CComControlBase::IOleInPlaceObject_SetObjectRects(prcPos, prcClip);
  178.     ATLTRACE(_T("CFireTabDlg::IOleInPlaceObject_SetObjectRects()\n"));
  179.  
  180.     // The width and height of the rectangle for the tab control
  181.     int cx = prcPos->right - prcPos->left;
  182.     int cy = prcPos->bottom - prcPos->top;
  183.  
  184.     // Here's where we cache away the size of the window
  185.     // for the fire bitmap display. rcSize is used in
  186.     // CFireWnd::CreateBitmap()
  187.     m_Display.rcSize.cx = cx - 12;
  188.     m_Display.rcSize.cy = cy - 40;
  189.  
  190.     // We'll set the window sizes if the tab control has a window
  191.     if( ::IsWindow(m_ctlSysTabControl32.m_hWnd) )
  192.     {
  193.         ::SetWindowPos(m_ctlSysTabControl32.m_hWnd, NULL, 0,
  194.             0, cx, cy, SWP_NOZORDER | SWP_NOACTIVATE);
  195.         ::SetWindowPos(m_Display.m_hWnd, NULL, 5,
  196.             30, cx - 12, cy - 40, SWP_NOZORDER | SWP_NOACTIVATE);
  197.     }
  198.  
  199.     return 0;
  200. }
  201.  
  202. STDMETHODIMP CFireTabCtrl::GetViewStatus(DWORD* pdwStatus)
  203. {
  204.     ATLTRACE(_T("IViewObjectExImpl::GetViewStatus\n"));
  205.     *pdwStatus = VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE;
  206.     return S_OK;
  207. }
  208.  
  209. STDMETHODIMP CFireTabCtrl::get_Decay(long * pVal)
  210. {
  211.     *pVal = m_Display.m_nDecay;
  212.     return S_OK;
  213. }
  214.  
  215. STDMETHODIMP CFireTabCtrl::put_Decay(long newVal)
  216. {
  217.     if( newVal < 1 || newVal > 100 )
  218.     {
  219.         Error(_T("Please insert a number between 1 and 100"));
  220.         return DISP_E_EXCEPTION;
  221.     }
  222.     m_Display.m_nDecay = newVal;
  223.     return S_OK;
  224. }
  225.  
  226. STDMETHODIMP CFireTabCtrl::get_Flammability(long * pVal)
  227. {
  228.     *pVal = m_Display.m_nFlammability;
  229.     return S_OK;
  230. }
  231.  
  232. STDMETHODIMP CFireTabCtrl::put_Flammability(long newVal)
  233. {
  234.     if( newVal < 1 || newVal > 399 )
  235.     {
  236.         Error(_T("Please insert a number between 1 and 399"));
  237.         return DISP_E_EXCEPTION;
  238.     }
  239.     m_Display.m_nFlammability = newVal;
  240.     return S_OK;
  241. }
  242.  
  243. STDMETHODIMP CFireTabCtrl::get_MaxHeat(long * pVal)
  244. {
  245.     *pVal = m_Display.m_nMaxHeat;
  246.     return S_OK;
  247. }
  248.  
  249. STDMETHODIMP CFireTabCtrl::put_MaxHeat(long newVal)
  250. {
  251.     if( newVal < 0 || newVal > 223 )
  252.     {
  253.         Error( _T("Please insert a number between 0 and 223"));
  254.         return DISP_E_EXCEPTION;
  255.     }
  256.     m_Display.m_nMaxHeat = newVal;
  257.     return S_OK;
  258. }
  259.  
  260. STDMETHODIMP CFireTabCtrl::get_SpreadRate(long * pVal)
  261. {
  262.     *pVal = m_Display.m_nSpreadRate;
  263.     return S_OK;
  264. }
  265.  
  266. STDMETHODIMP CFireTabCtrl::put_SpreadRate(long newVal)
  267. {
  268.     if( newVal < 1 || newVal > 100 )
  269.     {
  270.         Error(_T("Please insert a number between 1 and 100"));
  271.         return DISP_E_EXCEPTION;
  272.     }
  273.     m_Display.m_nSpreadRate = newVal;
  274.     return S_OK;
  275. }
  276.  
  277. STDMETHODIMP CFireTabCtrl::get_Size(long * pVal)
  278. {
  279.     *pVal = m_Display.m_nSize;
  280.     return S_OK;
  281. }
  282.  
  283. STDMETHODIMP CFireTabCtrl::put_Size(long newVal)
  284. {
  285.     if( m_Display.bmSize.cx  != 0)
  286.     {
  287.         int maxWidth = m_Display.bmSize.cx - 1;
  288.         if( newVal < 1 || newVal > maxWidth )
  289.         {
  290.             _TCHAR buf[128];
  291.             wsprintf(buf,_T("Please insert a number between 1 and %d (bitmap width)"), maxWidth);
  292.             Error(buf);
  293.             return DISP_E_EXCEPTION;
  294.         }
  295.     }
  296.  
  297.     m_Display.m_nSize = newVal;
  298.  
  299.     return S_OK;
  300. }
  301.  
  302. STDMETHODIMP CFireTabCtrl::get_Smoothness(long * pVal)
  303. {
  304.     *pVal = m_Display.m_nSmoothness;
  305.     return S_OK;
  306. }
  307.  
  308. STDMETHODIMP CFireTabCtrl::put_Smoothness(long newVal)
  309. {
  310.     if( newVal < 0 || newVal > 5 )
  311.     {
  312.         Error(_T("Please insert a number between 0 and 5"));
  313.         return DISP_E_EXCEPTION;
  314.     }
  315.     m_Display.m_nSmoothness = newVal;
  316.     return S_OK;
  317. }
  318.  
  319. STDMETHODIMP CFireTabCtrl::get_Distribution(long * pVal)
  320. {
  321.     *pVal = m_Display.m_nDistribution;
  322.     return S_OK;
  323. }
  324.  
  325. STDMETHODIMP CFireTabCtrl::put_Distribution(long newVal)
  326. {
  327.     if( newVal < 0 || newVal > 10 )
  328.     {
  329.         Error(_T("Please insert a number between 0 and 10"));
  330.         return DISP_E_EXCEPTION;
  331.     }
  332.     m_Display.m_nDistribution = newVal;
  333.     return S_OK;
  334. }
  335.  
  336. STDMETHODIMP CFireTabCtrl::get_Chaos(long * pVal)
  337. {
  338.     *pVal = m_Display.m_nChaos;
  339.     return S_OK;
  340. }
  341.  
  342. STDMETHODIMP CFireTabCtrl::put_Chaos(long newVal)
  343. {
  344.     if( newVal < 1 || newVal > 100 )
  345.     {
  346.         Error(_T("Please insert a number between 1 and 100"));
  347.         return DISP_E_EXCEPTION;
  348.     }
  349.     m_Display.m_nChaos = newVal;
  350.     return S_OK;
  351. }
  352.  
  353. STDMETHODIMP CFireTabCtrl::get_StartTab(long * pVal)
  354. {
  355.     *pVal = m_Display.m_curColor;
  356.     return S_OK;
  357. }
  358.  
  359. STDMETHODIMP CFireTabCtrl::put_StartTab(long newVal)
  360. {
  361.     ATLTRACE(_T("CFireTabCtrl::put_StartTab()\n"));
  362.     if( newVal < 0 || newVal > 2 )
  363.     {
  364.         Error(_T("Please insert a number between 0 and 2"));
  365.         return DISP_E_EXCEPTION;
  366.     }
  367.     m_Display.m_curColor = newVal;
  368.     return S_OK;
  369. }
  370.  
  371. STDMETHODIMP CFireTabCtrl::AboutBox()
  372. {
  373.     CAboutBox dlg;
  374.     dlg.DoModal();
  375.     return S_OK;
  376. }
  377.  
  378.  
  379. LRESULT CAboutBox::OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  380. {
  381.     EndDialog(wID);
  382.     return 0;
  383. }
  384.