17361592eSKatayama Hirofumi MZ /*
27361592eSKatayama Hirofumi MZ  * PROJECT:    PAINT for ReactOS
38f1f1c7aSStanislav Motylkov  * LICENSE:    LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
47361592eSKatayama Hirofumi MZ  * PURPOSE:    Providing the canvas window class
5f5200e6cSStanislav Motylkov  * COPYRIGHT:  Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
67361592eSKatayama Hirofumi MZ  */
77361592eSKatayama Hirofumi MZ 
87361592eSKatayama Hirofumi MZ #include "precomp.h"
97361592eSKatayama Hirofumi MZ 
1029e147beSKatayama Hirofumi MZ CCanvasWindow canvasWindow;
1129e147beSKatayama Hirofumi MZ 
127361592eSKatayama Hirofumi MZ /* FUNCTIONS ********************************************************/
137361592eSKatayama Hirofumi MZ 
CCanvasWindow()146b6a0457SKatayama Hirofumi MZ CCanvasWindow::CCanvasWindow()
15efe7368cSKatayama Hirofumi MZ     : m_drawing(FALSE)
16cb98e91bSKatayama Hirofumi MZ     , m_hitCanvasSizeBox(HIT_NONE)
17efe7368cSKatayama Hirofumi MZ     , m_ptOrig { -1, -1 }
186b6a0457SKatayama Hirofumi MZ {
19e8c7e300SKatayama Hirofumi MZ     m_ahbmCached[0] = m_ahbmCached[1] = NULL;
201aeebfdaSKatayama Hirofumi MZ     m_rcResizing.SetRectEmpty();
21efe7368cSKatayama Hirofumi MZ }
22efe7368cSKatayama Hirofumi MZ 
~CCanvasWindow()232f856f6fSKatayama Hirofumi MZ CCanvasWindow::~CCanvasWindow()
242f856f6fSKatayama Hirofumi MZ {
25e8c7e300SKatayama Hirofumi MZ     if (m_ahbmCached[0])
26e8c7e300SKatayama Hirofumi MZ         ::DeleteObject(m_ahbmCached[0]);
27e8c7e300SKatayama Hirofumi MZ     if (m_ahbmCached[1])
28e8c7e300SKatayama Hirofumi MZ         ::DeleteObject(m_ahbmCached[1]);
292f856f6fSKatayama Hirofumi MZ }
302f856f6fSKatayama Hirofumi MZ 
GetBaseRect()316b6a0457SKatayama Hirofumi MZ RECT CCanvasWindow::GetBaseRect()
326b6a0457SKatayama Hirofumi MZ {
33efe7368cSKatayama Hirofumi MZ     CRect rcBase;
34efe7368cSKatayama Hirofumi MZ     GetImageRect(rcBase);
35efe7368cSKatayama Hirofumi MZ     ImageToCanvas(rcBase);
361aeebfdaSKatayama Hirofumi MZ     rcBase.InflateRect(GRIP_SIZE, GRIP_SIZE);
376b6a0457SKatayama Hirofumi MZ     return rcBase;
386b6a0457SKatayama Hirofumi MZ }
396b6a0457SKatayama Hirofumi MZ 
ImageToCanvas(POINT & pt)40efe7368cSKatayama Hirofumi MZ VOID CCanvasWindow::ImageToCanvas(POINT& pt)
41efe7368cSKatayama Hirofumi MZ {
42e5792200SKatayama Hirofumi MZ     Zoomed(pt);
43efe7368cSKatayama Hirofumi MZ     pt.x += GRIP_SIZE - GetScrollPos(SB_HORZ);
44efe7368cSKatayama Hirofumi MZ     pt.y += GRIP_SIZE - GetScrollPos(SB_VERT);
45efe7368cSKatayama Hirofumi MZ }
46efe7368cSKatayama Hirofumi MZ 
ImageToCanvas(RECT & rc)47efe7368cSKatayama Hirofumi MZ VOID CCanvasWindow::ImageToCanvas(RECT& rc)
48efe7368cSKatayama Hirofumi MZ {
49e5792200SKatayama Hirofumi MZ     Zoomed(rc);
50efe7368cSKatayama Hirofumi MZ     ::OffsetRect(&rc, GRIP_SIZE - GetScrollPos(SB_HORZ), GRIP_SIZE - GetScrollPos(SB_VERT));
51efe7368cSKatayama Hirofumi MZ }
52efe7368cSKatayama Hirofumi MZ 
CanvasToImage(POINT & pt)5358c26d3fSKatayama Hirofumi MZ VOID CCanvasWindow::CanvasToImage(POINT& pt)
54efe7368cSKatayama Hirofumi MZ {
55efe7368cSKatayama Hirofumi MZ     pt.x -= GRIP_SIZE - GetScrollPos(SB_HORZ);
56efe7368cSKatayama Hirofumi MZ     pt.y -= GRIP_SIZE - GetScrollPos(SB_VERT);
57e5792200SKatayama Hirofumi MZ     UnZoomed(pt);
58efe7368cSKatayama Hirofumi MZ }
59efe7368cSKatayama Hirofumi MZ 
CanvasToImage(RECT & rc)6058c26d3fSKatayama Hirofumi MZ VOID CCanvasWindow::CanvasToImage(RECT& rc)
61efe7368cSKatayama Hirofumi MZ {
62efe7368cSKatayama Hirofumi MZ     ::OffsetRect(&rc, GetScrollPos(SB_HORZ) - GRIP_SIZE, GetScrollPos(SB_VERT) - GRIP_SIZE);
63e5792200SKatayama Hirofumi MZ     UnZoomed(rc);
64efe7368cSKatayama Hirofumi MZ }
65efe7368cSKatayama Hirofumi MZ 
GetImageRect(RECT & rc)66efe7368cSKatayama Hirofumi MZ VOID CCanvasWindow::GetImageRect(RECT& rc)
67efe7368cSKatayama Hirofumi MZ {
68e5792200SKatayama Hirofumi MZ     rc = { 0, 0, imageModel.GetWidth(), imageModel.GetHeight() };
69efe7368cSKatayama Hirofumi MZ }
70efe7368cSKatayama Hirofumi MZ 
CanvasHitTest(POINT pt)71cb98e91bSKatayama Hirofumi MZ HITTEST CCanvasWindow::CanvasHitTest(POINT pt)
726b6a0457SKatayama Hirofumi MZ {
73f6bd638bSKatayama Hirofumi MZ     if (selectionModel.m_bShow || ::IsWindowVisible(textEditWindow))
74f6bd638bSKatayama Hirofumi MZ         return HIT_INNER;
756b6a0457SKatayama Hirofumi MZ     RECT rcBase = GetBaseRect();
766b6a0457SKatayama Hirofumi MZ     return getSizeBoxHitTest(pt, &rcBase);
776b6a0457SKatayama Hirofumi MZ }
786b6a0457SKatayama Hirofumi MZ 
getNewZoomRect(CRect & rcView,INT newZoom,CPoint ptTarget)798f1eb03aSKatayama Hirofumi MZ VOID CCanvasWindow::getNewZoomRect(CRect& rcView, INT newZoom, CPoint ptTarget)
808f1eb03aSKatayama Hirofumi MZ {
818f1eb03aSKatayama Hirofumi MZ     CRect rcImage;
828f1eb03aSKatayama Hirofumi MZ     GetImageRect(rcImage);
838f1eb03aSKatayama Hirofumi MZ     ImageToCanvas(rcImage);
848f1eb03aSKatayama Hirofumi MZ 
858f1eb03aSKatayama Hirofumi MZ     // Calculate the zoom rectangle
868f1eb03aSKatayama Hirofumi MZ     INT oldZoom = toolsModel.GetZoom();
878f1eb03aSKatayama Hirofumi MZ     GetClientRect(rcView);
888f1eb03aSKatayama Hirofumi MZ     LONG cxView = rcView.right * oldZoom / newZoom, cyView = rcView.bottom * oldZoom / newZoom;
891aeebfdaSKatayama Hirofumi MZ     rcView.SetRect(ptTarget.x - cxView / 2, ptTarget.y - cyView / 2,
908f1eb03aSKatayama Hirofumi MZ                    ptTarget.x + cxView / 2, ptTarget.y + cyView / 2);
918f1eb03aSKatayama Hirofumi MZ 
928f1eb03aSKatayama Hirofumi MZ     // Shift the rectangle if necessary
938f1eb03aSKatayama Hirofumi MZ     INT dx = 0, dy = 0;
948f1eb03aSKatayama Hirofumi MZ     if (rcView.left < rcImage.left)
958f1eb03aSKatayama Hirofumi MZ         dx = rcImage.left - rcView.left;
968f1eb03aSKatayama Hirofumi MZ     else if (rcImage.right < rcView.right)
978f1eb03aSKatayama Hirofumi MZ         dx = rcImage.right - rcView.right;
988f1eb03aSKatayama Hirofumi MZ     if (rcView.top < rcImage.top)
998f1eb03aSKatayama Hirofumi MZ         dy = rcImage.top - rcView.top;
1008f1eb03aSKatayama Hirofumi MZ     else if (rcImage.bottom < rcView.bottom)
1018f1eb03aSKatayama Hirofumi MZ         dy = rcImage.bottom - rcView.bottom;
1028f1eb03aSKatayama Hirofumi MZ     rcView.OffsetRect(dx, dy);
1038f1eb03aSKatayama Hirofumi MZ 
1048f1eb03aSKatayama Hirofumi MZ     rcView.IntersectRect(&rcView, &rcImage);
1058f1eb03aSKatayama Hirofumi MZ }
1068f1eb03aSKatayama Hirofumi MZ 
zoomTo(INT newZoom,LONG left,LONG top)1078f1eb03aSKatayama Hirofumi MZ VOID CCanvasWindow::zoomTo(INT newZoom, LONG left, LONG top)
1088f1eb03aSKatayama Hirofumi MZ {
1098f1eb03aSKatayama Hirofumi MZ     POINT pt = { left, top };
1108f1eb03aSKatayama Hirofumi MZ     CanvasToImage(pt);
1118f1eb03aSKatayama Hirofumi MZ 
1128f1eb03aSKatayama Hirofumi MZ     toolsModel.SetZoom(newZoom);
1138f1eb03aSKatayama Hirofumi MZ     ImageToCanvas(pt);
1148f1eb03aSKatayama Hirofumi MZ     pt.x += GetScrollPos(SB_HORZ);
1158f1eb03aSKatayama Hirofumi MZ     pt.y += GetScrollPos(SB_VERT);
1168f1eb03aSKatayama Hirofumi MZ 
1178f1eb03aSKatayama Hirofumi MZ     updateScrollRange();
1188f1eb03aSKatayama Hirofumi MZ     updateScrollPos(pt.x, pt.y);
1198f1eb03aSKatayama Hirofumi MZ     Invalidate(TRUE);
1208f1eb03aSKatayama Hirofumi MZ }
1218f1eb03aSKatayama Hirofumi MZ 
DoDraw(HDC hDC,RECT & rcClient,RECT & rcPaint)1226b6a0457SKatayama Hirofumi MZ VOID CCanvasWindow::DoDraw(HDC hDC, RECT& rcClient, RECT& rcPaint)
1236b6a0457SKatayama Hirofumi MZ {
124fd1e1584SKatayama Hirofumi MZ     // This is the target area we have to draw on
125fd1e1584SKatayama Hirofumi MZ     CRect rcCanvasDraw;
126fd1e1584SKatayama Hirofumi MZ     rcCanvasDraw.IntersectRect(&rcClient, &rcPaint);
127fd1e1584SKatayama Hirofumi MZ 
1286b6a0457SKatayama Hirofumi MZ     // We use a memory bitmap to reduce flickering
129e8c7e300SKatayama Hirofumi MZ     HDC hdcMem0 = ::CreateCompatibleDC(hDC);
130e8c7e300SKatayama Hirofumi MZ     m_ahbmCached[0] = CachedBufferDIB(m_ahbmCached[0], rcClient.right, rcClient.bottom);
131e8c7e300SKatayama Hirofumi MZ     HGDIOBJ hbm0Old = ::SelectObject(hdcMem0, m_ahbmCached[0]);
1326b6a0457SKatayama Hirofumi MZ 
133e8c7e300SKatayama Hirofumi MZ     // Fill the background on hdcMem0
134fd1e1584SKatayama Hirofumi MZ     ::FillRect(hdcMem0, &rcCanvasDraw, (HBRUSH)(COLOR_APPWORKSPACE + 1));
1356b6a0457SKatayama Hirofumi MZ 
136efe7368cSKatayama Hirofumi MZ     // Draw the sizeboxes if necessary
1376b6a0457SKatayama Hirofumi MZ     RECT rcBase = GetBaseRect();
138f6bd638bSKatayama Hirofumi MZ     if (!selectionModel.m_bShow && !::IsWindowVisible(textEditWindow))
139fd1e1584SKatayama Hirofumi MZ         drawSizeBoxes(hdcMem0, &rcBase, FALSE, &rcCanvasDraw);
1406b6a0457SKatayama Hirofumi MZ 
141e8c7e300SKatayama Hirofumi MZ     // Calculate image size
142efe7368cSKatayama Hirofumi MZ     CRect rcImage;
143efe7368cSKatayama Hirofumi MZ     GetImageRect(rcImage);
144efe7368cSKatayama Hirofumi MZ     SIZE sizeImage = { imageModel.GetWidth(), imageModel.GetHeight() };
145efe7368cSKatayama Hirofumi MZ 
146fd1e1584SKatayama Hirofumi MZ     // Calculate the target area on the image
147fd1e1584SKatayama Hirofumi MZ     CRect rcImageDraw = rcCanvasDraw;
148fd1e1584SKatayama Hirofumi MZ     CanvasToImage(rcImageDraw);
149fd1e1584SKatayama Hirofumi MZ     rcImageDraw.IntersectRect(&rcImageDraw, &rcImage);
150fd1e1584SKatayama Hirofumi MZ 
151fd1e1584SKatayama Hirofumi MZ     // Consider rounding down by zooming
152fd1e1584SKatayama Hirofumi MZ     rcImageDraw.right += 1;
153fd1e1584SKatayama Hirofumi MZ     rcImageDraw.bottom += 1;
154fd1e1584SKatayama Hirofumi MZ 
155e8c7e300SKatayama Hirofumi MZ     // hdcMem1 <-- imageModel
156e8c7e300SKatayama Hirofumi MZ     HDC hdcMem1 = ::CreateCompatibleDC(hDC);
157e8c7e300SKatayama Hirofumi MZ     m_ahbmCached[1] = CachedBufferDIB(m_ahbmCached[1], sizeImage.cx, sizeImage.cy);
158e8c7e300SKatayama Hirofumi MZ     HGDIOBJ hbm1Old = ::SelectObject(hdcMem1, m_ahbmCached[1]);
159fd1e1584SKatayama Hirofumi MZ     ::BitBlt(hdcMem1, rcImageDraw.left, rcImageDraw.top, rcImageDraw.Width(), rcImageDraw.Height(),
160fd1e1584SKatayama Hirofumi MZ              imageModel.GetDC(), rcImageDraw.left, rcImageDraw.top, SRCCOPY);
161e8c7e300SKatayama Hirofumi MZ 
162e8c7e300SKatayama Hirofumi MZ     // Draw overlay #1 on hdcMem1
163e8c7e300SKatayama Hirofumi MZ     toolsModel.OnDrawOverlayOnImage(hdcMem1);
164e8c7e300SKatayama Hirofumi MZ 
165e8c7e300SKatayama Hirofumi MZ     // Transfer the bits with stretch (hdcMem0 <-- hdcMem1)
166e8c7e300SKatayama Hirofumi MZ     ImageToCanvas(rcImage);
167e8c7e300SKatayama Hirofumi MZ     ::StretchBlt(hdcMem0, rcImage.left, rcImage.top, rcImage.Width(), rcImage.Height(),
168e8c7e300SKatayama Hirofumi MZ                  hdcMem1, 0, 0, sizeImage.cx, sizeImage.cy, SRCCOPY);
169e8c7e300SKatayama Hirofumi MZ 
170e8c7e300SKatayama Hirofumi MZ     // Clean up hdcMem1
171e8c7e300SKatayama Hirofumi MZ     ::SelectObject(hdcMem1, hbm1Old);
172e8c7e300SKatayama Hirofumi MZ     ::DeleteDC(hdcMem1);
173e8c7e300SKatayama Hirofumi MZ 
174e8c7e300SKatayama Hirofumi MZ     // Draw the grid on hdcMem0
175e8c7e300SKatayama Hirofumi MZ     if (g_showGrid && toolsModel.GetZoom() >= 4000)
176efe7368cSKatayama Hirofumi MZ     {
177e8c7e300SKatayama Hirofumi MZ         HPEN oldPen = (HPEN) ::SelectObject(hdcMem0, ::CreatePen(PS_SOLID, 1, RGB(160, 160, 160)));
178efe7368cSKatayama Hirofumi MZ         for (INT counter = 0; counter < sizeImage.cy; counter++)
179efe7368cSKatayama Hirofumi MZ         {
180efe7368cSKatayama Hirofumi MZ             POINT pt0 = { 0, counter }, pt1 = { sizeImage.cx, counter };
181efe7368cSKatayama Hirofumi MZ             ImageToCanvas(pt0);
182efe7368cSKatayama Hirofumi MZ             ImageToCanvas(pt1);
183e8c7e300SKatayama Hirofumi MZ             ::MoveToEx(hdcMem0, pt0.x, pt0.y, NULL);
184e8c7e300SKatayama Hirofumi MZ             ::LineTo(hdcMem0, pt1.x, pt1.y);
185efe7368cSKatayama Hirofumi MZ         }
186efe7368cSKatayama Hirofumi MZ         for (INT counter = 0; counter < sizeImage.cx; counter++)
187efe7368cSKatayama Hirofumi MZ         {
188efe7368cSKatayama Hirofumi MZ             POINT pt0 = { counter, 0 }, pt1 = { counter, sizeImage.cy };
189efe7368cSKatayama Hirofumi MZ             ImageToCanvas(pt0);
190efe7368cSKatayama Hirofumi MZ             ImageToCanvas(pt1);
191e8c7e300SKatayama Hirofumi MZ             ::MoveToEx(hdcMem0, pt0.x, pt0.y, NULL);
192e8c7e300SKatayama Hirofumi MZ             ::LineTo(hdcMem0, pt1.x, pt1.y);
193efe7368cSKatayama Hirofumi MZ         }
194e8c7e300SKatayama Hirofumi MZ         ::DeleteObject(::SelectObject(hdcMem0, oldPen));
195efe7368cSKatayama Hirofumi MZ     }
196efe7368cSKatayama Hirofumi MZ 
197e8c7e300SKatayama Hirofumi MZ     // Draw overlay #2 on hdcMem0
198e8c7e300SKatayama Hirofumi MZ     toolsModel.OnDrawOverlayOnCanvas(hdcMem0);
199efe7368cSKatayama Hirofumi MZ 
200e8c7e300SKatayama Hirofumi MZ     // Draw new frame on hdcMem0 if any
2011aeebfdaSKatayama Hirofumi MZ     if (m_hitCanvasSizeBox != HIT_NONE && !m_rcResizing.IsRectEmpty())
202cb98e91bSKatayama Hirofumi MZ         DrawXorRect(hdcMem0, &m_rcResizing);
203efe7368cSKatayama Hirofumi MZ 
204e8c7e300SKatayama Hirofumi MZ     // Transfer the bits (hDC <-- hdcMem0)
205fd1e1584SKatayama Hirofumi MZ     ::BitBlt(hDC, rcCanvasDraw.left, rcCanvasDraw.top, rcCanvasDraw.Width(), rcCanvasDraw.Height(),
206fd1e1584SKatayama Hirofumi MZ              hdcMem0, rcCanvasDraw.left, rcCanvasDraw.top, SRCCOPY);
2076b6a0457SKatayama Hirofumi MZ 
208e8c7e300SKatayama Hirofumi MZ     // Clean up hdcMem0
209e8c7e300SKatayama Hirofumi MZ     ::SelectObject(hdcMem0, hbm0Old);
210e8c7e300SKatayama Hirofumi MZ     ::DeleteDC(hdcMem0);
2116b6a0457SKatayama Hirofumi MZ }
2126b6a0457SKatayama Hirofumi MZ 
updateScrollRange()2138f1eb03aSKatayama Hirofumi MZ VOID CCanvasWindow::updateScrollRange()
2147361592eSKatayama Hirofumi MZ {
2156b6a0457SKatayama Hirofumi MZ     CRect rcClient;
2166b6a0457SKatayama Hirofumi MZ     GetClientRect(&rcClient);
2177361592eSKatayama Hirofumi MZ 
2186b6a0457SKatayama Hirofumi MZ     CSize sizePage(rcClient.right, rcClient.bottom);
2197361592eSKatayama Hirofumi MZ     CSize sizeZoomed = { Zoomed(imageModel.GetWidth()), Zoomed(imageModel.GetHeight()) };
2207361592eSKatayama Hirofumi MZ     CSize sizeWhole = { sizeZoomed.cx + (GRIP_SIZE * 2), sizeZoomed.cy + (GRIP_SIZE * 2) };
2217361592eSKatayama Hirofumi MZ 
222efe7368cSKatayama Hirofumi MZ     // show/hide the scrollbars
2236b6a0457SKatayama Hirofumi MZ     ShowScrollBar(SB_HORZ, sizePage.cx < sizeWhole.cx);
2246b6a0457SKatayama Hirofumi MZ     ShowScrollBar(SB_VERT, sizePage.cy < sizeWhole.cy);
2257361592eSKatayama Hirofumi MZ 
2266b6a0457SKatayama Hirofumi MZ     if (sizePage.cx < sizeWhole.cx || sizePage.cy < sizeWhole.cy)
2277361592eSKatayama Hirofumi MZ     {
2286b6a0457SKatayama Hirofumi MZ         GetClientRect(&rcClient); // Scrollbars might change, get client rectangle again
2296b6a0457SKatayama Hirofumi MZ         sizePage = CSize(rcClient.right, rcClient.bottom);
2307361592eSKatayama Hirofumi MZ     }
2317361592eSKatayama Hirofumi MZ 
2327361592eSKatayama Hirofumi MZ     SCROLLINFO si = { sizeof(si), SIF_PAGE | SIF_RANGE };
2337361592eSKatayama Hirofumi MZ     si.nMin   = 0;
2347361592eSKatayama Hirofumi MZ 
2357361592eSKatayama Hirofumi MZ     si.nMax   = sizeWhole.cx;
2366b6a0457SKatayama Hirofumi MZ     si.nPage  = sizePage.cx;
237eacf003cSKatayama Hirofumi MZ     SetScrollInfo(SB_HORZ, &si);
2387361592eSKatayama Hirofumi MZ 
2397361592eSKatayama Hirofumi MZ     si.nMax   = sizeWhole.cy;
2406b6a0457SKatayama Hirofumi MZ     si.nPage  = sizePage.cy;
241eacf003cSKatayama Hirofumi MZ     SetScrollInfo(SB_VERT, &si);
2426b6a0457SKatayama Hirofumi MZ }
2437361592eSKatayama Hirofumi MZ 
updateScrollPos(INT x,INT y)2448f1eb03aSKatayama Hirofumi MZ VOID CCanvasWindow::updateScrollPos(INT x, INT y)
2451183e797SKatayama Hirofumi MZ {
2468f1eb03aSKatayama Hirofumi MZ     SetScrollPos(SB_HORZ, x);
2478f1eb03aSKatayama Hirofumi MZ     SetScrollPos(SB_VERT, y);
2481183e797SKatayama Hirofumi MZ }
2491183e797SKatayama Hirofumi MZ 
OnSize(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)2507361592eSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnSize(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
2517361592eSKatayama Hirofumi MZ {
2527361592eSKatayama Hirofumi MZ     if (m_hWnd)
2538f1eb03aSKatayama Hirofumi MZ         updateScrollRange();
2546b6a0457SKatayama Hirofumi MZ 
2557361592eSKatayama Hirofumi MZ     return 0;
2567361592eSKatayama Hirofumi MZ }
2577361592eSKatayama Hirofumi MZ 
OnHVScroll(WPARAM wParam,INT fnBar)2586b6a0457SKatayama Hirofumi MZ VOID CCanvasWindow::OnHVScroll(WPARAM wParam, INT fnBar)
2597361592eSKatayama Hirofumi MZ {
2607361592eSKatayama Hirofumi MZ     SCROLLINFO si;
2617361592eSKatayama Hirofumi MZ     si.cbSize = sizeof(SCROLLINFO);
2627361592eSKatayama Hirofumi MZ     si.fMask = SIF_ALL;
2636b6a0457SKatayama Hirofumi MZ     GetScrollInfo(fnBar, &si);
2647361592eSKatayama Hirofumi MZ     switch (LOWORD(wParam))
2657361592eSKatayama Hirofumi MZ     {
2667361592eSKatayama Hirofumi MZ         case SB_THUMBTRACK:
2677361592eSKatayama Hirofumi MZ         case SB_THUMBPOSITION:
26897e53c69SKatayama Hirofumi MZ             si.nPos = (SHORT)HIWORD(wParam);
2697361592eSKatayama Hirofumi MZ             break;
2707361592eSKatayama Hirofumi MZ         case SB_LINELEFT:
2717361592eSKatayama Hirofumi MZ             si.nPos -= 5;
2727361592eSKatayama Hirofumi MZ             break;
2737361592eSKatayama Hirofumi MZ         case SB_LINERIGHT:
2747361592eSKatayama Hirofumi MZ             si.nPos += 5;
2757361592eSKatayama Hirofumi MZ             break;
2767361592eSKatayama Hirofumi MZ         case SB_PAGELEFT:
2777361592eSKatayama Hirofumi MZ             si.nPos -= si.nPage;
2787361592eSKatayama Hirofumi MZ             break;
2797361592eSKatayama Hirofumi MZ         case SB_PAGERIGHT:
2807361592eSKatayama Hirofumi MZ             si.nPos += si.nPage;
2817361592eSKatayama Hirofumi MZ             break;
2827361592eSKatayama Hirofumi MZ     }
28397e53c69SKatayama Hirofumi MZ     si.nPos = max(min(si.nPos, si.nMax), si.nMin);
2846b6a0457SKatayama Hirofumi MZ     SetScrollInfo(fnBar, &si);
28597e53c69SKatayama Hirofumi MZ     Invalidate();
2866b6a0457SKatayama Hirofumi MZ }
2876b6a0457SKatayama Hirofumi MZ 
OnHScroll(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)2886b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnHScroll(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
2896b6a0457SKatayama Hirofumi MZ {
2906b6a0457SKatayama Hirofumi MZ     OnHVScroll(wParam, SB_HORZ);
2917361592eSKatayama Hirofumi MZ     return 0;
2927361592eSKatayama Hirofumi MZ }
2937361592eSKatayama Hirofumi MZ 
OnVScroll(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)2947361592eSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnVScroll(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
2957361592eSKatayama Hirofumi MZ {
2966b6a0457SKatayama Hirofumi MZ     OnHVScroll(wParam, SB_VERT);
2977361592eSKatayama Hirofumi MZ     return 0;
2987361592eSKatayama Hirofumi MZ }
2997361592eSKatayama Hirofumi MZ 
OnButtonDown(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)300f49919ceSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
3017361592eSKatayama Hirofumi MZ {
3026b6a0457SKatayama Hirofumi MZ     POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
3036b6a0457SKatayama Hirofumi MZ 
304f49919ceSKatayama Hirofumi MZ     m_nMouseDownMsg = nMsg;
30597e53c69SKatayama Hirofumi MZ     BOOL bLeftButton = (nMsg == WM_LBUTTONDOWN);
30697e53c69SKatayama Hirofumi MZ 
30797e53c69SKatayama Hirofumi MZ     if (nMsg == WM_MBUTTONDOWN)
30897e53c69SKatayama Hirofumi MZ     {
30997e53c69SKatayama Hirofumi MZ         m_ptOrig = pt;
31097e53c69SKatayama Hirofumi MZ         SetCapture();
311640d67d1SKatayama Hirofumi MZ         ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_HANDDRAG)));
31297e53c69SKatayama Hirofumi MZ         return 0;
31397e53c69SKatayama Hirofumi MZ     }
314f49919ceSKatayama Hirofumi MZ 
315c2bb5aa0SKatayama Hirofumi MZ     HITTEST hitSelection = selectionModel.hitTest(pt);
316efe7368cSKatayama Hirofumi MZ     if (hitSelection != HIT_NONE)
317efe7368cSKatayama Hirofumi MZ     {
318f710e5a2SKatayama Hirofumi MZ         m_drawing = TRUE;
319cb245ff7SKatayama Hirofumi MZ         CanvasToImage(pt);
320f710e5a2SKatayama Hirofumi MZ         SetCapture();
321f710e5a2SKatayama Hirofumi MZ         toolsModel.OnButtonDown(bLeftButton, pt.x, pt.y, FALSE);
322f710e5a2SKatayama Hirofumi MZ         Invalidate();
323efe7368cSKatayama Hirofumi MZ         return 0;
324efe7368cSKatayama Hirofumi MZ     }
325efe7368cSKatayama Hirofumi MZ 
326cb98e91bSKatayama Hirofumi MZ     HITTEST hit = CanvasHitTest(pt);
3276b6a0457SKatayama Hirofumi MZ     if (hit == HIT_NONE || hit == HIT_BORDER)
3287361592eSKatayama Hirofumi MZ     {
329aac89519SKatayama Hirofumi MZ         switch (toolsModel.GetActiveTool())
3306b6a0457SKatayama Hirofumi MZ         {
331aac89519SKatayama Hirofumi MZ             case TOOL_BEZIER:
332aac89519SKatayama Hirofumi MZ             case TOOL_SHAPE:
33337f56d24SKatayama Hirofumi MZ                 toolsModel.OnEndDraw(TRUE);
334f49919ceSKatayama Hirofumi MZ                 Invalidate();
335aac89519SKatayama Hirofumi MZ                 break;
336aac89519SKatayama Hirofumi MZ 
337aac89519SKatayama Hirofumi MZ             case TOOL_FREESEL:
338aac89519SKatayama Hirofumi MZ             case TOOL_RECTSEL:
33937f56d24SKatayama Hirofumi MZ                 toolsModel.OnEndDraw(FALSE);
340f49919ceSKatayama Hirofumi MZ                 Invalidate();
341aac89519SKatayama Hirofumi MZ                 break;
342aac89519SKatayama Hirofumi MZ 
343aac89519SKatayama Hirofumi MZ             default:
344aac89519SKatayama Hirofumi MZ                 break;
3457361592eSKatayama Hirofumi MZ         }
3467361592eSKatayama Hirofumi MZ 
3477361592eSKatayama Hirofumi MZ         toolsModel.resetTool(); // resets the point-buffer of the polygon and bezier functions
3487361592eSKatayama Hirofumi MZ         return 0;
3497361592eSKatayama Hirofumi MZ     }
3507361592eSKatayama Hirofumi MZ 
35158c26d3fSKatayama Hirofumi MZ     CanvasToImage(pt);
352efe7368cSKatayama Hirofumi MZ 
3536b6a0457SKatayama Hirofumi MZ     if (hit == HIT_INNER)
3546b6a0457SKatayama Hirofumi MZ     {
355efe7368cSKatayama Hirofumi MZ         m_drawing = TRUE;
356efe7368cSKatayama Hirofumi MZ         SetCapture();
357efe7368cSKatayama Hirofumi MZ         toolsModel.OnButtonDown(bLeftButton, pt.x, pt.y, FALSE);
358f710e5a2SKatayama Hirofumi MZ         Invalidate();
3596b6a0457SKatayama Hirofumi MZ         return 0;
3606b6a0457SKatayama Hirofumi MZ     }
3616b6a0457SKatayama Hirofumi MZ 
362efe7368cSKatayama Hirofumi MZ     if (bLeftButton)
363efe7368cSKatayama Hirofumi MZ     {
364cb98e91bSKatayama Hirofumi MZ         m_hitCanvasSizeBox = hit;
3656b6a0457SKatayama Hirofumi MZ         m_ptOrig = pt;
3666b6a0457SKatayama Hirofumi MZ         SetCapture();
367efe7368cSKatayama Hirofumi MZ     }
36858c26d3fSKatayama Hirofumi MZ 
3696b6a0457SKatayama Hirofumi MZ     return 0;
3706b6a0457SKatayama Hirofumi MZ }
3716b6a0457SKatayama Hirofumi MZ 
OnButtonDblClk(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)372f49919ceSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnButtonDblClk(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
373efe7368cSKatayama Hirofumi MZ {
374efe7368cSKatayama Hirofumi MZ     POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
375efe7368cSKatayama Hirofumi MZ     CanvasToImage(pt);
376efe7368cSKatayama Hirofumi MZ 
377efe7368cSKatayama Hirofumi MZ     m_drawing = FALSE;
378f49919ceSKatayama Hirofumi MZ     ::ReleaseCapture();
379f49919ceSKatayama Hirofumi MZ     m_nMouseDownMsg = 0;
380efe7368cSKatayama Hirofumi MZ 
381f49919ceSKatayama Hirofumi MZ     toolsModel.OnButtonDown(nMsg == WM_LBUTTONDBLCLK, pt.x, pt.y, TRUE);
382efe7368cSKatayama Hirofumi MZ     toolsModel.resetTool();
383f710e5a2SKatayama Hirofumi MZ     Invalidate();
384efe7368cSKatayama Hirofumi MZ     return 0;
385efe7368cSKatayama Hirofumi MZ }
386efe7368cSKatayama Hirofumi MZ 
OnMouseMove(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)3876b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
3886b6a0457SKatayama Hirofumi MZ {
389efe7368cSKatayama Hirofumi MZ     POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
39097e53c69SKatayama Hirofumi MZ 
39197e53c69SKatayama Hirofumi MZ     if (m_nMouseDownMsg == WM_MBUTTONDOWN)
39297e53c69SKatayama Hirofumi MZ     {
39397e53c69SKatayama Hirofumi MZ         INT x = GetScrollPos(SB_HORZ) - (pt.x - m_ptOrig.x);
39497e53c69SKatayama Hirofumi MZ         INT y = GetScrollPos(SB_VERT) - (pt.y - m_ptOrig.y);
39597e53c69SKatayama Hirofumi MZ         SendMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, x), 0);
39697e53c69SKatayama Hirofumi MZ         SendMessage(WM_VSCROLL, MAKEWPARAM(SB_THUMBPOSITION, y), 0);
39797e53c69SKatayama Hirofumi MZ         m_ptOrig = pt;
39897e53c69SKatayama Hirofumi MZ         return 0;
39997e53c69SKatayama Hirofumi MZ     }
40097e53c69SKatayama Hirofumi MZ 
401efe7368cSKatayama Hirofumi MZ     CanvasToImage(pt);
402efe7368cSKatayama Hirofumi MZ 
40362eeb158SKatayama Hirofumi MZ     if (toolsModel.GetActiveTool() == TOOL_ZOOM)
40462eeb158SKatayama Hirofumi MZ         Invalidate();
40562eeb158SKatayama Hirofumi MZ 
406efe7368cSKatayama Hirofumi MZ     if (!m_drawing || toolsModel.GetActiveTool() <= TOOL_AIRBRUSH)
407efe7368cSKatayama Hirofumi MZ     {
408efe7368cSKatayama Hirofumi MZ         TRACKMOUSEEVENT tme = { sizeof(tme) };
409efe7368cSKatayama Hirofumi MZ         tme.dwFlags = TME_LEAVE;
410efe7368cSKatayama Hirofumi MZ         tme.hwndTrack = m_hWnd;
411efe7368cSKatayama Hirofumi MZ         tme.dwHoverTime = 0;
412efe7368cSKatayama Hirofumi MZ         ::TrackMouseEvent(&tme);
413efe7368cSKatayama Hirofumi MZ 
414efe7368cSKatayama Hirofumi MZ         if (!m_drawing)
415efe7368cSKatayama Hirofumi MZ         {
4161aeebfdaSKatayama Hirofumi MZ             CRect rcImage;
4170c164f08SKatayama Hirofumi MZ             GetImageRect(rcImage);
4180c164f08SKatayama Hirofumi MZ 
419640d67d1SKatayama Hirofumi MZ             CStringW strCoord;
4201aeebfdaSKatayama Hirofumi MZ             if (rcImage.PtInRect(pt))
421640d67d1SKatayama Hirofumi MZ                 strCoord.Format(L"%ld, %ld", pt.x, pt.y);
422640d67d1SKatayama Hirofumi MZ             ::SendMessageW(g_hStatusBar, SB_SETTEXT, 1, (LPARAM)(LPCWSTR)strCoord);
423efe7368cSKatayama Hirofumi MZ         }
424efe7368cSKatayama Hirofumi MZ     }
425efe7368cSKatayama Hirofumi MZ 
426f710e5a2SKatayama Hirofumi MZ     if (m_drawing || toolsModel.IsSelection())
427efe7368cSKatayama Hirofumi MZ     {
428a114169cSKatayama Hirofumi MZ         toolsModel.DrawWithMouseTool(pt, wParam);
429efe7368cSKatayama Hirofumi MZ         return 0;
430efe7368cSKatayama Hirofumi MZ     }
431efe7368cSKatayama Hirofumi MZ 
432cb98e91bSKatayama Hirofumi MZ     if (m_hitCanvasSizeBox == HIT_NONE || ::GetCapture() != m_hWnd)
4336b6a0457SKatayama Hirofumi MZ         return 0;
4346b6a0457SKatayama Hirofumi MZ 
4356b6a0457SKatayama Hirofumi MZ     // Dragging now... Calculate the new size
4366b6a0457SKatayama Hirofumi MZ     INT cxImage = imageModel.GetWidth(), cyImage = imageModel.GetHeight();
437efe7368cSKatayama Hirofumi MZ     INT cxDelta = pt.x - m_ptOrig.x;
438efe7368cSKatayama Hirofumi MZ     INT cyDelta = pt.y - m_ptOrig.y;
439cb98e91bSKatayama Hirofumi MZ     switch (m_hitCanvasSizeBox)
4406b6a0457SKatayama Hirofumi MZ     {
4416b6a0457SKatayama Hirofumi MZ         case HIT_UPPER_LEFT:
4426b6a0457SKatayama Hirofumi MZ             cxImage -= cxDelta;
4436b6a0457SKatayama Hirofumi MZ             cyImage -= cyDelta;
4446b6a0457SKatayama Hirofumi MZ             break;
4456b6a0457SKatayama Hirofumi MZ         case HIT_UPPER_CENTER:
4466b6a0457SKatayama Hirofumi MZ             cyImage -= cyDelta;
4476b6a0457SKatayama Hirofumi MZ             break;
4486b6a0457SKatayama Hirofumi MZ         case HIT_UPPER_RIGHT:
4496b6a0457SKatayama Hirofumi MZ             cxImage += cxDelta;
4506b6a0457SKatayama Hirofumi MZ             cyImage -= cyDelta;
4516b6a0457SKatayama Hirofumi MZ             break;
4526b6a0457SKatayama Hirofumi MZ         case HIT_MIDDLE_LEFT:
4536b6a0457SKatayama Hirofumi MZ             cxImage -= cxDelta;
4546b6a0457SKatayama Hirofumi MZ             break;
4556b6a0457SKatayama Hirofumi MZ         case HIT_MIDDLE_RIGHT:
4566b6a0457SKatayama Hirofumi MZ             cxImage += cxDelta;
4576b6a0457SKatayama Hirofumi MZ             break;
4586b6a0457SKatayama Hirofumi MZ         case HIT_LOWER_LEFT:
4596b6a0457SKatayama Hirofumi MZ             cxImage -= cxDelta;
4606b6a0457SKatayama Hirofumi MZ             cyImage += cyDelta;
4616b6a0457SKatayama Hirofumi MZ             break;
4626b6a0457SKatayama Hirofumi MZ         case HIT_LOWER_CENTER:
4636b6a0457SKatayama Hirofumi MZ             cyImage += cyDelta;
4646b6a0457SKatayama Hirofumi MZ             break;
4656b6a0457SKatayama Hirofumi MZ         case HIT_LOWER_RIGHT:
4666b6a0457SKatayama Hirofumi MZ             cxImage += cxDelta;
4676b6a0457SKatayama Hirofumi MZ             cyImage += cyDelta;
4686b6a0457SKatayama Hirofumi MZ             break;
4696b6a0457SKatayama Hirofumi MZ         default:
4706b6a0457SKatayama Hirofumi MZ             return 0;
4716b6a0457SKatayama Hirofumi MZ     }
4726b6a0457SKatayama Hirofumi MZ 
4736b6a0457SKatayama Hirofumi MZ     // Limit bitmap size
4746b6a0457SKatayama Hirofumi MZ     cxImage = max(1, cxImage);
4756b6a0457SKatayama Hirofumi MZ     cyImage = max(1, cyImage);
4766b6a0457SKatayama Hirofumi MZ     cxImage = min(MAXWORD, cxImage);
4776b6a0457SKatayama Hirofumi MZ     cyImage = min(MAXWORD, cyImage);
4786b6a0457SKatayama Hirofumi MZ 
4796b6a0457SKatayama Hirofumi MZ     // Display new size
480640d67d1SKatayama Hirofumi MZ     CStringW strSize;
481640d67d1SKatayama Hirofumi MZ     strSize.Format(L"%d x %d", cxImage, cyImage);
482640d67d1SKatayama Hirofumi MZ     ::SendMessageW(g_hStatusBar, SB_SETTEXT, 2, (LPARAM)(LPCWSTR)strSize);
4836b6a0457SKatayama Hirofumi MZ 
484cb98e91bSKatayama Hirofumi MZ     // Dragging now... Fix the position...
485cb98e91bSKatayama Hirofumi MZ     CRect rcResizing = { 0, 0, cxImage, cyImage };
486cb98e91bSKatayama Hirofumi MZ     switch (m_hitCanvasSizeBox)
487efe7368cSKatayama Hirofumi MZ     {
488efe7368cSKatayama Hirofumi MZ         case HIT_UPPER_LEFT:
4891aeebfdaSKatayama Hirofumi MZ             rcResizing.OffsetRect(cxDelta, cyDelta);
490efe7368cSKatayama Hirofumi MZ             break;
491efe7368cSKatayama Hirofumi MZ         case HIT_UPPER_CENTER:
4921aeebfdaSKatayama Hirofumi MZ             rcResizing.OffsetRect(0, cyDelta);
493efe7368cSKatayama Hirofumi MZ             break;
494efe7368cSKatayama Hirofumi MZ         case HIT_UPPER_RIGHT:
4951aeebfdaSKatayama Hirofumi MZ             rcResizing.OffsetRect(0, cyDelta);
496efe7368cSKatayama Hirofumi MZ             break;
497efe7368cSKatayama Hirofumi MZ         case HIT_MIDDLE_LEFT:
4981aeebfdaSKatayama Hirofumi MZ             rcResizing.OffsetRect(cxDelta, 0);
499efe7368cSKatayama Hirofumi MZ             break;
500efe7368cSKatayama Hirofumi MZ         case HIT_LOWER_LEFT:
5011aeebfdaSKatayama Hirofumi MZ             rcResizing.OffsetRect(cxDelta, 0);
502efe7368cSKatayama Hirofumi MZ             break;
503efe7368cSKatayama Hirofumi MZ         default:
504efe7368cSKatayama Hirofumi MZ             break;
505efe7368cSKatayama Hirofumi MZ     }
506cb98e91bSKatayama Hirofumi MZ     ImageToCanvas(rcResizing);
507cb98e91bSKatayama Hirofumi MZ     m_rcResizing = rcResizing;
508efe7368cSKatayama Hirofumi MZ     Invalidate(TRUE);
509efe7368cSKatayama Hirofumi MZ 
5106b6a0457SKatayama Hirofumi MZ     return 0;
5116b6a0457SKatayama Hirofumi MZ }
5126b6a0457SKatayama Hirofumi MZ 
OnButtonUp(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)513f49919ceSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnButtonUp(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
5146b6a0457SKatayama Hirofumi MZ {
515efe7368cSKatayama Hirofumi MZ     POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
516efe7368cSKatayama Hirofumi MZ     CanvasToImage(pt);
517efe7368cSKatayama Hirofumi MZ 
5186b6a0457SKatayama Hirofumi MZ     ::ReleaseCapture();
5196b6a0457SKatayama Hirofumi MZ 
520f49919ceSKatayama Hirofumi MZ     BOOL bLeftButton = (m_nMouseDownMsg == WM_LBUTTONDOWN);
521f49919ceSKatayama Hirofumi MZ     m_nMouseDownMsg = 0;
522f49919ceSKatayama Hirofumi MZ 
523efe7368cSKatayama Hirofumi MZ     if (m_drawing)
524efe7368cSKatayama Hirofumi MZ     {
525efe7368cSKatayama Hirofumi MZ         m_drawing = FALSE;
526efe7368cSKatayama Hirofumi MZ         toolsModel.OnButtonUp(bLeftButton, pt.x, pt.y);
527efe7368cSKatayama Hirofumi MZ         Invalidate(FALSE);
528640d67d1SKatayama Hirofumi MZ         ::SendMessageW(g_hStatusBar, SB_SETTEXT, 2, (LPARAM)L"");
529efe7368cSKatayama Hirofumi MZ         return 0;
530efe7368cSKatayama Hirofumi MZ     }
531efe7368cSKatayama Hirofumi MZ 
532cb98e91bSKatayama Hirofumi MZ     if (m_hitCanvasSizeBox == HIT_NONE || !bLeftButton)
5336b6a0457SKatayama Hirofumi MZ         return 0;
5346b6a0457SKatayama Hirofumi MZ 
5356b6a0457SKatayama Hirofumi MZ     // Resize the image
5366b6a0457SKatayama Hirofumi MZ     INT cxImage = imageModel.GetWidth(), cyImage = imageModel.GetHeight();
537efe7368cSKatayama Hirofumi MZ     INT cxDelta = pt.x - m_ptOrig.x;
538efe7368cSKatayama Hirofumi MZ     INT cyDelta = pt.y - m_ptOrig.y;
539cb98e91bSKatayama Hirofumi MZ     switch (m_hitCanvasSizeBox)
5406b6a0457SKatayama Hirofumi MZ     {
5416b6a0457SKatayama Hirofumi MZ         case HIT_UPPER_LEFT:
5426b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage - cxDelta, cyImage - cyDelta, cxDelta, cyDelta);
5436b6a0457SKatayama Hirofumi MZ             break;
5446b6a0457SKatayama Hirofumi MZ         case HIT_UPPER_CENTER:
5456b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage, cyImage - cyDelta, 0, cyDelta);
5466b6a0457SKatayama Hirofumi MZ             break;
5476b6a0457SKatayama Hirofumi MZ         case HIT_UPPER_RIGHT:
5486b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage + cxDelta, cyImage - cyDelta, 0, cyDelta);
5496b6a0457SKatayama Hirofumi MZ             break;
5506b6a0457SKatayama Hirofumi MZ         case HIT_MIDDLE_LEFT:
5516b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage - cxDelta, cyImage, cxDelta, 0);
5526b6a0457SKatayama Hirofumi MZ             break;
5536b6a0457SKatayama Hirofumi MZ         case HIT_MIDDLE_RIGHT:
5546b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage + cxDelta, cyImage, 0, 0);
5556b6a0457SKatayama Hirofumi MZ             break;
5566b6a0457SKatayama Hirofumi MZ         case HIT_LOWER_LEFT:
5576b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage - cxDelta, cyImage + cyDelta, cxDelta, 0);
5586b6a0457SKatayama Hirofumi MZ             break;
5596b6a0457SKatayama Hirofumi MZ         case HIT_LOWER_CENTER:
5606b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage, cyImage + cyDelta, 0, 0);
5616b6a0457SKatayama Hirofumi MZ             break;
5626b6a0457SKatayama Hirofumi MZ         case HIT_LOWER_RIGHT:
5636b6a0457SKatayama Hirofumi MZ             imageModel.Crop(cxImage + cxDelta, cyImage + cyDelta, 0, 0);
5646b6a0457SKatayama Hirofumi MZ             break;
5656b6a0457SKatayama Hirofumi MZ         default:
5666b6a0457SKatayama Hirofumi MZ             break;
5676b6a0457SKatayama Hirofumi MZ     }
5681aeebfdaSKatayama Hirofumi MZ     m_rcResizing.SetRectEmpty();
5696b6a0457SKatayama Hirofumi MZ 
570e8c7e300SKatayama Hirofumi MZ     g_imageSaved = FALSE;
571132f482dSKatayama Hirofumi MZ 
572cb98e91bSKatayama Hirofumi MZ     m_hitCanvasSizeBox = HIT_NONE;
5736b6a0457SKatayama Hirofumi MZ     toolsModel.resetTool(); // resets the point-buffer of the polygon and bezier functions
5748f1eb03aSKatayama Hirofumi MZ     updateScrollRange();
5756b6a0457SKatayama Hirofumi MZ     Invalidate(TRUE);
5766b6a0457SKatayama Hirofumi MZ     return 0;
5776b6a0457SKatayama Hirofumi MZ }
5786b6a0457SKatayama Hirofumi MZ 
OnSetCursor(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)5796b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnSetCursor(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
5806b6a0457SKatayama Hirofumi MZ {
581993a4502SKatayama Hirofumi MZ     if (CWaitCursor::IsWaiting())
582993a4502SKatayama Hirofumi MZ     {
583993a4502SKatayama Hirofumi MZ         bHandled = FALSE;
584993a4502SKatayama Hirofumi MZ         return 0;
585993a4502SKatayama Hirofumi MZ     }
586993a4502SKatayama Hirofumi MZ 
58797e53c69SKatayama Hirofumi MZ     if (m_nMouseDownMsg == WM_MBUTTONDOWN)
58897e53c69SKatayama Hirofumi MZ     {
589640d67d1SKatayama Hirofumi MZ         ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_HANDDRAG)));
59097e53c69SKatayama Hirofumi MZ         return 0;
59197e53c69SKatayama Hirofumi MZ     }
59297e53c69SKatayama Hirofumi MZ 
5936b6a0457SKatayama Hirofumi MZ     POINT pt;
5946b6a0457SKatayama Hirofumi MZ     ::GetCursorPos(&pt);
5956b6a0457SKatayama Hirofumi MZ     ScreenToClient(&pt);
5966b6a0457SKatayama Hirofumi MZ 
5979ff9580fSJose Carlos Jesus     CRect rcClient;
5989ff9580fSJose Carlos Jesus     GetClientRect(&rcClient);
5999ff9580fSJose Carlos Jesus 
6001aeebfdaSKatayama Hirofumi MZ     if (!rcClient.PtInRect(pt))
6019ff9580fSJose Carlos Jesus     {
6029ff9580fSJose Carlos Jesus         bHandled = FALSE;
6039ff9580fSJose Carlos Jesus         return 0;
6049ff9580fSJose Carlos Jesus     }
6059ff9580fSJose Carlos Jesus 
606c2bb5aa0SKatayama Hirofumi MZ     HITTEST hitSelection = selectionModel.hitTest(pt);
607efe7368cSKatayama Hirofumi MZ     if (hitSelection != HIT_NONE)
608efe7368cSKatayama Hirofumi MZ     {
609efe7368cSKatayama Hirofumi MZ         if (!setCursorOnSizeBox(hitSelection))
610640d67d1SKatayama Hirofumi MZ             ::SetCursor(::LoadCursorW(NULL, (LPCWSTR)IDC_SIZEALL));
611efe7368cSKatayama Hirofumi MZ         return 0;
612efe7368cSKatayama Hirofumi MZ     }
613efe7368cSKatayama Hirofumi MZ 
614efe7368cSKatayama Hirofumi MZ     CRect rcImage;
615efe7368cSKatayama Hirofumi MZ     GetImageRect(rcImage);
616efe7368cSKatayama Hirofumi MZ     ImageToCanvas(rcImage);
6179ff9580fSJose Carlos Jesus 
6181aeebfdaSKatayama Hirofumi MZ     if (rcImage.PtInRect(pt))
619efe7368cSKatayama Hirofumi MZ     {
620efe7368cSKatayama Hirofumi MZ         switch (toolsModel.GetActiveTool())
621efe7368cSKatayama Hirofumi MZ         {
622efe7368cSKatayama Hirofumi MZ             case TOOL_FILL:
62356828b82SKatayama Hirofumi MZ                 ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_FILL)));
624efe7368cSKatayama Hirofumi MZ                 break;
625efe7368cSKatayama Hirofumi MZ             case TOOL_COLOR:
62656828b82SKatayama Hirofumi MZ                 ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_COLOR)));
627efe7368cSKatayama Hirofumi MZ                 break;
628efe7368cSKatayama Hirofumi MZ             case TOOL_ZOOM:
62956828b82SKatayama Hirofumi MZ                 ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_ZOOM)));
630efe7368cSKatayama Hirofumi MZ                 break;
631efe7368cSKatayama Hirofumi MZ             case TOOL_PEN:
63256828b82SKatayama Hirofumi MZ                 ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_PEN)));
633efe7368cSKatayama Hirofumi MZ                 break;
634efe7368cSKatayama Hirofumi MZ             case TOOL_AIRBRUSH:
63556828b82SKatayama Hirofumi MZ                 ::SetCursor(::LoadCursorW(g_hinstExe, MAKEINTRESOURCEW(IDC_AIRBRUSH)));
636efe7368cSKatayama Hirofumi MZ                 break;
637efe7368cSKatayama Hirofumi MZ             default:
638640d67d1SKatayama Hirofumi MZ                 ::SetCursor(::LoadCursorW(NULL, (LPCWSTR)IDC_CROSS));
639efe7368cSKatayama Hirofumi MZ         }
640efe7368cSKatayama Hirofumi MZ         return 0;
641efe7368cSKatayama Hirofumi MZ     }
642efe7368cSKatayama Hirofumi MZ 
643efe7368cSKatayama Hirofumi MZ     if (selectionModel.m_bShow || !setCursorOnSizeBox(CanvasHitTest(pt)))
64462b6859aSKatayama Hirofumi MZ         bHandled = FALSE;
6456b6a0457SKatayama Hirofumi MZ 
6466b6a0457SKatayama Hirofumi MZ     return 0;
6476b6a0457SKatayama Hirofumi MZ }
6486b6a0457SKatayama Hirofumi MZ 
OnKeyDown(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)6496b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnKeyDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
6506b6a0457SKatayama Hirofumi MZ {
651*b8598e09SKatayama Hirofumi MZ     if (wParam == VK_ESCAPE)
6526b6a0457SKatayama Hirofumi MZ     {
653*b8598e09SKatayama Hirofumi MZ         OnEndDraw(TRUE);
6546b6a0457SKatayama Hirofumi MZ         ::ReleaseCapture();
655f49919ceSKatayama Hirofumi MZ         m_nMouseDownMsg = 0;
656cb98e91bSKatayama Hirofumi MZ         m_hitCanvasSizeBox = HIT_NONE;
6571aeebfdaSKatayama Hirofumi MZ         m_rcResizing.SetRectEmpty();
658132f482dSKatayama Hirofumi MZ         Invalidate(TRUE);
6596b6a0457SKatayama Hirofumi MZ     }
6606b6a0457SKatayama Hirofumi MZ 
6616b6a0457SKatayama Hirofumi MZ     return 0;
6626b6a0457SKatayama Hirofumi MZ }
6636b6a0457SKatayama Hirofumi MZ 
OnCancelMode(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)6646b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnCancelMode(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
6656b6a0457SKatayama Hirofumi MZ {
6666b6a0457SKatayama Hirofumi MZ     // Cancel dragging
667cb98e91bSKatayama Hirofumi MZ     m_hitCanvasSizeBox = HIT_NONE;
6681aeebfdaSKatayama Hirofumi MZ     m_rcResizing.SetRectEmpty();
6696b6a0457SKatayama Hirofumi MZ     Invalidate(TRUE);
6706b6a0457SKatayama Hirofumi MZ     return 0;
6716b6a0457SKatayama Hirofumi MZ }
6726b6a0457SKatayama Hirofumi MZ 
OnMouseWheel(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)6737361592eSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnMouseWheel(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
6747361592eSKatayama Hirofumi MZ {
675640d67d1SKatayama Hirofumi MZ     return ::SendMessageW(GetParent(), nMsg, wParam, lParam);
6767361592eSKatayama Hirofumi MZ }
6776b6a0457SKatayama Hirofumi MZ 
OnCaptureChanged(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)6786b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnCaptureChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
6796b6a0457SKatayama Hirofumi MZ {
680640d67d1SKatayama Hirofumi MZ     ::SendMessageW(g_hStatusBar, SB_SETTEXT, 2, (LPARAM)L"");
6816b6a0457SKatayama Hirofumi MZ     return 0;
6826b6a0457SKatayama Hirofumi MZ }
6836b6a0457SKatayama Hirofumi MZ 
OnEraseBkgnd(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)6846b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnEraseBkgnd(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
6856b6a0457SKatayama Hirofumi MZ {
6866b6a0457SKatayama Hirofumi MZ     return TRUE; // do nothing => transparent background
6876b6a0457SKatayama Hirofumi MZ }
6886b6a0457SKatayama Hirofumi MZ 
OnPaint(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)6896b6a0457SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
6906b6a0457SKatayama Hirofumi MZ {
6916b6a0457SKatayama Hirofumi MZ     RECT rcClient;
6926b6a0457SKatayama Hirofumi MZ     GetClientRect(&rcClient);
6936b6a0457SKatayama Hirofumi MZ 
6946b6a0457SKatayama Hirofumi MZ     PAINTSTRUCT ps;
6956b6a0457SKatayama Hirofumi MZ     HDC hDC = BeginPaint(&ps);
6966b6a0457SKatayama Hirofumi MZ     DoDraw(hDC, rcClient, ps.rcPaint);
6976b6a0457SKatayama Hirofumi MZ     EndPaint(&ps);
6986b6a0457SKatayama Hirofumi MZ     return 0;
6996b6a0457SKatayama Hirofumi MZ }
700efe7368cSKatayama Hirofumi MZ 
OnEndDraw(BOOL bCancel)701*b8598e09SKatayama Hirofumi MZ VOID CCanvasWindow::OnEndDraw(BOOL bCancel)
702efe7368cSKatayama Hirofumi MZ {
703efe7368cSKatayama Hirofumi MZ     m_drawing = FALSE;
704*b8598e09SKatayama Hirofumi MZ     toolsModel.OnEndDraw(bCancel);
705efe7368cSKatayama Hirofumi MZ     Invalidate(FALSE);
706efe7368cSKatayama Hirofumi MZ }
707efe7368cSKatayama Hirofumi MZ 
OnCtlColorEdit(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)708aeffa57eSKatayama Hirofumi MZ LRESULT CCanvasWindow::OnCtlColorEdit(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
709aeffa57eSKatayama Hirofumi MZ {
710aeffa57eSKatayama Hirofumi MZ     SetTextColor((HDC)wParam, paletteModel.GetFgColor());
711aeffa57eSKatayama Hirofumi MZ     SetBkMode((HDC)wParam, TRANSPARENT);
712aeffa57eSKatayama Hirofumi MZ     return (LRESULT)GetStockObject(NULL_BRUSH);
713aeffa57eSKatayama Hirofumi MZ }
714e8c7e300SKatayama Hirofumi MZ 
OnPaletteModelColorChanged(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)715e8c7e300SKatayama Hirofumi MZ LRESULT CCanvasWindow::OnPaletteModelColorChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
716e8c7e300SKatayama Hirofumi MZ {
717e8c7e300SKatayama Hirofumi MZ     imageModel.NotifyImageChanged();
718e8c7e300SKatayama Hirofumi MZ     return 0;
719e8c7e300SKatayama Hirofumi MZ }
720