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>
17 #include "definitions.h"
18 #include "globalvar.h"
26 /* FUNCTIONS ********************************************************/
28 void selectTool(int tool
)
30 ShowWindow(hSelection
, SW_HIDE
);
32 SendMessage(hToolSettings
, WM_PAINT
, 0, 0);
35 void updateCanvasAndScrollbars()
37 ShowWindow(hSelection
, SW_HIDE
);
38 MoveWindow(hImageArea
, 3, 3, imgXRes
*zoom
/1000, imgYRes
*zoom
/1000, FALSE
);
39 InvalidateRect(hScrollbox
, NULL
, TRUE
);
40 InvalidateRect(hImageArea
, NULL
, FALSE
);
42 SetScrollPos(hScrollbox
, SB_HORZ
, 0, TRUE
);
43 SetScrollPos(hScrollbox
, SB_VERT
, 0, TRUE
);
46 void ZoomTo(int newZoom
)
49 updateCanvasAndScrollbars();
55 LRESULT CALLBACK
WindowProcedure (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
57 switch (message
) /* handle the messages */
60 PostQuitMessage (0); /* send a WM_QUIT to the message queue */
65 TCHAR programname
[20];
66 TCHAR saveprompttext
[100];
67 LoadString(hProgInstance
, IDS_PROGRAMNAME
, programname
, SIZEOF(programname
));
68 LoadString(hProgInstance
, IDS_SAVEPROMPTTEXT
, saveprompttext
, SIZEOF(saveprompttext
));
70 _stprintf(temptext
, saveprompttext
, filename
);
71 switch (MessageBox(hwnd
, temptext
, programname
, MB_YESNOCANCEL
| MB_ICONQUESTION
))
77 SendMessage(hwnd
, WM_COMMAND
, IDM_FILESAVEAS
, 0);
87 case WM_INITMENUPOPUP
:
93 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERPLANE
, MF_ENABLED
| MF_BYCOMMAND
);
94 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERCENTERED
, MF_ENABLED
| MF_BYCOMMAND
);
95 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERSTRETCHED
, MF_ENABLED
| MF_BYCOMMAND
);
99 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERPLANE
, MF_GRAYED
| MF_BYCOMMAND
);
100 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERCENTERED
, MF_GRAYED
| MF_BYCOMMAND
);
101 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERSTRETCHED
, MF_GRAYED
| MF_BYCOMMAND
);
106 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITUNDO
, MF_ENABLED
| MF_BYCOMMAND
);
108 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITUNDO
, MF_GRAYED
| MF_BYCOMMAND
);
110 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITREDO
, MF_ENABLED
| MF_BYCOMMAND
);
112 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITREDO
, MF_GRAYED
| MF_BYCOMMAND
);
113 if (IsWindowVisible(hSelection
))
115 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCUT
, MF_ENABLED
| MF_BYCOMMAND
);
116 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPY
, MF_ENABLED
| MF_BYCOMMAND
);
117 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITDELETESELECTION
, MF_ENABLED
| MF_BYCOMMAND
);
118 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITINVERTSELECTION
, MF_ENABLED
| MF_BYCOMMAND
);
119 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPYTO
, MF_ENABLED
| MF_BYCOMMAND
);
123 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCUT
, MF_GRAYED
| MF_BYCOMMAND
);
124 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPY
, MF_GRAYED
| MF_BYCOMMAND
);
125 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITDELETESELECTION
, MF_GRAYED
| MF_BYCOMMAND
);
126 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITINVERTSELECTION
, MF_GRAYED
| MF_BYCOMMAND
);
127 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPYTO
, MF_GRAYED
| MF_BYCOMMAND
);
129 OpenClipboard(hMainWnd
);
130 if (GetClipboardData(CF_BITMAP
)!=NULL
)
131 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITPASTE
, MF_ENABLED
| MF_BYCOMMAND
);
133 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITPASTE
, MF_GRAYED
| MF_BYCOMMAND
);
137 if (IsWindowVisible(hSelection
))
138 EnableMenuItem(GetMenu(hMainWnd
), IDM_IMAGECROP
, MF_ENABLED
| MF_BYCOMMAND
);
140 EnableMenuItem(GetMenu(hMainWnd
), IDM_IMAGECROP
, MF_GRAYED
| MF_BYCOMMAND
);
142 CheckMenuItem(GetMenu(hMainWnd
), IDM_IMAGEDRAWOPAQUE
, MF_CHECKED
| MF_BYCOMMAND
);
144 CheckMenuItem(GetMenu(hMainWnd
), IDM_IMAGEDRAWOPAQUE
, MF_UNCHECKED
| MF_BYCOMMAND
);
151 int test
[] = {LOWORD(lParam
)-260, LOWORD(lParam
)-140, LOWORD(lParam
)-20};
152 SendMessage(hStatusBar
, WM_SIZE
, wParam
, lParam
);
153 SendMessage(hStatusBar
, SB_SETPARTS
, 3, (int)&test
);
154 MoveWindow(hScrollbox
, 56, 49,LOWORD(lParam
)-56, HIWORD(lParam
)-72, TRUE
);
155 //InvalidateRect(hwnd, NULL, TRUE);
157 if ((hwnd
==hImageArea
)||(hwnd
==hScrollbox
))
159 long clientRectScrollbox
[4];
160 GetClientRect(hScrollbox
, (LPRECT
)&clientRectScrollbox
);
161 long clientRectImageArea
[4];
162 GetClientRect(hImageArea
, (LPRECT
)&clientRectImageArea
);
163 MoveWindow(hScrlClient
, 0, 0, max(clientRectImageArea
[2]+6, clientRectScrollbox
[2]), max(clientRectImageArea
[3]+6, clientRectScrollbox
[3]), TRUE
);
164 SCROLLINFO horzScroll
;
165 horzScroll
.cbSize
= sizeof(SCROLLINFO
);
166 horzScroll
.fMask
= SIF_PAGE
| SIF_RANGE
;
167 horzScroll
.nMax
= 10000;
169 horzScroll
.nPage
= clientRectScrollbox
[2]*10000/(clientRectImageArea
[2]+6);
171 horzScroll
.nTrackPos
= 0;
172 SetScrollInfo(hScrollbox
, SB_HORZ
, &horzScroll
, TRUE
);
173 GetClientRect(hScrollbox
, (LPRECT
)clientRectScrollbox
);
174 SCROLLINFO vertScroll
;
175 vertScroll
.cbSize
= sizeof(SCROLLINFO
);
176 vertScroll
.fMask
= SIF_PAGE
| SIF_RANGE
;
177 vertScroll
.nMax
= 10000;
179 vertScroll
.nPage
= clientRectScrollbox
[3]*10000/(clientRectImageArea
[3]+6);
181 vertScroll
.nTrackPos
= 0;
182 SetScrollInfo(hScrollbox
, SB_VERT
, &vertScroll
, TRUE
);
186 if (hwnd
==hScrollbox
)
188 if ((LOWORD(wParam
)==SB_THUMBPOSITION
)||(LOWORD(wParam
)==SB_THUMBTRACK
))
190 SetScrollPos(hScrollbox
, SB_HORZ
, HIWORD(wParam
), TRUE
);
191 MoveWindow(hScrlClient
, -(imgXRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_HORZ
)/10000,
192 -(imgYRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_VERT
)/10000, imgXRes
*zoom
/1000+6, imgYRes
*zoom
/1000+6, TRUE
);
197 if (hwnd
==hScrollbox
)
199 if ((LOWORD(wParam
)==SB_THUMBPOSITION
)||(LOWORD(wParam
)==SB_THUMBTRACK
))
201 SetScrollPos(hScrollbox
, SB_VERT
, HIWORD(wParam
), TRUE
);
202 MoveWindow(hScrlClient
, -(imgXRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_HORZ
)/10000,
203 -(imgYRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_VERT
)/10000, imgXRes
*zoom
/1000+6, imgYRes
*zoom
/1000+6, TRUE
);
207 case WM_GETMINMAXINFO
:
210 MINMAXINFO
*mm
= (LPMINMAXINFO
)lParam
;
211 (*mm
).ptMinTrackSize
.x
= 330;
212 (*mm
).ptMinTrackSize
.y
= 430;
216 DefWindowProc (hwnd
, message
, wParam
, lParam
);
217 if (hwnd
==hImageArea
)
219 HDC hdc
= GetDC(hImageArea
);
220 StretchBlt(hdc
, 0, 0, imgXRes
*zoom
/1000, imgYRes
*zoom
/1000, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
221 ReleaseDC(hImageArea
, hdc
);
222 SendMessage(hSelection
, WM_PAINT
, 0, 0);
226 // mouse events used for drawing
229 if (hwnd
==hImageArea
)
237 SetCursor(hCurColor
);
246 SetCursor(hCurAirbrush
);
249 SetCursor(LoadCursor(NULL
, IDC_CROSS
));
251 } else DefWindowProc(hwnd
, message
, wParam
, lParam
);
254 if (hwnd
==hImageArea
)
256 if ((!drawing
)||(activeTool
==5))
258 SetCapture(hImageArea
);
260 startPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
263 SendMessage(hwnd
, WM_LBUTTONUP
, wParam
, lParam
);
266 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
267 if ((activeTool
==6)&&(zoom
<8000)) ZoomTo(zoom
*2);
271 if (hwnd
==hImageArea
)
273 if ((!drawing
)||(activeTool
==5))
275 SetCapture(hImageArea
);
277 startPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
280 SendMessage(hwnd
, WM_RBUTTONUP
, wParam
, lParam
);
283 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
284 if ((activeTool
==6)&&(zoom
>125)) ZoomTo(zoom
/2);
288 if ((hwnd
==hImageArea
)&&drawing
)
292 endPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
293 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
296 int tempColor
= GetPixel(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
);
297 if (tempColor
!=CLR_INVALID
) fgColor
= tempColor
;
298 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
300 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)"");
304 if ((hwnd
==hImageArea
)&&drawing
)
308 endPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
309 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
312 int tempColor
= GetPixel(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
);
313 if (tempColor
!=CLR_INVALID
) bgColor
= tempColor
;
314 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
316 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)"");
320 if (hwnd
==hImageArea
)
322 if ((!drawing
)||(activeTool
<=9))
325 _stprintf(coordStr
, _T("%d, %d"), (short)LOWORD(lParam
)*1000/zoom
, (short)HIWORD(lParam
)*1000/zoom
);
326 SendMessage(hStatusBar
, SB_SETTEXT
, 1, (LPARAM
)coordStr
);
330 if ((wParam
&MK_LBUTTON
)!=0)
332 whilePainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
333 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
334 if ((activeTool
>=10)||(activeTool
==2))
337 _stprintf(sizeStr
, _T("%d x %d"), (short)LOWORD(lParam
)*1000/zoom
-startX
, (short)HIWORD(lParam
)*1000/zoom
-startY
);
338 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)sizeStr
);
341 if ((wParam
&MK_RBUTTON
)!=0)
343 whilePainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
344 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
348 _stprintf(sizeStr
, _T("%d x %d"), (short)LOWORD(lParam
)*1000/zoom
-startX
, (short)HIWORD(lParam
)*1000/zoom
-startY
);
349 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)sizeStr
);
355 SendMessage(hStatusBar
, SB_SETTEXT
, 1, (LPARAM
)_T(""));
359 // menu and button events
362 switch (LOWORD(wParam
))
366 HICON paintIcon
= LoadIcon(hProgInstance
, MAKEINTRESOURCE(IDI_APPICON
));
367 TCHAR infotitle
[100];
369 LoadString(hProgInstance
, IDS_INFOTITLE
, infotitle
, SIZEOF(infotitle
));
370 LoadString(hProgInstance
, IDS_INFOTEXT
, infotext
, SIZEOF(infotext
));
371 ShellAbout(hMainWnd
, infotitle
, infotext
, paintIcon
);
372 DeleteObject(paintIcon
);
375 case IDM_HELPHELPTOPICS
:
376 //HtmlHelp(hMainWnd, "help\\Paint.chm", 0, 0);
379 SendMessage(hwnd
, WM_CLOSE
, wParam
, lParam
);
382 Rectangle(hDrawingDC
, 0-1, 0-1, imgXRes
+1, imgYRes
+1);
383 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
386 if (GetOpenFileName(&ofn
)!=0)
388 HBITMAP bmNew
= (HBITMAP
)LoadDIBFromFile(ofn
.lpstrFile
);
391 insertReversible(bmNew
);
392 updateCanvasAndScrollbars();
395 CopyMemory(filename
, ofn
.lpstrFileTitle
, sizeof(filename
));
396 CopyMemory(filepathname
, ofn
.lpstrFileTitle
, sizeof(filepathname
));
397 LoadString(hProgInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
398 _stprintf(tempstr
, resstr
, filename
);
399 SetWindowText(hMainWnd
, tempstr
);
407 SaveDIBToFile(hBms
[currInd
], filepathname
, hDrawingDC
);
409 SendMessage(hwnd
, WM_COMMAND
, IDM_FILESAVEAS
, 0);
412 if (GetSaveFileName(&sfn
)!=0)
414 SaveDIBToFile(hBms
[currInd
], sfn
.lpstrFile
, hDrawingDC
);
417 CopyMemory(filename
, sfn
.lpstrFileTitle
, sizeof(filename
));
418 CopyMemory(filepathname
, sfn
.lpstrFileTitle
, sizeof(filepathname
));
419 LoadString(hProgInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
420 _stprintf(tempstr
, resstr
, filename
);
421 SetWindowText(hMainWnd
, tempstr
);
425 case IDM_FILEASWALLPAPERPLANE
:
426 setWallpaper(filepathname
, 2);
428 case IDM_FILEASWALLPAPERCENTERED
:
429 setWallpaper(filepathname
, 1);
431 case IDM_FILEASWALLPAPERSTRETCHED
:
432 setWallpaper(filepathname
, 0);
436 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
440 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
443 OpenClipboard(hMainWnd
);
445 SetClipboardData(CF_BITMAP
, CopyImage(hSelBm
, IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
449 OpenClipboard(hMainWnd
);
450 if (GetClipboardData(CF_BITMAP
)!=NULL
)
452 DeleteObject(SelectObject(hSelDC
, hSelBm
= CopyImage(GetClipboardData(CF_BITMAP
), IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
)));
454 rectSel_src
[0] = rectSel_src
[1] = rectSel_src
[2] = rectSel_src
[3] = 0;
455 rectSel_dest
[0] = rectSel_dest
[1] = 0;
456 rectSel_dest
[2] = GetDIBWidth(hSelBm
);
457 rectSel_dest
[3] = GetDIBHeight(hSelBm
);
458 BitBlt(hDrawingDC
, rectSel_dest
[0], rectSel_dest
[1], rectSel_dest
[2], rectSel_dest
[3], hSelDC
, 0, 0, SRCCOPY
);
460 ShowWindow(hSelection
, SW_SHOW
);
464 case IDM_EDITDELETESELECTION
:
465 ShowWindow(hSelection
, SW_HIDE
);
467 case IDM_EDITSELECTALL
:
470 startPainting(hDrawingDC
, 0, 0, fgColor
, bgColor
);
471 whilePainting(hDrawingDC
, imgXRes
, imgYRes
, fgColor
, bgColor
);
472 endPainting(hDrawingDC
, imgXRes
, imgYRes
, fgColor
, bgColor
);
476 if (GetSaveFileName(&ofn
)!=0) SaveDIBToFile(hSelBm
, ofn
.lpstrFile
, hDrawingDC
);
478 case IDM_COLORSEDITPALETTE
:
479 if (ChooseColor(&choosecolor
))
481 fgColor
= choosecolor
.rgbResult
;
482 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
485 case IDM_IMAGEINVERTCOLORS
:
488 int tempRect
[4] = {0, 0, imgXRes
, imgYRes
};
489 InvertRect(hDrawingDC
, (LPRECT
)tempRect
);
490 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
493 case IDM_IMAGEDELETEIMAGE
:
495 Rect(hDrawingDC
, 0, 0, imgXRes
, imgYRes
, bgColor
, bgColor
, 0, TRUE
);
496 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
498 case IDM_IMAGEROTATEMIRROR
:
499 switch (mirrorRotateDlg())
503 StretchBlt(hDrawingDC
, imgXRes
-1, 0, -imgXRes
, imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
504 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
508 StretchBlt(hDrawingDC
, 0, imgYRes
-1, imgXRes
, -imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
509 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
513 StretchBlt(hDrawingDC
, imgXRes
-1, imgYRes
-1, -imgXRes
, -imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
514 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
518 case IDM_IMAGEATTRIBUTES
:
520 int retVal
= attributesDlg();
521 if ((LOWORD(retVal
)!=0)&&(HIWORD(retVal
)!=0))
523 // cropReversible broken, dirty hack:
524 // insertReversible(CopyImage(hBms[currInd], IMAGE_BITMAP, LOWORD(retVal), HIWORD(retVal), 0));
525 cropReversible(LOWORD(retVal
), HIWORD(retVal
));
526 updateCanvasAndScrollbars();
530 case IDM_IMAGECHANGESIZE
:
532 int retVal
= changeSizeDlg();
533 if ((LOWORD(retVal
)!=0)&&(HIWORD(retVal
)!=0))
535 insertReversible(CopyImage(hBms
[currInd
], IMAGE_BITMAP
, imgXRes
*LOWORD(retVal
)/100, imgYRes
*HIWORD(retVal
)/100, 0));
536 updateCanvasAndScrollbars();
540 case IDM_IMAGEDRAWOPAQUE
:
541 transpBg
= 1-transpBg
;
542 SendMessage(hToolSettings
, WM_PAINT
, 0, 0);
545 insertReversible(CopyImage(hSelBm
, IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
546 updateCanvasAndScrollbars();
549 case IDM_VIEWZOOM125
:
558 case IDM_VIEWZOOM100
:
561 case IDM_VIEWZOOM200
:
564 case IDM_VIEWZOOM400
:
567 case IDM_VIEWZOOM800
:
621 return DefWindowProc (hwnd
, message
, wParam
, lParam
);