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();
77 * Check and see how many digits it will take
78 * so we get the indentation right every time.
82 _stprintf(Text
, _T("%d%%"), (int)CpuUsage
);
84 else if (CpuUsage
< 10)
86 _stprintf(Text
, _T(" %d%%"), (int)CpuUsage
);
90 _stprintf(Text
, _T(" %d%%"), (int)CpuUsage
);
94 * Draw the font text onto the graph
95 * The bottom 20 pixels are reserved for the text
97 Font_DrawText(hDC
, Text
, ((rcClient
.right
- rcClient
.left
) - 32) / 2, rcClient
.bottom
- 11 - 5);
100 * Now we have to draw the graph
101 * So first find out how many bars we can fit
103 nBars
= ((rcClient
.bottom
- rcClient
.top
) - 25) / 3;
104 nBarsUsed
= (nBars
* CpuUsage
) / 100;
105 if ((CpuUsage
) && (nBarsUsed
== 0))
109 nBarsFree
= nBars
- nBarsUsed
;
110 if (TaskManagerSettings
.ShowKernelTimes
)
112 nBarsUsedKernel
= ((nBars
* 2) * CpuKernelUsage
) / 100;
113 nBarsUsed
-= (nBarsUsedKernel
/ 2);
121 * Now draw the bar graph
123 rcBarLeft
.left
= ((rcClient
.right
- rcClient
.left
) - 33) / 2;
124 rcBarLeft
.right
= rcBarLeft
.left
+ 16;
125 rcBarRight
.left
= rcBarLeft
.left
+ 17;
126 rcBarRight
.right
= rcBarLeft
.right
+ 17;
127 rcBarLeft
.top
= rcBarRight
.top
= 5;
128 rcBarLeft
.bottom
= rcBarRight
.bottom
= 7;
130 if (nBarsUsed
< 0) nBarsUsed
= 0;
131 if (nBarsUsed
> nBars
) nBarsUsed
= nBars
;
133 if (nBarsFree
< 0) nBarsFree
= 0;
134 if (nBarsFree
> nBars
) nBarsFree
= nBars
;
136 if (nBarsUsedKernel
< 0) nBarsUsedKernel
= 0;
137 if (nBarsUsedKernel
> nBars
) nBarsUsedKernel
= nBars
;
140 * Draw the "free" bars
142 for (i
=0; i
<nBarsFree
; i
++)
144 FillSolidRect(hDC
, &rcBarLeft
, DARK_GREEN
);
145 FillSolidRect(hDC
, &rcBarRight
, DARK_GREEN
);
148 rcBarLeft
.bottom
+= 3;
151 rcBarRight
.bottom
+= 3;
155 * Draw the "used" bars
157 for (i
=0; i
<nBarsUsed
; i
++)
159 if (nBarsUsed
> 5000) nBarsUsed
= 5000;
161 FillSolidRect(hDC
, &rcBarLeft
, BRIGHT_GREEN
);
162 FillSolidRect(hDC
, &rcBarRight
, BRIGHT_GREEN
);
165 rcBarLeft
.bottom
+= 3;
168 rcBarRight
.bottom
+= 3;
172 * Draw the "used" kernel bars
176 if (nBarsUsedKernel
&& nBarsUsedKernel
% 2)
179 rcBarLeft
.bottom
-= 2;
182 rcBarRight
.bottom
-= 2;
184 FillSolidRect(hDC
, &rcBarLeft
, RED
);
185 FillSolidRect(hDC
, &rcBarRight
, RED
);
188 rcBarLeft
.bottom
+= 2;
191 rcBarRight
.bottom
+= 2;
195 for (i
=0; i
<nBarsUsedKernel
; i
++)
197 if (nBarsUsedKernel
> 5000) nBarsUsedKernel
= 5000;
199 FillSolidRect(hDC
, &rcBarLeft
, RED
);
200 FillSolidRect(hDC
, &rcBarRight
, RED
);
219 static void Graph_DrawMemUsageGraph(HDC hDC
, HWND hWnd
)
225 ULONGLONG CommitChargeTotal
;
226 ULONGLONG CommitChargeLimit
;
229 /* Bottom bars that are "used", i.e. are bright green, representing used memory */
231 /* Top bars that are "unused", i.e. are dark green, representing free memory */
235 * Get the client area rectangle
237 GetClientRect(hWnd
, &rcClient
);
240 * Fill it with blackness
242 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
245 * Get the memory usage
247 CommitChargeTotal
= (ULONGLONG
)PerfDataGetCommitChargeTotalK();
248 CommitChargeLimit
= (ULONGLONG
)PerfDataGetCommitChargeLimitK();
250 _stprintf(Text
, _T("%dK"), (int)CommitChargeTotal
);
253 * Draw the font text onto the graph
254 * The bottom 20 pixels are reserved for the text
256 Font_DrawText(hDC
, Text
, ((rcClient
.right
- rcClient
.left
) - (_tcslen(Text
) * 8)) / 2, rcClient
.bottom
- 11 - 5);
259 * Now we have to draw the graph
260 * So first find out how many bars we can fit
262 nBars
= ((rcClient
.bottom
- rcClient
.top
) - 25) / 3;
263 if (CommitChargeLimit
)
264 nBarsUsed
= (nBars
* (int)((CommitChargeTotal
* 100) / CommitChargeLimit
)) / 100;
265 nBarsFree
= nBars
- nBarsUsed
;
267 if (nBarsUsed
< 0) nBarsUsed
= 0;
268 if (nBarsUsed
> nBars
) nBarsUsed
= nBars
;
270 if (nBarsFree
< 0) nBarsFree
= 0;
271 if (nBarsFree
> nBars
) nBarsFree
= nBars
;
274 * Now draw the bar graph
276 rcBarLeft
.left
= ((rcClient
.right
- rcClient
.left
) - 33) / 2;
277 rcBarLeft
.right
= rcBarLeft
.left
+ 16;
278 rcBarRight
.left
= rcBarLeft
.left
+ 17;
279 rcBarRight
.right
= rcBarLeft
.right
+ 17;
280 rcBarLeft
.top
= rcBarRight
.top
= 5;
281 rcBarLeft
.bottom
= rcBarRight
.bottom
= 7;
284 * Draw the "free" bars
286 for (i
=0; i
<nBarsFree
; i
++)
288 FillSolidRect(hDC
, &rcBarLeft
, DARK_GREEN
);
289 FillSolidRect(hDC
, &rcBarRight
, DARK_GREEN
);
292 rcBarLeft
.bottom
+= 3;
295 rcBarRight
.bottom
+= 3;
299 * Draw the "used" bars
301 for (i
=0; i
<nBarsUsed
; i
++)
303 FillSolidRect(hDC
, &rcBarLeft
, BRIGHT_GREEN
);
304 FillSolidRect(hDC
, &rcBarRight
, BRIGHT_GREEN
);
307 rcBarLeft
.bottom
+= 3;
310 rcBarRight
.bottom
+= 3;
314 static void Graph_DrawMemUsageHistoryGraph(HDC hDC
, HWND hWnd
)
317 ULONGLONG CommitChargeLimit
;
319 static int offset
= 0;
325 * Get the client area rectangle
327 GetClientRect(hWnd
, &rcClient
);
330 * Fill it with blackness
332 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
335 * Get the memory usage
337 CommitChargeLimit
= (ULONGLONG
)PerfDataGetCommitChargeLimitK();
340 * Draw the graph background
342 * Draw the horizontal bars
344 for (i
=0; i
<rcClient
.bottom
; i
++)
348 /* FillSolidRect2(hDC, 0, i, rcClient.right, 1, DARK_GREEN); */
352 * Draw the vertical bars
354 for (i
=11; i
<rcClient
.right
+ offset
; i
++)
358 /* FillSolidRect2(hDC, i - offset, 0, 1, rcClient.bottom, DARK_GREEN); */
363 * Draw the memory usage
365 for (i
=rcClient
.right
; i
>=0; i
--)
371 Graph_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
383 * Filter out mouse & keyboard messages
385 /* case WM_APPCOMMAND: */
386 case WM_CAPTURECHANGED
:
387 case WM_LBUTTONDBLCLK
:
390 case WM_MBUTTONDBLCLK
:
393 case WM_MOUSEACTIVATE
:
397 /* case WM_MOUSEWHEEL: */
399 case WM_NCLBUTTONDBLCLK
:
400 case WM_NCLBUTTONDOWN
:
402 case WM_NCMBUTTONDBLCLK
:
403 case WM_NCMBUTTONDOWN
:
405 /* case WM_NCMOUSEHOVER: */
406 /* case WM_NCMOUSELEAVE: */
408 case WM_NCRBUTTONDBLCLK
:
409 case WM_NCRBUTTONDOWN
:
411 /* case WM_NCXBUTTONDBLCLK: */
412 /* case WM_NCXBUTTONDOWN: */
413 /* case WM_NCXBUTTONUP: */
414 case WM_RBUTTONDBLCLK
:
417 /* case WM_XBUTTONDBLCLK: */
418 /* case WM_XBUTTONDOWN: */
419 /* case WM_XBUTTONUP: */
440 hdc
= BeginPaint(hWnd
, &ps
);
442 WindowId
= GetWindowLongPtr(hWnd
, GWLP_ID
);
446 case IDC_CPU_USAGE_GRAPH
:
447 Graph_DrawCpuUsageGraph(hdc
, hWnd
);
449 case IDC_MEM_USAGE_GRAPH
:
450 Graph_DrawMemUsageGraph(hdc
, hWnd
);
452 case IDC_MEM_USAGE_HISTORY_GRAPH
:
453 Graph_DrawMemUsageHistoryGraph(hdc
, hWnd
);
464 * We pass on all non-handled messages
466 return CallWindowProc((WNDPROC
)OldGraphWndProc
, hWnd
, message
, wParam
, lParam
);