xref: /reactos/base/applications/mspaint/main.cpp (revision 8a978a17)
1 /*
2  * PROJECT:     PAINT for ReactOS
3  * LICENSE:     LGPL
4  * FILE:        base/applications/mspaint/main.cpp
5  * PURPOSE:     Initializing everything
6  * PROGRAMMERS: Benedikt Freisen
7  */
8 
9 /* INCLUDES *********************************************************/
10 
11 #include "precomp.h"
12 
13 /* FUNCTIONS ********************************************************/
14 
15 POINT start;
16 POINT last;
17 
18 ToolsModel toolsModel;
19 
20 SelectionModel selectionModel;
21 
22 LOGFONT lfTextFont;
23 HFONT hfontTextFont;
24 HWND hwndEditCtl;
25 LPTSTR textToolText = NULL;
26 int textToolTextMaxLen = 0;
27 
28 PaletteModel paletteModel;
29 
30 RegistrySettings registrySettings;
31 
32 ImageModel imageModel;
33 BOOL askBeforeEnlarging = FALSE;  // TODO: initialize from registry
34 
35 HWND hStatusBar;
36 CHOOSECOLOR choosecolor;
37 OPENFILENAME ofn;
38 OPENFILENAME sfn;
39 HICON hNontranspIcon;
40 HICON hTranspIcon;
41 
42 HCURSOR hCurFill;
43 HCURSOR hCurColor;
44 HCURSOR hCurZoom;
45 HCURSOR hCurPen;
46 HCURSOR hCurAirbrush;
47 
48 HWND hToolBtn[16];
49 
50 HINSTANCE hProgInstance;
51 
52 TCHAR filepathname[1000];
53 BOOL isAFile = FALSE;
54 BOOL imageSaved = FALSE;
55 int fileSize;
56 int fileHPPM = 2834;
57 int fileVPPM = 2834;
58 SYSTEMTIME fileTime;
59 
60 BOOL showGrid = FALSE;
61 BOOL showMiniature = FALSE;
62 
63 CMainWindow mainWindow;
64 CFullscreenWindow fullscreenWindow;
65 CMiniatureWindow miniature;
66 CToolBox toolBoxContainer;
67 CToolSettingsWindow toolSettingsWindow;
68 CPaletteWindow paletteWindow;
69 CScrollboxWindow scrollboxWindow;
70 CScrollboxWindow scrlClientWindow;
71 CSelectionWindow selectionWindow;
72 CImgAreaWindow imageArea;
73 CSizeboxWindow sizeboxLeftTop;
74 CSizeboxWindow sizeboxCenterTop;
75 CSizeboxWindow sizeboxRightTop;
76 CSizeboxWindow sizeboxLeftCenter;
77 CSizeboxWindow sizeboxRightCenter;
78 CSizeboxWindow sizeboxLeftBottom;
79 CSizeboxWindow sizeboxCenterBottom;
80 CSizeboxWindow sizeboxRightBottom;
81 CTextEditWindow textEditWindow;
82 
83 // get file name extension from filter string
84 static BOOL
85 FileExtFromFilter(LPTSTR pExt, LPCTSTR pTitle, OPENFILENAME *pOFN)
86 {
87     LPTSTR pchExt = pExt;
88     *pchExt = 0;
89 
90     DWORD nIndex = 1;
91     for (LPCTSTR pch = pOFN->lpstrFilter; *pch; ++nIndex)
92     {
93         pch += lstrlen(pch) + 1;
94         if (pOFN->nFilterIndex == nIndex)
95         {
96             for (++pch; *pch && *pch != _T(';'); ++pch)
97             {
98                 *pchExt++ = *pch;
99             }
100             *pchExt = 0;
101             CharLower(pExt);
102             return TRUE;
103         }
104         pch += lstrlen(pch) + 1;
105     }
106     return FALSE;
107 }
108 
109 // Hook procedure for OPENFILENAME to change the file name extension
110 static UINT_PTR APIENTRY
111 OFNHookProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
112 {
113     HWND hParent;
114     OFNOTIFY *pon;
115     switch (uMsg)
116     {
117     case WM_NOTIFY:
118         pon = (OFNOTIFY *)lParam;
119         if (pon->hdr.code == CDN_TYPECHANGE)
120         {
121             hParent = GetParent(hwnd);
122             TCHAR Path[MAX_PATH];
123             SendMessage(hParent, CDM_GETFILEPATH, SIZEOF(Path), (LPARAM)Path);
124             LPTSTR pchTitle = _tcsrchr(Path, _T('\\'));
125             if (pchTitle == NULL)
126                 pchTitle = _tcsrchr(Path, _T('/'));
127 
128             LPTSTR pch = _tcsrchr((pchTitle ? pchTitle : Path), _T('.'));
129             if (pch && pchTitle)
130             {
131                 pchTitle++;
132                 *pch = 0;
133                 FileExtFromFilter(pch, pchTitle, pon->lpOFN);
134                 SendMessage(hParent, CDM_SETCONTROLTEXT, 0x047c, (LPARAM)pchTitle);
135                 lstrcpyn(pon->lpOFN->lpstrFile, Path, pon->lpOFN->nMaxFile);
136             }
137         }
138         break;
139     }
140     return 0;
141 }
142 
143 /* entry point */
144 
145 int WINAPI
146 _tWinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgument, int nFunsterStil)
147 {
148     HWND hwnd;               /* This is the handle for our window */
149     MSG messages;            /* Here messages to the application are saved */
150 
151     HMENU menu;
152     HACCEL haccel;
153 
154     TCHAR sfnFilename[1000];
155     TCHAR sfnFiletitle[256];
156     TCHAR ofnFilename[1000];
157     TCHAR ofnFiletitle[256];
158     TCHAR miniaturetitle[100];
159     static COLORREF custColors[16] = {
160         0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
161         0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff
162     };
163 
164     /* init font for text tool */
165     ZeroMemory(&lfTextFont, sizeof(lfTextFont));
166     lfTextFont.lfHeight = 0;
167     lfTextFont.lfWeight = FW_NORMAL;
168     lfTextFont.lfCharSet = DEFAULT_CHARSET;
169     hfontTextFont = CreateFontIndirect(&lfTextFont);
170 
171     hProgInstance = hThisInstance;
172 
173     /* initialize common controls library */
174     InitCommonControls();
175 
176     LoadString(hThisInstance, IDS_DEFAULTFILENAME, filepathname, SIZEOF(filepathname));
177     CPath pathFileName(filepathname);
178     pathFileName.StripPath();
179     CString strTitle;
180     strTitle.Format(IDS_WINDOWTITLE, (LPCTSTR)pathFileName);
181     LoadString(hThisInstance, IDS_MINIATURETITLE, miniaturetitle, SIZEOF(miniaturetitle));
182 
183     /* load settings from registry */
184     registrySettings.Load();
185     showMiniature = registrySettings.ShowThumbnail;
186     imageModel.Crop(registrySettings.BMPWidth, registrySettings.BMPHeight);
187 
188     /* create main window */
189     RECT mainWindowPos = {0, 0, 544, 375};	// FIXME: use equivalent of CW_USEDEFAULT for position
190     hwnd = mainWindow.Create(HWND_DESKTOP, mainWindowPos, strTitle, WS_OVERLAPPEDWINDOW);
191 
192     RECT fullscreenWindowPos = {0, 0, 100, 100};
193     fullscreenWindow.Create(HWND_DESKTOP, fullscreenWindowPos, NULL, WS_POPUPWINDOW | WS_MAXIMIZE);
194 
195     RECT miniaturePos = {(LONG) registrySettings.ThumbXPos, (LONG) registrySettings.ThumbYPos,
196                          (LONG) registrySettings.ThumbXPos + (LONG) registrySettings.ThumbWidth,
197                          (LONG) registrySettings.ThumbYPos + (LONG) registrySettings.ThumbHeight};
198     miniature.Create(hwnd, miniaturePos, miniaturetitle,
199                      WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, WS_EX_PALETTEWINDOW);
200     miniature.ShowWindow(showMiniature ? SW_SHOW : SW_HIDE);
201 
202     /* loading and setting the window menu from resource */
203     menu = LoadMenu(hThisInstance, MAKEINTRESOURCE(ID_MENU));
204     SetMenu(hwnd, menu);
205     haccel = LoadAccelerators(hThisInstance, MAKEINTRESOURCE(800));
206 
207     /* preloading the draw transparent/nontransparent icons for later use */
208     hNontranspIcon =
209         (HICON) LoadImage(hThisInstance, MAKEINTRESOURCE(IDI_NONTRANSPARENT), IMAGE_ICON, 40, 30, LR_DEFAULTCOLOR);
210     hTranspIcon =
211         (HICON) LoadImage(hThisInstance, MAKEINTRESOURCE(IDI_TRANSPARENT), IMAGE_ICON, 40, 30, LR_DEFAULTCOLOR);
212 
213     hCurFill     = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_FILL));
214     hCurColor    = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_COLOR));
215     hCurZoom     = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_ZOOM));
216     hCurPen      = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_PEN));
217     hCurAirbrush = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_AIRBRUSH));
218 
219     CreateWindowEx(0, _T("STATIC"), NULL, WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ, 0, 0, 5000, 2, hwnd, NULL,
220                    hThisInstance, NULL);
221 
222     RECT toolBoxContainerPos = {2, 2, 2 + 52, 2 + 350};
223     toolBoxContainer.Create(hwnd, toolBoxContainerPos, NULL, WS_CHILD | WS_VISIBLE);
224     /* creating the tool settings child window */
225     RECT toolSettingsWindowPos = {5, 208, 5 + 42, 208 + 140};
226     toolSettingsWindow.Create(toolBoxContainer.m_hWnd, toolSettingsWindowPos, NULL, WS_CHILD | WS_VISIBLE);
227 
228     /* creating the palette child window */
229     RECT paletteWindowPos = {56, 9, 56 + 255, 9 + 32};
230     paletteWindow.Create(hwnd, paletteWindowPos, NULL, WS_CHILD | WS_VISIBLE);
231 
232     /* creating the scroll box */
233     RECT scrollboxWindowPos = {56, 49, 56 + 472, 49 + 248};
234     scrollboxWindow.Create(hwnd, scrollboxWindowPos, NULL,
235                            WS_CHILD | WS_GROUP | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE, WS_EX_CLIENTEDGE);
236 
237     /* creating the status bar */
238     hStatusBar =
239         CreateWindowEx(0, STATUSCLASSNAME, NULL, SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd,
240                        NULL, hThisInstance, NULL);
241     SendMessage(hStatusBar, SB_SETMINHEIGHT, 21, 0);
242 
243     RECT scrlClientWindowPos = {0, 0, 0 + 500, 0 + 500};
244     scrlClientWindow.Create(scrollboxWindow.m_hWnd, scrlClientWindowPos, NULL, WS_CHILD | WS_VISIBLE);
245 
246     /* create selection window (initially hidden) */
247     RECT selectionWindowPos = {350, 0, 350 + 100, 0 + 100};
248     selectionWindow.Create(scrlClientWindow.m_hWnd, selectionWindowPos, NULL, WS_CHILD | BS_OWNERDRAW);
249 
250     /* creating the window inside the scroll box, on which the image in hDrawingDC's bitmap is drawn */
251     RECT imageAreaPos = {3, 3, 3 + imageModel.GetWidth(), 3 + imageModel.GetHeight()};
252     imageArea.Create(scrlClientWindow.m_hWnd, imageAreaPos, NULL, WS_CHILD | WS_VISIBLE);
253 
254     if (__argc >= 2)
255     {
256         DoLoadImageFile(mainWindow, __targv[1], TRUE);
257     }
258 
259     /* initializing the CHOOSECOLOR structure for use with ChooseColor */
260     ZeroMemory(&choosecolor, sizeof(choosecolor));
261     choosecolor.lStructSize    = sizeof(CHOOSECOLOR);
262     choosecolor.hwndOwner      = hwnd;
263     choosecolor.rgbResult      = 0x00ffffff;
264     choosecolor.lpCustColors   = custColors;
265 
266     /* initializing the OPENFILENAME structure for use with GetOpenFileName and GetSaveFileName */
267     CopyMemory(ofnFilename, filepathname, sizeof(filepathname));
268     CString strImporters;
269     CSimpleArray<GUID> aguidFileTypesI;
270     CString strAllPictureFiles;
271     strAllPictureFiles.LoadString(hThisInstance, IDS_ALLPICTUREFILES);
272     CImage::GetImporterFilterString(strImporters, aguidFileTypesI, strAllPictureFiles, CImage::excludeDefaultLoad, _T('\0'));
273 //     CAtlStringW strAllFiles;
274 //     strAllFiles.LoadString(hThisInstance, IDS_ALLFILES);
275 //     strImporters = strAllFiles + CAtlStringW(_T("|*.*|")).Replace('|', '\0') + strImporters;
276     ZeroMemory(&ofn, sizeof(OPENFILENAME));
277     ofn.lStructSize    = sizeof(OPENFILENAME);
278     ofn.hwndOwner      = hwnd;
279     ofn.hInstance      = hThisInstance;
280     ofn.lpstrFilter    = strImporters;
281     ofn.lpstrFile      = ofnFilename;
282     ofn.nMaxFile       = SIZEOF(ofnFilename);
283     ofn.lpstrFileTitle = ofnFiletitle;
284     ofn.nMaxFileTitle  = SIZEOF(ofnFiletitle);
285     ofn.Flags          = OFN_HIDEREADONLY;
286     ofn.lpstrDefExt    = L"bmp";
287 
288     CopyMemory(sfnFilename, filepathname, sizeof(filepathname));
289     CString strExporters;
290     CSimpleArray<GUID> aguidFileTypesE;
291     CImage::GetExporterFilterString(strExporters, aguidFileTypesE, NULL, CImage::excludeDefaultSave, _T('\0'));
292     ZeroMemory(&sfn, sizeof(OPENFILENAME));
293     sfn.lStructSize    = sizeof(OPENFILENAME);
294     sfn.hwndOwner      = hwnd;
295     sfn.hInstance      = hThisInstance;
296     sfn.lpstrFilter    = strExporters;
297     sfn.lpstrFile      = sfnFilename;
298     sfn.nMaxFile       = SIZEOF(sfnFilename);
299     sfn.lpstrFileTitle = sfnFiletitle;
300     sfn.nMaxFileTitle  = SIZEOF(sfnFiletitle);
301     sfn.Flags          = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLEHOOK;
302     sfn.lpfnHook       = OFNHookProc;
303     sfn.lpstrDefExt    = L"bmp";
304 
305     /* creating the size boxes */
306     RECT sizeboxPos = {0, 0, 0 + 3, 0 + 3};
307     sizeboxLeftTop.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
308     sizeboxCenterTop.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
309     sizeboxRightTop.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
310     sizeboxLeftCenter.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
311     sizeboxRightCenter.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
312     sizeboxLeftBottom.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
313     sizeboxCenterBottom.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
314     sizeboxRightBottom.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
315     /* placing the size boxes around the image */
316     imageArea.SendMessage(WM_SIZE, 0, 0);
317 
318     /* by moving the window, the things in WM_SIZE are done */
319     mainWindow.SetWindowPlacement(&(registrySettings.WindowPlacement));
320 
321     /* creating the text editor window for the text tool */
322     RECT textEditWindowPos = {300, 0, 300 + 300, 0 + 200};
323     textEditWindow.Create(hwnd, textEditWindowPos, NULL, WS_OVERLAPPEDWINDOW);
324 
325     /* Make the window visible on the screen */
326     ShowWindow (hwnd, nFunsterStil);
327 
328     /* inform the system, that the main window accepts dropped files */
329     DragAcceptFiles(hwnd, TRUE);
330 
331     /* Run the message loop. It will run until GetMessage() returns 0 */
332     while (GetMessage(&messages, NULL, 0, 0))
333     {
334         TranslateAccelerator(hwnd, haccel, &messages);
335 
336         /* Translate virtual-key messages into character messages */
337         TranslateMessage(&messages);
338         /* Send message to WindowProcedure */
339         DispatchMessage(&messages);
340     }
341 
342     /* write back settings to registry */
343     registrySettings.ShowThumbnail = showMiniature;
344     registrySettings.BMPWidth = imageModel.GetWidth();
345     registrySettings.BMPHeight = imageModel.GetHeight();
346     registrySettings.Store();
347 
348     /* The program return-value is 0 - The value that PostQuitMessage() gave */
349     return messages.wParam;
350 }
351