2 * PROJECT: PAINT for ReactOS
4 * FILE: base/applications/mspaint_new/selection.cpp
5 * PURPOSE: Window procedure of the selection window
6 * PROGRAMMERS: Benedikt Freisen
9 /* INCLUDES *********************************************************/
13 /* FUNCTIONS ********************************************************/
15 const LPCTSTR
CSelectionWindow::m_lpszCursorLUT
[9] = { /* action to mouse cursor lookup table */
18 IDC_SIZENWSE
, IDC_SIZENS
, IDC_SIZENESW
,
19 IDC_SIZEWE
, IDC_SIZEWE
,
20 IDC_SIZENESW
, IDC_SIZENS
, IDC_SIZENWSE
24 ColorKeyedMaskBlt(HDC hdcDest
, int nXDest
, int nYDest
, int nWidth
, int nHeight
, HDC hdcSrc
, int nXSrc
, int nYSrc
, HBITMAP hbmMask
, int xMask
, int yMask
, DWORD dwRop
, COLORREF keyColor
)
32 hTempDC
= CreateCompatibleDC(hdcSrc
);
33 hTempDC2
= CreateCompatibleDC(hdcSrc
);
34 hTempBm
= CreateCompatibleBitmap(hTempDC
, nWidth
, nHeight
);
35 SelectObject(hTempDC
, hTempBm
);
36 hTempBrush
= CreateSolidBrush(keyColor
);
37 SelectObject(hTempDC
, hTempBrush
);
38 BitBlt(hTempDC
, 0, 0, nWidth
, nHeight
, hdcSrc
, nXSrc
, nYSrc
, SRCCOPY
);
39 PatBlt(hTempDC
, 0, 0, nWidth
, nHeight
, PATINVERT
);
40 hTempMask
= CreateBitmap(nWidth
, nHeight
, 1, 1, NULL
);
41 SelectObject(hTempDC2
, hTempMask
);
42 BitBlt(hTempDC2
, 0, 0, nWidth
, nHeight
, hTempDC
, 0, 0, SRCCOPY
);
43 SelectObject(hTempDC
, hbmMask
);
44 BitBlt(hTempDC2
, 0, 0, nWidth
, nHeight
, hTempDC
, xMask
, yMask
, SRCAND
);
45 MaskBlt(hdcDest
, nXDest
, nYDest
, nWidth
, nHeight
, hdcSrc
, nXSrc
, nYSrc
, hTempMask
, xMask
, yMask
, dwRop
);
48 DeleteObject(hTempBm
);
49 DeleteObject(hTempBrush
);
50 DeleteObject(hTempMask
);
55 ForceRefreshSelectionContents()
57 if (selectionWindow
.IsWindowVisible())
59 selectionWindow
.SendMessage(WM_LBUTTONDOWN
, 0, MAKELPARAM(0, 0));
60 selectionWindow
.SendMessage(WM_MOUSEMOVE
, 0, MAKELPARAM(0, 0));
61 selectionWindow
.SendMessage(WM_LBUTTONUP
, 0, MAKELPARAM(0, 0));
65 int CSelectionWindow::IdentifyCorner(int iXPos
, int iYPos
, int iWidth
, int iHeight
)
70 return ACTION_RESIZE_TOP_LEFT
;
71 if ((iXPos
< iWidth
/ 2 + 2) && (iXPos
>= iWidth
/ 2 - 1))
72 return ACTION_RESIZE_TOP
;
73 if (iXPos
>= iWidth
- 3)
74 return ACTION_RESIZE_TOP_RIGHT
;
76 if ((iYPos
< iHeight
/ 2 + 2) && (iYPos
>= iHeight
/ 2 - 1))
79 return ACTION_RESIZE_LEFT
;
80 if (iXPos
>= iWidth
- 3)
81 return ACTION_RESIZE_RIGHT
;
83 if (iYPos
>= iHeight
- 3)
86 return ACTION_RESIZE_BOTTOM_LEFT
;
87 if ((iXPos
< iWidth
/ 2 + 2) && (iXPos
>= iWidth
/ 2 - 1))
88 return ACTION_RESIZE_BOTTOM
;
89 if (iXPos
>= iWidth
- 3)
90 return ACTION_RESIZE_BOTTOM_RIGHT
;
95 LRESULT
CSelectionWindow::OnPaint(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
100 DefWindowProc(WM_PAINT
, wParam
, lParam
);
101 SelectionFrame(hDC
, 1, 1, selectionModel
.GetDestRectWidth() * toolsModel
.GetZoom() / 1000 + 5,
102 selectionModel
.GetDestRectHeight() * toolsModel
.GetZoom() / 1000 + 5,
103 m_dwSystemSelectionColor
);
109 LRESULT
CSelectionWindow::OnEraseBkgnd(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
111 // do nothing => transparent background
115 LRESULT
CSelectionWindow::OnCreate(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
118 m_iAction
= ACTION_MOVE
;
119 /* update the system selection color */
120 m_dwSystemSelectionColor
= GetSysColor(COLOR_HIGHLIGHT
);
121 SendMessage(WM_PAINT
, 0, MAKELPARAM(0, 0));
125 LRESULT
CSelectionWindow::OnSysColorChange(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
127 /* update the system selection color */
128 m_dwSystemSelectionColor
= GetSysColor(COLOR_HIGHLIGHT
);
129 SendMessage(WM_PAINT
, 0, MAKELPARAM(0, 0));
133 LRESULT
CSelectionWindow::OnSetCursor(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
135 SetCursor(LoadCursor(NULL
, IDC_SIZEALL
));
139 LRESULT
CSelectionWindow::OnLButtonDown(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
141 m_ptPos
.x
= GET_X_LPARAM(lParam
);
142 m_ptPos
.y
= GET_Y_LPARAM(lParam
);
146 if (m_iAction
!= ACTION_MOVE
)
147 SetCursor(LoadCursor(NULL
, m_lpszCursorLUT
[m_iAction
]));
149 scrlClientWindow
.InvalidateRect(NULL
, TRUE
);
150 imageArea
.SendMessage(WM_PAINT
, 0, 0);
154 LRESULT
CSelectionWindow::OnMouseMove(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
159 imageModel
.ResetToPrevious();
160 m_ptFrac
.x
+= GET_X_LPARAM(lParam
) - m_ptPos
.x
;
161 m_ptFrac
.y
+= GET_Y_LPARAM(lParam
) - m_ptPos
.y
;
162 m_ptDelta
.x
+= m_ptFrac
.x
* 1000 / toolsModel
.GetZoom();
163 m_ptDelta
.y
+= m_ptFrac
.y
* 1000 / toolsModel
.GetZoom();
164 if (toolsModel
.GetZoom() < 1000)
171 m_ptFrac
.x
-= (m_ptFrac
.x
* 1000 / toolsModel
.GetZoom()) * toolsModel
.GetZoom() / 1000;
172 m_ptFrac
.y
-= (m_ptFrac
.y
* 1000 / toolsModel
.GetZoom()) * toolsModel
.GetZoom() / 1000;
174 selectionModel
.ModifyDestRect(m_ptDelta
, m_iAction
);
176 _stprintf(sizeStr
, _T("%d x %d"), selectionModel
.GetDestRectWidth(), selectionModel
.GetDestRectHeight());
177 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
) sizeStr
);
179 if (toolsModel
.GetActiveTool() == TOOL_TEXT
)
181 selectionModel
.DrawTextToolText(imageModel
.GetDC(), paletteModel
.GetFgColor(), paletteModel
.GetBgColor(), toolsModel
.IsBackgroundTransparent());
185 if (m_iAction
!= ACTION_MOVE
)
186 selectionModel
.DrawSelectionStretched(imageModel
.GetDC());
188 selectionModel
.DrawSelection(imageModel
.GetDC(), paletteModel
.GetBgColor(), toolsModel
.IsBackgroundTransparent());
190 imageArea
.InvalidateRect(NULL
, FALSE
);
191 imageArea
.SendMessage(WM_PAINT
, 0, 0);
192 m_ptPos
.x
= GET_X_LPARAM(lParam
);
193 m_ptPos
.y
= GET_Y_LPARAM(lParam
);
197 int w
= selectionModel
.GetDestRectWidth() * toolsModel
.GetZoom() / 1000 + 6;
198 int h
= selectionModel
.GetDestRectHeight() * toolsModel
.GetZoom() / 1000 + 6;
199 m_ptPos
.x
= GET_X_LPARAM(lParam
);
200 m_ptPos
.y
= GET_Y_LPARAM(lParam
);
201 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
) NULL
);
202 m_iAction
= IdentifyCorner(m_ptPos
.x
, m_ptPos
.y
, w
, h
);
203 if (m_iAction
!= ACTION_MOVE
)
204 SetCursor(LoadCursor(NULL
, m_lpszCursorLUT
[m_iAction
]));
209 LRESULT
CSelectionWindow::OnLButtonUp(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
215 if (m_iAction
!= ACTION_MOVE
)
217 if (toolsModel
.GetActiveTool() == TOOL_TEXT
)
219 // FIXME: What to do?
223 selectionModel
.ScaleContentsToFit();
233 LRESULT
CSelectionWindow::OnPaletteModelColorChanged(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
235 if (toolsModel
.GetActiveTool() == TOOL_TEXT
)
236 ForceRefreshSelectionContents();
240 LRESULT
CSelectionWindow::OnToolsModelSettingsChanged(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
242 if (toolsModel
.GetActiveTool() == TOOL_FREESEL
||
243 toolsModel
.GetActiveTool() == TOOL_RECTSEL
||
244 toolsModel
.GetActiveTool() == TOOL_TEXT
)
245 ForceRefreshSelectionContents();
249 LRESULT
CSelectionWindow::OnSelectionModelRefreshNeeded(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
251 ForceRefreshSelectionContents();