-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
1388 lines (1236 loc) · 45.3 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROJECT: PAINT for ReactOS
* LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
* PURPOSE: The main window and wWinMain etc.
* COPYRIGHT: Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
* Copyright 2017-2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
* Copyright 2018 Stanislav Motylkov <x86corez@gmail.com>
*/
#include "precomp.h"
#include <dlgs.h>
#include <mapi.h>
#include <assert.h>
BOOL g_askBeforeEnlarging = FALSE; // TODO: initialize from registry
HINSTANCE g_hinstExe = NULL;
WCHAR g_szFileName[MAX_LONG_PATH] = { 0 };
WCHAR g_szMailTempFile[MAX_LONG_PATH] = { 0 };
BOOL g_isAFile = FALSE;
BOOL g_imageSaved = FALSE;
BOOL g_showGrid = FALSE;
HWND g_hStatusBar = NULL;
CMainWindow mainWindow;
typedef HWND (WINAPI *FN_HtmlHelpW)(HWND, LPCWSTR, UINT, DWORD_PTR);
static HINSTANCE s_hHHCTRL_OCX = NULL; // HtmlHelpW needs "hhctrl.ocx"
static FN_HtmlHelpW s_pHtmlHelpW = NULL;
/* FUNCTIONS ********************************************************/
void ShowOutOfMemory(void)
{
WCHAR szText[256];
::FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
ERROR_OUTOFMEMORY,
0,
szText, _countof(szText),
NULL);
mainWindow.MessageBox(szText, NULL, MB_ICONERROR);
}
// get file name extension from filter string
static BOOL
FileExtFromFilter(LPWSTR pExt, OPENFILENAME *pOFN)
{
LPWSTR pchExt = pExt;
*pchExt = 0;
DWORD nIndex = 1;
for (LPCWSTR pch = pOFN->lpstrFilter; *pch; ++nIndex)
{
pch += lstrlen(pch) + 1;
if (pOFN->nFilterIndex == nIndex)
{
for (++pch; *pch && *pch != L';'; ++pch)
{
*pchExt++ = *pch;
}
*pchExt = 0;
CharLower(pExt);
return TRUE;
}
pch += wcslen(pch) + 1;
}
return FALSE;
}
// Hook procedure for OPENFILENAME to change the file name extension
static UINT_PTR APIENTRY
OFNHookProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND hParent;
OFNOTIFYW *pon;
WCHAR Path[MAX_PATH];
switch (uMsg)
{
case WM_NOTIFY:
pon = (OFNOTIFYW *)lParam;
if (pon->hdr.code == CDN_TYPECHANGE)
{
hParent = GetParent(hwnd);
SendMessageW(hParent, CDM_GETFILEPATH, _countof(Path), (LPARAM)Path);
FileExtFromFilter(PathFindExtensionW(Path), pon->lpOFN);
SendMessageW(hParent, CDM_SETCONTROLTEXT, cmb13, (LPARAM)PathFindFileNameW(Path));
StringCchCopyW(pon->lpOFN->lpstrFile, pon->lpOFN->nMaxFile, Path);
}
break;
}
return 0;
}
typedef ULONG (WINAPI *FN_MAPISendMail)(LHANDLE, ULONG_PTR, lpMapiMessage, FLAGS, ULONG);
typedef ULONG (WINAPI *FN_MAPISendMailW)(LHANDLE, ULONG_PTR, lpMapiMessageW, FLAGS, ULONG);
BOOL OpenMailer(HWND hWnd, LPCWSTR pszPathName)
{
// Delete the temporary file if any
if (g_szMailTempFile[0])
{
::DeleteFileW(g_szMailTempFile);
g_szMailTempFile[0] = UNICODE_NULL;
}
CStringW strFileTitle;
if (PathFileExistsW(pszPathName) && imageModel.IsImageSaved())
{
strFileTitle = PathFindFileNameW(pszPathName);
}
else // Not existing or not saved
{
// Get the name of a temporary file
WCHAR szTempDir[MAX_PATH];
::GetTempPathW(_countof(szTempDir), szTempDir);
if (!::GetTempFileNameW(szTempDir, L"afx", 0, g_szMailTempFile))
return FALSE; // Failure
if (PathFileExistsW(g_szFileName))
{
// Set file title
strFileTitle = PathFindFileNameW(g_szFileName);
// Copy to the temporary file
if (!::CopyFileW(g_szFileName, g_szMailTempFile, FALSE))
{
g_szMailTempFile[0] = UNICODE_NULL;
return FALSE; // Failure
}
}
else
{
// Set file title
strFileTitle.LoadString(IDS_DEFAULTFILENAME);
strFileTitle += L".png";
// Save it to the temporary file
HBITMAP hbmLocked = imageModel.LockBitmap();
BOOL ret = SaveDIBToFile(hbmLocked, g_szMailTempFile, FALSE, Gdiplus::ImageFormatPNG);
imageModel.UnlockBitmap(hbmLocked);
if (!ret)
{
g_szMailTempFile[0] = UNICODE_NULL;
return FALSE; // Failure
}
}
// Use the temporary file
pszPathName = g_szMailTempFile;
}
// Load "mapi32.dll"
HINSTANCE hMAPI = LoadLibraryW(L"mapi32.dll");
if (!hMAPI)
return FALSE; // Failure
// Attachment
MapiFileDescW attachmentW = { 0 };
attachmentW.nPosition = (ULONG)-1;
attachmentW.lpszPathName = (LPWSTR)pszPathName;
attachmentW.lpszFileName = (LPWSTR)(LPCWSTR)strFileTitle;
// Message with attachment
MapiMessageW messageW = { 0 };
messageW.lpszSubject = NULL;
messageW.nFileCount = 1;
messageW.lpFiles = &attachmentW;
// First, try to open the mailer by the function of Unicode version
FN_MAPISendMailW pMAPISendMailW = (FN_MAPISendMailW)::GetProcAddress(hMAPI, "MAPISendMailW");
if (pMAPISendMailW)
{
pMAPISendMailW(0, (ULONG_PTR)hWnd, &messageW, MAPI_DIALOG | MAPI_LOGON_UI, 0);
::FreeLibrary(hMAPI);
return TRUE; // MAPISendMailW will show an error message on failure
}
// Convert to ANSI strings
CStringA szPathNameA(pszPathName), szFileTitleA(strFileTitle);
MapiFileDesc attachment = { 0 };
attachment.nPosition = (ULONG)-1;
attachment.lpszPathName = (LPSTR)(LPCSTR)szPathNameA;
attachment.lpszFileName = (LPSTR)(LPCSTR)szFileTitleA;
MapiMessage message = { 0 };
message.lpszSubject = NULL;
message.nFileCount = 1;
message.lpFiles = &attachment;
// Try again but in ANSI version
FN_MAPISendMail pMAPISendMail = (FN_MAPISendMail)::GetProcAddress(hMAPI, "MAPISendMail");
if (pMAPISendMail)
{
pMAPISendMail(0, (ULONG_PTR)hWnd, &message, MAPI_DIALOG | MAPI_LOGON_UI, 0);
::FreeLibrary(hMAPI);
return TRUE; // MAPISendMail will show an error message on failure
}
::FreeLibrary(hMAPI);
return FALSE; // Failure
}
BOOL CMainWindow::GetOpenFileName(IN OUT LPWSTR pszFile, INT cchMaxFile)
{
static OPENFILENAMEW ofn = { 0 };
static CStringW strFilter;
if (ofn.lStructSize == 0)
{
// The "All Files" item text
CStringW strAllPictureFiles;
strAllPictureFiles.LoadString(g_hinstExe, IDS_ALLPICTUREFILES);
// Get the import filter
CSimpleArray<GUID> aguidFileTypesI;
CImage::GetImporterFilterString(strFilter, aguidFileTypesI, strAllPictureFiles,
CImage::excludeDefaultLoad, L'|');
strFilter.Replace(L'|', UNICODE_NULL);
// Initializing the OPENFILENAME structure for GetOpenFileName
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = m_hWnd;
ofn.hInstance = g_hinstExe;
ofn.lpstrFilter = strFilter;
ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY;
ofn.lpstrDefExt = L"png";
}
ofn.lpstrFile = pszFile;
ofn.nMaxFile = cchMaxFile;
return ::GetOpenFileNameW(&ofn);
}
BOOL CMainWindow::GetSaveFileName(IN OUT LPWSTR pszFile, INT cchMaxFile)
{
static OPENFILENAMEW sfn = { 0 };
static CStringW strFilter;
if (sfn.lStructSize == 0)
{
// Get the export filter
CSimpleArray<GUID> aguidFileTypesE;
CImage::GetExporterFilterString(strFilter, aguidFileTypesE, NULL,
CImage::excludeDefaultSave, L'|');
strFilter.Replace(L'|', UNICODE_NULL);
// Initializing the OPENFILENAME structure for GetSaveFileName
ZeroMemory(&sfn, sizeof(sfn));
sfn.lStructSize = sizeof(sfn);
sfn.hwndOwner = m_hWnd;
sfn.hInstance = g_hinstExe;
sfn.lpstrFilter = strFilter;
sfn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_ENABLEHOOK;
sfn.lpfnHook = OFNHookProc;
sfn.lpstrDefExt = L"png";
LPWSTR pchDotExt = PathFindExtensionW(pszFile);
if (*pchDotExt == UNICODE_NULL)
{
// Choose PNG
StringCchCatW(pszFile, cchMaxFile, L".png");
for (INT i = 0; i < aguidFileTypesE.GetSize(); ++i)
{
if (aguidFileTypesE[i] == Gdiplus::ImageFormatPNG)
{
sfn.nFilterIndex = i + 1;
break;
}
}
}
}
sfn.lpstrFile = pszFile;
sfn.nMaxFile = cchMaxFile;
return ::GetSaveFileNameW(&sfn);
}
BOOL CMainWindow::ChooseColor(IN OUT COLORREF *prgbColor)
{
static CHOOSECOLOR choosecolor = { 0 };
static COLORREF custColors[16] =
{
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff
};
if (choosecolor.lStructSize == 0)
{
// Initializing the CHOOSECOLOR structure for ChooseColor
ZeroMemory(&choosecolor, sizeof(choosecolor));
choosecolor.lStructSize = sizeof(choosecolor);
choosecolor.hwndOwner = m_hWnd;
choosecolor.lpCustColors = custColors;
}
choosecolor.Flags = CC_RGBINIT;
choosecolor.rgbResult = *prgbColor;
if (!::ChooseColor(&choosecolor))
return FALSE;
*prgbColor = choosecolor.rgbResult;
return TRUE;
}
HWND CMainWindow::DoCreate()
{
::LoadStringW(g_hinstExe, IDS_DEFAULTFILENAME, g_szFileName, _countof(g_szFileName));
CStringW strTitle;
strTitle.Format(IDS_WINDOWTITLE, PathFindFileName(g_szFileName));
RECT& rc = registrySettings.WindowPlacement.rcNormalPosition;
return Create(HWND_DESKTOP, rc, strTitle, WS_OVERLAPPEDWINDOW, WS_EX_ACCEPTFILES);
}
// A wrapper function for HtmlHelpW
static HWND DoHtmlHelpW(HWND hwndCaller, LPCWSTR pszFile, UINT uCommand, DWORD_PTR dwData)
{
WCHAR szPath[MAX_PATH];
if (!s_hHHCTRL_OCX && (uCommand != HH_CLOSE_ALL))
{
// The function loads the system library, not local
GetSystemDirectoryW(szPath, _countof(szPath));
StringCchCatW(szPath, _countof(szPath), L"\\hhctrl.ocx");
s_hHHCTRL_OCX = LoadLibraryW(szPath);
if (s_hHHCTRL_OCX)
s_pHtmlHelpW = (FN_HtmlHelpW)GetProcAddress(s_hHHCTRL_OCX, "HtmlHelpW");
}
if (!s_pHtmlHelpW)
return NULL;
return s_pHtmlHelpW(hwndCaller, pszFile, uCommand, dwData);
}
void CMainWindow::alignChildrenToMainWindow()
{
RECT clientRect, rc;
GetClientRect(&clientRect);
RECT rcSpace = clientRect;
const UINT uFlags = (SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREPOSITION | SWP_NOCOPYBITS);
if (::IsWindowVisible(g_hStatusBar))
{
::GetWindowRect(g_hStatusBar, &rc);
rcSpace.bottom -= rc.bottom - rc.top;
}
HDWP hDWP = ::BeginDeferWindowPos(3);
if (::IsWindowVisible(toolBoxContainer))
{
if (registrySettings.Bar2ID == BAR2ID_RIGHT)
{
hDWP = ::DeferWindowPos(hDWP, toolBoxContainer, NULL,
rcSpace.right - CX_TOOLBAR, rcSpace.top,
CX_TOOLBAR, rcSpace.bottom - rcSpace.top,
uFlags);
rcSpace.right -= CX_TOOLBAR;
}
else
{
hDWP = ::DeferWindowPos(hDWP, toolBoxContainer, NULL,
rcSpace.left, rcSpace.top,
CX_TOOLBAR, rcSpace.bottom - rcSpace.top,
uFlags);
rcSpace.left += CX_TOOLBAR;
}
}
if (::IsWindowVisible(paletteWindow))
{
if (registrySettings.Bar1ID == BAR1ID_BOTTOM)
{
hDWP = ::DeferWindowPos(hDWP, paletteWindow, NULL,
rcSpace.left, rcSpace.bottom - CY_PALETTE,
rcSpace.right - rcSpace.left, CY_PALETTE,
uFlags);
rcSpace.bottom -= CY_PALETTE;
}
else
{
hDWP = ::DeferWindowPos(hDWP, paletteWindow, NULL,
rcSpace.left, rcSpace.top,
rcSpace.right - rcSpace.left, CY_PALETTE,
uFlags);
rcSpace.top += CY_PALETTE;
}
}
if (canvasWindow.IsWindow())
{
hDWP = ::DeferWindowPos(hDWP, canvasWindow, NULL,
rcSpace.left, rcSpace.top,
rcSpace.right - rcSpace.left, rcSpace.bottom - rcSpace.top,
uFlags);
}
::EndDeferWindowPos(hDWP);
}
void CMainWindow::saveImage(BOOL overwrite)
{
canvasWindow.OnEndDraw(FALSE);
// Is the extension not supported?
PWCHAR pchDotExt = PathFindExtensionW(g_szFileName);
if (pchDotExt && *pchDotExt && !CImageDx::IsExtensionSupported(pchDotExt))
{
// Remove the extension
PathRemoveExtensionW(g_szFileName);
// No overwrite
overwrite = FALSE;
}
if (g_isAFile && overwrite)
{
imageModel.SaveImage(g_szFileName);
}
else if (GetSaveFileName(g_szFileName, _countof(g_szFileName)))
{
imageModel.SaveImage(g_szFileName);
}
}
void CMainWindow::InsertSelectionFromHBITMAP(HBITMAP bitmap, HWND window)
{
int width = GetDIBWidth(bitmap);
int height = GetDIBHeight(bitmap);
int curWidth = imageModel.GetWidth();
int curHeight = imageModel.GetHeight();
if (width > curWidth || height > curHeight)
{
BOOL shouldEnlarge = TRUE;
if (g_askBeforeEnlarging)
{
WCHAR programname[20];
WCHAR shouldEnlargePromptText[100];
::LoadStringW(g_hinstExe, IDS_PROGRAMNAME, programname, _countof(programname));
::LoadStringW(g_hinstExe, IDS_ENLARGEPROMPTTEXT, shouldEnlargePromptText, _countof(shouldEnlargePromptText));
switch (MessageBox(shouldEnlargePromptText, programname, MB_YESNOCANCEL | MB_ICONQUESTION))
{
case IDYES:
break;
case IDNO:
shouldEnlarge = FALSE;
break;
case IDCANCEL:
return;
}
}
if (shouldEnlarge)
{
if (width > curWidth)
curWidth = width;
if (height > curHeight)
curHeight = height;
imageModel.Crop(curWidth, curHeight, 0, 0);
}
}
toolsModel.SetActiveTool(TOOL_RECTSEL);
selectionModel.InsertFromHBITMAP(bitmap, 0, 0);
selectionModel.m_bShow = TRUE;
imageModel.NotifyImageChanged();
}
LRESULT CMainWindow::OnMouseWheel(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
INT zDelta = (SHORT)HIWORD(wParam);
if (::GetKeyState(VK_CONTROL) < 0) // Ctrl+Wheel
{
if (zDelta < 0)
{
if (toolsModel.GetZoom() > MIN_ZOOM)
canvasWindow.zoomTo(toolsModel.GetZoom() / 2);
}
else if (zDelta > 0)
{
if (toolsModel.GetZoom() < MAX_ZOOM)
canvasWindow.zoomTo(toolsModel.GetZoom() * 2);
}
}
else // Wheel only
{
UINT nCount = 3;
if (::GetAsyncKeyState(VK_SHIFT) < 0)
{
#ifndef SPI_GETWHEELSCROLLCHARS
#define SPI_GETWHEELSCROLLCHARS 0x006C // Needed for pre-NT6 PSDK
#endif
SystemParametersInfoW(SPI_GETWHEELSCROLLCHARS, 0, &nCount, 0);
for (UINT i = 0; i < nCount; ++i)
{
if (zDelta < 0)
::PostMessageW(canvasWindow, WM_HSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), 0);
else if (zDelta > 0)
::PostMessageW(canvasWindow, WM_HSCROLL, MAKEWPARAM(SB_LINEUP, 0), 0);
}
}
else
{
SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &nCount, 0);
for (UINT i = 0; i < nCount; ++i)
{
if (zDelta < 0)
::PostMessageW(canvasWindow, WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), 0);
else if (zDelta > 0)
::PostMessageW(canvasWindow, WM_VSCROLL, MAKEWPARAM(SB_LINEUP, 0), 0);
}
}
}
return 0;
}
LRESULT CMainWindow::OnDropFiles(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
WCHAR droppedfile[MAX_PATH];
HDROP hDrop = (HDROP)wParam;
DragQueryFile(hDrop, 0, droppedfile, _countof(droppedfile));
DragFinish(hDrop);
ConfirmSave() && DoLoadImageFile(m_hWnd, droppedfile, TRUE);
return 0;
}
LRESULT CMainWindow::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Loading and setting the window menu from resource
m_hMenu = ::LoadMenuW(g_hinstExe, MAKEINTRESOURCEW(ID_MENU));
SetMenu(m_hMenu);
// Create the status bar
DWORD style = SBARS_SIZEGRIP | WS_CHILD | (registrySettings.ShowStatusBar ? WS_VISIBLE : 0);
g_hStatusBar = ::CreateWindowExW(0, STATUSCLASSNAME, NULL, style, 0, 0, 0, 0, m_hWnd,
NULL, g_hinstExe, NULL);
::SendMessageW(g_hStatusBar, SB_SETMINHEIGHT, 21, 0);
// Create the tool box
toolBoxContainer.DoCreate(m_hWnd);
// Create the palette window
RECT rcEmpty = { 0, 0, 0, 0 }; // Rely on WM_SIZE
style = WS_CHILD | (registrySettings.ShowPalette ? WS_VISIBLE : 0);
paletteWindow.Create(m_hWnd, rcEmpty, NULL, style, WS_EX_STATICEDGE);
// Create the canvas
style = WS_CHILD | WS_GROUP | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE;
canvasWindow.Create(m_hWnd, rcEmpty, NULL, style, WS_EX_CLIENTEDGE);
// Create and show the miniature if necessary
if (registrySettings.ShowThumbnail)
{
miniature.DoCreate(m_hWnd);
miniature.ShowWindow(SW_SHOWNOACTIVATE);
}
// Set icon
SendMessage(WM_SETICON, ICON_BIG, (LPARAM)::LoadIconW(g_hinstExe, MAKEINTRESOURCEW(IDI_APPICON)));
SendMessage(WM_SETICON, ICON_SMALL, (LPARAM)::LoadIconW(g_hinstExe, MAKEINTRESOURCEW(IDI_APPICON)));
return 0;
}
LRESULT CMainWindow::OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
registrySettings.WindowPlacement.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(&(registrySettings.WindowPlacement));
DoHtmlHelpW(NULL, NULL, HH_CLOSE_ALL, 0);
if (s_hHHCTRL_OCX)
{
FreeLibrary(s_hHHCTRL_OCX);
s_hHHCTRL_OCX = NULL;
s_pHtmlHelpW = NULL;
}
SetMenu(NULL);
if (m_hMenu)
{
::DestroyMenu(m_hMenu);
m_hMenu = NULL;
}
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
return 0;
}
BOOL CMainWindow::ConfirmSave()
{
canvasWindow.OnEndDraw(FALSE);
if (imageModel.IsImageSaved())
return TRUE;
CStringW strProgramName;
strProgramName.LoadString(IDS_PROGRAMNAME);
CStringW strSavePromptText;
strSavePromptText.Format(IDS_SAVEPROMPTTEXT, PathFindFileName(g_szFileName));
switch (MessageBox(strSavePromptText, strProgramName, MB_YESNOCANCEL | MB_ICONQUESTION))
{
case IDYES:
saveImage(TRUE);
return imageModel.IsImageSaved();
case IDNO:
return TRUE;
case IDCANCEL:
return FALSE;
}
return TRUE;
}
LRESULT CMainWindow::OnClose(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (ConfirmSave())
{
DestroyWindow();
}
return 0;
}
void CMainWindow::ProcessFileMenu(HMENU hPopupMenu)
{
for (INT iItem = 0; iItem < MAX_RECENT_FILES; ++iItem)
RemoveMenu(hPopupMenu, IDM_FILE1 + iItem, MF_BYCOMMAND);
if (registrySettings.strFiles[0].IsEmpty())
return;
RemoveMenu(hPopupMenu, IDM_FILEMOSTRECENTLYUSEDFILE, MF_BYCOMMAND);
INT cMenuItems = GetMenuItemCount(hPopupMenu);
for (INT iItem = 0; iItem < MAX_RECENT_FILES; ++iItem)
{
CStringW& strFile = registrySettings.strFiles[iItem];
if (strFile.IsEmpty())
break;
// Condense the lengthy pathname by using '...'
#define MAX_RECENT_PATHNAME_DISPLAY 30
CPath pathFile(strFile);
pathFile.CompactPathEx(MAX_RECENT_PATHNAME_DISPLAY);
assert(wcslen((LPCWSTR)pathFile) <= MAX_RECENT_PATHNAME_DISPLAY);
// Add an accelerator (by '&') to the item number for quick access
WCHAR szText[4 + MAX_RECENT_PATHNAME_DISPLAY + 1];
StringCchPrintfW(szText, _countof(szText), L"&%u %s", iItem + 1, (LPCWSTR)pathFile);
INT iMenuItem = (cMenuItems - 2) + iItem;
InsertMenu(hPopupMenu, iMenuItem, MF_BYPOSITION | MF_STRING, IDM_FILE1 + iItem, szText);
}
}
BOOL CMainWindow::CanUndo() const
{
if (toolsModel.GetActiveTool() == TOOL_TEXT && ::IsWindowVisible(textEditWindow))
return (BOOL)textEditWindow.SendMessage(EM_CANUNDO);
if (selectionModel.m_bShow && toolsModel.IsSelection())
return TRUE;
return imageModel.CanUndo();
}
BOOL CMainWindow::CanRedo() const
{
if (toolsModel.GetActiveTool() == TOOL_TEXT && ::IsWindowVisible(textEditWindow))
return FALSE; // There is no "WM_REDO" in EDIT control
return imageModel.CanRedo();
}
BOOL CMainWindow::CanPaste() const
{
if (toolsModel.GetActiveTool() == TOOL_TEXT && ::IsWindowVisible(textEditWindow))
return ::IsClipboardFormatAvailable(CF_UNICODETEXT);
return (::IsClipboardFormatAvailable(CF_ENHMETAFILE) ||
::IsClipboardFormatAvailable(CF_DIB) ||
::IsClipboardFormatAvailable(CF_BITMAP));
}
LRESULT CMainWindow::OnInitMenuPopup(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HMENU menu = (HMENU)wParam;
BOOL trueSelection = (selectionModel.m_bShow && toolsModel.IsSelection());
BOOL textShown = (toolsModel.GetActiveTool() == TOOL_TEXT && ::IsWindowVisible(textEditWindow));
DWORD dwStart = 0, dwEnd = 0;
if (textShown)
textEditWindow.SendMessage(EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
BOOL hasTextSel = (dwStart < dwEnd);
//
// File menu
//
if (::GetSubMenu(GetMenu(), 0) == menu)
{
ProcessFileMenu(menu);
}
//
// Edit menu
//
EnableMenuItem(menu, IDM_EDITUNDO, ENABLED_IF(CanUndo()));
EnableMenuItem(menu, IDM_EDITREDO, ENABLED_IF(CanRedo()));
EnableMenuItem(menu, IDM_EDITCUT, ENABLED_IF(textShown ? hasTextSel : trueSelection));
EnableMenuItem(menu, IDM_EDITCOPY, ENABLED_IF(textShown ? hasTextSel : trueSelection));
EnableMenuItem(menu, IDM_EDITDELETESELECTION,
ENABLED_IF(textShown ? hasTextSel : trueSelection));
EnableMenuItem(menu, IDM_EDITINVERTSELECTION, ENABLED_IF(trueSelection));
EnableMenuItem(menu, IDM_EDITCOPYTO, ENABLED_IF(trueSelection));
EnableMenuItem(menu, IDM_EDITPASTE, ENABLED_IF(CanPaste()));
EnableMenuItem(menu, IDM_CROPSELECTION, ENABLED_IF(trueSelection));
//
// View menu
//
CheckMenuItem(menu, IDM_VIEWTOOLBOX, CHECKED_IF(::IsWindowVisible(toolBoxContainer)));
CheckMenuItem(menu, IDM_VIEWCOLORPALETTE, CHECKED_IF(::IsWindowVisible(paletteWindow)));
CheckMenuItem(menu, IDM_VIEWSTATUSBAR, CHECKED_IF(::IsWindowVisible(g_hStatusBar)));
CheckMenuItem(menu, IDM_FORMATICONBAR, CHECKED_IF(::IsWindowVisible(fontsDialog)));
EnableMenuItem(menu, IDM_FORMATICONBAR, ENABLED_IF(toolsModel.GetActiveTool() == TOOL_TEXT));
CheckMenuItem(menu, IDM_VIEWZOOM125, CHECKED_IF(toolsModel.GetZoom() == 125));
CheckMenuItem(menu, IDM_VIEWZOOM25, CHECKED_IF(toolsModel.GetZoom() == 250));
CheckMenuItem(menu, IDM_VIEWZOOM50, CHECKED_IF(toolsModel.GetZoom() == 500));
CheckMenuItem(menu, IDM_VIEWZOOM100, CHECKED_IF(toolsModel.GetZoom() == 1000));
CheckMenuItem(menu, IDM_VIEWZOOM200, CHECKED_IF(toolsModel.GetZoom() == 2000));
CheckMenuItem(menu, IDM_VIEWZOOM400, CHECKED_IF(toolsModel.GetZoom() == 4000));
CheckMenuItem(menu, IDM_VIEWZOOM800, CHECKED_IF(toolsModel.GetZoom() == 8000));
CheckMenuItem(menu, IDM_VIEWSHOWGRID, CHECKED_IF(g_showGrid));
CheckMenuItem(menu, IDM_VIEWSHOWMINIATURE, CHECKED_IF(registrySettings.ShowThumbnail));
//
// Image menu
//
EnableMenuItem(menu, IDM_IMAGECROP, ENABLED_IF(selectionModel.m_bShow));
EnableMenuItem(menu, IDM_IMAGEDELETEIMAGE, ENABLED_IF(!selectionModel.m_bShow));
CheckMenuItem(menu, IDM_IMAGEDRAWOPAQUE, CHECKED_IF(!toolsModel.IsBackgroundTransparent()));
//
// Palette menu
//
CheckMenuItem(menu, IDM_COLORSMODERNPALETTE, CHECKED_IF(paletteModel.SelectedPalette() == PAL_MODERN));
CheckMenuItem(menu, IDM_COLORSOLDPALETTE, CHECKED_IF(paletteModel.SelectedPalette() == PAL_OLDTYPE));
return 0;
}
LRESULT CMainWindow::OnSize(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int test[] = { LOWORD(lParam) - 260, LOWORD(lParam) - 140, LOWORD(lParam) - 20 };
if (::IsWindow(g_hStatusBar))
{
::SendMessageW(g_hStatusBar, WM_SIZE, 0, 0);
::SendMessageW(g_hStatusBar, SB_SETPARTS, 3, (LPARAM)&test);
}
alignChildrenToMainWindow();
return 0;
}
LRESULT CMainWindow::OnGetMinMaxInfo(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
MINMAXINFO *mm = (MINMAXINFO*)lParam;
mm->ptMinTrackSize = { 330, 360 };
return 0;
}
LRESULT CMainWindow::OnKeyDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (wParam)
{
case VK_ESCAPE:
canvasWindow.PostMessage(nMsg, wParam, lParam);
break;
case VK_LEFT:
selectionModel.moveSelection(-1, 0);
break;
case VK_RIGHT:
selectionModel.moveSelection(+1, 0);
break;
case VK_UP:
selectionModel.moveSelection(0, -1);
break;
case VK_DOWN:
selectionModel.moveSelection(0, +1);
break;
default:
break;
}
return 0;
}
LRESULT CMainWindow::OnSysColorChange(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
/* Redirect message to common controls */
HWND hToolbar = FindWindowEx(toolBoxContainer.m_hWnd, NULL, TOOLBARCLASSNAME, NULL);
::SendMessageW(hToolbar, WM_SYSCOLORCHANGE, 0, 0);
return 0;
}
LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Disable commands while dragging mouse
if (canvasWindow.m_drawing && ::GetCapture())
{
ATLTRACE("locking!\n");
return 0;
}
BOOL textShown = (toolsModel.GetActiveTool() == TOOL_TEXT && ::IsWindowVisible(textEditWindow));
switch (LOWORD(wParam))
{
case IDM_HELPINFO:
{
WCHAR infotitle[100], infotext[200];
::LoadStringW(g_hinstExe, IDS_INFOTITLE, infotitle, _countof(infotitle));
::LoadStringW(g_hinstExe, IDS_INFOTEXT, infotext, _countof(infotext));
::ShellAboutW(m_hWnd, infotitle, infotext,
LoadIconW(g_hinstExe, MAKEINTRESOURCEW(IDI_APPICON)));
break;
}
case IDM_HELPHELPTOPICS:
DoHtmlHelpW(m_hWnd, L"%SystemRoot%\\Help\\mspaint.chm", HH_DISPLAY_TOPIC, 0);
break;
case IDM_FILEEXIT:
SendMessage(WM_CLOSE, wParam, lParam);
break;
case IDM_FILENEW:
if (ConfirmSave())
{
InitializeImage(NULL, NULL, FALSE);
}
break;
case IDM_FILEOPEN:
{
WCHAR szFileName[MAX_LONG_PATH] = L"";
if (ConfirmSave() && GetOpenFileName(szFileName, _countof(szFileName)))
{
DoLoadImageFile(m_hWnd, szFileName, TRUE);
}
break;
}
case IDM_FILESAVE:
saveImage(TRUE);
break;
case IDM_FILESAVEAS:
saveImage(FALSE);
break;
case IDM_FILEPAGESETUP:
// DUMMY: Shows the dialog only, no functionality
PAGESETUPDLG psd;
ZeroMemory(&psd, sizeof(psd));
psd.lStructSize = sizeof(psd);
psd.hwndOwner = m_hWnd;
PageSetupDlg(&psd);
break;
case IDM_FILEPRINT:
// TODO: Test whether it actually works
PRINTDLG pd;
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = m_hWnd;
pd.hDevMode = NULL; // freed by user
pd.hDevNames = NULL; // freed by user
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 0xffff;
pd.nToPage = 0xffff;
pd.nMinPage = 1;
pd.nMaxPage = 0xffff;
if (PrintDlg(&pd) == TRUE)
{
::BitBlt(pd.hDC, 0, 0, imageModel.GetWidth(), imageModel.GetHeight(), imageModel.GetDC(), 0, 0, SRCCOPY);
DeleteDC(pd.hDC);
}
if (pd.hDevMode)
GlobalFree(pd.hDevMode);
if (pd.hDevNames)
GlobalFree(pd.hDevNames);
break;
case IDM_FILESEND:
canvasWindow.OnEndDraw(FALSE);
if (!OpenMailer(m_hWnd, g_szFileName))
{
ShowError(IDS_CANTSENDMAIL);
}
break;
case IDM_FILEASWALLPAPERPLANE:
RegistrySettings::SetWallpaper(g_szFileName, RegistrySettings::TILED);
break;
case IDM_FILEASWALLPAPERCENTERED:
RegistrySettings::SetWallpaper(g_szFileName, RegistrySettings::CENTERED);
break;
case IDM_FILEASWALLPAPERSTRETCHED:
RegistrySettings::SetWallpaper(g_szFileName, RegistrySettings::STRETCHED);
break;
case IDM_FILE1:
case IDM_FILE2:
case IDM_FILE3:
case IDM_FILE4:
{
INT iFile = LOWORD(wParam) - IDM_FILE1;
if (ConfirmSave())
DoLoadImageFile(m_hWnd, registrySettings.strFiles[iFile], TRUE);
break;
}
case IDM_EDITUNDO:
if (textShown)
{
textEditWindow.PostMessage(WM_UNDO, 0, 0);
break;
}
canvasWindow.OnEndDraw(FALSE);
imageModel.Undo();
break;
case IDM_EDITREDO:
if (textShown)
{
// There is no "WM_REDO" in EDIT control
break;
}
canvasWindow.OnEndDraw(FALSE);
imageModel.Redo();
break;
case IDM_EDITCOPY:
if (textShown)
{
textEditWindow.SendMessage(WM_COPY);
break;
}
if (!selectionModel.m_bShow || !OpenClipboard())
break;
EmptyClipboard();
selectionModel.TakeOff();
{
HBITMAP hbmCopy = selectionModel.GetSelectionContents();
HGLOBAL hGlobal = BitmapToClipboardDIB(hbmCopy);
if (hGlobal)
::SetClipboardData(CF_DIB, hGlobal);
else
ShowOutOfMemory();
::DeleteObject(hbmCopy);
}
CloseClipboard();
break;
case IDM_EDITCUT:
if (textShown)
{
textEditWindow.SendMessage(WM_CUT);
break;
}
/* Copy */
SendMessage(WM_COMMAND, IDM_EDITCOPY, 0);
/* Delete selection */
SendMessage(WM_COMMAND, IDM_EDITDELETESELECTION, 0);
break;
case IDM_EDITPASTE:
if (textShown)
{
textEditWindow.SendMessage(WM_PASTE);
break;
}
if (!OpenClipboard())
break;
// In many cases, CF_ENHMETAFILE provides a better image than CF_DIB
if (::IsClipboardFormatAvailable(CF_ENHMETAFILE))
{
HENHMETAFILE hEMF = (HENHMETAFILE)::GetClipboardData(CF_ENHMETAFILE);
if (hEMF)
{
HBITMAP hbm = BitmapFromHEMF(hEMF);
::DeleteEnhMetaFile(hEMF);
if (hbm)
{
InsertSelectionFromHBITMAP(hbm, m_hWnd);
CloseClipboard();