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