2 * PROJECT: PAINT for ReactOS
4 * FILE: base/applications/paint/main.c
5 * PURPOSE: Initializing everything
6 * PROGRAMMERS: Benedikt Freisen
9 /* INCLUDES *********************************************************/
16 #include "definitions.h"
21 #include "globalvar.h"
27 #include "toolsettings.h"
28 #include "selection.h"
31 /* FUNCTIONS ********************************************************/
36 BITMAPINFO bitmapinfo
;
40 HBITMAP hBms
[HISTORYSIZE
];
44 BOOL imageSaved
= TRUE
;
53 int activeTool
= TOOL_PEN
;
54 int airBrushWidth
= 5;
65 /* initial palette colors; may be changed by the user during execution */
66 int palColors
[28] = { 0x000000, 0x464646, 0x787878, 0x300099, 0x241ced, 0x0078ff, 0x0ec2ff,
67 0x00f2ff, 0x1de6a8, 0x4cb122, 0xefb700, 0xf36d4d, 0x99362f, 0x98316f,
68 0xffffff, 0xdcdcdc, 0xb4b4b4, 0x3c5a9c, 0xb1a3ff, 0x7aaae5, 0x9ce4f5,
69 0xbdf9ff, 0xbcf9d3, 0x61bb9d, 0xead999, 0xd19a70, 0x8e6d54, 0xd5a5b5
72 /* foreground and background colors with initial value */
73 int fgColor
= 0x00000000;
74 int bgColor
= 0x00ffffff;
80 HWND hToolBoxContainer
;
83 CHOOSECOLOR choosecolor
;
99 HINSTANCE hProgInstance
;
102 TCHAR filepathname
[1000];
103 BOOL isAFile
= FALSE
;
109 BOOL showGrid
= FALSE
;
110 BOOL showMiniature
= FALSE
;
114 HWND hSizeboxLeftTop
;
115 HWND hSizeboxCenterTop
;
116 HWND hSizeboxRightTop
;
117 HWND hSizeboxLeftCenter
;
118 HWND hSizeboxRightCenter
;
119 HWND hSizeboxLeftBottom
;
120 HWND hSizeboxCenterBottom
;
121 HWND hSizeboxRightBottom
;
126 _tWinMain (HINSTANCE hThisInstance
, HINSTANCE hPrevInstance
, LPTSTR lpszArgument
, int nFunsterStil
)
128 HWND hwnd
; /* This is the handle for our window */
129 MSG messages
; /* Here messages to the application are saved */
131 WNDCLASSEX wclScroll
;
134 WNDCLASSEX wclSettings
;
135 WNDCLASSEX wclSelection
;
137 TCHAR progtitle
[1000];
141 HIMAGELIST hImageList
;
145 TCHAR tooltips
[16][30];
149 TCHAR sfnFilename
[1000];
150 TCHAR sfnFiletitle
[256];
151 TCHAR sfnFilter
[1000];
152 TCHAR ofnFilename
[1000];
153 TCHAR ofnFiletitle
[256];
154 TCHAR ofnFilter
[1000];
155 TCHAR miniaturetitle
[100];
156 static int custColors
[16] = { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
157 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff
160 hProgInstance
= hThisInstance
;
163 InitCommonControls();
165 /* initializing and registering the window class used for the main window */
166 wincl
.hInstance
= hThisInstance
;
167 wincl
.lpszClassName
= _T("WindowsApp");
168 wincl
.lpfnWndProc
= WindowProcedure
;
169 wincl
.style
= CS_DBLCLKS
;
170 wincl
.cbSize
= sizeof(WNDCLASSEX
);
171 wincl
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
172 wincl
.hIconSm
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(500));
173 wincl
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
174 wincl
.lpszMenuName
= NULL
;
175 wincl
.cbClsExtra
= 0;
176 wincl
.cbWndExtra
= 0;
177 wincl
.hbrBackground
= GetSysColorBrush(COLOR_BTNFACE
);
178 RegisterClassEx (&wincl
);
180 /* initializing and registering the window class used for the scroll box */
181 wclScroll
.hInstance
= hThisInstance
;
182 wclScroll
.lpszClassName
= _T("Scrollbox");
183 wclScroll
.lpfnWndProc
= WindowProcedure
;
185 wclScroll
.cbSize
= sizeof(WNDCLASSEX
);
186 wclScroll
.hIcon
= NULL
;
187 wclScroll
.hIconSm
= NULL
;
188 wclScroll
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
189 wclScroll
.lpszMenuName
= NULL
;
190 wclScroll
.cbClsExtra
= 0;
191 wclScroll
.cbWndExtra
= 0;
192 wclScroll
.hbrBackground
= GetSysColorBrush(COLOR_APPWORKSPACE
);
193 RegisterClassEx (&wclScroll
);
195 /* initializing and registering the window class used for the palette window */
196 wclPal
.hInstance
= hThisInstance
;
197 wclPal
.lpszClassName
= _T("Palette");
198 wclPal
.lpfnWndProc
= PalWinProc
;
199 wclPal
.style
= CS_DBLCLKS
;
200 wclPal
.cbSize
= sizeof(WNDCLASSEX
);
202 wclPal
.hIconSm
= NULL
;
203 wclPal
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
204 wclPal
.lpszMenuName
= NULL
;
205 wclPal
.cbClsExtra
= 0;
206 wclPal
.cbWndExtra
= 0;
207 wclPal
.hbrBackground
= GetSysColorBrush(COLOR_BTNFACE
);
208 RegisterClassEx (&wclPal
);
210 /* initializing and registering the window class for the settings window */
211 wclSettings
.hInstance
= hThisInstance
;
212 wclSettings
.lpszClassName
= _T("ToolSettings");
213 wclSettings
.lpfnWndProc
= SettingsWinProc
;
214 wclSettings
.style
= CS_DBLCLKS
;
215 wclSettings
.cbSize
= sizeof(WNDCLASSEX
);
216 wclSettings
.hIcon
= NULL
;
217 wclSettings
.hIconSm
= NULL
;
218 wclSettings
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
219 wclSettings
.lpszMenuName
= NULL
;
220 wclSettings
.cbClsExtra
= 0;
221 wclSettings
.cbWndExtra
= 0;
222 wclSettings
.hbrBackground
= GetSysColorBrush(COLOR_BTNFACE
);
223 RegisterClassEx (&wclSettings
);
225 /* initializing and registering the window class for the selection frame */
226 wclSelection
.hInstance
= hThisInstance
;
227 wclSelection
.lpszClassName
= _T("Selection");
228 wclSelection
.lpfnWndProc
= SelectionWinProc
;
229 wclSelection
.style
= CS_DBLCLKS
;
230 wclSelection
.cbSize
= sizeof(WNDCLASSEX
);
231 wclSelection
.hIcon
= NULL
;
232 wclSelection
.hIconSm
= NULL
;
233 wclSelection
.hCursor
= LoadCursor(NULL
, IDC_SIZEALL
);
234 wclSelection
.lpszMenuName
= NULL
;
235 wclSelection
.cbClsExtra
= 0;
236 wclSelection
.cbWndExtra
= 0;
237 wclSelection
.hbrBackground
= NULL
;
238 RegisterClassEx (&wclSelection
);
240 /* initializing and registering the window class for the size boxes */
241 wclSettings
.hInstance
= hThisInstance
;
242 wclSettings
.lpszClassName
= _T("Sizebox");
243 wclSettings
.lpfnWndProc
= SizeboxWinProc
;
244 wclSettings
.style
= CS_DBLCLKS
;
245 wclSettings
.cbSize
= sizeof(WNDCLASSEX
);
246 wclSettings
.hIcon
= NULL
;
247 wclSettings
.hIconSm
= NULL
;
248 wclSettings
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
249 wclSettings
.lpszMenuName
= NULL
;
250 wclSettings
.cbClsExtra
= 0;
251 wclSettings
.cbWndExtra
= 0;
252 wclSettings
.hbrBackground
= GetSysColorBrush(COLOR_HIGHLIGHT
);
253 RegisterClassEx (&wclSettings
);
255 LoadString(hThisInstance
, IDS_DEFAULTFILENAME
, filename
, SIZEOF(filename
));
256 LoadString(hThisInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
257 _stprintf(progtitle
, resstr
, filename
);
258 LoadString(hThisInstance
, IDS_MINIATURETITLE
, miniaturetitle
, SIZEOF(miniaturetitle
));
260 /* create main window */
262 CreateWindowEx(0, _T("WindowsApp"), progtitle
, WS_OVERLAPPEDWINDOW
, CW_USEDEFAULT
, CW_USEDEFAULT
, 544,
263 375, HWND_DESKTOP
, NULL
, hThisInstance
, NULL
);
267 CreateWindowEx(WS_EX_PALETTEWINDOW
, _T("WindowsApp"), miniaturetitle
,
268 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_THICKFRAME
, 180, 200, 120, 100, hwnd
,
269 NULL
, hThisInstance
, NULL
);
271 /* loading and setting the window menu from resource */
272 menu
= LoadMenu(hThisInstance
, MAKEINTRESOURCE(ID_MENU
));
274 haccel
= LoadAccelerators(hThisInstance
, MAKEINTRESOURCE(800));
276 /* preloading the draw transparent/nontransparent icons for later use */
278 LoadImage(hThisInstance
, MAKEINTRESOURCE(IDI_NONTRANSPARENT
), IMAGE_ICON
, 40, 30, LR_DEFAULTCOLOR
);
280 LoadImage(hThisInstance
, MAKEINTRESOURCE(IDI_TRANSPARENT
), IMAGE_ICON
, 40, 30, LR_DEFAULTCOLOR
);
282 hCurFill
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_FILL
));
283 hCurColor
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_COLOR
));
284 hCurZoom
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_ZOOM
));
285 hCurPen
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_PEN
));
286 hCurAirbrush
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_AIRBRUSH
));
288 CreateWindowEx(0, _T("STATIC"), _T(""), WS_CHILD
| WS_VISIBLE
| SS_ETCHEDHORZ
, 0, 0, 5000, 2, hwnd
, NULL
,
289 hThisInstance
, NULL
);
292 CreateWindowEx(0, _T("WindowsApp"), _T(""), WS_CHILD
| WS_VISIBLE
, 2, 2, 52, 350, hwnd
, NULL
,
293 hThisInstance
, NULL
);
294 /* creating the 16 bitmap radio buttons and setting the bitmap */
298 * FIXME: Unintentionally there is a line above the tool bar.
299 * To prevent cropping of the buttons height has been increased from 200 to 205
302 CreateWindowEx(0, TOOLBARCLASSNAME
, NULL
,
303 WS_CHILD
| WS_VISIBLE
| CCS_NOPARENTALIGN
| CCS_VERT
| CCS_NORESIZE
| TBSTYLE_TOOLTIPS
,
304 1, 1, 50, 205, hToolBoxContainer
, NULL
, hThisInstance
, NULL
);
305 hImageList
= ImageList_Create(16, 16, ILC_COLOR24
| ILC_MASK
, 16, 0);
306 SendMessage(hToolbar
, TB_SETIMAGELIST
, 0, (LPARAM
) hImageList
);
307 tempBm
= LoadImage(hThisInstance
, MAKEINTRESOURCE(IDB_TOOLBARICONS
), IMAGE_BITMAP
, 256, 16, 0);
308 ImageList_AddMasked(hImageList
, tempBm
, 0xff00ff);
309 DeleteObject(tempBm
);
310 SendMessage(hToolbar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
312 for(i
= 0; i
< 16; i
++)
318 wrapnow
= TBSTATE_WRAP
;
320 LoadString(hThisInstance
, IDS_TOOLTIP1
+ i
, tooltips
[i
], 30);
321 ZeroMemory(&tbbutton
, sizeof(TBBUTTON
));
322 tbbutton
.iString
= (INT_PTR
) tooltips
[i
];
323 tbbutton
.fsStyle
= TBSTYLE_CHECKGROUP
;
324 tbbutton
.fsState
= TBSTATE_ENABLED
| wrapnow
;
325 tbbutton
.idCommand
= ID_FREESEL
+ i
;
326 tbbutton
.iBitmap
= i
;
327 SendMessage(hToolbar
, TB_ADDBUTTONS
, 1, (LPARAM
) &tbbutton
);
330 SendMessage(hToolbar
, TB_CHECKBUTTON
, ID_PEN
, MAKELONG(TRUE
, 0));
331 SendMessage(hToolbar
, TB_SETMAXTEXTROWS
, 0, 0);
332 SendMessage(hToolbar
, TB_SETBUTTONSIZE
, 0, MAKELONG(25, 25));
334 /* creating the tool settings child window */
336 CreateWindowEx(0, _T("ToolSettings"), _T(""), WS_CHILD
| WS_VISIBLE
, 5, 208, 42, 140,
337 hToolBoxContainer
, NULL
, hThisInstance
, NULL
);
339 CreateWindowEx(0, TRACKBAR_CLASS
, _T(""), WS_CHILD
| TBS_VERT
| TBS_AUTOTICKS
, 1, 1, 40, 64,
340 hToolSettings
, NULL
, hThisInstance
, NULL
);
341 SendMessage(hTrackbarZoom
, TBM_SETRANGE
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG(0, 6));
342 SendMessage(hTrackbarZoom
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) 3);
344 /* creating the palette child window */
346 CreateWindowEx(0, _T("Palette"), _T(""), WS_CHILD
| WS_VISIBLE
, 56, 9, 255, 32, hwnd
, NULL
,
347 hThisInstance
, NULL
);
349 /* creating the scroll box */
351 CreateWindowEx(WS_EX_CLIENTEDGE
, _T("Scrollbox"), _T(""),
352 WS_CHILD
| WS_GROUP
| WS_HSCROLL
| WS_VSCROLL
| WS_VISIBLE
, 56, 49, 472, 248, hwnd
,
353 NULL
, hThisInstance
, NULL
);
355 /* creating the status bar */
357 CreateWindowEx(0, STATUSCLASSNAME
, _T(""), SBARS_SIZEGRIP
| WS_CHILD
| WS_VISIBLE
, 0, 0, 0, 0, hwnd
,
358 NULL
, hThisInstance
, NULL
);
359 SendMessage(hStatusBar
, SB_SETMINHEIGHT
, 21, 0);
362 CreateWindowEx(0, _T("Scrollbox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 500, 500, hScrollbox
, NULL
,
363 hThisInstance
, NULL
);
365 /* create selection window (initially hidden) */
367 CreateWindowEx(WS_EX_TRANSPARENT
, _T("Selection"), _T(""), WS_CHILD
| BS_OWNERDRAW
, 350, 0, 100, 100,
368 hScrlClient
, NULL
, hThisInstance
, NULL
);
370 /* creating the window inside the scroll box, on which the image in hDrawingDC's bitmap is drawn */
372 CreateWindowEx(0, _T("Scrollbox"), _T(""), WS_CHILD
| WS_VISIBLE
, 3, 3, imgXRes
, imgYRes
, hScrlClient
,
373 NULL
, hThisInstance
, NULL
);
375 hDC
= GetDC(hImageArea
);
376 hDrawingDC
= CreateCompatibleDC(hDC
);
377 hSelDC
= CreateCompatibleDC(hDC
);
378 ReleaseDC(hImageArea
, hDC
);
379 SelectObject(hDrawingDC
, CreatePen(PS_SOLID
, 0, fgColor
));
380 SelectObject(hDrawingDC
, CreateSolidBrush(bgColor
));
382 hBms
[0] = CreateDIBWithProperties(imgXRes
, imgYRes
);
383 SelectObject(hDrawingDC
, hBms
[0]);
384 Rectangle(hDrawingDC
, 0 - 1, 0 - 1, imgXRes
+ 1, imgYRes
+ 1);
386 if (lpszArgument
[0] != 0)
388 HBITMAP bmNew
= NULL
;
389 LoadDIBFromFile(&bmNew
, lpszArgument
, &fileTime
, &fileSize
, &fileHPPM
, &fileVPPM
);
395 insertReversible(bmNew
);
396 GetFullPathName(lpszArgument
, SIZEOF(filepathname
), filepathname
, &temp
);
397 _tcscpy(filename
, temp
);
398 LoadString(hProgInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
399 _stprintf(tempstr
, resstr
, filename
);
400 SetWindowText(hMainWnd
, tempstr
);
406 /* initializing the CHOOSECOLOR structure for use with ChooseColor */
407 choosecolor
.lStructSize
= sizeof(CHOOSECOLOR
);
408 choosecolor
.hwndOwner
= hwnd
;
409 choosecolor
.hInstance
= NULL
;
410 choosecolor
.rgbResult
= 0x00ffffff;
411 choosecolor
.lpCustColors
= (COLORREF
*) &custColors
;
412 choosecolor
.Flags
= 0;
413 choosecolor
.lCustData
= 0;
414 choosecolor
.lpfnHook
= NULL
;
415 choosecolor
.lpTemplateName
= NULL
;
417 /* initializing the OPENFILENAME structure for use with GetOpenFileName and GetSaveFileName */
418 CopyMemory(ofnFilename
, filename
, sizeof(filename
));
419 LoadString(hThisInstance
, IDS_OPENFILTER
, ofnFilter
, SIZEOF(ofnFilter
));
420 for(c
= ofnFilter
; *c
; c
++)
423 ZeroMemory(&ofn
, sizeof(OPENFILENAME
));
424 ofn
.lStructSize
= sizeof(OPENFILENAME
);
425 ofn
.hwndOwner
= hwnd
;
426 ofn
.hInstance
= hThisInstance
;
427 ofn
.lpstrFilter
= ofnFilter
;
428 ofn
.lpstrFile
= ofnFilename
;
429 ofn
.nMaxFile
= SIZEOF(ofnFilename
);
430 ofn
.lpstrFileTitle
= ofnFiletitle
;
431 ofn
.nMaxFileTitle
= SIZEOF(ofnFiletitle
);
432 ofn
.Flags
= OFN_HIDEREADONLY
;
434 CopyMemory(sfnFilename
, filename
, sizeof(filename
));
435 LoadString(hThisInstance
, IDS_SAVEFILTER
, sfnFilter
, SIZEOF(sfnFilter
));
436 for(c
= sfnFilter
; *c
; c
++)
439 ZeroMemory(&sfn
, sizeof(OPENFILENAME
));
440 sfn
.lStructSize
= sizeof(OPENFILENAME
);
441 sfn
.hwndOwner
= hwnd
;
442 sfn
.hInstance
= hThisInstance
;
443 sfn
.lpstrFilter
= sfnFilter
;
444 sfn
.lpstrFile
= sfnFilename
;
445 sfn
.nMaxFile
= SIZEOF(sfnFilename
);
446 sfn
.lpstrFileTitle
= sfnFiletitle
;
447 sfn
.nMaxFileTitle
= SIZEOF(sfnFiletitle
);
448 sfn
.Flags
= OFN_OVERWRITEPROMPT
| OFN_HIDEREADONLY
;
450 /* creating the size boxes */
452 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
453 hThisInstance
, NULL
);
455 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
456 hThisInstance
, NULL
);
458 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
459 hThisInstance
, NULL
);
461 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
462 hThisInstance
, NULL
);
463 hSizeboxRightCenter
=
464 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
465 hThisInstance
, NULL
);
467 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
468 hThisInstance
, NULL
);
469 hSizeboxCenterBottom
=
470 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
471 hThisInstance
, NULL
);
472 hSizeboxRightBottom
=
473 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
474 hThisInstance
, NULL
);
475 /* placing the size boxes around the image */
476 SendMessage(hImageArea
, WM_SIZE
, 0, 0);
478 /* by moving the window, the things in WM_SIZE are done */
479 MoveWindow(hwnd
, 100, 100, 600, 450, TRUE
);
481 /* Make the window visible on the screen */
482 ShowWindow (hwnd
, nFunsterStil
);
484 /* Run the message loop. It will run until GetMessage() returns 0 */
485 while (GetMessage(&messages
, NULL
, 0, 0))
487 TranslateAccelerator(hwnd
, haccel
, &messages
);
489 /* Translate virtual-key messages into character messages */
490 TranslateMessage(&messages
);
491 /* Send message to WindowProcedure */
492 DispatchMessage(&messages
);
495 /* The program return-value is 0 - The value that PostQuitMessage() gave */
496 return messages
.wParam
;