notepad: Remove the License and NO WARRANTY options from the Help->Information popup...
[wine] / programs / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  *
23  */
24
25 #define UNICODE
26
27 #include <windows.h>
28 #include <shlwapi.h>
29 #include <stdio.h>
30
31 #include "main.h"
32 #include "dialog.h"
33 #include "notepad_res.h"
34
35 NOTEPAD_GLOBALS Globals;
36 static ATOM aFINDMSGSTRING;
37 static RECT main_rect;
38
39 static const WCHAR notepad_reg_key[] = {'S','o','f','t','w','a','r','e','\\',
40                                         'M','i','c','r','o','s','o','f','t','\\','N','o','t','e','p','a','d','\0'};
41 static const WCHAR value_fWrap[]            = {'f','W','r','a','p','\0'};
42 static const WCHAR value_iPointSize[]       = {'i','P','o','i','n','t','S','i','z','e','\0'};
43 static const WCHAR value_iWindowPosDX[]     = {'i','W','i','n','d','o','w','P','o','s','D','X','\0'};
44 static const WCHAR value_iWindowPosDY[]     = {'i','W','i','n','d','o','w','P','o','s','D','Y','\0'};
45 static const WCHAR value_iWindowPosX[]      = {'i','W','i','n','d','o','w','P','o','s','X','\0'};
46 static const WCHAR value_iWindowPosY[]      = {'i','W','i','n','d','o','w','P','o','s','Y','\0'};
47 static const WCHAR value_lfCharSet[]        = {'l','f','C','h','a','r','S','e','t','\0'};
48 static const WCHAR value_lfClipPrecision[]  = {'l','f','C','l','i','p','P','r','e','c','i','s','i','o','n','\0'};
49 static const WCHAR value_lfEscapement[]     = {'l','f','E','s','c','a','p','e','m','e','n','t','\0'};
50 static const WCHAR value_lfItalic[]         = {'l','f','I','t','a','l','i','c','\0'};
51 static const WCHAR value_lfOrientation[]    = {'l','f','O','r','i','e','n','t','a','t','i','o','n','\0'};
52 static const WCHAR value_lfOutPrecision[]   = {'l','f','O','u','t','P','r','e','c','i','s','i','o','n','\0'};
53 static const WCHAR value_lfPitchAndFamily[] = {'l','f','P','i','t','c','h','A','n','d','F','a','m','i','l','y','\0'};
54 static const WCHAR value_lfQuality[]        = {'l','f','Q','u','a','l','i','t','y','\0'};
55 static const WCHAR value_lfStrikeOut[]      = {'l','f','S','t','r','i','k','e','O','u','t','\0'};
56 static const WCHAR value_lfUnderline[]      = {'l','f','U','n','d','e','r','l','i','n','e','\0'};
57 static const WCHAR value_lfWeight[]         = {'l','f','W','e','i','g','h','t','\0'};
58 static const WCHAR value_lfFaceName[]       = {'l','f','F','a','c','e','N','a','m','e','\0'};
59 static const WCHAR value_iMarginTop[]       = {'i','M','a','r','g','i','n','T','o','p','\0'};
60 static const WCHAR value_iMarginBottom[]    = {'i','M','a','r','g','i','n','B','o','t','t','o','m','\0'};
61 static const WCHAR value_iMarginLeft[]      = {'i','M','a','r','g','i','n','L','e','f','t','\0'};
62 static const WCHAR value_iMarginRight[]     = {'i','M','a','r','g','i','n','R','i','g','h','t','\0'};
63 static const WCHAR value_szHeader[]         = {'s','z','H','e','a','d','e','r','\0'};
64 static const WCHAR value_szFooter[]         = {'s','z','T','r','a','i','l','e','r','\0'};
65
66 /***********************************************************************
67  *
68  *           SetFileName
69  *
70  *  Sets Global File Name.
71  */
72 VOID SetFileName(LPCWSTR szFileName)
73 {
74     lstrcpy(Globals.szFileName, szFileName);
75     Globals.szFileTitle[0] = 0;
76     GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
77 }
78
79 /******************************************************************************
80  *      get_dpi
81  *
82  * Get the dpi from registry HKCC\Software\Fonts\LogPixels.
83  */
84 DWORD get_dpi(void)
85 {
86     static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
87     static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
88     DWORD dpi = 96;
89     HKEY hkey;
90
91     if (RegOpenKey(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
92     {
93         DWORD type, size, new_dpi;
94
95         size = sizeof(new_dpi);
96         if(RegQueryValueEx(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
97         {
98             if(type == REG_DWORD && new_dpi != 0)
99                 dpi = new_dpi;
100         }
101         RegCloseKey(hkey);
102     }
103     return dpi;
104 }
105
106 /***********************************************************************
107  *
108  *           NOTEPAD_SaveSettingToRegistry
109  *
110  *  Save settring to registry HKCU\Software\Microsoft\Notepad.
111  */
112 static VOID NOTEPAD_SaveSettingToRegistry(void)
113 {
114     HKEY hkey;
115     DWORD disp;
116
117     if(RegCreateKeyEx(HKEY_CURRENT_USER, notepad_reg_key, 0, NULL,
118                 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disp) == ERROR_SUCCESS)
119     {
120         DWORD data;
121
122         GetWindowRect(Globals.hMainWnd, &main_rect);
123
124 #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)
125         SET_NOTEPAD_REG(hkey, value_fWrap,            Globals.bWrapLongLines);
126         SET_NOTEPAD_REG(hkey, value_iWindowPosX,      main_rect.left);
127         SET_NOTEPAD_REG(hkey, value_iWindowPosY,      main_rect.top);
128         SET_NOTEPAD_REG(hkey, value_iWindowPosDX,     main_rect.right - main_rect.left);
129         SET_NOTEPAD_REG(hkey, value_iWindowPosDY,     main_rect.bottom - main_rect.top);
130         SET_NOTEPAD_REG(hkey, value_lfCharSet,        Globals.lfFont.lfCharSet);
131         SET_NOTEPAD_REG(hkey, value_lfClipPrecision,  Globals.lfFont.lfClipPrecision);
132         SET_NOTEPAD_REG(hkey, value_lfEscapement,     Globals.lfFont.lfEscapement);
133         SET_NOTEPAD_REG(hkey, value_lfItalic,         Globals.lfFont.lfItalic);
134         SET_NOTEPAD_REG(hkey, value_lfOrientation,    Globals.lfFont.lfOrientation);
135         SET_NOTEPAD_REG(hkey, value_lfOutPrecision,   Globals.lfFont.lfOutPrecision);
136         SET_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
137         SET_NOTEPAD_REG(hkey, value_lfQuality,        Globals.lfFont.lfQuality);
138         SET_NOTEPAD_REG(hkey, value_lfStrikeOut,      Globals.lfFont.lfStrikeOut);
139         SET_NOTEPAD_REG(hkey, value_lfUnderline,      Globals.lfFont.lfUnderline);
140         SET_NOTEPAD_REG(hkey, value_lfWeight,         Globals.lfFont.lfWeight);
141         SET_NOTEPAD_REG(hkey, value_iMarginTop,       Globals.iMarginTop);
142         SET_NOTEPAD_REG(hkey, value_iMarginBottom,    Globals.iMarginBottom);
143         SET_NOTEPAD_REG(hkey, value_iMarginLeft,      Globals.iMarginLeft);
144         SET_NOTEPAD_REG(hkey, value_iMarginRight,     Globals.iMarginRight);
145 #undef SET_NOTEPAD_REG
146
147         /* Store the current value as 10 * twips */
148         data = MulDiv(abs(Globals.lfFont.lfHeight), 720 , get_dpi());
149         RegSetValueEx(hkey, value_iPointSize, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
150
151         RegSetValueEx(hkey, value_lfFaceName, 0, REG_SZ, (LPBYTE)&Globals.lfFont.lfFaceName,
152                       lstrlen(Globals.lfFont.lfFaceName) * sizeof(Globals.lfFont.lfFaceName[0]));
153
154         RegSetValueEx(hkey, value_szHeader, 0, REG_SZ, (LPBYTE)&Globals.szHeader,
155                       lstrlen(Globals.szHeader) * sizeof(Globals.szHeader[0]));
156
157         RegSetValueEx(hkey, value_szFooter, 0, REG_SZ, (LPBYTE)&Globals.szFooter,
158                       lstrlen(Globals.szFooter) * sizeof(Globals.szFooter[0]));
159
160         RegCloseKey(hkey);
161     }
162 }
163
164 /***********************************************************************
165  *
166  *           NOTEPAD_LoadSettingFromRegistry
167  *
168  *  Load setting from registry HKCU\Software\Microsoft\Notepad.
169  */
170 static VOID NOTEPAD_LoadSettingFromRegistry(void)
171 {
172     static const WCHAR systemW[] = { 'S','y','s','t','e','m','\0' };
173     HKEY hkey;
174     INT base_length, dx, dy;
175
176     base_length = (GetSystemMetrics(SM_CXSCREEN) > GetSystemMetrics(SM_CYSCREEN))?
177         GetSystemMetrics(SM_CYSCREEN) : GetSystemMetrics(SM_CXSCREEN);
178
179     dx = base_length * .95;
180     dy = dx * 3 / 4;
181     SetRect( &main_rect, 0, 0, dx, dy );
182
183     Globals.bWrapLongLines  = TRUE;
184     Globals.iMarginTop = 2500;
185     Globals.iMarginBottom = 2500;
186     Globals.iMarginLeft = 2000;
187     Globals.iMarginRight = 2000;
188     
189     Globals.lfFont.lfHeight         = -12;
190     Globals.lfFont.lfWidth          = 0;
191     Globals.lfFont.lfEscapement     = 0;
192     Globals.lfFont.lfOrientation    = 0;
193     Globals.lfFont.lfWeight         = FW_REGULAR;
194     Globals.lfFont.lfItalic         = FALSE;
195     Globals.lfFont.lfUnderline      = FALSE;
196     Globals.lfFont.lfStrikeOut      = FALSE;
197     Globals.lfFont.lfCharSet        = DEFAULT_CHARSET;
198     Globals.lfFont.lfOutPrecision   = OUT_DEFAULT_PRECIS;
199     Globals.lfFont.lfClipPrecision  = CLIP_DEFAULT_PRECIS;
200     Globals.lfFont.lfQuality        = DEFAULT_QUALITY;
201     Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
202     lstrcpy(Globals.lfFont.lfFaceName, systemW);
203
204     LoadString(Globals.hInstance, STRING_PAGESETUP_HEADERVALUE, Globals.szHeader,
205                sizeof(Globals.szHeader) / sizeof(Globals.szHeader[0]));
206     LoadString(Globals.hInstance, STRING_PAGESETUP_FOOTERVALUE, Globals.szFooter,
207                sizeof(Globals.szFooter) / sizeof(Globals.szFooter[0]));
208
209     if(RegOpenKey(HKEY_CURRENT_USER, notepad_reg_key, &hkey) == ERROR_SUCCESS)
210     {
211         WORD  data_helper[MAX_PATH];
212         DWORD type, data, size;
213
214 #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)
215         QUERY_NOTEPAD_REG(hkey, value_fWrap,            Globals.bWrapLongLines);
216         QUERY_NOTEPAD_REG(hkey, value_iWindowPosX,      main_rect.left);
217         QUERY_NOTEPAD_REG(hkey, value_iWindowPosY,      main_rect.top);
218         QUERY_NOTEPAD_REG(hkey, value_iWindowPosDX,     dx);
219         QUERY_NOTEPAD_REG(hkey, value_iWindowPosDY,     dy);
220         QUERY_NOTEPAD_REG(hkey, value_lfCharSet,        Globals.lfFont.lfCharSet);
221         QUERY_NOTEPAD_REG(hkey, value_lfClipPrecision,  Globals.lfFont.lfClipPrecision);
222         QUERY_NOTEPAD_REG(hkey, value_lfEscapement,     Globals.lfFont.lfEscapement);
223         QUERY_NOTEPAD_REG(hkey, value_lfItalic,         Globals.lfFont.lfItalic);
224         QUERY_NOTEPAD_REG(hkey, value_lfOrientation,    Globals.lfFont.lfOrientation);
225         QUERY_NOTEPAD_REG(hkey, value_lfOutPrecision,   Globals.lfFont.lfOutPrecision);
226         QUERY_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
227         QUERY_NOTEPAD_REG(hkey, value_lfQuality,        Globals.lfFont.lfQuality);
228         QUERY_NOTEPAD_REG(hkey, value_lfStrikeOut,      Globals.lfFont.lfStrikeOut);
229         QUERY_NOTEPAD_REG(hkey, value_lfUnderline,      Globals.lfFont.lfUnderline);
230         QUERY_NOTEPAD_REG(hkey, value_lfWeight,         Globals.lfFont.lfWeight);
231         QUERY_NOTEPAD_REG(hkey, value_iMarginTop,       Globals.iMarginTop);
232         QUERY_NOTEPAD_REG(hkey, value_iMarginBottom,    Globals.iMarginBottom);
233         QUERY_NOTEPAD_REG(hkey, value_iMarginLeft,      Globals.iMarginLeft);
234         QUERY_NOTEPAD_REG(hkey, value_iMarginRight,     Globals.iMarginRight);
235 #undef QUERY_NOTEPAD_REG
236
237         main_rect.right = main_rect.left + dx;
238         main_rect.bottom = main_rect.top + dy;
239
240         size = sizeof(DWORD);
241         if(RegQueryValueEx(hkey, value_iPointSize, 0, &type, (LPBYTE)&data, &size) == ERROR_SUCCESS)
242             if(type == REG_DWORD)
243                 /* The value is stored as 10 * twips */
244                 Globals.lfFont.lfHeight = -MulDiv(abs(data), get_dpi(), 720);
245
246         size = sizeof(Globals.lfFont.lfFaceName);
247         if(RegQueryValueEx(hkey, value_lfFaceName, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
248             if(type == REG_SZ)
249                 lstrcpy(Globals.lfFont.lfFaceName, data_helper);
250         
251         size = sizeof(Globals.szHeader);
252         if(RegQueryValueEx(hkey, value_szHeader, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
253             if(type == REG_SZ)
254                 lstrcpy(Globals.szHeader, data_helper);
255
256         size = sizeof(Globals.szFooter);
257         if(RegQueryValueEx(hkey, value_szFooter, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
258             if(type == REG_SZ)
259                 lstrcpy(Globals.szFooter, data_helper);
260         RegCloseKey(hkey);
261     }
262 }
263
264 /***********************************************************************
265  *
266  *           NOTEPAD_MenuCommand
267  *
268  *  All handling of main menu events
269  */
270 static int NOTEPAD_MenuCommand(WPARAM wParam)
271 {
272     switch (wParam)
273     {
274     case CMD_NEW:               DIALOG_FileNew(); break;
275     case CMD_OPEN:              DIALOG_FileOpen(); break;
276     case CMD_SAVE:              DIALOG_FileSave(); break;
277     case CMD_SAVE_AS:           DIALOG_FileSaveAs(); break;
278     case CMD_PRINT:             DIALOG_FilePrint(); break;
279     case CMD_PAGE_SETUP:        DIALOG_FilePageSetup(); break;
280     case CMD_PRINTER_SETUP:     DIALOG_FilePrinterSetup();break;
281     case CMD_EXIT:              DIALOG_FileExit(); break;
282
283     case CMD_UNDO:             DIALOG_EditUndo(); break;
284     case CMD_CUT:              DIALOG_EditCut(); break;
285     case CMD_COPY:             DIALOG_EditCopy(); break;
286     case CMD_PASTE:            DIALOG_EditPaste(); break;
287     case CMD_DELETE:           DIALOG_EditDelete(); break;
288     case CMD_SELECT_ALL:       DIALOG_EditSelectAll(); break;
289     case CMD_TIME_DATE:        DIALOG_EditTimeDate();break;
290
291     case CMD_SEARCH:           DIALOG_Search(); break;
292     case CMD_SEARCH_NEXT:      DIALOG_SearchNext(); break;
293                                
294     case CMD_WRAP:             DIALOG_EditWrap(); break;
295     case CMD_FONT:             DIALOG_SelectFont(); break;
296
297     case CMD_HELP_CONTENTS:    DIALOG_HelpContents(); break;
298     case CMD_HELP_SEARCH:      DIALOG_HelpSearch(); break;
299     case CMD_HELP_ON_HELP:     DIALOG_HelpHelp(); break;
300     case CMD_HELP_ABOUT_NOTEPAD: DIALOG_HelpAboutNotepad(); break;
301
302     default:
303         break;
304     }
305    return 0;
306 }
307
308 /***********************************************************************
309  * Data Initialization
310  */
311 static VOID NOTEPAD_InitData(VOID)
312 {
313     LPWSTR p = Globals.szFilter;
314     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
315     static const WCHAR all_files[] = { '*','.','*',0 };
316
317     LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
318     p += lstrlen(p) + 1;
319     lstrcpy(p, txt_files);
320     p += lstrlen(p) + 1;
321     LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
322     p += lstrlen(p) + 1;
323     lstrcpy(p, all_files);
324     p += lstrlen(p) + 1;
325     *p = '\0';
326     Globals.hDevMode = NULL;
327     Globals.hDevNames = NULL;
328
329     CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
330             MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
331 }
332
333 /***********************************************************************
334  * Enable/disable items on the menu based on control state
335  */
336 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
337 {
338     int enable;
339
340     EnableMenuItem(menu, CMD_UNDO,
341         SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
342     EnableMenuItem(menu, CMD_PASTE,
343         IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
344     enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
345     enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
346     EnableMenuItem(menu, CMD_CUT, enable);
347     EnableMenuItem(menu, CMD_COPY, enable);
348     EnableMenuItem(menu, CMD_DELETE, enable);
349     
350     EnableMenuItem(menu, CMD_SELECT_ALL,
351         GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
352 }
353
354 static LPTSTR NOTEPAD_StrRStr(LPTSTR pszSource, LPTSTR pszLast, LPTSTR pszSrch)
355 {
356     int len = lstrlen(pszSrch);
357     pszLast--;
358     while (pszLast >= pszSource)
359     {
360         if (StrCmpN(pszLast, pszSrch, len) == 0)
361             return pszLast;
362         pszLast--;
363     }
364     return NULL;
365 }
366
367 /***********************************************************************
368  * The user activated the Find dialog
369  */
370 void NOTEPAD_DoFind(FINDREPLACE *fr)
371 {
372     LPTSTR content;
373     LPTSTR found;
374     int len = lstrlen(fr->lpstrFindWhat);
375     int fileLen;
376     DWORD pos;
377     
378     fileLen = GetWindowTextLength(Globals.hEdit) + 1;
379     content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(TCHAR));
380     if (!content) return;
381     GetWindowText(Globals.hEdit, content, fileLen);
382
383     SendMessage(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);        
384     switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
385     {
386         case 0:
387             found = StrRStrI(content, content+pos-len, fr->lpstrFindWhat);
388             break;
389         case FR_DOWN:
390             found = StrStrI(content+pos, fr->lpstrFindWhat);
391             break;
392         case FR_MATCHCASE:
393             found = NOTEPAD_StrRStr(content, content+pos-len, fr->lpstrFindWhat);
394             break;
395         case FR_DOWN|FR_MATCHCASE:
396             found = StrStr(content+pos, fr->lpstrFindWhat);
397             break;
398         default:    /* shouldn't happen */
399             return;
400     }
401     HeapFree(GetProcessHeap(), 0, content);
402
403     if (found == NULL)
404     {
405         DIALOG_StringMsgBox(Globals.hFindReplaceDlg, STRING_NOTFOUND, fr->lpstrFindWhat,
406             MB_ICONINFORMATION|MB_OK);
407         return;
408     }
409
410     SendMessage(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
411 }
412
413 /***********************************************************************
414  *
415  *           NOTEPAD_WndProc
416  */
417 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
418                                LPARAM lParam)
419 {
420     if (msg == aFINDMSGSTRING)      /* not a constant so can't be used in switch */
421     {
422         FINDREPLACE *fr = (FINDREPLACE *)lParam;
423         
424         if (fr->Flags & FR_DIALOGTERM)
425             Globals.hFindReplaceDlg = NULL;
426         if (fr->Flags & FR_FINDNEXT)
427         {
428             Globals.lastFind = *fr;
429             NOTEPAD_DoFind(fr);
430         }
431         return 0;
432     }
433     
434     switch (msg) {
435
436     case WM_CREATE:
437     {
438         static const WCHAR editW[] = { 'e','d','i','t',0 };
439         DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
440                         ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
441         RECT rc;
442         GetClientRect(hWnd, &rc);
443
444         if (!Globals.bWrapLongLines) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
445
446         Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
447                              dwStyle,
448                              0, 0, rc.right, rc.bottom, hWnd,
449                              NULL, Globals.hInstance, NULL);
450
451         Globals.hFont = CreateFontIndirect(&Globals.lfFont);
452         SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
453         break;
454     }
455
456     case WM_COMMAND:
457         NOTEPAD_MenuCommand(LOWORD(wParam));
458         break;
459
460     case WM_DESTROYCLIPBOARD:
461         /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
462         break;
463
464     case WM_CLOSE:
465         if (DoCloseFile()) {
466             DestroyWindow(hWnd);
467         }
468         break;
469
470     case WM_QUERYENDSESSION:
471         if (DoCloseFile()) {
472             return 1;
473         }
474         break;
475
476     case WM_DESTROY:
477         NOTEPAD_SaveSettingToRegistry();
478
479         PostQuitMessage(0);
480         break;
481
482     case WM_SIZE:
483         SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
484                      SWP_NOOWNERZORDER | SWP_NOZORDER);
485         break;
486
487     case WM_SETFOCUS:
488         SetFocus(Globals.hEdit);
489         break;
490
491     case WM_DROPFILES:
492     {
493         WCHAR szFileName[MAX_PATH];
494         HANDLE hDrop = (HANDLE) wParam;
495
496         DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
497         DragFinish(hDrop);
498         DoOpenFile(szFileName);
499         break;
500     }
501     
502     case WM_INITMENUPOPUP:
503         NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
504         break;
505
506     default:
507         return DefWindowProc(hWnd, msg, wParam, lParam);
508     }
509     return 0;
510 }
511
512 static int AlertFileDoesNotExist(LPCWSTR szFileName)
513 {
514    int nResult;
515    WCHAR szMessage[MAX_STRING_LEN];
516    WCHAR szResource[MAX_STRING_LEN];
517
518    LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
519    wsprintf(szMessage, szResource, szFileName);
520
521    LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
522
523    nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
524                         MB_ICONEXCLAMATION | MB_YESNO);
525
526    return(nResult);
527 }
528
529 static void HandleCommandLine(LPWSTR cmdline)
530 {
531     WCHAR delimiter;
532     int opt_print=0;
533     
534     /* skip white space */
535     while (*cmdline == ' ') cmdline++;
536
537     /* skip executable name */
538     delimiter = (*cmdline == '"' ? '"' : ' ');
539
540     if (*cmdline == delimiter) cmdline++;
541
542     while (*cmdline && *cmdline != delimiter) cmdline++;
543
544     if (*cmdline == delimiter) cmdline++;
545
546     while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
547     {
548         WCHAR option;
549
550         if (*cmdline++ == ' ') continue;
551
552         option = *cmdline;
553         if (option) cmdline++;
554         while (*cmdline == ' ') cmdline++;
555
556         switch(option)
557         {
558             case 'p':
559             case 'P':
560                 opt_print=1;
561                 break;
562         }
563     }
564
565     if (*cmdline)
566     {
567         /* file name is passed in the command line */
568         LPCWSTR file_name;
569         BOOL file_exists;
570         WCHAR buf[MAX_PATH];
571
572         if (cmdline[0] == '"')
573         {
574             cmdline++;
575             cmdline[lstrlen(cmdline) - 1] = 0;
576         }
577
578         if (FileExists(cmdline))
579         {
580             file_exists = TRUE;
581             file_name = cmdline;
582         }
583         else
584         {
585             static const WCHAR txtW[] = { '.','t','x','t',0 };
586
587             /* try to find file with ".txt" extension */
588             if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
589             {
590                 file_exists = FALSE;
591                 file_name = cmdline;
592             }
593             else
594             {
595                 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
596                 lstrcat(buf, txtW);
597                 file_name = buf;
598                 file_exists = FileExists(buf);
599             }
600         }
601
602         if (file_exists)
603         {
604             DoOpenFile(file_name);
605             InvalidateRect(Globals.hMainWnd, NULL, FALSE);
606             if (opt_print)
607                 DIALOG_FilePrint();
608         }
609         else
610         {
611             switch (AlertFileDoesNotExist(file_name)) {
612             case IDYES:
613                 DoOpenFile(file_name);
614                 break;
615
616             case IDNO:
617                 break;
618             }
619         }
620      }
621 }
622
623 /***********************************************************************
624  *
625  *           WinMain
626  */
627 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
628 {
629     MSG        msg;
630     HACCEL      hAccel;
631     WNDCLASSEX class;
632     HMONITOR monitor;
633     MONITORINFO info;
634     INT x, y;
635     static const WCHAR className[] = {'N','o','t','e','p','a','d',0};
636     static const WCHAR winName[]   = {'N','o','t','e','p','a','d',0};
637
638     aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
639
640     ZeroMemory(&Globals, sizeof(Globals));
641     Globals.hInstance       = hInstance;
642     NOTEPAD_LoadSettingFromRegistry();
643
644     ZeroMemory(&class, sizeof(class));
645     class.cbSize        = sizeof(class);
646     class.lpfnWndProc   = NOTEPAD_WndProc;
647     class.hInstance     = Globals.hInstance;
648     class.hIcon         = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_NOTEPAD));
649     class.hCursor       = LoadCursor(0, IDC_ARROW);
650     class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
651     class.lpszMenuName  = MAKEINTRESOURCE(MAIN_MENU);
652     class.lpszClassName = className;
653
654     if (!RegisterClassEx(&class)) return FALSE;
655
656     /* Setup windows */
657
658     monitor = MonitorFromRect( &main_rect, MONITOR_DEFAULTTOPRIMARY );
659     info.cbSize = sizeof(info);
660     GetMonitorInfoW( monitor, &info );
661
662     x = main_rect.left;
663     y = main_rect.top;
664     if (main_rect.left >= info.rcWork.right ||
665         main_rect.top >= info.rcWork.bottom ||
666         main_rect.right < info.rcWork.left ||
667         main_rect.bottom < info.rcWork.top)
668         x = y = CW_USEDEFAULT;
669
670     Globals.hMainWnd =
671         CreateWindow(className, winName, WS_OVERLAPPEDWINDOW, x, y,
672                      main_rect.right - main_rect.left, main_rect.bottom - main_rect.top,
673                      NULL, NULL, Globals.hInstance, NULL);
674     if (!Globals.hMainWnd)
675     {
676         ShowLastError();
677         ExitProcess(1);
678     }
679
680     NOTEPAD_InitData();
681     DIALOG_FileNew();
682
683     ShowWindow(Globals.hMainWnd, show);
684     UpdateWindow(Globals.hMainWnd);
685     DragAcceptFiles(Globals.hMainWnd, TRUE);
686
687     HandleCommandLine(GetCommandLine());
688
689     hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
690
691     while (GetMessage(&msg, 0, 0, 0))
692     {
693         if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
694         {
695             TranslateMessage(&msg);
696             DispatchMessage(&msg);
697         }
698     }
699     return msg.wParam;
700 }