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.

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