Minor changes for ATAPI Srb Functions
[reactos.git] / base / applications / mspaint / main.cpp
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 int widthSetInDlg;
16 int heightSetInDlg;
17
18 STRETCHSKEW stretchSkew;
19
20 POINT start;
21 POINT last;
22
23 ToolsModel toolsModel;
24
25 SelectionModel selectionModel;
26
27 LOGFONT lfTextFont;
28 HFONT hfontTextFont;
29 HWND hwndEditCtl;
30 LPTSTR textToolText = NULL;
31 int textToolTextMaxLen = 0;
32
33 PaletteModel paletteModel;
34
35 RegistrySettings registrySettings;
36
37 ImageModel imageModel;
38 BOOL askBeforeEnlarging = FALSE; // TODO: initialize from registry
39
40 HWND hStatusBar;
41 CHOOSECOLOR choosecolor;
42 OPENFILENAME ofn;
43 OPENFILENAME sfn;
44 HICON hNontranspIcon;
45 HICON hTranspIcon;
46
47 HCURSOR hCurFill;
48 HCURSOR hCurColor;
49 HCURSOR hCurZoom;
50 HCURSOR hCurPen;
51 HCURSOR hCurAirbrush;
52
53 HWND hToolBtn[16];
54
55 HINSTANCE hProgInstance;
56
57 TCHAR filename[256];
58 TCHAR filepathname[1000];
59 BOOL isAFile = FALSE;
60 int fileSize;
61 int fileHPPM = 2834;
62 int fileVPPM = 2834;
63 SYSTEMTIME fileTime;
64
65 BOOL showGrid = FALSE;
66 BOOL showMiniature = FALSE;
67
68 CMainWindow mainWindow;
69 CFullscreenWindow fullscreenWindow;
70 CMiniatureWindow miniature;
71 CToolBox toolBoxContainer;
72 CToolSettingsWindow toolSettingsWindow;
73 CPaletteWindow paletteWindow;
74 CScrollboxWindow scrollboxWindow;
75 CScrollboxWindow scrlClientWindow;
76 CSelectionWindow selectionWindow;
77 CImgAreaWindow imageArea;
78 CSizeboxWindow sizeboxLeftTop;
79 CSizeboxWindow sizeboxCenterTop;
80 CSizeboxWindow sizeboxRightTop;
81 CSizeboxWindow sizeboxLeftCenter;
82 CSizeboxWindow sizeboxRightCenter;
83 CSizeboxWindow sizeboxLeftBottom;
84 CSizeboxWindow sizeboxCenterBottom;
85 CSizeboxWindow sizeboxRightBottom;
86 CTextEditWindow textEditWindow;
87
88 /* entry point */
89
90 int WINAPI
91 _tWinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgument, int nFunsterStil)
92 {
93 HWND hwnd; /* This is the handle for our window */
94 MSG messages; /* Here messages to the application are saved */
95
96 TCHAR progtitle[1000];
97 TCHAR resstr[100];
98 HMENU menu;
99 HANDLE haccel;
100
101 TCHAR *c;
102 TCHAR sfnFilename[1000];
103 TCHAR sfnFiletitle[256];
104 TCHAR sfnFilter[1000];
105 TCHAR ofnFilename[1000];
106 TCHAR ofnFiletitle[256];
107 TCHAR ofnFilter[1000];
108 TCHAR miniaturetitle[100];
109 static int custColors[16] = { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
110 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff
111 };
112
113 /* init font for text tool */
114 lfTextFont.lfHeight = 0;
115 lfTextFont.lfWidth = 0;
116 lfTextFont.lfEscapement = 0;
117 lfTextFont.lfOrientation = 0;
118 lfTextFont.lfWeight = FW_NORMAL;
119 lfTextFont.lfItalic = FALSE;
120 lfTextFont.lfUnderline = FALSE;
121 lfTextFont.lfStrikeOut = FALSE;
122 lfTextFont.lfCharSet = DEFAULT_CHARSET;
123 lfTextFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
124 lfTextFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
125 lfTextFont.lfQuality = DEFAULT_QUALITY;
126 lfTextFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
127 lstrcpy(lfTextFont.lfFaceName, _T(""));
128 hfontTextFont = CreateFontIndirect(&lfTextFont);
129
130 hProgInstance = hThisInstance;
131
132 /* initialize common controls library */
133 InitCommonControls();
134
135 LoadString(hThisInstance, IDS_DEFAULTFILENAME, filename, SIZEOF(filename));
136 LoadString(hThisInstance, IDS_WINDOWTITLE, resstr, SIZEOF(resstr));
137 _stprintf(progtitle, resstr, filename);
138 LoadString(hThisInstance, IDS_MINIATURETITLE, miniaturetitle, SIZEOF(miniaturetitle));
139
140 /* load settings from registry */
141 registrySettings.Load();
142 showMiniature = registrySettings.ShowThumbnail;
143 imageModel.Crop(registrySettings.BMPWidth, registrySettings.BMPHeight);
144
145 /* create main window */
146 RECT mainWindowPos = {0, 0, 544, 375}; // FIXME: use equivalent of CW_USEDEFAULT for position
147 hwnd = mainWindow.Create(HWND_DESKTOP, mainWindowPos, progtitle, WS_OVERLAPPEDWINDOW);
148
149 RECT fullscreenWindowPos = {0, 0, 100, 100};
150 fullscreenWindow.Create(HWND_DESKTOP, fullscreenWindowPos, NULL, WS_POPUPWINDOW | WS_MAXIMIZE);
151
152 RECT miniaturePos = {(LONG) registrySettings.ThumbXPos, (LONG) registrySettings.ThumbYPos,
153 (LONG) registrySettings.ThumbXPos + (LONG) registrySettings.ThumbWidth,
154 (LONG) registrySettings.ThumbYPos + (LONG) registrySettings.ThumbHeight};
155 miniature.Create(hwnd, miniaturePos, miniaturetitle,
156 WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, WS_EX_PALETTEWINDOW);
157 miniature.ShowWindow(showMiniature ? SW_SHOW : SW_HIDE);
158
159 /* loading and setting the window menu from resource */
160 menu = LoadMenu(hThisInstance, MAKEINTRESOURCE(ID_MENU));
161 SetMenu(hwnd, menu);
162 haccel = LoadAccelerators(hThisInstance, MAKEINTRESOURCE(800));
163
164 /* preloading the draw transparent/nontransparent icons for later use */
165 hNontranspIcon =
166 (HICON) LoadImage(hThisInstance, MAKEINTRESOURCE(IDI_NONTRANSPARENT), IMAGE_ICON, 40, 30, LR_DEFAULTCOLOR);
167 hTranspIcon =
168 (HICON) LoadImage(hThisInstance, MAKEINTRESOURCE(IDI_TRANSPARENT), IMAGE_ICON, 40, 30, LR_DEFAULTCOLOR);
169
170 hCurFill = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_FILL));
171 hCurColor = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_COLOR));
172 hCurZoom = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_ZOOM));
173 hCurPen = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_PEN));
174 hCurAirbrush = LoadIcon(hThisInstance, MAKEINTRESOURCE(IDC_AIRBRUSH));
175
176 CreateWindowEx(0, _T("STATIC"), NULL, WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ, 0, 0, 5000, 2, hwnd, NULL,
177 hThisInstance, NULL);
178
179 RECT toolBoxContainerPos = {2, 2, 2 + 52, 2 + 350};
180 toolBoxContainer.Create(hwnd, toolBoxContainerPos, NULL, WS_CHILD | WS_VISIBLE);
181 /* creating the tool settings child window */
182 RECT toolSettingsWindowPos = {5, 208, 5 + 42, 208 + 140};
183 toolSettingsWindow.Create(toolBoxContainer.m_hWnd, toolSettingsWindowPos, NULL, WS_CHILD | WS_VISIBLE);
184
185 /* creating the palette child window */
186 RECT paletteWindowPos = {56, 9, 56 + 255, 9 + 32};
187 paletteWindow.Create(hwnd, paletteWindowPos, NULL, WS_CHILD | WS_VISIBLE);
188
189 /* creating the scroll box */
190 RECT scrollboxWindowPos = {56, 49, 56 + 472, 49 + 248};
191 scrollboxWindow.Create(hwnd, scrollboxWindowPos, NULL,
192 WS_CHILD | WS_GROUP | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE, WS_EX_CLIENTEDGE);
193
194 /* creating the status bar */
195 hStatusBar =
196 CreateWindowEx(0, STATUSCLASSNAME, NULL, SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd,
197 NULL, hThisInstance, NULL);
198 SendMessage(hStatusBar, SB_SETMINHEIGHT, 21, 0);
199
200 RECT scrlClientWindowPos = {0, 0, 0 + 500, 0 + 500};
201 scrlClientWindow.Create(scrollboxWindow.m_hWnd, scrlClientWindowPos, NULL, WS_CHILD | WS_VISIBLE);
202
203 /* create selection window (initially hidden) */
204 RECT selectionWindowPos = {350, 0, 350 + 100, 0 + 100};
205 selectionWindow.Create(scrlClientWindow.m_hWnd, selectionWindowPos, NULL, WS_CHILD | BS_OWNERDRAW);
206
207 /* creating the window inside the scroll box, on which the image in hDrawingDC's bitmap is drawn */
208 RECT imageAreaPos = {3, 3, 3 + imageModel.GetWidth(), 3 + imageModel.GetHeight()};
209 imageArea.Create(scrlClientWindow.m_hWnd, imageAreaPos, NULL, WS_CHILD | WS_VISIBLE);
210
211 if (lpszArgument[0] != 0)
212 {
213 HBITMAP bmNew = NULL;
214 LoadDIBFromFile(&bmNew, lpszArgument, &fileTime, &fileSize, &fileHPPM, &fileVPPM);
215 if (bmNew != NULL)
216 {
217 TCHAR tempstr[1000];
218 TCHAR resstr[100];
219 TCHAR *temp;
220 imageModel.Insert(bmNew);
221 GetFullPathName(lpszArgument, SIZEOF(filepathname), filepathname, &temp);
222 _tcscpy(filename, temp);
223 LoadString(hProgInstance, IDS_WINDOWTITLE, resstr, SIZEOF(resstr));
224 _stprintf(tempstr, resstr, filename);
225 mainWindow.SetWindowText(tempstr);
226 imageModel.ClearHistory();
227 isAFile = TRUE;
228 }
229 else
230 {
231 exit(0);
232 }
233 }
234
235 /* initializing the CHOOSECOLOR structure for use with ChooseColor */
236 choosecolor.lStructSize = sizeof(CHOOSECOLOR);
237 choosecolor.hwndOwner = hwnd;
238 choosecolor.hInstance = NULL;
239 choosecolor.rgbResult = 0x00ffffff;
240 choosecolor.lpCustColors = (COLORREF*) &custColors;
241 choosecolor.Flags = 0;
242 choosecolor.lCustData = 0;
243 choosecolor.lpfnHook = NULL;
244 choosecolor.lpTemplateName = NULL;
245
246 /* initializing the OPENFILENAME structure for use with GetOpenFileName and GetSaveFileName */
247 CopyMemory(ofnFilename, filename, sizeof(filename));
248 LoadString(hThisInstance, IDS_OPENFILTER, ofnFilter, SIZEOF(ofnFilter));
249 for(c = ofnFilter; *c; c++)
250 if (*c == '\1')
251 *c = '\0';
252 ZeroMemory(&ofn, sizeof(OPENFILENAME));
253 ofn.lStructSize = sizeof(OPENFILENAME);
254 ofn.hwndOwner = hwnd;
255 ofn.hInstance = hThisInstance;
256 ofn.lpstrFilter = ofnFilter;
257 ofn.lpstrFile = ofnFilename;
258 ofn.nMaxFile = SIZEOF(ofnFilename);
259 ofn.lpstrFileTitle = ofnFiletitle;
260 ofn.nMaxFileTitle = SIZEOF(ofnFiletitle);
261 ofn.Flags = OFN_HIDEREADONLY;
262
263 CopyMemory(sfnFilename, filename, sizeof(filename));
264 LoadString(hThisInstance, IDS_SAVEFILTER, sfnFilter, SIZEOF(sfnFilter));
265 for(c = sfnFilter; *c; c++)
266 if (*c == '\1')
267 *c = '\0';
268 ZeroMemory(&sfn, sizeof(OPENFILENAME));
269 sfn.lStructSize = sizeof(OPENFILENAME);
270 sfn.hwndOwner = hwnd;
271 sfn.hInstance = hThisInstance;
272 sfn.lpstrFilter = sfnFilter;
273 sfn.lpstrFile = sfnFilename;
274 sfn.nMaxFile = SIZEOF(sfnFilename);
275 sfn.lpstrFileTitle = sfnFiletitle;
276 sfn.nMaxFileTitle = SIZEOF(sfnFiletitle);
277 sfn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
278
279 /* creating the size boxes */
280 RECT sizeboxPos = {0, 0, 0 + 3, 0 + 3};
281 sizeboxLeftTop.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
282 sizeboxCenterTop.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
283 sizeboxRightTop.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
284 sizeboxLeftCenter.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
285 sizeboxRightCenter.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
286 sizeboxLeftBottom.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
287 sizeboxCenterBottom.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
288 sizeboxRightBottom.Create(scrlClientWindow.m_hWnd, sizeboxPos, NULL, WS_CHILD | WS_VISIBLE);
289 /* placing the size boxes around the image */
290 imageArea.SendMessage(WM_SIZE, 0, 0);
291
292 /* by moving the window, the things in WM_SIZE are done */
293 mainWindow.SetWindowPlacement(&(registrySettings.WindowPlacement));
294
295 /* creating the text editor window for the text tool */
296 RECT textEditWindowPos = {300, 0, 300 + 300, 0 + 200};
297 textEditWindow.Create(hwnd, textEditWindowPos, NULL, WS_OVERLAPPEDWINDOW);
298
299 /* Make the window visible on the screen */
300 ShowWindow (hwnd, nFunsterStil);
301
302 /* inform the system, that the main window accepts dropped files */
303 DragAcceptFiles(hwnd, TRUE);
304
305 /* Run the message loop. It will run until GetMessage() returns 0 */
306 while (GetMessage(&messages, NULL, 0, 0))
307 {
308 TranslateAccelerator(hwnd, (HACCEL) haccel, &messages);
309
310 /* Translate virtual-key messages into character messages */
311 TranslateMessage(&messages);
312 /* Send message to WindowProcedure */
313 DispatchMessage(&messages);
314 }
315
316 /* write back settings to registry */
317 registrySettings.ShowThumbnail = showMiniature;
318 registrySettings.BMPWidth = imageModel.GetWidth();
319 registrySettings.BMPHeight = imageModel.GetHeight();
320 registrySettings.Store();
321
322 /* The program return-value is 0 - The value that PostQuitMessage() gave */
323 return messages.wParam;
324 }