home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / activedoc / toolbar.h < prev   
C/C++ Source or Header  |  1998-04-03  |  5KB  |  195 lines

  1. // Toolbar.h : toolbar implementation
  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 <commctrl.h>
  14.  
  15. // ID used for the separator
  16. #define ID_SEP 0
  17.  
  18. #define BEGIN_TOOLBAR_MAP(x) public: \
  19.     const static int* _GetToolbarEntries(int& nButtons) { \
  20.     static const int _entries[] = {
  21. #define TOOLBAR_BUTTON(x) x,
  22. #define TOOLBAR_SEPARATOR() ID_SEP,
  23. #define END_TOOLBAR_MAP() }; \
  24.     nButtons = sizeof(_entries)/sizeof(int); \
  25.     return _entries; }
  26.  
  27. template <class T>
  28. class CToolbar
  29. {
  30. public:
  31.     CToolbar()
  32. //      : m_wndToolbar(TOOLBARCLASSNAME, (T*)this, 2)
  33.     {
  34.     }
  35.     void SetupToolbar(IOleInPlaceUIWindow* pInPlaceUIWindow)
  36.     {
  37.         T* pT = static_cast<T*>(this);
  38.         HRESULT         hr;
  39.         HWND            hWnd;
  40.  
  41.         // Negotiate the border space for the toolbar
  42.         if (pInPlaceUIWindow != NULL)
  43.         {
  44.             SetRectEmpty(&m_bw);
  45.             pInPlaceUIWindow->SetBorderSpace(&m_bw);
  46.         }
  47.  
  48.         // Get the frame HWND so that we can parent our toolbar correctly
  49.         pT->m_spInPlaceFrame->GetWindow(&hWnd);
  50.  
  51.         SetRect(&m_bw, 0, 30, 0, 0);
  52.         hr = pT->m_spInPlaceFrame->RequestBorderSpace(&m_bw);
  53.         if (FAILED(hr))
  54.             return;
  55.  
  56.         pT->m_spInPlaceFrame->SetBorderSpace(&m_bw);
  57.  
  58.         // Now we can create the toolbar
  59.         CreateToolbar(hWnd);
  60.  
  61.         ::SendMessage(m_wndToolbar.m_hWnd, TB_AUTOSIZE, 0, 0);
  62.         ::ShowWindow(m_wndToolbar.m_hWnd, SW_SHOW);
  63.         // Set the window we want the toolbar to send it's messages to
  64.         SendMessage(m_wndToolbar.m_hWnd, TB_SETPARENT, (WPARAM)pT->m_hWnd, 0);
  65.     }
  66.     void MoveToolbar(LPCRECT pRect = NULL)
  67.     {
  68.         if (m_wndToolbar.m_hWnd == NULL)
  69.             return;
  70.  
  71.         RECT    rect;
  72.         HRESULT hr;
  73.  
  74.         T* pT = static_cast<T*>(this);
  75.         hr = pT->m_spInPlaceFrame->RequestBorderSpace(&m_bw);
  76.         if (FAILED(hr))
  77.             return;
  78.  
  79.         pT->m_spInPlaceFrame->SetBorderSpace(&m_bw);
  80.  
  81.         // Get the border if we don't have it
  82.         if (pRect == NULL)
  83.             pT->m_spInPlaceFrame->GetBorder(&rect);
  84.         else
  85.             rect = *pRect;
  86.  
  87.         rect.left   += m_bw.left;
  88.         rect.bottom = rect.top + m_bw.top;
  89.  
  90.         // Now move the toolbar
  91.         m_wndToolbar.MoveWindow(&rect);
  92.     }
  93.     void CreateToolbar(HWND hParent)
  94.     {
  95.         InitCommonControls();
  96.  
  97.         RECT rect;
  98.         T* pT = static_cast<T*>(this);
  99.         pT->m_spInPlaceFrame->GetBorder(&rect);
  100.  
  101.         int nLeft   = rect.left + m_bw.left;
  102.         int nTop    = rect.top;
  103.         int nRight  = rect.right;
  104.         int nBottom = rect.top + m_bw.top;
  105.  
  106. #if 0
  107.         m_wndToolbar.Create(hParent, &rect, NULL,
  108.             WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS,
  109.             0, IDR_TOOLBAR1);
  110. #else
  111.         m_wndToolbar.m_hWnd = CreateWindowEx(
  112.                 0,
  113.                 TOOLBARCLASSNAME,
  114.                 NULL,
  115.                 WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS |
  116.                 CCS_TOP  | CCS_NOMOVEX | CCS_NORESIZE ,
  117.                 nLeft, nTop, nRight-nLeft, nBottom-nTop,
  118.                 hParent,
  119.                 (HMENU)IDR_TOOLBAR1,
  120.                 _Module.GetResourceInstance(),
  121.                 NULL);
  122. #endif
  123.         AddButtons();
  124.     }
  125.  
  126.     void AddButtons()
  127.     {
  128.         int nButtons;
  129.         const int* arrID = T::_GetToolbarEntries(nButtons);
  130.         TBBUTTON* pButton = new TBBUTTON[nButtons];
  131.         TBADDBITMAP tbab;
  132.         int nIndex = 0;
  133.  
  134.         // Send the TB_BUTTONSTRUCTSIZE message, which is required for
  135.         // backward compatibility.
  136.         SendMessage(m_wndToolbar.m_hWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
  137.  
  138.         // Add the bitmap containing button images to the toolbar.
  139.         tbab.hInst = _Module.GetResourceInstance();
  140.         tbab.nID = IDR_TOOLBAR1;
  141.         SendMessage(m_wndToolbar.m_hWnd, TB_ADDBITMAP, nButtons, (WPARAM)&tbab);
  142.  
  143.         // Loop around, adding each button to the TBBUTTON array
  144.         for (int i = 0; i < nButtons; i++)
  145.         {
  146.             pButton[i].fsState   = TBSTATE_ENABLED;
  147.             pButton[i].dwData    = 0;
  148.             pButton[i].iString   = 0;
  149.             if (arrID[i] != ID_SEP)
  150.             {
  151.                 pButton[i].iBitmap   = nIndex;
  152.                 pButton[i].fsStyle   = TBSTYLE_BUTTON;
  153.                 pButton[i].idCommand = arrID[i];
  154.                 nIndex++;
  155.             }
  156.             else
  157.             {
  158.                 pButton[i].fsStyle   = TBSTYLE_BUTTON|TBSTYLE_SEP;
  159.                 pButton[i].iBitmap   = 8;   // Width of separator
  160.                 pButton[i].idCommand = 0;
  161.             }
  162.         }
  163.  
  164.         // Add the buttons
  165.         SendMessage(m_wndToolbar.m_hWnd, TB_ADDBUTTONS, nButtons, (LPARAM)pButton);
  166.         delete [] pButton;
  167.     }
  168.  
  169.     void DestroyToolbar()
  170.     {
  171.         if (m_wndToolbar.m_hWnd != NULL)
  172.         {
  173.             ::DestroyWindow(m_wndToolbar.m_hWnd);
  174.             m_wndToolbar.m_hWnd = NULL;
  175.         }
  176.     }
  177.  
  178.     LRESULT OnToolbarNeedText(WPARAM, LPNMHDR pnmh, BOOL&)
  179.     {
  180.         LPTOOLTIPTEXT lpToolTipText = (LPTOOLTIPTEXT)pnmh;
  181.         static TCHAR szBuf[128];
  182.  
  183.         // Get the string with the same ID as the button from the resource
  184.         LoadString(_Module.GetResourceInstance(),
  185.             lpToolTipText->hdr.idFrom,
  186.             szBuf, sizeof(szBuf) * sizeof(TCHAR));
  187.         lpToolTipText->lpszText = szBuf;
  188.  
  189.         return 0;
  190.     }
  191.  
  192.     CContainedWindow    m_wndToolbar;
  193.     BORDERWIDTHS        m_bw;
  194. };
  195.