6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
36 #define BRIGHT_GREEN RGB(0, 255, 0)
37 #define DARK_GREEN RGB(0, 130, 0)
38 #define RED RGB(255, 0, 0)
41 WNDPROC OldGraphWndProc
;
43 static void Graph_DrawCpuUsageGraph(HDC hDC
, HWND hWnd
)
53 /* Bottom bars that are "used", i.e. are bright green, representing used cpu time */
55 /* Bottom bars that are "used", i.e. are bright green, representing used cpu kernel time */
57 /* Top bars that are "unused", i.e. are dark green, representing free cpu time */
61 * Get the client area rectangle
63 GetClientRect(hWnd
, &rcClient
);
66 * Fill it with blackness
68 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
73 CpuUsage
= PerfDataGetProcessorUsage();
74 CpuKernelUsage
= PerfDataGetProcessorSystemUsage();
75 if (CpuUsage
< 0) CpuUsage
= 0;
76 if (CpuUsage
> 100) CpuUsage
= 100;
77 if (CpuKernelUsage
< 0) CpuKernelUsage
= 0;
78 if (CpuKernelUsage
> 100) CpuKernelUsage
= 100;
81 * Check and see how many digits it will take
82 * so we get the indentation right every time.
86 _stprintf(Text
, _T("%d%%"), (int)CpuUsage
);
88 else if (CpuUsage
< 10)
90 _stprintf(Text
, _T(" %d%%"), (int)CpuUsage
);
94 _stprintf(Text
, _T(" %d%%"), (int)CpuUsage
);
98 * Draw the font text onto the graph
99 * The bottom 20 pixels are reserved for the text
101 Font_DrawText(hDC
, Text
, ((rcClient
.right
- rcClient
.left
) - 32) / 2, rcClient
.bottom
- 11 - 5);
104 * Now we have to draw the graph
105 * So first find out how many bars we can fit
107 nBars
= ((rcClient
.bottom
- rcClient
.top
) - 25) / 3;
108 nBarsUsed
= (nBars
* CpuUsage
) / 100;
109 if ((CpuUsage
) && (nBarsUsed
== 0))
113 nBarsFree
= nBars
- nBarsUsed
;
114 if (TaskManagerSettings
.ShowKernelTimes
)
116 nBarsUsedKernel
= ((nBars
* 2) * CpuKernelUsage
) / 100;
117 nBarsUsed
-= (nBarsUsedKernel
/ 2);
125 * Now draw the bar graph
127 rcBarLeft
.left
= ((rcClient
.right
- rcClient
.left
) - 33) / 2;
128 rcBarLeft
.right
= rcBarLeft
.left
+ 16;
129 rcBarRight
.left
= rcBarLeft
.left
+ 17;
130 rcBarRight
.right
= rcBarLeft
.right
+ 17;
131 rcBarLeft
.top
= rcBarRight
.top
= 5;
132 rcBarLeft
.bottom
= rcBarRight
.bottom
= 7;
134 if (nBarsUsed
< 0) nBarsUsed
= 0;
135 if (nBarsUsed
> nBars
) nBarsUsed
= nBars
;
137 if (nBarsFree
< 0) nBarsFree
= 0;
138 if (nBarsFree
> nBars
) nBarsFree
= nBars
;
140 if (nBarsUsedKernel
< 0) nBarsUsedKernel
= 0;
141 if (nBarsUsedKernel
> nBars
) nBarsUsedKernel
= nBars
;
144 * Draw the "free" bars
146 for (i
=0; i
<nBarsFree
; i
++)
148 FillSolidRect(hDC
, &rcBarLeft
, DARK_GREEN
);
149 FillSolidRect(hDC
, &rcBarRight
, DARK_GREEN
);
152 rcBarLeft
.bottom
+= 3;
155 rcBarRight
.bottom
+= 3;
159 * Draw the "used" bars
161 for (i
=0; i
<nBarsUsed
; i
++)
163 if (nBarsUsed
> 5000) nBarsUsed
= 5000;
165 FillSolidRect(hDC
, &rcBarLeft
, BRIGHT_GREEN
);
166 FillSolidRect(hDC
, &rcBarRight
, BRIGHT_GREEN
);
169 rcBarLeft
.bottom
+= 3;
172 rcBarRight
.bottom
+= 3;
176 * Draw the "used" kernel bars
180 if (nBarsUsedKernel
&& nBarsUsedKernel
% 2)
183 rcBarLeft
.bottom
-= 2;
186 rcBarRight
.bottom
-= 2;
188 FillSolidRect(hDC
, &rcBarLeft
, RED
);
189 FillSolidRect(hDC
, &rcBarRight
, RED
);
192 rcBarLeft
.bottom
+= 2;
195 rcBarRight
.bottom
+= 2;
199 for (i
=0; i
<nBarsUsedKernel
; i
++)
201 if (nBarsUsedKernel
> 5000) nBarsUsedKernel
= 5000;
203 FillSolidRect(hDC
, &rcBarLeft
, RED
);
204 FillSolidRect(hDC
, &rcBarRight
, RED
);
223 static void Graph_DrawMemUsageGraph(HDC hDC
, HWND hWnd
)
229 ULONGLONG CommitChargeTotal
;
230 ULONGLONG CommitChargeLimit
;
233 /* Bottom bars that are "used", i.e. are bright green, representing used memory */
235 /* Top bars that are "unused", i.e. are dark green, representing free memory */
239 * Get the client area rectangle
241 GetClientRect(hWnd
, &rcClient
);
244 * Fill it with blackness
246 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
249 * Get the memory usage
251 CommitChargeTotal
= (ULONGLONG
)PerfDataGetCommitChargeTotalK();
252 CommitChargeLimit
= (ULONGLONG
)PerfDataGetCommitChargeLimitK();
254 _stprintf(Text
, _T("%dK"), (int)CommitChargeTotal
);
257 * Draw the font text onto the graph
258 * The bottom 20 pixels are reserved for the text
260 Font_DrawText(hDC
, Text
, ((rcClient
.right
- rcClient
.left
) - (_tcslen(Text
) * 8)) / 2, rcClient
.bottom
- 11 - 5);
263 * Now we have to draw the graph
264 * So first find out how many bars we can fit
266 nBars
= ((rcClient
.bottom
- rcClient
.top
) - 25) / 3;
267 if (CommitChargeLimit
)
268 nBarsUsed
= (nBars
* (int)((CommitChargeTotal
* 100) / CommitChargeLimit
)) / 100;
269 nBarsFree
= nBars
- nBarsUsed
;
271 if (nBarsUsed
< 0) nBarsUsed
= 0;
272 if (nBarsUsed
> nBars
) nBarsUsed
= nBars
;
274 if (nBarsFree
< 0) nBarsFree
= 0;
275 if (nBarsFree
> nBars
) nBarsFree
= nBars
;
278 * Now draw the bar graph
280 rcBarLeft
.left
= ((rcClient
.right
- rcClient
.left
) - 33) / 2;
281 rcBarLeft
.right
= rcBarLeft
.left
+ 16;
282 rcBarRight
.left
= rcBarLeft
.left
+ 17;
283 rcBarRight
.right
= rcBarLeft
.right
+ 17;
284 rcBarLeft
.top
= rcBarRight
.top
= 5;
285 rcBarLeft
.bottom
= rcBarRight
.bottom
= 7;
288 * Draw the "free" bars
290 for (i
=0; i
<nBarsFree
; i
++)
292 FillSolidRect(hDC
, &rcBarLeft
, DARK_GREEN
);
293 FillSolidRect(hDC
, &rcBarRight
, DARK_GREEN
);
296 rcBarLeft
.bottom
+= 3;
299 rcBarRight
.bottom
+= 3;
303 * Draw the "used" bars
305 for (i
=0; i
<nBarsUsed
; i
++)
307 FillSolidRect(hDC
, &rcBarLeft
, BRIGHT_GREEN
);
308 FillSolidRect(hDC
, &rcBarRight
, BRIGHT_GREEN
);
311 rcBarLeft
.bottom
+= 3;
314 rcBarRight
.bottom
+= 3;
318 static void Graph_DrawMemUsageHistoryGraph(HDC hDC
, HWND hWnd
)
321 ULONGLONG CommitChargeLimit
;
323 static int offset
= 0;
329 * Get the client area rectangle
331 GetClientRect(hWnd
, &rcClient
);
334 * Fill it with blackness
336 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
339 * Get the memory usage
341 CommitChargeLimit
= (ULONGLONG
)PerfDataGetCommitChargeLimitK();
344 * Draw the graph background
346 * Draw the horizontal bars
348 for (i
=0; i
<rcClient
.bottom
; i
++)
352 /* FillSolidRect2(hDC, 0, i, rcClient.right, 1, DARK_GREEN); */
356 * Draw the vertical bars
358 for (i
=11; i
<rcClient
.right
+ offset
; i
++)
362 /* FillSolidRect2(hDC, i - offset, 0, 1, rcClient.bottom, DARK_GREEN); */
367 * Draw the memory usage
369 for (i
=rcClient
.right
; i
>=0; i
--)
375 Graph_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
387 * Filter out mouse & keyboard messages
389 /* case WM_APPCOMMAND: */
390 case WM_CAPTURECHANGED
:
391 case WM_LBUTTONDBLCLK
:
394 case WM_MBUTTONDBLCLK
:
397 case WM_MOUSEACTIVATE
:
401 /* case WM_MOUSEWHEEL: */
403 case WM_NCLBUTTONDBLCLK
:
404 case WM_NCLBUTTONDOWN
:
406 case WM_NCMBUTTONDBLCLK
:
407 case WM_NCMBUTTONDOWN
:
409 /* case WM_NCMOUSEHOVER: */
410 /* case WM_NCMOUSELEAVE: */
412 case WM_NCRBUTTONDBLCLK
:
413 case WM_NCRBUTTONDOWN
:
415 /* case WM_NCXBUTTONDBLCLK: */
416 /* case WM_NCXBUTTONDOWN: */
417 /* case WM_NCXBUTTONUP: */
418 case WM_RBUTTONDBLCLK
:
421 /* case WM_XBUTTONDBLCLK: */
422 /* case WM_XBUTTONDOWN: */
423 /* case WM_XBUTTONUP: */
444 hdc
= BeginPaint(hWnd
, &ps
);
446 WindowId
= GetWindowLongPtr(hWnd
, GWLP_ID
);
450 case IDC_CPU_USAGE_GRAPH
:
451 Graph_DrawCpuUsageGraph(hdc
, hWnd
);
453 case IDC_MEM_USAGE_GRAPH
:
454 Graph_DrawMemUsageGraph(hdc
, hWnd
);
456 case IDC_MEM_USAGE_HISTORY_GRAPH
:
457 Graph_DrawMemUsageHistoryGraph(hdc
, hWnd
);
468 * We pass on all non-handled messages
470 return CallWindowProc((WNDPROC
)OldGraphWndProc
, hWnd
, message
, wParam
, lParam
);