ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

graph.c
Go to the documentation of this file.
00001 /*
00002  *  ReactOS Task Manager
00003  *
00004  *  graph.c
00005  *
00006  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 #include <precomp.h>
00024 
00025 int      nlastBarsUsed = 0;
00026 
00027 WNDPROC  OldGraphWndProc;
00028 
00029 void     Graph_DrawCpuUsageGraph(HDC hDC, HWND hWnd);
00030 void     Graph_DrawMemUsageGraph(HDC hDC, HWND hWnd);
00031 void     Graph_DrawMemUsageHistoryGraph(HDC hDC, HWND hWnd);
00032 
00033 INT_PTR CALLBACK
00034 Graph_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00035 {
00036     HDC          hdc;
00037     PAINTSTRUCT  ps;
00038     LONG         WindowId;
00039 
00040     switch (message)
00041     {
00042     case WM_ERASEBKGND:
00043         return TRUE;
00044 
00045     /*
00046      * Filter out mouse  & keyboard messages
00047      */
00048     /* case WM_APPCOMMAND: */
00049     case WM_CAPTURECHANGED:
00050     case WM_LBUTTONDBLCLK:
00051     case WM_LBUTTONDOWN:
00052     case WM_LBUTTONUP:
00053     case WM_MBUTTONDBLCLK:
00054     case WM_MBUTTONDOWN:
00055     case WM_MBUTTONUP:
00056     case WM_MOUSEACTIVATE:
00057     case WM_MOUSEHOVER:
00058     case WM_MOUSELEAVE:
00059     case WM_MOUSEMOVE:
00060     /* case WM_MOUSEWHEEL: */
00061     case WM_NCHITTEST:
00062     case WM_NCLBUTTONDBLCLK:
00063     case WM_NCLBUTTONDOWN:
00064     case WM_NCLBUTTONUP:
00065     case WM_NCMBUTTONDBLCLK:
00066     case WM_NCMBUTTONDOWN:
00067     case WM_NCMBUTTONUP:
00068     /* case WM_NCMOUSEHOVER: */
00069     /* case WM_NCMOUSELEAVE: */
00070     case WM_NCMOUSEMOVE:
00071     case WM_NCRBUTTONDBLCLK:
00072     case WM_NCRBUTTONDOWN:
00073     case WM_NCRBUTTONUP:
00074     /* case WM_NCXBUTTONDBLCLK: */
00075     /* case WM_NCXBUTTONDOWN: */
00076     /* case WM_NCXBUTTONUP: */
00077     case WM_RBUTTONDBLCLK:
00078     case WM_RBUTTONDOWN:
00079     case WM_RBUTTONUP:
00080     /* case WM_XBUTTONDBLCLK: */
00081     /* case WM_XBUTTONDOWN: */
00082     /* case WM_XBUTTONUP: */
00083     case WM_ACTIVATE:
00084     case WM_CHAR:
00085     case WM_DEADCHAR:
00086     case WM_GETHOTKEY:
00087     case WM_HOTKEY:
00088     case WM_KEYDOWN:
00089     case WM_KEYUP:
00090     case WM_KILLFOCUS:
00091     case WM_SETFOCUS:
00092     case WM_SETHOTKEY:
00093     case WM_SYSCHAR:
00094     case WM_SYSDEADCHAR:
00095     case WM_SYSKEYDOWN:
00096     case WM_SYSKEYUP:
00097 
00098     case WM_NCCALCSIZE:
00099         return 0;
00100 
00101     case WM_PAINT:
00102 
00103         hdc = BeginPaint(hWnd, &ps);
00104 
00105         WindowId = GetWindowLongPtrW(hWnd, GWL_ID);
00106 
00107         switch (WindowId)
00108         {
00109         case IDC_CPU_USAGE_GRAPH:
00110             Graph_DrawCpuUsageGraph(hdc, hWnd);
00111             break;
00112         case IDC_MEM_USAGE_GRAPH:
00113             Graph_DrawMemUsageGraph(hdc, hWnd);
00114             break;
00115         case IDC_MEM_USAGE_HISTORY_GRAPH:
00116             Graph_DrawMemUsageHistoryGraph(hdc, hWnd);
00117             break;
00118         }
00119 
00120         EndPaint(hWnd, &ps);
00121 
00122         return 0;
00123 
00124     }
00125 
00126     /*
00127      * We pass on all non-handled messages
00128      */
00129     return CallWindowProcW((WNDPROC)OldGraphWndProc, hWnd, message, wParam, lParam);
00130 }
00131 
00132 void Graph_DrawCpuUsageGraph(HDC hDC, HWND hWnd)
00133 {
00134     RECT      rcClient;
00135     RECT      rcBarLeft;
00136     RECT      rcBarRight;
00137     RECT      rcText;
00138     COLORREF  crPrevForeground;
00139     WCHAR     Text[260];
00140     HFONT     hOldFont;
00141     ULONG     CpuUsage;
00142     ULONG     CpuKernelUsage;
00143     int       nBars;
00144     int       nBarsUsed;
00145 /* Bottom bars that are "used", i.e. are bright green, representing used cpu time */
00146     int       nBarsUsedKernel;
00147 /* Bottom bars that are "used", i.e. are bright green, representing used cpu kernel time */
00148     int       nBarsFree;
00149 /* Top bars that are "unused", i.e. are dark green, representing free cpu time */
00150     int       i;
00151 
00152     /*
00153      * Get the client area rectangle
00154      */
00155     GetClientRect(hWnd, &rcClient);
00156 
00157     /*
00158      * Fill it with blackness
00159      */
00160     FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
00161 
00162     /*
00163      * Get the CPU usage
00164      */
00165     CpuUsage = PerfDataGetProcessorUsage();
00166     if (CpuUsage <= 0)   CpuUsage = 0;
00167     if (CpuUsage > 100)  CpuUsage = 100;
00168 
00169     wsprintfW(Text, L"%d%%", (int)CpuUsage);
00170 
00171     /*
00172      * Draw the font text onto the graph
00173      */
00174     rcText = rcClient;
00175     InflateRect(&rcText, -2, -2);
00176     crPrevForeground = SetTextColor(hDC, RGB(0, 255, 0));
00177     hOldFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
00178     DrawTextW(hDC, Text, -1, &rcText, DT_BOTTOM | DT_CENTER | DT_NOPREFIX | DT_SINGLELINE);
00179     SelectObject(hDC, hOldFont);
00180     SetTextColor(hDC, crPrevForeground);
00181 
00182     /*
00183      * Now we have to draw the graph
00184      * So first find out how many bars we can fit
00185      */
00186     nBars = ((rcClient.bottom - rcClient.top) - 25) / 3;
00187     nBarsUsed = (nBars * CpuUsage) / 100;
00188     if ((CpuUsage) && (nBarsUsed == 0))
00189     {
00190         nBarsUsed = 1;
00191     }
00192     nBarsFree = nBars - (nlastBarsUsed>nBarsUsed ? nlastBarsUsed : nBarsUsed);
00193 
00194     if (TaskManagerSettings.ShowKernelTimes)
00195     {
00196         CpuKernelUsage = PerfDataGetProcessorSystemUsage();
00197         if (CpuKernelUsage <= 0)   CpuKernelUsage = 0;
00198         if (CpuKernelUsage >= 100) CpuKernelUsage = 100;
00199         nBarsUsedKernel = (nBars * CpuKernelUsage) / 100;
00200     }
00201     else
00202     {
00203         nBarsUsedKernel = 0;
00204     }
00205 
00206     /*
00207      * Now draw the bar graph
00208      */
00209     rcBarLeft.left =  ((rcClient.right - rcClient.left) - 33) / 2;
00210     rcBarLeft.right =  rcBarLeft.left + 16;
00211     rcBarRight.left = rcBarLeft.left + 17;
00212     rcBarRight.right = rcBarLeft.right + 17;
00213     rcBarLeft.top = rcBarRight.top = 5;
00214     rcBarLeft.bottom = rcBarRight.bottom = 7;
00215 
00216     if (nBarsUsed < 0)     nBarsUsed = 0;
00217     if (nBarsUsed > nBars) nBarsUsed = nBars;
00218 
00219     if (nBarsFree < 0)     nBarsFree = 0;
00220     if (nBarsFree > nBars) nBarsFree = nBars;
00221 
00222     if (nBarsUsedKernel < 0)     nBarsUsedKernel = 0;
00223     if (nBarsUsedKernel > nBars) nBarsUsedKernel = nBars;
00224 
00225     /*
00226      * Draw the "free" bars
00227      */
00228     for (i=0; i<nBarsFree; i++)
00229     {
00230         FillSolidRect(hDC, &rcBarLeft, DARK_GREEN);
00231         FillSolidRect(hDC, &rcBarRight, DARK_GREEN);
00232 
00233         rcBarLeft.top += 3;
00234         rcBarLeft.bottom += 3;
00235 
00236         rcBarRight.top += 3;
00237         rcBarRight.bottom += 3;
00238     }
00239 
00240     /*
00241      * Draw the last "used" bars
00242      */
00243     if ((nlastBarsUsed - nBarsUsed) > 0) {
00244         for (i=0; i< (nlastBarsUsed - nBarsUsed); i++)
00245         {
00246             if (nlastBarsUsed > 5000) nlastBarsUsed = 5000;
00247 
00248             FillSolidRect(hDC, &rcBarLeft, MEDIUM_GREEN);
00249             FillSolidRect(hDC, &rcBarRight, MEDIUM_GREEN);
00250 
00251             rcBarLeft.top += 3;
00252             rcBarLeft.bottom += 3;
00253 
00254             rcBarRight.top += 3;
00255             rcBarRight.bottom += 3;
00256         }
00257     }
00258     nlastBarsUsed = nBarsUsed;
00259     /*
00260      * Draw the "used" bars
00261      */
00262     for (i=0; i<nBarsUsed; i++)
00263     {
00264         if (nBarsUsed > 5000) nBarsUsed = 5000;
00265 
00266         FillSolidRect(hDC, &rcBarLeft, BRIGHT_GREEN);
00267         FillSolidRect(hDC, &rcBarRight, BRIGHT_GREEN);
00268 
00269         rcBarLeft.top += 3;
00270         rcBarLeft.bottom += 3;
00271 
00272         rcBarRight.top += 3;
00273         rcBarRight.bottom += 3;
00274     }
00275 
00276     /*
00277      * Draw the "used" kernel bars
00278      */
00279 
00280     rcBarLeft.top -=3;
00281     rcBarLeft.bottom -=3;
00282 
00283     rcBarRight.top -=3;
00284     rcBarRight.bottom -=3;
00285 
00286     for (i=0; i<nBarsUsedKernel; i++)
00287     {
00288 
00289         FillSolidRect(hDC, &rcBarLeft, RED);
00290         FillSolidRect(hDC, &rcBarRight, RED);
00291 
00292         rcBarLeft.top -=3;
00293         rcBarLeft.bottom -=3;
00294 
00295         rcBarRight.top -=3;
00296         rcBarRight.bottom -=3;
00297 
00298     }
00299 
00300     SelectObject(hDC, hOldFont);
00301 }
00302 
00303 void Graph_DrawMemUsageGraph(HDC hDC, HWND hWnd)
00304 {
00305     RECT       rcClient;
00306     RECT       rcBarLeft;
00307     RECT       rcBarRight;
00308     RECT       rcText;
00309     COLORREF   crPrevForeground;
00310     WCHAR      Text[260];
00311     HFONT      hOldFont;
00312     ULONGLONG  CommitChargeTotal;
00313     ULONGLONG  CommitChargeLimit;
00314     int        nBars;
00315     int        nBarsUsed = 0;
00316 /* Bottom bars that are "used", i.e. are bright green, representing used memory */
00317     int        nBarsFree;
00318 /* Top bars that are "unused", i.e. are dark green, representing free memory */
00319     int        i;
00320 
00321     /*
00322      * Get the client area rectangle
00323      */
00324     GetClientRect(hWnd, &rcClient);
00325 
00326     /*
00327      * Fill it with blackness
00328      */
00329     FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
00330 
00331     /*
00332      * Get the memory usage
00333      */
00334     CommitChargeTotal = (ULONGLONG)PerfDataGetCommitChargeTotalK();
00335     CommitChargeLimit = (ULONGLONG)PerfDataGetCommitChargeLimitK();
00336 
00337     if (CommitChargeTotal > 1024)
00338         wsprintfW(Text, L"%d MB", (int)(CommitChargeTotal / 1024));
00339     else
00340         wsprintfW(Text, L"%d K", (int)CommitChargeTotal);
00341     /*
00342      * Draw the font text onto the graph
00343      */
00344     rcText = rcClient;
00345     InflateRect(&rcText, -2, -2);
00346     crPrevForeground = SetTextColor(hDC, RGB(0, 255, 0));
00347     hOldFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
00348     DrawTextW(hDC, Text, -1, &rcText, DT_BOTTOM | DT_CENTER | DT_NOPREFIX | DT_SINGLELINE);
00349     SelectObject(hDC, hOldFont);
00350     SetTextColor(hDC, crPrevForeground);
00351 
00352     /*
00353      * Now we have to draw the graph
00354      * So first find out how many bars we can fit
00355      */
00356     nBars = ((rcClient.bottom - rcClient.top) - 25) / 3;
00357         if (CommitChargeLimit)
00358     nBarsUsed = (nBars * (int)((CommitChargeTotal * 100) / CommitChargeLimit)) / 100;
00359     nBarsFree = nBars - nBarsUsed;
00360 
00361     if (nBarsUsed < 0)     nBarsUsed = 0;
00362     if (nBarsUsed > nBars) nBarsUsed = nBars;
00363 
00364     if (nBarsFree < 0)     nBarsFree = 0;
00365     if (nBarsFree > nBars) nBarsFree = nBars;
00366 
00367     /*
00368      * Now draw the bar graph
00369      */
00370     rcBarLeft.left =  ((rcClient.right - rcClient.left) - 33) / 2;
00371     rcBarLeft.right =  rcBarLeft.left + 16;
00372     rcBarRight.left = rcBarLeft.left + 17;
00373     rcBarRight.right = rcBarLeft.right + 17;
00374     rcBarLeft.top = rcBarRight.top = 5;
00375     rcBarLeft.bottom = rcBarRight.bottom = 7;
00376 
00377     /*
00378      * Draw the "free" bars
00379      */
00380     for (i=0; i<nBarsFree; i++)
00381     {
00382         FillSolidRect(hDC, &rcBarLeft, DARK_GREEN);
00383         FillSolidRect(hDC, &rcBarRight, DARK_GREEN);
00384 
00385         rcBarLeft.top += 3;
00386         rcBarLeft.bottom += 3;
00387 
00388         rcBarRight.top += 3;
00389         rcBarRight.bottom += 3;
00390     }
00391 
00392     /*
00393      * Draw the "used" bars
00394      */
00395     for (i=0; i<nBarsUsed; i++)
00396     {
00397         FillSolidRect(hDC, &rcBarLeft, BRIGHT_GREEN);
00398         FillSolidRect(hDC, &rcBarRight, BRIGHT_GREEN);
00399 
00400         rcBarLeft.top += 3;
00401         rcBarLeft.bottom += 3;
00402 
00403         rcBarRight.top += 3;
00404         rcBarRight.bottom += 3;
00405     }
00406 
00407     SelectObject(hDC, hOldFont);
00408 }
00409 
00410 void Graph_DrawMemUsageHistoryGraph(HDC hDC, HWND hWnd)
00411 {
00412     RECT        rcClient;
00413     //ULONGLONG   CommitChargeLimit;
00414     int         i;
00415     static int  offset = 0;
00416 
00417     if (offset++ >= 10)
00418         offset = 0;
00419 
00420     /*
00421      * Get the client area rectangle
00422      */
00423     GetClientRect(hWnd, &rcClient);
00424 
00425     /*
00426      * Fill it with blackness
00427      */
00428     FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
00429 
00430     /*
00431      * Get the memory usage
00432      */
00433     //CommitChargeLimit = (ULONGLONG)PerfDataGetCommitChargeLimitK();
00434 
00435     /*
00436      * Draw the graph background
00437      *
00438      * Draw the horizontal bars
00439      */
00440     for (i=0; i<rcClient.bottom; i++)
00441     {
00442         if ((i % 11) == 0)
00443         {
00444             /* FillSolidRect2(hDC, 0, i, rcClient.right, 1, DARK_GREEN);  */
00445         }
00446     }
00447     /*
00448      * Draw the vertical bars
00449      */
00450     for (i=11; i<rcClient.right + offset; i++)
00451     {
00452         if ((i % 11) == 0)
00453         {
00454             /* FillSolidRect2(hDC, i - offset, 0, 1, rcClient.bottom, DARK_GREEN);  */
00455         }
00456     }
00457 
00458     /*
00459      * Draw the memory usage
00460      */
00461     for (i=rcClient.right; i>=0; i--)
00462     {
00463     }
00464 }

Generated on Fri May 25 2012 04:15:52 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.