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.

459 lines
14 KiB

  1. /*++
  2. Copyright (C) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. toolbar.cpp
  5. Abstract:
  6. <abstract>
  7. --*/
  8. #include "toolbar.h"
  9. #include "globals.h"
  10. // define the toolbar button properties for each toolbar button
  11. // these will be added to the toolbar structure as determined by the bitmap
  12. TBBUTTON SysmonToolbarButtons[] = {
  13. // include this separator on ALL toolbars
  14. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  15. {(int)sysmonTbNew, IDM_TB_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  16. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  17. {(int)sysmonTbClear, IDM_TB_CLEAR, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  18. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  19. {(int)sysmonTbCurrentActivity, IDM_TB_REALTIME, TBSTATE_ENABLED, TBSTYLE_CHECK, 0, 0},
  20. {(int)sysmonTbLogData, IDM_TB_LOGFILE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  21. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  22. {(int)sysmonTbChartDisplay, IDM_TB_CHART, TBSTATE_ENABLED, TBSTYLE_CHECKGROUP, 0, 0},
  23. {(int)sysmonTbHistogramDisplay, IDM_TB_HISTOGRAM, TBSTATE_ENABLED, TBSTYLE_CHECKGROUP, 0, 0},
  24. {(int)sysmonTbReportDisplay, IDM_TB_REPORT, TBSTATE_ENABLED, TBSTYLE_CHECKGROUP, 0, 0},
  25. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  26. {(int)sysmonTbAdd, IDM_TB_ADD, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  27. {(int)sysmonTbDelete, IDM_TB_DELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  28. {(int)sysmonTbHighlight, IDM_TB_HIGHLIGHT, TBSTATE_ENABLED, TBSTYLE_CHECK, 0, 0},
  29. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  30. {(int)sysmonTbCopy, IDM_TB_COPY, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  31. {(int)sysmonTbPaste, IDM_TB_PASTE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  32. {(int)sysmonTbProperties, IDM_TB_PROPERTIES, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
  33. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  34. {(int)sysmonTbFreeze, IDM_TB_FREEZE, TBSTATE_ENABLED, TBSTYLE_CHECK, 0, 0},
  35. {(int)sysmonTbUpdate, IDM_TB_UPDATE, 0, TBSTYLE_BUTTON, 0, 0},
  36. {(int)sysmonTbBlank, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0},
  37. {(int)sysmonTbHelp, IDM_TB_HELP, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0}
  38. };
  39. #define TB_BUTTON_COUNT (DWORD)((DWORD)sysmonTbLastButton + 1)
  40. #define TB_ENTRIES (sizeof(SysmonToolbarButtons) / sizeof (SysmonToolbarButtons[0]))
  41. CSysmonToolbar::CSysmonToolbar (void)
  42. {
  43. m_hToolbarWnd = NULL;
  44. memset(&m_rectToolbar, 0, sizeof(m_rectToolbar));
  45. m_pCtrl = NULL;
  46. m_dwToolbarFlags = TBF_DefaultButtons;
  47. m_bVisible = TRUE;
  48. }
  49. CSysmonToolbar::~CSysmonToolbar (void)
  50. {
  51. if (m_hToolbarWnd != NULL) {
  52. DestroyWindow (m_hToolbarWnd);
  53. m_hToolbarWnd = NULL;
  54. }
  55. }
  56. LONG CSysmonToolbar::GetToolbarCmdId (UINT nBtnId)
  57. {
  58. LONG lBtnIndex;
  59. for (lBtnIndex = 0; lBtnIndex <TB_ENTRIES; lBtnIndex++) {
  60. if (SysmonToolbarButtons[lBtnIndex].iBitmap == (int)nBtnId) break;
  61. }
  62. if (SysmonToolbarButtons[lBtnIndex].iBitmap == (int)nBtnId) {
  63. return SysmonToolbarButtons[lBtnIndex].idCommand ;
  64. } else {
  65. return (LONG)-1;
  66. }
  67. }
  68. BOOL CSysmonToolbar::Init (CSysmonControl *pCtrl, HWND hWnd)
  69. {
  70. BOOL bReturn = TRUE;
  71. UINT nIndex;
  72. DWORD dwBitMask;
  73. if (m_hToolbarWnd == NULL) {
  74. if ( NULL != pCtrl && NULL != hWnd ) {
  75. // save pointer to owner control
  76. m_pCtrl = pCtrl;
  77. // create toolbar window
  78. m_hToolbarWnd = CreateToolbarEx (
  79. hWnd,
  80. WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS |
  81. TBSTYLE_TRANSPARENT | TBSTYLE_WRAPABLE | TBSTYLE_CUSTOMERASE,
  82. IDM_TOOLBAR,
  83. TB_BUTTON_COUNT,
  84. g_hInstance,
  85. IDB_TOOLBAR,
  86. SysmonToolbarButtons,
  87. TB_ENTRIES,
  88. SMTB_BM_X,
  89. SMTB_BM_Y,
  90. SMTB_BM_X,
  91. SMTB_BM_Y,
  92. sizeof (TBBUTTON));
  93. if (m_hToolbarWnd != NULL) {
  94. // set/enable the buttons as desired
  95. dwBitMask = 0;
  96. for (nIndex = 0; nIndex < TB_BUTTON_COUNT; nIndex++) {
  97. dwBitMask = 1 << nIndex;
  98. if ((m_dwToolbarFlags & dwBitMask) == 0) {
  99. RemoveButton(nIndex);
  100. }
  101. }
  102. // hide/show toolbar as desired
  103. ShowToolbar (m_bVisible);
  104. } else {
  105. bReturn = FALSE;
  106. }
  107. } else {
  108. bReturn = FALSE;
  109. }
  110. }
  111. return bReturn;
  112. }
  113. BOOL CSysmonToolbar::GetRect (LPRECT pRect)
  114. {
  115. BOOL bReturn;
  116. if (m_hToolbarWnd) {
  117. if (m_bVisible) {
  118. bReturn = GetWindowRect (m_hToolbarWnd, pRect);
  119. } else {
  120. memset (pRect, 0, sizeof (RECT));
  121. bReturn = TRUE;
  122. }
  123. } else {
  124. bReturn = FALSE;
  125. }
  126. return bReturn;
  127. }
  128. LONG CSysmonToolbar::Height()
  129. {
  130. RECT tbRect;
  131. LONG lHeight;
  132. memset (&tbRect, 0, sizeof(RECT));
  133. GetRect (&tbRect);
  134. lHeight = (LONG)(tbRect.bottom - tbRect.top);
  135. return lHeight;
  136. }
  137. BOOL CSysmonToolbar::RemoveButton (UINT nBtnId)
  138. {
  139. int nBtnIndex;
  140. BOOL bReturn = TRUE;
  141. if (m_hToolbarWnd != NULL) {
  142. // find matching toolbar in array
  143. nBtnIndex = (int)GetToolbarCmdId (nBtnId);
  144. if (nBtnIndex >= 0) {
  145. bReturn = (BOOL)SendMessage (m_hToolbarWnd, TB_DELETEBUTTON, nBtnIndex, 0L);
  146. } else {
  147. //not found
  148. bReturn = FALSE;
  149. }
  150. } else {
  151. // no toolbar window
  152. bReturn = FALSE;
  153. }
  154. return bReturn;
  155. }
  156. BOOL CSysmonToolbar::SizeComponents (LPRECT pRect)
  157. {
  158. //stretch toolbar to fit
  159. RECT rNewToolbar;
  160. int cX, cY;
  161. rNewToolbar = *pRect;
  162. cX = rNewToolbar.right - rNewToolbar.left;
  163. cY = Height();
  164. if ((cX > 0) && (m_bVisible)) {
  165. SetWindowPos(m_hToolbarWnd, NULL, 0, 0, cX, cY, SWP_NOMOVE);
  166. } // else do nothing
  167. return TRUE;
  168. }
  169. BOOL CSysmonToolbar::EnableButton (UINT nBtnId, BOOL bState)
  170. {
  171. int nBtnIndex;
  172. BOOL bReturn = TRUE;
  173. if (m_hToolbarWnd != NULL) {
  174. // find matching toolbar in array
  175. nBtnIndex = (int)GetToolbarCmdId (nBtnId);
  176. if (nBtnIndex >= 0) {
  177. bReturn = (BOOL)SendMessage (m_hToolbarWnd, TB_ENABLEBUTTON, nBtnIndex, (LONG)bState);
  178. } else {
  179. //not found
  180. bReturn = FALSE;
  181. }
  182. } else {
  183. // no toolbar window
  184. bReturn = FALSE;
  185. }
  186. return bReturn;
  187. }
  188. void
  189. CSysmonToolbar::PostEnableButton (UINT nBtnId, BOOL bState)
  190. {
  191. int nBtnIndex;
  192. if (m_hToolbarWnd != NULL) {
  193. // find matching toolbar in array
  194. nBtnIndex = (int)GetToolbarCmdId (nBtnId);
  195. if (nBtnIndex >= 0) {
  196. PostMessage (
  197. m_hToolbarWnd,
  198. TB_ENABLEBUTTON,
  199. nBtnIndex,
  200. (LPARAM)MAKELONG(bState, 0));
  201. }
  202. }
  203. return;
  204. }
  205. BOOL CSysmonToolbar::SyncToolbar ()
  206. {
  207. LONG lPushBtnId = -1;
  208. LONG lUnPushBtnId = -1;
  209. LONG lUnPush2BtnId;
  210. LONG wpBtnIndex;
  211. BOOL bClearBtnState;
  212. BOOL bBtnState;
  213. DWORD dwNumCounters;
  214. BOOL bCanModify;
  215. INT iDisplayType;
  216. BOOL bContinue = TRUE;
  217. if ( NULL != m_pCtrl ) {
  218. if ( NULL == m_pCtrl->m_pObj ) {
  219. bContinue = FALSE;
  220. }
  221. } else {
  222. bContinue = FALSE;
  223. }
  224. if ( bContinue ) {
  225. // get the count of counters in the control to use later
  226. dwNumCounters = m_pCtrl->m_pObj->m_Graph.CounterTree.NumCounters();
  227. // Get the Modify state to use later;
  228. // Buttons disabled for ReadOnly:
  229. // New counter set
  230. // Current data vs. log file data source
  231. // Add counter
  232. // Delete counter
  233. // Paste
  234. // Properties
  235. //
  236. bCanModify = !m_pCtrl->IsReadOnly();
  237. // sync data source
  238. if ( bCanModify ) {
  239. wpBtnIndex = GetToolbarCmdId (sysmonTbCurrentActivity);
  240. if (wpBtnIndex >= 0) {
  241. PostMessage (m_hToolbarWnd, TB_CHECKBUTTON, wpBtnIndex,(LPARAM)MAKELONG(!m_pCtrl->IsLogSource(), 0));
  242. }
  243. }
  244. // sync display type
  245. iDisplayType = m_pCtrl->m_pObj->m_Graph.Options.iDisplayType;
  246. switch ( iDisplayType ) {
  247. case LINE_GRAPH:
  248. lPushBtnId = sysmonTbChartDisplay;
  249. lUnPushBtnId = sysmonTbHistogramDisplay;
  250. lUnPush2BtnId = sysmonTbReportDisplay;
  251. bClearBtnState = TRUE;
  252. break;
  253. case BAR_GRAPH:
  254. lUnPushBtnId = sysmonTbChartDisplay;
  255. lPushBtnId = sysmonTbHistogramDisplay;
  256. lUnPush2BtnId = sysmonTbReportDisplay;
  257. bClearBtnState = TRUE;
  258. break;
  259. case REPORT_GRAPH:
  260. lUnPushBtnId = sysmonTbChartDisplay;
  261. lUnPush2BtnId = sysmonTbHistogramDisplay;
  262. lPushBtnId = sysmonTbReportDisplay;
  263. bClearBtnState = FALSE;
  264. break;
  265. default:
  266. lUnPush2BtnId = 0;
  267. bClearBtnState = TRUE;
  268. assert (FALSE);
  269. break;
  270. }
  271. wpBtnIndex = GetToolbarCmdId (lUnPushBtnId);
  272. if (wpBtnIndex >= 0) {
  273. PostMessage (m_hToolbarWnd, TB_CHECKBUTTON, wpBtnIndex,(LPARAM)MAKELONG(FALSE, 0));
  274. }
  275. wpBtnIndex = GetToolbarCmdId (lUnPush2BtnId);
  276. if (wpBtnIndex >= 0) {
  277. PostMessage (m_hToolbarWnd, TB_CHECKBUTTON, wpBtnIndex,(LPARAM)MAKELONG(FALSE, 0));
  278. }
  279. wpBtnIndex = GetToolbarCmdId (lPushBtnId);
  280. if (wpBtnIndex >= 0) {
  281. PostMessage (m_hToolbarWnd, TB_CHECKBUTTON, wpBtnIndex,(LPARAM)MAKELONG(TRUE, 0));
  282. }
  283. // sync update status
  284. wpBtnIndex = GetToolbarCmdId (sysmonTbFreeze);
  285. if (wpBtnIndex >= 0) {
  286. // set push state
  287. PostMessage (m_hToolbarWnd, TB_CHECKBUTTON, wpBtnIndex,
  288. (LPARAM)MAKELONG(m_pCtrl->m_pObj->m_Graph.Options.bManualUpdate, 0));
  289. // set enable state
  290. bBtnState = (dwNumCounters > 0);
  291. PostMessage (m_hToolbarWnd, TB_ENABLEBUTTON, wpBtnIndex,
  292. (LPARAM)MAKELONG(bBtnState, 0));
  293. }
  294. // Manual update button not enabled in design mode.
  295. bBtnState = m_pCtrl->m_pObj->m_Graph.Options.bManualUpdate
  296. && (dwNumCounters > 0)
  297. && m_pCtrl->IsUserMode();
  298. PostEnableButton ( sysmonTbUpdate, bBtnState );
  299. // clear display button
  300. bBtnState = bClearBtnState && (dwNumCounters > 0) && (!m_pCtrl->IsLogSource());
  301. PostEnableButton ( sysmonTbClear, bBtnState );
  302. // Help is always enabled
  303. PostEnableButton ( sysmonTbHelp, TRUE );
  304. // Add, paste and properties are affected by the ReadOnly state.
  305. PostEnableButton ( sysmonTbAdd, bCanModify );
  306. PostEnableButton ( sysmonTbPaste, bCanModify );
  307. PostEnableButton ( sysmonTbProperties, bCanModify );
  308. // Data source buttons are affectedby bCanModify;
  309. PostEnableButton ( sysmonTbLogData, bCanModify );
  310. PostEnableButton ( sysmonTbCurrentActivity, bCanModify );
  311. // set the other buttons that are contingent on the presence of counters
  312. bBtnState = (dwNumCounters > 0);
  313. // the highlight button is only enabled in line_graph and histogram views
  314. PostEnableButton (
  315. sysmonTbHighlight,
  316. ( bBtnState && ( REPORT_GRAPH != iDisplayType ) ) );
  317. wpBtnIndex = GetToolbarCmdId (sysmonTbHighlight);
  318. PostMessage (
  319. m_hToolbarWnd,
  320. TB_CHECKBUTTON,
  321. wpBtnIndex,
  322. (LPARAM)MAKELONG(m_pCtrl->m_pObj->m_Graph.Options.bHighlight, 0));
  323. // the copy button
  324. PostEnableButton ( sysmonTbCopy, bBtnState );
  325. // New/reset and delete are affected by ReadOnly state.
  326. bBtnState = (dwNumCounters > 0) && bCanModify;
  327. // the new/reset button
  328. PostEnableButton ( sysmonTbNew, bBtnState );
  329. // the delete button
  330. PostEnableButton ( sysmonTbDelete, bBtnState );
  331. bContinue = TRUE;
  332. }
  333. return bContinue;
  334. }
  335. BOOL CSysmonToolbar::ShowToolbar (BOOL bVisible)
  336. {
  337. BOOL bReturn = m_bVisible;
  338. if ((m_hToolbarWnd != NULL) && (m_bVisible != bVisible)) {
  339. // only do this is the window is there and the new stat is different
  340. // from the old state
  341. ShowWindow (m_hToolbarWnd, (bVisible ? SW_SHOW : SW_HIDE));
  342. // update local flag
  343. m_bVisible = bVisible;
  344. //sync buttons with the control if it's visible
  345. if (m_pCtrl && m_bVisible) {
  346. SyncToolbar ();
  347. }
  348. } else {
  349. if (m_hToolbarWnd != NULL) {
  350. bReturn = FALSE;
  351. } else {
  352. // the state is already as requested so that's ok
  353. bReturn = TRUE;
  354. }
  355. }
  356. return bReturn;
  357. }
  358. BOOL CSysmonToolbar::SetBackgroundColor (COLORREF ocBackClr)
  359. {
  360. COLORSCHEME csToolbar;
  361. LRESULT lResult;
  362. BOOL bReturn = TRUE;
  363. memset (&csToolbar, 0, sizeof(csToolbar));
  364. csToolbar.dwSize = sizeof(csToolbar);
  365. // get current scheme
  366. lResult = SendMessage (m_hToolbarWnd, TB_GETCOLORSCHEME,
  367. 0, (LPARAM)&csToolbar);
  368. if (lResult) {
  369. // set color
  370. csToolbar.clrBtnHighlight = (COLORREF)ocBackClr;
  371. // leave shadow color alone
  372. lResult = SendMessage (m_hToolbarWnd, TB_SETCOLORSCHEME,
  373. 0, (LPARAM)&csToolbar);
  374. if (!lResult) bReturn = FALSE;
  375. } else {
  376. bReturn = FALSE;
  377. }
  378. return bReturn;
  379. }