Skip to content

Commit

Permalink
[MSPAINT] Improve Zoom tool (reactos#5781)
Browse files Browse the repository at this point in the history
- Delete CCanvasWindow::drawZoomFrame.
- Invalidate the canvas on mouse move when
  the active tool is Zoom tool.
- Add ZoomTool::OnDrawOverlayOnCanvas to
  draw the zoom rectangle without flickering.
- Improve the zoom trackbar position.
- Display the zoom rate on changing the value
  of the zoom trackbar.
- Reverse the direction of the zoom trackbar.
- Don't draw the focus rectangle.
CORE-19215, CORE-19216
  • Loading branch information
katahiromz authored Oct 10, 2023
1 parent 0c164f0 commit 62eeb15
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
23 changes: 3 additions & 20 deletions base/applications/mspaint/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ CCanvasWindow::~CCanvasWindow()
::DeleteObject(m_ahbmCached[1]);
}

VOID CCanvasWindow::drawZoomFrame(INT mouseX, INT mouseY)
{
// FIXME: Draw the border of the area that is to be zoomed in
CRect rc;
GetImageRect(rc);
ImageToCanvas(rc);

HDC hdc = GetDC();
DrawXorRect(hdc, &rc);
ReleaseDC(hdc);
}

RECT CCanvasWindow::GetBaseRect()
{
CRect rcBase;
Expand Down Expand Up @@ -376,6 +364,9 @@ LRESULT CCanvasWindow::OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
CanvasToImage(pt);

if (toolsModel.GetActiveTool() == TOOL_ZOOM)
Invalidate();

if (m_hitSelection != HIT_NONE)
{
SelectionDragging(pt);
Expand All @@ -384,14 +375,6 @@ LRESULT CCanvasWindow::OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL

if (!m_drawing || toolsModel.GetActiveTool() <= TOOL_AIRBRUSH)
{
if (toolsModel.GetActiveTool() == TOOL_ZOOM)
{
Invalidate(FALSE);
UpdateWindow();
CanvasToImage(pt);
drawZoomFrame(pt.x, pt.y);
}

TRACKMOUSEEVENT tme = { sizeof(tme) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = m_hWnd;
Expand Down
1 change: 0 additions & 1 deletion base/applications/mspaint/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class CCanvasWindow : public CWindowImpl<CCanvasWindow>
RECT GetBaseRect();
VOID DoDraw(HDC hDC, RECT& rcClient, RECT& rcPaint);
VOID OnHVScroll(WPARAM wParam, INT fnBar);
VOID drawZoomFrame(INT mouseX, INT mouseY);

HITTEST SelectionHitTest(POINT ptImage);
VOID StartSelectionDrag(HITTEST hit, POINT ptImage);
Expand Down
15 changes: 15 additions & 0 deletions base/applications/mspaint/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,21 @@ struct ZoomTool : ToolBase
{
}

void OnDrawOverlayOnCanvas(HDC hdc) override
{
CRect rc;
canvasWindow.GetImageRect(rc);
canvasWindow.ImageToCanvas(rc);

POINT pt;
::GetCursorPos(&pt);
::ScreenToClient(canvasWindow, &pt);

// FIXME: Draw the border of the area that is to be zoomed in
if (rc.PtInRect(pt))
DrawXorRect(hdc, &rc);
}

void OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick) override
{
if (bLeftButton)
Expand Down
47 changes: 37 additions & 10 deletions base/applications/mspaint/toolsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#define MARGIN1 3
#define MARGIN2 2

#define MAX_ZOOM_TRACK 6
#define MIN_ZOOM_TRACK 0
#define DEFAULT_ZOOM_TRACK 3

static const BYTE s_AirRadius[4] = { 5, 8, 3, 12 };

CToolSettingsWindow toolSettingsWindow;
Expand Down Expand Up @@ -285,10 +289,13 @@ LRESULT CToolSettingsWindow::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, W
m_hTranspIcon = (HICON)LoadImage(g_hinstExe, MAKEINTRESOURCE(IDI_TRANSPARENT),
IMAGE_ICON, CX_TRANS_ICON, CY_TRANS_ICON, LR_DEFAULTCOLOR);

RECT trackbarZoomPos = {1, 1, 1 + 40, 1 + 64};
RECT trackbarZoomPos, rect2;
calculateTwoBoxes(trackbarZoomPos, rect2);
::InflateRect(&trackbarZoomPos, -1, -1);

trackbarZoom.Create(TRACKBAR_CLASS, m_hWnd, trackbarZoomPos, NULL, WS_CHILD | TBS_VERT | TBS_AUTOTICKS);
trackbarZoom.SendMessage(TBM_SETRANGE, (WPARAM) TRUE, MAKELPARAM(0, 6));
trackbarZoom.SendMessage(TBM_SETPOS, (WPARAM) TRUE, (LPARAM) 3);
trackbarZoom.SendMessage(TBM_SETRANGE, TRUE, MAKELPARAM(MIN_ZOOM_TRACK, MAX_ZOOM_TRACK));
trackbarZoom.SendMessage(TBM_SETPOS, TRUE, DEFAULT_ZOOM_TRACK);
return 0;
}

Expand All @@ -301,9 +308,30 @@ LRESULT CToolSettingsWindow::OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam,

LRESULT CToolSettingsWindow::OnVScroll(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (!zoomTo(125 << trackbarZoom.SendMessage(TBM_GETPOS, 0, 0), 0, 0))
INT trackPos = MAX_ZOOM_TRACK - (INT)trackbarZoom.SendMessage(TBM_GETPOS, 0, 0);
zoomTo(MIN_ZOOM << trackPos, 0, 0);

INT zoomRate = toolsModel.GetZoom();

CString strZoom;
if (zoomRate % 10 == 0)
strZoom.Format(_T("%d%%"), zoomRate / 10);
else
strZoom.Format(_T("%d.%d%%"), zoomRate / 10, zoomRate % 10);

::SendMessage(g_hStatusBar, SB_SETTEXT, 1, (LPARAM)(LPCTSTR)strZoom);

OnToolsModelZoomChanged(nMsg, wParam, lParam, bHandled);
return 0;
}

LRESULT CToolSettingsWindow::OnNotify(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
NMHDR *pnmhdr = (NMHDR*)lParam;
if (pnmhdr->code == NM_CUSTOMDRAW)
{
OnToolsModelZoomChanged(nMsg, wParam, lParam, bHandled);
NMCUSTOMDRAW *pCustomDraw = (NMCUSTOMDRAW*)pnmhdr;
pCustomDraw->uItemState &= ~CDIS_FOCUS; // Do not draw the focus
}
return 0;
}
Expand All @@ -330,9 +358,7 @@ LRESULT CToolSettingsWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BO
PAINTSTRUCT ps;
HDC hdc = BeginPaint(&ps);

if (toolsModel.GetActiveTool() == TOOL_ZOOM)
::DrawEdge(hdc, &rect1, BDR_SUNKENOUTER, BF_RECT);
else
if (toolsModel.GetActiveTool() != TOOL_ZOOM)
::DrawEdge(hdc, &rect1, BDR_SUNKENOUTER, BF_RECT | BF_MIDDLE);

if (toolsModel.GetActiveTool() >= TOOL_RECT)
Expand Down Expand Up @@ -458,14 +484,15 @@ LRESULT CToolSettingsWindow::OnToolsModelSettingsChanged(UINT nMsg, WPARAM wPara

LRESULT CToolSettingsWindow::OnToolsModelZoomChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int tbPos = 0;
int tbPos = MIN_ZOOM_TRACK;
int tempZoom = toolsModel.GetZoom();

while (tempZoom > MIN_ZOOM)
{
tbPos++;
tempZoom = tempZoom >> 1;
}
trackbarZoom.SendMessage(TBM_SETPOS, (WPARAM) TRUE, (LPARAM) tbPos);

trackbarZoom.SendMessage(TBM_SETPOS, TRUE, MAX_ZOOM_TRACK - tbPos);
return 0;
}
2 changes: 2 additions & 0 deletions base/applications/mspaint/toolsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CToolSettingsWindow : public CWindowImpl<CToolSettingsWindow>
MESSAGE_HANDLER(WM_VSCROLL, OnVScroll)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
MESSAGE_HANDLER(WM_NOTIFY, OnNotify)
MESSAGE_HANDLER(WM_TOOLSMODELTOOLCHANGED, OnToolsModelToolChanged)
MESSAGE_HANDLER(WM_TOOLSMODELSETTINGSCHANGED, OnToolsModelSettingsChanged)
MESSAGE_HANDLER(WM_TOOLSMODELZOOMCHANGED, OnToolsModelZoomChanged)
Expand All @@ -43,6 +44,7 @@ class CToolSettingsWindow : public CWindowImpl<CToolSettingsWindow>
LRESULT OnVScroll(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnLButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnNotify(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnToolsModelToolChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnToolsModelSettingsChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnToolsModelZoomChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
Expand Down

0 comments on commit 62eeb15

Please sign in to comment.