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 this->m_rectPlot
.left
= 0;
121 this->m_rectPlot
.top
= -1;
122 this->m_rectPlot
.right
= this->m_rectClient
.right
-0;
123 this->m_rectPlot
.bottom
= this->m_rectClient
.bottom
-0;
125 /* set some member variables to avoid multiple function calls */
126 this->m_nPlotHeight
= this->m_rectPlot
.bottom
- this->m_rectPlot
.top
;/* m_rectPlot.Height(); */
127 this->m_nPlotWidth
= this->m_rectPlot
.right
- this->m_rectPlot
.left
;/* m_rectPlot.Width(); */
129 /* set the scaling factor for now, this can be adjusted */
130 /* in the SetRange functions */
131 this->m_dVerticalFactor
= (double)this->m_nPlotHeight
/ this->m_dRange
;
134 void GraphCtrl_Create(TGraphCtrl
* this, HWND hWnd
, HWND hParentWnd
, UINT nID
)
136 GraphCtrl_Init(this);
137 this->m_hParentWnd
= hParentWnd
;
139 GraphCtrl_Resize(this);
142 static void GraphCtrl_InvalidateCtrl(TGraphCtrl
* this)
144 /* There is a lot of drawing going on here - particularly in terms of */
145 /* drawing the grid. Don't panic, this is all being drawn (only once) */
146 /* to a bitmap. The result is then BitBlt'd to the control whenever needed. */
149 int nTopGridPix
, nMidGridPix
, nBottomGridPix
;
152 HPEN solidPen
= CreatePen(PS_SOLID
, 0, this->m_crGridColor
);
153 /* HFONT axisFont, yUnitFont, oldFont; */
154 /* char strTemp[50]; */
156 /* in case we haven't established the memory dc's */
157 /* CClientDC dc(this); */
158 HDC dc
= GetDC(this->m_hParentWnd
);
160 /* if we don't have one yet, set up a memory dc for the grid */
161 if (this->m_dcGrid
== NULL
)
163 this->m_dcGrid
= CreateCompatibleDC(dc
);
164 this->m_bitmapGrid
= CreateCompatibleBitmap(dc
, this->m_nClientWidth
, this->m_nClientHeight
);
165 this->m_bitmapOldGrid
= SelectObject(this->m_dcGrid
, this->m_bitmapGrid
);
168 SetBkColor(this->m_dcGrid
, this->m_crBackColor
);
170 /* fill the grid background */
171 FillRect(this->m_dcGrid
, &this->m_rectClient
, this->m_brushBack
);
173 /* draw the plot rectangle: */
174 /* determine how wide the y axis scaling values are */
175 nCharacters
= abs((int)log10(fabs(this->m_dUpperLimit
)));
176 nCharacters
= max(nCharacters
, abs((int)log10(fabs(this->m_dLowerLimit
))));
178 /* add the units digit, decimal point and a minus sign, and an extra space */
179 /* as well as the number of decimal places to display */
180 nCharacters
= nCharacters
+ 4 + this->m_nYDecimals
;
182 /* adjust the plot rectangle dimensions */
183 /* assume 6 pixels per character (this may need to be adjusted) */
184 /* m_rectPlot.left = m_rectClient.left + 6*(nCharacters); */
185 this->m_rectPlot
.left
= this->m_rectClient
.left
;
186 this->m_nPlotWidth
= this->m_rectPlot
.right
- this->m_rectPlot
.left
;/* m_rectPlot.Width(); */
188 /* draw the plot rectangle */
189 oldPen
= SelectObject(this->m_dcGrid
, solidPen
);
190 MoveToEx(this->m_dcGrid
, this->m_rectPlot
.left
, this->m_rectPlot
.top
, NULL
);
191 LineTo(this->m_dcGrid
, this->m_rectPlot
.right
+1, this->m_rectPlot
.top
);
192 LineTo(this->m_dcGrid
, this->m_rectPlot
.right
+1, this->m_rectPlot
.bottom
+1);
193 LineTo(this->m_dcGrid
, this->m_rectPlot
.left
, this->m_rectPlot
.bottom
+1);
194 /* LineTo(m_dcGrid, m_rectPlot.left, m_rectPlot.top); */
195 SelectObject(this->m_dcGrid
, oldPen
);
196 DeleteObject(solidPen
);
198 /* draw the dotted lines,
199 * use SetPixel instead of a dotted pen - this allows for a
200 * finer dotted line and a more "technical" look
202 nMidGridPix
= (this->m_rectPlot
.top
+ this->m_rectPlot
.bottom
)/2;
203 nTopGridPix
= nMidGridPix
- this->m_nPlotHeight
/4;
204 nBottomGridPix
= nMidGridPix
+ this->m_nPlotHeight
/4;
206 for (i
=this->m_rectPlot
.left
; i
<this->m_rectPlot
.right
; i
+=2)
208 SetPixel(this->m_dcGrid
, i
, nTopGridPix
, this->m_crGridColor
);
209 SetPixel(this->m_dcGrid
, i
, nMidGridPix
, this->m_crGridColor
);
210 SetPixel(this->m_dcGrid
, i
, nBottomGridPix
, this->m_crGridColor
);
213 for (i
=this->m_rectPlot
.left
; i
<this->m_rectPlot
.right
; i
+=10)
215 for (j
=this->m_rectPlot
.top
; j
<this->m_rectPlot
.bottom
; j
+=2)
217 SetPixel(this->m_dcGrid
, i
, j
, this->m_crGridColor
);
218 /* SetPixel(m_dcGrid, i, j, m_crGridColor); */
219 /* SetPixel(m_dcGrid, i, j, m_crGridColor); */
223 /* at this point we are done filling the grid bitmap, */
224 /* no more drawing to this bitmap is needed until the settings are changed */
226 /* if we don't have one yet, set up a memory dc for the plot */
227 if (this->m_dcPlot
== NULL
)
229 this->m_dcPlot
= CreateCompatibleDC(dc
);
230 this->m_bitmapPlot
= CreateCompatibleBitmap(dc
, this->m_nClientWidth
, this->m_nClientHeight
);
231 this->m_bitmapOldPlot
= SelectObject(this->m_dcPlot
, this->m_bitmapPlot
);
234 /* make sure the plot bitmap is cleared */
235 SetBkColor(this->m_dcPlot
, this->m_crBackColor
);
236 FillRect(this->m_dcPlot
, &this->m_rectClient
, this->m_brushBack
);
238 /* finally, force the plot area to redraw */
239 InvalidateRect(this->m_hParentWnd
, &this->m_rectClient
, TRUE
);
240 ReleaseDC(this->m_hParentWnd
, dc
);
243 void GraphCtrl_SetRange(TGraphCtrl
* this, double dLower
, double dUpper
, int nDecimalPlaces
)
245 /* ASSERT(dUpper > dLower); */
246 this->m_dLowerLimit
= dLower
;
247 this->m_dUpperLimit
= dUpper
;
248 this->m_nYDecimals
= nDecimalPlaces
;
249 this->m_dRange
= this->m_dUpperLimit
- this->m_dLowerLimit
;
250 this->m_dVerticalFactor
= (double)this->m_nPlotHeight
/ this->m_dRange
;
251 /* clear out the existing garbage, re-start with a clean plot */
252 GraphCtrl_InvalidateCtrl(this);
255 void GraphCtrl_SetGridColor(TGraphCtrl
* this, COLORREF color
)
257 this->m_crGridColor
= color
;
258 /* clear out the existing garbage, re-start with a clean plot */
259 GraphCtrl_InvalidateCtrl(this);
262 void GraphCtrl_SetPlotColor(TGraphCtrl
* this, int plot
, COLORREF color
)
264 this->m_crPlotColor
[plot
] = color
;
265 DeleteObject(this->m_penPlot
[plot
]);
266 this->m_penPlot
[plot
] = CreatePen(PS_SOLID
, 0, this->m_crPlotColor
[plot
]);
267 /* clear out the existing garbage, re-start with a clean plot */
268 GraphCtrl_InvalidateCtrl(this);
271 void GraphCtrl_SetBackgroundColor(TGraphCtrl
* this, COLORREF color
)
273 this->m_crBackColor
= color
;
274 DeleteObject(this->m_brushBack
);
275 this->m_brushBack
= CreateSolidBrush(this->m_crBackColor
);
276 /* clear out the existing garbage, re-start with a clean plot */
277 GraphCtrl_InvalidateCtrl(this);
280 static void GraphCtrl_DrawPoint(TGraphCtrl
* this)
282 /* this does the work of "scrolling" the plot to the left
283 * and appending a new data point all of the plotting is
284 * directed to the memory based bitmap associated with m_dcPlot
285 * the will subsequently be BitBlt'd to the client in Paint
287 int currX
, prevX
, currY
, prevY
;
292 if (this->m_dcPlot
!= NULL
)
294 /* shift the plot by BitBlt'ing it to itself
295 * note: the m_dcPlot covers the entire client
296 * but we only shift bitmap that is the size
297 * of the plot rectangle
298 * grab the right side of the plot (excluding m_nShiftPixels on the left)
299 * move this grabbed bitmap to the left by m_nShiftPixels
301 BitBlt(this->m_dcPlot
, this->m_rectPlot
.left
, this->m_rectPlot
.top
+1,
302 this->m_nPlotWidth
, this->m_nPlotHeight
, this->m_dcPlot
,
303 this->m_rectPlot
.left
+this->m_nShiftPixels
, this->m_rectPlot
.top
+1,
306 /* establish a rectangle over the right side of plot */
307 /* which now needs to be cleaned up prior to adding the new point */
308 rectCleanUp
= this->m_rectPlot
;
309 rectCleanUp
.left
= rectCleanUp
.right
- this->m_nShiftPixels
;
311 /* fill the cleanup area with the background */
312 FillRect(this->m_dcPlot
, &rectCleanUp
, this->m_brushBack
);
314 /* draw the next line segment */
315 for (i
= 0; i
< MAX_PLOTS
; i
++)
317 /* grab the plotting pen */
318 oldPen
= SelectObject(this->m_dcPlot
, this->m_penPlot
[i
]);
320 /* move to the previous point */
321 prevX
= this->m_rectPlot
.right
-this->m_nPlotShiftPixels
;
322 prevY
= this->m_rectPlot
.bottom
-
323 (int)((this->m_dPreviousPosition
[i
] - this->m_dLowerLimit
) * this->m_dVerticalFactor
);
324 MoveToEx(this->m_dcPlot
, prevX
, prevY
, NULL
);
326 /* draw to the current point */
327 currX
= this->m_rectPlot
.right
-this->m_nHalfShiftPixels
;
328 currY
= this->m_rectPlot
.bottom
-
329 (int)((this->m_dCurrentPosition
[i
] - this->m_dLowerLimit
) * this->m_dVerticalFactor
);
330 LineTo(this->m_dcPlot
, currX
, currY
);
332 /* Restore the pen */
333 SelectObject(this->m_dcPlot
, oldPen
);
335 /* if the data leaks over the upper or lower plot boundaries
336 * fill the upper and lower leakage with the background
337 * this will facilitate clipping on an as needed basis
338 * as opposed to always calling IntersectClipRect
340 if ((prevY
<= this->m_rectPlot
.top
) || (currY
<= this->m_rectPlot
.top
))
343 rc
.bottom
= this->m_rectPlot
.top
+1;
346 rc
.top
= this->m_rectClient
.top
;
347 FillRect(this->m_dcPlot
, &rc
, this->m_brushBack
);
349 if ((prevY
>= this->m_rectPlot
.bottom
) || (currY
>= this->m_rectPlot
.bottom
))
352 rc
.bottom
= this->m_rectClient
.bottom
+1;
355 rc
.top
= this->m_rectPlot
.bottom
+1;
356 /* RECT rc(prevX, m_rectPlot.bottom+1, currX+1, m_rectClient.bottom+1); */
357 FillRect(this->m_dcPlot
, &rc
, this->m_brushBack
);
360 /* store the current point for connection to the next point */
361 this->m_dPreviousPosition
[i
] = this->m_dCurrentPosition
[i
];
366 double GraphCtrl_AppendPoint(TGraphCtrl
* this,
367 double dNewPoint0
, double dNewPoint1
,
368 double dNewPoint2
, double dNewPoint3
)
370 /* append a data point to the plot & return the previous point */
373 dPrevious
= this->m_dCurrentPosition
[0];
374 this->m_dCurrentPosition
[0] = dNewPoint0
;
375 this->m_dCurrentPosition
[1] = dNewPoint1
;
376 this->m_dCurrentPosition
[2] = dNewPoint2
;
377 this->m_dCurrentPosition
[3] = dNewPoint3
;
378 GraphCtrl_DrawPoint(this);
383 static void GraphCtrl_Paint(TGraphCtrl
* this, HWND hWnd
, HDC dc
)
387 HBITMAP oldBitmap
; /* bitmap originally found in CMemDC */
389 /* no real plotting work is performed here, */
390 /* just putting the existing bitmaps on the client */
392 /* to avoid flicker, establish a memory dc, draw to it */
393 /* and then BitBlt it to the client */
394 memDC
= CreateCompatibleDC(dc
);
395 memBitmap
= CreateCompatibleBitmap(dc
, this->m_nClientWidth
, this->m_nClientHeight
);
396 oldBitmap
= SelectObject(memDC
, memBitmap
);
400 /* first drop the grid on the memory dc */
401 BitBlt(memDC
, 0, 0, this->m_nClientWidth
, this->m_nClientHeight
, this->m_dcGrid
, 0, 0, SRCCOPY
);
402 /* now add the plot on top as a "pattern" via SRCPAINT. */
403 /* works well with dark background and a light plot */
404 BitBlt(memDC
, 0, 0, this->m_nClientWidth
, this->m_nClientHeight
, this->m_dcPlot
, 0, 0, SRCPAINT
); /* SRCPAINT */
405 /* finally send the result to the display */
406 BitBlt(dc
, 0, 0, this->m_nClientWidth
, this->m_nClientHeight
, memDC
, 0, 0, SRCCOPY
);
408 SelectObject(memDC
, oldBitmap
);
409 DeleteObject(memBitmap
);
413 extern TGraphCtrl PerformancePageCpuUsageHistoryGraph
;
414 extern TGraphCtrl PerformancePageMemUsageHistoryGraph
;
415 extern HWND hPerformancePageCpuUsageHistoryGraph
;
416 extern HWND hPerformancePageMemUsageHistoryGraph
;
419 GraphCtrl_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
430 * Filter out mouse & keyboard messages
432 /* case WM_APPCOMMAND: */
433 case WM_CAPTURECHANGED
:
434 case WM_LBUTTONDBLCLK
:
437 case WM_MBUTTONDBLCLK
:
440 case WM_MOUSEACTIVATE
:
444 /* case WM_MOUSEWHEEL: */
446 case WM_NCLBUTTONDBLCLK
:
447 case WM_NCLBUTTONDOWN
:
449 case WM_NCMBUTTONDBLCLK
:
450 case WM_NCMBUTTONDOWN
:
452 /* case WM_NCMOUSEHOVER: */
453 /* case WM_NCMOUSELEAVE: */
455 case WM_NCRBUTTONDBLCLK
:
456 case WM_NCRBUTTONDOWN
:
458 /* case WM_NCXBUTTONDBLCLK: */
459 /* case WM_NCXBUTTONDOWN: */
460 /* case WM_NCXBUTTONUP: */
461 case WM_RBUTTONDBLCLK
:
464 /* case WM_XBUTTONDBLCLK: */
465 /* case WM_XBUTTONDOWN: */
466 /* case WM_XBUTTONUP: */
487 if (hWnd
== hPerformancePageMemUsageHistoryGraph
)
489 GraphCtrl_Resize(&PerformancePageMemUsageHistoryGraph
);
490 GraphCtrl_InvalidateCtrl(&PerformancePageMemUsageHistoryGraph
);
492 if (hWnd
== hPerformancePageCpuUsageHistoryGraph
)
494 GraphCtrl_Resize(&PerformancePageCpuUsageHistoryGraph
);
495 GraphCtrl_InvalidateCtrl(&PerformancePageCpuUsageHistoryGraph
);
500 hdc
= BeginPaint(hWnd
, &ps
);
501 GetClientRect(hWnd
, &rcClient
);
502 if (hWnd
== hPerformancePageMemUsageHistoryGraph
)
503 GraphCtrl_Paint(&PerformancePageMemUsageHistoryGraph
, hWnd
, hdc
);
504 if (hWnd
== hPerformancePageCpuUsageHistoryGraph
)
505 GraphCtrl_Paint(&PerformancePageCpuUsageHistoryGraph
, hWnd
, hdc
);
511 * We pass on all non-handled messages
513 return CallWindowProcW(OldGraphCtrlWndProc
, hWnd
, message
, wParam
, lParam
);