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