* only show message box on exit if the image has not been saved and is not blank
[reactos.git] / reactos / base / applications / paint / main.c
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: main.c
5 * PURPOSE: Initializing everything
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include <windows.h>
12 #include <windowsx.h>
13 #include <commctrl.h>
14 #include <stdio.h>
15 #include <tchar.h>
16 #include "definitions.h"
17
18 #include "drawing.h"
19 #include "dib.h"
20
21 #include "globalvar.h"
22 #include "history.h"
23 #include "mouse.h"
24
25 #include "winproc.h"
26 #include "palette.h"
27 #include "toolsettings.h"
28 #include "selection.h"
29 #include "sizebox.h"
30
31 /* FUNCTIONS ********************************************************/
32
33 HDC hDrawingDC;
34 HDC hSelDC;
35 int *bmAddress;
36 BITMAPINFO bitmapinfo;
37 int imgXRes = 400;
38 int imgYRes = 300;
39
40 HBITMAP hBms[HISTORYSIZE];
41 int currInd = 0;
42 int undoSteps = 0;
43 int redoSteps = 0;
44 BOOL imageSaved = TRUE;
45
46 // global status variables
47
48 short startX;
49 short startY;
50 short lastX;
51 short lastY;
52 int lineWidth = 1;
53 int shapeStyle = 0;
54 int brushStyle = 0;
55 int activeTool = 7;
56 int airBrushWidth = 5;
57 int rubberRadius = 4;
58 int transpBg = 0;
59 int zoom = 1000;
60 int rectSel_src[4];
61 int rectSel_dest[4];
62 HWND hSelection;
63 HWND hImageArea;
64 HBITMAP hSelBm;
65
66
67 // global declarations and WinMain
68
69
70 // initial palette colors; may be changed by the user during execution
71 int palColors[28] =
72 {0x000000, 0x464646, 0x787878, 0x300099, 0x241ced, 0x0078ff, 0x0ec2ff,
73 0x00f2ff, 0x1de6a8, 0x4cb122, 0xefb700, 0xf36d4d, 0x99362f, 0x98316f,
74 0xffffff, 0xdcdcdc, 0xb4b4b4, 0x3c5a9c, 0xb1a3ff, 0x7aaae5, 0x9ce4f5,
75 0xbdf9ff, 0xbcf9d3, 0x61bb9d, 0xead999, 0xd19a70, 0x8e6d54, 0xd5a5b5};
76 // foreground and background colors with initial value
77 int fgColor = 0x00000000;
78 int bgColor = 0x00ffffff;
79 // the current zoom in percent*10
80 HWND hStatusBar;
81 HWND hScrollbox;
82 HWND hMainWnd;
83 HWND hPalWin;
84 HWND hToolSettings;
85 CHOOSECOLOR choosecolor;
86 OPENFILENAME ofn;
87 OPENFILENAME sfn;
88 HICON hNontranspIcon;
89 HICON hTranspIcon;
90
91 HCURSOR hCurFill;
92 HCURSOR hCurColor;
93 HCURSOR hCurZoom;
94 HCURSOR hCurPen;
95 HCURSOR hCurAirbrush;
96
97 HWND hScrlClient;
98
99 HWND hToolBtn[16];
100
101 HINSTANCE hProgInstance;
102
103 TCHAR filename[256];
104 TCHAR filepathname[1000];
105 BOOL isAFile = FALSE;
106
107 BOOL showGrid = FALSE;
108 BOOL showMiniature = FALSE;
109
110 HWND hwndMiniature;
111
112 HWND hSizeboxLeftTop;
113 HWND hSizeboxCenterTop;
114 HWND hSizeboxRightTop;
115 HWND hSizeboxLeftCenter;
116 HWND hSizeboxRightCenter;
117 HWND hSizeboxLeftBottom;
118 HWND hSizeboxCenterBottom;
119 HWND hSizeboxRightBottom;
120
121 HWND hTrackbarZoom;
122
123 int WINAPI _tWinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgument, int nFunsterStil)
124 {
125 HWND hwnd; /* This is the handle for our window */
126 MSG messages; /* Here messages to the application are saved */
127 WNDCLASSEX wclScroll;
128 WNDCLASSEX wincl;
129 WNDCLASSEX wclPal;
130 WNDCLASSEX wclSettings;
131 WNDCLASSEX wclSelection;
132 TCHAR progtitle[1000];
133 TCHAR resstr[100];
134 HMENU menu;
135 HWND hToolbar;
136 HIMAGELIST hImageList;
137 HANDLE haccel;
138 HBITMAP tempBm;
139 int i;
140 TCHAR tooltips[16][30];
141 TCHAR *c;
142 TCHAR sfnFilename[1000];
143 TCHAR sfnFiletitle[256];
144 TCHAR sfnFilter[1000];
145 TCHAR ofnFilename[1000];
146 TCHAR ofnFiletitle[256];
147 TCHAR ofnFilter[1000];
148 TCHAR miniaturetitle[100];
149 int custColors[16] =
150 {0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
151 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff};
152
153 hProgInstance = hThisInstance;
154
155 /* Necessary */
156 InitCommonControls();
157
158 /* initializing and registering the window class used for the main window */
159 wincl.hInstance = hThisInstance;
160 wincl.lpszClassName = _T("WindowsApp");
161 wincl.lpfnWndProc = WindowProcedure;
162 wincl.style = CS_DBLCLKS;
163 wincl.cbSize = sizeof (WNDCLASSEX);
164 wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
165 wincl.hIconSm = LoadIcon (hThisInstance, MAKEINTRESOURCE(500));
166 wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
167 wincl.lpszMenuName = NULL;
168 wincl.cbClsExtra = 0;
169 wincl.cbWndExtra = 0;
170 wincl.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
171 RegisterClassEx (&wincl);
172
173 /* initializing and registering the window class used for the scroll box */
174 wclScroll.hInstance = hThisInstance;
175 wclScroll.lpszClassName = _T("Scrollbox");
176 wclScroll.lpfnWndProc = WindowProcedure;
177 wclScroll.style = 0;
178 wclScroll.cbSize = sizeof (WNDCLASSEX);
179 wclScroll.hIcon = NULL;
180 wclScroll.hIconSm = NULL;
181 wclScroll.hCursor = LoadCursor (NULL, IDC_ARROW);
182 wclScroll.lpszMenuName = NULL;
183 wclScroll.cbClsExtra = 0;
184 wclScroll.cbWndExtra = 0;
185 wclScroll.hbrBackground = GetSysColorBrush(COLOR_APPWORKSPACE);
186 RegisterClassEx (&wclScroll);
187
188 /* initializing and registering the window class used for the palette window */
189 wclPal.hInstance = hThisInstance;
190 wclPal.lpszClassName = _T("Palette");
191 wclPal.lpfnWndProc = PalWinProc;
192 wclPal.style = CS_DBLCLKS;
193 wclPal.cbSize = sizeof (WNDCLASSEX);
194 wclPal.hIcon = NULL;
195 wclPal.hIconSm = NULL;
196 wclPal.hCursor = LoadCursor (NULL, IDC_ARROW);
197 wclPal.lpszMenuName = NULL;
198 wclPal.cbClsExtra = 0;
199 wclPal.cbWndExtra = 0;
200 wclPal.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
201 RegisterClassEx (&wclPal);
202
203 /* initializing and registering the window class for the settings window */
204 wclSettings.hInstance = hThisInstance;
205 wclSettings.lpszClassName = _T("ToolSettings");
206 wclSettings.lpfnWndProc = SettingsWinProc;
207 wclSettings.style = CS_DBLCLKS;
208 wclSettings.cbSize = sizeof (WNDCLASSEX);
209 wclSettings.hIcon = NULL;
210 wclSettings.hIconSm = NULL;
211 wclSettings.hCursor = LoadCursor (NULL, IDC_ARROW);
212 wclSettings.lpszMenuName = NULL;
213 wclSettings.cbClsExtra = 0;
214 wclSettings.cbWndExtra = 0;
215 wclSettings.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
216 RegisterClassEx (&wclSettings);
217
218 /* initializing and registering the window class for the selection frame */
219 wclSelection.hInstance = hThisInstance;
220 wclSelection.lpszClassName = _T("Selection");
221 wclSelection.lpfnWndProc = SelectionWinProc;
222 wclSelection.style = CS_DBLCLKS;
223 wclSelection.cbSize = sizeof (WNDCLASSEX);
224 wclSelection.hIcon = NULL;
225 wclSelection.hIconSm = NULL;
226 wclSelection.hCursor = LoadCursor (NULL, IDC_SIZEALL);
227 wclSelection.lpszMenuName = NULL;
228 wclSelection.cbClsExtra = 0;
229 wclSelection.cbWndExtra = 0;
230 wclSelection.hbrBackground = NULL;//GetSysColorBrush(COLOR_BTNFACE);
231 RegisterClassEx (&wclSelection);
232
233 /* initializing and registering the window class for the size boxes */
234 wclSettings.hInstance = hThisInstance;
235 wclSettings.lpszClassName = _T("Sizebox");
236 wclSettings.lpfnWndProc = SizeboxWinProc;
237 wclSettings.style = CS_DBLCLKS;
238 wclSettings.cbSize = sizeof (WNDCLASSEX);
239 wclSettings.hIcon = NULL;
240 wclSettings.hIconSm = NULL;
241 wclSettings.hCursor = LoadCursor (NULL, IDC_ARROW);
242 wclSettings.lpszMenuName = NULL;
243 wclSettings.cbClsExtra = 0;
244 wclSettings.cbWndExtra = 0;
245 wclSettings.hbrBackground = GetSysColorBrush(COLOR_HIGHLIGHT);
246 RegisterClassEx (&wclSettings);
247
248 LoadString(hThisInstance, IDS_DEFAULTFILENAME, filename, SIZEOF(filename));
249 LoadString(hThisInstance, IDS_WINDOWTITLE, resstr, SIZEOF(resstr));
250 _stprintf(progtitle, resstr, filename);
251 LoadString(hThisInstance, IDS_MINIATURETITLE, miniaturetitle, SIZEOF(miniaturetitle));
252
253 /* create main window */
254 hwnd = CreateWindowEx (0, _T("WindowsApp"), progtitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL);
255
256 hMainWnd = hwnd;
257 hwndMiniature = CreateWindowEx(WS_EX_PALETTEWINDOW, _T("WindowsApp"), miniaturetitle, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, 180, 200, 120, 100, hwnd, NULL, hThisInstance, NULL);
258
259 /* loading and setting the window menu from resource */
260 menu = LoadMenu(hThisInstance, MAKEINTRESOURCE(ID_MENU));
261 SetMenu(hwnd, menu);
262 haccel = LoadAccelerators(hThisInstance, MAKEINTRESOURCE(800));
263
264 /* preloading the draw transparent/nontransparent icons for later use */
265 hNontranspIcon = LoadImage(hThisInstance, MAKEINTRESOURCE(IDI_NONTRANSPARENT), IMAGE_ICON, 40, 30, LR_DEFAULTCOLOR);
266 hTranspIcon = LoadImage(hThisInstance, MAKEINTRESOURCE(IDI_TRANSPARENT), IMAGE_ICON, 40, 30, LR_DEFAULTCOLOR);
267
268 hCurFill = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_FILL));
269 hCurColor = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_COLOR));
270 hCurZoom = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_ZOOM));
271 hCurPen = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_PEN));
272 hCurAirbrush = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_AIRBRUSH));
273
274 CreateWindowEx (0, _T("STATIC"), _T(""), WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ, 0, 0, 5000, 2, hwnd, NULL, hThisInstance, NULL);
275
276 /* creating the 16 bitmap radio buttons and setting the bitmap */
277
278
279 /* FIXME: Unintentionally there is a line above the tool bar. To prevent cropping of the buttons height has been increased from 200 to 205 */
280 hToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | CCS_NOPARENTALIGN | CCS_VERT | CCS_NORESIZE | TBSTYLE_TOOLTIPS, 3, 3, 50, 205, hwnd, NULL, hThisInstance, NULL);
281 hImageList = ImageList_Create(16, 16, ILC_COLOR24 | ILC_MASK, 16, 0);
282 SendMessage(hToolbar, TB_SETIMAGELIST, 0, (LPARAM)hImageList);
283 tempBm = LoadImage(hThisInstance, MAKEINTRESOURCE(IDB_TOOLBARICONS), IMAGE_BITMAP, 256, 16, 0);
284 ImageList_AddMasked(hImageList, tempBm, 0xff00ff);
285 DeleteObject(tempBm);
286 SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
287 for (i=0; i<16; i++)
288 {
289 TBBUTTON tbbutton;
290 int wrapnow = 0;
291
292 if (i % 2 == 1) wrapnow = TBSTATE_WRAP;
293 LoadString(hThisInstance, IDS_TOOLTIP1 + i, tooltips[i], 30);
294 ZeroMemory(&tbbutton, sizeof(TBBUTTON));
295 tbbutton.iString = (INT_PTR)tooltips[i];
296 tbbutton.fsStyle = TBSTYLE_CHECKGROUP;
297 tbbutton.fsState = TBSTATE_ENABLED | wrapnow;
298 tbbutton.idCommand = ID_FREESEL + i;
299 tbbutton.iBitmap = i;
300 SendMessage(hToolbar, TB_ADDBUTTONS, 1, (LPARAM)&tbbutton);
301 }
302 /* SendMessage(hToolbar, TB_SETROWS, MAKEWPARAM(8, FALSE), (LPARAM)NULL); */
303 SendMessage(hToolbar, TB_CHECKBUTTON, ID_PEN, MAKELONG(TRUE, 0));
304 SendMessage(hToolbar, TB_SETMAXTEXTROWS, 0, 0);
305
306 SendMessage(hToolbar, TB_SETBUTTONSIZE, 0, MAKELONG(25, 25));
307 /* SendMessage(hToolbar, TB_AUTOSIZE, 0, 0); */
308
309 /* creating the tool settings child window */
310 hToolSettings = CreateWindowEx(0, _T("ToolSettings"), _T(""), WS_CHILD | WS_VISIBLE, 7, 210, 42, 140, hwnd, NULL, hThisInstance, NULL);
311 hTrackbarZoom = CreateWindowEx(0, TRACKBAR_CLASS, _T(""), WS_CHILD | TBS_VERT | TBS_AUTOTICKS, 1, 1, 40, 64, hToolSettings, NULL, hThisInstance, NULL);
312 SendMessage(hTrackbarZoom, TBM_SETRANGE, (WPARAM)TRUE, (LPARAM)MAKELONG(0, 6));
313 SendMessage(hTrackbarZoom, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)3);
314
315 /* creating the palette child window */
316 hPalWin = CreateWindowEx(0, _T("Palette"), _T(""), WS_CHILD | WS_VISIBLE, 56, 9, 255, 32, hwnd, NULL, hThisInstance, NULL);
317
318 /* creating the scroll box */
319 hScrollbox = CreateWindowEx (WS_EX_CLIENTEDGE, _T("Scrollbox"), _T(""), WS_CHILD | WS_GROUP | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE, 56, 49, 472, 248, hwnd, NULL, hThisInstance, NULL);
320
321 /* creating the status bar */
322 hStatusBar = CreateWindowEx (0, STATUSCLASSNAME, _T(""), SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, NULL, hThisInstance, NULL);
323 SendMessage(hStatusBar, SB_SETMINHEIGHT, 21, 0);
324
325 hScrlClient = CreateWindowEx(0, _T("Scrollbox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 500, 500, hScrollbox, NULL, hThisInstance, NULL);
326
327 /* create selection window (initially hidden) */
328 hSelection = CreateWindowEx(WS_EX_TRANSPARENT, _T("Selection"), _T(""), WS_CHILD | BS_OWNERDRAW, 350, 0, 100, 100, hScrlClient, NULL, hThisInstance, NULL);
329
330 /* creating the window inside the scroll box, on which the image in hDrawingDC's bitmap is drawn */
331 hImageArea = CreateWindowEx (0, _T("Scrollbox"), _T(""), WS_CHILD | WS_VISIBLE, 3, 3, imgXRes, imgYRes, hScrlClient, NULL, hThisInstance, NULL);
332
333 hDrawingDC = CreateCompatibleDC(GetDC(hImageArea));
334 hSelDC = CreateCompatibleDC(GetDC(hImageArea));
335 SelectObject(hDrawingDC, CreatePen(PS_SOLID, 0, fgColor));
336 SelectObject(hDrawingDC, CreateSolidBrush(bgColor));
337
338 hBms[0] = CreateDIBWithProperties(imgXRes, imgYRes);
339 SelectObject(hDrawingDC, hBms[0]);
340 Rectangle(hDrawingDC, 0-1, 0-1, imgXRes+1, imgYRes+1);
341
342 /* initializing the CHOOSECOLOR structure for use with ChooseColor */
343 choosecolor.lStructSize = sizeof(CHOOSECOLOR);
344 choosecolor.hwndOwner = hwnd;
345 choosecolor.hInstance = NULL;
346 choosecolor.rgbResult = 0x00ffffff;
347 choosecolor.lpCustColors = (COLORREF*)&custColors;
348 choosecolor.Flags = 0;
349 choosecolor.lCustData = 0;
350 choosecolor.lpfnHook = NULL;
351 choosecolor.lpTemplateName = NULL;
352
353 /* initializing the OPENFILENAME structure for use with GetOpenFileName and GetSaveFileName */
354 CopyMemory(ofnFilename, filename, sizeof(filename));
355 LoadString(hThisInstance, IDS_OPENFILTER, ofnFilter, SIZEOF(ofnFilter));
356 for (c = ofnFilter; *c; c++) if (*c == '\1') *c = '\0';
357 ZeroMemory(&ofn, sizeof(OPENFILENAME));
358 ofn.lStructSize = sizeof (OPENFILENAME);
359 ofn.hwndOwner = hwnd;
360 ofn.hInstance = hThisInstance;
361 ofn.lpstrFilter = ofnFilter;
362 ofn.lpstrFile = ofnFilename;
363 ofn.nMaxFile = SIZEOF(ofnFilename);
364 ofn.lpstrFileTitle = ofnFiletitle;
365 ofn.nMaxFileTitle = SIZEOF(ofnFiletitle);
366 ofn.Flags = OFN_HIDEREADONLY;
367
368 CopyMemory(sfnFilename, filename, sizeof(filename));
369 LoadString(hThisInstance, IDS_SAVEFILTER, sfnFilter, SIZEOF(sfnFilter));
370 for (c = sfnFilter; *c; c++) if (*c == '\1') *c = '\0';
371 ZeroMemory(&sfn, sizeof(OPENFILENAME));
372 sfn.lStructSize = sizeof (OPENFILENAME);
373 sfn.hwndOwner = hwnd;
374 sfn.hInstance = hThisInstance;
375 sfn.lpstrFilter = sfnFilter;
376 sfn.lpstrFile = sfnFilename;
377 sfn.nMaxFile = SIZEOF(sfnFilename);
378 sfn.lpstrFileTitle = sfnFiletitle;
379 sfn.nMaxFileTitle = SIZEOF(sfnFiletitle);
380 sfn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
381
382 hSizeboxLeftTop = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
383 hSizeboxCenterTop = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
384 hSizeboxRightTop = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
385 hSizeboxLeftCenter = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
386 hSizeboxRightCenter = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
387 hSizeboxLeftBottom = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
388 hSizeboxCenterBottom= CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
389 hSizeboxRightBottom = CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD | WS_VISIBLE, 0, 0, 3, 3, hScrlClient, NULL, hThisInstance, NULL);
390 SendMessage(hImageArea, WM_SIZE, 0, 0);
391
392 /* by moving the window, the things in WM_SIZE are done */
393 MoveWindow(hwnd, 100, 100, 600, 450, TRUE);
394
395 /* Make the window visible on the screen */
396 ShowWindow (hwnd, nFunsterStil);
397
398 /* Run the message loop. It will run until GetMessage() returns 0 */
399 while (GetMessage (&messages, NULL, 0, 0))
400 {
401 TranslateAccelerator(hwnd, haccel, &messages);
402
403 /* Translate virtual-key messages into character messages */
404 TranslateMessage(&messages);
405 /* Send message to WindowProcedure */
406 DispatchMessage(&messages);
407 }
408
409 /* The program return-value is 0 - The value that PostQuitMessage() gave */
410 return messages.wParam;
411 }