home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WRITEPAD.PAK / TOOLBAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.4 KB  |  279 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 "resource.h"
  24. #include "toolbar.h"            // prototypes and #defines for toolbar.c
  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       16
  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_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  58.  
  59.      {0, 0,                    TBSTATE_ENABLED, TBSTYLE_SEP,    {0, 0}, 0, 0},
  60.  
  61.      {3, IDM_EDITCUT,          TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  62.      {4, IDM_EDITCOPY,         TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  63.      {5, IDM_EDITPASTE,        TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  64.  
  65.      {0, 0,                    TBSTATE_ENABLED, TBSTYLE_SEP,    {0, 0}, 0, 0},
  66.  
  67.      {6, IDM_FILEPRINT,        TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  68.      {7, IDM_FILEPRINTPREV,    TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  69.  
  70.      {0, 0,                    TBSTATE_ENABLED, TBSTYLE_SEP,    {0, 0}, 0, 0},
  71.  
  72.      {10,IDM_EDITBOLD,         TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  73.      {11,IDM_EDITITALIC,       TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  74.      {12,IDM_EDITUNDERLINE,    TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  75.  
  76.      {0, 0,                    TBSTATE_ENABLED, TBSTYLE_SEP,    {0, 0}, 0, 0},
  77.  
  78.      {13,IDM_EDITINCREASEFONT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  79.      {14,IDM_EDITDECREASEFONT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  80.      {15,IDM_EDITFONTDIALOG,   TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  81.  
  82.      {0, 0,                    TBSTATE_ENABLED, TBSTYLE_SEP,    {0, 0}, 0, 0},
  83.  
  84.      {8, IDM_ABOUT,            TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0},
  85. };
  86.  
  87.  
  88. //
  89. //  FUNCTION: CreateTBar(HWND)
  90. //
  91. //  PURPOSE:  Calls CreateToolBarEx()
  92. //
  93. //
  94. //  PARAMETERS:
  95. //
  96. //    hwnd - Window handle : Used for the hWndParent parameter of the control.
  97. //
  98. //  RETURN VALUE:
  99. //
  100. //    If toolbar control was created successfully Return TRUE,
  101. //    else returns FALSE.
  102. //
  103. //  COMMENTS:
  104. //
  105. //
  106.  
  107. BOOL CreateTBar(HWND hwnd)
  108. {
  109.     hWndToolbar = CreateToolbarEx(hwnd,
  110.                                   WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS,
  111.                                   IDM_TOOLBAR,
  112.                                   NUMIMAGES,
  113.                                   hInst,
  114.                                   IDB_BMP,
  115.                                   tbButton,
  116.                                   sizeof(tbButton)/sizeof(TBBUTTON),
  117.                                   BUTTONWIDTH,
  118.                                   BUTTONHEIGHT,
  119.                                   IMAGEWIDTH,
  120.                                   IMAGEHEIGHT,
  121.                                   sizeof(TBBUTTON));
  122.  
  123.     return (hWndToolbar != NULL);
  124. }
  125.  
  126.  
  127. //
  128. //  FUNCTION: MsgNotify(HWND, UINT, WPARAM, LPARAM)
  129. //
  130. //  PURPOSE:  WM_NOTIFY is sent to the parent window to get the
  131. //            tooltip text assoc'd with that toolbar button.
  132. //
  133. //  PARAMETERS:
  134. //
  135. //    hwnd      - Window handle  (Unused)
  136. //    uMessage  - Message number (Unused)
  137. //    wparam    - Extra data     (Unused)
  138. //    lparam    - TOOLTIPTEXT FAR*
  139. //
  140. //  RETURN VALUE:
  141. //    Always returns 0 - Message handled
  142. //
  143. //
  144. //  COMMENTS:
  145. //    This message fills in the lpszText field of the TOOLTIPTEXT
  146. //    structure if code == TTN_NEEDTEXT
  147. //
  148.  
  149. #pragma argsused
  150. LRESULT MsgNotify(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  151. {
  152.     LPTOOLTIPTEXT lpToolTipText;
  153.     static char   szBuffer[64];
  154.  
  155.     lpToolTipText = (LPTOOLTIPTEXT)lparam;
  156.     if (lpToolTipText->hdr.code == TTN_NEEDTEXT)
  157.     {
  158.         LoadString(hInst,
  159.                    lpToolTipText->hdr.idFrom,   // string ID == command ID
  160.                    szBuffer,
  161.                          sizeof(szBuffer));
  162.  
  163.         lpToolTipText->lpszText = szBuffer;
  164.     }
  165.     return 0;
  166. }
  167.  
  168. //
  169. //  FUNCTION: UpdateButton()
  170. //
  171. //  PURPOSE:  This routine will update the state of the given button.
  172. //            
  173. //
  174. //  PARAMETERS:
  175. //
  176. //    iID    - ID of the button to update
  177. //    iFlags - Enable/Disabled flag used to update the button
  178. //
  179. //  RETURN VALUE:
  180. //    void
  181. //
  182. //  COMMENTS:
  183. //
  184.  
  185. void UpdateButton(UINT iID, UINT iFlags)
  186. {
  187.     int iCurrentFlags;
  188.  
  189.     iCurrentFlags = (int)SendMessage(hWndToolbar, 
  190.                                      TB_GETSTATE, 
  191.                                      iID, 0L);
  192.  
  193.     if (iCurrentFlags & TBSTATE_PRESSED)
  194.         iFlags |= TBSTATE_PRESSED;
  195.  
  196.     SendMessage(hWndToolbar, 
  197.                 TB_SETSTATE, 
  198.                 iID, 
  199.                 MAKELPARAM(iFlags, 0));
  200.     return;
  201. }
  202.  
  203.  
  204. //
  205. //  FUNCTION: UpdateToolbar()
  206. //
  207. //  PURPOSE:  This function will enable/disable the toolbar buttons
  208. //            to reflect their current state.  The toolbar buttons
  209. //            reflect the state of the active MDI child window.
  210. //            
  211. //
  212. //  PARAMETERS:
  213. //
  214. //    void
  215. //
  216. //  RETURN VALUE:
  217. //
  218. //    void
  219. //
  220. //  COMMENTS: Called from OnIdle so that the buttons are constantly
  221. //            updated to reflect their current state.  See WINMAIN.C
  222. //            for OnIdle details.
  223. //
  224.  
  225. void UpdateToolbar(VOID)
  226. {
  227.     UINT ichStart;
  228.     UINT ichEnd;
  229.     UINT tf = TBSTATE_INDETERMINATE;
  230.  
  231.     // Enable/Disable Paste button
  232.     if (cOpen && OpenClipboard(hwndMDIClient))
  233.     {
  234.         if (IsClipboardFormatAvailable(CF_TEXT) ||
  235.             IsClipboardFormatAvailable(CF_OEMTEXT))
  236.         {
  237.             tf = TBSTATE_ENABLED;
  238.         }
  239.     }
  240.     CloseClipboard();
  241.     UpdateButton(IDM_EDITPASTE, tf);
  242.  
  243.     // Enable/Disable Cut/Copy/Delete button
  244.     if (cOpen)
  245.     {
  246.         SendMessage(GetEditWindow(NULL), 
  247.                     EM_GETSEL, 
  248.                     (WPARAM)&ichStart, 
  249.                     (LPARAM)&ichEnd);
  250.         tf = (ichEnd != ichStart) ? TBSTATE_ENABLED : TBSTATE_INDETERMINATE;
  251.     }
  252.     else
  253.        tf = TBSTATE_INDETERMINATE;
  254.  
  255.     UpdateButton(IDM_EDITCUT, tf);
  256.     UpdateButton(IDM_EDITCOPY, tf);
  257.     UpdateButton(IDM_EDITCLEAR, tf);
  258.  
  259.     // Enable/Disable Save button
  260.     if (cOpen)
  261.         tf = SendMessage(GetEditWindow(NULL), 
  262.                          EM_GETMODIFY, 0, 0L) ? TBSTATE_ENABLED :TBSTATE_INDETERMINATE;
  263.     else
  264.        tf = TBSTATE_INDETERMINATE;
  265.  
  266.     UpdateButton(IDM_FILESAVE, tf);
  267.  
  268.     // Enable/Disable Save As button
  269.     if (cOpen)
  270.         tf = SendMessage(GetEditWindow(NULL),
  271.                          WM_GETTEXTLENGTH, 0, 0L) ? TBSTATE_ENABLED : TBSTATE_INDETERMINATE;
  272.     else
  273.        tf = TBSTATE_INDETERMINATE;
  274.  
  275.     UpdateButton(IDM_FILESAVEAS, tf);
  276.     UpdateButton(IDM_FILEPRINT, tf);
  277.     UpdateButton(IDM_FILEPRINTPREV, tf);
  278. }
  279.