ntdll/tests: Add a bunch of tests for creating kernel objects with the names containi...
[wine.git] / programs / notepad / main.c
blobb1cdad8156d2b102f940ee513bd5f75172fce37c
1 /*
2 * Notepad
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
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <stdio.h>
26 #include <windows.h>
27 #include <commctrl.h>
28 #include <commdlg.h>
29 #include <shellapi.h>
30 #include <shlwapi.h>
32 #include "main.h"
33 #include "dialog.h"
34 #include "notepad_res.h"
36 NOTEPAD_GLOBALS Globals;
37 static ATOM aFINDMSGSTRING;
38 static RECT main_rect;
40 static const WCHAR notepad_reg_key[] = {'S','o','f','t','w','a','r','e','\\',
41 'M','i','c','r','o','s','o','f','t','\\','N','o','t','e','p','a','d','\0'};
42 static const WCHAR value_fWrap[] = {'f','W','r','a','p','\0'};
43 static const WCHAR value_iPointSize[] = {'i','P','o','i','n','t','S','i','z','e','\0'};
44 static const WCHAR value_iWindowPosDX[] = {'i','W','i','n','d','o','w','P','o','s','D','X','\0'};
45 static const WCHAR value_iWindowPosDY[] = {'i','W','i','n','d','o','w','P','o','s','D','Y','\0'};
46 static const WCHAR value_iWindowPosX[] = {'i','W','i','n','d','o','w','P','o','s','X','\0'};
47 static const WCHAR value_iWindowPosY[] = {'i','W','i','n','d','o','w','P','o','s','Y','\0'};
48 static const WCHAR value_lfCharSet[] = {'l','f','C','h','a','r','S','e','t','\0'};
49 static const WCHAR value_lfClipPrecision[] = {'l','f','C','l','i','p','P','r','e','c','i','s','i','o','n','\0'};
50 static const WCHAR value_lfEscapement[] = {'l','f','E','s','c','a','p','e','m','e','n','t','\0'};
51 static const WCHAR value_lfItalic[] = {'l','f','I','t','a','l','i','c','\0'};
52 static const WCHAR value_lfOrientation[] = {'l','f','O','r','i','e','n','t','a','t','i','o','n','\0'};
53 static const WCHAR value_lfOutPrecision[] = {'l','f','O','u','t','P','r','e','c','i','s','i','o','n','\0'};
54 static const WCHAR value_lfPitchAndFamily[] = {'l','f','P','i','t','c','h','A','n','d','F','a','m','i','l','y','\0'};
55 static const WCHAR value_lfQuality[] = {'l','f','Q','u','a','l','i','t','y','\0'};
56 static const WCHAR value_lfStrikeOut[] = {'l','f','S','t','r','i','k','e','O','u','t','\0'};
57 static const WCHAR value_lfUnderline[] = {'l','f','U','n','d','e','r','l','i','n','e','\0'};
58 static const WCHAR value_lfWeight[] = {'l','f','W','e','i','g','h','t','\0'};
59 static const WCHAR value_lfFaceName[] = {'l','f','F','a','c','e','N','a','m','e','\0'};
60 static const WCHAR value_iMarginTop[] = {'i','M','a','r','g','i','n','T','o','p','\0'};
61 static const WCHAR value_iMarginBottom[] = {'i','M','a','r','g','i','n','B','o','t','t','o','m','\0'};
62 static const WCHAR value_iMarginLeft[] = {'i','M','a','r','g','i','n','L','e','f','t','\0'};
63 static const WCHAR value_iMarginRight[] = {'i','M','a','r','g','i','n','R','i','g','h','t','\0'};
64 static const WCHAR value_szHeader[] = {'s','z','H','e','a','d','e','r','\0'};
65 static const WCHAR value_szFooter[] = {'s','z','T','r','a','i','l','e','r','\0'};
67 /***********************************************************************
69 * SetFileNameAndEncoding
71 * Sets global file name and encoding (which is used to preselect original
72 * encoding in Save As dialog, and when saving without using the Save As
73 * dialog).
75 VOID SetFileNameAndEncoding(LPCWSTR szFileName, ENCODING enc)
77 lstrcpyW(Globals.szFileName, szFileName);
78 Globals.szFileTitle[0] = 0;
79 GetFileTitleW(szFileName, Globals.szFileTitle, ARRAY_SIZE(Globals.szFileTitle));
80 Globals.encFile = enc;
83 /******************************************************************************
84 * get_dpi
86 * Get the dpi from registry HKCC\Software\Fonts\LogPixels.
88 DWORD get_dpi(void)
90 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
91 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
92 DWORD dpi = 96;
93 HKEY hkey;
95 if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
97 DWORD type, size, new_dpi;
99 size = sizeof(new_dpi);
100 if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (LPBYTE)&new_dpi, &size) == ERROR_SUCCESS)
102 if(type == REG_DWORD && new_dpi != 0)
103 dpi = new_dpi;
105 RegCloseKey(hkey);
107 return dpi;
110 /***********************************************************************
112 * NOTEPAD_SaveSettingToRegistry
114 * Save setting to registry HKCU\Software\Microsoft\Notepad.
116 static VOID NOTEPAD_SaveSettingToRegistry(void)
118 HKEY hkey;
119 DWORD disp;
121 if(RegCreateKeyExW(HKEY_CURRENT_USER, notepad_reg_key, 0, NULL,
122 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disp) == ERROR_SUCCESS)
124 DWORD data;
125 WINDOWPLACEMENT wndpl;
127 wndpl.length = sizeof(WINDOWPLACEMENT);
128 GetWindowPlacement(Globals.hMainWnd, &wndpl);
129 main_rect = wndpl.rcNormalPosition;
131 #define SET_NOTEPAD_REG(hkey, value_name, value_data) do { DWORD data = value_data; RegSetValueExW(hkey, value_name, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); }while(0)
132 SET_NOTEPAD_REG(hkey, value_fWrap, Globals.bWrapLongLines);
133 SET_NOTEPAD_REG(hkey, value_iWindowPosX, main_rect.left);
134 SET_NOTEPAD_REG(hkey, value_iWindowPosY, main_rect.top);
135 SET_NOTEPAD_REG(hkey, value_iWindowPosDX, main_rect.right - main_rect.left);
136 SET_NOTEPAD_REG(hkey, value_iWindowPosDY, main_rect.bottom - main_rect.top);
137 SET_NOTEPAD_REG(hkey, value_lfCharSet, Globals.lfFont.lfCharSet);
138 SET_NOTEPAD_REG(hkey, value_lfClipPrecision, Globals.lfFont.lfClipPrecision);
139 SET_NOTEPAD_REG(hkey, value_lfEscapement, Globals.lfFont.lfEscapement);
140 SET_NOTEPAD_REG(hkey, value_lfItalic, Globals.lfFont.lfItalic);
141 SET_NOTEPAD_REG(hkey, value_lfOrientation, Globals.lfFont.lfOrientation);
142 SET_NOTEPAD_REG(hkey, value_lfOutPrecision, Globals.lfFont.lfOutPrecision);
143 SET_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
144 SET_NOTEPAD_REG(hkey, value_lfQuality, Globals.lfFont.lfQuality);
145 SET_NOTEPAD_REG(hkey, value_lfStrikeOut, Globals.lfFont.lfStrikeOut);
146 SET_NOTEPAD_REG(hkey, value_lfUnderline, Globals.lfFont.lfUnderline);
147 SET_NOTEPAD_REG(hkey, value_lfWeight, Globals.lfFont.lfWeight);
148 SET_NOTEPAD_REG(hkey, value_iMarginTop, Globals.iMarginTop);
149 SET_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom);
150 SET_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft);
151 SET_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight);
152 #undef SET_NOTEPAD_REG
154 /* Store the current value as 10 * twips */
155 data = MulDiv(abs(Globals.lfFont.lfHeight), 720 , get_dpi());
156 RegSetValueExW(hkey, value_iPointSize, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
158 RegSetValueExW(hkey, value_lfFaceName, 0, REG_SZ, (LPBYTE)&Globals.lfFont.lfFaceName,
159 lstrlenW(Globals.lfFont.lfFaceName) * sizeof(Globals.lfFont.lfFaceName[0]));
161 RegSetValueExW(hkey, value_szHeader, 0, REG_SZ, (LPBYTE)&Globals.szHeader,
162 lstrlenW(Globals.szHeader) * sizeof(Globals.szHeader[0]));
164 RegSetValueExW(hkey, value_szFooter, 0, REG_SZ, (LPBYTE)&Globals.szFooter,
165 lstrlenW(Globals.szFooter) * sizeof(Globals.szFooter[0]));
167 RegCloseKey(hkey);
171 /***********************************************************************
173 * NOTEPAD_LoadSettingFromRegistry
175 * Load setting from registry HKCU\Software\Microsoft\Notepad.
177 static VOID NOTEPAD_LoadSettingFromRegistry(void)
179 static const WCHAR systemW[] = { 'S','y','s','t','e','m','\0' };
180 HKEY hkey;
181 INT base_length, dx, dy;
183 base_length = (GetSystemMetrics(SM_CXSCREEN) > GetSystemMetrics(SM_CYSCREEN))?
184 GetSystemMetrics(SM_CYSCREEN) : GetSystemMetrics(SM_CXSCREEN);
186 dx = base_length * .95;
187 dy = dx * 3 / 4;
188 SetRect( &main_rect, 0, 0, dx, dy );
190 Globals.bWrapLongLines = TRUE;
191 Globals.iMarginTop = 2500;
192 Globals.iMarginBottom = 2500;
193 Globals.iMarginLeft = 2000;
194 Globals.iMarginRight = 2000;
196 Globals.lfFont.lfHeight = -12;
197 Globals.lfFont.lfWidth = 0;
198 Globals.lfFont.lfEscapement = 0;
199 Globals.lfFont.lfOrientation = 0;
200 Globals.lfFont.lfWeight = FW_REGULAR;
201 Globals.lfFont.lfItalic = FALSE;
202 Globals.lfFont.lfUnderline = FALSE;
203 Globals.lfFont.lfStrikeOut = FALSE;
204 Globals.lfFont.lfCharSet = DEFAULT_CHARSET;
205 Globals.lfFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
206 Globals.lfFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
207 Globals.lfFont.lfQuality = DEFAULT_QUALITY;
208 Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
209 lstrcpyW(Globals.lfFont.lfFaceName, systemW);
211 LoadStringW(Globals.hInstance, STRING_PAGESETUP_HEADERVALUE,
212 Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
213 LoadStringW(Globals.hInstance, STRING_PAGESETUP_FOOTERVALUE,
214 Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
216 if(RegOpenKeyW(HKEY_CURRENT_USER, notepad_reg_key, &hkey) == ERROR_SUCCESS)
218 WORD data_helper[MAX_PATH];
219 DWORD type, size;
220 int point_size;
222 #define QUERY_NOTEPAD_REG(hkey, value_name, ret) do { DWORD type, data; DWORD size = sizeof(DWORD); if(RegQueryValueExW(hkey, value_name, 0, &type, (LPBYTE)&data, &size) == ERROR_SUCCESS) if(type == REG_DWORD) ret = data; } while(0)
223 QUERY_NOTEPAD_REG(hkey, value_fWrap, Globals.bWrapLongLines);
224 QUERY_NOTEPAD_REG(hkey, value_iWindowPosX, main_rect.left);
225 QUERY_NOTEPAD_REG(hkey, value_iWindowPosY, main_rect.top);
226 QUERY_NOTEPAD_REG(hkey, value_iWindowPosDX, dx);
227 QUERY_NOTEPAD_REG(hkey, value_iWindowPosDY, dy);
228 QUERY_NOTEPAD_REG(hkey, value_lfCharSet, Globals.lfFont.lfCharSet);
229 QUERY_NOTEPAD_REG(hkey, value_lfClipPrecision, Globals.lfFont.lfClipPrecision);
230 QUERY_NOTEPAD_REG(hkey, value_lfEscapement, Globals.lfFont.lfEscapement);
231 QUERY_NOTEPAD_REG(hkey, value_lfItalic, Globals.lfFont.lfItalic);
232 QUERY_NOTEPAD_REG(hkey, value_lfOrientation, Globals.lfFont.lfOrientation);
233 QUERY_NOTEPAD_REG(hkey, value_lfOutPrecision, Globals.lfFont.lfOutPrecision);
234 QUERY_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
235 QUERY_NOTEPAD_REG(hkey, value_lfQuality, Globals.lfFont.lfQuality);
236 QUERY_NOTEPAD_REG(hkey, value_lfStrikeOut, Globals.lfFont.lfStrikeOut);
237 QUERY_NOTEPAD_REG(hkey, value_lfUnderline, Globals.lfFont.lfUnderline);
238 QUERY_NOTEPAD_REG(hkey, value_lfWeight, Globals.lfFont.lfWeight);
239 QUERY_NOTEPAD_REG(hkey, value_iMarginTop, Globals.iMarginTop);
240 QUERY_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom);
241 QUERY_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft);
242 QUERY_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight);
243 #undef QUERY_NOTEPAD_REG
245 main_rect.right = main_rect.left + dx;
246 main_rect.bottom = main_rect.top + dy;
248 size = sizeof(DWORD);
249 if(RegQueryValueExW(hkey, value_iPointSize, 0, &type, (LPBYTE)&point_size, &size) == ERROR_SUCCESS)
250 if(type == REG_DWORD)
251 /* The value is stored as 10 * twips */
252 Globals.lfFont.lfHeight = -MulDiv(abs(point_size), get_dpi(), 720);
254 size = sizeof(Globals.lfFont.lfFaceName);
255 if(RegQueryValueExW(hkey, value_lfFaceName, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
256 if(type == REG_SZ)
257 lstrcpyW(Globals.lfFont.lfFaceName, data_helper);
259 size = sizeof(Globals.szHeader);
260 if(RegQueryValueExW(hkey, value_szHeader, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
261 if(type == REG_SZ)
262 lstrcpyW(Globals.szHeader, data_helper);
264 size = sizeof(Globals.szFooter);
265 if(RegQueryValueExW(hkey, value_szFooter, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
266 if(type == REG_SZ)
267 lstrcpyW(Globals.szFooter, data_helper);
268 RegCloseKey(hkey);
272 /***********************************************************************
274 * NOTEPAD_MenuCommand
276 * All handling of main menu events
278 static int NOTEPAD_MenuCommand(WPARAM wParam)
280 switch (wParam)
282 case CMD_NEW: DIALOG_FileNew(); break;
283 case CMD_OPEN: DIALOG_FileOpen(); break;
284 case CMD_SAVE: DIALOG_FileSave(); break;
285 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
286 case CMD_PRINT: DIALOG_FilePrint(); break;
287 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
288 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
289 case CMD_EXIT: DIALOG_FileExit(); break;
291 case CMD_UNDO: DIALOG_EditUndo(); break;
292 case CMD_CUT: DIALOG_EditCut(); break;
293 case CMD_COPY: DIALOG_EditCopy(); break;
294 case CMD_PASTE: DIALOG_EditPaste(); break;
295 case CMD_DELETE: DIALOG_EditDelete(); break;
296 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
297 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
299 case CMD_SEARCH: DIALOG_Search(); break;
300 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
301 case CMD_REPLACE: DIALOG_Replace(); break;
303 case CMD_WRAP: DIALOG_EditWrap(); break;
304 case CMD_FONT: DIALOG_SelectFont(); break;
306 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
307 case CMD_HELP_ABOUT_NOTEPAD: DIALOG_HelpAboutNotepad(); break;
309 default:
310 break;
312 return 0;
315 /***********************************************************************
316 * Data Initialization
318 static VOID NOTEPAD_InitData(VOID)
320 LPWSTR p = Globals.szFilter;
321 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
322 static const WCHAR all_files[] = { '*','.','*',0 };
324 LoadStringW(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
325 p += lstrlenW(p) + 1;
326 lstrcpyW(p, txt_files);
327 p += lstrlenW(p) + 1;
328 LoadStringW(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
329 p += lstrlenW(p) + 1;
330 lstrcpyW(p, all_files);
331 p += lstrlenW(p) + 1;
332 *p = '\0';
333 Globals.hDevMode = NULL;
334 Globals.hDevNames = NULL;
336 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
337 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
340 /***********************************************************************
341 * Enable/disable items on the menu based on control state
343 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
345 int enable;
347 EnableMenuItem(menu, CMD_UNDO,
348 SendMessageW(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
349 EnableMenuItem(menu, CMD_PASTE,
350 IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
351 enable = SendMessageW(Globals.hEdit, EM_GETSEL, 0, 0);
352 enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
353 EnableMenuItem(menu, CMD_CUT, enable);
354 EnableMenuItem(menu, CMD_COPY, enable);
355 EnableMenuItem(menu, CMD_DELETE, enable);
357 EnableMenuItem(menu, CMD_SELECT_ALL,
358 GetWindowTextLengthW(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
361 static LPWSTR NOTEPAD_StrRStr(LPWSTR pszSource, LPWSTR pszLast, LPWSTR pszSrch)
363 int len = lstrlenW(pszSrch);
364 pszLast--;
365 while (pszLast >= pszSource)
367 if (StrCmpNW(pszLast, pszSrch, len) == 0)
368 return pszLast;
369 pszLast--;
371 return NULL;
374 /***********************************************************************
375 * The user activated the Find dialog
377 void NOTEPAD_DoFind(FINDREPLACEW *fr)
379 LPWSTR content;
380 int len = lstrlenW(fr->lpstrFindWhat);
381 int fileLen;
382 SIZE_T pos;
384 fileLen = GetWindowTextLengthW(Globals.hEdit) + 1;
385 content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(WCHAR));
386 if (!content) return;
387 GetWindowTextW(Globals.hEdit, content, fileLen);
389 SendMessageW(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
390 switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
392 case 0:
393 pos = StrRStrIW(content, content+pos-len, fr->lpstrFindWhat) - content;
394 if (pos == -(SIZE_T)content) pos = ~(SIZE_T)0;
395 break;
396 case FR_DOWN:
397 pos = StrStrIW(content+pos, fr->lpstrFindWhat) - content;
398 if (pos == -(SIZE_T)content) pos = ~(SIZE_T)0;
399 break;
400 case FR_MATCHCASE:
401 pos = NOTEPAD_StrRStr(content, content+pos-len, fr->lpstrFindWhat) - content;
402 if (pos == -(SIZE_T)content) pos = ~(SIZE_T)0;
403 break;
404 case FR_DOWN|FR_MATCHCASE:
405 pos = StrStrW(content+pos, fr->lpstrFindWhat) - content;
406 if (pos == -(SIZE_T)content) pos = ~(SIZE_T)0;
407 break;
408 default: /* shouldn't happen */
409 return;
411 HeapFree(GetProcessHeap(), 0, content);
413 if (pos == ~(SIZE_T)0)
415 DIALOG_StringMsgBox(Globals.hFindReplaceDlg, STRING_NOTFOUND, fr->lpstrFindWhat,
416 MB_ICONINFORMATION|MB_OK);
417 return;
420 SendMessageW(Globals.hEdit, EM_SETSEL, pos, pos + len);
423 static void NOTEPAD_DoReplace(FINDREPLACEW *fr)
425 LPWSTR content;
426 int len = lstrlenW(fr->lpstrFindWhat);
427 int fileLen;
428 DWORD pos;
429 DWORD pos_start;
431 fileLen = GetWindowTextLengthW(Globals.hEdit) + 1;
432 content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(WCHAR));
433 if (!content) return;
434 GetWindowTextW(Globals.hEdit, content, fileLen);
436 SendMessageW(Globals.hEdit, EM_GETSEL, (WPARAM)&pos_start, (LPARAM)&pos);
437 switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
439 case FR_DOWN:
440 if ( pos-pos_start == len && StrCmpNIW(fr->lpstrFindWhat, content+pos_start, len) == 0)
441 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
442 break;
443 case FR_DOWN|FR_MATCHCASE:
444 if ( pos-pos_start == len && StrCmpNW(fr->lpstrFindWhat, content+pos_start, len) == 0)
445 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
446 break;
447 default: /* shouldn't happen */
448 return;
450 HeapFree(GetProcessHeap(), 0, content);
452 NOTEPAD_DoFind(fr);
455 static void NOTEPAD_DoReplaceAll(FINDREPLACEW *fr)
457 LPWSTR content;
458 int len = lstrlenW(fr->lpstrFindWhat);
459 int fileLen;
460 SIZE_T pos;
462 SendMessageW(Globals.hEdit, EM_SETSEL, 0, 0);
463 while(TRUE){
464 fileLen = GetWindowTextLengthW(Globals.hEdit) + 1;
465 content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(WCHAR));
466 if (!content) return;
467 GetWindowTextW(Globals.hEdit, content, fileLen);
469 SendMessageW(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
470 switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
472 case FR_DOWN:
473 pos = StrStrIW(content+pos, fr->lpstrFindWhat) - content;
474 if (pos == -(SIZE_T)content) pos = ~(SIZE_T)0;
475 break;
476 case FR_DOWN|FR_MATCHCASE:
477 pos = StrStrW(content+pos, fr->lpstrFindWhat) - content;
478 if (pos == -(SIZE_T)content) pos = ~(SIZE_T)0;
479 break;
480 default: /* shouldn't happen */
481 return;
483 HeapFree(GetProcessHeap(), 0, content);
485 if(pos == ~(SIZE_T)0)
487 SendMessageW(Globals.hEdit, EM_SETSEL, 0, 0);
488 return;
490 SendMessageW(Globals.hEdit, EM_SETSEL, pos, pos + len);
491 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
495 /***********************************************************************
497 * NOTEPAD_WndProc
499 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
500 LPARAM lParam)
502 if (msg == aFINDMSGSTRING) /* not a constant so can't be used in switch */
504 FINDREPLACEW *fr = (FINDREPLACEW *)lParam;
506 if (fr->Flags & FR_DIALOGTERM)
507 Globals.hFindReplaceDlg = NULL;
508 if (fr->Flags & FR_FINDNEXT)
510 Globals.lastFind = *fr;
511 NOTEPAD_DoFind(fr);
513 if (fr->Flags & FR_REPLACE)
515 Globals.lastFind = *fr;
516 NOTEPAD_DoReplace(fr);
518 if (fr->Flags & FR_REPLACEALL)
520 Globals.lastFind = *fr;
521 NOTEPAD_DoReplaceAll(fr);
523 return 0;
526 switch (msg) {
528 case WM_CREATE:
530 static const WCHAR editW[] = { 'e','d','i','t',0 };
531 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
532 ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
533 RECT rc;
534 GetClientRect(hWnd, &rc);
536 if (!Globals.bWrapLongLines) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
538 Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL,
539 dwStyle, 0, 0, rc.right, rc.bottom, hWnd,
540 NULL, Globals.hInstance, NULL);
542 Globals.hFont = CreateFontIndirectW(&Globals.lfFont);
543 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
544 SendMessageW(Globals.hEdit, EM_LIMITTEXT, 0, 0);
545 break;
548 case WM_COMMAND:
549 NOTEPAD_MenuCommand(LOWORD(wParam));
550 break;
552 case WM_DESTROYCLIPBOARD:
553 /*MessageBoxW(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
554 break;
556 case WM_CLOSE:
557 if (DoCloseFile()) {
558 DestroyWindow(hWnd);
560 break;
562 case WM_QUERYENDSESSION:
563 if (DoCloseFile()) {
564 return 1;
566 break;
568 case WM_DESTROY:
569 NOTEPAD_SaveSettingToRegistry();
571 PostQuitMessage(0);
572 break;
574 case WM_SIZE:
575 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
576 SWP_NOOWNERZORDER | SWP_NOZORDER);
577 break;
579 case WM_SETFOCUS:
580 SetFocus(Globals.hEdit);
581 break;
583 case WM_DROPFILES:
585 WCHAR szFileName[MAX_PATH];
586 HANDLE hDrop = (HANDLE) wParam;
588 DragQueryFileW(hDrop, 0, szFileName, ARRAY_SIZE(szFileName));
589 DragFinish(hDrop);
590 DoOpenFile(szFileName, ENCODING_AUTO);
591 break;
594 case WM_INITMENUPOPUP:
595 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
596 break;
598 default:
599 return DefWindowProcW(hWnd, msg, wParam, lParam);
601 return 0;
604 static int AlertFileDoesNotExist(LPCWSTR szFileName)
606 int nResult;
607 WCHAR szMessage[MAX_STRING_LEN];
608 WCHAR szResource[MAX_STRING_LEN];
610 LoadStringW(Globals.hInstance, STRING_DOESNOTEXIST, szResource, ARRAY_SIZE(szResource));
611 wsprintfW(szMessage, szResource, szFileName);
613 LoadStringW(Globals.hInstance, STRING_ERROR, szResource, ARRAY_SIZE(szResource));
615 nResult = MessageBoxW(Globals.hMainWnd, szMessage, szResource,
616 MB_ICONEXCLAMATION | MB_YESNOCANCEL);
618 return(nResult);
621 static void HandleCommandLine(LPWSTR cmdline)
623 WCHAR delimiter, *ptr;
624 BOOL opt_print = FALSE;
626 /* skip white space */
627 while (*cmdline == ' ') cmdline++;
629 /* skip executable name */
630 delimiter = (*cmdline == '"' ? '"' : ' ');
632 if (*cmdline == delimiter) cmdline++;
634 while (*cmdline && *cmdline != delimiter) cmdline++;
636 if (*cmdline == delimiter) cmdline++;
638 while (*cmdline == ' ') cmdline++;
640 ptr = cmdline;
641 while (*ptr == ' ' || *ptr == '-' || *ptr == '/')
643 WCHAR option;
645 if (*ptr++ == ' ') continue;
647 option = *ptr;
648 if (option) ptr++;
649 while (*ptr == ' ') ptr++;
651 switch(option)
653 case 'p':
654 case 'P':
656 if (!opt_print)
658 opt_print = TRUE;
659 cmdline = ptr;
661 break;
666 if (*cmdline)
668 /* file name is passed in the command line */
669 LPCWSTR file_name;
670 BOOL file_exists;
671 WCHAR buf[MAX_PATH];
673 if (cmdline[0] == '"')
675 WCHAR* wc;
676 cmdline++;
677 wc=cmdline;
678 /* Note: Double-quotes are not allowed in Windows filenames */
679 while (*wc && *wc != '"') wc++;
680 /* On Windows notepad ignores further arguments too */
681 *wc = 0;
684 if (FileExists(cmdline))
686 file_exists = TRUE;
687 file_name = cmdline;
689 else
691 static const WCHAR txtW[] = { '.','t','x','t',0 };
693 /* try to find file with ".txt" extension */
694 if (wcschr(PathFindFileNameW(cmdline), '.'))
696 file_exists = FALSE;
697 file_name = cmdline;
699 else
701 lstrcpynW(buf, cmdline, MAX_PATH - lstrlenW(txtW) - 1);
702 lstrcatW(buf, txtW);
703 file_name = buf;
704 file_exists = FileExists(buf);
708 if (file_exists)
710 DoOpenFile(file_name, ENCODING_AUTO);
711 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
712 if (opt_print)
713 DIALOG_FilePrint();
715 else
717 switch (AlertFileDoesNotExist(file_name)) {
718 case IDYES:
721 HANDLE file;
722 SetFileNameAndEncoding(file_name, ENCODING_ANSI);
724 file = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_WRITE,
725 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
726 if (file != INVALID_HANDLE_VALUE) CloseHandle(file);
728 UpdateWindowCaption();
729 break;
732 case IDNO:
733 break;
735 case IDCANCEL:
736 DestroyWindow(Globals.hMainWnd);
737 break;
743 /***********************************************************************
745 * WinMain
747 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
749 MSG msg;
750 HACCEL hAccel;
751 WNDCLASSEXW class;
752 HMONITOR monitor;
753 MONITORINFO info;
754 INT x, y;
755 static const WCHAR className[] = {'N','o','t','e','p','a','d',0};
756 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
758 InitCommonControls();
760 aFINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
762 ZeroMemory(&Globals, sizeof(Globals));
763 Globals.hInstance = hInstance;
764 NOTEPAD_LoadSettingFromRegistry();
766 ZeroMemory(&class, sizeof(class));
767 class.cbSize = sizeof(class);
768 class.lpfnWndProc = NOTEPAD_WndProc;
769 class.hInstance = Globals.hInstance;
770 class.hIcon = LoadIconW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD));
771 class.hIconSm = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD), IMAGE_ICON,
772 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
773 LR_SHARED);
774 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
775 class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
776 class.lpszMenuName = MAKEINTRESOURCEW(MAIN_MENU);
777 class.lpszClassName = className;
779 if (!RegisterClassExW(&class)) return FALSE;
781 /* Setup windows */
783 monitor = MonitorFromRect( &main_rect, MONITOR_DEFAULTTOPRIMARY );
784 info.cbSize = sizeof(info);
785 GetMonitorInfoW( monitor, &info );
787 x = main_rect.left;
788 y = main_rect.top;
789 if (main_rect.left >= info.rcWork.right ||
790 main_rect.top >= info.rcWork.bottom ||
791 main_rect.right < info.rcWork.left ||
792 main_rect.bottom < info.rcWork.top)
793 x = y = CW_USEDEFAULT;
795 Globals.hMainWnd =
796 CreateWindowW(className, winName, WS_OVERLAPPEDWINDOW, x, y,
797 main_rect.right - main_rect.left, main_rect.bottom - main_rect.top,
798 NULL, NULL, Globals.hInstance, NULL);
799 if (!Globals.hMainWnd)
801 ShowLastError();
802 ExitProcess(1);
805 NOTEPAD_InitData();
806 DIALOG_FileNew();
808 ShowWindow(Globals.hMainWnd, show);
809 UpdateWindow(Globals.hMainWnd);
810 DragAcceptFiles(Globals.hMainWnd, TRUE);
812 HandleCommandLine(GetCommandLineW());
814 hAccel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(ID_ACCEL));
816 while (GetMessageW(&msg, 0, 0, 0))
818 if (!IsDialogMessageW(Globals.hFindReplaceDlg, &msg) && !TranslateAcceleratorW(Globals.hMainWnd, hAccel, &msg))
820 TranslateMessage(&msg);
821 DispatchMessageW(&msg);
824 return msg.wParam;