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 Street, Fifth Floor, Boston, MA 02110-1301 USA
25 int nlastBarsUsed
= 0;
27 WNDPROC OldGraphWndProc
;
29 void Graph_DrawCpuUsageGraph(HDC hDC
, HWND hWnd
);
30 void Graph_DrawMemUsageGraph(HDC hDC
, HWND hWnd
);
31 void Graph_DrawMemUsageHistoryGraph(HDC hDC
, HWND hWnd
);
34 Graph_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
46 * Filter out mouse & keyboard messages
48 /* case WM_APPCOMMAND: */
49 case WM_CAPTURECHANGED
:
50 case WM_LBUTTONDBLCLK
:
53 case WM_MBUTTONDBLCLK
:
56 case WM_MOUSEACTIVATE
:
60 /* case WM_MOUSEWHEEL: */
62 case WM_NCLBUTTONDBLCLK
:
63 case WM_NCLBUTTONDOWN
:
65 case WM_NCMBUTTONDBLCLK
:
66 case WM_NCMBUTTONDOWN
:
68 /* case WM_NCMOUSEHOVER: */
69 /* case WM_NCMOUSELEAVE: */
71 case WM_NCRBUTTONDBLCLK
:
72 case WM_NCRBUTTONDOWN
:
74 /* case WM_NCXBUTTONDBLCLK: */
75 /* case WM_NCXBUTTONDOWN: */
76 /* case WM_NCXBUTTONUP: */
77 case WM_RBUTTONDBLCLK
:
80 /* case WM_XBUTTONDBLCLK: */
81 /* case WM_XBUTTONDOWN: */
82 /* case WM_XBUTTONUP: */
103 hdc
= BeginPaint(hWnd
, &ps
);
105 WindowId
= GetWindowLongPtrW(hWnd
, GWL_ID
);
109 case IDC_CPU_USAGE_GRAPH
:
110 Graph_DrawCpuUsageGraph(hdc
, hWnd
);
112 case IDC_MEM_USAGE_GRAPH
:
113 Graph_DrawMemUsageGraph(hdc
, hWnd
);
115 case IDC_MEM_USAGE_HISTORY_GRAPH
:
116 Graph_DrawMemUsageHistoryGraph(hdc
, hWnd
);
127 * We pass on all non-handled messages
129 return CallWindowProcW((WNDPROC
)OldGraphWndProc
, hWnd
, message
, wParam
, lParam
);
132 void Graph_DrawCpuUsageGraph(HDC hDC
, HWND hWnd
)
138 COLORREF crPrevForeground
;
142 ULONG CpuKernelUsage
;
145 /* Bottom bars that are "used", i.e. are bright green, representing used cpu time */
147 /* Bottom bars that are "used", i.e. are bright green, representing used cpu kernel time */
149 /* Top bars that are "unused", i.e. are dark green, representing free cpu time */
153 * Get the client area rectangle
155 GetClientRect(hWnd
, &rcClient
);
158 * Fill it with blackness
160 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
165 CpuUsage
= PerfDataGetProcessorUsage();
166 if (CpuUsage
<= 0) CpuUsage
= 0;
167 if (CpuUsage
> 100) CpuUsage
= 100;
169 wsprintfW(Text
, L
"%d%%", (int)CpuUsage
);
172 * Draw the font text onto the graph
175 InflateRect(&rcText
, -2, -2);
176 crPrevForeground
= SetTextColor(hDC
, RGB(0, 255, 0));
177 hOldFont
= SelectObject(hDC
, GetStockObject(DEFAULT_GUI_FONT
));
178 DrawTextW(hDC
, Text
, -1, &rcText
, DT_BOTTOM
| DT_CENTER
| DT_NOPREFIX
| DT_SINGLELINE
);
179 SelectObject(hDC
, hOldFont
);
180 SetTextColor(hDC
, crPrevForeground
);
183 * Now we have to draw the graph
184 * So first find out how many bars we can fit
186 nBars
= ((rcClient
.bottom
- rcClient
.top
) - 25) / 3;
187 nBarsUsed
= (nBars
* CpuUsage
) / 100;
188 if ((CpuUsage
) && (nBarsUsed
== 0))
192 nBarsFree
= nBars
- (nlastBarsUsed
>nBarsUsed
? nlastBarsUsed
: nBarsUsed
);
194 if (TaskManagerSettings
.ShowKernelTimes
)
196 CpuKernelUsage
= PerfDataGetProcessorSystemUsage();
197 if (CpuKernelUsage
<= 0) CpuKernelUsage
= 0;
198 if (CpuKernelUsage
>= 100) CpuKernelUsage
= 100;
199 nBarsUsedKernel
= (nBars
* CpuKernelUsage
) / 100;
207 * Now draw the bar graph
209 rcBarLeft
.left
= ((rcClient
.right
- rcClient
.left
) - 33) / 2;
210 rcBarLeft
.right
= rcBarLeft
.left
+ 16;
211 rcBarRight
.left
= rcBarLeft
.left
+ 17;
212 rcBarRight
.right
= rcBarLeft
.right
+ 17;
213 rcBarLeft
.top
= rcBarRight
.top
= 5;
214 rcBarLeft
.bottom
= rcBarRight
.bottom
= 7;
216 if (nBarsUsed
< 0) nBarsUsed
= 0;
217 if (nBarsUsed
> nBars
) nBarsUsed
= nBars
;
219 if (nBarsFree
< 0) nBarsFree
= 0;
220 if (nBarsFree
> nBars
) nBarsFree
= nBars
;
222 if (nBarsUsedKernel
< 0) nBarsUsedKernel
= 0;
223 if (nBarsUsedKernel
> nBars
) nBarsUsedKernel
= nBars
;
226 * Draw the "free" bars
228 for (i
=0; i
<nBarsFree
; i
++)
230 FillSolidRect(hDC
, &rcBarLeft
, DARK_GREEN
);
231 FillSolidRect(hDC
, &rcBarRight
, DARK_GREEN
);
234 rcBarLeft
.bottom
+= 3;
237 rcBarRight
.bottom
+= 3;
241 * Draw the last "used" bars
243 if ((nlastBarsUsed
- nBarsUsed
) > 0) {
244 for (i
=0; i
< (nlastBarsUsed
- nBarsUsed
); i
++)
246 if (nlastBarsUsed
> 5000) nlastBarsUsed
= 5000;
248 FillSolidRect(hDC
, &rcBarLeft
, MEDIUM_GREEN
);
249 FillSolidRect(hDC
, &rcBarRight
, MEDIUM_GREEN
);
252 rcBarLeft
.bottom
+= 3;
255 rcBarRight
.bottom
+= 3;
258 nlastBarsUsed
= nBarsUsed
;
260 * Draw the "used" bars
262 for (i
=0; i
<nBarsUsed
; i
++)
264 if (nBarsUsed
> 5000) nBarsUsed
= 5000;
266 FillSolidRect(hDC
, &rcBarLeft
, BRIGHT_GREEN
);
267 FillSolidRect(hDC
, &rcBarRight
, BRIGHT_GREEN
);
270 rcBarLeft
.bottom
+= 3;
273 rcBarRight
.bottom
+= 3;
277 * Draw the "used" kernel bars
281 rcBarLeft
.bottom
-=3;
284 rcBarRight
.bottom
-=3;
286 for (i
=0; i
<nBarsUsedKernel
; i
++)
289 FillSolidRect(hDC
, &rcBarLeft
, RED
);
290 FillSolidRect(hDC
, &rcBarRight
, RED
);
293 rcBarLeft
.bottom
-=3;
296 rcBarRight
.bottom
-=3;
300 SelectObject(hDC
, hOldFont
);
303 void Graph_DrawMemUsageGraph(HDC hDC
, HWND hWnd
)
309 COLORREF crPrevForeground
;
312 ULONGLONG CommitChargeTotal
;
313 ULONGLONG CommitChargeLimit
;
316 /* Bottom bars that are "used", i.e. are bright green, representing used memory */
318 /* Top bars that are "unused", i.e. are dark green, representing free memory */
322 * Get the client area rectangle
324 GetClientRect(hWnd
, &rcClient
);
327 * Fill it with blackness
329 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
332 * Get the memory usage
334 CommitChargeTotal
= (ULONGLONG
)PerfDataGetCommitChargeTotalK();
335 CommitChargeLimit
= (ULONGLONG
)PerfDataGetCommitChargeLimitK();
337 if (CommitChargeTotal
> 1024)
338 wsprintfW(Text
, L
"%d MB", (int)(CommitChargeTotal
/ 1024));
340 wsprintfW(Text
, L
"%d K", (int)CommitChargeTotal
);
342 * Draw the font text onto the graph
345 InflateRect(&rcText
, -2, -2);
346 crPrevForeground
= SetTextColor(hDC
, RGB(0, 255, 0));
347 hOldFont
= SelectObject(hDC
, GetStockObject(DEFAULT_GUI_FONT
));
348 DrawTextW(hDC
, Text
, -1, &rcText
, DT_BOTTOM
| DT_CENTER
| DT_NOPREFIX
| DT_SINGLELINE
);
349 SelectObject(hDC
, hOldFont
);
350 SetTextColor(hDC
, crPrevForeground
);
353 * Now we have to draw the graph
354 * So first find out how many bars we can fit
356 nBars
= ((rcClient
.bottom
- rcClient
.top
) - 25) / 3;
357 if (CommitChargeLimit
)
358 nBarsUsed
= (nBars
* (int)((CommitChargeTotal
* 100) / CommitChargeLimit
)) / 100;
359 nBarsFree
= nBars
- nBarsUsed
;
361 if (nBarsUsed
< 0) nBarsUsed
= 0;
362 if (nBarsUsed
> nBars
) nBarsUsed
= nBars
;
364 if (nBarsFree
< 0) nBarsFree
= 0;
365 if (nBarsFree
> nBars
) nBarsFree
= nBars
;
368 * Now draw the bar graph
370 rcBarLeft
.left
= ((rcClient
.right
- rcClient
.left
) - 33) / 2;
371 rcBarLeft
.right
= rcBarLeft
.left
+ 16;
372 rcBarRight
.left
= rcBarLeft
.left
+ 17;
373 rcBarRight
.right
= rcBarLeft
.right
+ 17;
374 rcBarLeft
.top
= rcBarRight
.top
= 5;
375 rcBarLeft
.bottom
= rcBarRight
.bottom
= 7;
378 * Draw the "free" bars
380 for (i
=0; i
<nBarsFree
; i
++)
382 FillSolidRect(hDC
, &rcBarLeft
, DARK_GREEN
);
383 FillSolidRect(hDC
, &rcBarRight
, DARK_GREEN
);
386 rcBarLeft
.bottom
+= 3;
389 rcBarRight
.bottom
+= 3;
393 * Draw the "used" bars
395 for (i
=0; i
<nBarsUsed
; i
++)
397 FillSolidRect(hDC
, &rcBarLeft
, BRIGHT_GREEN
);
398 FillSolidRect(hDC
, &rcBarRight
, BRIGHT_GREEN
);
401 rcBarLeft
.bottom
+= 3;
404 rcBarRight
.bottom
+= 3;
407 SelectObject(hDC
, hOldFont
);
410 void Graph_DrawMemUsageHistoryGraph(HDC hDC
, HWND hWnd
)
413 ULONGLONG CommitChargeLimit
;
415 static int offset
= 0;
421 * Get the client area rectangle
423 GetClientRect(hWnd
, &rcClient
);
426 * Fill it with blackness
428 FillSolidRect(hDC
, &rcClient
, RGB(0, 0, 0));
431 * Get the memory usage
433 CommitChargeLimit
= (ULONGLONG
)PerfDataGetCommitChargeLimitK();
436 * Draw the graph background
438 * Draw the horizontal bars
440 for (i
=0; i
<rcClient
.bottom
; i
++)
444 /* FillSolidRect2(hDC, 0, i, rcClient.right, 1, DARK_GREEN); */
448 * Draw the vertical bars
450 for (i
=11; i
<rcClient
.right
+ offset
; i
++)
454 /* FillSolidRect2(hDC, i - offset, 0, 1, rcClient.bottom, DARK_GREEN); */
459 * Draw the memory usage
461 for (i
=rcClient
.right
; i
>=0; i
--)