1426e086d594bd0e2f0d6f2c2088c825b27da69c
[reactos.git] / base / applications / mspaint / selection.cpp
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: base/applications/mspaint/selection.cpp
5 * PURPOSE: Window procedure of the selection window
6 * PROGRAMMERS: Benedikt Freisen
7 * Katayama Hirofumi MZ
8 */
9
10 #include "precomp.h"
11
12 CSelectionWindow selectionWindow;
13
14 /* FUNCTIONS ********************************************************/
15
16 const LPCTSTR CSelectionWindow::m_lpszCursorLUT[9] = { /* action to mouse cursor lookup table */
17 IDC_SIZEALL,
18
19 IDC_SIZENWSE, IDC_SIZENS, IDC_SIZENESW,
20 IDC_SIZEWE, IDC_SIZEWE,
21 IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE
22 };
23
24 void CSelectionWindow::ForceRefreshSelectionContents()
25 {
26 if (::IsWindowVisible(selectionWindow))
27 {
28 imageModel.ResetToPrevious();
29 imageModel.DrawSelectionBackground(m_rgbBack);
30 selectionModel.DrawSelection(imageModel.GetDC(), paletteModel.GetBgColor(), toolsModel.IsBackgroundTransparent());
31 }
32 }
33
34 int CSelectionWindow::IdentifyCorner(int iXPos, int iYPos, int iWidth, int iHeight)
35 {
36 return 0;
37 }
38
39 LRESULT CSelectionWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
40 {
41 PAINTSTRUCT ps;
42 HDC hDC = BeginPaint(&ps);
43 if (!m_bMoving)
44 {
45 RECT rcClient;
46 GetClientRect(&rcClient);
47 drawSizeBoxes(hDC, &rcClient, TRUE, &ps.rcPaint);
48 }
49 EndPaint(&ps);
50 return 0;
51 }
52
53 LRESULT CSelectionWindow::OnEraseBkgnd(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
54 {
55 // do nothing => transparent background
56 return TRUE;
57 }
58
59 LRESULT CSelectionWindow::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
60 {
61 m_bMoving = FALSE;
62 m_iAction = ACTION_MOVE;
63 /* update the system selection color */
64 Invalidate();
65 return 0;
66 }
67
68 LRESULT CSelectionWindow::OnSysColorChange(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
69 {
70 /* update the system selection color */
71 Invalidate();
72 return 0;
73 }
74
75 LRESULT CSelectionWindow::OnSetCursor(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
76 {
77 RECT rcClient;
78 GetClientRect(&rcClient);
79
80 POINT pt;
81 ::GetCursorPos(&pt);
82 ScreenToClient(&pt);
83
84 if (!setCursorOnSizeBox(getSizeBoxHitTest(pt, &rcClient)))
85 ::SetCursor(::LoadCursor(NULL, IDC_SIZEALL));
86
87 return 0;
88 }
89
90 LRESULT CSelectionWindow::OnLButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
91 {
92 m_ptPos.x = GET_X_LPARAM(lParam);
93 m_ptPos.y = GET_Y_LPARAM(lParam);
94 m_ptDelta.x = 0;
95 m_ptDelta.y = 0;
96 SetCapture();
97 if (m_iAction != ACTION_MOVE)
98 SetCursor(LoadCursor(NULL, m_lpszCursorLUT[m_iAction]));
99 m_bMoving = TRUE;
100 imageArea.InvalidateRect(NULL, FALSE);
101 imageArea.SendMessage(WM_PAINT, 0, 0);
102 m_rgbBack = paletteModel.GetBgColor();
103 return 0;
104 }
105
106 LRESULT CSelectionWindow::OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
107 {
108 if (m_bMoving)
109 {
110 imageModel.ResetToPrevious();
111 imageModel.DrawSelectionBackground(m_rgbBack);
112 m_ptFrac.x += GET_X_LPARAM(lParam) - m_ptPos.x;
113 m_ptFrac.y += GET_Y_LPARAM(lParam) - m_ptPos.y;
114 m_ptDelta.x += UnZoomed(m_ptFrac.x);
115 m_ptDelta.y += UnZoomed(m_ptFrac.y);
116 if (toolsModel.GetZoom() < 1000)
117 {
118 m_ptFrac.x = 0;
119 m_ptFrac.y = 0;
120 }
121 else
122 {
123 m_ptFrac.x -= Zoomed(UnZoomed(m_ptFrac.x));
124 m_ptFrac.y -= Zoomed(UnZoomed(m_ptFrac.y));
125 }
126 selectionModel.ModifyDestRect(m_ptDelta, m_iAction);
127
128 CString strSize;
129 strSize.Format(_T("%ld x %ld"), selectionModel.GetDestRectWidth(), selectionModel.GetDestRectHeight());
130 SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM) (LPCTSTR) strSize);
131
132 if (m_iAction != ACTION_MOVE)
133 selectionModel.DrawSelectionStretched(imageModel.GetDC());
134 else
135 selectionModel.DrawSelection(imageModel.GetDC(), paletteModel.GetBgColor(), toolsModel.IsBackgroundTransparent());
136 imageArea.InvalidateRect(NULL, FALSE);
137 imageArea.SendMessage(WM_PAINT, 0, 0);
138 m_ptPos.x = GET_X_LPARAM(lParam);
139 m_ptPos.y = GET_Y_LPARAM(lParam);
140 }
141 else
142 {
143 int w = Zoomed(selectionModel.GetDestRectWidth()) + 2 * GRIP_SIZE;
144 int h = Zoomed(selectionModel.GetDestRectHeight()) + 2 * GRIP_SIZE;
145 m_ptPos.x = GET_X_LPARAM(lParam);
146 m_ptPos.y = GET_Y_LPARAM(lParam);
147 SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM) NULL);
148 m_iAction = IdentifyCorner(m_ptPos.x, m_ptPos.y, w, h);
149 if (m_iAction != ACTION_MOVE)
150 SetCursor(LoadCursor(NULL, m_lpszCursorLUT[m_iAction]));
151 }
152 return 0;
153 }
154
155 LRESULT CSelectionWindow::OnMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
156 {
157 m_bMoved = TRUE;
158 return 0;
159 }
160
161 LRESULT CSelectionWindow::OnLButtonUp(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
162 {
163 if (m_bMoving)
164 {
165 m_bMoving = FALSE;
166 ReleaseCapture();
167 if (m_iAction != ACTION_MOVE && toolsModel.GetActiveTool() != TOOL_TEXT)
168 {
169 imageModel.Undo();
170 imageModel.DrawSelectionBackground(m_rgbBack);
171 selectionModel.ScaleContentsToFit();
172 imageModel.CopyPrevious();
173 }
174 placeSelWin();
175 }
176 return 0;
177 }
178
179 LRESULT CSelectionWindow::OnCaptureChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
180 {
181 if (m_bMoving)
182 {
183 m_bMoving = FALSE;
184 if (m_iAction == ACTION_MOVE)
185 {
186 if (toolsModel.GetActiveTool() == TOOL_RECTSEL)
187 imageArea.cancelDrawing();
188 else
189 placeSelWin();
190 }
191 else
192 {
193 m_iAction = ACTION_MOVE;
194 }
195 ShowWindow(SW_HIDE);
196 }
197 return 0;
198 }
199
200 LRESULT CSelectionWindow::OnKeyDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
201 {
202 if (wParam == VK_ESCAPE)
203 {
204 if (GetCapture() == m_hWnd)
205 {
206 ReleaseCapture();
207 }
208 }
209 return 0;
210 }
211
212 LRESULT CSelectionWindow::OnPaletteModelColorChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
213 {
214 return 0;
215 }
216
217 LRESULT CSelectionWindow::OnToolsModelSettingsChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
218 {
219 return 0;
220 }
221
222 LRESULT CSelectionWindow::OnSelectionModelRefreshNeeded(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
223 {
224 ForceRefreshSelectionContents();
225 return 0;
226 }
227
228 LRESULT CSelectionWindow::OnMouseWheel(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
229 {
230 return ::SendMessage(GetParent(), nMsg, wParam, lParam);
231 }
232
233 LRESULT CSelectionWindow::OnToolsModelZoomChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
234 {
235 placeSelWin();
236 return 0;
237 }