6 * Copyright (C) 2002 Robert Dickenson <robd@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
32 WNDPROC OldGraphCtrlWndProc
;
34 static void GraphCtrl_Init(TGraphCtrl
* this)
39 this->m_hParentWnd
= 0;
42 this->m_bitmapOldGrid
= 0;
43 this->m_bitmapOldPlot
= 0;
44 this->m_bitmapGrid
= 0;
45 this->m_bitmapPlot
= 0;
46 this->m_brushBack
= 0;
48 this->m_penPlot
[0] = 0;
49 this->m_penPlot
[1] = 0;
50 this->m_penPlot
[2] = 0;
51 this->m_penPlot
[3] = 0;
53 /* since plotting is based on a LineTo for each new point
54 * we need a starting point (i.e. a "previous" point)
55 * use 0.0 as the default first point.
56 * these are public member variables, and can be changed outside
57 * (after construction). Therefore m_dPreviousPosition could be set to
58 * a more appropriate value prior to the first call to SetPosition.
60 this->m_dPreviousPosition
[0] = 0.0;
61 this->m_dPreviousPosition
[1] = 0.0;
62 this->m_dPreviousPosition
[2] = 0.0;
63 this->m_dPreviousPosition
[3] = 0.0;
65 /* public variable for the number of decimal places on the y axis */
66 this->m_nYDecimals
= 3;
68 /* set some initial values for the scaling until "SetRange" is called.
69 * these are protected variables and must be set with SetRange
70 * in order to ensure that m_dRange is updated accordingly
72 /* m_dLowerLimit = -10.0; */
73 /* m_dUpperLimit = 10.0; */
74 this->m_dLowerLimit
= 0.0;
75 this->m_dUpperLimit
= 100.0;
76 this->m_dRange
= this->m_dUpperLimit
- this->m_dLowerLimit
; /* protected member variable */
78 /* m_nShiftPixels determines how much the plot shifts (in terms of pixels) */
79 /* with the addition of a new data point */
80 this->m_nShiftPixels
= 4;
81 this->m_nHalfShiftPixels
= this->m_nShiftPixels
/2; /* protected */
82 this->m_nPlotShiftPixels
= this->m_nShiftPixels
+ this->m_nHalfShiftPixels
; /* protected */
84 /* background, grid and data colors */
85 /* these are public variables and can be set directly */
86 this->m_crBackColor
= RGB( 0, 0, 0); /* see also SetBackgroundColor */
87 this->m_crGridColor
= RGB( 0, 255, 255); /* see also SetGridColor */
88 this->m_crPlotColor
[0] = RGB(255, 255, 255); /* see also SetPlotColor */
89 this->m_crPlotColor
[1] = RGB(100, 255, 255); /* see also SetPlotColor */
90 this->m_crPlotColor
[2] = RGB(255, 100, 255); /* see also SetPlotColor */
91 this->m_crPlotColor
[3] = RGB(255, 255, 100); /* see also SetPlotColor */
93 /* protected variables */
94 for (i
= 0; i
< MAX_PLOTS
; i
++)
96 this->m_penPlot
[i
] = CreatePen(PS_SOLID
, 0, this->m_crPlotColor
[i
]);
98 this->m_brushBack
= CreateSolidBrush(this->m_crBackColor
);
100 /* public member variables, can be set directly */
101 strcpy(this->m_strXUnitsString
, "Samples"); /* can also be set with SetXUnits */
102 strcpy(this->m_strYUnitsString
, "Y units"); /* can also be set with SetYUnits */
104 /* protected bitmaps to restore the memory DC's */
105 this->m_bitmapOldGrid
= NULL
;
106 this->m_bitmapOldPlot
= NULL
;
109 static void GraphCtrl_Resize(TGraphCtrl
* this)
111 /* NOTE: Resize automatically gets called during the setup of the control */
112 GetClientRect(this->m_hWnd
, &this->m_rectClient
);
114 /* set some member variables to avoid multiple function calls */
115 this->m_nClientHeight
= this->m_rectClient
.bottom
- this->m_rectClient
.top
;/* m_rectClient.Height(); */
116 this->m_nClientWidth
= this->m_rectClient
.right
- this->m_rectClient
.left
;/* m_rectClient.Width(); */
118 /* the "left" coordinate and "width" will be modified in */
119 /* InvalidateCtrl to be based on the width of the y axis scaling */
120 SetRect(&this->m_rectPlot
, 0, -1, this->m_rectClient
.right
, this->m_rectClient
.bottom
);
122 /* set some member variables to avoid multiple function calls */
123 this->m_nPlotHeight
= this->m_rectPlot
.bottom
- this->m_rectPlot
.top
;/* m_rectPlot.Height(); */
124 this->m_nPlotWidth
= this->m_rectPlot
.right
- this->m_rectPlot
.left
;/* m_rectPlot.Width(); */
126 /* set the scaling factor for now, this can be adjusted */
127 /* in the SetRange functions */
128 this->m_dVerticalFactor
= (double)this->m_nPlotHeight
/ this->m_dRange
;
131 void GraphCtrl_Create(TGraphCtrl
* this, HWND hWnd
, HWND hParentWnd
, UINT nID
)
133 GraphCtrl_Init(this);
134 this->m_hParentWnd
= hParentWnd
;
136 GraphCtrl_Resize(this);
139 static void GraphCtrl_InvalidateCtrl(TGraphCtrl
* this)
141 /* There is a lot of drawing going on here - particularly in terms of */
142 /* drawing the grid. Don't panic, this is all being drawn (only once) */
143 /* to a bitmap. The result is then BitBlt'd to the control whenever needed. */
146 int nTopGridPix
, nMidGridPix
, nBottomGridPix
;
149 HPEN solidPen
= CreatePen(PS_SOLID
, 0, this->m_crGridColor
);
150 /* HFONT axisFont, yUnitFont, oldFont; */
151 /* char strTemp[50]; */
153 /* in case we haven't established the memory dc's */
154 /* CClientDC dc(this); */
155 HDC dc
= GetDC(this->m_hParentWnd
);
157 /* if we don't have one yet, set up a memory dc for the grid */
158 if (this->m_dcGrid
== NULL
)
160 this->m_dcGrid
= CreateCompatibleDC(dc
);
161 this->m_bitmapGrid
= CreateCompatibleBitmap(dc
, this->m_nClientWidth
, this->m_nClientHeight
);
162 this->m_bitmapOldGrid
= SelectObject(this->m_dcGrid
, this->m_bitmapGrid
);
165 SetBkColor(this->m_dcGrid
, this->m_crBackColor
);
167 /* fill the grid background */
168 FillRect(this->m_dcGrid
, &this->m_rectClient
, this->m_brushBack
);
170 /* draw the plot rectangle: */
171 /* determine how wide the y axis scaling values are */
172 nCharacters
= abs((int)log10(fabs(this->m_dUpperLimit
)));
173 nCharacters
= max(nCharacters
, abs((int)log10(fabs(this->m_dLowerLimit
))));
175 /* add the units digit, decimal point and a minus sign, and an extra space */
176 /* as well as the number of decimal places to display */
177 nCharacters
= nCharacters
+ 4 + this->m_nYDecimals
;
179 /* adjust the plot rectangle dimensions */
180 /* assume 6 pixels per character (this may need to be adjusted) */
181 /* m_rectPlot.left = m_rectClient.left + 6*(nCharacters); */
182 this->m_rectPlot
.left
= this->m_rectClient
.left
;
183 this->m_nPlotWidth
= this->m_rectPlot
.right
- this->m_rectPlot
.left
;/* m_rectPlot.Width(); */
185 /* draw the plot rectangle */
186 oldPen
= SelectObject(this->m_dcGrid
, solidPen
);
187 MoveToEx(this->m_dcGrid
, this->m_rectPlot
.left
, this->m_rectPlot
.top
, NULL
);
188 LineTo(this->m_dcGrid
, this->m_rectPlot
.right
+1, this->m_rectPlot
.top
);
189 LineTo(this->m_dcGrid
, this->m_rectPlot
.right
+1, this->m_rectPlot
.bottom
+1);
190 LineTo(this->m_dcGrid
, this->m_rectPlot
.left
, this->m_rectPlot
.bottom
+1);
191 /* LineTo(m_dcGrid, m_rectPlot.left, m_rectPlot.top); */
192 SelectObject(this->m_dcGrid
, oldPen
);
193 DeleteObject(solidPen
);
195 /* draw the dotted lines,
196 * use SetPixel instead of a dotted pen - this allows for a
197 * finer dotted line and a more "technical" look
199 nMidGridPix
= (this->m_rectPlot
.top
+ this->m_rectPlot
.bottom
)/2;
200 nTopGridPix
= nMidGridPix
- this->m_nPlotHeight
/4;
201 nBottomGridPix
= nMidGridPix
+ this->m_nPlotHeight
/4;
203 for (i
=this->m_rectPlot
.left
; i
<this->m_rectPlot
.right
; i
+=2)
205 SetPixel(this->m_dcGrid
, i
, nTopGridPix
, this->m_crGridColor
);
206 SetPixel(this->m_dcGrid
, i
, nMidGridPix
, this->m_crGridColor
);
207 SetPixel(this->m_dcGrid
, i
, nBottomGridPix
, this->m_crGridColor
);
210 for (i
=this->m_rectPlot
.left
; i
<this->m_rectPlot
.right
; i
+=10)
212 for (j
=this->m_rectPlot
.top
; j
<this->m_rectPlot
.bottom
; j
+=2)
214 SetPixel(this->m_dcGrid
, i
, j
, this->m_crGridColor
);
215 /* SetPixel(m_dcGrid, i, j, m_crGridColor); */
216 /* SetPixel(m_dcGrid, i, j, m_crGridColor); */
220 /* at this point we are done filling the grid bitmap, */
221 /* no more drawing to this bitmap is needed until the settings are changed */
223 /* if we don't have one yet, set up a memory dc for the plot */
224 if (this->m_dcPlot
== NULL
)
226 this->m_dcPlot
= CreateCompatibleDC(dc
);
227 this->m_bitmapPlot
= CreateCompatibleBitmap(dc
, this->m_nClientWidth
, this->m_nClientHeight
);
228 this->m_bitmapOldPlot
= SelectObject(this->m_dcPlot
, this->m_bitmapPlot
);
231 /* make sure the plot bitmap is cleared */
232 SetBkColor(this->m_dcPlot
, this->m_crBackColor
);
233 FillRect(this->m_dcPlot
, &this->m_rectClient
, this->m_brushBack
);
235 /* finally, force the plot area to redraw */
236 InvalidateRect(this->m_hParentWnd
, &this->m_rectClient
, TRUE
);
237 ReleaseDC(this->m_hParentWnd
, dc
);
240 void GraphCtrl_SetRange(TGraphCtrl
* this, double dLower
, double dUpper
, int nDecimalPlaces
)
242 /* ASSERT(dUpper > dLower); */
243 this->m_dLowerLimit
= dLower
;
244 this->m_dUpperLimit
= dUpper
;
245 this->m_nYDecimals
= nDecimalPlaces
;
246 this->m_dRange
= this->m_dUpperLimit
- this->m_dLowerLimit
;
247 this->m_dVerticalFactor
= (double)this->m_nPlotHeight
/ this->m_dRange
;
248 /* clear out the existing garbage, re-start with a clean plot */
249 GraphCtrl_InvalidateCtrl(this);
252 void GraphCtrl_SetGridColor(TGraphCtrl
* this, COLORREF color
)
254 this->m_crGridColor
= color
;
255 /* clear out the existing garbage, re-start with a clean plot */
256 GraphCtrl_InvalidateCtrl(this);
259 void GraphCtrl_SetPlotColor(TGraphCtrl
* this, int plot
, COLORREF color
)
261 this->m_crPlotColor
[plot
] = color
;
262 DeleteObject(this->m_penPlot
[plot
]);
263 this->m_penPlot
[plot
] = CreatePen(PS_SOLID
, 0, this->m_crPlotColor
[plot
]);
264 /* clear out the existing garbage, re-start with a clean plot */
265 GraphCtrl_InvalidateCtrl(this);
268 void GraphCtrl_SetBackgroundColor(TGraphCtrl
* this, COLORREF color
)
270 this->m_crBackColor
= color
;
271 DeleteObject(this->m_brushBack
);
272 this->m_brushBack
= CreateSolidBrush(this->m_crBackColor
);
273 /* clear out the existing garbage, re-start with a clean plot */
274 GraphCtrl_InvalidateCtrl(this);
277 static void GraphCtrl_DrawPoint(TGraphCtrl
* this)
279 /* this does the work of "scrolling" the plot to the left
280 * and appending a new data point all of the plotting is
281 * directed to the memory based bitmap associated with m_dcPlot
282 * the will subsequently be BitBlt'd to the client in Paint
284 int currX
, prevX
, currY
, prevY
;
289 if (this->m_dcPlot
!= NULL
)
291 /* shift the plot by BitBlt'ing it to itself
292 * note: the m_dcPlot covers the entire client
293 * but we only shift bitmap that is the size
294 * of the plot rectangle
295 * grab the right side of the plot (excluding m_nShiftPixels on the left)
296 * move this grabbed bitmap to the left by m_nShiftPixels
298 BitBlt(this->m_dcPlot
, this->m_rectPlot
.left
, this->m_rectPlot
.top
+1,
299 this->m_nPlotWidth
, this->m_nPlotHeight
, this->m_dcPlot
,
300 this->m_rectPlot
.left
+this->m_nShiftPixels
, this->m_rectPlot
.top
+1,
303 /* establish a rectangle over the right side of plot */
304 /* which now needs to be cleaned up prior to adding the new point */
305 rectCleanUp
= this->m_rectPlot
;
306 rectCleanUp
.left
= rectCleanUp
.right
- this->m_nShiftPixels
;
308 /* fill the cleanup area with the background */
309 FillRect(this->m_dcPlot
, &rectCleanUp
, this->m_brushBack
);
311 /* draw the next line segment */
312 for (i
= 0; i
< MAX_PLOTS
; i
++)
314 /* grab the plotting pen */
315 oldPen
= SelectObject(this->m_dcPlot
, this->m_penPlot
[i
]);
317 /* move to the previous point */
318 prevX
= this->m_rectPlot
.right
-this->m_nPlotShiftPixels
;
319 prevY
= this->m_rectPlot
.bottom
-
320 (int)((this->m_dPreviousPosition
[i
] - this->m_dLowerLimit
) * this->m_dVerticalFactor
);
321 MoveToEx(this->m_dcPlot
, prevX
, prevY
, NULL
);
323 /* draw to the current point */
324 currX
= this->m_rectPlot
.right
-this->m_nHalfShiftPixels
;
325 currY
= this->m_rectPlot
.bottom
-
326 (int)((this->m_dCurrentPosition
[i
] - this->m_dLowerLimit
) * this->m_dVerticalFactor
);
327 LineTo(this->m_dcPlot
, currX
, currY
);
329 /* Restore the pen */
330 SelectObject(this->m_dcPlot
, oldPen
);
332 /* if the data leaks over the upper or lower plot boundaries
333 * fill the upper and lower leakage with the background
334 * this will facilitate clipping on an as needed basis
335 * as opposed to always calling IntersectClipRect
337 if ((prevY
<= this->m_rectPlot
.top
) || (currY
<= this->m_rectPlot
.top
))
340 SetRect(&rc
, prevX
, this->m_rectClient
.top
, currX
+ 1, this->m_rectPlot
.top
+ 1);
341 FillRect(this->m_dcPlot
, &rc
, this->m_brushBack
);
343 if ((prevY
>= this->m_rectPlot
.bottom
) || (currY
>= this->m_rectPlot
.bottom
))
346 SetRect(&rc
, prevX
, this->m_rectPlot
.bottom
+ 1, currX
+ 1,
347 this->m_rectClient
.bottom
+ 1);
348 /* RECT rc(prevX, m_rectPlot.bottom+1, currX+1, m_rectClient.bottom+1); */
349 FillRect(this->m_dcPlot
, &rc
, this->m_brushBack
);
352 /* store the current point for connection to the next point */
353 this->m_dPreviousPosition
[i
] = this->m_dCurrentPosition
[i
];
358 double GraphCtrl_AppendPoint(TGraphCtrl
* this,
359 double dNewPoint0
, double dNewPoint1
,
360 double dNewPoint2
, double dNewPoint3
)
362 /* append a data point to the plot & return the previous point */
365 dPrevious
= this->m_dCurrentPosition
[0];
366 this->m_dCurrentPosition
[0] = dNewPoint0
;
367 this->m_dCurrentPosition
[1] = dNewPoint1
;
368 this->m_dCurrentPosition
[2] = dNewPoint2
;
369 this->m_dCurrentPosition
[3] = dNewPoint3
;
370 GraphCtrl_DrawPoint(this);
375 static void GraphCtrl_Paint(TGraphCtrl
* this, HWND hWnd
, HDC dc
)
379 HBITMAP oldBitmap
; /* bitmap originally found in CMemDC */
381 /* no real plotting work is performed here, */
382 /* just putting the existing bitmaps on the client */
384 /* to avoid flicker, establish a memory dc, draw to it */
385 /* and then BitBlt it to the client */
386 memDC
= CreateCompatibleDC(dc
);
387 memBitmap
= CreateCompatibleBitmap(dc
, this->m_nClientWidth
, this->m_nClientHeight
);
388 oldBitmap
= SelectObject(memDC
, memBitmap
);
392 /* first drop the grid on the memory dc */
393 BitBlt(memDC
, 0, 0, this->m_nClientWidth
, this->m_nClientHeight
, this->m_dcGrid
, 0, 0, SRCCOPY
);
394 /* now add the plot on top as a "pattern" via SRCPAINT. */
395 /* works well with dark background and a light plot */
396 BitBlt(memDC
, 0, 0, this->m_nClientWidth
, this->m_nClientHeight
, this->m_dcPlot
, 0, 0, SRCPAINT
); /* SRCPAINT */
397 /* finally send the result to the display */
398 BitBlt(dc
, 0, 0, this->m_nClientWidth
, this->m_nClientHeight
, memDC
, 0, 0, SRCCOPY
);
400 SelectObject(memDC
, oldBitmap
);
401 DeleteObject(memBitmap
);
405 extern TGraphCtrl PerformancePageCpuUsageHistoryGraph
;
406 extern TGraphCtrl PerformancePageMemUsageHistoryGraph
;
407 extern HWND hPerformancePageCpuUsageHistoryGraph
;
408 extern HWND hPerformancePageMemUsageHistoryGraph
;
411 GraphCtrl_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
422 * Filter out mouse & keyboard messages
424 /* case WM_APPCOMMAND: */
425 case WM_CAPTURECHANGED
:
426 case WM_LBUTTONDBLCLK
:
429 case WM_MBUTTONDBLCLK
:
432 case WM_MOUSEACTIVATE
:
436 /* case WM_MOUSEWHEEL: */
438 case WM_NCLBUTTONDBLCLK
:
439 case WM_NCLBUTTONDOWN
:
441 case WM_NCMBUTTONDBLCLK
:
442 case WM_NCMBUTTONDOWN
:
444 /* case WM_NCMOUSEHOVER: */
445 /* case WM_NCMOUSELEAVE: */
447 case WM_NCRBUTTONDBLCLK
:
448 case WM_NCRBUTTONDOWN
:
450 /* case WM_NCXBUTTONDBLCLK: */
451 /* case WM_NCXBUTTONDOWN: */
452 /* case WM_NCXBUTTONUP: */
453 case WM_RBUTTONDBLCLK
:
456 /* case WM_XBUTTONDBLCLK: */
457 /* case WM_XBUTTONDOWN: */
458 /* case WM_XBUTTONUP: */
479 if (hWnd
== hPerformancePageMemUsageHistoryGraph
)
481 GraphCtrl_Resize(&PerformancePageMemUsageHistoryGraph
);
482 GraphCtrl_InvalidateCtrl(&PerformancePageMemUsageHistoryGraph
);
484 if (hWnd
== hPerformancePageCpuUsageHistoryGraph
)
486 GraphCtrl_Resize(&PerformancePageCpuUsageHistoryGraph
);
487 GraphCtrl_InvalidateCtrl(&PerformancePageCpuUsageHistoryGraph
);
492 hdc
= BeginPaint(hWnd
, &ps
);
493 GetClientRect(hWnd
, &rcClient
);
494 if (hWnd
== hPerformancePageMemUsageHistoryGraph
)
495 GraphCtrl_Paint(&PerformancePageMemUsageHistoryGraph
, hWnd
, hdc
);
496 if (hWnd
== hPerformancePageCpuUsageHistoryGraph
)
497 GraphCtrl_Paint(&PerformancePageCpuUsageHistoryGraph
, hWnd
, hdc
);
503 * We pass on all non-handled messages
505 return CallWindowProcW(OldGraphCtrlWndProc
, hWnd
, message
, wParam
, lParam
);