Windows NT 4.0 source code leak
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.

1050 lines
30 KiB

4 years ago
  1. /***************************************************************************\
  2. * winmeter.c
  3. *
  4. * Microsoft Confidential
  5. * Copyright (c) 1991 Microsoft Corporation
  6. *
  7. * main module for WINMETER application - sets up windows, etc.
  8. *
  9. * History:
  10. * Written by Hadi Partovi (t-hadip) summer 1991
  11. *
  12. * Re-written and adapted for NT by Fran Borda (v-franb) Nov.1991
  13. * for Newman Consulting
  14. * Took out all WIN-specific and bargraph code. Added 3 new
  15. * linegraphs (Mem/Paging, Process/Threads/Handles, IO), and
  16. * tailored info to that available under NT.
  17. \***************************************************************************/
  18. #include "winmeter.h"
  19. // Main global information structures
  20. GLOBAL g;
  21. // local function declarations
  22. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  23. LPSTR lpszCmdLine, int nCmdShow); // main function
  24. long FAR PASCAL WndProc (HWND, WORD, WPTYPE, LONG);
  25. // window procedure
  26. extern SYSTEM_PERFORMANCE_INFORMATION PerfInfo,PreviousPerfInfo;
  27. extern int win_on_top;
  28. extern void FreeDatabaseMemory(void) ;
  29. extern void QueryThreadData(void);
  30. // dialog procedures
  31. void EnableIntervalField(HWND hdlg, BOOL fEnable);
  32. // enables Sampling Interval field
  33. BOOL fRefreshDlgOK(HWND hdlg); // handles OK - refresh dlg
  34. BOOL HandleCMD(WPTYPE wParam, DWORD lParam); // handles WM_COMMAND msg
  35. void HandleKey(WPTYPE wParam, DWORD lParam); // handles keyboard input
  36. void HandleKeyUp(WPTYPE wParam, DWORD lParam); // handles keyboard input
  37. void HandleSwitchToNewLG(PLGRAPH plg); // handle switch to LG
  38. void HandleTimer(WPTYPE wParam, DWORD lParam); // handles WM_TIMER messages
  39. void InitializeMenu(void); // initializes main menu
  40. void InitializeRefreshDlgInfo(HWND hdlg, BOOL fManual, int nInterval);
  41. // initialize refresh dialog
  42. void KillWinmeterTimer(void); // destroys timer
  43. BOOL FAR PASCAL OKDlgProc(HWND, WORD, WPTYPE, LONG);
  44. BOOL FAR PASCAL RefreshDlgProc(HWND, WORD, WPTYPE, LONG);
  45. BOOL FAR PASCAL AboutDlgProc(HWND, WORD, WPTYPE, LONG);
  46. void ResetWinmeterTimer(void); // resets timer
  47. void SetWindowTitle(void); // to reflect current display
  48. void SetWinmeterTimer(void); // sets up timer
  49. void UnCheckDisplayMenuItems(void); // removes menu checkmarks
  50. /***************************************************************************\
  51. * main()
  52. \***************************************************************************/
  53. int _CRTAPI1 main(
  54. int argc,
  55. char *argv[])
  56. {
  57. HANDLE hInstance;
  58. hInstance=GetModuleHandle("WINMETER");
  59. WinMain(hInstance, 0, NULL, SW_SHOWNORMAL);
  60. return 1;
  61. argc, argv; // just to avoid compiler warning that param not used
  62. }
  63. /***************************************************************************\
  64. * WinMain()
  65. \***************************************************************************/
  66. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  67. LPSTR lpszCmdLine, int nCmdShow)
  68. {
  69. MSG msg;
  70. WNDCLASS wndclass;
  71. // open file for debugging (#ifdef DEBUGDUMP)
  72. OPENDUMPFILE;
  73. g.hInstance = hInstance;
  74. // set up menu
  75. g.hMenu = LoadMenu(g.hInstance, MAKEINTRESOURCE(IDM_MAINMENU));
  76. if (!g.hMenu)
  77. return 0;
  78. // set up application name
  79. MyLoadString(IDS_APPNAME);
  80. g.lpszAppName = MemAlloc(lstrlen(g.szBuf)+1);
  81. if (!g.lpszAppName)
  82. return 0;
  83. lstrcpy(g.lpszAppName, g.szBuf);
  84. // load initial settings
  85. LoadRefreshSettings();
  86. LoadWindowSettings();
  87. LoadLGDispSettings();
  88. g.fDisplayMenu=TRUE;
  89. if (!hPrevInstance)
  90. {
  91. wndclass.style = CS_DBLCLKS | CS_BYTEALIGNCLIENT;
  92. wndclass.lpfnWndProc = (WNDPROC) WndProc;
  93. wndclass.hInstance = g.hInstance;
  94. wndclass.cbClsExtra = 0;
  95. wndclass.cbWndExtra = 0;
  96. wndclass.hIcon = LoadIcon (g.hInstance, g.lpszAppName);
  97. wndclass.hCursor = LoadCursor (NULL, IDI_APPLICATION);
  98. wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  99. wndclass.lpszMenuName = NULL;
  100. wndclass.lpszClassName = g.lpszAppName;
  101. if (!RegisterClass (&wndclass))
  102. return 0;
  103. }
  104. NtQuerySystemInformation(SystemPerformanceInformation,
  105. &PerfInfo,
  106. sizeof(PerfInfo),
  107. NULL
  108. );
  109. PreviousPerfInfo = PerfInfo;
  110. g.hwnd = CreateWindow (g.lpszAppName, g.lpszAppName,
  111. WS_OVERLAPPEDWINDOW | WS_VSCROLL,
  112. g.xWindowLeft, g.yWindowTop, g.cxClient, g.cyClient,
  113. NULL, NULL, g.hInstance, NULL);
  114. if (!g.hwnd)
  115. return 0;
  116. SetMenu(g.hwnd, g.hMenu);
  117. SetWindowTitle();
  118. // set up sampling timer with default value
  119. if (!g.fManualSampling)
  120. SetWinmeterTimer();
  121. ShowWindow (g.hwnd, nCmdShow);
  122. SetFocus(g.hwnd);
  123. UpdateWindow (g.hwnd);
  124. while (GetMessage (&msg, NULL, 0, 0))
  125. {
  126. TranslateMessage (&msg);
  127. DispatchMessage (&msg);
  128. }
  129. return msg.wParam;
  130. lpszCmdLine;
  131. }
  132. /***************************************************************************\
  133. * WndProc()
  134. *
  135. * Entry: Regular WndProc parameters
  136. * Exit: Handles all top level User Interface, main WindowProcedure
  137. \***************************************************************************/
  138. long FAR PASCAL WndProc (HWND hwnd, WORD message, WPTYPE wParam, LONG lParam)
  139. {
  140. HDC hdc;
  141. PAINTSTRUCT ps;
  142. switch (message) {
  143. case WM_TIMER:
  144. HandleTimer(wParam, lParam);
  145. return 0;
  146. case WM_SIZE:
  147. if (win_on_top)
  148. SetWindowPos(g.hwnd, (HWND) -1,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
  149. HandleSize(wParam, lParam);
  150. return 0;
  151. case WM_PAINT:
  152. hdc = BeginPaint (hwnd, &ps);
  153. SetupDC(hdc);
  154. if (g.LineGraph)
  155. DoLineGraphics(hdc, &(ps.rcPaint));
  156. ResetDC(hdc);
  157. EndPaint (hwnd, &ps);
  158. return 0;
  159. case WM_KEYDOWN:
  160. HandleKey(wParam, lParam);
  161. return 0;
  162. case WM_KEYUP:
  163. HandleKeyUp(wParam, lParam);
  164. return 0;
  165. case WM_COMMAND:
  166. if (HandleCMD(wParam, lParam)) {
  167. return 0;
  168. }
  169. break;
  170. case WM_NCLBUTTONDBLCLK:
  171. // user double clicked on a non-client area of the window
  172. // in case the menu is hidden, this might be an attempt to
  173. // bring it back (clicking on the client area when menu is hidden
  174. // is translated to clicking on the caption bar (see WM_NCHHITTEST)
  175. if ((g.fMenuFits) && (g.fDisplayMenu)) {
  176. // pass to DefWndProc()
  177. break;
  178. }
  179. // otherwise, fall through...
  180. case WM_LBUTTONDBLCLK:
  181. DoMouseDblClk(lParam);
  182. return 0;
  183. case WM_NCHITTEST:
  184. /* if we have no title/menu bar, clicking and dragging the client
  185. * area moves the window. To do this, return HTCAPTION.
  186. * Note dragging not allowed if window maximized, or if caption
  187. * bar is present.
  188. */
  189. lParam = DefWindowProc(hwnd, message, wParam, lParam);
  190. if((!g.fMenuFits || !g.fDisplayMenu) && (lParam == HTCLIENT)
  191. && !IsZoomed(hwnd) ) {
  192. return HTCAPTION;
  193. }
  194. else {
  195. return lParam;
  196. }
  197. case WM_CREATE:
  198. // NewCon: 10/25/91,
  199. // g.hwnd is undefined until we return from the CreateWindow call.
  200. // so during the processing of this WM_CREATE message we need to
  201. // assign the window handle passed into this WinProc so things
  202. // will work.
  203. g.hwnd = hwnd ;
  204. GetFont(hwnd);
  205. GetBrushesAndPens();
  206. InitializeDatabase();
  207. LoadDisplayState();
  208. InitializeMenu();
  209. // force initial display, but without scrollbar
  210. SetScrollRange(hwnd, SB_VERT, 0, 0, TRUE);
  211. ClearLineGraph() ;
  212. PostMessage(hwnd, WM_TIMER, TIMER_ID, 0L);
  213. return 0;
  214. case WM_GETMINMAXINFO:
  215. SetMinMaxInfo(wParam, lParam);
  216. return 0;
  217. case WM_DESTROY:
  218. #ifdef DEBUGDUMP
  219. doCloseDumpFile();
  220. #endif
  221. KillWinmeterTimer();
  222. SaveDisplayState();
  223. TossBrushesAndPens();
  224. FreeDatabaseMemory();
  225. SaveWindowSettings();
  226. PostQuitMessage (0);
  227. return 0;
  228. case WM_SYSCOLORCHANGE:
  229. if (g.BlankBrush) {
  230. DeleteObject(g.BlankBrush);
  231. }
  232. g.BlankBrush = CreateSolidBrush(BLANK_COLOR);
  233. break;
  234. }
  235. return DefWindowProc (hwnd, message, wParam, lParam);
  236. }
  237. /***************************************************************************\
  238. * EnableIntervalField()
  239. *
  240. * Entry: Handle to dialog box, and a enable flag
  241. * Exit: If fEnable is TRUE, enables field. Else disables it
  242. * This is so that the user can't change the sampling rate if he
  243. * has selected manual sampling
  244. \***************************************************************************/
  245. void EnableIntervalField(
  246. HWND hdlg, // handle to dialog box
  247. BOOL fEnable) // flag specifying what to do
  248. {
  249. EnableWindow(GetDlgItem(hdlg,IDD_INTERVAL),fEnable);
  250. EnableWindow(GetDlgItem(hdlg,IDD_INTERVALTEXT1),fEnable);
  251. EnableWindow(GetDlgItem(hdlg,IDD_INTERVALTEXT2),fEnable);
  252. return;
  253. }
  254. /***************************************************************************\
  255. * ErrorExit()
  256. *
  257. * Entry: A string to display
  258. * Exit: Displays the string in a message box, then destroys window
  259. * NOTE: THIS DOES NOT WORK RIGHT IN WIN32. IT WILL CRASH THE
  260. * PROGRAM. A METHOD OF EXITING SHOULD BE USED (like _exit(0))
  261. \***************************************************************************/
  262. void ErrorExit(
  263. PSTR pszError) // string to display
  264. {
  265. g.fStopQuerying=TRUE;
  266. if (g.hwnd)
  267. {
  268. MessageBox(NULL, pszError, g.lpszAppName, WINMETER_MB_FLAGS);
  269. DestroyWindow(g.hwnd);
  270. }
  271. return;
  272. }
  273. /***************************************************************************\
  274. * fRefreshDlgOK()
  275. *
  276. * Entry: Handle to dialog box
  277. * Exit: Processes pressing the OK button
  278. * RETURNS TRUE IF DATA WAS OK, FALSE OTHERWISE
  279. \***************************************************************************/
  280. BOOL fRefreshDlgOK(
  281. HWND hdlg) // handle to dialog box
  282. {
  283. int wInterval; // interval requested
  284. BOOL fOK; // flag, if numeric entry OK
  285. BOOL fOldfManual=FALSE; // flag -> old g.fManualSampling value
  286. char szBuf[TEMP_BUF_LEN];// used for message if number out of range
  287. if (g.fManualSampling)
  288. fOldfManual=TRUE;
  289. // check radio buttons, etc. check that values are OK, ...
  290. g.fManualSampling = IsDlgButtonChecked(hdlg, IDD_MANUAL);
  291. wInterval=GetDlgItemInt(hdlg,IDD_INTERVAL,&fOK, FALSE);
  292. if (!fOK)
  293. {
  294. // bad numeric entry in edit box
  295. wsprintf(szBuf, MyLoadString(IDS_NONNUMERIC),0,UINT_MAX);
  296. MessageBox(NULL, szBuf, g.lpszAppName,
  297. MB_TASKMODAL|MB_ICONEXCLAMATION|MB_OK);
  298. SetFocus(GetDlgItem(hdlg,IDD_INTERVAL));
  299. return FALSE;
  300. }
  301. // implement requested changes
  302. g.nTimerInterval=wInterval;
  303. if (g.fManualSampling)
  304. {
  305. // kill old timer
  306. if (!fOldfManual)
  307. KillWinmeterTimer();
  308. EnableMenuItem(g.hMenu, IDM_REFRESH_NOW, MF_ENABLED);
  309. }
  310. else
  311. {
  312. // set new timer interval
  313. if (fOldfManual)
  314. SetWinmeterTimer();
  315. else
  316. ResetWinmeterTimer();
  317. NtQuerySystemInformation(SystemPerformanceInformation,
  318. &PerfInfo,
  319. sizeof(PerfInfo),
  320. NULL
  321. );
  322. PreviousPerfInfo = PerfInfo;
  323. EnableMenuItem(g.hMenu, IDM_REFRESH_NOW, MF_GRAYED);
  324. }
  325. return TRUE;
  326. }
  327. /***************************************************************************\
  328. * HandleCMD()
  329. *
  330. * Entry: the wParam and lParam from the WM_COMMAND message
  331. * Exit: checks what menuitem was selected, and acts accordingly
  332. * Returns true if actually processed the message, false otherwise
  333. \***************************************************************************/
  334. BOOL HandleCMD(
  335. WPTYPE wParam, // wParam from WndProc WM_COMMAND message
  336. DWORD lParam) // lParam from WndProc WM_COMMAND message
  337. {
  338. switch(wParam)
  339. {
  340. case IDM_CPU_USAGE:
  341. if ((g.LineGraph == DO_CPU) && (g.plg == g.plgCPU))
  342. return FALSE; // no switch necessary, do default processing
  343. g.LineGraph = DO_CPU;
  344. // check the correct menu item
  345. UnCheckDisplayMenuItems();
  346. CheckMenuItem(g.hMenu, IDM_CPU_USAGE, MF_CHECKED);
  347. HandleSwitchToNewLG(g.plgCPU);
  348. return TRUE;
  349. case IDM_MEM_USAGE:
  350. if ((g.LineGraph == DO_MEM) && (g.plg == g.plgMemory))
  351. return FALSE; // pass to DefaultWndProc
  352. g.LineGraph = DO_MEM;
  353. // check correct menu item
  354. UnCheckDisplayMenuItems();
  355. CheckMenuItem(g.hMenu, IDM_MEM_USAGE, MF_CHECKED);
  356. HandleSwitchToNewLG(g.plgMemory);
  357. return TRUE;
  358. case IDM_IO_USAGE:
  359. if ((g.LineGraph == DO_IO) && (g.plg == g.plgIO))
  360. return FALSE; // pass to DefaultWndProc
  361. g.LineGraph = DO_IO;
  362. // check correct menu item
  363. UnCheckDisplayMenuItems();
  364. CheckMenuItem(g.hMenu, IDM_IO_USAGE, MF_CHECKED);
  365. HandleSwitchToNewLG(g.plgIO);
  366. return TRUE;
  367. case IDM_PROCS:
  368. if ((g.LineGraph == DO_PROCS) && (g.plg == g.plgProcs))
  369. return FALSE; // pass to DefaultWndProc
  370. g.LineGraph = DO_PROCS;
  371. // check correct menu item
  372. UnCheckDisplayMenuItems();
  373. CheckMenuItem(g.hMenu, IDM_PROCS, MF_CHECKED);
  374. HandleSwitchToNewLG(g.plgProcs);
  375. return TRUE;
  376. case IDM_CLEAR_GRAPH:
  377. ClearLineGraph();
  378. RedrawLineGraph();
  379. return TRUE;
  380. case IDM_DISPLAY_LEGEND:
  381. CheckMenuItem(g.hMenu, IDM_DISPLAY_LEGEND,
  382. ((g.fDisplayLegend^=1) ? MF_CHECKED : MF_UNCHECKED));
  383. // fake a window resize to do recalculations, etc.
  384. HandleSize(SIZENORMAL, MAKELONG((WORD) g.cxClient, (WORD) g.cyClient));
  385. SaveLGDispSettings();
  386. return TRUE;
  387. case IDM_DISPLAY_CALIBRATION:
  388. CheckMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION,
  389. ((g.fDisplayCalibration^=1) ? MF_CHECKED : MF_UNCHECKED));
  390. // fake a window resize to do recalculations, etc.
  391. HandleSize(SIZENORMAL, MAKELONG((WORD) g.cxClient, (WORD) g.cyClient));
  392. SaveLGDispSettings();
  393. return TRUE;
  394. case IDM_HIDE_MENU:
  395. // act as if user double clicked (in LG mode)
  396. Assert(g.LineGraph);
  397. SendMessage(g.hwnd, WM_LBUTTONDBLCLK, 0, 0L);
  398. return TRUE;
  399. case IDM_EXIT:
  400. PostMessage(g.hwnd, WM_DESTROY, 0, 0L);
  401. return TRUE;
  402. /******************************************\
  403. *** OPTIONS MENU ***
  404. \******************************************/
  405. case IDM_SETTINGS:
  406. SetWindowPos(g.hwnd, (HWND)(win_on_top ? 1 : -1) ,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
  407. win_on_top = !win_on_top;
  408. CheckMenuItem(g.hMenu, IDM_SETTINGS, win_on_top ? MF_CHECKED : MF_UNCHECKED);
  409. return TRUE;
  410. case IDM_REFRESH:
  411. DialogBox(g.hInstance, MAKEINTRESOURCE(IDD_REFRESH),
  412. g.hwnd, (WNDPROC) RefreshDlgProc);
  413. return TRUE;
  414. case IDM_REFRESH_NOW:
  415. // force timer tick
  416. PostMessage(g.hwnd, WM_TIMER, TIMER_ID, 0L);
  417. return TRUE;
  418. /******************************************\
  419. *** HELP MENU ***
  420. \******************************************/
  421. case IDM_HELP_ABOUT:
  422. DialogBox(g.hInstance,MAKEINTRESOURCE(IDD_ABOUT),
  423. g.hwnd, (WNDPROC) AboutDlgProc);
  424. return TRUE;
  425. /*
  426. NO HELP EVEN IN MENU
  427. case IDM_HELP_CONT:
  428. case IDM_HELP_SEARCH:
  429. DialogBox(g.hInstance, MAKEINTRESOURCE(IDD_HELP),
  430. g.hwnd, (WNDPROC) OKDlgProc);
  431. return TRUE;
  432. */
  433. default:
  434. return FALSE;
  435. }
  436. lParam; // just to avoid compiler warning that param not used
  437. }
  438. /***************************************************************************\
  439. * HandleKey()
  440. *
  441. * Entry: the wParam and lParam from the WM_KEYDOWN message
  442. * Exit: sends WM_SCROLL message back to the window to mimic scroll bar
  443. * (if in manual sampling mode, sends WndProc a WM_TIMER message)
  444. \***************************************************************************/
  445. void HandleKey(
  446. WPTYPE wParam, // wParam from WndProc WM_KEYDOWN message
  447. DWORD lParam) // lParam from WndProc WM_KEYDOWN message
  448. {
  449. if (g.LineGraph)
  450. {
  451. // (if not manual sampling, pressing a key should do nothing)
  452. if (g.fManualSampling)
  453. // if in linegraph mode, no scrolling (YET)
  454. SendMessage(g.hwnd, WM_TIMER, TIMER_ID, 0L);
  455. return;
  456. }
  457. switch (wParam)
  458. {
  459. case VK_HOME:
  460. SendMessage(g.hwnd, WM_VSCROLL, SB_TOP, 0L);
  461. break;
  462. case VK_END:
  463. SendMessage(g.hwnd, WM_VSCROLL, SB_BOTTOM, 0L);
  464. break;
  465. case VK_PRIOR:
  466. SendMessage(g.hwnd, WM_VSCROLL, SB_PAGEUP, 0L);
  467. break;
  468. case VK_NEXT:
  469. SendMessage(g.hwnd, WM_VSCROLL, SB_PAGEDOWN, 0L);
  470. break;
  471. case VK_UP:
  472. SendMessage(g.hwnd, WM_VSCROLL, SB_LINEUP, 0L);
  473. break;
  474. case VK_DOWN:
  475. SendMessage(g.hwnd, WM_VSCROLL, SB_LINEDOWN, 0L);
  476. break;
  477. default:
  478. if (g.fManualSampling)
  479. SendMessage(g.hwnd, WM_TIMER, TIMER_ID, 0L);
  480. break;
  481. }
  482. return;
  483. lParam; // just to avoid compiler warning that param not used
  484. }
  485. /***************************************************************************\
  486. * HandleKeyUp()
  487. *
  488. * Entry: the wParam and lParam from the WM_KEYUP message
  489. * Exit: sends an WM_SCROLL message with SB_ENDSCROLL to end scrolling
  490. \***************************************************************************/
  491. void HandleKeyUp(
  492. WPTYPE wParam, // wParam from WndProc WM_KEYUP message
  493. DWORD lParam) // lParam from WndProc WM_KEYUP message
  494. {
  495. if (g.LineGraph)
  496. // do nothing if in linegraph mode (YET)
  497. return;
  498. switch (wParam)
  499. {
  500. case VK_HOME:
  501. case VK_END:
  502. case VK_PRIOR:
  503. case VK_NEXT:
  504. case VK_UP:
  505. case VK_DOWN:
  506. // assume user ended scrolling by releasing a cursor
  507. // key -> enable querying once more
  508. SendMessage(g.hwnd, WM_VSCROLL, SB_ENDSCROLL, 0L);
  509. break;
  510. default:
  511. break;
  512. }
  513. return;
  514. lParam; // just to avoid compiler warning that param not used
  515. }
  516. /***************************************************************************\
  517. * HandleSwitchToNewLG()
  518. *
  519. * Entry: Linegraph to switch to
  520. * Exit:
  521. \***************************************************************************/
  522. void HandleSwitchToNewLG(
  523. PLGRAPH plg) // linegraph to switch to
  524. {
  525. int nMinPos=0; // to be used to check if scroll bar is up
  526. int nMaxPos=0;
  527. if (g.LineGraph)
  528. // deallocate old linegraph data
  529. FreeLGValues(FALSE);
  530. // set up new linegraph
  531. g.plg = plg;
  532. AllocLGValues();
  533. if (g.LineGraph == DO_PROCS)
  534. {
  535. g.plg->dvalAxisHeight = T_DEFAULT_DVAL_AXISHEIGHT;
  536. g.plg->nMaxValues = T_DEFAULT_MAX_VALUES;
  537. }
  538. else
  539. if (g.LineGraph == DO_MEM)
  540. {
  541. g.plg->dvalAxisHeight = M_DEFAULT_DVAL_AXISHEIGHT;
  542. g.plg->nMaxValues = M_DEFAULT_MAX_VALUES;
  543. }
  544. else
  545. if (g.LineGraph == DO_IO)
  546. {
  547. g.plg->dvalAxisHeight = I_DEFAULT_DVAL_AXISHEIGHT;
  548. g.plg->nMaxValues = I_DEFAULT_MAX_VALUES;
  549. }
  550. EnableMenuItem(g.hMenu, IDM_CLEAR_GRAPH, MF_ENABLED);
  551. EnableMenuItem(g.hMenu, IDM_DISPLAY_LEGEND, MF_ENABLED);
  552. EnableMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION, MF_ENABLED);
  553. EnableMenuItem(g.hMenu, IDM_HIDE_MENU, MF_ENABLED);
  554. CheckMenuItem(g.hMenu, IDM_DISPLAY_LEGEND,
  555. ((g.fDisplayLegend) ? MF_CHECKED : MF_UNCHECKED));
  556. CheckMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION,
  557. ((g.fDisplayCalibration) ? MF_CHECKED : MF_UNCHECKED));
  558. GetScrollRange(g.hwnd, SB_VERT, &nMinPos, &nMaxPos);
  559. if ((nMinPos)||(nMaxPos))
  560. // scroll bar existed, remove it, this will cause a WM_SIZE
  561. SetScrollRange(g.hwnd, SB_VERT, 0, 0, TRUE);
  562. SetWindowTitle();
  563. if ((!nMinPos) && (!nMaxPos))
  564. // fake the WM_SIZE message to process the switch
  565. HandleSize(SIZENORMAL, MAKELONG((WORD) g.cxClient, (WORD) g.cyClient));
  566. PostMessage(g.hwnd, WM_TIMER, TIMER_ID, 0L);
  567. return;
  568. }
  569. /***************************************************************************\
  570. * HandleTimer
  571. *
  572. * Entry: the wParam and lParam from the WM_TIMER message
  573. * Exit: queries system info, and displays graphics - either linegraph or
  574. * bargraph
  575. \***************************************************************************/
  576. void HandleTimer(
  577. WPTYPE wParam, // wParam from WndProc WM_TIMER message
  578. DWORD lParam) // lParam from WndProc WM_TIMER message
  579. {
  580. HDC hdc;
  581. if (g.fStopQuerying)
  582. return;
  583. switch (wParam)
  584. {
  585. case TIMER_ID:
  586. hdc = GetDC(g.hwnd);
  587. SetupDC(hdc);
  588. // only do query if not busy scrolling
  589. if(g.LineGraph == DO_PROCS)
  590. {
  591. QueryThreadData();
  592. UpdateLGS();
  593. DoLineGraphics(hdc, NULL);
  594. }
  595. else
  596. {
  597. PreviousPerfInfo = PerfInfo;
  598. QueryGlobalData();
  599. UpdateLGS();
  600. DoLineGraphics(hdc, NULL);
  601. }
  602. ResetDC(hdc);
  603. ReleaseDC(g.hwnd, hdc);
  604. break;
  605. default:
  606. ErrorExit(MyLoadString(IDS_BADTIMERMSG));
  607. }
  608. return;
  609. lParam; // just to avoid compiler warning that param not used
  610. }
  611. /***************************************************************************\
  612. * InitializeMenu()
  613. *
  614. * Entry: None
  615. * Exit: Initializes menu based on winmeter settings
  616. \***************************************************************************/
  617. void InitializeMenu(void)
  618. {
  619. // start with clean slate (just in case)
  620. UnCheckDisplayMenuItems();
  621. EnableMenuItem(g.hMenu, IDM_CLEAR_GRAPH, MF_ENABLED);
  622. EnableMenuItem(g.hMenu, IDM_DISPLAY_LEGEND, MF_ENABLED);
  623. EnableMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION, MF_ENABLED);
  624. EnableMenuItem(g.hMenu, IDM_HIDE_MENU, MF_ENABLED);
  625. CheckMenuItem(g.hMenu, IDM_DISPLAY_LEGEND,
  626. ((g.fDisplayLegend) ? MF_CHECKED : MF_UNCHECKED));
  627. CheckMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION,
  628. ((g.fDisplayCalibration) ? MF_CHECKED : MF_UNCHECKED));
  629. if ((g.LineGraph == DO_CPU) && (g.plg == g.plgCPU))
  630. CheckMenuItem(g.hMenu, IDM_CPU_USAGE, MF_CHECKED);
  631. else
  632. if ((g.LineGraph == DO_PROCS) && (g.plg == g.plgProcs))
  633. CheckMenuItem(g.hMenu, IDM_PROCS, MF_CHECKED);
  634. else
  635. if ((g.LineGraph == DO_MEM) && (g.plg == g.plgMemory))
  636. CheckMenuItem(g.hMenu, IDM_MEM_USAGE, MF_CHECKED);
  637. else
  638. if ((g.LineGraph == DO_IO) && (g.plg == g.plgIO))
  639. CheckMenuItem(g.hMenu, IDM_IO_USAGE, MF_CHECKED);
  640. else
  641. {
  642. EnableMenuItem(g.hMenu, IDM_CLEAR_GRAPH, MF_GRAYED);
  643. EnableMenuItem(g.hMenu, IDM_DISPLAY_LEGEND, MF_GRAYED);
  644. EnableMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION, MF_GRAYED);
  645. EnableMenuItem(g.hMenu, IDM_HIDE_MENU, MF_GRAYED);
  646. CheckMenuItem(g.hMenu, IDM_DISPLAY_LEGEND, MF_UNCHECKED);
  647. CheckMenuItem(g.hMenu, IDM_DISPLAY_CALIBRATION, MF_UNCHECKED);
  648. }
  649. // settings irrespective of current display
  650. EnableMenuItem(g.hMenu, IDM_REFRESH_NOW,
  651. (g.fManualSampling) ? MF_ENABLED : MF_GRAYED);
  652. return;
  653. }
  654. /***************************************************************************\
  655. * InitializeRefreshDlgInfo()
  656. *
  657. * Entry: Handle to dialog box, info to display
  658. * Exit: Initializes the refresh dialog box with given values
  659. \***************************************************************************/
  660. void InitializeRefreshDlgInfo(
  661. HWND hdlg, // handle to dialog box
  662. BOOL fManual, // information to display
  663. int nInterval)
  664. {
  665. // SAMPLING INTERVAL
  666. if (fManual)
  667. {
  668. CheckRadioButton(hdlg, IDD_AUTOMATIC, IDD_MANUAL, IDD_MANUAL);
  669. EnableIntervalField(hdlg, FALSE);
  670. }
  671. else
  672. {
  673. CheckRadioButton(hdlg, IDD_AUTOMATIC, IDD_MANUAL, IDD_AUTOMATIC);
  674. EnableIntervalField(hdlg, TRUE);
  675. }
  676. wsprintf(g.szBuf, "%d", nInterval);
  677. SetDlgItemText(hdlg, IDD_INTERVAL, g.szBuf);
  678. return;
  679. }
  680. /***************************************************************************\
  681. * KillWinmeterTimer()
  682. *
  683. * Entry: None
  684. * Exit: Destroys timer
  685. \***************************************************************************/
  686. void KillWinmeterTimer(void)
  687. {
  688. KillTimer(g.hwnd, TIMER_ID);
  689. return;
  690. }
  691. /***************************************************************************\
  692. * MyLoadString()
  693. *
  694. * Entry: a string ID for the string table
  695. * Exit: loads that string into g.szBuf, and returns a pointer to it
  696. \***************************************************************************/
  697. char *MyLoadString(
  698. WORD wID) // string ID
  699. {
  700. LoadString(g.hInstance, wID, g.szBuf, TEMP_BUF_LEN);
  701. return g.szBuf;
  702. }
  703. /***************************************************************************\
  704. * OKDlgProc()
  705. *
  706. * Entry: Standard dialog procedure stuff
  707. * Exit: Closes window with click on OK button
  708. \***************************************************************************/
  709. BOOL FAR PASCAL OKDlgProc(
  710. HWND hdlg, // handle to dialog box
  711. WORD message, // message ID
  712. WPTYPE wParam, // other info
  713. LONG lParam)
  714. {
  715. switch (message)
  716. {
  717. case WM_INITDIALOG:
  718. return TRUE;
  719. case WM_COMMAND:
  720. switch (wParam)
  721. {
  722. case IDOK:
  723. case IDCANCEL:
  724. EndDialog(hdlg, 0);
  725. return TRUE;
  726. }
  727. break;
  728. }
  729. return FALSE;
  730. lParam; // just to avoid compiler warning that param not used
  731. }
  732. BOOL FAR PASCAL AboutDlgProc(
  733. HWND hdlg,
  734. WORD message,
  735. WPTYPE wParam,
  736. LONG lParam)
  737. {
  738. switch (message)
  739. {
  740. case WM_INITDIALOG:
  741. return TRUE;
  742. case WM_COMMAND:
  743. switch (wParam)
  744. {
  745. case IDOK:
  746. case IDCANCEL:
  747. EndDialog(hdlg, 0);
  748. return TRUE;
  749. }
  750. break;
  751. }
  752. return FALSE;
  753. lParam; // just to avoid compiler warning that param not used
  754. }
  755. /***************************************************************************\
  756. * RefreshDlgProc()
  757. *
  758. * Entry: Standard dialog procedure stuff
  759. * Exit: Changes sampling rate, or uses manual sampling
  760. * The global variables are not changed until the user presses ok,
  761. * they are just changed on the screen, in case the user wants to
  762. * choose cancel (even in the case of a "reset"
  763. \***************************************************************************/
  764. BOOL FAR PASCAL RefreshDlgProc(
  765. HWND hdlg, // handle to dialog box
  766. WORD message, // message ID
  767. WPTYPE wParam, // other info
  768. LONG lParam)
  769. {
  770. switch (message)
  771. {
  772. case WM_INITDIALOG:
  773. // initialize info in dialog box
  774. InitializeRefreshDlgInfo(hdlg, g.fManualSampling, g.nTimerInterval);
  775. return TRUE;
  776. case WM_COMMAND:
  777. switch (wParam)
  778. {
  779. // check pressing of radio buttons -> to enable/disable interval
  780. // edit field
  781. case IDD_MANUAL:
  782. CheckRadioButton(hdlg, IDD_AUTOMATIC, IDD_MANUAL, IDD_MANUAL);
  783. EnableIntervalField(hdlg, FALSE);
  784. return TRUE;
  785. case IDD_AUTOMATIC:
  786. CheckRadioButton(hdlg, IDD_AUTOMATIC, IDD_MANUAL, IDD_AUTOMATIC);
  787. EnableIntervalField(hdlg, TRUE);
  788. return TRUE;
  789. case IDOK:
  790. if (fRefreshDlgOK(hdlg))
  791. {
  792. EndDialog(hdlg, 0);
  793. SaveRefreshSettings();
  794. }
  795. return TRUE;
  796. case IDD_USEDEFAULT:
  797. InitializeRefreshDlgInfo(hdlg, DEFAULT_F_MANUAL,
  798. DEFAULT_TIMER_INTERVAL);
  799. return TRUE;
  800. case IDCANCEL:
  801. EndDialog(hdlg, 0);
  802. return TRUE;
  803. }
  804. break;
  805. }
  806. return FALSE;
  807. lParam; // just to avoid compiler warning that param not used
  808. }
  809. /***************************************************************************\
  810. * ResetWinmeterTimer()
  811. *
  812. * Entry: None
  813. * Exit: Resets timer with global interval, if possible
  814. \***************************************************************************/
  815. void ResetWinmeterTimer()
  816. {
  817. KillWinmeterTimer();
  818. SetWinmeterTimer();
  819. return;
  820. }
  821. /***************************************************************************\
  822. * SetWindowTitle()
  823. *
  824. * Entry: None
  825. * Exit: Sets the window text to reflect current display
  826. \***************************************************************************/
  827. void SetWindowTitle(void)
  828. {
  829. char szBuf[TEMP_BUF_LEN]; // to hold window title line
  830. lstrcpy(szBuf, g.lpszAppName);
  831. MyLoadString(IDS_TITLE_DIVIDER);
  832. lstrcat(szBuf, g.szBuf);
  833. lstrcat(szBuf, g.plg->lpszTitle);
  834. // display it
  835. SetWindowText(g.hwnd, szBuf);
  836. return;
  837. }
  838. /***************************************************************************\
  839. * SetWinmeterTimer()
  840. *
  841. * Entry: None
  842. * Exit: Creates a timer with global interval, if possible
  843. \***************************************************************************/
  844. void SetWinmeterTimer()
  845. {
  846. while (!SetTimer(g.hwnd, TIMER_ID,
  847. max(g.nTimerInterval, MAX_TIMER_INTERVAL), NULL)) {
  848. if (IDCANCEL == MessageBox(NULL, MyLoadString(IDS_MANYCLOCKS),
  849. g.lpszAppName, MB_ICONHAND|MB_RETRYCANCEL|MB_TASKMODAL)) {
  850. ErrorExit(MyLoadString(IDS_CANTDOTIMER));
  851. }
  852. }
  853. return;
  854. }
  855. /***************************************************************************\
  856. * UnCheckDisplayMenuItems()
  857. *
  858. * Entry: None
  859. * Exit: Unchecks the menu items in the "Display" menu
  860. \***************************************************************************/
  861. void UnCheckDisplayMenuItems(void)
  862. {
  863. CheckMenuItem(g.hMenu, IDM_CPU_USAGE, MF_UNCHECKED);
  864. CheckMenuItem(g.hMenu, IDM_PROCS, MF_UNCHECKED);
  865. CheckMenuItem(g.hMenu, IDM_MEM_USAGE, MF_UNCHECKED);
  866. CheckMenuItem(g.hMenu, IDM_IO_USAGE, MF_UNCHECKED);
  867. return;
  868. }