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