2 * PROJECT: PAINT for ReactOS
5 * PURPOSE: Window procedure of the main window and all children apart from
6 * hPalWin, hToolSettings and hSelection
7 * PROGRAMMERS: Benedikt Freisen
10 /* INCLUDES *********************************************************/
14 //#include <htmlhelp.h>
16 #include "definitions.h"
17 #include "globalvar.h"
21 /* FUNCTIONS ********************************************************/
23 void selectTool(int tool
)
25 ShowWindow(hSelection
, SW_HIDE
);
27 SendMessage(hToolSettings
, WM_PAINT
, 0, 0);
30 void updateCanvasAndScrollbars()
32 ShowWindow(hSelection
, SW_HIDE
);
33 MoveWindow(hImageArea
, 3, 3, imgXRes
*zoom
/1000, imgYRes
*zoom
/1000, FALSE
);
34 InvalidateRect(hScrollbox
, NULL
, TRUE
);
35 InvalidateRect(hImageArea
, NULL
, FALSE
);
37 SetScrollPos(hScrollbox
, SB_HORZ
, 0, TRUE
);
38 SetScrollPos(hScrollbox
, SB_VERT
, 0, TRUE
);
41 void ZoomTo(int newZoom
)
44 updateCanvasAndScrollbars();
50 LRESULT CALLBACK
WindowProcedure (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
52 switch (message
) /* handle the messages */
55 PostQuitMessage (0); /* send a WM_QUIT to the message queue */
61 char saveprompttext
[100];
62 LoadString(hProgInstance
, IDS_PROGRAMNAME
, (LPTSTR
)&programname
, 20);
63 LoadString(hProgInstance
, IDS_SAVEPROMPTTEXT
, (LPTSTR
)&saveprompttext
, 100);
65 sprintf(temptext
, saveprompttext
, filename
);
66 switch (MessageBox(hwnd
, (LPTSTR
)&temptext
, (LPTSTR
)&programname
, MB_YESNOCANCEL
| MB_ICONQUESTION
))
72 SendMessage(hwnd
, WM_COMMAND
, IDM_FILESAVEAS
, 0);
82 case WM_INITMENUPOPUP
:
88 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERPLANE
, MF_ENABLED
| MF_BYCOMMAND
);
89 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERCENTERED
, MF_ENABLED
| MF_BYCOMMAND
);
90 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERSTRETCHED
, MF_ENABLED
| MF_BYCOMMAND
);
94 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERPLANE
, MF_GRAYED
| MF_BYCOMMAND
);
95 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERCENTERED
, MF_GRAYED
| MF_BYCOMMAND
);
96 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERSTRETCHED
, MF_GRAYED
| MF_BYCOMMAND
);
101 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITUNDO
, MF_ENABLED
| MF_BYCOMMAND
);
103 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITUNDO
, MF_GRAYED
| MF_BYCOMMAND
);
105 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITREDO
, MF_ENABLED
| MF_BYCOMMAND
);
107 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITREDO
, MF_GRAYED
| MF_BYCOMMAND
);
108 if (IsWindowVisible(hSelection
))
110 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCUT
, MF_ENABLED
| MF_BYCOMMAND
);
111 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPY
, MF_ENABLED
| MF_BYCOMMAND
);
112 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITDELETESELECTION
, MF_ENABLED
| MF_BYCOMMAND
);
113 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITINVERTSELECTION
, MF_ENABLED
| MF_BYCOMMAND
);
114 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPYTO
, MF_ENABLED
| MF_BYCOMMAND
);
118 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCUT
, MF_GRAYED
| MF_BYCOMMAND
);
119 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPY
, MF_GRAYED
| MF_BYCOMMAND
);
120 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITDELETESELECTION
, MF_GRAYED
| MF_BYCOMMAND
);
121 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITINVERTSELECTION
, MF_GRAYED
| MF_BYCOMMAND
);
122 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPYTO
, MF_GRAYED
| MF_BYCOMMAND
);
124 OpenClipboard(hMainWnd
);
125 if (GetClipboardData(CF_BITMAP
)!=NULL
)
126 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITPASTE
, MF_ENABLED
| MF_BYCOMMAND
);
128 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITPASTE
, MF_GRAYED
| MF_BYCOMMAND
);
132 if (IsWindowVisible(hSelection
))
133 EnableMenuItem(GetMenu(hMainWnd
), IDM_IMAGECROP
, MF_ENABLED
| MF_BYCOMMAND
);
135 EnableMenuItem(GetMenu(hMainWnd
), IDM_IMAGECROP
, MF_GRAYED
| MF_BYCOMMAND
);
137 CheckMenuItem(GetMenu(hMainWnd
), IDM_IMAGEDRAWOPAQUE
, MF_CHECKED
| MF_BYCOMMAND
);
139 CheckMenuItem(GetMenu(hMainWnd
), IDM_IMAGEDRAWOPAQUE
, MF_UNCHECKED
| MF_BYCOMMAND
);
146 int test
[] = {LOWORD(lParam
)-260, LOWORD(lParam
)-140, LOWORD(lParam
)-20};
147 SendMessage(hStatusBar
, WM_SIZE
, wParam
, lParam
);
148 SendMessage(hStatusBar
, SB_SETPARTS
, 3, (int)&test
);
149 MoveWindow(hScrollbox
, 56, 49,LOWORD(lParam
)-56, HIWORD(lParam
)-72, TRUE
);
150 //InvalidateRect(hwnd, NULL, TRUE);
152 if ((hwnd
==hImageArea
)||(hwnd
==hScrollbox
))
154 long clientRectScrollbox
[4];
155 GetClientRect(hScrollbox
, (LPRECT
)&clientRectScrollbox
);
156 long clientRectImageArea
[4];
157 GetClientRect(hImageArea
, (LPRECT
)&clientRectImageArea
);
158 MoveWindow(hScrlClient
, 0, 0, max(clientRectImageArea
[2]+6, clientRectScrollbox
[2]), max(clientRectImageArea
[3]+6, clientRectScrollbox
[3]), TRUE
);
159 SCROLLINFO horzScroll
;
160 horzScroll
.cbSize
= sizeof(SCROLLINFO
);
161 horzScroll
.fMask
= SIF_PAGE
| SIF_RANGE
;
162 horzScroll
.nMax
= 10000;
164 horzScroll
.nPage
= clientRectScrollbox
[2]*10000/(clientRectImageArea
[2]+6);
166 horzScroll
.nTrackPos
= 0;
167 SetScrollInfo(hScrollbox
, SB_HORZ
, &horzScroll
, TRUE
);
168 GetClientRect(hScrollbox
, (LPRECT
)clientRectScrollbox
);
169 SCROLLINFO vertScroll
;
170 vertScroll
.cbSize
= sizeof(SCROLLINFO
);
171 vertScroll
.fMask
= SIF_PAGE
| SIF_RANGE
;
172 vertScroll
.nMax
= 10000;
174 vertScroll
.nPage
= clientRectScrollbox
[3]*10000/(clientRectImageArea
[3]+6);
176 vertScroll
.nTrackPos
= 0;
177 SetScrollInfo(hScrollbox
, SB_VERT
, &vertScroll
, TRUE
);
181 if (hwnd
==hScrollbox
)
183 if ((LOWORD(wParam
)==SB_THUMBPOSITION
)||(LOWORD(wParam
)==SB_THUMBTRACK
))
185 SetScrollPos(hScrollbox
, SB_HORZ
, HIWORD(wParam
), TRUE
);
186 MoveWindow(hScrlClient
, -(imgXRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_HORZ
)/10000,
187 -(imgYRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_VERT
)/10000, imgXRes
*zoom
/1000+6, imgYRes
*zoom
/1000+6, TRUE
);
192 if (hwnd
==hScrollbox
)
194 if ((LOWORD(wParam
)==SB_THUMBPOSITION
)||(LOWORD(wParam
)==SB_THUMBTRACK
))
196 SetScrollPos(hScrollbox
, SB_VERT
, HIWORD(wParam
), TRUE
);
197 MoveWindow(hScrlClient
, -(imgXRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_HORZ
)/10000,
198 -(imgYRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_VERT
)/10000, imgXRes
*zoom
/1000+6, imgYRes
*zoom
/1000+6, TRUE
);
202 case WM_GETMINMAXINFO
:
205 MINMAXINFO
*mm
= (LPMINMAXINFO
)lParam
;
206 (*mm
).ptMinTrackSize
.x
= 330;
207 (*mm
).ptMinTrackSize
.y
= 430;
211 DefWindowProc (hwnd
, message
, wParam
, lParam
);
212 if (hwnd
==hImageArea
)
214 HDC hdc
= GetDC(hImageArea
);
215 StretchBlt(hdc
, 0, 0, imgXRes
*zoom
/1000, imgYRes
*zoom
/1000, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
216 ReleaseDC(hImageArea
, hdc
);
217 SendMessage(hSelection
, WM_PAINT
, 0, 0);
221 // mouse events used for drawing
224 if (hwnd
==hImageArea
)
232 SetCursor(hCurColor
);
241 SetCursor(hCurAirbrush
);
244 SetCursor(LoadCursor(NULL
, IDC_CROSS
));
246 } else DefWindowProc(hwnd
, message
, wParam
, lParam
);
249 if (hwnd
==hImageArea
)
251 if ((!drawing
)||(activeTool
==5))
253 SetCapture(hImageArea
);
255 startPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
258 SendMessage(hwnd
, WM_LBUTTONUP
, wParam
, lParam
);
261 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
262 if ((activeTool
==6)&&(zoom
<8000)) ZoomTo(zoom
*2);
266 if (hwnd
==hImageArea
)
268 if ((!drawing
)||(activeTool
==5))
270 SetCapture(hImageArea
);
272 startPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
275 SendMessage(hwnd
, WM_RBUTTONUP
, wParam
, lParam
);
278 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
279 if ((activeTool
==6)&&(zoom
>125)) ZoomTo(zoom
/2);
283 if ((hwnd
==hImageArea
)&&drawing
)
287 endPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
288 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
291 int tempColor
= GetPixel(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
);
292 if (tempColor
!=CLR_INVALID
) fgColor
= tempColor
;
293 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
295 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)"");
299 if ((hwnd
==hImageArea
)&&drawing
)
303 endPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
304 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
307 int tempColor
= GetPixel(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
);
308 if (tempColor
!=CLR_INVALID
) bgColor
= tempColor
;
309 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
311 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)"");
315 if (hwnd
==hImageArea
)
317 if ((!drawing
)||(activeTool
<=9))
320 sprintf(coordStr
, "%d, %d", (short)LOWORD(lParam
)*1000/zoom
, (short)HIWORD(lParam
)*1000/zoom
);
321 SendMessage(hStatusBar
, SB_SETTEXT
, 1, (LPARAM
)coordStr
);
325 if ((wParam
&MK_LBUTTON
)!=0)
327 whilePainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
328 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
329 if ((activeTool
>=10)||(activeTool
==2))
332 sprintf(sizeStr
, "%d x %d", (short)LOWORD(lParam
)*1000/zoom
-startX
, (short)HIWORD(lParam
)*1000/zoom
-startY
);
333 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)sizeStr
);
336 if ((wParam
&MK_RBUTTON
)!=0)
338 whilePainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
339 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
343 sprintf(sizeStr
, "%d x %d", (short)LOWORD(lParam
)*1000/zoom
-startX
, (short)HIWORD(lParam
)*1000/zoom
-startY
);
344 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)sizeStr
);
350 SendMessage(hStatusBar
, SB_SETTEXT
, 1, (LPARAM
)"");
354 // menu and button events
357 switch (LOWORD(wParam
))
361 HICON paintIcon
= LoadIcon(hProgInstance
, MAKEINTRESOURCE(IDI_APPICON
));
364 LoadString(hProgInstance
, IDS_INFOTITLE
, (LPTSTR
)&infotitle
, 100);
365 LoadString(hProgInstance
, IDS_INFOTEXT
, (LPTSTR
)&infotext
, 200);
366 ShellAbout(hMainWnd
, (LPTSTR
)&infotitle
, (LPTSTR
)&infotext
, paintIcon
);
367 DeleteObject(paintIcon
);
370 case IDM_HELPHELPTOPICS
:
371 //HtmlHelp(hMainWnd, "help\\Paint.chm", 0, 0);
374 SendMessage(hwnd
, WM_CLOSE
, wParam
, lParam
);
377 Rectangle(hDrawingDC
, 0-1, 0-1, imgXRes
+1, imgYRes
+1);
378 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
381 if (GetOpenFileName(&ofn
)!=0)
383 HBITMAP bmNew
= (HBITMAP
)LoadDIBFromFile(ofn
.lpstrFile
);
386 insertReversible(bmNew
);
387 updateCanvasAndScrollbars();
390 CopyMemory(&filename
, ofn
.lpstrFileTitle
, 256);
391 CopyMemory(&filepathname
, ofn
.lpstrFileTitle
, 1000);
392 LoadString(hProgInstance
, IDS_WINDOWTITLE
, (LPTSTR
)&resstr
, 100);
393 sprintf(tempstr
, resstr
, &filename
);
394 SetWindowText(hMainWnd
, tempstr
);
402 SaveDIBToFile(hBms
[currInd
], &filepathname
, hDrawingDC
);
404 SendMessage(hwnd
, WM_COMMAND
, IDM_FILESAVEAS
, 0);
407 if (GetSaveFileName(&sfn
)!=0)
409 SaveDIBToFile(hBms
[currInd
], sfn
.lpstrFile
, hDrawingDC
);
412 CopyMemory(&filename
, sfn
.lpstrFileTitle
, 256);
413 CopyMemory(&filepathname
, sfn
.lpstrFileTitle
, 1000);
414 LoadString(hProgInstance
, IDS_WINDOWTITLE
, (LPTSTR
)&resstr
, 100);
415 sprintf(tempstr
, resstr
, &filename
);
416 SetWindowText(hMainWnd
, tempstr
);
422 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
426 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
429 OpenClipboard(hMainWnd
);
431 SetClipboardData(CF_BITMAP
, CopyImage(hSelBm
, IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
435 OpenClipboard(hMainWnd
);
436 if (GetClipboardData(CF_BITMAP
)!=NULL
)
438 DeleteObject(SelectObject(hSelDC
, hSelBm
= CopyImage(GetClipboardData(CF_BITMAP
), IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
)));
440 rectSel_src
[0] = rectSel_src
[1] = rectSel_src
[2] = rectSel_src
[3] = 0;
441 rectSel_dest
[0] = rectSel_dest
[1] = 0;
442 rectSel_dest
[2] = GetDIBWidth(hSelBm
);
443 rectSel_dest
[3] = GetDIBHeight(hSelBm
);
444 BitBlt(hDrawingDC
, rectSel_dest
[0], rectSel_dest
[1], rectSel_dest
[2], rectSel_dest
[3], hSelDC
, 0, 0, SRCCOPY
);
446 ShowWindow(hSelection
, SW_SHOW
);
450 case IDM_EDITDELETESELECTION
:
451 ShowWindow(hSelection
, SW_HIDE
);
453 case IDM_EDITSELECTALL
:
456 startPainting(hDrawingDC
, 0, 0, fgColor
, bgColor
);
457 whilePainting(hDrawingDC
, imgXRes
, imgYRes
, fgColor
, bgColor
);
458 endPainting(hDrawingDC
, imgXRes
, imgYRes
, fgColor
, bgColor
);
462 if (GetSaveFileName(&ofn
)!=0) SaveDIBToFile(hSelBm
, ofn
.lpstrFile
, hDrawingDC
);
464 case IDM_COLORSEDITPALETTE
:
465 if (ChooseColor(&choosecolor
))
467 fgColor
= choosecolor
.rgbResult
;
468 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
471 case IDM_IMAGEINVERTCOLORS
:
474 int tempRect
[4] = {0, 0, imgXRes
, imgYRes
};
475 InvertRect(hDrawingDC
, (LPRECT
)tempRect
);
476 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
479 case IDM_IMAGEDELETEIMAGE
:
481 Rect(hDrawingDC
, 0, 0, imgXRes
, imgYRes
, bgColor
, bgColor
, 0, TRUE
);
482 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
484 case IDM_IMAGEROTATEMIRROR
:
485 switch (mirrorRotateDlg())
489 StretchBlt(hDrawingDC
, imgXRes
-1, 0, -imgXRes
, imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
490 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
494 StretchBlt(hDrawingDC
, 0, imgYRes
-1, imgXRes
, -imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
495 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
499 StretchBlt(hDrawingDC
, imgXRes
-1, imgYRes
-1, -imgXRes
, -imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
500 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
504 case IDM_IMAGEATTRIBUTES
:
506 int attrdata
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
508 //cropReversible(640, 200);
512 case IDM_IMAGECHANGESIZE
:
514 int retVal
= changeSizeDlg();
515 if ((LOWORD(retVal
)!=0)&&(HIWORD(retVal
)!=0))
517 insertReversible(CopyImage(hBms
[currInd
], IMAGE_BITMAP
, imgXRes
*LOWORD(retVal
)/100, imgYRes
*HIWORD(retVal
)/100, 0));
518 updateCanvasAndScrollbars();
522 case IDM_IMAGEDRAWOPAQUE
:
523 transpBg
= 1-transpBg
;
524 SendMessage(hToolSettings
, WM_PAINT
, 0, 0);
527 insertReversible(CopyImage(hSelBm
, IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
528 updateCanvasAndScrollbars();
531 case IDM_VIEWZOOM125
:
540 case IDM_VIEWZOOM100
:
543 case IDM_VIEWZOOM200
:
546 case IDM_VIEWZOOM400
:
549 case IDM_VIEWZOOM800
:
603 return DefWindowProc (hwnd
, message
, wParam
, lParam
);