ReactOS  0.4.14-dev-822-g065afd9
imgarea.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: PAINT for ReactOS
3  * LICENSE: LGPL
4  * FILE: base/applications/mspaint/imgarea.cpp
5  * PURPOSE: Window procedure of the main window and all children apart from
6  * hPalWin, hToolSettings and hSelection
7  * PROGRAMMERS: Benedikt Freisen
8  * Katayama Hirofumi MZ
9  */
10 
11 /* INCLUDES *********************************************************/
12 
13 #include "precomp.h"
14 
15 #include "dialogs.h"
16 
17 /* FUNCTIONS ********************************************************/
18 
19 void
21 {
22  selectionWindow.ShowWindow(SW_HIDE);
23 
24  int zoomedWidth = imageModel.GetWidth() * toolsModel.GetZoom() / 1000;
25  int zoomedHeight = imageModel.GetHeight() * toolsModel.GetZoom() / 1000;
26  imageArea.MoveWindow(3, 3, zoomedWidth, zoomedHeight, FALSE);
27 
28  scrollboxWindow.Invalidate(TRUE);
30 
31  scrollboxWindow.SetScrollPos(SB_HORZ, 0, TRUE);
32  scrollboxWindow.SetScrollPos(SB_VERT, 0, TRUE);
33 }
34 
35 void CImgAreaWindow::drawZoomFrame(int mouseX, int mouseY)
36 {
37  HDC hdc;
38  HPEN oldPen;
39  HBRUSH oldBrush;
40  LOGBRUSH logbrush;
41  int rop;
42 
43  RECT clientRectScrollbox;
44  RECT clientRectImageArea;
45  int x, y, w, h;
46  scrollboxWindow.GetClientRect(&clientRectScrollbox);
47  GetClientRect(&clientRectImageArea);
48  w = clientRectImageArea.right * 2;
49  h = clientRectImageArea.bottom * 2;
50  if (!w || !h)
51  {
52  return;
53  }
54  w = clientRectImageArea.right * clientRectScrollbox.right / w;
55  h = clientRectImageArea.bottom * clientRectScrollbox.bottom / h;
56  x = max(0, min(clientRectImageArea.right - w, mouseX - w / 2));
57  y = max(0, min(clientRectImageArea.bottom - h, mouseY - h / 2));
58 
59  hdc = GetDC();
60  oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 0, 0));
61  logbrush.lbStyle = BS_HOLLOW;
62  oldBrush = (HBRUSH) SelectObject(hdc, CreateBrushIndirect(&logbrush));
63  rop = SetROP2(hdc, R2_NOT);
64  Rectangle(hdc, x, y, x + w, y + h);
65  SetROP2(hdc, rop);
66  DeleteObject(SelectObject(hdc, oldBrush));
67  DeleteObject(SelectObject(hdc, oldPen));
68  ReleaseDC(hdc);
69 }
70 
72 {
73  int imgXRes = imageModel.GetWidth();
74  int imgYRes = imageModel.GetHeight();
75  sizeboxLeftTop.MoveWindow(
76  0,
77  0, 3, 3, TRUE);
78  sizeboxCenterTop.MoveWindow(
79  imgXRes * toolsModel.GetZoom() / 2000 + 3 * 3 / 4,
80  0, 3, 3, TRUE);
81  sizeboxRightTop.MoveWindow(
82  imgXRes * toolsModel.GetZoom() / 1000 + 3,
83  0, 3, 3, TRUE);
84  sizeboxLeftCenter.MoveWindow(
85  0,
86  imgYRes * toolsModel.GetZoom() / 2000 + 3 * 3 / 4, 3, 3, TRUE);
87  sizeboxRightCenter.MoveWindow(
88  imgXRes * toolsModel.GetZoom() / 1000 + 3,
89  imgYRes * toolsModel.GetZoom() / 2000 + 3 * 3 / 4, 3, 3, TRUE);
90  sizeboxLeftBottom.MoveWindow(
91  0,
92  imgYRes * toolsModel.GetZoom() / 1000 + 3, 3, 3, TRUE);
93  sizeboxCenterBottom.MoveWindow(
94  imgXRes * toolsModel.GetZoom() / 2000 + 3 * 3 / 4,
95  imgYRes * toolsModel.GetZoom() / 1000 + 3, 3, 3, TRUE);
96  sizeboxRightBottom.MoveWindow(
97  imgXRes * toolsModel.GetZoom() / 1000 + 3,
98  imgYRes * toolsModel.GetZoom() / 1000 + 3, 3, 3, TRUE);
100  return 0;
101 }
102 
104 {
106  HDC hdc = GetDC();
107  int imgXRes = imageModel.GetWidth();
108  int imgYRes = imageModel.GetHeight();
109  StretchBlt(hdc, 0, 0, imgXRes * toolsModel.GetZoom() / 1000, imgYRes * toolsModel.GetZoom() / 1000, imageModel.GetDC(), 0, 0, imgXRes,
110  imgYRes, SRCCOPY);
111  if (showGrid && (toolsModel.GetZoom() >= 4000))
112  {
113  HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00a0a0a0));
114  int counter;
115  for(counter = 0; counter <= imgYRes; counter++)
116  {
117  MoveToEx(hdc, 0, counter * toolsModel.GetZoom() / 1000, NULL);
118  LineTo(hdc, imgXRes * toolsModel.GetZoom() / 1000, counter * toolsModel.GetZoom() / 1000);
119  }
120  for(counter = 0; counter <= imgXRes; counter++)
121  {
122  MoveToEx(hdc, counter * toolsModel.GetZoom() / 1000, 0, NULL);
123  LineTo(hdc, counter * toolsModel.GetZoom() / 1000, imgYRes * toolsModel.GetZoom() / 1000);
124  }
125  DeleteObject(SelectObject(hdc, oldPen));
126  }
127  ReleaseDC(hdc);
128  selectionWindow.Invalidate(FALSE);
129  miniature.Invalidate(FALSE);
130  return 0;
131 }
132 
134 {
135  switch (toolsModel.GetActiveTool())
136  {
137  case TOOL_FILL:
139  break;
140  case TOOL_COLOR:
142  break;
143  case TOOL_ZOOM:
145  break;
146  case TOOL_PEN:
148  break;
149  case TOOL_AIRBRUSH:
151  break;
152  default:
154  }
155  return 0;
156 }
157 
159 {
160  if ((!drawing) || (toolsModel.GetActiveTool() == TOOL_COLOR))
161  {
162  SetCapture();
163  drawing = TRUE;
166  }
167  else
168  {
170  imageModel.Undo();
171  }
172  Invalidate(FALSE);
173  if ((toolsModel.GetActiveTool() == TOOL_ZOOM) && (toolsModel.GetZoom() < 8000))
175  return 0;
176 }
177 
179 {
180  if ((!drawing) || (toolsModel.GetActiveTool() == TOOL_COLOR))
181  {
182  SetCapture();
183  drawing = TRUE;
186  }
187  else
188  {
190  imageModel.Undo();
191  }
192  Invalidate(FALSE);
193  if ((toolsModel.GetActiveTool() == TOOL_ZOOM) && (toolsModel.GetZoom() > 125))
195  return 0;
196 }
197 
199 {
200  if (drawing)
201  {
204  Invalidate(FALSE);
206  {
207  COLORREF tempColor =
209  if (tempColor != CLR_INVALID)
210  paletteModel.SetFgColor(tempColor);
211  }
213  }
214  drawing = FALSE;
215  ReleaseCapture();
216  return 0;
217 }
218 
220 {
221  POINT pt;
222  switch (toolsModel.GetActiveTool())
223  {
224  case TOOL_FREESEL: case TOOL_RECTSEL:
225  case TOOL_TEXT: case TOOL_ZOOM: case TOOL_SHAPE:
228  pointSP = 0;
229  Invalidate(FALSE);
230  break;
231  default:
232  GetCursorPos(&pt);
233  ScreenToClient(&pt);
234  // FIXME: dirty hack
235  if (GetKeyState(VK_LBUTTON) < 0)
236  {
239  }
240  else if (GetKeyState(VK_RBUTTON) < 0)
241  {
244  }
245  imageModel.Undo();
246  pointSP = 0;
248  }
249 }
250 
252 {
253  if (drawing)
254  {
255  cancelDrawing();
256  drawing = FALSE;
257  }
258  return 0;
259 }
260 
262 {
263  if (wParam == VK_ESCAPE)
264  {
265  if (GetCapture() == m_hWnd)
266  {
267  ReleaseCapture();
268  }
269  else
270  {
271  switch (toolsModel.GetActiveTool())
272  {
273  case TOOL_SHAPE: case TOOL_BEZIER:
274  cancelDrawing();
275  break;
276  }
277  }
278  }
279  return 0;
280 }
281 
283 {
284  if (drawing)
285  {
288  Invalidate(FALSE);
290  {
291  COLORREF tempColor =
293  if (tempColor != CLR_INVALID)
294  paletteModel.SetBgColor(tempColor);
295  }
297  }
298  drawing = FALSE;
299  ReleaseCapture();
300  return 0;
301 }
302 
304 {
305  LONG xNow = GET_X_LPARAM(lParam) * 1000 / toolsModel.GetZoom();
306  LONG yNow = GET_Y_LPARAM(lParam) * 1000 / toolsModel.GetZoom();
308  {
309  TRACKMOUSEEVENT tme;
310 
312  {
313  Invalidate(FALSE);
314  UpdateWindow();
316  }
317 
318  tme.cbSize = sizeof(TRACKMOUSEEVENT);
319  tme.dwFlags = TME_LEAVE;
320  tme.hwndTrack = m_hWnd;
321  tme.dwHoverTime = 0;
322  TrackMouseEvent(&tme);
323 
324  if (!drawing)
325  {
326  CString strCoord;
327  strCoord.Format(_T("%ld, %ld"), xNow, yNow);
328  SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM) (LPCTSTR) strCoord);
329  }
330  }
331  if (drawing)
332  {
333  /* values displayed in statusbar */
334  LONG xRel = xNow - start.x;
335  LONG yRel = yNow - start.y;
336  /* freesel, rectsel and text tools always show numbers limited to fit into image area */
338  {
339  if (xRel < 0)
340  xRel = (xNow < 0) ? -start.x : xRel;
341  else if (xNow > imageModel.GetWidth())
342  xRel = imageModel.GetWidth() - start.x;
343  if (yRel < 0)
344  yRel = (yNow < 0) ? -start.y : yRel;
345  else if (yNow > imageModel.GetHeight())
346  yRel = imageModel.GetHeight() - start.y;
347  }
348  /* rectsel and shape tools always show non-negative numbers when drawing */
350  {
351  if (xRel < 0)
352  xRel = -xRel;
353  if (yRel < 0)
354  yRel = -yRel;
355  }
356  /* while drawing, update cursor coordinates only for tools 3, 7, 8, 9, 14 */
357  switch(toolsModel.GetActiveTool())
358  {
359  case TOOL_RUBBER:
360  case TOOL_PEN:
361  case TOOL_BRUSH:
362  case TOOL_AIRBRUSH:
363  case TOOL_SHAPE:
364  {
365  CString strCoord;
366  strCoord.Format(_T("%ld, %ld"), xNow, yNow);
367  SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM) (LPCTSTR) strCoord);
368  break;
369  }
370  }
371  if ((wParam & MK_LBUTTON) != 0)
372  {
374  Invalidate(FALSE);
376  {
377  CString strSize;
379  yRel = xRel;
380  strSize.Format(_T("%ld x %ld"), xRel, yRel);
381  SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM) (LPCTSTR) strSize);
382  }
383  }
384  if ((wParam & MK_RBUTTON) != 0)
385  {
387  Invalidate(FALSE);
389  {
390  CString strSize;
392  yRel = xRel;
393  strSize.Format(_T("%ld x %ld"), xRel, yRel);
394  SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM) (LPCTSTR) strSize);
395  }
396  }
397  }
398  return 0;
399 }
400 
402 {
405  Invalidate(FALSE);
406  return 0;
407 }
408 
410 {
412  return 0;
413 }
414 
416 {
417  Invalidate(FALSE);
418  return 0;
419 }
#define TOOL_FILL
Definition: definitions.h:138
HCURSOR hCurFill
Definition: main.cpp:42
ToolsModel toolsModel
Definition: main.cpp:18
#define max(a, b)
Definition: svc.c:63
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
HPEN WINAPI CreatePen(_In_ int, _In_ int, _In_ COLORREF)
BOOL MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint=TRUE)
Definition: atlwin.h:958
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
#define TOOL_TEXT
Definition: definitions.h:144
#define R2_NOT
Definition: wingdi.h:346
#define MK_LBUTTON
Definition: winuser.h:2342
UINT lbStyle
Definition: wingdi.h:1746
BOOL WINAPI Rectangle(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
#define SB_VERT
Definition: winuser.h:553
#define VK_LBUTTON
Definition: winuser.h:2165
#define TOOL_LINE
Definition: definitions.h:145
#define pt(x, y)
Definition: drawing.c:79
void endPaintingL(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg)
Definition: mouse.cpp:218
BOOL showGrid
Definition: main.cpp:59
#define SW_HIDE
Definition: winuser.h:762
BOOL zoomTo(int, int, int)
Definition: winproc.cpp:21
#define SB_HORZ
Definition: winuser.h:552
static HDC
Definition: imagelist.c:92
#define CLR_INVALID
Definition: wingdi.h:882
void drawZoomFrame(int mouseX, int mouseY)
Definition: imgarea.cpp:35
#define TOOL_RUBBER
Definition: definitions.h:137
#define GET_X_LPARAM(lp)
Definition: windowsx.h:274
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
CSizeboxWindow sizeboxRightCenter
Definition: main.cpp:76
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
BOOL WINAPI LineTo(_In_ HDC, _In_ int, _In_ int)
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:668
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
#define TOOL_SHAPE
Definition: definitions.h:148
UINT_PTR WPARAM
Definition: windef.h:207
SelectionModel selectionModel
Definition: main.cpp:20
#define VK_ESCAPE
Definition: winuser.h:2189
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2635
DWORD GetPixel(LPDIRECTDRAWSURFACE7 Surface, UINT x, UINT y)
Definition: blt.cpp:2
LONG right
Definition: windef.h:308
BOOL WINAPI StretchBlt(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_opt_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ DWORD)
WPARAM wParam
Definition: combotst.c:138
void SetBgColor(int newColor)
LRESULT DefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: atlwin.h:1521
#define TOOL_PEN
Definition: definitions.h:141
void whilePaintingL(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg)
Definition: mouse.cpp:125
LRESULT OnRButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:178
BOOL WINAPI TrackMouseEvent(_Inout_ LPTRACKMOUSEEVENT)
HCURSOR hCurPen
Definition: main.cpp:45
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
int GetZoom()
Definition: toolsmodel.cpp:104
CImgAreaWindow imageArea
Definition: main.cpp:71
BOOL WINAPI MoveToEx(_In_ HDC, _In_ int, _In_ int, _Out_opt_ LPPOINT)
#define PS_SOLID
Definition: wingdi.h:585
CSizeboxWindow sizeboxCenterTop
Definition: main.cpp:73
CSizeboxWindow sizeboxRightTop
Definition: main.cpp:74
CSizeboxWindow sizeboxCenterBottom
Definition: main.cpp:78
#define VK_RBUTTON
Definition: winuser.h:2166
void startPaintingL(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg)
Definition: mouse.cpp:56
LRESULT OnSize(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:71
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
#define TOOL_BEZIER
Definition: definitions.h:146
int WINAPI SetROP2(_In_ HDC, _In_ int)
Definition: dc.c:1083
HCURSOR hCurAirbrush
Definition: main.cpp:46
HDC GetDC()
Definition: history.cpp:219
static int rop(int rop, int src, int dst)
Definition: nanoxwin.c:124
BOOL GetClientRect(LPRECT lpRect) const
Definition: atlwin.h:507
HWND WINAPI GetCapture(void)
Definition: message.c:2879
#define VK_SHIFT
Definition: winuser.h:2177
CMiniatureWindow miniature
Definition: main.cpp:64
#define TOOL_AIRBRUSH
Definition: definitions.h:143
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
CSizeboxWindow sizeboxRightBottom
Definition: main.cpp:79
GLuint counter
Definition: glext.h:11116
HCURSOR hCurZoom
Definition: main.cpp:44
#define MK_RBUTTON
Definition: winuser.h:2343
SHORT WINAPI GetKeyState(_In_ int)
#define _T(x)
Definition: vfdio.h:22
LRESULT OnLButtonUp(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:198
if(!(yy_init))
Definition: macro.lex.yy.c:714
DWORD COLORREF
Definition: windef.h:300
short pointSP
Definition: mouse.cpp:53
BOOL UpdateWindow()
Definition: atlwin.h:1277
void updateCanvasAndScrollbars()
Definition: imgarea.cpp:20
HWND hStatusBar
Definition: main.cpp:35
#define WM_RBUTTONUP
Definition: winuser.h:1762
#define BS_HOLLOW
Definition: wingdi.h:1087
#define TOOL_COLOR
Definition: definitions.h:139
#define WM_PAINT
Definition: winuser.h:1602
int GetWidth()
Definition: history.cpp:195
int GetHeight()
Definition: history.cpp:200
BOOL ScreenToClient(LPPOINT lpPoint) const
Definition: atlwin.h:1024
struct tagTRACKMOUSEEVENT TRACKMOUSEEVENT
HDC hdc
Definition: main.c:9
#define SB_SETTEXT
Definition: commctrl.h:1945
CSizeboxWindow sizeboxLeftCenter
Definition: main.cpp:75
SHORT WINAPI GetAsyncKeyState(_In_ int)
void SetFgColor(int newColor)
ImageModel imageModel
Definition: main.cpp:32
HWND m_hWnd
Definition: atlwin.h:268
#define TOOL_FREESEL
Definition: definitions.h:135
#define TOOL_BRUSH
Definition: definitions.h:142
LRESULT OnLButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:158
#define TOOL_RECTSEL
Definition: definitions.h:136
#define LoadCursor
Definition: winuser.h:5787
HBRUSH WINAPI CreateBrushIndirect(_In_ const LOGBRUSH *plb)
LRESULT OnImageModelImageChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:415
int ReleaseDC(HDC hDC)
Definition: atlwin.h:1006
#define IDC_CROSS
Definition: winuser.h:685
CSizeboxWindow sizeboxLeftBottom
Definition: main.cpp:77
PaletteModel paletteModel
Definition: main.cpp:28
LRESULT OnImageModelDimensionsChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:409
HCURSOR WINAPI SetCursor(_In_opt_ HCURSOR)
#define TOOL_ZOOM
Definition: definitions.h:140
GLuint start
Definition: gl.h:1545
int GetActiveTool()
Definition: toolsmodel.cpp:60
#define TME_LEAVE
Definition: commctrl.h:4977
#define WM_LBUTTONUP
Definition: winuser.h:1759
LRESULT OnSetCursor(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:133
LRESULT OnKeyDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:261
#define min(a, b)
Definition: monoChain.cc:55
unsigned int UINT
Definition: ndis.h:50
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
LRESULT OnMouseLeave(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:401
BOOL drawing
Definition: imgarea.h:34
#define GET_Y_LPARAM(lp)
Definition: windowsx.h:275
CScrollboxWindow scrollboxWindow
Definition: main.cpp:68
void UpdateScrollbox()
Definition: scrollbox.cpp:53
void cancelDrawing()
Definition: imgarea.cpp:219
BOOL Invalidate(BOOL bErase=TRUE)
Definition: atlwin.h:828
void Undo(void)
Definition: history.cpp:57
LRESULT OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:103
BOOL WINAPI ReleaseCapture(void)
Definition: message.c:2888
CSizeboxWindow sizeboxLeftTop
Definition: main.cpp:72
void startPaintingR(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg)
Definition: mouse.cpp:329
LRESULT OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:303
LRESULT OnRButtonUp(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:282
LRESULT SendMessage(UINT message, WPARAM wParam=0, LPARAM lParam=0)
Definition: atlwin.h:1048
LONG bottom
Definition: windef.h:309
void endPaintingR(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg)
Definition: mouse.cpp:464
LONG_PTR LRESULT
Definition: windef.h:209
void ResetToPrevious(void)
Definition: history.cpp:93
LPARAM lParam
Definition: combotst.c:139
HCURSOR hCurColor
Definition: main.cpp:43
HDC GetDC()
Definition: atlwin.h:513
#define SRCCOPY
Definition: wingdi.h:332
void whilePaintingR(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg)
Definition: mouse.cpp:389
HWND SetCapture()
Definition: atlwin.h:1100
CSelectionWindow selectionWindow
Definition: main.cpp:70
LRESULT OnCaptureChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: imgarea.cpp:251