Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
22 KiB

  1. /*-----------------------------------------------------------------------------+
  2. | TOOLBAR.H |
  3. | |
  4. | Program Description: Implements a generic toolbar. |
  5. | |
  6. | Here's how to use it: |
  7. | |
  8. | Include the source files "toolbar.h" and "toolbar.c" in your |
  9. | application. |
  10. | |
  11. | Include a line in your application's RC file that gives a file |
  12. | name with a resource id eg. IDBMP_BUTTONS. This is a .BMP file that |
  13. | contains all of the pictures of the buttons you want on your toolbar. |
  14. | Also, make a define for your label with a unique value. If your app has |
  15. | more than one toolbar, and all toolbars don't share a bitmap file, then |
  16. | you will need several defines. |
  17. | |
  18. | e.g. IDBMP_BUTTONS BITMAP "buttons.bmp" |
  19. | IDBMP_ARROWS BITMAP "arrows.bmp" |
  20. | |
  21. | This file must have the different buttons across horizontally |
  22. | and the different states for these buttons vertically. Change the |
  23. | defines in this header file to match the button names and state names of |
  24. | your buttons. You must include the states listed here, and actually |
  25. | you probably won't need to change them at all. The numbers for a button |
  26. | or state are indexes into the bitmap, so the pictures must match. |
  27. | |
  28. | STATE DESCRIPTIONS: |
  29. | GRAYED: The button cannot be pressed & is inactive |
  30. | UP: The button is up |
  31. | DOWN: The button is down |
  32. | FOCUSUP: The button is up and is the one with focus |
  33. | FOCUSDOWN: The button is down and is the one with focus |
  34. | FULLDOWN: A checkbox button has this additional state |
  35. | where it is all the way down when pressed |
  36. | and when it is let go, it will go into |
  37. | either the UP or DOWN state (maybe focused) |
  38. | |
  39. | When you draw the pictures, make sure to get the right state in the right |
  40. | vertical position in the bitmap to match the #define's. |
  41. | |
  42. | A button can also have a type associated with it: |
  43. | |
  44. | PUSH: When pressed it goes down, when let go it bounces |
  45. | up. Therefore, when you aren't currently holding |
  46. | the mouse button or space bar on it, it will |
  47. | ALWAYS be in the up position. It can be in any |
  48. | state except FULLDOWN, which is invalid. |
  49. | |
  50. | CHECKBOX: This button can be up or down. When pushed, it |
  51. | toggles into the opposite state. However, it |
  52. | is always in the FULLDOWN state when being held |
  53. | down with the mouse button or space bar, and when |
  54. | let go, it will go into the opposite state of what |
  55. | it was in before you pressed it. E.G. The button |
  56. | is up. You press it, and it goes way down. You let |
  57. | go, and it comes up a bit, but it's still down. You |
  58. | press it again, and it goes further down before |
  59. | popping all the way up. |
  60. | |
  61. | RADIO: This is a group of buttons that can be up or down, |
  62. | and also have the intermediate step of being |
  63. | FULLDOWN when being held down. But, when you |
  64. | push one of the radio buttons down, all other radio |
  65. | buttons in its group will pop up. Any group can |
  66. | have only 1 down at a time, and 1 must be down. |
  67. | |
  68. | CUSTOM: If your application is wierd, you can have a custom |
  69. | type button that does anything you want it to. |
  70. | |
  71. | First, your app must call: toolbarInit(hInst, hPrev); |
  72. | with the two instance parameters to register a toolbar window class. |
  73. | Then your app is free to call CreateWindow with a class of |
  74. | szToolBarClass to create one or more toolbar windows anywhere it wants |
  75. | and of any size it wants, presumably as the child window of another of the |
  76. | app's windows. The file that creates the window must declare an |
  77. | extern char szToolBarClass[]; All messages about activity to a toolbar |
  78. | button will go to the parent window of the toolbar. |
  79. | |
  80. | Next, call: toolbarSetBitmap(HWND hwnd, HANDLE hInst, int ibmp, |
  81. | POINT ptSize); |
  82. | Pass it the resource ID (eg. IDBMP_BUTTONS) to tell the toolbar where to |
  83. | find the pictures for the buttons. Also pass a point with the width and |
  84. | height of each button (eg. 24 X 22) so it knows how to find individual |
  85. | buttons in the bitmap file. |
  86. | |
  87. | Next, call: toolbarAddTool(HWND hwnd, TOOLBUTTON tb); |
  88. | as many times as you want to add a button to the toolbar specified by |
  89. | hwnd. You fill in the "tb" struct with the following information: |
  90. | |
  91. | tb.rc = the rect in the toolbar window to place the button |
  92. | based at 0,0 and measured in pixels. |
  93. | tb.iButton = the ID of the button you wish the add (which is |
  94. | the horizontal offset into the bitmap of buttons). |
  95. | Only one of each button allowed. Use one of the |
  96. | defines (BTN_??????). |
  97. | tb.iState = the initial state of the button (GRAYED, UP, DOWN). |
  98. | If you wish, you can specify a FOCUS'ed state to give |
  99. | any button you wish the focus. By default, it's the |
  100. | one furthest left and tabbing order goes to the right. |
  101. | This is the vertical offset into the bitmap. |
  102. | Use one of the defines (BTNST_?????). |
  103. | tb.iType = The type of button (BTNTYPE_???). Either pushbutton, |
  104. | checkbox, or radio button. (or custom). If it is a |
  105. | radio button, you can have many groups of radio btn's |
  106. | on the same toolbar. Type BTNTYPE_RADIO is one group. |
  107. | Use BTNTYPE_RADIO+1 for another group, BTNTYPE_RADIO+2 |
  108. | for a third group, etc. You have thousands. |
  109. | tb.iString = The resource ID of a string to be associated with |
  110. | this button (if you'd like). |
  111. | |
  112. | |
  113. | At any time in the app, you can call toolbarAddTool to add more buttons |
  114. | or toolbarRemoveTool to take some away. To take one away, identify it |
  115. | with it's button ID (horizontal offset in the bitmap). |
  116. | |
  117. | You can also call toolbarRetrieveTool to get the TOOLBUTTON struct back |
  118. | from a button that is on the toolbar. This is the way to change a |
  119. | button's position. Change the tb.rc and then Remove and Add the button |
  120. | again so that the tabbing order will be re-calculated based on the new |
  121. | rect of the tool. |
  122. | |
  123. | Now, all buttons will automatically behave properly. They'll go up and |
  124. | down as you press on them, or use the keyboard, groups of radio buttons |
  125. | will pop up as you press a different one down, etc. etc. etc. |
  126. | You don't have to do a thing! |
  127. | |
  128. | The parent of the toolbar window will get a WM_COMMAND message with |
  129. | a wParam of IDC_TOOLBAR whenever anything happens to a button. |
  130. | On Win16: |
  131. | LOWORD(lParam) == hwnd of the toolbar window that has the button on it. |
  132. | (HIWORD(lParam) & 0xFF) == the button ID of the button. |
  133. | On Win32: |
  134. | lParam == hwnd of the toolbar window that has the button on it. |
  135. | (HIWORD(wParam) & 0xFF) == the button ID of the button. |
  136. | (This relies on a 32 bit button id being ACTUALLY only 16 bits |
  137. | Someday this could go sour and need redesign). |
  138. | |
  139. | Remember to change IDC_TOOLBAR to something unique. |
  140. | |
  141. | The app can then call toolbarIndexFromButton(hwnd, buttonID) |
  142. | to get the index of the button (used for subsequent calls). |
  143. | |
  144. | Then call: toolbarStateFromButton(hwnd, buttonID) |
  145. | |
  146. | to get either BTNST_UP or BTNST_DOWN. This is the |
  147. | NEW state of the button since the activity on the |
  148. | button. It can also be BTNST_GRAYED, but you won't get |
  149. | any activity messages while it's grayed, unless it is a |
  150. | cutsom button. |
  151. | |
  152. | Call toolbarFullStateFromButton(hwnd, buttonID) |
  153. | |
  154. | to get more detail about the state. It can also return |
  155. | BTNST_FULLDOWN as well as the above states. In the case |
  156. | of BTNST_FULLDOWN, you'll have to call |
  157. | toolbarPrevStateFromButton(hwnd, btn ID) to get the state |
  158. | before it went full down. |
  159. | |
  160. | toolbarPrevStateFromButton(hwnd, buttonID) |
  161. | |
  162. | is only valid when the state is BTNST_FULLDOWN. |
  163. | |
  164. | toolbarActivityFromIndex(hwnd, buttonID) |
  165. | |
  166. | tells you what just happened to the button. |
  167. | BTNACT_KEYDOWN, BTNACT_MOUSEUP, etc. are possibilities. |
  168. | BTNACT_MOUSEMOUSEOFF means that they pressed it down and |
  169. | moved the mouse off of the button ( so it was re- drawn |
  170. | in its previous state before being pressed). |
  171. | BTNACT_MOUSEMOUSEON means that the above happened and |
  172. | then the mouse moved back on top of the button again, so |
  173. | the button was re-drawn as if it was pushed again. |
  174. | |
  175. | For any of the above activities....... |
  176. | |
  177. | HIWORD & BTN_SHIFT is set if this activity involves the right mouse |
  178. | button, or else it is clear. |
  179. | HIWORD & BTN_DBLCLICK is set means that this mouse button down activity |
  180. | is really a double click (if you care). |
  181. | |
  182. | If you are a custom button, you can also receive this message... |
  183. | |
  184. | HIWORD & BTN_REPEAT is set means that the button or key is being held |
  185. | down, and you are being sent many down messages |
  186. | in a row. The first such message is sent with |
  187. | this flag clear, all others have this flag set. |
  188. | If you are a custom button, you will have to |
  189. | ignore messages that are repeats if you don't |
  190. | want to get many down messages in a row. |
  191. | |
  192. | |
  193. | toolbarStringFromIndex(hwnd, index) |
  194. | |
  195. | will return you the string resource ID you gave when |
  196. | you registered this button. |
  197. | |
  198. | |
  199. | IMPORTANT !!!!!!!!!!!!!!!!!!! |
  200. | ============================= |
  201. | |
  202. | When you get the state of a button, it's already been changed by the |
  203. | activity so it's the NEW STATE!!!!!!!!! |
  204. | |
  205. | EXCEPT!!! for a custom button! For a custom button, NOTHING WILL |
  206. | happen, you have to do it all yourself!!!! So the state is going to be |
  207. | the state BEFORE the activity and you have to call |
  208. | toolbarModifyState(hwnd, buttonID, newState) to change the state |
  209. | yourself!!!! |
  210. | |
  211. | You also have toolbarGetNumButtons(hwnd) to tell you how many are on the |
  212. | the toolbar. |
  213. | And... you have other routines you can use if you really want. |
  214. | |
  215. | ENJOY!! |
  216. | |
  217. | P.S. Don't forget to pass on WM_SYSCOLORCHANGE msgs to each toolbar. |
  218. | |
  219. | |
  220. | (C) Copyright Microsoft Corporation 1992. All rights reserved. |
  221. | |
  222. | Revision History |
  223. | Created by Danny Miller, based on David Maymudes' code |
  224. | which was based on Todd Laney's SBUTTON code |
  225. | and stuff from Eric Ledoux. Did I miss any- |
  226. | body? |
  227. | Oct-1992 Laurie Griffiths converted it to 32/16 bit |
  228. | common code. Mike Tricker reviewed it. |
  229. | |
  230. +-----------------------------------------------------------------------------*/
  231. #include "ctrls.h"
  232. #define TOOLGROW 8 // power of 2
  233. #define IDC_TOOLBAR 189 // wParam sent to Parent
  234. /* We keep an array of these around (one for each button on the toolbar) */
  235. typedef struct {
  236. RECT rc; // draw it at this postion in the toolbar
  237. int iButton; // it's this button
  238. int iState; // in this state
  239. int iPrevState; // for non-push buttons - last state
  240. int iType; // type of button
  241. int iActivity; // what just happened to button
  242. int iString; // string resource associated with button
  243. } TOOLBUTTON, FAR *LPTOOLBUTTON;
  244. HWND CreateStaticStatusWindow(HWND hwndParent,BOOL fSizeGrip);
  245. BOOL WriteStatusMessage(HWND hwnd, LPTSTR szMsg);
  246. BOOL GetStatusTextExtent(HWND hwnd, LPSIZE pTextExtent);
  247. /* We keep an array of these around (one for each button on the toolbar) */
  248. BOOL FAR PASCAL toolbarInit(void);
  249. HWND FAR PASCAL toolbarCreateMain(HWND hwndParent);
  250. HWND FAR PASCAL toolbarCreateMark(HWND hwndParent);
  251. HWND FAR PASCAL toolbarCreateArrows(HWND hwndParent);
  252. BOOL FAR PASCAL toolbarStateFromButton(HWND hwnd, int iButton, int tbIndex);
  253. BOOL FAR PASCAL toolbarAddTool(HWND hwnd, int iButton, int tbIndex, int iState);
  254. BOOL FAR PASCAL toolbarSwapTools(HWND hwnd, int iButton, int jButton, int tbIndex);
  255. BOOL FAR PASCAL toolbarRemoveTool(HWND hwnd, int iButton, int tbIndex);
  256. BOOL FAR PASCAL toolbarModifyState(HWND hwnd, int iButton, int tbIndex, int iState);
  257. BOOL FAR PASCAL toolbarSetFocus(HWND hwnd, int iButton);
  258. HBITMAP FAR PASCAL LoadUIBitmap(
  259. HANDLE hInstance, // EXE file to load resource from
  260. LPCTSTR szName, // name of bitmap resource
  261. COLORREF rgbText, // color to use for "Button Text"
  262. COLORREF rgbFace, // color to use for "Button Face"
  263. COLORREF rgbShadow, // color to use for "Button Shadow"
  264. COLORREF rgbHighlight, // color to use for "Button Hilight"
  265. COLORREF rgbWindow, // color to use for "Window Color"
  266. COLORREF rgbFrame); // color to use for "Window Frame"
  267. /* In a bitmap file, each button is the same size, and contains
  268. * the picture of a button. Each column contains the picture of a distinct
  269. * button (e.g. BTN_REWIND, BTN_REVERSE, etc.) and each row contains
  270. * a specific button state (BTNST_UP, BTNST_DOWN,
  271. * BTNBAR_GRAYED, etc. just as an example).
  272. *
  273. */
  274. #define TB_FIRST -1
  275. #define TB_LAST -2
  276. #define BTN_PLAY 0
  277. #define BTN_PAUSE 1
  278. #define BTN_STOP 2
  279. #define BTN_EJECT 3
  280. #define BTN_HOME 4
  281. #define BTN_RWD 5
  282. #define BTN_FWD 6
  283. #define BTN_END 7
  284. #define ARROW_PREV 0
  285. #define ARROW_NEXT 1
  286. #define BTN_MARKIN 0
  287. #define BTN_MARKOUT 1
  288. #define BTNST_GRAYED 0
  289. #define BTNST_UP 1
  290. #define BTNST_DOWN 2
  291. #define BTNST_FOCUSUP 3
  292. #define BTNST_FOCUSDOWN 4
  293. #define BTNST_FULLDOWN 5
  294. #define BTN_REPEAT 0x100 // add this to button index
  295. #define BTN_SHIFT 0x200
  296. #define BTN_DBLCLICK 0x400
  297. /* constants */
  298. #define MSEC_BUTTONREPEAT 200 // milliseconds for auto-repeat
  299. /* timers */
  300. #define TIMER_BUTTONREPEAT 1 // timer for button auto-repeat
  301. /* bitmap resources */
  302. #define IDBMP_TOOLBAR 100 // main toolbar
  303. #define IDBMP_ARROWS 101 // arrows for scrollbar
  304. #define IDBMP_MARK 102 // arrows for scrollbar
  305. // 103 and 104 used by track.h
  306.