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