Dmitry Timoshkov <dmitry@codeweavers.com> Fix the usage of COLOR_xxx constants in...
[reactos.git] / reactos / subsys / system / notepad / main.c
1 /*
2 * Notepad
3 *
4 * Copyright 2000 Mike McCormack <Mike_McCormack@looksmart.com.au>
5 * Copyright 1997,98 Marcel Baur <mbaur@g26.ethz.ch>
6 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
7 * Copyright 2002 Andriy Palamarchuk
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25 #define UNICODE
26
27 #include <windows.h>
28 #include <stdio.h>
29
30 #include "main.h"
31 #include "dialog.h"
32 #include "notepad_res.h"
33
34 NOTEPAD_GLOBALS Globals;
35 static ATOM aFINDMSGSTRING;
36
37 /***********************************************************************
38 *
39 * SetFileName
40 *
41 * Sets Global File Name.
42 */
43 VOID SetFileName(LPCWSTR szFileName)
44 {
45 lstrcpy(Globals.szFileName, szFileName);
46 Globals.szFileTitle[0] = 0;
47 GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
48 }
49
50 /***********************************************************************
51 *
52 * NOTEPAD_MenuCommand
53 *
54 * All handling of main menu events
55 */
56 static int NOTEPAD_MenuCommand(WPARAM wParam)
57 {
58 switch (wParam)
59 {
60 case CMD_NEW: DIALOG_FileNew(); break;
61 case CMD_OPEN: DIALOG_FileOpen(); break;
62 case CMD_SAVE: DIALOG_FileSave(); break;
63 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
64 case CMD_PRINT: DIALOG_FilePrint(); break;
65 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
66 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
67 case CMD_EXIT: DIALOG_FileExit(); break;
68
69 case CMD_UNDO: DIALOG_EditUndo(); break;
70 case CMD_CUT: DIALOG_EditCut(); break;
71 case CMD_COPY: DIALOG_EditCopy(); break;
72 case CMD_PASTE: DIALOG_EditPaste(); break;
73 case CMD_DELETE: DIALOG_EditDelete(); break;
74 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
75 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
76
77 case CMD_SEARCH: DIALOG_Search(); break;
78 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
79
80 case CMD_WRAP: DIALOG_EditWrap(); break;
81 case CMD_FONT: DIALOG_SelectFont(); break;
82
83 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
84 case CMD_HELP_SEARCH: DIALOG_HelpSearch(); break;
85 case CMD_HELP_ON_HELP: DIALOG_HelpHelp(); break;
86 case CMD_LICENSE: DIALOG_HelpLicense(); break;
87 case CMD_NO_WARRANTY: DIALOG_HelpNoWarranty(); break;
88 case CMD_ABOUT_WINE: DIALOG_HelpAboutWine(); break;
89
90 default:
91 break;
92 }
93 return 0;
94 }
95
96 /***********************************************************************
97 * Data Initialization
98 */
99 static VOID NOTEPAD_InitData(VOID)
100 {
101 LPWSTR p = Globals.szFilter;
102 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
103 static const WCHAR all_files[] = { '*','.','*',0 };
104
105 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
106 p += lstrlen(p) + 1;
107 lstrcpy(p, txt_files);
108 p += lstrlen(p) + 1;
109 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
110 p += lstrlen(p) + 1;
111 lstrcpy(p, all_files);
112 p += lstrlen(p) + 1;
113 *p = '\0';
114 }
115
116 /***********************************************************************
117 * Enable/disable items on the menu based on control state
118 */
119 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
120 {
121 int enable;
122
123 EnableMenuItem(menu, CMD_UNDO,
124 SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
125 EnableMenuItem(menu, CMD_PASTE,
126 IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
127 enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
128 enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
129 EnableMenuItem(menu, CMD_CUT, enable);
130 EnableMenuItem(menu, CMD_COPY, enable);
131 EnableMenuItem(menu, CMD_DELETE, enable);
132
133 EnableMenuItem(menu, CMD_SELECT_ALL,
134 GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
135 }
136
137 /***********************************************************************
138 *
139 * NOTEPAD_WndProc
140 */
141 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
142 LPARAM lParam)
143 {
144 switch (msg) {
145
146 case WM_CREATE:
147 {
148 static const WCHAR editW[] = { 'e','d','i','t',0 };
149 RECT rc;
150 GetClientRect(hWnd, &rc);
151 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
152 WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL |
153 ES_AUTOVSCROLL | ES_MULTILINE,
154 0, 0, rc.right, rc.bottom, hWnd,
155 NULL, Globals.hInstance, NULL);
156 break;
157 }
158
159 case WM_COMMAND:
160 NOTEPAD_MenuCommand(LOWORD(wParam));
161 break;
162
163 case WM_DESTROYCLIPBOARD:
164 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
165 break;
166
167 case WM_CLOSE:
168 if (DoCloseFile()) {
169 DestroyWindow(hWnd);
170 }
171 break;
172
173 case WM_QUERYENDSESSION:
174 if (DoCloseFile()) {
175 return 1;
176 }
177 break;
178
179 case WM_DESTROY:
180 PostQuitMessage(0);
181 break;
182
183 case WM_SIZE:
184 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
185 SWP_NOOWNERZORDER | SWP_NOZORDER);
186 break;
187
188 case WM_SETFOCUS:
189 SetFocus(Globals.hEdit);
190 break;
191
192 case WM_DROPFILES:
193 {
194 WCHAR szFileName[MAX_PATH];
195 HANDLE hDrop = (HANDLE) wParam;
196
197 DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
198 DragFinish(hDrop);
199 DoOpenFile(szFileName);
200 break;
201 }
202
203 case WM_INITMENUPOPUP:
204 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
205 break;
206
207 default:
208 return DefWindowProc(hWnd, msg, wParam, lParam);
209 }
210 return 0;
211 }
212
213 static int AlertFileDoesNotExist(LPCWSTR szFileName)
214 {
215 int nResult;
216 WCHAR szMessage[MAX_STRING_LEN];
217 WCHAR szResource[MAX_STRING_LEN];
218
219 LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
220 wsprintf(szMessage, szResource, szFileName);
221
222 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
223
224 nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
225 MB_ICONEXCLAMATION | MB_YESNO);
226
227 return(nResult);
228 }
229
230 static void HandleCommandLine(LPWSTR cmdline)
231 {
232 WCHAR delimiter;
233 int opt_print=0;
234
235 /* skip white space */
236 while (*cmdline == ' ') cmdline++;
237
238 /* skip executable name */
239 delimiter = (*cmdline == '"' ? '"' : ' ');
240
241 do
242 {
243 cmdline++;
244 }
245 while (*cmdline && *cmdline != delimiter);
246 if (*cmdline == delimiter) cmdline++;
247
248 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
249 {
250 WCHAR option;
251
252 if (*cmdline++ == ' ') continue;
253
254 option = *cmdline;
255 if (option) cmdline++;
256 while (*cmdline == ' ') cmdline++;
257
258 switch(option)
259 {
260 case 'p':
261 case 'P':
262 opt_print=1;
263 break;
264 }
265 }
266
267 if (*cmdline)
268 {
269 /* file name is passed in the command line */
270 LPCWSTR file_name;
271 BOOL file_exists;
272 WCHAR buf[MAX_PATH];
273
274 if (cmdline[0] == '"')
275 {
276 cmdline++;
277 cmdline[lstrlen(cmdline) - 1] = 0;
278 }
279
280 if (FileExists(cmdline))
281 {
282 file_exists = TRUE;
283 file_name = cmdline;
284 }
285 else
286 {
287 static const WCHAR txtW[] = { '.','t','x','t',0 };
288
289 /* try to find file with ".txt" extension */
290 if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
291 {
292 file_exists = FALSE;
293 file_name = cmdline;
294 }
295 else
296 {
297 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
298 lstrcat(buf, txtW);
299 file_name = buf;
300 file_exists = FileExists(buf);
301 }
302 }
303
304 if (file_exists)
305 {
306 DoOpenFile(file_name);
307 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
308 if (opt_print)
309 DIALOG_FilePrint();
310 }
311 else
312 {
313 switch (AlertFileDoesNotExist(file_name)) {
314 case IDYES:
315 DoOpenFile(file_name);
316 break;
317
318 case IDNO:
319 break;
320 }
321 }
322 }
323 }
324
325 /***********************************************************************
326 *
327 * WinMain
328 */
329 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
330 {
331 MSG msg;
332 HACCEL hAccel;
333 WNDCLASSEX class;
334 static const WCHAR className[] = {'N','P','C','l','a','s','s',0};
335 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
336
337 aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
338
339 ZeroMemory(&Globals, sizeof(Globals));
340 Globals.hInstance = hInstance;
341
342 ZeroMemory(&class, sizeof(class));
343 class.cbSize = sizeof(class);
344 class.lpfnWndProc = NOTEPAD_WndProc;
345 class.hInstance = Globals.hInstance;
346 class.hIcon = LoadIcon(0, IDI_APPLICATION);
347 class.hCursor = LoadCursor(0, IDC_ARROW);
348 class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
349 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
350 class.lpszClassName = className;
351
352 if (!RegisterClassEx(&class)) return FALSE;
353
354 /* Setup windows */
355
356 Globals.hMainWnd =
357 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
358 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
359 NULL, NULL, Globals.hInstance, NULL);
360 if (!Globals.hMainWnd)
361 {
362 ShowLastError();
363 ExitProcess(1);
364 }
365
366 NOTEPAD_InitData();
367 DIALOG_FileNew();
368
369 ShowWindow(Globals.hMainWnd, show);
370 UpdateWindow(Globals.hMainWnd);
371 DragAcceptFiles(Globals.hMainWnd, TRUE);
372
373 HandleCommandLine(GetCommandLine());
374
375 hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
376
377 while (GetMessage(&msg, 0, 0, 0))
378 {
379 if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
380 {
381 TranslateMessage(&msg);
382 DispatchMessage(&msg);
383 }
384 }
385 return msg.wParam;
386 }