reactos/base/applications/taskmgr/graphctl.c
Joachim Henze 43aab86681 [0.4.10][TASKMGR] Mainly Graph-stuff backports 2023-10-31
This backports the following commits:
0.4.15-dev-6770-g ddd1d19b3f [TASKMGR] Get rid of cplusplus extern c (#5808)
partially 0.4.15-dev-6120-g 6e77747b30 [TASKMGR] Simplify tray icon code
0.4.15-dev-6114-g 9a6c85f68a [TASKMGR] Fix PerfDataDeallocCommandLineCache, part of CORE-18014 (unresolved still)
0.4.15-dev-6113-g 7989e3f26c [TASKMGR] GraphCtrl_Dispose: Delete hdcGraph first, part of CORE-18014 (unresolved still)
0.4.15-dev-6112-g dc14a9f6e0 [TASKMGR] GraphCtrl: Use DeleteDC instead of DeleteObject to delete inst->hdcGraph, part of CORE-18014 (unresolved still)
partially 0.4.15-dev-4994-g 15a0f7adb0 picked a tiny part from PR4657 (I picked only the refactoring to switch-statement, no functional changes. Especially NOT the formatting changes which would require the additional winnls.h-include)
0.4.15-dev-3737-g f8faa0b660 [TASKMGR] Fix a heap corruption bug (#4311), just an addendum to PR4141 which is picked with this backport as well. Older branches were never affected.
partially 0.4.15-dev-3514-g 1c82bf0324 [TASKMGR] Avoid freezing in getting icons, from (PR4180) CORE17894. I picked only the 1000ms->100ms part and the stripping of WM_QUERYDRAGICON call. Therefore I don't consider CORE17894 as fully covered.
0.4.15-dev-3486-g 545e1190f2 [TASKMGR] Avoid hangs as much as possible (#4166) CORE17894
partially 0.4.15-dev-3483-g 403222dd4f [TASKMGR] Preserve graphs history on resizes (#4141). I left aside the structs type renaming and OOM-Handling upon graph creation. Picked all the logical changes though.
0.4.15-dev-3269-g 0ed04e3640 [TASKMGR] Make performance graph grid scroll (#3581)
0.4.15-dev-3268-g a4ab9a1e19 [TASKMGR] Formatting only (#3581). Covers the last bits of that PR.

Main motivation was getting the toggling of ShowKernelTimes in the Performance tab
switch on and off in realtime without introducing gaps in the graph.
It also makes the grid scroll together with the data, like on Windows.

Most other parts I picked solely for their binary-shrinking effect.
I decided to strip the ID_HELP_TOPICS from the rc files, as this was not implemented,
and I would never port that back later. So it is one less non-functional-button in the older branches.
I favored memset() over Zeromemory() in this usermode-app everywhere, and favored for (;;) over while(1).

Binary size shrinks slightly on all branches:

master taskmgr.exe RosBEWin2.2.2 GCC8.4.0dbg             696.832 (0.4.15-dev-6820-gb3194e3)
0.4.14 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  549.888 -> 548.864
0.4.13 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  542.720 -> 542.208
0.4.12 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  545.792 -> 543.232
0.4.11 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  545.792 -> 543.232
0.4.10 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  532.480 -> 530.432
0.4. 9 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  532.480 -> 530.432
0.4. 8 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  532.480 -> 530.432
0.4. 7 taskmgr.exe RosBEWin2.1.6 GCC4.7.2dbg  531.456 -> 529.408

       taskmgr.exe MS XPSP3 german                                           140.800 Bytes
0.4.14 taskmgr.exe RosBEWin2.1.6 MSVC2010SP1rls I18N=de-DE  110.080 Bytes -> 109.056 Bytes (my current taskmgr of choice)
0.4. 8 taskmgr.exe RosBEWin2.1.6 MSVC2010SP1rls I18N=en-US  108.032 Bytes -> 105.984 Bytes
0.4. 7 taskmgr.exe RosBEWin2.1.6 MSVC2010SP1rls I18N=en-US  107.520 Bytes -> 105.472 Bytes
2023-10-31 21:01:57 +01:00

348 lines
9.7 KiB
C

/*
* PROJECT: ReactOS Task Manager
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
* COPYRIGHT: 2002 Robert Dickenson <robd@reactos.org>
*/
#include "precomp.h"
WNDPROC OldGraphCtrlWndProc;
void GraphCtrl_Create(PTGraphCtrl inst, HWND hWnd, HWND hParentWnd, PTFormat fmt)
{
HDC hdc, hdcg;
HBITMAP hbmOld;
UINT Size;
INT p;
RECT rc;
inst->hParentWnd = hParentWnd;
inst->hWnd = hWnd;
Size = GetSystemMetrics(SM_CXSCREEN);
inst->BitmapWidth = Size;
Size /= PLOT_SHIFT;
inst->PointBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Size * NUM_PLOTS);
if (!inst->PointBuffer)
goto fail;
inst->NumberOfPoints = Size;
inst->CurrIndex = 0;
inst->hPenGrid = CreatePen(PS_SOLID, 0, fmt->clrGrid);
inst->hPen0 = CreatePen(PS_SOLID, 0, fmt->clrPlot0);
inst->hPen1 = CreatePen(PS_SOLID, 0, fmt->clrPlot1);
inst->hBrushBack = CreateSolidBrush(fmt->clrBack);
if (!inst->hPenGrid || !inst->hPen0 || !inst->hPen1 || !inst->hBrushBack)
goto fail;
if (fmt->GridCellWidth >= PLOT_SHIFT << 2)
inst->GridCellWidth = fmt->GridCellWidth;
else
inst->GridCellWidth = PLOT_SHIFT << 2;
if (fmt->GridCellHeight >= PLOT_SHIFT << 2)
inst->GridCellHeight = fmt->GridCellHeight;
else
inst->GridCellHeight = PLOT_SHIFT << 2;
inst->DrawSecondaryPlot = fmt->DrawSecondaryPlot;
GetClientRect(hWnd, &rc);
inst->BitmapHeight = rc.bottom;
inst->ftPixelsPerPercent = (FLOAT)(inst->BitmapHeight) / 100.00f;
hdc = GetDC(hParentWnd);
hdcg = CreateCompatibleDC(hdc);
inst->hdcGraph = hdcg;
inst->hbmGraph = CreateCompatibleBitmap(hdc, inst->BitmapWidth, inst->BitmapHeight);
if (!hdc || !hdcg || !inst->hbmGraph)
goto fail;
ReleaseDC(hParentWnd, hdc);
hbmOld = (HBITMAP)SelectObject(hdcg, inst->hbmGraph);
DeleteObject(hbmOld);
SetBkColor(hdcg, fmt->clrBack);
rc.right = inst->BitmapWidth;
FillRect(hdcg, &rc, inst->hBrushBack);
inst->CurrShift = 0;
SelectObject(hdcg, inst->hPenGrid);
for (p = inst->GridCellHeight - 1; p < inst->BitmapHeight; p += inst->GridCellHeight)
{
MoveToEx(hdcg, 0, p, NULL);
LineTo(hdcg, inst->BitmapWidth, p);
}
for (p = inst->BitmapWidth - 1; p > 0; p -= inst->GridCellWidth)
{
MoveToEx(hdcg, p, 0, NULL);
LineTo(hdcg, p, inst->BitmapHeight);
}
SelectObject(hdcg, inst->hPen0);
return;
fail:
GraphCtrl_Dispose(inst);
}
void GraphCtrl_Dispose(PTGraphCtrl inst)
{
if (inst->PointBuffer)
HeapFree(GetProcessHeap(), 0, inst->PointBuffer);
if (inst->hdcGraph)
DeleteDC(inst->hdcGraph);
if (inst->hPenGrid)
DeleteObject(inst->hPenGrid);
if (inst->hPen0)
DeleteObject(inst->hPen0);
if (inst->hPen1)
DeleteObject(inst->hPen1);
if (inst->hBrushBack)
DeleteObject(inst->hBrushBack);
if (inst->hbmGraph)
DeleteObject(inst->hbmGraph);
}
void GraphCtrl_AddPoint(PTGraphCtrl inst, BYTE val0, BYTE val1)
{
HDC hdcg;
PBYTE t;
RECT rcDirt;
UINT Prev0, Prev1, RetainingWidth;
INT PrevY, CurrY, p, v;
hdcg = inst->hdcGraph;
RetainingWidth = inst->BitmapWidth - PLOT_SHIFT;
t = inst->PointBuffer;
Prev0 = *(t + inst->CurrIndex);
Prev1 = *(t + inst->CurrIndex + inst->NumberOfPoints);
if (inst->CurrIndex < inst->NumberOfPoints - 1)
inst->CurrIndex++;
else
inst->CurrIndex = 0;
*(t + inst->CurrIndex) = val0;
*(t + inst->CurrIndex + inst->NumberOfPoints) = val1;
// Drawing points, first shifting the plot left
BitBlt(hdcg, 0, 0, RetainingWidth, inst->BitmapHeight, hdcg, PLOT_SHIFT, 0, SRCCOPY);
rcDirt.left = RetainingWidth;
rcDirt.top = 0;
rcDirt.right = inst->BitmapWidth;
rcDirt.bottom = inst->BitmapHeight;
FillRect(hdcg, &rcDirt, inst->hBrushBack);
SelectObject(hdcg, inst->hPenGrid);
for (p = inst->GridCellHeight - 1; p < inst->BitmapHeight; p += inst->GridCellHeight)
{
MoveToEx(hdcg, RetainingWidth, p, NULL);
LineTo(hdcg, inst->BitmapWidth, p);
}
v = inst->CurrShift + PLOT_SHIFT;
if (v >= inst->GridCellWidth)
{
v -= inst->GridCellWidth;
p = inst->BitmapWidth - v - 1;
MoveToEx(hdcg, p, 0, NULL);
LineTo(hdcg, p, inst->BitmapHeight);
}
inst->CurrShift = v;
if (inst->DrawSecondaryPlot)
{
SelectObject(inst->hdcGraph, inst->hPen1);
PrevY = inst->BitmapHeight - Prev1 * inst->ftPixelsPerPercent;
MoveToEx(inst->hdcGraph, RetainingWidth - 1, PrevY, NULL);
CurrY = inst->BitmapHeight - val1 * inst->ftPixelsPerPercent;
LineTo(inst->hdcGraph, inst->BitmapWidth - 1, CurrY);
}
SelectObject(inst->hdcGraph, inst->hPen0);
PrevY = inst->BitmapHeight - Prev0 * inst->ftPixelsPerPercent;
MoveToEx(inst->hdcGraph, RetainingWidth - 1, PrevY, NULL);
CurrY = inst->BitmapHeight - val0 * inst->ftPixelsPerPercent;
LineTo(inst->hdcGraph, inst->BitmapWidth - 1, CurrY);
}
inline void GraphCtrl_RedrawBitmap(PTGraphCtrl inst, INT h)
{
HDC hdcg;
PBYTE t;
RECT rc;
INT i, j, y, x, p;
FLOAT coef;
hdcg = inst->hdcGraph;
rc.left = 0; rc.top = 0;
rc.right = inst->BitmapWidth; rc.bottom = h;
FillRect(hdcg, &rc, inst->hBrushBack);
SelectObject(hdcg, inst->hPenGrid);
for (p = inst->GridCellHeight - 1; p < inst->BitmapHeight; p += inst->GridCellHeight)
{
MoveToEx(hdcg, 0, p, NULL);
LineTo(hdcg, inst->BitmapWidth, p);
}
for (p = inst->BitmapWidth - inst->CurrShift - 1; p > 0; p -= inst->GridCellWidth)
{
MoveToEx(hdcg, p, 0, NULL);
LineTo(hdcg, p, inst->BitmapHeight);
}
coef = inst->ftPixelsPerPercent;
if (inst->DrawSecondaryPlot)
{
SelectObject(hdcg, inst->hPen1);
t = inst->PointBuffer + inst->NumberOfPoints;
x = inst->BitmapWidth - 1;
j = inst->CurrIndex;
y = h - *(t + j) * coef;
MoveToEx(hdcg, x, y, NULL);
for (i = 0; i < inst->NumberOfPoints; i++)
{
j = (j ? j : inst->NumberOfPoints) - 1;
y = h - *(t + j) * coef;
x -= PLOT_SHIFT;
LineTo(hdcg, x, y);
}
}
SelectObject(hdcg, inst->hPen0);
t = inst->PointBuffer;
x = inst->BitmapWidth - 1;
j = inst->CurrIndex;
y = h - *(t + j) * coef;
MoveToEx(hdcg, x, y, NULL);
for (i = 0; i < inst->NumberOfPoints; i++)
{
j = (j ? j : inst->NumberOfPoints) - 1;
y = h - *(t + j) * coef;
x -= PLOT_SHIFT;
LineTo(hdcg, x, y);
}
}
inline void GraphCtrl_RedrawOnHeightChange(PTGraphCtrl inst, INT nh)
{
HDC hdc;
HBITMAP hbmOld;
inst->BitmapHeight = nh;
inst->ftPixelsPerPercent = (FLOAT)nh / 100.00f;
hdc = GetDC(inst->hParentWnd);
hbmOld = inst->hbmGraph;
inst->hbmGraph = CreateCompatibleBitmap(hdc, inst->BitmapWidth, nh);
SelectObject(inst->hdcGraph, inst->hbmGraph);
DeleteObject(hbmOld);
ReleaseDC(inst->hParentWnd, hdc);
GraphCtrl_RedrawBitmap(inst, nh);
}
extern TGraphCtrl PerformancePageCpuUsageHistoryGraph;
extern TGraphCtrl PerformancePageMemUsageHistoryGraph;
extern HWND hPerformancePageCpuUsageHistoryGraph;
extern HWND hPerformancePageMemUsageHistoryGraph;
INT_PTR CALLBACK
GraphCtrl_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PTGraphCtrl graph;
RECT rcClient;
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_ERASEBKGND:
return TRUE;
case WM_CAPTURECHANGED:
case WM_LBUTTONDBLCLK:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MOUSEACTIVATE:
case WM_MOUSEHOVER:
case WM_MOUSELEAVE:
case WM_MOUSEMOVE:
case WM_NCHITTEST:
case WM_NCLBUTTONDBLCLK:
case WM_NCLBUTTONDOWN:
case WM_NCLBUTTONUP:
case WM_NCMBUTTONDBLCLK:
case WM_NCMBUTTONDOWN:
case WM_NCMBUTTONUP:
case WM_NCMOUSEMOVE:
case WM_NCRBUTTONDBLCLK:
case WM_NCRBUTTONDOWN:
case WM_NCRBUTTONUP:
case WM_RBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_ACTIVATE:
case WM_CHAR:
case WM_DEADCHAR:
case WM_GETHOTKEY:
case WM_HOTKEY:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_KILLFOCUS:
case WM_SETFOCUS:
case WM_SETHOTKEY:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_NCCALCSIZE:
return 0;
case WM_SIZE:
if (hWnd == hPerformancePageCpuUsageHistoryGraph)
graph = &PerformancePageCpuUsageHistoryGraph;
else if (hWnd == hPerformancePageMemUsageHistoryGraph)
graph = &PerformancePageMemUsageHistoryGraph;
else
return 0;
if (HIWORD(lParam) != graph->BitmapHeight)
GraphCtrl_RedrawOnHeightChange(graph, HIWORD(lParam));
InvalidateRect(hWnd, NULL, FALSE);
return 0;
case WM_PAINT:
if (hWnd == hPerformancePageCpuUsageHistoryGraph)
graph = &PerformancePageCpuUsageHistoryGraph;
else if (hWnd == hPerformancePageMemUsageHistoryGraph)
graph = &PerformancePageMemUsageHistoryGraph;
else
return 0;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rcClient);
BitBlt(hdc, 0, 0,
rcClient.right,
rcClient.bottom,
graph->hdcGraph,
graph->BitmapWidth - rcClient.right,
0,
SRCCOPY);
EndPaint(hWnd, &ps);
return 0;
}
return CallWindowProcW(OldGraphCtrlWndProc, hWnd, message, wParam, lParam);
}