push 20d539bd3127e997ce79233e3988ba5740356ce5
[wine/hacks.git] / programs / notepad / main.c
blob33b48595e1aa34530aac39e2c7b109f607750d95
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 #define UNICODE
27 #include <windows.h>
28 #include <shlwapi.h>
29 #include <stdio.h>
31 #include "main.h"
32 #include "dialog.h"
33 #include "notepad_res.h"
34 #include "wine/unicode.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 * SetFileName
71 * Sets Global File Name.
73 VOID SetFileName(LPCWSTR szFileName)
75 lstrcpy(Globals.szFileName, szFileName);
76 Globals.szFileTitle[0] = 0;
77 GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
80 /******************************************************************************
81 * get_dpi
83 * Get the dpi from registry HKCC\Software\Fonts\LogPixels.
85 DWORD get_dpi(void)
87 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
88 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
89 DWORD dpi = 96;
90 HKEY hkey;
92 if (RegOpenKey(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
94 DWORD type, size, new_dpi;
96 size = sizeof(new_dpi);
97 if(RegQueryValueEx(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
99 if(type == REG_DWORD && new_dpi != 0)
100 dpi = new_dpi;
102 RegCloseKey(hkey);
104 return dpi;
107 /***********************************************************************
109 * NOTEPAD_SaveSettingToRegistry
111 * Save setting to registry HKCU\Software\Microsoft\Notepad.
113 static VOID NOTEPAD_SaveSettingToRegistry(void)
115 HKEY hkey;
116 DWORD disp;
118 if(RegCreateKeyEx(HKEY_CURRENT_USER, notepad_reg_key, 0, NULL,
119 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disp) == ERROR_SUCCESS)
121 DWORD data;
122 WINDOWPLACEMENT wndpl;
124 wndpl.length = sizeof(WINDOWPLACEMENT);
125 GetWindowPlacement(Globals.hMainWnd, &wndpl);
126 main_rect = wndpl.rcNormalPosition;
128 #define SET_NOTEPAD_REG(hkey, value_name, value_data) do { DWORD data = (DWORD)(value_data); RegSetValueEx(hkey, value_name, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); }while(0)
129 SET_NOTEPAD_REG(hkey, value_fWrap, Globals.bWrapLongLines);
130 SET_NOTEPAD_REG(hkey, value_iWindowPosX, main_rect.left);
131 SET_NOTEPAD_REG(hkey, value_iWindowPosY, main_rect.top);
132 SET_NOTEPAD_REG(hkey, value_iWindowPosDX, main_rect.right - main_rect.left);
133 SET_NOTEPAD_REG(hkey, value_iWindowPosDY, main_rect.bottom - main_rect.top);
134 SET_NOTEPAD_REG(hkey, value_lfCharSet, Globals.lfFont.lfCharSet);
135 SET_NOTEPAD_REG(hkey, value_lfClipPrecision, Globals.lfFont.lfClipPrecision);
136 SET_NOTEPAD_REG(hkey, value_lfEscapement, Globals.lfFont.lfEscapement);
137 SET_NOTEPAD_REG(hkey, value_lfItalic, Globals.lfFont.lfItalic);
138 SET_NOTEPAD_REG(hkey, value_lfOrientation, Globals.lfFont.lfOrientation);
139 SET_NOTEPAD_REG(hkey, value_lfOutPrecision, Globals.lfFont.lfOutPrecision);
140 SET_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
141 SET_NOTEPAD_REG(hkey, value_lfQuality, Globals.lfFont.lfQuality);
142 SET_NOTEPAD_REG(hkey, value_lfStrikeOut, Globals.lfFont.lfStrikeOut);
143 SET_NOTEPAD_REG(hkey, value_lfUnderline, Globals.lfFont.lfUnderline);
144 SET_NOTEPAD_REG(hkey, value_lfWeight, Globals.lfFont.lfWeight);
145 SET_NOTEPAD_REG(hkey, value_iMarginTop, Globals.iMarginTop);
146 SET_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom);
147 SET_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft);
148 SET_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight);
149 #undef SET_NOTEPAD_REG
151 /* Store the current value as 10 * twips */
152 data = MulDiv(abs(Globals.lfFont.lfHeight), 720 , get_dpi());
153 RegSetValueEx(hkey, value_iPointSize, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
155 RegSetValueEx(hkey, value_lfFaceName, 0, REG_SZ, (LPBYTE)&Globals.lfFont.lfFaceName,
156 lstrlen(Globals.lfFont.lfFaceName) * sizeof(Globals.lfFont.lfFaceName[0]));
158 RegSetValueEx(hkey, value_szHeader, 0, REG_SZ, (LPBYTE)&Globals.szHeader,
159 lstrlen(Globals.szHeader) * sizeof(Globals.szHeader[0]));
161 RegSetValueEx(hkey, value_szFooter, 0, REG_SZ, (LPBYTE)&Globals.szFooter,
162 lstrlen(Globals.szFooter) * sizeof(Globals.szFooter[0]));
164 RegCloseKey(hkey);
168 /***********************************************************************
170 * NOTEPAD_LoadSettingFromRegistry
172 * Load setting from registry HKCU\Software\Microsoft\Notepad.
174 static VOID NOTEPAD_LoadSettingFromRegistry(void)
176 static const WCHAR systemW[] = { 'S','y','s','t','e','m','\0' };
177 HKEY hkey;
178 INT base_length, dx, dy;
180 base_length = (GetSystemMetrics(SM_CXSCREEN) > GetSystemMetrics(SM_CYSCREEN))?
181 GetSystemMetrics(SM_CYSCREEN) : GetSystemMetrics(SM_CXSCREEN);
183 dx = base_length * .95;
184 dy = dx * 3 / 4;
185 SetRect( &main_rect, 0, 0, dx, dy );
187 Globals.bWrapLongLines = TRUE;
188 Globals.iMarginTop = 2500;
189 Globals.iMarginBottom = 2500;
190 Globals.iMarginLeft = 2000;
191 Globals.iMarginRight = 2000;
193 Globals.lfFont.lfHeight = -12;
194 Globals.lfFont.lfWidth = 0;
195 Globals.lfFont.lfEscapement = 0;
196 Globals.lfFont.lfOrientation = 0;
197 Globals.lfFont.lfWeight = FW_REGULAR;
198 Globals.lfFont.lfItalic = FALSE;
199 Globals.lfFont.lfUnderline = FALSE;
200 Globals.lfFont.lfStrikeOut = FALSE;
201 Globals.lfFont.lfCharSet = DEFAULT_CHARSET;
202 Globals.lfFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
203 Globals.lfFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
204 Globals.lfFont.lfQuality = DEFAULT_QUALITY;
205 Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
206 lstrcpy(Globals.lfFont.lfFaceName, systemW);
208 LoadString(Globals.hInstance, STRING_PAGESETUP_HEADERVALUE, Globals.szHeader,
209 sizeof(Globals.szHeader) / sizeof(Globals.szHeader[0]));
210 LoadString(Globals.hInstance, STRING_PAGESETUP_FOOTERVALUE, Globals.szFooter,
211 sizeof(Globals.szFooter) / sizeof(Globals.szFooter[0]));
213 if(RegOpenKey(HKEY_CURRENT_USER, notepad_reg_key, &hkey) == ERROR_SUCCESS)
215 WORD data_helper[MAX_PATH];
216 DWORD type, data, size;
218 #define QUERY_NOTEPAD_REG(hkey, value_name, ret) do { DWORD type, data; DWORD size = sizeof(DWORD); if(RegQueryValueEx(hkey, value_name, 0, &type, (LPBYTE)&data, &size) == ERROR_SUCCESS) if(type == REG_DWORD) ret = (typeof(ret))data; } while(0)
219 QUERY_NOTEPAD_REG(hkey, value_fWrap, Globals.bWrapLongLines);
220 QUERY_NOTEPAD_REG(hkey, value_iWindowPosX, main_rect.left);
221 QUERY_NOTEPAD_REG(hkey, value_iWindowPosY, main_rect.top);
222 QUERY_NOTEPAD_REG(hkey, value_iWindowPosDX, dx);
223 QUERY_NOTEPAD_REG(hkey, value_iWindowPosDY, dy);
224 QUERY_NOTEPAD_REG(hkey, value_lfCharSet, Globals.lfFont.lfCharSet);
225 QUERY_NOTEPAD_REG(hkey, value_lfClipPrecision, Globals.lfFont.lfClipPrecision);
226 QUERY_NOTEPAD_REG(hkey, value_lfEscapement, Globals.lfFont.lfEscapement);
227 QUERY_NOTEPAD_REG(hkey, value_lfItalic, Globals.lfFont.lfItalic);
228 QUERY_NOTEPAD_REG(hkey, value_lfOrientation, Globals.lfFont.lfOrientation);
229 QUERY_NOTEPAD_REG(hkey, value_lfOutPrecision, Globals.lfFont.lfOutPrecision);
230 QUERY_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
231 QUERY_NOTEPAD_REG(hkey, value_lfQuality, Globals.lfFont.lfQuality);
232 QUERY_NOTEPAD_REG(hkey, value_lfStrikeOut, Globals.lfFont.lfStrikeOut);
233 QUERY_NOTEPAD_REG(hkey, value_lfUnderline, Globals.lfFont.lfUnderline);
234 QUERY_NOTEPAD_REG(hkey, value_lfWeight, Globals.lfFont.lfWeight);
235 QUERY_NOTEPAD_REG(hkey, value_iMarginTop, Globals.iMarginTop);
236 QUERY_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom);
237 QUERY_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft);
238 QUERY_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight);
239 #undef QUERY_NOTEPAD_REG
241 main_rect.right = main_rect.left + dx;
242 main_rect.bottom = main_rect.top + dy;
244 size = sizeof(DWORD);
245 if(RegQueryValueEx(hkey, value_iPointSize, 0, &type, (LPBYTE)&data, &size) == ERROR_SUCCESS)
246 if(type == REG_DWORD)
247 /* The value is stored as 10 * twips */
248 Globals.lfFont.lfHeight = -MulDiv(abs(data), get_dpi(), 720);
250 size = sizeof(Globals.lfFont.lfFaceName);
251 if(RegQueryValueEx(hkey, value_lfFaceName, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
252 if(type == REG_SZ)
253 lstrcpy(Globals.lfFont.lfFaceName, data_helper);
255 size = sizeof(Globals.szHeader);
256 if(RegQueryValueEx(hkey, value_szHeader, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
257 if(type == REG_SZ)
258 lstrcpy(Globals.szHeader, data_helper);
260 size = sizeof(Globals.szFooter);
261 if(RegQueryValueEx(hkey, value_szFooter, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
262 if(type == REG_SZ)
263 lstrcpy(Globals.szFooter, data_helper);
264 RegCloseKey(hkey);
268 /***********************************************************************
270 * NOTEPAD_MenuCommand
272 * All handling of main menu events
274 static int NOTEPAD_MenuCommand(WPARAM wParam)
276 switch (wParam)
278 case CMD_NEW: DIALOG_FileNew(); break;
279 case CMD_OPEN: DIALOG_FileOpen(); break;
280 case CMD_SAVE: DIALOG_FileSave(); break;
281 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
282 case CMD_PRINT: DIALOG_FilePrint(); break;
283 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
284 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
285 case CMD_EXIT: DIALOG_FileExit(); break;
287 case CMD_UNDO: DIALOG_EditUndo(); break;
288 case CMD_CUT: DIALOG_EditCut(); break;
289 case CMD_COPY: DIALOG_EditCopy(); break;
290 case CMD_PASTE: DIALOG_EditPaste(); break;
291 case CMD_DELETE: DIALOG_EditDelete(); break;
292 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
293 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
295 case CMD_SEARCH: DIALOG_Search(); break;
296 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
297 case CMD_REPLACE: DIALOG_Replace(); break;
299 case CMD_WRAP: DIALOG_EditWrap(); break;
300 case CMD_FONT: DIALOG_SelectFont(); break;
302 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
303 case CMD_HELP_SEARCH: DIALOG_HelpSearch(); break;
304 case CMD_HELP_ON_HELP: DIALOG_HelpHelp(); break;
305 case CMD_HELP_ABOUT_NOTEPAD: DIALOG_HelpAboutNotepad(); break;
307 default:
308 break;
310 return 0;
313 /***********************************************************************
314 * Data Initialization
316 static VOID NOTEPAD_InitData(VOID)
318 LPWSTR p = Globals.szFilter;
319 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
320 static const WCHAR all_files[] = { '*','.','*',0 };
322 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
323 p += lstrlen(p) + 1;
324 lstrcpy(p, txt_files);
325 p += lstrlen(p) + 1;
326 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
327 p += lstrlen(p) + 1;
328 lstrcpy(p, all_files);
329 p += lstrlen(p) + 1;
330 *p = '\0';
331 Globals.hDevMode = NULL;
332 Globals.hDevNames = NULL;
334 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
335 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
338 /***********************************************************************
339 * Enable/disable items on the menu based on control state
341 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
343 int enable;
345 EnableMenuItem(menu, CMD_UNDO,
346 SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
347 EnableMenuItem(menu, CMD_PASTE,
348 IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
349 enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
350 enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
351 EnableMenuItem(menu, CMD_CUT, enable);
352 EnableMenuItem(menu, CMD_COPY, enable);
353 EnableMenuItem(menu, CMD_DELETE, enable);
355 EnableMenuItem(menu, CMD_SELECT_ALL,
356 GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
359 static LPTSTR NOTEPAD_StrRStr(LPTSTR pszSource, LPTSTR pszLast, LPTSTR pszSrch)
361 int len = lstrlen(pszSrch);
362 pszLast--;
363 while (pszLast >= pszSource)
365 if (StrCmpN(pszLast, pszSrch, len) == 0)
366 return pszLast;
367 pszLast--;
369 return NULL;
372 /***********************************************************************
373 * The user activated the Find dialog
375 void NOTEPAD_DoFind(FINDREPLACE *fr)
377 LPTSTR content;
378 LPTSTR found;
379 int len = lstrlen(fr->lpstrFindWhat);
380 int fileLen;
381 DWORD pos;
383 fileLen = GetWindowTextLength(Globals.hEdit) + 1;
384 content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(TCHAR));
385 if (!content) return;
386 GetWindowText(Globals.hEdit, content, fileLen);
388 SendMessage(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
389 switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
391 case 0:
392 found = StrRStrI(content, content+pos-len, fr->lpstrFindWhat);
393 break;
394 case FR_DOWN:
395 found = StrStrI(content+pos, fr->lpstrFindWhat);
396 break;
397 case FR_MATCHCASE:
398 found = NOTEPAD_StrRStr(content, content+pos-len, fr->lpstrFindWhat);
399 break;
400 case FR_DOWN|FR_MATCHCASE:
401 found = StrStr(content+pos, fr->lpstrFindWhat);
402 break;
403 default: /* shouldn't happen */
404 return;
406 HeapFree(GetProcessHeap(), 0, content);
408 if (found == NULL)
410 DIALOG_StringMsgBox(Globals.hFindReplaceDlg, STRING_NOTFOUND, fr->lpstrFindWhat,
411 MB_ICONINFORMATION|MB_OK);
412 return;
415 SendMessage(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
418 void NOTEPAD_DoReplace(FINDREPLACE *fr)
420 LPTSTR content;
421 int len = lstrlen(fr->lpstrFindWhat);
422 int fileLen;
423 DWORD pos;
424 DWORD pos_start;
426 fileLen = GetWindowTextLength(Globals.hEdit) + 1;
427 content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(TCHAR));
428 if (!content) return;
429 GetWindowText(Globals.hEdit, content, fileLen);
431 SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM)&pos_start, (LPARAM)&pos);
432 switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
434 case FR_DOWN:
435 if ( pos-pos_start == len && StrCmpNI(fr->lpstrFindWhat, content+pos_start, len) == 0)
436 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
437 break;
438 case FR_DOWN|FR_MATCHCASE:
439 if ( pos-pos_start == len && StrCmpN(fr->lpstrFindWhat, content+pos_start, len) == 0)
440 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
441 break;
442 default: /* shouldn't happen */
443 return;
445 HeapFree(GetProcessHeap(), 0, content);
447 NOTEPAD_DoFind(fr);
450 void NOTEPAD_DoReplaceAll(FINDREPLACE *fr)
452 LPTSTR content;
453 LPTSTR found;
454 int len = lstrlen(fr->lpstrFindWhat);
455 int fileLen;
456 DWORD pos;
458 SendMessage(Globals.hEdit, EM_SETSEL, 0, 0);
459 while(TRUE){
460 fileLen = GetWindowTextLength(Globals.hEdit) + 1;
461 content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(TCHAR));
462 if (!content) return;
463 GetWindowText(Globals.hEdit, content, fileLen);
465 SendMessage(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
466 switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
468 case FR_DOWN:
469 found = StrStrI(content+pos, fr->lpstrFindWhat);
470 break;
471 case FR_DOWN|FR_MATCHCASE:
472 found = StrStr(content+pos, fr->lpstrFindWhat);
473 break;
474 default: /* shouldn't happen */
475 return;
477 HeapFree(GetProcessHeap(), 0, content);
479 if(found == NULL)
481 SendMessage(Globals.hEdit, EM_SETSEL, 0, 0);
482 return;
484 SendMessage(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
485 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
489 /***********************************************************************
491 * NOTEPAD_WndProc
493 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
494 LPARAM lParam)
496 if (msg == aFINDMSGSTRING) /* not a constant so can't be used in switch */
498 FINDREPLACE *fr = (FINDREPLACE *)lParam;
500 if (fr->Flags & FR_DIALOGTERM)
501 Globals.hFindReplaceDlg = NULL;
502 if (fr->Flags & FR_FINDNEXT)
504 Globals.lastFind = *fr;
505 NOTEPAD_DoFind(fr);
507 if (fr->Flags & FR_REPLACE)
509 Globals.lastFind = *fr;
510 NOTEPAD_DoReplace(fr);
512 if (fr->Flags & FR_REPLACEALL)
514 Globals.lastFind = *fr;
515 NOTEPAD_DoReplaceAll(fr);
517 return 0;
520 switch (msg) {
522 case WM_CREATE:
524 static const WCHAR editW[] = { 'e','d','i','t',0 };
525 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
526 ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
527 RECT rc;
528 GetClientRect(hWnd, &rc);
530 if (!Globals.bWrapLongLines) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
532 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
533 dwStyle,
534 0, 0, rc.right, rc.bottom, hWnd,
535 NULL, Globals.hInstance, NULL);
537 Globals.hFont = CreateFontIndirect(&Globals.lfFont);
538 SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
539 break;
542 case WM_COMMAND:
543 NOTEPAD_MenuCommand(LOWORD(wParam));
544 break;
546 case WM_DESTROYCLIPBOARD:
547 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
548 break;
550 case WM_CLOSE:
551 if (DoCloseFile()) {
552 DestroyWindow(hWnd);
554 break;
556 case WM_QUERYENDSESSION:
557 if (DoCloseFile()) {
558 return 1;
560 break;
562 case WM_DESTROY:
563 NOTEPAD_SaveSettingToRegistry();
565 PostQuitMessage(0);
566 break;
568 case WM_SIZE:
569 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
570 SWP_NOOWNERZORDER | SWP_NOZORDER);
571 break;
573 case WM_SETFOCUS:
574 SetFocus(Globals.hEdit);
575 break;
577 case WM_DROPFILES:
579 WCHAR szFileName[MAX_PATH];
580 HANDLE hDrop = (HANDLE) wParam;
582 DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
583 DragFinish(hDrop);
584 DoOpenFile(szFileName);
585 break;
588 case WM_INITMENUPOPUP:
589 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
590 break;
592 default:
593 return DefWindowProc(hWnd, msg, wParam, lParam);
595 return 0;
598 static int AlertFileDoesNotExist(LPCWSTR szFileName)
600 int nResult;
601 WCHAR szMessage[MAX_STRING_LEN];
602 WCHAR szResource[MAX_STRING_LEN];
604 LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
605 wsprintf(szMessage, szResource, szFileName);
607 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
609 nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
610 MB_ICONEXCLAMATION | MB_YESNO);
612 return(nResult);
615 static void HandleCommandLine(LPWSTR cmdline)
617 WCHAR delimiter;
618 int opt_print=0;
620 /* skip white space */
621 while (*cmdline == ' ') cmdline++;
623 /* skip executable name */
624 delimiter = (*cmdline == '"' ? '"' : ' ');
626 if (*cmdline == delimiter) cmdline++;
628 while (*cmdline && *cmdline != delimiter) cmdline++;
630 if (*cmdline == delimiter) cmdline++;
632 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
634 WCHAR option;
636 if (*cmdline++ == ' ') continue;
638 option = *cmdline;
639 if (option) cmdline++;
640 while (*cmdline == ' ') cmdline++;
642 switch(option)
644 case 'p':
645 case 'P':
646 opt_print=1;
647 break;
651 if (*cmdline)
653 /* file name is passed in the command line */
654 LPCWSTR file_name;
655 BOOL file_exists;
656 WCHAR buf[MAX_PATH];
658 if (cmdline[0] == '"')
660 WCHAR* wc;
661 cmdline++;
662 wc=cmdline;
663 /* Note: Double-quotes are not allowed in Windows filenames */
664 while (*wc && *wc != '"') wc++;
665 /* On Windows notepad ignores further arguments too */
666 *wc = 0;
669 if (FileExists(cmdline))
671 file_exists = TRUE;
672 file_name = cmdline;
674 else
676 static const WCHAR txtW[] = { '.','t','x','t',0 };
678 /* try to find file with ".txt" extension */
679 if (strchrW(PathFindFileNameW(cmdline), '.'))
681 file_exists = FALSE;
682 file_name = cmdline;
684 else
686 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
687 lstrcat(buf, txtW);
688 file_name = buf;
689 file_exists = FileExists(buf);
693 if (file_exists)
695 DoOpenFile(file_name);
696 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
697 if (opt_print)
698 DIALOG_FilePrint();
700 else
702 switch (AlertFileDoesNotExist(file_name)) {
703 case IDYES:
704 DoOpenFile(file_name);
705 break;
707 case IDNO:
708 break;
714 /***********************************************************************
716 * WinMain
718 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
720 MSG msg;
721 HACCEL hAccel;
722 WNDCLASSEX class;
723 HMONITOR monitor;
724 MONITORINFO info;
725 INT x, y;
726 static const WCHAR className[] = {'N','o','t','e','p','a','d',0};
727 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
729 aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
731 ZeroMemory(&Globals, sizeof(Globals));
732 Globals.hInstance = hInstance;
733 NOTEPAD_LoadSettingFromRegistry();
735 ZeroMemory(&class, sizeof(class));
736 class.cbSize = sizeof(class);
737 class.lpfnWndProc = NOTEPAD_WndProc;
738 class.hInstance = Globals.hInstance;
739 class.hIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_NOTEPAD));
740 class.hCursor = LoadCursor(0, IDC_ARROW);
741 class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
742 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
743 class.lpszClassName = className;
745 if (!RegisterClassEx(&class)) return FALSE;
747 /* Setup windows */
749 monitor = MonitorFromRect( &main_rect, MONITOR_DEFAULTTOPRIMARY );
750 info.cbSize = sizeof(info);
751 GetMonitorInfoW( monitor, &info );
753 x = main_rect.left;
754 y = main_rect.top;
755 if (main_rect.left >= info.rcWork.right ||
756 main_rect.top >= info.rcWork.bottom ||
757 main_rect.right < info.rcWork.left ||
758 main_rect.bottom < info.rcWork.top)
759 x = y = CW_USEDEFAULT;
761 Globals.hMainWnd =
762 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW, x, y,
763 main_rect.right - main_rect.left, main_rect.bottom - main_rect.top,
764 NULL, NULL, Globals.hInstance, NULL);
765 if (!Globals.hMainWnd)
767 ShowLastError();
768 ExitProcess(1);
771 NOTEPAD_InitData();
772 DIALOG_FileNew();
774 ShowWindow(Globals.hMainWnd, show);
775 UpdateWindow(Globals.hMainWnd);
776 DragAcceptFiles(Globals.hMainWnd, TRUE);
778 HandleCommandLine(GetCommandLine());
780 hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
782 while (GetMessage(&msg, 0, 0, 0))
784 if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
786 TranslateMessage(&msg);
787 DispatchMessage(&msg);
790 return msg.wParam;