Leaked source code of windows server 2003
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.

320 lines
6.5 KiB

  1. //
  2. // TWND.CPP
  3. // ToolBar handler
  4. //
  5. // Copyright Microsoft 1998-
  6. //
  7. // PRECOMP
  8. #include "precomp.h"
  9. //
  10. // This is the button layout for the toolbar
  11. //
  12. static UINT g_uToolBar[TOOLBAR_MAXBUTTON] =
  13. {
  14. IDM_SELECT,
  15. IDM_ERASER,
  16. IDM_TEXT,
  17. IDM_HIGHLIGHT,
  18. IDM_PEN,
  19. IDM_LINE,
  20. IDM_BOX,
  21. IDM_FILLED_BOX,
  22. IDM_ELLIPSE,
  23. IDM_FILLED_ELLIPSE,
  24. 0,
  25. IDM_ZOOM,
  26. IDM_REMOTE,
  27. IDM_LOCK,
  28. IDM_SYNC,
  29. 0,
  30. IDM_GRAB_AREA,
  31. IDM_GRAB_WINDOW
  32. };
  33. //
  34. //
  35. // Function: WbToolBar constructor
  36. //
  37. // Purpose: Create the tool window
  38. //
  39. //
  40. WbToolBar::WbToolBar()
  41. {
  42. m_hwnd = NULL;
  43. m_hbmImages = NULL;
  44. }
  45. WbToolBar::~WbToolBar()
  46. {
  47. if (m_hbmImages != NULL)
  48. {
  49. ::DeleteBitmap(m_hbmImages);
  50. m_hbmImages = NULL;
  51. }
  52. }
  53. //
  54. //
  55. // Function: Create
  56. //
  57. // Purpose: Create the tool window
  58. //
  59. //
  60. BOOL WbToolBar::Create(HWND hwndParent)
  61. {
  62. TBBUTTON tb;
  63. int iImage, i;
  64. //
  65. // Create the tool window
  66. //
  67. m_hwnd = ::CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
  68. WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | TBSTYLE_WRAPABLE |
  69. CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NOMOVEY | CCS_NORESIZE,
  70. 0, 0, 0, 0,
  71. hwndParent, (HMENU)IDC_TOOLBAR, g_hInstance, NULL);
  72. if (!m_hwnd)
  73. {
  74. ERROR_OUT(("WbToolBar::Create create of window failed"));
  75. return(FALSE);
  76. }
  77. //
  78. // Tell COMCTL32 the structure size for the buttons
  79. //
  80. ::SendMessage(m_hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
  81. //
  82. // And the margin for the buttons
  83. //
  84. ::SendMessage(m_hwnd, TB_SETINDENT, TOOLBAR_MARGINX, 0);
  85. //
  86. // Add the buttons into the toolbar
  87. //
  88. ZeroMemory(&tb, sizeof(tb));
  89. iImage = 0;
  90. for (i = 0; i < TOOLBAR_MAXBUTTON; i++)
  91. {
  92. tb.fsState = TBSTATE_ENABLED;
  93. tb.idCommand = g_uToolBar[i];
  94. if (!tb.idCommand)
  95. {
  96. tb.fsStyle = TBSTYLE_SEP;
  97. tb.iBitmap = TOOLBAR_SEPARATORY;
  98. }
  99. else
  100. {
  101. tb.fsStyle = TBSTYLE_BUTTON;
  102. tb.iBitmap = iImage++;
  103. }
  104. if (!::SendMessage(m_hwnd, TB_ADDBUTTONS, 1, (LPARAM)&tb))
  105. {
  106. ERROR_OUT(("Failed to add button %d to toolbar", i));
  107. return(FALSE);
  108. }
  109. }
  110. //
  111. // Tell the toolbar the image and button sizes
  112. //
  113. ::SendMessage(m_hwnd, TB_SETBITMAPSIZE, 0,
  114. MAKELONG(TOOLBAR_IMAGEWIDTH, TOOLBAR_IMAGEHEIGHT));
  115. ::SendMessage(m_hwnd, TB_SETBUTTONSIZE, 0,
  116. MAKELONG(TOOLBAR_BTNWIDTH, TOOLBAR_BTNHEIGHT));
  117. //
  118. // Load the bitmap resource -- use sys color change handler
  119. //
  120. RecolorButtonImages();
  121. // set up rows
  122. ::SendMessage(m_hwnd, TB_SETROWS, MAKELPARAM(TOOLBAR_NUMROWS +
  123. TOOLBAR_NUMSEPARATORS, TRUE), 0);
  124. ::InvalidateRect(m_hwnd, NULL, TRUE);
  125. return(TRUE);
  126. }
  127. //
  128. //
  129. // Function: GetNaturalSize
  130. //
  131. // Purpose: Return the natural size of the tool client area
  132. //
  133. //
  134. void WbToolBar::GetNaturalSize(LPSIZE lpsize)
  135. {
  136. RECT rectButton;
  137. RECT rectButton2;
  138. if (!::SendMessage(m_hwnd, TB_GETITEMRECT, TOOLBAR_FIRSTBUTTON,
  139. (LPARAM)&rectButton))
  140. {
  141. ::SetRectEmpty(&rectButton);
  142. }
  143. if (!::SendMessage(m_hwnd, TB_GETITEMRECT, TOOLBAR_LASTBUTTON,
  144. (LPARAM)&rectButton2))
  145. {
  146. ::SetRectEmpty(&rectButton2);
  147. }
  148. lpsize->cx = TOOLBAR_WIDTH;
  149. lpsize->cy = rectButton2.bottom - rectButton.top +
  150. // Vertical margin
  151. (rectButton2.bottom - rectButton2.top);
  152. }
  153. //
  154. //
  155. // Function: WidthFromHeight
  156. //
  157. // Purpose: Calculate the width of the toolbar, given the height for
  158. // the fixed mode.
  159. //
  160. //
  161. UINT WbToolBar::WidthFromHeight(UINT uiHeight)
  162. {
  163. SIZE size;
  164. GetNaturalSize(&size);
  165. return(size.cx);
  166. }
  167. //
  168. //
  169. // Function: PushDown
  170. //
  171. // Purpose: Push down a button in the tool window
  172. //
  173. //
  174. BOOL WbToolBar::PushDown(UINT uiId)
  175. {
  176. UINT butId;
  177. BOOL bDown;
  178. // If this isn't an exclusive checkable group, it's easy.
  179. if ((uiId < IDM_TOOLS_START) || (uiId >= IDM_TOOLS_MAX))
  180. {
  181. return (BOOL)(::SendMessage(m_hwnd, TB_CHECKBUTTON, uiId, MAKELPARAM(TRUE, 0)));
  182. }
  183. // Push this one down and pop up all the others
  184. for (butId = IDM_TOOLS_START; butId < IDM_TOOLS_MAX; butId++)
  185. {
  186. bDown = (butId == uiId);
  187. ::SendMessage(m_hwnd, TB_CHECKBUTTON, butId, MAKELPARAM(bDown, 0));
  188. }
  189. return( TRUE );
  190. }
  191. //
  192. //
  193. // Function: PopUp
  194. //
  195. // Purpose: Pop up a button in the tool window
  196. //
  197. //
  198. BOOL WbToolBar::PopUp(UINT uiId)
  199. {
  200. return (BOOL)(::SendMessage(m_hwnd, TB_CHECKBUTTON, uiId, MAKELPARAM(FALSE, 0)));
  201. }
  202. //
  203. //
  204. // Function: Enable
  205. //
  206. // Purpose: Enable a button in the tool window
  207. //
  208. //
  209. BOOL WbToolBar::Enable(UINT uiId)
  210. {
  211. return (BOOL)(::SendMessage(m_hwnd, TB_ENABLEBUTTON, uiId, MAKELPARAM(TRUE, 0)));
  212. }
  213. //
  214. //
  215. // Function: Disable
  216. //
  217. // Purpose: Disable a button in the tool window
  218. //
  219. //
  220. BOOL WbToolBar::Disable(UINT uiId)
  221. {
  222. return (BOOL)(::SendMessage(m_hwnd, TB_ENABLEBUTTON, uiId, MAKELPARAM(FALSE, 0)));
  223. }
  224. void WbToolBar::RecolorButtonImages(void)
  225. {
  226. // re-color bitmap for toolbar
  227. HBITMAP hbmNew;
  228. hbmNew = (HBITMAP)::LoadImage(g_hInstance, MAKEINTRESOURCE(IDR_TOOLS),
  229. IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
  230. if (hbmNew == NULL)
  231. {
  232. ERROR_OUT(("OnSysColorChange: failed to load toolbar bitmap"));
  233. }
  234. else
  235. {
  236. BITMAP bmp;
  237. ::GetObject(hbmNew, sizeof(bmp), &bmp);
  238. if (m_hbmImages == NULL)
  239. {
  240. TBADDBITMAP addBitmap;
  241. // First time
  242. addBitmap.hInst = NULL;
  243. addBitmap.nID = (UINT_PTR)hbmNew;
  244. ::SendMessage(m_hwnd, TB_ADDBITMAP,
  245. (bmp.bmWidth / TOOLBAR_IMAGEWIDTH), (LPARAM)&addBitmap);
  246. }
  247. else
  248. {
  249. TBREPLACEBITMAP replaceBitmap;
  250. replaceBitmap.hInstOld = NULL;
  251. replaceBitmap.nIDOld = (UINT_PTR)m_hbmImages;
  252. replaceBitmap.hInstNew = NULL;
  253. replaceBitmap.nIDNew = (UINT_PTR)hbmNew;
  254. ::SendMessage(m_hwnd, TB_REPLACEBITMAP, 0, (LPARAM)&replaceBitmap);
  255. }
  256. if (m_hbmImages)
  257. {
  258. ::DeleteBitmap(m_hbmImages);
  259. }
  260. m_hbmImages = hbmNew;
  261. }
  262. }