home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / video / vidcap / toolbar.h < prev    next >
Encoding:
Text File  |  1997-10-05  |  22.5 KB  |  364 lines

  1. /**************************************************************************
  2.  *
  3.  *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4.  *  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5.  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6.  *  PURPOSE.
  7.  *
  8.  *  Copyright (C) 1992 - 1997 Microsoft Corporation.  All Rights Reserved.
  9.  *
  10.  **************************************************************************/
  11. /****************************************************************************
  12.  *
  13.  *   toolbar.h: Toolbar include file
  14.  *
  15.  *   Vidcap32 Source code
  16.  *
  17.  ***************************************************************************/
  18.  
  19. /*****************************************************************************
  20. *                                                                            *
  21. *  Program Description: Implements a generic toolbar.                      *
  22. *                                                                            *
  23. *  Here's how to use it:                                                     *
  24. *                                                                            *
  25. *            Include the source files "toolbar.h" and "toolbar.c" in your    *
  26. *  application.                                                              *
  27. *                                                                            *
  28. *            Include a line in your application's RC file that gives a file  *
  29. *  name with a resource id eg. IDBMP_BUTTONS.  This is a .BMP file that      *
  30. *  contains all of the pictures of the buttons you want on your toolbar.     *
  31. *  Also, make a define for your label with a unique value.  If your app has  *
  32. *  more than one toolbar, and all toolbars don't share a bitmap file, then   *
  33. *  you will need several defines.                                            *
  34. *                                                                            *
  35. *  e.g.        IDBMP_BUTTONS     BITMAP     "buttons.bmp"                   *
  36. *              IDBMP_ARROWS      BITMAP     "arrows.bmp"                    *
  37. *                                                                            *
  38. *            This file must have the different buttons across horizontally   *
  39. *  and the different states for these buttons vertically.  Change the        *
  40. *  defines in this header file to match the button names and state names of  *
  41. *  your buttons.  You must include the states listed here, and actually      *
  42. *  you probably won't need to change them at all.  The numbers for a button  *
  43. *  or state are indexes into the bitmap, so the pictures must match.         *
  44. *                                                                            *
  45. *  STATE DESCRIPTIONS:                                                       *
  46. *                       GRAYED:  The button cannot be pressed & is inactive  *
  47. *                           UP:  The button is up                            *
  48. *                         DOWN:  The button is down                          *
  49. *                      FOCUSUP:  The button is up and is the one with focus  *
  50. *                    FOCUSDOWN:  The button is down and is the one with focus*
  51. *                     FULLDOWN:  A checkbox button has this additional state *
  52. *                                where it is all the way down when pressed   *
  53. *                                and when it is let go, it will go into      *
  54. *                                either the UP or DOWN state (maybe focused) *
  55. *                                                                            *
  56. *  When you draw the pictures, make sure to get the right state in the right *
  57. *  vertical position in the bitmap to match the #define's.                   *
  58. *                                                                            *
  59. *  A button can also have a type associated with it:                         *
  60. *                                                                            *
  61. *                 PUSH:  When pressed it goes down, when let go it bounces   *
  62. *                        up.  Therefore, when you aren't currently holding   *
  63. *                        the mouse button or space bar on it, it will        *
  64. *                        ALWAYS be in the up position. It can be in any      *
  65. *                        state except FULLDOWN, which is invalid.            *
  66. *                                                                            *
  67. *             CHECKBOX:  This button can be up or down.  When pushed, it     *
  68. *                        toggles into the opposite state.  However, it       *
  69. *                        is always in the FULLDOWN state when being held     *
  70. *                        down with the mouse button or space bar, and when   *
  71. *                        let go, it will go into the opposite state of what  *
  72. *                        it was in before you pressed it.  E.G.  The button  *
  73. *                        is up.  You press it, and it goes way down. You let *
  74. *                        go, and it comes up a bit, but it's still down.  You*
  75. *                        press it again, and it goes further down before     *
  76. *                        popping all the way up.                             *
  77. *                                                                            *
  78. *                RADIO:  This is a group of buttons that can be up or down,  *
  79. *                        and also have the intermediate step of being        *
  80. *                        FULLDOWN when being held down.  But, when you       *
  81. *                        push one of the radio buttons down, all other radio *
  82. *                        buttons in its group will pop up.  Any group can    *
  83. *                        have only 1 down at a time, and 1 must be down.     *
  84. *                                                                            *
  85. *                CUSTOM: If your application is wierd, you can have a custom *
  86. *                        type button that does anything you want it to.      *
  87. *                                                                            *
  88. *  First, your app must call:    toolbarInit(hInst, hPrev);                  *
  89. *  with the two instance parameters to register a toolbar window class.      *
  90. *  Then your app is free to call CreateWindow with a class of                *
  91. *  szToolBarClass   to create one or more toolbar windows anywhere it wants  *
  92. *  and of any size it wants, presumably as the child window of another of the*
  93. *  app's windows.  The file that creates the window must declare an          *
  94. *  extern char szToolBarClass[];   All messages about activity to a toolbar  *
  95. *  button will go to the parent window of the toolbar.                       *
  96. *                                                                            *
  97. *  Next, call:     toolbarSetBitmap(HWND hwnd, HANDLE hInst, int ibmp,       *
  98. *                             POINT ptSize);      *
  99. *  Pass it the resource ID (eg. IDBMP_BUTTONS) to tell the toolbar where to  *
  100. *  find the pictures for the buttons.  Also pass a point with the width and  *
  101. *  height of each button (eg. 24 X 22) so it knows how to find individual    *
  102. *  buttons in the bitmap file.                                               *
  103. *                                                                            *
  104. *  Next, call:     toolbarAddTool(HWND hwnd, TOOLBUTTON tb);                 *
  105. *  as many times as you want to add a button to the toolbar specified by     *
  106. *  hwnd.  You fill in the "tb" struct with the following information:        *
  107. *                                                                            *
  108. *       tb.rc        = the rect in the toolbar window to place the button    *
  109. *                      based at 0,0 and measured in pixels.                  *
  110. *       tb.iButton   = the ID of the button you wish the add (which is       *
  111. *                      the horizontal offset into the bitmap of buttons).    *
  112. *                      Only one of each button allowed.  Use one of the      *
  113. *                      defines (BTN_??????).                                 *
  114. *       tb.iState    = the initial state of the button (GRAYED, UP, DOWN).   *
  115. *                      If you wish, you can specify a FOCUS'ed state to give *
  116. *                      any button you wish the focus.  By default, it's the  *
  117. *                      one furthest left and tabbing order goes to the right.*
  118. *                      This is the vertical offset into the bitmap.          *
  119. *                      Use one of the defines (BTNST_?????).                 *
  120. *       tb.iType     = The type of button (BTNTYPE_???).  Either pushbutton, *
  121. *                      checkbox, or radio button. (or custom).  If it is a   *
  122. *                      radio button, you can have many groups of radio btn's *
  123. *                      on the same toolbar.  Type BTNTYPE_RADIO is one group.*
  124. *                      Use BTNTYPE_RADIO+1 for another group, BTNTYPE_RADIO+2*
  125. *                      for a third group, etc.  You have thousands.          *
  126. *       tb.iString   = The resource ID of a string to be associated with     *
  127. *                      this button (if you'd like).                          *
  128. *                                                                            *
  129. *                                                                            *
  130. *   At any time in the app, you can call toolbarAddTool to add more buttons  *
  131. *   or toolbarRemoveTool to take some away.  To take one away, identify it   *
  132. *   with it's button ID (horizontal offset in the bitmap).                   *
  133. *                                                                            *
  134. *   You can also call toolbarRetrieveTool to get the TOOLBUTTON struct back  *
  135. *   from a button that is on the toolbar.  This is the way to change a       *
  136. *   button's position.  Change the tb.rc and then Remove and Add the button  *
  137. *   again so that the tabbing order will be re-calculated based on the new   *
  138. *   rect of the tool.                                                        *
  139. *                                                                            *
  140. *   Now, all buttons will automatically behave properly.  They'll go up and  *
  141. *   down as you press on them, or use the keyboard, groups of radio buttons  *
  142. *   will pop up as you press a different one down, etc. etc. etc.            *
  143. *   You don't have to do a thing!                                            *
  144. *                                                                            *
  145. *   The parent of the toolbar window will get a WM_COMMAND message with      *
  146. *   a wParam of IDC_TOOLBAR  whenever anything happens to a button.          *
  147. *   The LOWORD of the lParam is the hwnd of the toolbar window that has the  *
  148. *   button on it.  The (HIWORD & 0xFF) is the button ID of the button.       *
  149. *   Remember to change IDC_TOOLBAR to something unique.                      *
  150. *                                                                            *
  151. *   The app can then call   toolbarIndexFromButton(hwnd, buttonID)           *
  152. *   to get the index of the button (used for subsequent calls).              *
  153. *                                                                            *
  154. *   Then call:      toolbarStateFromButton(hwnd, buttonID)                   *
  155. *                                                                            *
  156. *                   to get either BTNST_UP or BTNST_DOWN.  This is the       *
  157. *                   NEW state of the button since the activity on the        *
  158. *                   button.  It can also be BTNST_GRAYED, but you won't get  *
  159. *                   any activity messages while it's grayed, unless it is a  *
  160. *                   cutsom button.                                           *
  161. *                                                                            *
  162. *             Call  toolbarFullStateFromButton(hwnd, buttonID)               *
  163. *                                                                            *
  164. *                   to get more detail about the state.  It can also return  *
  165. *                   BTNST_FULLDOWN as well as the above states. In the case  *
  166. *                   of BTNST_FULLDOWN, you'll have to call                   *
  167. *                   toolbarPrevStateFromButton(hwnd, btn ID) to get the state*
  168. *                   before it went full down.                                *
  169. *                                                                            *
  170. *                   toolbarPrevStateFromButton(hwnd, buttonID)               *
  171. *                                                                            *
  172. *                   is only valid when the state is BTNST_FULLDOWN.          *
  173. *                                                                            *
  174. *                   toolbarActivityFromIndex(hwnd, buttonID)                 *
  175. *                                                                            *
  176. *                   tells you what just happened to the button.              *
  177. *                   BTNACT_KEYDOWN, BTNACT_MOUSEUP, etc. are possibilities.  *
  178. *                   BTNACT_MOUSEMOUSEOFF means that they pressed it down and *
  179. *                   moved the mouse off of the button (  so it was re- drawn *
  180. *                   in its previous state before being pressed).             *
  181. *                   BTNACT_MOUSEMOUSEON  means that the above happened and   *
  182. *                   then the mouse moved back on top of the button again, so *
  183. *                   the button was re-drawn as if it was pushed again.       *
  184. *                                                                            *
  185. *                   For any of the above activities.......                   *
  186. *                                                                            *
  187. *   HIWORD & BTN_SHIFT     is set if this activity involves the right mouse  *
  188. *                          button, or else it is clear.                      *
  189. *   HIWORD & BTN_DBLCLICK  is set means that this mouse button down activity *
  190. *                          is really a double click (if you care).           *
  191. *                                                                            *
  192. *           If you are a custom button, you can also receive this message... *
  193. *                                                                            *
  194. *   HIWORD & BTN_REPEAT    is set means that the button or key is being held *
  195. *                          down, and you are being sent many down messages   *
  196. *                          in a row.  The first such message is sent with    *
  197. *                          this flag clear, all others have this flag set.   *
  198. *                          If you are a custom button, you will have to      *
  199. *                          ignore messages that are repeats if you don't     *
  200. *                          want to get many down messages in a row.          *
  201. *                                                                            *
  202. *                                                                            *
  203. *                    toolbarStringFromIndex(hwnd, index)                     *
  204. *                                                                            *
  205. *                    will return you the string resource ID you gave when    *
  206. *                    you registered this button.                             *
  207. *                                                                            *
  208. *                                                                            *
  209. *  IMPORTANT !!!!!!!!!!!!!!!!!!!                                             *
  210. *  =============================                                             *
  211. *                                                                            *
  212. *  When you get the state of a button, it's already been changed by the      *
  213. *  activity so it's the NEW STATE!!!!!!!!!                                   *
  214. *                                                                            *
  215. *   EXCEPT!!!   for a custom button!  For a custom button, NOTHING WILL      *
  216. *   happen, you have to do it all yourself!!!! So the state is going to be   *
  217. *   the state BEFORE the activity and you have to call                       *
  218. *   toolbarModifyState(hwnd, buttonID, newState) to change the state         *
  219. *   yourself!!!!                                                             *
  220. *                                                                            *
  221. *   You also have toolbarGetNumButtons(hwnd) to tell you how many are on the *
  222. *   the toolbar.                                                             *
  223. *   And... you have other routines you can use if you really want.           *
  224. *                                                                            *
  225. *   ENJOY!!                                                                  *
  226. *                                                                            *
  227. *  P.S.  Don't forget to pass on WM_SYSCOLORCHANGE msgs to each toolbar.     *
  228. *                                                                            *
  229. *****************************************************************************/
  230.  
  231. #define TOOLGROW    8        // power of 2
  232.  
  233. #define IDC_TOOLBAR    189        // wParam sent to Parent
  234.  
  235. /* We keep an array of these around (one for each button on the toolbar) */
  236.  
  237. typedef struct {
  238.     RECT    rc;        // draw it at this postion in the toolbar
  239.     int    iButton;    // it's this button
  240.     int    iState;        // in this state
  241.     int    iPrevState;    // for non-push buttons - last state
  242.     int    iType;        // type of button
  243.     int    iActivity;    // what just happened to button
  244.     int    iString;    // string resource associated with button
  245. } TOOLBUTTON, FAR *LPTOOLBUTTON;
  246.  
  247. BOOL FAR PASCAL toolbarInit(HANDLE hInst, HANDLE hPrev);
  248. BOOL FAR PASCAL toolbarSetBitmap(HWND hwnd, HANDLE hInst, int ibmp,
  249.                                 POINT ptSize);
  250. BOOL FAR PASCAL toolbarAddTool(HWND hwnd, TOOLBUTTON tb);
  251. BOOL FAR PASCAL toolbarRetrieveTool(HWND hwnd, int iButton, LPTOOLBUTTON tb);
  252. BOOL FAR PASCAL toolbarRemoveTool(HWND hwnd, int iButton);
  253. int FAR PASCAL toolbarGetNumButtons(HWND hwnd);
  254. int FAR PASCAL toolbarButtonFromIndex(HWND hwnd, int iBtnPos);
  255. int FAR PASCAL toolbarIndexFromButton(HWND hwnd, int iButton);
  256. int FAR PASCAL toolbarPrevStateFromButton(HWND hwnd, int iButton);
  257. int FAR PASCAL toolbarActivityFromButton(HWND hwnd, int iButton);
  258. int FAR PASCAL toolbarIndexFromPoint(HWND hwnd, POINT pt);
  259. BOOL FAR PASCAL toolbarRectFromIndex(HWND hwnd, int iBtnPos, LPRECT lprc);
  260. int FAR PASCAL toolbarStringFromIndex(HWND hwnd, int iBtnPos);
  261. int FAR PASCAL toolbarStateFromButton(HWND hwnd, int iButton);
  262. int FAR PASCAL toolbarFullStateFromButton(HWND hwnd, int iButton);
  263. int FAR PASCAL toolbarTypeFromIndex(HWND hwnd, int iBtnPos);
  264. BOOL FAR PASCAL toolbarModifyState(HWND hwnd, int iButton, int iState);
  265. BOOL FAR PASCAL toolbarModifyString(HWND hwnd, int iButton, int iString);
  266. BOOL FAR PASCAL toolbarModifyPrevState(HWND hwnd, int iButton, int iPrevState);
  267. BOOL FAR PASCAL toolbarModifyActivity(HWND hwnd, int iButton, int iActivity);
  268. BOOL FAR PASCAL toolbarExclusiveRadio(HWND hwnd, int iType, int iButton);
  269. BOOL FAR PASCAL toolbarMoveFocus(HWND hwnd, BOOL fBackward);
  270. BOOL FAR PASCAL toolbarSetFocus(HWND hwnd, int iButton);
  271. HBITMAP FAR PASCAL  LoadUIBitmap(
  272.     HANDLE      hInstance,          // EXE file to load resource from
  273.     LPCSTR      szName,             // name of bitmap resource
  274.     COLORREF    rgbText,            // color to use for "Button Text"
  275.     COLORREF    rgbFace,            // color to use for "Button Face"
  276.     COLORREF    rgbShadow,          // color to use for "Button Shadow"
  277.     COLORREF    rgbHighlight,       // color to use for "Button Hilight"
  278.     COLORREF    rgbWindow,          // color to use for "Window Color"
  279.     COLORREF    rgbFrame);          // color to use for "Window Frame"
  280.  
  281.  
  282. /* In a bitmap file, each button is the same size, and contains
  283.  * the picture of a button.  Each column contains the picture of a distinct
  284.  * button (e.g. BTN_REWIND, BTN_REVERSE, etc.) and each row contains
  285.  * a specific button state (BTNST_UP, BTNST_DOWN,
  286.  * BTNBAR_GRAYED, etc. just as an example).
  287.  *
  288.  */
  289.  
  290. #define TB_FIRST    -1
  291. #define TB_LAST        -2
  292.  
  293.  
  294.  
  295. #define BTNST_GRAYED        0    //
  296. #define BTNST_UP        1    //
  297. #define BTNST_DOWN        2    //
  298. #define BTNST_FOCUSUP        3    //
  299. #define BTNST_FOCUSDOWN        4    //
  300. #define BTNST_FULLDOWN        5    //
  301.  
  302. #define BTN_REPEAT        0x100    // add this to button index
  303. #define BTN_SHIFT        0x200
  304. #define BTN_DBLCLICK        0x400
  305.  
  306.  
  307. /* Types of buttons */
  308.  
  309. #define BTNTYPE_PUSH        0
  310. #define BTNTYPE_CHECKBOX    1
  311. #define BTNTYPE_CUSTOM        2
  312. #define BTNTYPE_RADIO        3    // MUST BE LAST to reserve room for more
  313.                     // radio groups.  (3 == one group,
  314.                     // 4 == another group, etc.)
  315.  
  316.  
  317. /* tells parent recent activity on button */
  318. #define BTNACT_MOUSEDOWN    0    // clicked mouse button down on tool
  319. #define BTNACT_MOUSEUP        1    // let go of mouse button while on tool
  320. #define BTNACT_MOUSEMOVEOFF    2    // moved mouse off tool while btn down
  321. #define BTNACT_MOUSEMOVEON    3    // moved back on tool (btn still down)
  322. #define BTNACT_MOUSEDBLCLK    4    // dbl clicked on tool
  323. #define BTNACT_KEYDOWN        5    // key down on tool
  324. #define BTNACT_KEYUP        6    // key up from tool
  325.  
  326.  
  327. /* constants */
  328. #define MSEC_BUTTONREPEAT    200    // milliseconds for auto-repeat
  329.  
  330. /* timers */
  331. #define TIMER_BUTTONREPEAT    1    // timer for button auto-repeat
  332.  
  333.  
  334.  
  335.  
  336.  
  337. // Window words for Toolbar
  338. #ifdef _WIN32
  339. #define GWL_ARRAYBUTT    0        /* Pointer to array of buttons  */
  340. #define GWL_NUMBUTTONS    4        /* Number of buttons in array   */
  341. #define GWL_PRESSED    8        /* Is a button currently pressed*/
  342. #define GWL_KEYPRESSED    12          /* Is a key currently pressed?  */
  343. #define GWL_WHICH    16              /* Which button has the focus?  */
  344. #define GWL_SHIFTED    20        /* Is it rt-click or shift-left?*/
  345. #define GWL_BMPHANDLE    24        /* handle to bmp of the buttons */
  346. #define GWL_BMPINT    28        /* resource int of button bmp    */
  347. #define GWL_BUTTONSIZE    32        /* a point (x=hi y=lo)        */
  348. #define GWL_HINST    36        /* hinst of the app           */
  349. #define TOOLBAR_EXTRABYTES    40
  350. #else
  351. #define GWW_ARRAYBUTT    0        /* Pointer to array of buttons  */
  352. #define GWW_NUMBUTTONS    2        /* Number of buttons in array   */
  353. #define GWW_PRESSED    4        /* Is a button currently pressed*/
  354. #define GWW_KEYPRESSED    6        /* Is a key currently pressed?  */
  355. #define GWW_WHICH    8        /* Which button has the focus?  */
  356. #define GWW_SHIFTED    10        /* Is it rt-click or shift-left?*/
  357. #define GWW_BMPHANDLE    12        /* handle to bmp of the buttons */
  358. #define GWW_BMPINT    14        /* resource int of button bmp    */
  359. #define GWL_BUTTONSIZE    16        /* a point (x=hi y=lo)        */
  360. #define GWW_HINST    20        /* hinst of the app           */
  361. #define TOOLBAR_EXTRABYTES    22
  362. #endif
  363.  
  364.