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