6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * Copyright (C) 2008 Vladimir Pankratov
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/unicode.h"
35 #define BRIGHT_GREEN RGB(0, 255, 0)
36 #define DARK_GREEN RGB(0, 130, 0)
37 #define RED RGB(255, 0, 0)
40 WNDPROC OldGraphWndProc;
42 static void Graph_DrawCpuUsageGraph(HDC hDC, HWND hWnd)
52 /* Bottom bars that are "used", i.e. are bright green, representing used cpu time */
54 /* Bottom bars that are "used", i.e. are bright green, representing used cpu kernel time */
56 /* Top bars that are "unused", i.e. are dark green, representing free cpu time */
59 static const WCHAR wszFormatI[] = {'%','d','%','%',0};
60 static const WCHAR wszFormatII[] = {' ',' ','%','d','%','%',0};
61 static const WCHAR wszFormatIII[] = {' ','%','d','%','%',0};
64 * Get the client area rectangle
66 GetClientRect(hWnd, &rcClient);
69 * Fill it with blackness
71 FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
76 CpuUsage = PerfDataGetProcessorUsage();
77 CpuKernelUsage = PerfDataGetProcessorSystemUsage();
80 * Check and see how many digits it will take
81 * so we get the indentation right every time.
85 sprintfW(Text, wszFormatI, (int)CpuUsage);
87 else if (CpuUsage < 10)
89 sprintfW(Text, wszFormatII, (int)CpuUsage);
93 sprintfW(Text, wszFormatIII, (int)CpuUsage);
97 * Draw the font text onto the graph
98 * The bottom 20 pixels are reserved for the text
100 Font_DrawText(hDC, Text, ((rcClient.right - rcClient.left) - 32) / 2, rcClient.bottom - 11 - 5);
103 * Now we have to draw the graph
104 * So first find out how many bars we can fit
106 nBars = ((rcClient.bottom - rcClient.top) - 25) / 3;
107 nBarsUsed = (nBars * CpuUsage) / 100;
108 if ((CpuUsage) && (nBarsUsed == 0))
112 nBarsFree = nBars - nBarsUsed;
113 if (TaskManagerSettings.ShowKernelTimes)
115 nBarsUsedKernel = ((nBars * 2) * CpuKernelUsage) / 100;
116 nBarsUsed -= (nBarsUsedKernel / 2);
124 * Now draw the bar graph
126 rcBarLeft.left = ((rcClient.right - rcClient.left) - 33) / 2;
127 rcBarLeft.right = rcBarLeft.left + 16;
128 rcBarRight.left = rcBarLeft.left + 17;
129 rcBarRight.right = rcBarLeft.right + 17;
130 rcBarLeft.top = rcBarRight.top = 5;
131 rcBarLeft.bottom = rcBarRight.bottom = 7;
133 if (nBarsUsed < 0) nBarsUsed = 0;
134 if (nBarsUsed > nBars) nBarsUsed = nBars;
136 if (nBarsFree < 0) nBarsFree = 0;
137 if (nBarsFree > nBars) nBarsFree = nBars;
139 if (nBarsUsedKernel < 0) nBarsUsedKernel = 0;
140 if (nBarsUsedKernel > nBars) nBarsUsedKernel = nBars;
143 * Draw the "free" bars
145 for (i=0; i<nBarsFree; i++)
147 FillSolidRect(hDC, &rcBarLeft, DARK_GREEN);
148 FillSolidRect(hDC, &rcBarRight, DARK_GREEN);
151 rcBarLeft.bottom += 3;
154 rcBarRight.bottom += 3;
158 * Draw the "used" bars
160 for (i=0; i<nBarsUsed; i++)
162 if (nBarsUsed > 5000) nBarsUsed = 5000;
164 FillSolidRect(hDC, &rcBarLeft, BRIGHT_GREEN);
165 FillSolidRect(hDC, &rcBarRight, BRIGHT_GREEN);
168 rcBarLeft.bottom += 3;
171 rcBarRight.bottom += 3;
175 * Draw the "used" kernel bars
179 if (nBarsUsedKernel && nBarsUsedKernel % 2)
182 rcBarLeft.bottom -= 2;
185 rcBarRight.bottom -= 2;
187 FillSolidRect(hDC, &rcBarLeft, RED);
188 FillSolidRect(hDC, &rcBarRight, RED);
191 rcBarLeft.bottom += 2;
194 rcBarRight.bottom += 2;
198 for (i=0; i<nBarsUsedKernel; i++)
200 if (nBarsUsedKernel > 5000) nBarsUsedKernel = 5000;
202 FillSolidRect(hDC, &rcBarLeft, RED);
203 FillSolidRect(hDC, &rcBarRight, RED);
222 static void Graph_DrawMemUsageGraph(HDC hDC, HWND hWnd)
228 ULONGLONG CommitChargeTotal;
229 ULONGLONG CommitChargeLimit;
232 /* Bottom bars that are "used", i.e. are bright green, representing used memory */
234 /* Top bars that are "unused", i.e. are dark green, representing free memory */
237 static const WCHAR wszFormat[] = {'%','d','K',0};
240 * Get the client area rectangle
242 GetClientRect(hWnd, &rcClient);
245 * Fill it with blackness
247 FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
250 * Get the memory usage
252 CommitChargeTotal = (ULONGLONG)PerfDataGetCommitChargeTotalK();
253 CommitChargeLimit = (ULONGLONG)PerfDataGetCommitChargeLimitK();
255 sprintfW(Text, wszFormat, (int)CommitChargeTotal);
258 * Draw the font text onto the graph
259 * The bottom 20 pixels are reserved for the text
261 Font_DrawText(hDC, Text, ((rcClient.right - rcClient.left) - (strlenW(Text) * 8)) / 2, rcClient.bottom - 11 - 5);
264 * Now we have to draw the graph
265 * So first find out how many bars we can fit
267 nBars = ((rcClient.bottom - rcClient.top) - 25) / 3;
268 if (CommitChargeLimit)
269 nBarsUsed = (nBars * (int)((CommitChargeTotal * 100) / CommitChargeLimit)) / 100;
270 nBarsFree = nBars - nBarsUsed;
272 if (nBarsUsed < 0) nBarsUsed = 0;
273 if (nBarsUsed > nBars) nBarsUsed = nBars;
275 if (nBarsFree < 0) nBarsFree = 0;
276 if (nBarsFree > nBars) nBarsFree = nBars;
279 * Now draw the bar graph
281 rcBarLeft.left = ((rcClient.right - rcClient.left) - 33) / 2;
282 rcBarLeft.right = rcBarLeft.left + 16;
283 rcBarRight.left = rcBarLeft.left + 17;
284 rcBarRight.right = rcBarLeft.right + 17;
285 rcBarLeft.top = rcBarRight.top = 5;
286 rcBarLeft.bottom = rcBarRight.bottom = 7;
289 * Draw the "free" bars
291 for (i=0; i<nBarsFree; i++)
293 FillSolidRect(hDC, &rcBarLeft, DARK_GREEN);
294 FillSolidRect(hDC, &rcBarRight, DARK_GREEN);
297 rcBarLeft.bottom += 3;
300 rcBarRight.bottom += 3;
304 * Draw the "used" bars
306 for (i=0; i<nBarsUsed; i++)
308 FillSolidRect(hDC, &rcBarLeft, BRIGHT_GREEN);
309 FillSolidRect(hDC, &rcBarRight, BRIGHT_GREEN);
312 rcBarLeft.bottom += 3;
315 rcBarRight.bottom += 3;
319 static void Graph_DrawMemUsageHistoryGraph(HDC hDC, HWND hWnd)
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 * Draw the graph background
341 * Draw the horizontal bars
343 for (i=0; i<rcClient.bottom; i++)
347 /* FillSolidRect2(hDC, 0, i, rcClient.right, 1, DARK_GREEN); */
351 * Draw the vertical bars
353 for (i=11; i<rcClient.right + offset; i++)
357 /* FillSolidRect2(hDC, i - offset, 0, 1, rcClient.bottom, DARK_GREEN); */
362 * Draw the memory usage
364 for (i=rcClient.right; i>=0; i--)
370 Graph_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
382 * Filter out mouse & keyboard messages
384 /* case WM_APPCOMMAND: */
385 case WM_CAPTURECHANGED:
386 case WM_LBUTTONDBLCLK:
389 case WM_MBUTTONDBLCLK:
392 case WM_MOUSEACTIVATE:
396 /* case WM_MOUSEWHEEL: */
398 case WM_NCLBUTTONDBLCLK:
399 case WM_NCLBUTTONDOWN:
401 case WM_NCMBUTTONDBLCLK:
402 case WM_NCMBUTTONDOWN:
404 /* case WM_NCMOUSEHOVER: */
405 /* case WM_NCMOUSELEAVE: */
407 case WM_NCRBUTTONDBLCLK:
408 case WM_NCRBUTTONDOWN:
410 /* case WM_NCXBUTTONDBLCLK: */
411 /* case WM_NCXBUTTONDOWN: */
412 /* case WM_NCXBUTTONUP: */
413 case WM_RBUTTONDBLCLK:
416 /* case WM_XBUTTONDBLCLK: */
417 /* case WM_XBUTTONDOWN: */
418 /* case WM_XBUTTONUP: */
439 hdc = BeginPaint(hWnd, &ps);
441 WindowId = GetWindowLongPtrW(hWnd, GWLP_ID);
445 case IDC_CPU_USAGE_GRAPH:
446 Graph_DrawCpuUsageGraph(hdc, hWnd);
448 case IDC_MEM_USAGE_GRAPH:
449 Graph_DrawMemUsageGraph(hdc, hWnd);
451 case IDC_MEM_USAGE_HISTORY_GRAPH:
452 Graph_DrawMemUsageHistoryGraph(hdc, hWnd);
463 * We pass on all non-handled messages
465 return CallWindowProcW(OldGraphWndProc, hWnd, message, wParam, lParam);