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"
24 /* FUNCTIONS ********************************************************/
26 void selectTool(int tool
)
28 ShowWindow(hSelection
, SW_HIDE
);
30 SendMessage(hToolSettings
, WM_PAINT
, 0, 0);
33 void updateCanvasAndScrollbars()
35 ShowWindow(hSelection
, SW_HIDE
);
36 MoveWindow(hImageArea
, 3, 3, imgXRes
*zoom
/1000, imgYRes
*zoom
/1000, FALSE
);
37 InvalidateRect(hScrollbox
, NULL
, TRUE
);
38 InvalidateRect(hImageArea
, NULL
, FALSE
);
40 SetScrollPos(hScrollbox
, SB_HORZ
, 0, TRUE
);
41 SetScrollPos(hScrollbox
, SB_VERT
, 0, TRUE
);
44 void ZoomTo(int newZoom
)
47 updateCanvasAndScrollbars();
53 LRESULT CALLBACK
WindowProcedure (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
55 switch (message
) /* handle the messages */
58 PostQuitMessage (0); /* send a WM_QUIT to the message queue */
64 char saveprompttext
[100];
65 LoadString(hProgInstance
, IDS_PROGRAMNAME
, (LPTSTR
)programname
, sizeof(programname
));
66 LoadString(hProgInstance
, IDS_SAVEPROMPTTEXT
, (LPTSTR
)saveprompttext
, sizeof(saveprompttext
));
68 sprintf(temptext
, saveprompttext
, filename
);
69 switch (MessageBox(hwnd
, (LPTSTR
)temptext
, (LPTSTR
)programname
, MB_YESNOCANCEL
| MB_ICONQUESTION
))
75 SendMessage(hwnd
, WM_COMMAND
, IDM_FILESAVEAS
, 0);
85 case WM_INITMENUPOPUP
:
91 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERPLANE
, MF_ENABLED
| MF_BYCOMMAND
);
92 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERCENTERED
, MF_ENABLED
| MF_BYCOMMAND
);
93 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERSTRETCHED
, MF_ENABLED
| MF_BYCOMMAND
);
97 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERPLANE
, MF_GRAYED
| MF_BYCOMMAND
);
98 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERCENTERED
, MF_GRAYED
| MF_BYCOMMAND
);
99 EnableMenuItem(GetMenu(hMainWnd
), IDM_FILEASWALLPAPERSTRETCHED
, MF_GRAYED
| MF_BYCOMMAND
);
104 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITUNDO
, MF_ENABLED
| MF_BYCOMMAND
);
106 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITUNDO
, MF_GRAYED
| MF_BYCOMMAND
);
108 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITREDO
, MF_ENABLED
| MF_BYCOMMAND
);
110 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITREDO
, MF_GRAYED
| MF_BYCOMMAND
);
111 if (IsWindowVisible(hSelection
))
113 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCUT
, MF_ENABLED
| MF_BYCOMMAND
);
114 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPY
, MF_ENABLED
| MF_BYCOMMAND
);
115 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITDELETESELECTION
, MF_ENABLED
| MF_BYCOMMAND
);
116 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITINVERTSELECTION
, MF_ENABLED
| MF_BYCOMMAND
);
117 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPYTO
, MF_ENABLED
| MF_BYCOMMAND
);
121 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCUT
, MF_GRAYED
| MF_BYCOMMAND
);
122 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPY
, MF_GRAYED
| MF_BYCOMMAND
);
123 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITDELETESELECTION
, MF_GRAYED
| MF_BYCOMMAND
);
124 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITINVERTSELECTION
, MF_GRAYED
| MF_BYCOMMAND
);
125 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITCOPYTO
, MF_GRAYED
| MF_BYCOMMAND
);
127 OpenClipboard(hMainWnd
);
128 if (GetClipboardData(CF_BITMAP
)!=NULL
)
129 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITPASTE
, MF_ENABLED
| MF_BYCOMMAND
);
131 EnableMenuItem(GetMenu(hMainWnd
), IDM_EDITPASTE
, MF_GRAYED
| MF_BYCOMMAND
);
135 if (IsWindowVisible(hSelection
))
136 EnableMenuItem(GetMenu(hMainWnd
), IDM_IMAGECROP
, MF_ENABLED
| MF_BYCOMMAND
);
138 EnableMenuItem(GetMenu(hMainWnd
), IDM_IMAGECROP
, MF_GRAYED
| MF_BYCOMMAND
);
140 CheckMenuItem(GetMenu(hMainWnd
), IDM_IMAGEDRAWOPAQUE
, MF_CHECKED
| MF_BYCOMMAND
);
142 CheckMenuItem(GetMenu(hMainWnd
), IDM_IMAGEDRAWOPAQUE
, MF_UNCHECKED
| MF_BYCOMMAND
);
149 int test
[] = {LOWORD(lParam
)-260, LOWORD(lParam
)-140, LOWORD(lParam
)-20};
150 SendMessage(hStatusBar
, WM_SIZE
, wParam
, lParam
);
151 SendMessage(hStatusBar
, SB_SETPARTS
, 3, (int)&test
);
152 MoveWindow(hScrollbox
, 56, 49,LOWORD(lParam
)-56, HIWORD(lParam
)-72, TRUE
);
153 //InvalidateRect(hwnd, NULL, TRUE);
155 if ((hwnd
==hImageArea
)||(hwnd
==hScrollbox
))
157 long clientRectScrollbox
[4];
158 GetClientRect(hScrollbox
, (LPRECT
)&clientRectScrollbox
);
159 long clientRectImageArea
[4];
160 GetClientRect(hImageArea
, (LPRECT
)&clientRectImageArea
);
161 MoveWindow(hScrlClient
, 0, 0, max(clientRectImageArea
[2]+6, clientRectScrollbox
[2]), max(clientRectImageArea
[3]+6, clientRectScrollbox
[3]), TRUE
);
162 SCROLLINFO horzScroll
;
163 horzScroll
.cbSize
= sizeof(SCROLLINFO
);
164 horzScroll
.fMask
= SIF_PAGE
| SIF_RANGE
;
165 horzScroll
.nMax
= 10000;
167 horzScroll
.nPage
= clientRectScrollbox
[2]*10000/(clientRectImageArea
[2]+6);
169 horzScroll
.nTrackPos
= 0;
170 SetScrollInfo(hScrollbox
, SB_HORZ
, &horzScroll
, TRUE
);
171 GetClientRect(hScrollbox
, (LPRECT
)clientRectScrollbox
);
172 SCROLLINFO vertScroll
;
173 vertScroll
.cbSize
= sizeof(SCROLLINFO
);
174 vertScroll
.fMask
= SIF_PAGE
| SIF_RANGE
;
175 vertScroll
.nMax
= 10000;
177 vertScroll
.nPage
= clientRectScrollbox
[3]*10000/(clientRectImageArea
[3]+6);
179 vertScroll
.nTrackPos
= 0;
180 SetScrollInfo(hScrollbox
, SB_VERT
, &vertScroll
, TRUE
);
184 if (hwnd
==hScrollbox
)
186 if ((LOWORD(wParam
)==SB_THUMBPOSITION
)||(LOWORD(wParam
)==SB_THUMBTRACK
))
188 SetScrollPos(hScrollbox
, SB_HORZ
, HIWORD(wParam
), TRUE
);
189 MoveWindow(hScrlClient
, -(imgXRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_HORZ
)/10000,
190 -(imgYRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_VERT
)/10000, imgXRes
*zoom
/1000+6, imgYRes
*zoom
/1000+6, TRUE
);
195 if (hwnd
==hScrollbox
)
197 if ((LOWORD(wParam
)==SB_THUMBPOSITION
)||(LOWORD(wParam
)==SB_THUMBTRACK
))
199 SetScrollPos(hScrollbox
, SB_VERT
, HIWORD(wParam
), TRUE
);
200 MoveWindow(hScrlClient
, -(imgXRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_HORZ
)/10000,
201 -(imgYRes
*zoom
/1000+6)*GetScrollPos(hScrollbox
, SB_VERT
)/10000, imgXRes
*zoom
/1000+6, imgYRes
*zoom
/1000+6, TRUE
);
205 case WM_GETMINMAXINFO
:
208 MINMAXINFO
*mm
= (LPMINMAXINFO
)lParam
;
209 (*mm
).ptMinTrackSize
.x
= 330;
210 (*mm
).ptMinTrackSize
.y
= 430;
214 DefWindowProc (hwnd
, message
, wParam
, lParam
);
215 if (hwnd
==hImageArea
)
217 HDC hdc
= GetDC(hImageArea
);
218 StretchBlt(hdc
, 0, 0, imgXRes
*zoom
/1000, imgYRes
*zoom
/1000, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
219 ReleaseDC(hImageArea
, hdc
);
220 SendMessage(hSelection
, WM_PAINT
, 0, 0);
224 // mouse events used for drawing
227 if (hwnd
==hImageArea
)
235 SetCursor(hCurColor
);
244 SetCursor(hCurAirbrush
);
247 SetCursor(LoadCursor(NULL
, IDC_CROSS
));
249 } else DefWindowProc(hwnd
, message
, wParam
, lParam
);
252 if (hwnd
==hImageArea
)
254 if ((!drawing
)||(activeTool
==5))
256 SetCapture(hImageArea
);
258 startPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
261 SendMessage(hwnd
, WM_LBUTTONUP
, wParam
, lParam
);
264 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
265 if ((activeTool
==6)&&(zoom
<8000)) ZoomTo(zoom
*2);
269 if (hwnd
==hImageArea
)
271 if ((!drawing
)||(activeTool
==5))
273 SetCapture(hImageArea
);
275 startPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
278 SendMessage(hwnd
, WM_RBUTTONUP
, wParam
, lParam
);
281 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
282 if ((activeTool
==6)&&(zoom
>125)) ZoomTo(zoom
/2);
286 if ((hwnd
==hImageArea
)&&drawing
)
290 endPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
291 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
294 int tempColor
= GetPixel(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
);
295 if (tempColor
!=CLR_INVALID
) fgColor
= tempColor
;
296 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
298 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)"");
302 if ((hwnd
==hImageArea
)&&drawing
)
306 endPainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
307 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
310 int tempColor
= GetPixel(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
);
311 if (tempColor
!=CLR_INVALID
) bgColor
= tempColor
;
312 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
314 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)"");
318 if (hwnd
==hImageArea
)
320 if ((!drawing
)||(activeTool
<=9))
323 sprintf(coordStr
, "%d, %d", (short)LOWORD(lParam
)*1000/zoom
, (short)HIWORD(lParam
)*1000/zoom
);
324 SendMessage(hStatusBar
, SB_SETTEXT
, 1, (LPARAM
)coordStr
);
328 if ((wParam
&MK_LBUTTON
)!=0)
330 whilePainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, fgColor
, bgColor
);
331 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
332 if ((activeTool
>=10)||(activeTool
==2))
335 sprintf(sizeStr
, "%d x %d", (short)LOWORD(lParam
)*1000/zoom
-startX
, (short)HIWORD(lParam
)*1000/zoom
-startY
);
336 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)sizeStr
);
339 if ((wParam
&MK_RBUTTON
)!=0)
341 whilePainting(hDrawingDC
, LOWORD(lParam
)*1000/zoom
, HIWORD(lParam
)*1000/zoom
, bgColor
, fgColor
);
342 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
346 sprintf(sizeStr
, "%d x %d", (short)LOWORD(lParam
)*1000/zoom
-startX
, (short)HIWORD(lParam
)*1000/zoom
-startY
);
347 SendMessage(hStatusBar
, SB_SETTEXT
, 2, (LPARAM
)sizeStr
);
353 SendMessage(hStatusBar
, SB_SETTEXT
, 1, (LPARAM
)"");
357 // menu and button events
360 switch (LOWORD(wParam
))
364 HICON paintIcon
= LoadIcon(hProgInstance
, MAKEINTRESOURCE(IDI_APPICON
));
367 LoadString(hProgInstance
, IDS_INFOTITLE
, (LPTSTR
)infotitle
, sizeof(infotitle
));
368 LoadString(hProgInstance
, IDS_INFOTEXT
, (LPTSTR
)infotext
, sizeof(infotext
));
369 ShellAbout(hMainWnd
, (LPTSTR
)infotitle
, (LPTSTR
)infotext
, paintIcon
);
370 DeleteObject(paintIcon
);
373 case IDM_HELPHELPTOPICS
:
374 //HtmlHelp(hMainWnd, "help\\Paint.chm", 0, 0);
377 SendMessage(hwnd
, WM_CLOSE
, wParam
, lParam
);
380 Rectangle(hDrawingDC
, 0-1, 0-1, imgXRes
+1, imgYRes
+1);
381 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
384 if (GetOpenFileName(&ofn
)!=0)
386 HBITMAP bmNew
= (HBITMAP
)LoadDIBFromFile(ofn
.lpstrFile
);
389 insertReversible(bmNew
);
390 updateCanvasAndScrollbars();
393 CopyMemory(filename
, ofn
.lpstrFileTitle
, sizeof(filename
));
394 CopyMemory(filepathname
, ofn
.lpstrFileTitle
, sizeof(filepathname
));
395 LoadString(hProgInstance
, IDS_WINDOWTITLE
, (LPTSTR
)resstr
, sizeof(resstr
));
396 sprintf(tempstr
, resstr
, filename
);
397 SetWindowText(hMainWnd
, tempstr
);
405 SaveDIBToFile(hBms
[currInd
], filepathname
, hDrawingDC
);
407 SendMessage(hwnd
, WM_COMMAND
, IDM_FILESAVEAS
, 0);
410 if (GetSaveFileName(&sfn
)!=0)
412 SaveDIBToFile(hBms
[currInd
], sfn
.lpstrFile
, hDrawingDC
);
415 CopyMemory(filename
, sfn
.lpstrFileTitle
, sizeof(filename
));
416 CopyMemory(filepathname
, sfn
.lpstrFileTitle
, sizeof(filepathname
));
417 LoadString(hProgInstance
, IDS_WINDOWTITLE
, (LPTSTR
)resstr
, sizeof(resstr
));
418 sprintf(tempstr
, resstr
, filename
);
419 SetWindowText(hMainWnd
, tempstr
);
425 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
429 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
432 OpenClipboard(hMainWnd
);
434 SetClipboardData(CF_BITMAP
, CopyImage(hSelBm
, IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
438 OpenClipboard(hMainWnd
);
439 if (GetClipboardData(CF_BITMAP
)!=NULL
)
441 DeleteObject(SelectObject(hSelDC
, hSelBm
= CopyImage(GetClipboardData(CF_BITMAP
), IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
)));
443 rectSel_src
[0] = rectSel_src
[1] = rectSel_src
[2] = rectSel_src
[3] = 0;
444 rectSel_dest
[0] = rectSel_dest
[1] = 0;
445 rectSel_dest
[2] = GetDIBWidth(hSelBm
);
446 rectSel_dest
[3] = GetDIBHeight(hSelBm
);
447 BitBlt(hDrawingDC
, rectSel_dest
[0], rectSel_dest
[1], rectSel_dest
[2], rectSel_dest
[3], hSelDC
, 0, 0, SRCCOPY
);
449 ShowWindow(hSelection
, SW_SHOW
);
453 case IDM_EDITDELETESELECTION
:
454 ShowWindow(hSelection
, SW_HIDE
);
456 case IDM_EDITSELECTALL
:
459 startPainting(hDrawingDC
, 0, 0, fgColor
, bgColor
);
460 whilePainting(hDrawingDC
, imgXRes
, imgYRes
, fgColor
, bgColor
);
461 endPainting(hDrawingDC
, imgXRes
, imgYRes
, fgColor
, bgColor
);
465 if (GetSaveFileName(&ofn
)!=0) SaveDIBToFile(hSelBm
, ofn
.lpstrFile
, hDrawingDC
);
467 case IDM_COLORSEDITPALETTE
:
468 if (ChooseColor(&choosecolor
))
470 fgColor
= choosecolor
.rgbResult
;
471 SendMessage(hPalWin
, WM_PAINT
, 0, 0);
474 case IDM_IMAGEINVERTCOLORS
:
477 int tempRect
[4] = {0, 0, imgXRes
, imgYRes
};
478 InvertRect(hDrawingDC
, (LPRECT
)tempRect
);
479 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
482 case IDM_IMAGEDELETEIMAGE
:
484 Rect(hDrawingDC
, 0, 0, imgXRes
, imgYRes
, bgColor
, bgColor
, 0, TRUE
);
485 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
487 case IDM_IMAGEROTATEMIRROR
:
488 switch (mirrorRotateDlg())
492 StretchBlt(hDrawingDC
, imgXRes
-1, 0, -imgXRes
, imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
493 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
497 StretchBlt(hDrawingDC
, 0, imgYRes
-1, imgXRes
, -imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
498 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
502 StretchBlt(hDrawingDC
, imgXRes
-1, imgYRes
-1, -imgXRes
, -imgYRes
, hDrawingDC
, 0, 0, imgXRes
, imgYRes
, SRCCOPY
);
503 SendMessage(hImageArea
, WM_PAINT
, 0, 0);
507 case IDM_IMAGEATTRIBUTES
:
509 //int attrdata[8] = {0, 0, 0, 0, 0, 0, 0, 0};
511 //cropReversible(640, 200);
515 case IDM_IMAGECHANGESIZE
:
517 int retVal
= changeSizeDlg();
518 if ((LOWORD(retVal
)!=0)&&(HIWORD(retVal
)!=0))
520 insertReversible(CopyImage(hBms
[currInd
], IMAGE_BITMAP
, imgXRes
*LOWORD(retVal
)/100, imgYRes
*HIWORD(retVal
)/100, 0));
521 updateCanvasAndScrollbars();
525 case IDM_IMAGEDRAWOPAQUE
:
526 transpBg
= 1-transpBg
;
527 SendMessage(hToolSettings
, WM_PAINT
, 0, 0);
530 insertReversible(CopyImage(hSelBm
, IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
531 updateCanvasAndScrollbars();
534 case IDM_VIEWZOOM125
:
543 case IDM_VIEWZOOM100
:
546 case IDM_VIEWZOOM200
:
549 case IDM_VIEWZOOM400
:
552 case IDM_VIEWZOOM800
:
606 return DefWindowProc (hwnd
, message
, wParam
, lParam
);