2 * PROJECT: PAINT for ReactOS
4 * FILE: base/applications/mspaint_new/winproc.cpp
5 * PURPOSE: Window procedure of the main window and all children apart from
6 * hPalWin, hToolSettings and hSelection
7 * PROGRAMMERS: Benedikt Freisen
10 /* INCLUDES *********************************************************/
17 /* FUNCTIONS ********************************************************/
20 zoomTo(int newZoom
, int mouseX
, int mouseY
)
22 RECT clientRectScrollbox
;
23 RECT clientRectImageArea
;
25 scrollboxWindow
.GetClientRect(&clientRectScrollbox
);
26 imageArea
.GetClientRect(&clientRectImageArea
);
27 w
= clientRectImageArea
.right
* clientRectScrollbox
.right
/ (clientRectImageArea
.right
* newZoom
/ toolsModel
.GetZoom());
28 h
= clientRectImageArea
.bottom
* clientRectScrollbox
.bottom
/ (clientRectImageArea
.bottom
* newZoom
/ toolsModel
.GetZoom());
29 x
= max(0, min(clientRectImageArea
.right
- w
, mouseX
- w
/ 2)) * newZoom
/ toolsModel
.GetZoom();
30 y
= max(0, min(clientRectImageArea
.bottom
- h
, mouseY
- h
/ 2)) * newZoom
/ toolsModel
.GetZoom();
32 toolsModel
.SetZoom(newZoom
);
34 selectionWindow
.ShowWindow(SW_HIDE
);
35 imageArea
.MoveWindow(3, 3, imageModel
.GetWidth() * toolsModel
.GetZoom() / 1000, imageModel
.GetHeight() * toolsModel
.GetZoom() / 1000, FALSE
);
36 scrollboxWindow
.Invalidate(TRUE
);
37 imageArea
.Invalidate(FALSE
);
39 scrollboxWindow
.SendMessage(WM_HSCROLL
, MAKEWPARAM(SB_THUMBPOSITION
, x
), 0);
40 scrollboxWindow
.SendMessage(WM_VSCROLL
, MAKEWPARAM(SB_THUMBPOSITION
, y
), 0);
43 void CMainWindow::alignChildrenToMainWindow()
47 GetClientRect(&clientRect
);
49 if (toolBoxContainer
.IsWindowVisible())
52 w
= clientRect
.right
- 56;
59 if (paletteWindow
.IsWindowVisible())
62 h
= clientRect
.bottom
- 49;
67 h
= clientRect
.bottom
- 3;
70 scrollboxWindow
.MoveWindow(x
, y
, w
, ::IsWindowVisible(hStatusBar
) ? h
- 23 : h
, TRUE
);
71 paletteWindow
.MoveWindow(x
, 9, 255, 32, TRUE
);
74 void CMainWindow::saveImage(BOOL overwrite
)
76 if (isAFile
&& overwrite
)
78 imageModel
.SaveImage(filepathname
);
80 else if (GetSaveFileName(&sfn
) != 0)
84 imageModel
.SaveImage(sfn
.lpstrFile
);
85 CopyMemory(filename
, sfn
.lpstrFileTitle
, sizeof(filename
));
86 CopyMemory(filepathname
, sfn
.lpstrFile
, sizeof(filepathname
));
87 LoadString(hProgInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
88 _stprintf(tempstr
, resstr
, filename
);
89 SetWindowText(tempstr
);
94 void CMainWindow::UpdateApplicationProperties(HBITMAP bitmap
, LPTSTR newfilename
, LPTSTR newfilepathname
)
98 imageModel
.Insert(bitmap
);
99 CopyMemory(filename
, newfilename
, sizeof(filename
));
100 CopyMemory(filepathname
, newfilepathname
, sizeof(filepathname
));
101 LoadString(hProgInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
102 _stprintf(tempstr
, resstr
, filename
);
103 SetWindowText(tempstr
);
104 imageModel
.ClearHistory();
108 void CMainWindow::InsertSelectionFromHBITMAP(HBITMAP bitmap
, HWND window
)
110 HWND hToolbar
= FindWindowEx(toolBoxContainer
.m_hWnd
, NULL
, TOOLBARCLASSNAME
, NULL
);
111 SendMessage(hToolbar
, TB_CHECKBUTTON
, ID_RECTSEL
, MAKELPARAM(TRUE
, 0));
112 toolBoxContainer
.SendMessage(WM_COMMAND
, ID_RECTSEL
);
114 imageModel
.CopyPrevious();
115 selectionModel
.InsertFromHBITMAP(bitmap
);
118 selectionWindow
.ShowWindow(SW_SHOW
);
119 ForceRefreshSelectionContents();
122 LRESULT
CMainWindow::OnDropFiles(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
125 TCHAR droppedfile
[MAX_PATH
];
126 HBITMAP bmNew
= NULL
;
127 drophandle
= (HDROP
)wParam
;
128 DragQueryFile(drophandle
, 0, droppedfile
, SIZEOF(droppedfile
));
129 DragFinish(drophandle
);
130 LoadDIBFromFile(&bmNew
, droppedfile
, &fileTime
, &fileSize
, &fileHPPM
, &fileVPPM
);
134 pathend
= _tcsrchr(droppedfile
, '\\');
136 UpdateApplicationProperties(bmNew
, pathend
, pathend
);
141 LRESULT
CMainWindow::OnCreate(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
143 SendMessage(WM_SETICON
, ICON_BIG
, (LPARAM
) LoadIcon(hProgInstance
, MAKEINTRESOURCE(IDI_APPICON
)));
144 SendMessage(WM_SETICON
, ICON_SMALL
, (LPARAM
) LoadIcon(hProgInstance
, MAKEINTRESOURCE(IDI_APPICON
)));
148 LRESULT
CMainWindow::OnDestroy(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
150 PostQuitMessage(0); /* send a WM_QUIT to the message queue */
154 LRESULT
CMainWindow::OnClose(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
156 if (!imageModel
.IsImageSaved())
158 TCHAR programname
[20];
159 TCHAR saveprompttext
[100];
161 LoadString(hProgInstance
, IDS_PROGRAMNAME
, programname
, SIZEOF(programname
));
162 LoadString(hProgInstance
, IDS_SAVEPROMPTTEXT
, saveprompttext
, SIZEOF(saveprompttext
));
163 _stprintf(temptext
, saveprompttext
, filename
);
164 switch (MessageBox(temptext
, programname
, MB_YESNOCANCEL
| MB_ICONQUESTION
))
171 if (imageModel
.IsImageSaved())
183 LRESULT
CMainWindow::OnInitMenuPopup(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
185 HMENU menu
= GetMenu();
186 BOOL trueSelection
= (selectionWindow
.IsWindowVisible() && ((toolsModel
.GetActiveTool() == TOOL_FREESEL
) || (toolsModel
.GetActiveTool() == TOOL_RECTSEL
)));
189 case 0: /* File menu */
190 EnableMenuItem(menu
, IDM_FILEASWALLPAPERPLANE
, ENABLED_IF(isAFile
));
191 EnableMenuItem(menu
, IDM_FILEASWALLPAPERCENTERED
, ENABLED_IF(isAFile
));
192 EnableMenuItem(menu
, IDM_FILEASWALLPAPERSTRETCHED
, ENABLED_IF(isAFile
));
194 case 1: /* Edit menu */
195 EnableMenuItem(menu
, IDM_EDITUNDO
, ENABLED_IF(imageModel
.HasUndoSteps()));
196 EnableMenuItem(menu
, IDM_EDITREDO
, ENABLED_IF(imageModel
.HasRedoSteps()));
197 EnableMenuItem(menu
, IDM_EDITCUT
, ENABLED_IF(trueSelection
));
198 EnableMenuItem(menu
, IDM_EDITCOPY
, ENABLED_IF(trueSelection
));
199 EnableMenuItem(menu
, IDM_EDITDELETESELECTION
, ENABLED_IF(trueSelection
));
200 EnableMenuItem(menu
, IDM_EDITINVERTSELECTION
, ENABLED_IF(trueSelection
));
201 EnableMenuItem(menu
, IDM_EDITCOPYTO
, ENABLED_IF(trueSelection
));
203 EnableMenuItem(menu
, IDM_EDITPASTE
, ENABLED_IF(GetClipboardData(CF_BITMAP
) != NULL
));
206 case 2: /* View menu */
207 CheckMenuItem(menu
, IDM_VIEWTOOLBOX
, CHECKED_IF(toolBoxContainer
.IsWindowVisible()));
208 CheckMenuItem(menu
, IDM_VIEWCOLORPALETTE
, CHECKED_IF(paletteWindow
.IsWindowVisible()));
209 CheckMenuItem(menu
, IDM_VIEWSTATUSBAR
, CHECKED_IF(::IsWindowVisible(hStatusBar
)));
210 CheckMenuItem(menu
, IDM_FORMATICONBAR
, CHECKED_IF(textEditWindow
.IsWindowVisible()));
211 EnableMenuItem(menu
, IDM_FORMATICONBAR
, ENABLED_IF(toolsModel
.GetActiveTool() == TOOL_TEXT
));
213 CheckMenuItem(menu
, IDM_VIEWSHOWGRID
, CHECKED_IF(showGrid
));
214 CheckMenuItem(menu
, IDM_VIEWSHOWMINIATURE
, CHECKED_IF(showMiniature
));
216 case 3: /* Image menu */
217 EnableMenuItem(menu
, IDM_IMAGECROP
, ENABLED_IF(selectionWindow
.IsWindowVisible()));
218 CheckMenuItem(menu
, IDM_IMAGEDRAWOPAQUE
, CHECKED_IF(!toolsModel
.IsBackgroundTransparent()));
222 CheckMenuItem(menu
, IDM_VIEWZOOM125
, CHECKED_IF(toolsModel
.GetZoom() == 125));
223 CheckMenuItem(menu
, IDM_VIEWZOOM25
, CHECKED_IF(toolsModel
.GetZoom() == 250));
224 CheckMenuItem(menu
, IDM_VIEWZOOM50
, CHECKED_IF(toolsModel
.GetZoom() == 500));
225 CheckMenuItem(menu
, IDM_VIEWZOOM100
, CHECKED_IF(toolsModel
.GetZoom() == 1000));
226 CheckMenuItem(menu
, IDM_VIEWZOOM200
, CHECKED_IF(toolsModel
.GetZoom() == 2000));
227 CheckMenuItem(menu
, IDM_VIEWZOOM400
, CHECKED_IF(toolsModel
.GetZoom() == 4000));
228 CheckMenuItem(menu
, IDM_VIEWZOOM800
, CHECKED_IF(toolsModel
.GetZoom() == 8000));
230 CheckMenuItem(menu
, IDM_COLORSMODERNPALETTE
, CHECKED_IF(paletteModel
.SelectedPalette() == 1));
231 CheckMenuItem(menu
, IDM_COLORSOLDPALETTE
, CHECKED_IF(paletteModel
.SelectedPalette() == 2));
235 LRESULT
CMainWindow::OnSize(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
237 int test
[] = { LOWORD(lParam
) - 260, LOWORD(lParam
) - 140, LOWORD(lParam
) - 20 };
238 SendMessage(hStatusBar
, WM_SIZE
, wParam
, lParam
);
239 SendMessage(hStatusBar
, SB_SETPARTS
, 3, (LPARAM
)&test
);
240 alignChildrenToMainWindow();
245 LRESULT
CMainWindow::OnGetMinMaxInfo(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
247 MINMAXINFO
*mm
= (LPMINMAXINFO
) lParam
;
248 mm
->ptMinTrackSize
.x
= 330;
249 mm
->ptMinTrackSize
.y
= 430;
253 LRESULT
CMainWindow::OnSetCursor(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
255 SetCursor(LoadCursor(NULL
, IDC_ARROW
));
259 LRESULT
CMainWindow::OnKeyDown(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
261 if (wParam
== VK_ESCAPE
)
263 if (!imageArea
.drawing
)
266 if ((toolsModel
.GetActiveTool() == TOOL_RECTSEL
) || (toolsModel
.GetActiveTool() == TOOL_FREESEL
))
268 startPaintingL(hDrawingDC
, 0, 0, paletteModel
.GetFgColor(), paletteModel
.GetBgColor());
269 whilePaintingL(hDrawingDC
, 0, 0, paletteModel
.GetFgColor(), paletteModel
.GetBgColor());
270 endPaintingL(hDrawingDC
, 0, 0, paletteModel
.GetFgColor(), paletteModel
.GetBgColor());
271 selectionWindow
.ShowWindow(SW_HIDE
);
274 /* FIXME: also cancel current drawing underway */
279 LRESULT
CMainWindow::OnSysColorChange(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
281 /* Redirect message to common controls */
282 HWND hToolbar
= FindWindowEx(toolBoxContainer
.m_hWnd
, NULL
, TOOLBARCLASSNAME
, NULL
);
283 SendMessage(hToolbar
, WM_SYSCOLORCHANGE
, 0, 0);
287 LRESULT
CMainWindow::OnCommand(UINT nMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
289 switch (LOWORD(wParam
))
293 HICON paintIcon
= LoadIcon(hProgInstance
, MAKEINTRESOURCE(IDI_APPICON
));
294 TCHAR infotitle
[100];
296 LoadString(hProgInstance
, IDS_INFOTITLE
, infotitle
, SIZEOF(infotitle
));
297 LoadString(hProgInstance
, IDS_INFOTEXT
, infotext
, SIZEOF(infotext
));
298 ShellAbout(m_hWnd
, infotitle
, infotext
, paintIcon
);
299 DeleteObject(paintIcon
);
302 case IDM_HELPHELPTOPICS
:
303 HtmlHelp(m_hWnd
, _T("help\\Paint.chm"), 0, 0);
306 SendMessage(WM_CLOSE
, wParam
, lParam
);
311 if (!imageModel
.IsImageSaved())
313 TCHAR programname
[20];
314 TCHAR saveprompttext
[100];
316 LoadString(hProgInstance
, IDS_PROGRAMNAME
, programname
, SIZEOF(programname
));
317 LoadString(hProgInstance
, IDS_SAVEPROMPTTEXT
, saveprompttext
, SIZEOF(saveprompttext
));
318 _stprintf(temptext
, saveprompttext
, filename
);
319 switch (MessageBox(temptext
, programname
, MB_YESNOCANCEL
| MB_ICONQUESTION
))
322 imageModel
.imageSaved
= TRUE
; //TODO: move to ImageModel
332 if (reset
&& imageModel
.IsImageSaved()) //TODO: move to ImageModel
335 imageModel
.ClearHistory();
340 if (GetOpenFileName(&ofn
) != 0)
342 HBITMAP bmNew
= NULL
;
343 LoadDIBFromFile(&bmNew
, ofn
.lpstrFile
, &fileTime
, &fileSize
, &fileHPPM
, &fileVPPM
);
346 UpdateApplicationProperties(bmNew
, ofn
.lpstrFileTitle
, ofn
.lpstrFileTitle
);
356 case IDM_FILEASWALLPAPERPLANE
:
357 SetWallpaper(filepathname
, 1, 1);
359 case IDM_FILEASWALLPAPERCENTERED
:
360 SetWallpaper(filepathname
, 1, 0);
362 case IDM_FILEASWALLPAPERSTRETCHED
:
363 SetWallpaper(filepathname
, 2, 0);
367 imageArea
.Invalidate(FALSE
);
371 imageArea
.Invalidate(FALSE
);
376 SetClipboardData(CF_BITMAP
, CopyImage(selectionModel
.GetBitmap(), IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
381 SendMessage(WM_COMMAND
, IDM_EDITCOPY
, 0);
382 /* Delete selection */
383 SendMessage(WM_COMMAND
, IDM_EDITDELETESELECTION
, 0);
387 if (GetClipboardData(CF_BITMAP
) != NULL
)
389 InsertSelectionFromHBITMAP((HBITMAP
) GetClipboardData(CF_BITMAP
), m_hWnd
);
393 case IDM_EDITDELETESELECTION
:
395 /* remove selection window and already painted content using undo */
399 case IDM_EDITSELECTALL
:
401 HWND hToolbar
= FindWindowEx(toolBoxContainer
.m_hWnd
, NULL
, TOOLBARCLASSNAME
, NULL
);
402 SendMessage(hToolbar
, TB_CHECKBUTTON
, ID_RECTSEL
, MAKELPARAM(TRUE
, 0));
403 toolBoxContainer
.SendMessage(WM_COMMAND
, ID_RECTSEL
);
404 //TODO: do this properly
405 startPaintingL(hDrawingDC
, 0, 0, paletteModel
.GetFgColor(), paletteModel
.GetBgColor());
406 whilePaintingL(hDrawingDC
, imageModel
.GetWidth(), imageModel
.GetHeight(), paletteModel
.GetFgColor(), paletteModel
.GetBgColor());
407 endPaintingL(hDrawingDC
, imageModel
.GetWidth(), imageModel
.GetHeight(), paletteModel
.GetFgColor(), paletteModel
.GetBgColor());
411 if (GetSaveFileName(&ofn
) != 0)
412 SaveDIBToFile(selectionModel
.GetBitmap(), ofn
.lpstrFile
, hDrawingDC
, NULL
, NULL
, fileHPPM
, fileVPPM
);
414 case IDM_EDITPASTEFROM
:
415 if (GetOpenFileName(&ofn
) != 0)
417 HBITMAP bmNew
= NULL
;
418 LoadDIBFromFile(&bmNew
, ofn
.lpstrFile
, &fileTime
, &fileSize
, &fileHPPM
, &fileVPPM
);
421 InsertSelectionFromHBITMAP(bmNew
, m_hWnd
);
426 case IDM_COLORSEDITPALETTE
:
427 if (ChooseColor(&choosecolor
))
428 paletteModel
.SetFgColor(choosecolor
.rgbResult
);
430 case IDM_COLORSMODERNPALETTE
:
431 paletteModel
.SelectPalette(1);
433 case IDM_COLORSOLDPALETTE
:
434 paletteModel
.SelectPalette(2);
436 case IDM_IMAGEINVERTCOLORS
:
438 imageModel
.InvertColors();
441 case IDM_IMAGEDELETEIMAGE
:
442 imageModel
.CopyPrevious();
443 Rect(hDrawingDC
, 0, 0, imageModel
.GetWidth(), imageModel
.GetHeight(), paletteModel
.GetBgColor(), paletteModel
.GetBgColor(), 0, TRUE
);
444 imageArea
.Invalidate(FALSE
);
446 case IDM_IMAGEROTATEMIRROR
:
447 switch (mirrorRotateDlg())
449 case 1: /* flip horizontally */
450 if (selectionWindow
.IsWindowVisible())
452 selectionModel
.FlipHorizontally();
453 ForceRefreshSelectionContents();
457 imageModel
.CopyPrevious();
458 StretchBlt(hDrawingDC
, imageModel
.GetWidth() - 1, 0, -imageModel
.GetWidth(), imageModel
.GetHeight(), hDrawingDC
, 0, 0,
459 imageModel
.GetWidth(), imageModel
.GetHeight(), SRCCOPY
);
460 imageArea
.Invalidate(FALSE
);
463 case 2: /* flip vertically */
464 if (selectionWindow
.IsWindowVisible())
466 selectionModel
.FlipVertically();
467 ForceRefreshSelectionContents();
471 imageModel
.CopyPrevious();
472 StretchBlt(hDrawingDC
, 0, imageModel
.GetHeight() - 1, imageModel
.GetWidth(), -imageModel
.GetHeight(), hDrawingDC
, 0, 0,
473 imageModel
.GetWidth(), imageModel
.GetHeight(), SRCCOPY
);
474 imageArea
.Invalidate(FALSE
);
477 case 3: /* rotate 90 degrees */
479 case 4: /* rotate 180 degrees */
480 if (selectionWindow
.IsWindowVisible())
482 selectionModel
.RotateNTimes90Degrees(2);
483 ForceRefreshSelectionContents();
487 imageModel
.CopyPrevious();
488 StretchBlt(hDrawingDC
, imageModel
.GetWidth() - 1, imageModel
.GetHeight() - 1, -imageModel
.GetWidth(), -imageModel
.GetHeight(), hDrawingDC
,
489 0, 0, imageModel
.GetWidth(), imageModel
.GetHeight(), SRCCOPY
);
490 imageArea
.Invalidate(FALSE
);
493 case 5: /* rotate 270 degrees */
497 case IDM_IMAGEATTRIBUTES
:
501 imageModel
.Crop(widthSetInDlg
, heightSetInDlg
, 0, 0);
505 case IDM_IMAGESTRETCHSKEW
:
509 imageModel
.StretchSkew(stretchSkew
.percentage
.x
, stretchSkew
.percentage
.y
,
510 stretchSkew
.angle
.x
, stretchSkew
.angle
.y
);
514 case IDM_IMAGEDRAWOPAQUE
:
515 toolsModel
.SetBackgroundTransparent(!toolsModel
.IsBackgroundTransparent());
518 imageModel
.Insert((HBITMAP
) CopyImage(selectionModel
.GetBitmap(), IMAGE_BITMAP
, 0, 0, LR_COPYRETURNORG
));
521 case IDM_VIEWTOOLBOX
:
522 toolBoxContainer
.ShowWindow(toolBoxContainer
.IsWindowVisible() ? SW_HIDE
: SW_SHOW
);
523 alignChildrenToMainWindow();
525 case IDM_VIEWCOLORPALETTE
:
526 paletteWindow
.ShowWindow(paletteWindow
.IsWindowVisible() ? SW_HIDE
: SW_SHOW
);
527 alignChildrenToMainWindow();
529 case IDM_VIEWSTATUSBAR
:
530 ::ShowWindow(hStatusBar
, ::IsWindowVisible(hStatusBar
) ? SW_HIDE
: SW_SHOW
);
531 alignChildrenToMainWindow();
533 case IDM_FORMATICONBAR
:
534 textEditWindow
.ShowWindow(textEditWindow
.IsWindowVisible() ? SW_HIDE
: SW_SHOW
);
536 case IDM_VIEWSHOWGRID
:
537 showGrid
= !showGrid
;
538 imageArea
.Invalidate(FALSE
);
540 case IDM_VIEWSHOWMINIATURE
:
541 showMiniature
= !showMiniature
;
542 miniature
.ShowWindow(showMiniature
? SW_SHOW
: SW_HIDE
);
545 case IDM_VIEWZOOM125
:
554 case IDM_VIEWZOOM100
:
557 case IDM_VIEWZOOM200
:
560 case IDM_VIEWZOOM400
:
563 case IDM_VIEWZOOM800
: