home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / RICHCT / TOOLBAR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-05  |  6.9 KB  |  249 lines

  1. /*  Project toolctrl
  2.     DHB Software
  3.     Copyright ⌐ 1996. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    listree.apx Application
  6.     FILE:         toolbar.cpp
  7.     AUTHOR:       David H. Borg
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of ToolBar (TWindow).
  13. */
  14.  
  15.  
  16. #include <owl\owlpch.h>
  17. #pragma hdrstop
  18.  
  19. #include "toolbar.h"
  20.  
  21.  
  22. // A command enabler for the toolbar. This makes the toolbar automatically
  23. // enable and disable the buttons, just like the OWL TControlBar.
  24. class ToolBarButtonEnabler : public TCommandEnabler {
  25.     public:
  26.         ToolBarButtonEnabler(HWND hWndReceiver, ToolBar* tb, uint id)
  27.                 : TCommandEnabler( id, hWndReceiver) {
  28.             toolBar = tb;
  29.         }
  30.  
  31.     // override member functions of TCommandEnabler
  32.     void  Enable (bool enable);
  33.     void  SetText (const char far* text);
  34.     void  SetCheck (int state);
  35.  
  36.     protected:
  37.         ToolBar*  toolBar;
  38. };
  39.  
  40.  
  41. void ToolBarButtonEnabler::Enable(bool enable)
  42. {
  43.     TCommandEnabler::Enable(enable);
  44.     toolBar->SendMessage( TB_ENABLEBUTTON, Id, enable);
  45. }
  46.  
  47.  
  48. void ToolBarButtonEnabler::SetText(const char far* /*text*/)
  49. {
  50.     // implement later, do nothing for now
  51. }
  52.  
  53.  
  54. void ToolBarButtonEnabler::SetCheck(int state)
  55. {
  56.     toolBar->SendMessage( TB_CHECKBUTTON, Id, state);
  57. }
  58.  
  59. // Initialization parameters for tbButton array. Include one line per button.
  60. // A better way to do this would be to implement a ToolBar::InsertButton(...)
  61. // function to insert each button in TApplication::InitMainWindow()
  62. static TBBUTTON tbButton[] = {
  63.     { STD_FILENEW, CM_FILENEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0 },
  64.     { STD_FILEOPEN, CM_FILEOPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  65.     { STD_FILESAVE, CM_FILESAVE1, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  66.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0, 0},
  67.     { STD_PRINT, CM_FILEPRINT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  68.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0, 0},
  69.     { STD_UNDO, CM_EDITUNDO, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  70.     { STD_CUT, CM_EDITCUT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  71.     { STD_COPY, CM_EDITCOPY, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  72.     { STD_PASTE, CM_EDITPASTE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  73.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0, 0},
  74.     { STD_DELETE, CM_EDITDELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  75.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0, 0},
  76.     { STD_FIND, CM_EDITFIND, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0},
  77.     { STD_REPLACE, CM_EDITREPLACE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0, 0}
  78.     };  // end of inittbbuttontemplate
  79.  
  80. const int toolBarItems = sizeof( tbButton) / sizeof( tbButton[0]);
  81. const int nStdItems = 14;        // there are 15 standard bitmap items
  82. const int nViewItems = 12;        // there are 12 view bitmap items
  83. const int firstViewButton = 4;    // first view button in structure
  84.  
  85. //
  86. // Build a response table for all messages/commands handled
  87. // by the application.
  88. //
  89. DEFINE_RESPONSE_TABLE1(ToolBar, TWindow)
  90. //{{ToolBarRSP_TBL_BEGIN}}
  91.     EV_NOTIFY_AT_CHILD(BN_CLICKED, BnClicked),
  92.     EV_MESSAGE( WM_NOTIFY, EvCommonControlNotify),
  93.     EV_WM_PAINT,
  94. //{{ToolBarRSP_TBL_END}}
  95. END_RESPONSE_TABLE;
  96.  
  97.  
  98. //{{ToolBar Implementation}}
  99.  
  100.  
  101. // the tab control is derived from TWindow with specific Attributes
  102. ToolBar::ToolBar (TWindow* parent, int id, int w, int h, TModule* module):
  103.     TWindow(parent, 0, module)
  104. {
  105.     Attr.Id = id;
  106.     Attr.X = 0;
  107.     Attr.Y = 0;
  108.     Attr.W = w;
  109.     Attr.H = h;
  110.     Attr.ExStyle = 0;
  111.     Attr.Style = WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | CCS_NOMOVEY;
  112. }
  113.  
  114.  
  115. // the default constructor is not used
  116. ToolBar::ToolBar (TWindow* parent, const char far* title, TModule* module):
  117.     TWindow(parent, title, module)
  118. {
  119.     // INSERT>> Your constructor code here.
  120.  
  121. }
  122.  
  123.  
  124. ToolBar::~ToolBar ()
  125. {
  126.     Destroy();
  127.  
  128.     // INSERT>> Your destructor code here.
  129.  
  130. }
  131.  
  132.  
  133. void ToolBar::EvPaint ()
  134. {
  135.     // Let Common Control paint the window, don't call TWindow::EvPaint()
  136.     DefaultProcessing();
  137. }
  138.  
  139.  
  140. char far* ToolBar::GetClassName ()
  141. {
  142.     // return the Windows 95 Toolbar common control class name
  143.     // this is what makes the window a tool bar
  144.     return TOOLBARCLASSNAME;
  145. }
  146.  
  147.  
  148. void ToolBar::SetupWindow ()
  149. {
  150.     TWindow::SetupWindow();
  151.  
  152.     // INSERT>> Your code here.
  153.  
  154.     SetupToolBar();
  155. }
  156.  
  157.  
  158. // Create the toolbar control and add the buttons and commands.
  159. void ToolBar::SetupToolBar ()
  160. {
  161.     // Tell the control the size of button structure.
  162.     SendMessage( TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
  163.  
  164.     // Initialize structures that identify the bitmaps.
  165.     tbStandardBmp.hInst = HINST_COMMCTRL;
  166.     tbStandardBmp.nID = IDB_STD_SMALL_COLOR;
  167.  
  168.     // initialize toolbar with large bitmaps
  169.     stdBitmap = SendMessage( TB_ADDBITMAP, (WPARAM)nStdItems, (LPARAM)&tbStandardBmp);
  170.  
  171.     // Add the standard bitmap base number to the view offsets from the template.
  172.     // note: this is not needed for the standard bitmaps if added first.
  173. //    for( int j = firstViewButton; j < toolBarItems; j++){
  174. //        tbButton[j].iBitmap += viewBitmap;
  175. //    }
  176.  
  177.     // Send the initialized buttons structure to the common controls dll.
  178.     SendMessage( TB_ADDBUTTONS, (WPARAM)toolBarItems, (LPARAM)(LPTBBUTTON)tbButton);
  179.  
  180.     // Automatically size of the toolbar to the button size.
  181.     SendMessage( TB_AUTOSIZE, 0, 0);
  182. }
  183.  
  184.  
  185. // This function handles the messages sent by Windows 95 common controls.
  186. LRESULT ToolBar::EvCommonControlNotify( WPARAM /*wParam*/, LPARAM lParam)
  187. {
  188.     LRESULT retVal = false;
  189.     NMHDR FAR *pNmhdr = (NMHDR FAR *)lParam;
  190.     LPTBNOTIFY lpTbNotify = (LPTBNOTIFY)lParam;
  191.  
  192.     switch( pNmhdr->code){
  193.         case TTN_NEEDTEXT:         // process tool tip control message
  194.             {
  195.                 LPTOOLTIPTEXT lpText;
  196.  
  197.                 lpText = (LPTOOLTIPTEXT)lParam;
  198.                 lpText->hinst = GetModule()->GetInstance();
  199.                 lpText->lpszText = MAKEINTRESOURCE(lpText->hdr.idFrom);
  200.             }
  201.             break;
  202.         case TBN_GETBUTTONINFO:    // Customize Toolbar dialog nees this info
  203.             if (lpTbNotify->iItem < toolBarItems) {
  204.                 lpTbNotify->tbButton = tbButton[lpTbNotify->iItem];
  205.                 retVal = true;
  206.             }
  207.             break;
  208.         case TBN_QUERYINSERT:    // Can Customize Dialog insert?
  209.             retVal = true;
  210.             break;
  211.         case TBN_QUERYDELETE:    // Can Customize Dialog delete?
  212.             retVal = true;
  213.             break;
  214.         default:
  215.             break;
  216.     }
  217.     return retVal;
  218. }
  219.  
  220.  
  221. // Pressing a toolbar button causes a BN_CLICKED message to be sent.
  222. // Re-transmit the BN_CLICKED Id as CM_... message to simulate menu selections.
  223. void ToolBar::BnClicked()
  224. {
  225.     TCurrentEvent& currentEvent = GetCurrentEvent();
  226.  
  227.     // Post button Id to simulate a menu command.
  228.     Parent->PostMessage( WM_COMMAND, LOWORD(currentEvent.WParam));
  229. }
  230.  
  231.  
  232. // IdleAction takes care of button enabling through ToolBarButtonEnabler.
  233. bool ToolBar::IdleAction (long idleCount)
  234. {
  235.     if (idleCount == 0) {
  236.         for( int i=0; i < toolBarItems; i++){
  237.             if( tbButton[i].fsStyle != TBSTYLE_BUTTON)
  238.                 continue;
  239.             // Must use SendMessage here since a ptr to a temp is passed
  240.             Parent->SendMessage( WM_COMMAND_ENABLE, 0,
  241.                 (LPARAM)&ToolBarButtonEnabler( Parent->HWindow, this,
  242.                 tbButton[i].idCommand));
  243.         }
  244.     }
  245.     return TWindow::IdleAction(idleCount);
  246. }
  247.  
  248.  
  249.