25c23e80c9181d4631a2660ec9aaced8183021bd
[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 #define _UNICODE
27
28 #include <windows.h>
29 #include <stdio.h>
30 #include <tchar.h>
31
32 #include "main.h"
33 #include "dialog.h"
34 #include "notepad_res.h"
35
36 NOTEPAD_GLOBALS Globals;
37 static ATOM aFINDMSGSTRING;
38
39 /***********************************************************************
40 *
41 * SetFileName
42 *
43 * Sets Global File Name.
44 */
45 VOID SetFileName(LPCWSTR szFileName)
46 {
47 lstrcpy(Globals.szFileName, szFileName);
48 Globals.szFileTitle[0] = 0;
49 GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
50 }
51
52 /***********************************************************************
53 *
54 * NOTEPAD_MenuCommand
55 *
56 * All handling of main menu events
57 */
58 static int NOTEPAD_MenuCommand(WPARAM wParam)
59 {
60 switch (wParam)
61 {
62 case CMD_NEW: DIALOG_FileNew(); break;
63 case CMD_OPEN: DIALOG_FileOpen(); break;
64 case CMD_SAVE: DIALOG_FileSave(); break;
65 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
66 case CMD_PRINT: DIALOG_FilePrint(); break;
67 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
68 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
69 case CMD_EXIT: DIALOG_FileExit(); break;
70
71 case CMD_UNDO: DIALOG_EditUndo(); break;
72 case CMD_CUT: DIALOG_EditCut(); break;
73 case CMD_COPY: DIALOG_EditCopy(); break;
74 case CMD_PASTE: DIALOG_EditPaste(); break;
75 case CMD_DELETE: DIALOG_EditDelete(); break;
76 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
77 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
78
79 case CMD_SEARCH: DIALOG_Search(); break;
80 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
81
82 case CMD_WRAP: DIALOG_EditWrap(); break;
83 case CMD_FONT: DIALOG_SelectFont(); break;
84
85 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
86 case CMD_HELP_SEARCH: DIALOG_HelpSearch(); break;
87 case CMD_HELP_ON_HELP: DIALOG_HelpHelp(); break;
88 case CMD_LICENSE: DIALOG_HelpLicense(); break;
89 case CMD_NO_WARRANTY: DIALOG_HelpNoWarranty(); break;
90 case CMD_ABOUT_WINE: DIALOG_HelpAboutWine(); break;
91
92 default:
93 break;
94 }
95 return 0;
96 }
97
98 /***********************************************************************
99 *
100 * NOTEPAD_FindNext
101 */
102
103 static VOID NOTEPAD_FindNext(FINDREPLACE *pFindReplace)
104 {
105 int iTextLength, iTargetLength;
106 LPTSTR pszText = NULL;
107 DWORD dwPosition, dwDummy;
108 BOOL bMatches = FALSE;
109
110 iTargetLength = _tcslen(pFindReplace->lpstrFindWhat);
111
112 iTextLength = GetWindowTextLength(Globals.hEdit);
113
114 if (iTextLength > 0)
115 {
116 pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (iTextLength + 1) * sizeof(TCHAR));
117 if (!pszText)
118 return;
119
120 GetWindowText(Globals.hEdit, pszText, iTextLength + 1);
121 }
122
123 SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwDummy, (LPARAM) &dwPosition);
124
125 while(dwPosition < iTextLength)
126 {
127 /* Make proper comparison */
128 if (pFindReplace->Flags & FR_MATCHCASE)
129 bMatches = !_tcsncmp(&pszText[dwPosition], pFindReplace->lpstrFindWhat, iTargetLength);
130 else
131 bMatches = !_tcsnicmp(&pszText[dwPosition], pFindReplace->lpstrFindWhat, iTargetLength);
132
133 if (bMatches && pFindReplace->Flags & FR_WHOLEWORD)
134 {
135 if ((dwPosition > 0) && !_istspace(pszText[dwPosition-1]))
136 bMatches = FALSE;
137 if ((dwPosition < iTextLength - 1) && !_istspace(pszText[dwPosition+1]))
138 bMatches = FALSE;
139 }
140
141 if (bMatches)
142 break;
143
144 if (pFindReplace->Flags & FR_DOWN)
145 dwPosition++;
146 else
147 dwPosition--;
148 }
149
150 if (bMatches)
151 {
152 SendMessage(Globals.hEdit, EM_SETSEL, dwPosition, dwPosition + iTargetLength);
153 SendMessage(Globals.hEdit, EM_SCROLLCARET, 0, 0);
154 }
155
156 if (pszText)
157 HeapFree(GetProcessHeap(), 0, pszText);
158 }
159
160 /***********************************************************************
161 *
162 * NOTEPAD_FindTerm
163 */
164
165 static VOID NOTEPAD_FindTerm(VOID)
166 {
167 Globals.hFindReplaceDlg = NULL;
168 }
169
170 /***********************************************************************
171 * Data Initialization
172 */
173 static VOID NOTEPAD_InitData(VOID)
174 {
175 LPWSTR p = Globals.szFilter;
176 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
177 static const WCHAR all_files[] = { '*','.','*',0 };
178
179 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
180 p += lstrlen(p) + 1;
181 lstrcpy(p, txt_files);
182 p += lstrlen(p) + 1;
183 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
184 p += lstrlen(p) + 1;
185 lstrcpy(p, all_files);
186 p += lstrlen(p) + 1;
187 *p = '\0';
188 }
189
190 /***********************************************************************
191 * Enable/disable items on the menu based on control state
192 */
193 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
194 {
195 int enable;
196
197 EnableMenuItem(menu, CMD_UNDO,
198 SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
199 EnableMenuItem(menu, CMD_PASTE,
200 IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
201 enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
202 enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
203 EnableMenuItem(menu, CMD_CUT, enable);
204 EnableMenuItem(menu, CMD_COPY, enable);
205 EnableMenuItem(menu, CMD_DELETE, enable);
206
207 EnableMenuItem(menu, CMD_SELECT_ALL,
208 GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
209 }
210
211 /***********************************************************************
212 *
213 * NOTEPAD_WndProc
214 */
215 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
216 LPARAM lParam)
217 {
218 switch (msg) {
219
220 case WM_CREATE:
221 {
222 static const WCHAR editW[] = { 'e','d','i','t',0 };
223 RECT rc;
224 GetClientRect(hWnd, &rc);
225 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
226 WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL |
227 ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL,
228 0, 0, rc.right, rc.bottom, hWnd,
229 NULL, Globals.hInstance, NULL);
230 break;
231 }
232
233 case WM_COMMAND:
234 NOTEPAD_MenuCommand(LOWORD(wParam));
235 break;
236
237 case WM_DESTROYCLIPBOARD:
238 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
239 break;
240
241 case WM_CLOSE:
242 if (DoCloseFile()) {
243 DestroyWindow(hWnd);
244 }
245 break;
246
247 case WM_QUERYENDSESSION:
248 if (DoCloseFile()) {
249 return 1;
250 }
251 break;
252
253 case WM_DESTROY:
254 PostQuitMessage(0);
255 break;
256
257 case WM_SIZE:
258 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
259 SWP_NOOWNERZORDER | SWP_NOZORDER);
260 break;
261
262 case WM_SETFOCUS:
263 SetFocus(Globals.hEdit);
264 break;
265
266 case WM_DROPFILES:
267 {
268 WCHAR szFileName[MAX_PATH];
269 HANDLE hDrop = (HANDLE) wParam;
270
271 DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
272 DragFinish(hDrop);
273 DoOpenFile(szFileName);
274 break;
275 }
276
277 case WM_INITMENUPOPUP:
278 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
279 break;
280
281 default:
282 if (msg == aFINDMSGSTRING)
283 {
284 FINDREPLACE *pFindReplace = (FINDREPLACE *) lParam;
285
286 if (pFindReplace->Flags & FR_FINDNEXT)
287 NOTEPAD_FindNext(pFindReplace);
288 else if (pFindReplace->Flags & FR_DIALOGTERM)
289 NOTEPAD_FindTerm();
290 break;
291 }
292
293 return DefWindowProc(hWnd, msg, wParam, lParam);
294 }
295 return 0;
296 }
297
298 static int AlertFileDoesNotExist(LPCWSTR szFileName)
299 {
300 int nResult;
301 WCHAR szMessage[MAX_STRING_LEN];
302 WCHAR szResource[MAX_STRING_LEN];
303
304 LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
305 wsprintf(szMessage, szResource, szFileName);
306
307 LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
308
309 nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
310 MB_ICONEXCLAMATION | MB_YESNO);
311
312 return(nResult);
313 }
314
315 static void HandleCommandLine(LPWSTR cmdline)
316 {
317 WCHAR delimiter;
318 int opt_print=0;
319
320 /* skip white space */
321 while (*cmdline == ' ') cmdline++;
322
323 /* skip executable name */
324 delimiter = (*cmdline == '"' ? '"' : ' ');
325
326 do
327 {
328 cmdline++;
329 }
330 while (*cmdline && *cmdline != delimiter);
331 if (*cmdline == delimiter) cmdline++;
332
333 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
334 {
335 WCHAR option;
336
337 if (*cmdline++ == ' ') continue;
338
339 option = *cmdline;
340 if (option) cmdline++;
341 while (*cmdline == ' ') cmdline++;
342
343 switch(option)
344 {
345 case 'p':
346 case 'P':
347 opt_print=1;
348 break;
349 }
350 }
351
352 if (*cmdline)
353 {
354 /* file name is passed in the command line */
355 LPCWSTR file_name;
356 BOOL file_exists;
357 WCHAR buf[MAX_PATH];
358
359 if (cmdline[0] == '"')
360 {
361 cmdline++;
362 cmdline[lstrlen(cmdline) - 1] = 0;
363 }
364
365 if (FileExists(cmdline))
366 {
367 file_exists = TRUE;
368 file_name = cmdline;
369 }
370 else
371 {
372 static const WCHAR txtW[] = { '.','t','x','t',0 };
373
374 /* try to find file with ".txt" extension */
375 if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
376 {
377 file_exists = FALSE;
378 file_name = cmdline;
379 }
380 else
381 {
382 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
383 lstrcat(buf, txtW);
384 file_name = buf;
385 file_exists = FileExists(buf);
386 }
387 }
388
389 if (file_exists)
390 {
391 DoOpenFile(file_name);
392 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
393 if (opt_print)
394 DIALOG_FilePrint();
395 }
396 else
397 {
398 switch (AlertFileDoesNotExist(file_name)) {
399 case IDYES:
400 DoOpenFile(file_name);
401 break;
402
403 case IDNO:
404 break;
405 }
406 }
407 }
408 }
409
410 /***********************************************************************
411 *
412 * WinMain
413 */
414 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
415 {
416 MSG msg;
417 HACCEL hAccel;
418 WNDCLASSEX class;
419 static const WCHAR className[] = {'N','P','C','l','a','s','s',0};
420 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
421
422 aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
423
424 ZeroMemory(&Globals, sizeof(Globals));
425 Globals.hInstance = hInstance;
426
427 ZeroMemory(&class, sizeof(class));
428 class.cbSize = sizeof(class);
429 class.lpfnWndProc = NOTEPAD_WndProc;
430 class.hInstance = Globals.hInstance;
431 class.hIcon = LoadIcon(0, IDI_APPLICATION);
432 class.hCursor = LoadCursor(0, IDC_ARROW);
433 class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
434 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
435 class.lpszClassName = className;
436
437 if (!RegisterClassEx(&class)) return FALSE;
438
439 /* Setup windows */
440
441 Globals.hMainWnd =
442 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
443 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
444 NULL, NULL, Globals.hInstance, NULL);
445 if (!Globals.hMainWnd)
446 {
447 ShowLastError();
448 ExitProcess(1);
449 }
450
451 NOTEPAD_InitData();
452 DIALOG_FileNew();
453
454 ShowWindow(Globals.hMainWnd, show);
455 UpdateWindow(Globals.hMainWnd);
456 DragAcceptFiles(Globals.hMainWnd, TRUE);
457
458 HandleCommandLine(GetCommandLine());
459
460 hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
461
462 while (GetMessage(&msg, 0, 0, 0))
463 {
464 if (!IsDialogMessage(Globals.hFindReplaceDlg, &msg) &&
465 !TranslateAccelerator(Globals.hMainWnd, hAccel, &msg))
466 {
467 TranslateMessage(&msg);
468 DispatchMessage(&msg);
469 }
470 }
471 return msg.wParam;
472 }