home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / MULTIPAD.PAK / TOOLBAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.6 KB  |  258 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: toolbar.c
  9. //
  10. //  PURPOSE: Handles general routines for the Toolbar control
  11. //
  12. //  FUNCTIONS:
  13. //    CreateTBar    - Creates the Toolbar control for the sample.
  14. //    MsgNotify     - Handles the WM_NOTIFY message that gets sent to
  15. //                    the parent window to get ToolTip Text.
  16. //  COMMENTS:
  17. //
  18.  
  19. #include <windows.h>            // required for all Windows applications
  20. #include <windowsx.h>
  21. #include <commctrl.h>           // prototypes and defs for common controls
  22. #include "globals.h"            // prototypes specific to this application
  23. #include "toolbar.h"            // prototypes and #defines for toolbar.c
  24. #include "resource.h"
  25.  
  26. // Global Variables for toolbar control.
  27.  
  28. HWND hWndToolbar;
  29.  
  30. //  **TODO**  Change the following values to match your toolbar bitmap
  31. //
  32. // NUMIMAGES    = Number of images in toolbar.bmp.  Note that this is not
  33. //                the same as the number of elements on the toolbar.
  34. // IMAGEWIDTH   = Width of a single button image in toolbar.bmp
  35. // IMAGEHEIGHT  = Height of a single button image in toolbar.bmp
  36. // BUTTONWIDTH  = Width of a button on the toolbar (zero = default)
  37. // BUTTONHEIGHT = Height of a button on the toolbar (zero = default)
  38.  
  39. #define NUMIMAGES       9
  40. #define IMAGEWIDTH      18
  41. #define IMAGEHEIGHT     17
  42. #define BUTTONWIDTH     0
  43. #define BUTTONHEIGHT    0
  44.  
  45. //  **TODO**  Add/remove entries in the following array to define the 
  46. //            toolbar buttons (see documentation for TBBUTTON).
  47.  
  48. TBBUTTON tbButton[] =           // Array defining the toolbar buttons
  49.  
  50.     // the struct goes like this:
  51.     // bitmap index, WM_COMMAND id, state, style, reserved bytes, app data,
  52.     // string index
  53.     // Note that the reserved bytes exist only for Win32 targets
  54. {
  55.      {0, IDM_FILENEW,    TBSTATE_ENABLED,       TBSTYLE_BUTTON, {0, 0}, 0, 0},
  56.      {1, IDM_FILEOPEN,   TBSTATE_ENABLED,       TBSTYLE_BUTTON, {0, 0}, 0, 0},
  57.      {2, IDM_FILESAVE,   TBSTATE_INDETERMINATE, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  58.      {6, IDM_FILEPRINT,  TBSTATE_INDETERMINATE, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  59.      {0, 0,              TBSTATE_ENABLED,       TBSTYLE_SEP,    {0, 0}, 0, 0},
  60.      {3, IDM_EDITCUT,    TBSTATE_INDETERMINATE, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  61.      {4, IDM_EDITCOPY,   TBSTATE_INDETERMINATE, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  62.      {5, IDM_EDITPASTE,  TBSTATE_INDETERMINATE, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  63.      {0, 0,              TBSTATE_ENABLED,       TBSTYLE_SEP,    {0, 0}, 0, 0},
  64.      {7, IDM_ABOUT,      TBSTATE_ENABLED,       TBSTYLE_BUTTON, {0, 0}, 0, 0}
  65. };
  66.  
  67.  
  68. //
  69. //  FUNCTION: CreateTBar(HWND)
  70. //
  71. //  PURPOSE:  Calls CreateToolBarEx()
  72. //
  73. //
  74. //  PARAMETERS:
  75. //
  76. //    hwnd - Window handle : Used for the hWndParent parameter of the control.
  77. //
  78. //  RETURN VALUE:
  79. //
  80. //    If toolbar control was created successfully Return TRUE,
  81. //    else returns FALSE.
  82. //
  83. //  COMMENTS:
  84. //
  85. //
  86.  
  87. BOOL CreateTBar(HWND hwnd)
  88. {
  89.     hWndToolbar = CreateToolbarEx(hwnd,
  90.                                   WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS,
  91.                                   IDM_TOOLBAR,
  92.                                   NUMIMAGES,
  93.                                   hInst,
  94.                                   IDB_BMP,
  95.                                   tbButton,
  96.                                   sizeof(tbButton)/sizeof(TBBUTTON),
  97.                                   BUTTONWIDTH,
  98.                                   BUTTONHEIGHT,
  99.                                   IMAGEWIDTH,
  100.                                   IMAGEHEIGHT,
  101.                                   sizeof(TBBUTTON));
  102.  
  103.     return (hWndToolbar != NULL);
  104. }
  105.  
  106.  
  107. //
  108. //  FUNCTION: MsgNotify(HWND, UINT, WPARAM, LPARAM)
  109. //
  110. //  PURPOSE:  WM_NOTIFY is sent to the parent window to get the
  111. //            tooltip text assoc'd with that toolbar button.
  112. //
  113. //  PARAMETERS:
  114. //
  115. //    hwnd      - Window handle  (Unused)
  116. //    uMessage  - Message number (Unused)
  117. //    wparam    - Extra data     (Unused)
  118. //    lparam    - TOOLTIPTEXT FAR*
  119. //
  120. //  RETURN VALUE:
  121. //    Always returns 0 - Message handled
  122. //
  123. //
  124. //  COMMENTS:
  125. //    This message fills in the lpszText field of the TOOLTIPTEXT
  126. //    structure if code == TTN_NEEDTEXT
  127. //
  128.  
  129. #pragma argsused
  130. LRESULT MsgNotify(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  131. {
  132.     LPTOOLTIPTEXT lpToolTipText;
  133.     static char   szBuffer[64];
  134.  
  135.     lpToolTipText = (LPTOOLTIPTEXT)lparam;
  136.     if (lpToolTipText->hdr.code == TTN_NEEDTEXT)
  137.     {
  138.         LoadString(hInst,
  139.                    lpToolTipText->hdr.idFrom,   // string ID == command ID
  140.                    szBuffer,
  141.                          sizeof(szBuffer));
  142.  
  143.         lpToolTipText->lpszText = szBuffer;
  144.     }
  145.     return 0;
  146. }
  147.  
  148. //
  149. //  FUNCTION: UpdateButton()
  150. //
  151. //  PURPOSE:  This routine will update the state of the given button.
  152. //            
  153. //
  154. //  PARAMETERS:
  155. //
  156. //    iID    - ID of the button to update
  157. //    iFlags - Enable/Disabled flag used to update the button
  158. //
  159. //  RETURN VALUE:
  160. //    void
  161. //
  162. //  COMMENTS:
  163. //
  164.  
  165. void UpdateButton(UINT iID, UINT iFlags)
  166. {
  167.     int iCurrentFlags;
  168.  
  169.     iCurrentFlags = (int)SendMessage(hWndToolbar, 
  170.                                      TB_GETSTATE, 
  171.                                      iID, 0L);
  172.  
  173.     if (iCurrentFlags & TBSTATE_PRESSED)
  174.         iFlags |= TBSTATE_PRESSED;
  175.  
  176.     SendMessage(hWndToolbar, 
  177.                 TB_SETSTATE, 
  178.                 iID, 
  179.                 MAKELPARAM(iFlags, 0));
  180.     return;
  181. }
  182.  
  183.  
  184. //
  185. //  FUNCTION: UpdateToolbar()
  186. //
  187. //  PURPOSE:  This function will enable/disable the toolbar buttons
  188. //            to reflect their current state.  The toolbar buttons
  189. //            reflect the state of the active MDI child window.
  190. //            
  191. //
  192. //  PARAMETERS:
  193. //
  194. //    void
  195. //
  196. //  RETURN VALUE:
  197. //
  198. //    void
  199. //
  200. //  COMMENTS: Called from OnIdle so that the buttons are constantly
  201. //            updated to reflect their current state.  See WINMAIN.C
  202. //            for OnIdle details.
  203. //
  204.  
  205. void UpdateToolbar(VOID)
  206. {
  207.     UINT ichStart;
  208.     UINT ichEnd;
  209.     UINT tf = TBSTATE_INDETERMINATE;
  210.  
  211.     // Enable/Disable Paste button
  212.     if (cOpen && OpenClipboard(hwndMDIClient))
  213.     {
  214.         if (IsClipboardFormatAvailable(CF_TEXT) ||
  215.             IsClipboardFormatAvailable(CF_OEMTEXT))
  216.         {
  217.             tf = TBSTATE_ENABLED;
  218.         }
  219.     }
  220.     CloseClipboard();
  221.     UpdateButton(IDM_EDITPASTE, tf);
  222.  
  223.     // Enable/Disable Cut/Copy/Delete button
  224.     if (cOpen)
  225.     {
  226.         SendMessage(GetEditWindow(NULL), 
  227.                     EM_GETSEL, 
  228.                     (WPARAM)&ichStart, 
  229.                     (LPARAM)&ichEnd);
  230.         tf = (ichEnd != ichStart) ? TBSTATE_ENABLED : TBSTATE_INDETERMINATE;
  231.     }
  232.     else
  233.        tf = TBSTATE_INDETERMINATE;
  234.  
  235.     UpdateButton(IDM_EDITCUT, tf);
  236.     UpdateButton(IDM_EDITCOPY, tf);
  237.     UpdateButton(IDM_EDITCLEAR, tf);
  238.  
  239.     // Enable/Disable Save button
  240.     if (cOpen)
  241.         tf = SendMessage(GetEditWindow(NULL), 
  242.                          EM_GETMODIFY, 0, 0L) ? TBSTATE_ENABLED :TBSTATE_INDETERMINATE;
  243.     else
  244.        tf = TBSTATE_INDETERMINATE;
  245.  
  246.     UpdateButton(IDM_FILESAVE, tf);
  247.  
  248.     // Enable/Disable Save As button
  249.     if (cOpen)
  250.         tf = SendMessage(GetEditWindow(NULL),
  251.                          WM_GETTEXTLENGTH, 0, 0L) ? TBSTATE_ENABLED : TBSTATE_INDETERMINATE;
  252.     else
  253.        tf = TBSTATE_INDETERMINATE;
  254.  
  255.     UpdateButton(IDM_FILESAVEAS, tf);
  256.     UpdateButton(IDM_FILEPRINT, tf);
  257. }
  258.