home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Basic / Visual Basic.60 / COMMON / TOOLS / VCM / VCM.MDB / VcmComponentContainer / 02_Cabinet / TOOLBAR2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-18  |  10.2 KB  |  376 lines

  1. // ToolBar2.cpp : implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "afxpriv.h"
  16. #include "CmnCtrl1.h"
  17. #include "Toolbar2.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CPaletteBar
  27.  
  28. CPaletteBar::CPaletteBar() : m_pTBButtons(NULL)
  29. {
  30.  
  31. }
  32.  
  33. CPaletteBar::~CPaletteBar()
  34. {
  35.     if (m_pTBButtons)
  36.         delete []m_pTBButtons;
  37.  
  38. }
  39.  
  40.  
  41. BEGIN_MESSAGE_MAP(CPaletteBar, CToolBarCtrl)
  42.     //{{AFX_MSG_MAP(CPaletteBar)
  43.     ON_NOTIFY_RANGE( TTN_NEEDTEXTA, ID_ERASE, ID_ELLIPSE, OnNeedTextA)
  44.     ON_NOTIFY_RANGE( TTN_NEEDTEXTW, ID_ERASE, ID_ELLIPSE, OnNeedTextW)
  45.     //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47.  
  48.  
  49. BOOL CPaletteBar::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID )
  50. {
  51.     BOOL bRet = CToolBarCtrl::Create(dwStyle, rect, pParentWnd, nID);
  52.  
  53.     m_nButtonCount = ID_ELLIPSE - ID_ERASE + 1;
  54.  
  55.     VERIFY(AddBitmap(m_nButtonCount,IDR_PALETTEBAR) != -1);
  56.  
  57.     m_pTBButtons = new TBBUTTON[m_nButtonCount];
  58.  
  59.     for (int nIndex = 0; nIndex < m_nButtonCount; nIndex++)
  60.     {
  61.         CString string;
  62.         string.LoadString(nIndex + ID_ERASE);
  63.  
  64.         // Add second '\0'
  65.         int nStringLength = string.GetLength() + 1;
  66.         TCHAR* pString = string.GetBufferSetLength(nStringLength);
  67.         pString[nStringLength] = 0;
  68.  
  69.         VERIFY((m_pTBButtons[nIndex].iString = AddStrings(pString)) != -1);
  70.  
  71.         string.ReleaseBuffer();
  72.  
  73.  
  74.         m_pTBButtons[nIndex].fsState = TBSTATE_ENABLED;
  75.         m_pTBButtons[nIndex].fsStyle = TBSTYLE_CHECKGROUP;
  76.         m_pTBButtons[nIndex].dwData = 0;
  77.         m_pTBButtons[nIndex].iBitmap = nIndex;
  78.         m_pTBButtons[nIndex].idCommand = nIndex + ID_ERASE;
  79.  
  80.     }
  81.  
  82.  
  83.     for (nIndex = 0; nIndex < m_nButtonCount; nIndex++)
  84.     {
  85.         VERIFY(AddButtons(1,&m_pTBButtons[nIndex]));
  86.     }
  87.  
  88.  
  89.     return bRet;
  90. }
  91.  
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CPaletteBar message handlers
  95.  
  96. // MFC routes the notifications sent to the parent of the control to
  97. // the control before the parent can process the notification.
  98. // The control object can handle the notification or ignore it.
  99. // If the notification is handled then return TRUE. Otherwise MFC
  100. // will route it to the parent of the control.
  101.  
  102. BOOL CPaletteBar::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
  103. {
  104.     if (message == WM_NOTIFY)
  105.     {
  106.         NMHDR *pNMHDR = (NMHDR *) lParam;
  107.         switch (pNMHDR->code)
  108.         {
  109.         case TBN_BEGINADJUST :
  110.             return BeginAdjust(wParam,  lParam,  pLResult);
  111.  
  112.         case TBN_BEGINDRAG:
  113.             return BeginDrag(wParam,  lParam,  pLResult);
  114.  
  115.         case TBN_CUSTHELP:
  116.             return CustomizeHelp(wParam,  lParam,  pLResult);
  117.  
  118.         case TBN_ENDADJUST:
  119.             return EndAdjust(wParam,  lParam,  pLResult);
  120.  
  121.         case TBN_ENDDRAG:
  122.             return EndDrag(wParam,  lParam,  pLResult);
  123.  
  124.         case TBN_GETBUTTONINFO:
  125.             return GetButtonInfo(wParam,  lParam,  pLResult);
  126.  
  127.         case TBN_QUERYDELETE:
  128.             return QueryDelete(wParam,  lParam,  pLResult);
  129.  
  130.         case TBN_QUERYINSERT:
  131.             return QueryInsert(wParam,  lParam,  pLResult);
  132.  
  133.         case TBN_RESET:
  134.             return Reset(wParam, lParam,  pLResult);
  135.  
  136.         case TBN_TOOLBARCHANGE:
  137.             return ToolBarChange(wParam, lParam, pLResult);
  138.         }
  139.     }
  140.  
  141.     return CToolBarCtrl::OnChildNotify(message, wParam, lParam, pLResult);
  142. }
  143.  
  144. BOOL CPaletteBar::BeginAdjust(WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
  145. {
  146.     TRACE(_T("TBN_BEGINADJUST\n"));
  147.  
  148.     // the customize dialog box is about to be displayed
  149.  
  150.     // save toolbar state before customization using the dialog
  151.     // Use this information to restore the state if reset button is pressed
  152.     SaveState(HKEY_CURRENT_USER,_T("Software\\Microsoft\\VC40\\Samples\\CtrlDemo"),_T("Palette Tool Bar"));
  153.  
  154.     return TRUE;
  155. }
  156.  
  157. BOOL CPaletteBar::BeginDrag(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  158. {
  159.     TRACE(_T("TBN_BEGINDRAG\n"));
  160.  
  161.     // we are not implementing custon drag and drop
  162.     * pLResult = FALSE;
  163.     return FALSE;
  164. }
  165.  
  166. BOOL CPaletteBar::CustomizeHelp(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  167. {
  168.     TRACE(_T("TBN_CUSTHELP\n"));
  169.  
  170.     // Sample displays a message box but a valid help topic
  171.     // can be displayed for the customize dialog for this toolbar
  172.     AfxMessageBox(_T("Help not implemented!"));
  173.  
  174.     return TRUE;
  175. }
  176.  
  177. BOOL CPaletteBar::EndAdjust(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  178. {
  179.     TRACE(_T("TBN_ENDADJUST\n"));
  180.  
  181.     // the customize dialog box has been closed
  182.  
  183.     return TRUE;
  184. }
  185.  
  186. BOOL CPaletteBar::EndDrag(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  187. {
  188.     TRACE(_T("TBN_ENDDRAG\n"));
  189.  
  190.     // Code to handle custom drag and drop. This message indicates that
  191.     // the item is being dropped
  192.     * pLResult = FALSE;
  193.     return TRUE;
  194. }
  195.  
  196. BOOL CPaletteBar::GetButtonInfo(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  197. {
  198.     // This notification message has to be handled correctly if
  199.     // all operations in the custom dialogbox has to function correctly
  200.     // We have to supply information for the button specified by pTBN->tbButton
  201.     //
  202.     // This notification is sent in the following cases
  203.     //
  204.     // After TBN_BEGINADJUST the control sends these notifications until
  205.     // * pLResult is TRUE. We have to supply valid values when this value is
  206.     // set to TRUE. Here the control is collecting information for all
  207.     // the buttons that have to be displayed in the dialog box
  208.     //
  209.     // The control sends this notification to get information about
  210.     // a button if the user is trying to add it to the toolbar or
  211.     // rearranging the buttons on the toolbar from within the dialog
  212.  
  213.     TRACE(_T("TBN_GETBUTTONINFO\n"));
  214.  
  215.     TBNOTIFY *pTBN = (TBNOTIFY *) lParam;
  216.  
  217.     if (pTBN->iItem >= m_nButtonCount)
  218.     {
  219.         * pLResult = FALSE;
  220.     }
  221.     else
  222.     {
  223.         CString buffer;
  224.         buffer.LoadString(pTBN->iItem + ID_ERASE);
  225.  
  226.         // set the string for the button
  227.         // truncate the string if its length is greater than the buffer
  228.         // supplied by the toolbar
  229.         _tcsncpy(pTBN->pszText, buffer, pTBN->cchText - 1);
  230.         pTBN->pszText[pTBN->cchText - 1] = '\0';
  231.  
  232.         // set the button info
  233.         pTBN->tbButton = m_pTBButtons[pTBN->iItem];
  234.  
  235.         // valid values are structure
  236.         *pLResult = TRUE;
  237.     }
  238.  
  239.     return TRUE;
  240. }
  241.  
  242. BOOL CPaletteBar::QueryDelete(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  243. {
  244.     TRACE(_T("TBN_QUERYDELETE\n"));
  245.  
  246.     // in this sample any button can be deleted
  247.     // if a particular button cannot be deleted set *pResult to FALSE for that item
  248.     *pLResult = TRUE;
  249.     return TRUE;
  250. }
  251.  
  252. BOOL CPaletteBar::QueryInsert(WPARAM wParam, LPARAM  lParam, LRESULT* pLResult)
  253. {
  254.     TRACE(_T("TBN_QUERYINSERT\n"));
  255.  
  256.     // in this sample buttons can be inserted at any location on the
  257.     // toolbar
  258.     *pLResult = TRUE;
  259.     return TRUE;
  260. }
  261.  
  262. BOOL CPaletteBar::Reset(WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
  263. {
  264.     TRACE(_T("TBN_RESET\n"));
  265.  
  266.     // User has pressed the reset button
  267.     // restore the state of the toolbar to the state it was before customization
  268.     RestoreState(HKEY_CURRENT_USER,_T("Software\\Microsoft\\VC40\\Samples\\CtrlDemo"),_T("Palette Tool Bar"));
  269.  
  270.     *pLResult = TRUE;
  271.     return TRUE;
  272. }
  273.  
  274. BOOL CPaletteBar::ToolBarChange(WPARAM wParam, LPARAM lParam,LRESULT* pLResult)
  275. {
  276.     TRACE(_T("TBN_TOOLBARCHANGE\n"));
  277.  
  278.     // the toolbar has changed
  279.     return TRUE;
  280. }
  281.  
  282.  
  283. // Helper function for tooltips
  284.  
  285. CString CPaletteBar::NeedText( UINT nID, NMHDR * pNotifyStruct, LRESULT * lResult )
  286. {
  287.     LPTOOLTIPTEXT lpTTT = (LPTOOLTIPTEXT)pNotifyStruct ;
  288.     ASSERT(nID == lpTTT->hdr.idFrom);
  289.  
  290.     CString toolTipText;
  291.     toolTipText.LoadString(nID);
  292.  
  293.     // szText length is 80
  294.     int nLength = (toolTipText.GetLength() > 79) ? 79 : toolTipText.GetLength();
  295.  
  296.     toolTipText = toolTipText.Left(nLength);
  297.  
  298.     return toolTipText;
  299. }
  300.  
  301.  
  302. void CPaletteBar::OnNeedTextW( UINT nID, NMHDR * pNotifyStruct, LRESULT * lResult )
  303. {
  304.     CString toolTipText = NeedText(nID, pNotifyStruct, lResult);
  305.  
  306.     LPTOOLTIPTEXTW lpTTT = (LPTOOLTIPTEXTW)pNotifyStruct;
  307.  
  308. #ifdef _UNICODE
  309.     _tcsncpy(lpTTT->szText,(LPCTSTR)toolTipText, toolTipText.GetLength() + 1);
  310. #else
  311.     mbstowcs(lpTTT->szText,(LPCTSTR)toolTipText, toolTipText.GetLength() + 1);
  312. #endif
  313. }
  314.  
  315. void CPaletteBar::OnNeedTextA( UINT nID, NMHDR * pNotifyStruct, LRESULT * lResult )
  316. {
  317.     CString toolTipText = NeedText(nID, pNotifyStruct, lResult);
  318.  
  319.     LPTOOLTIPTEXT lpTTT = (LPTOOLTIPTEXT)pNotifyStruct;
  320.  
  321.     _tcscpy(lpTTT->szText,(LPCTSTR)toolTipText);
  322. }
  323.  
  324.  
  325. ///////////////////////////////////////////////////////////////////////
  326. // This has been overridden so we can handle the tooltip TTN_NEEDTEXT//
  327. // notification message                                              //
  328. ///////////////////////////////////////////////////////////////////////
  329.  
  330. BOOL CPaletteBar::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  331. {
  332.     ASSERT(pResult != NULL);
  333.     NMHDR* pNMHDR = (NMHDR*)lParam;
  334.     HWND hWndCtrl = pNMHDR->hwndFrom;
  335.  
  336.     // get the child ID from the window itself
  337.     // UINT nID = _AfxGetDlgCtrlID(hWndCtrl);
  338.  
  339.     //////////////////////////////////////////////////////////////////
  340.     // If TTN_NEEDTEXT we cannot get the ID from the tooltip window //
  341.     //////////////////////////////////////////////////////////////////
  342.  
  343.     int nCode = pNMHDR->code;
  344.  
  345.     //
  346.     // if it is the following notification message
  347.     // nID has to obtained from wParam
  348.     //
  349.  
  350.     if (nCode == TTN_NEEDTEXTA || nCode == TTN_NEEDTEXTW)
  351.     {
  352.         UINT nID;   // = _AfxGetDlgCtrlID(hWndCtrl);
  353.         nID = (UINT)wParam;
  354.  
  355.  
  356.         ASSERT((UINT)pNMHDR->idFrom == (UINT)wParam);
  357.         UNUSED(wParam);  // not used in release build
  358.         ASSERT(hWndCtrl != NULL);
  359.         ASSERT(::IsWindow(hWndCtrl));
  360.  
  361.         if (AfxGetThreadState()->m_hLockoutNotifyWindow == m_hWnd)
  362.             return TRUE;        // locked out - ignore control notification
  363.  
  364.     // reflect notification to child window control
  365.         if (ReflectLastMsg(hWndCtrl, pResult))
  366.             return TRUE;        // eaten by child
  367.  
  368.         AFX_NOTIFY notify;
  369.         notify.pResult = pResult;
  370.         notify.pNMHDR = pNMHDR;
  371.         return OnCmdMsg(nID, MAKELONG(nCode, WM_NOTIFY), ¬ify, NULL);
  372.     }
  373.  
  374.     return CToolBarCtrl::OnNotify(wParam, lParam, pResult);
  375. }
  376.