2 * PROJECT: PAINT for ReactOS
4 * FILE: base/applications/paint/main.c
5 * PURPOSE: Initializing everything
6 * PROGRAMMERS: Benedikt Freisen
9 /* INCLUDES *********************************************************/
13 /* FUNCTIONS ********************************************************/
18 BITMAPINFO bitmapinfo
;
25 STRETCHSKEW stretchSkew
;
27 HBITMAP hBms
[HISTORYSIZE
];
31 BOOL imageSaved
= TRUE
;
38 int activeTool
= TOOL_PEN
;
39 int airBrushWidth
= 5;
50 /* array holding palette colors; may be changed by the user during execution */
54 int modernPalColors
[28] = { 0x000000, 0x464646, 0x787878, 0x300099, 0x241ced, 0x0078ff, 0x0ec2ff,
55 0x00f2ff, 0x1de6a8, 0x4cb122, 0xefb700, 0xf36d4d, 0x99362f, 0x98316f,
56 0xffffff, 0xdcdcdc, 0xb4b4b4, 0x3c5a9c, 0xb1a3ff, 0x7aaae5, 0x9ce4f5,
57 0xbdf9ff, 0xbcf9d3, 0x61bb9d, 0xead999, 0xd19a70, 0x8e6d54, 0xd5a5b5
60 /* older palette containing VGA colors */
61 int oldPalColors
[28] = { 0x000000, 0x808080, 0x000080, 0x008080, 0x008000, 0x808000, 0x800000,
62 0x800080, 0x408080, 0x404000, 0xff8000, 0x804000, 0xff0040, 0x004080,
63 0xffffff, 0xc0c0c0, 0x0000ff, 0x00ffff, 0x00ff00, 0xffff00, 0xff0000,
64 0xff00ff, 0x80ffff, 0x80ff00, 0xffff80, 0xff8080, 0x8000ff, 0x4080ff
67 /* palette currently in use (1: modern, 2: old) */
70 /* foreground and background colors with initial value */
71 int fgColor
= 0x00000000;
72 int bgColor
= 0x00ffffff;
78 HWND hToolBoxContainer
;
81 CHOOSECOLOR choosecolor
;
97 HINSTANCE hProgInstance
;
100 TCHAR filepathname
[1000];
101 BOOL isAFile
= FALSE
;
107 BOOL showGrid
= FALSE
;
108 BOOL showMiniature
= FALSE
;
112 HWND hSizeboxLeftTop
;
113 HWND hSizeboxCenterTop
;
114 HWND hSizeboxRightTop
;
115 HWND hSizeboxLeftCenter
;
116 HWND hSizeboxRightCenter
;
117 HWND hSizeboxLeftBottom
;
118 HWND hSizeboxCenterBottom
;
119 HWND hSizeboxRightBottom
;
124 _tWinMain (HINSTANCE hThisInstance
, HINSTANCE hPrevInstance
, LPTSTR lpszArgument
, int nFunsterStil
)
126 HWND hwnd
; /* This is the handle for our window */
127 MSG messages
; /* Here messages to the application are saved */
129 WNDCLASSEX wclScroll
;
132 WNDCLASSEX wclSettings
;
133 WNDCLASSEX wclSelection
;
135 TCHAR progtitle
[1000];
139 HIMAGELIST hImageList
;
143 TCHAR tooltips
[16][30];
147 TCHAR sfnFilename
[1000];
148 TCHAR sfnFiletitle
[256];
149 TCHAR sfnFilter
[1000];
150 TCHAR ofnFilename
[1000];
151 TCHAR ofnFiletitle
[256];
152 TCHAR ofnFilter
[1000];
153 TCHAR miniaturetitle
[100];
154 static int custColors
[16] = { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
155 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff
160 CopyMemory(palColors
, modernPalColors
, sizeof(palColors
));
162 hProgInstance
= hThisInstance
;
165 InitCommonControls();
167 /* initializing and registering the window class used for the main window */
168 wincl
.hInstance
= hThisInstance
;
169 wincl
.lpszClassName
= _T("WindowsApp");
170 wincl
.lpfnWndProc
= WindowProcedure
;
171 wincl
.style
= CS_DBLCLKS
;
172 wincl
.cbSize
= sizeof(WNDCLASSEX
);
173 wincl
.hIcon
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDI_APPICON
));
174 wincl
.hIconSm
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDI_APPICON
));
175 wincl
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
176 wincl
.lpszMenuName
= NULL
;
177 wincl
.cbClsExtra
= 0;
178 wincl
.cbWndExtra
= 0;
179 wincl
.hbrBackground
= GetSysColorBrush(COLOR_BTNFACE
);
180 RegisterClassEx (&wincl
);
182 /* initializing and registering the window class used for the scroll box */
183 wclScroll
.hInstance
= hThisInstance
;
184 wclScroll
.lpszClassName
= _T("Scrollbox");
185 wclScroll
.lpfnWndProc
= WindowProcedure
;
187 wclScroll
.cbSize
= sizeof(WNDCLASSEX
);
188 wclScroll
.hIcon
= NULL
;
189 wclScroll
.hIconSm
= NULL
;
190 wclScroll
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
191 wclScroll
.lpszMenuName
= NULL
;
192 wclScroll
.cbClsExtra
= 0;
193 wclScroll
.cbWndExtra
= 0;
194 wclScroll
.hbrBackground
= GetSysColorBrush(COLOR_APPWORKSPACE
);
195 RegisterClassEx (&wclScroll
);
197 /* initializing and registering the window class used for the palette window */
198 wclPal
.hInstance
= hThisInstance
;
199 wclPal
.lpszClassName
= _T("Palette");
200 wclPal
.lpfnWndProc
= PalWinProc
;
201 wclPal
.style
= CS_DBLCLKS
;
202 wclPal
.cbSize
= sizeof(WNDCLASSEX
);
204 wclPal
.hIconSm
= NULL
;
205 wclPal
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
206 wclPal
.lpszMenuName
= NULL
;
207 wclPal
.cbClsExtra
= 0;
208 wclPal
.cbWndExtra
= 0;
209 wclPal
.hbrBackground
= GetSysColorBrush(COLOR_BTNFACE
);
210 RegisterClassEx (&wclPal
);
212 /* initializing and registering the window class for the settings window */
213 wclSettings
.hInstance
= hThisInstance
;
214 wclSettings
.lpszClassName
= _T("ToolSettings");
215 wclSettings
.lpfnWndProc
= SettingsWinProc
;
216 wclSettings
.style
= CS_DBLCLKS
;
217 wclSettings
.cbSize
= sizeof(WNDCLASSEX
);
218 wclSettings
.hIcon
= NULL
;
219 wclSettings
.hIconSm
= NULL
;
220 wclSettings
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
221 wclSettings
.lpszMenuName
= NULL
;
222 wclSettings
.cbClsExtra
= 0;
223 wclSettings
.cbWndExtra
= 0;
224 wclSettings
.hbrBackground
= GetSysColorBrush(COLOR_BTNFACE
);
225 RegisterClassEx (&wclSettings
);
227 /* initializing and registering the window class for the selection frame */
228 wclSelection
.hInstance
= hThisInstance
;
229 wclSelection
.lpszClassName
= _T("Selection");
230 wclSelection
.lpfnWndProc
= SelectionWinProc
;
231 wclSelection
.style
= CS_DBLCLKS
;
232 wclSelection
.cbSize
= sizeof(WNDCLASSEX
);
233 wclSelection
.hIcon
= NULL
;
234 wclSelection
.hIconSm
= NULL
;
235 wclSelection
.hCursor
= LoadCursor(NULL
, IDC_SIZEALL
);
236 wclSelection
.lpszMenuName
= NULL
;
237 wclSelection
.cbClsExtra
= 0;
238 wclSelection
.cbWndExtra
= 0;
239 wclSelection
.hbrBackground
= NULL
;
240 RegisterClassEx (&wclSelection
);
242 /* initializing and registering the window class for the size boxes */
243 wclSettings
.hInstance
= hThisInstance
;
244 wclSettings
.lpszClassName
= _T("Sizebox");
245 wclSettings
.lpfnWndProc
= SizeboxWinProc
;
246 wclSettings
.style
= CS_DBLCLKS
;
247 wclSettings
.cbSize
= sizeof(WNDCLASSEX
);
248 wclSettings
.hIcon
= NULL
;
249 wclSettings
.hIconSm
= NULL
;
250 wclSettings
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
251 wclSettings
.lpszMenuName
= NULL
;
252 wclSettings
.cbClsExtra
= 0;
253 wclSettings
.cbWndExtra
= 0;
254 wclSettings
.hbrBackground
= GetSysColorBrush(COLOR_HIGHLIGHT
);
255 RegisterClassEx (&wclSettings
);
257 LoadString(hThisInstance
, IDS_DEFAULTFILENAME
, filename
, SIZEOF(filename
));
258 LoadString(hThisInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
259 _stprintf(progtitle
, resstr
, filename
);
260 LoadString(hThisInstance
, IDS_MINIATURETITLE
, miniaturetitle
, SIZEOF(miniaturetitle
));
262 /* create main window */
264 CreateWindowEx(0, _T("WindowsApp"), progtitle
, WS_OVERLAPPEDWINDOW
, CW_USEDEFAULT
, CW_USEDEFAULT
, 544,
265 375, HWND_DESKTOP
, NULL
, hThisInstance
, NULL
);
269 CreateWindowEx(WS_EX_PALETTEWINDOW
, _T("WindowsApp"), miniaturetitle
,
270 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_THICKFRAME
, 180, 200, 120, 100, hwnd
,
271 NULL
, hThisInstance
, NULL
);
273 /* loading and setting the window menu from resource */
274 menu
= LoadMenu(hThisInstance
, MAKEINTRESOURCE(ID_MENU
));
276 haccel
= LoadAccelerators(hThisInstance
, MAKEINTRESOURCE(800));
278 /* preloading the draw transparent/nontransparent icons for later use */
280 LoadImage(hThisInstance
, MAKEINTRESOURCE(IDI_NONTRANSPARENT
), IMAGE_ICON
, 40, 30, LR_DEFAULTCOLOR
);
282 LoadImage(hThisInstance
, MAKEINTRESOURCE(IDI_TRANSPARENT
), IMAGE_ICON
, 40, 30, LR_DEFAULTCOLOR
);
284 hCurFill
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_FILL
));
285 hCurColor
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_COLOR
));
286 hCurZoom
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_ZOOM
));
287 hCurPen
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_PEN
));
288 hCurAirbrush
= LoadIcon(hThisInstance
, MAKEINTRESOURCE(IDC_AIRBRUSH
));
290 CreateWindowEx(0, _T("STATIC"), _T(""), WS_CHILD
| WS_VISIBLE
| SS_ETCHEDHORZ
, 0, 0, 5000, 2, hwnd
, NULL
,
291 hThisInstance
, NULL
);
294 CreateWindowEx(0, _T("WindowsApp"), _T(""), WS_CHILD
| WS_VISIBLE
, 2, 2, 52, 350, hwnd
, NULL
,
295 hThisInstance
, NULL
);
296 /* creating the 16 bitmap radio buttons and setting the bitmap */
300 * FIXME: Unintentionally there is a line above the tool bar (hidden by y-offset).
301 * To prevent cropping of the buttons height has been increased from 200 to 205
304 CreateWindowEx(0, TOOLBARCLASSNAME
, NULL
,
305 WS_CHILD
| WS_VISIBLE
| CCS_NOPARENTALIGN
| CCS_VERT
| CCS_NORESIZE
| TBSTYLE_TOOLTIPS
,
306 1, -2, 50, 205, hToolBoxContainer
, NULL
, hThisInstance
, NULL
);
307 hImageList
= ImageList_Create(16, 16, ILC_COLOR24
| ILC_MASK
, 16, 0);
308 SendMessage(hToolbar
, TB_SETIMAGELIST
, 0, (LPARAM
) hImageList
);
309 tempBm
= LoadImage(hThisInstance
, MAKEINTRESOURCE(IDB_TOOLBARICONS
), IMAGE_BITMAP
, 256, 16, 0);
310 ImageList_AddMasked(hImageList
, tempBm
, 0xff00ff);
311 DeleteObject(tempBm
);
312 SendMessage(hToolbar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
314 for(i
= 0; i
< 16; i
++)
320 wrapnow
= TBSTATE_WRAP
;
322 LoadString(hThisInstance
, IDS_TOOLTIP1
+ i
, tooltips
[i
], 30);
323 ZeroMemory(&tbbutton
, sizeof(TBBUTTON
));
324 tbbutton
.iString
= (INT_PTR
) tooltips
[i
];
325 tbbutton
.fsStyle
= TBSTYLE_CHECKGROUP
;
326 tbbutton
.fsState
= TBSTATE_ENABLED
| wrapnow
;
327 tbbutton
.idCommand
= ID_FREESEL
+ i
;
328 tbbutton
.iBitmap
= i
;
329 SendMessage(hToolbar
, TB_ADDBUTTONS
, 1, (LPARAM
) &tbbutton
);
332 SendMessage(hToolbar
, TB_CHECKBUTTON
, ID_PEN
, MAKELONG(TRUE
, 0));
333 SendMessage(hToolbar
, TB_SETMAXTEXTROWS
, 0, 0);
334 SendMessage(hToolbar
, TB_SETBUTTONSIZE
, 0, MAKELONG(25, 25));
336 /* creating the tool settings child window */
338 CreateWindowEx(0, _T("ToolSettings"), _T(""), WS_CHILD
| WS_VISIBLE
, 5, 208, 42, 140,
339 hToolBoxContainer
, NULL
, hThisInstance
, NULL
);
341 CreateWindowEx(0, TRACKBAR_CLASS
, _T(""), WS_CHILD
| TBS_VERT
| TBS_AUTOTICKS
, 1, 1, 40, 64,
342 hToolSettings
, NULL
, hThisInstance
, NULL
);
343 SendMessage(hTrackbarZoom
, TBM_SETRANGE
, (WPARAM
) TRUE
, (LPARAM
) MAKELONG(0, 6));
344 SendMessage(hTrackbarZoom
, TBM_SETPOS
, (WPARAM
) TRUE
, (LPARAM
) 3);
346 /* creating the palette child window */
348 CreateWindowEx(0, _T("Palette"), _T(""), WS_CHILD
| WS_VISIBLE
, 56, 9, 255, 32, hwnd
, NULL
,
349 hThisInstance
, NULL
);
351 /* creating the scroll box */
353 CreateWindowEx(WS_EX_CLIENTEDGE
, _T("Scrollbox"), _T(""),
354 WS_CHILD
| WS_GROUP
| WS_HSCROLL
| WS_VSCROLL
| WS_VISIBLE
, 56, 49, 472, 248, hwnd
,
355 NULL
, hThisInstance
, NULL
);
357 /* creating the status bar */
359 CreateWindowEx(0, STATUSCLASSNAME
, _T(""), SBARS_SIZEGRIP
| WS_CHILD
| WS_VISIBLE
, 0, 0, 0, 0, hwnd
,
360 NULL
, hThisInstance
, NULL
);
361 SendMessage(hStatusBar
, SB_SETMINHEIGHT
, 21, 0);
364 CreateWindowEx(0, _T("Scrollbox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 500, 500, hScrollbox
, NULL
,
365 hThisInstance
, NULL
);
367 /* create selection window (initially hidden) */
369 CreateWindowEx(WS_EX_TRANSPARENT
, _T("Selection"), _T(""), WS_CHILD
| BS_OWNERDRAW
, 350, 0, 100, 100,
370 hScrlClient
, NULL
, hThisInstance
, NULL
);
372 /* creating the window inside the scroll box, on which the image in hDrawingDC's bitmap is drawn */
374 CreateWindowEx(0, _T("Scrollbox"), _T(""), WS_CHILD
| WS_VISIBLE
, 3, 3, imgXRes
, imgYRes
, hScrlClient
,
375 NULL
, hThisInstance
, NULL
);
377 hDC
= GetDC(hImageArea
);
378 hDrawingDC
= CreateCompatibleDC(hDC
);
379 hSelDC
= CreateCompatibleDC(hDC
);
380 ReleaseDC(hImageArea
, hDC
);
381 SelectObject(hDrawingDC
, CreatePen(PS_SOLID
, 0, fgColor
));
382 SelectObject(hDrawingDC
, CreateSolidBrush(bgColor
));
384 hBms
[0] = CreateDIBWithProperties(imgXRes
, imgYRes
);
385 SelectObject(hDrawingDC
, hBms
[0]);
386 Rectangle(hDrawingDC
, 0 - 1, 0 - 1, imgXRes
+ 1, imgYRes
+ 1);
388 if (lpszArgument
[0] != 0)
390 HBITMAP bmNew
= NULL
;
391 LoadDIBFromFile(&bmNew
, lpszArgument
, &fileTime
, &fileSize
, &fileHPPM
, &fileVPPM
);
397 insertReversible(bmNew
);
398 GetFullPathName(lpszArgument
, SIZEOF(filepathname
), filepathname
, &temp
);
399 _tcscpy(filename
, temp
);
400 LoadString(hProgInstance
, IDS_WINDOWTITLE
, resstr
, SIZEOF(resstr
));
401 _stprintf(tempstr
, resstr
, filename
);
402 SetWindowText(hMainWnd
, tempstr
);
408 /* initializing the CHOOSECOLOR structure for use with ChooseColor */
409 choosecolor
.lStructSize
= sizeof(CHOOSECOLOR
);
410 choosecolor
.hwndOwner
= hwnd
;
411 choosecolor
.hInstance
= NULL
;
412 choosecolor
.rgbResult
= 0x00ffffff;
413 choosecolor
.lpCustColors
= (COLORREF
*) &custColors
;
414 choosecolor
.Flags
= 0;
415 choosecolor
.lCustData
= 0;
416 choosecolor
.lpfnHook
= NULL
;
417 choosecolor
.lpTemplateName
= NULL
;
419 /* initializing the OPENFILENAME structure for use with GetOpenFileName and GetSaveFileName */
420 CopyMemory(ofnFilename
, filename
, sizeof(filename
));
421 LoadString(hThisInstance
, IDS_OPENFILTER
, ofnFilter
, SIZEOF(ofnFilter
));
422 for(c
= ofnFilter
; *c
; c
++)
425 ZeroMemory(&ofn
, sizeof(OPENFILENAME
));
426 ofn
.lStructSize
= sizeof(OPENFILENAME
);
427 ofn
.hwndOwner
= hwnd
;
428 ofn
.hInstance
= hThisInstance
;
429 ofn
.lpstrFilter
= ofnFilter
;
430 ofn
.lpstrFile
= ofnFilename
;
431 ofn
.nMaxFile
= SIZEOF(ofnFilename
);
432 ofn
.lpstrFileTitle
= ofnFiletitle
;
433 ofn
.nMaxFileTitle
= SIZEOF(ofnFiletitle
);
434 ofn
.Flags
= OFN_HIDEREADONLY
;
436 CopyMemory(sfnFilename
, filename
, sizeof(filename
));
437 LoadString(hThisInstance
, IDS_SAVEFILTER
, sfnFilter
, SIZEOF(sfnFilter
));
438 for(c
= sfnFilter
; *c
; c
++)
441 ZeroMemory(&sfn
, sizeof(OPENFILENAME
));
442 sfn
.lStructSize
= sizeof(OPENFILENAME
);
443 sfn
.hwndOwner
= hwnd
;
444 sfn
.hInstance
= hThisInstance
;
445 sfn
.lpstrFilter
= sfnFilter
;
446 sfn
.lpstrFile
= sfnFilename
;
447 sfn
.nMaxFile
= SIZEOF(sfnFilename
);
448 sfn
.lpstrFileTitle
= sfnFiletitle
;
449 sfn
.nMaxFileTitle
= SIZEOF(sfnFiletitle
);
450 sfn
.Flags
= OFN_OVERWRITEPROMPT
| OFN_HIDEREADONLY
;
452 /* creating the size boxes */
454 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
455 hThisInstance
, NULL
);
457 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
458 hThisInstance
, NULL
);
460 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
461 hThisInstance
, NULL
);
463 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
464 hThisInstance
, NULL
);
465 hSizeboxRightCenter
=
466 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
467 hThisInstance
, NULL
);
469 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
470 hThisInstance
, NULL
);
471 hSizeboxCenterBottom
=
472 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
473 hThisInstance
, NULL
);
474 hSizeboxRightBottom
=
475 CreateWindowEx(0, _T("Sizebox"), _T(""), WS_CHILD
| WS_VISIBLE
, 0, 0, 3, 3, hScrlClient
, NULL
,
476 hThisInstance
, NULL
);
477 /* placing the size boxes around the image */
478 SendMessage(hImageArea
, WM_SIZE
, 0, 0);
480 /* by moving the window, the things in WM_SIZE are done */
481 MoveWindow(hwnd
, 100, 100, 600, 450, TRUE
);
483 /* Make the window visible on the screen */
484 ShowWindow (hwnd
, nFunsterStil
);
486 /* Run the message loop. It will run until GetMessage() returns 0 */
487 while (GetMessage(&messages
, NULL
, 0, 0))
489 TranslateAccelerator(hwnd
, haccel
, &messages
);
491 /* Translate virtual-key messages into character messages */
492 TranslateMessage(&messages
);
493 /* Send message to WindowProcedure */
494 DispatchMessage(&messages
);
497 /* The program return-value is 0 - The value that PostQuitMessage() gave */
498 return messages
.wParam
;