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 / TOOLBAR1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-18  |  10.5 KB  |  387 lines

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