wined3d: Only register the np2 texture fixup if needed.
[wine] / programs / notepad / dialog.c
1 /*
2  *  Notepad (dialog.c)
3  *
4  *  Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5  *  Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6  *  Copyright 2002 Andriy Palamarchuk
7  *  Copyright 2007 Rolf Kalbermatter
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 #define UNICODE
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <windows.h>
29 #include <commdlg.h>
30 #include <shlwapi.h>
31
32 #include "main.h"
33 #include "dialog.h"
34
35 #define SPACES_IN_TAB 8
36 #define PRINT_LEN_MAX 500
37
38 static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
39
40 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
41
42 VOID ShowLastError(void)
43 {
44     DWORD error = GetLastError();
45     if (error != NO_ERROR)
46     {
47         LPWSTR lpMsgBuf;
48         WCHAR szTitle[MAX_STRING_LEN];
49
50         LoadString(Globals.hInstance, STRING_ERROR, szTitle, SIZEOF(szTitle));
51         FormatMessage(
52             FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
53             NULL, error, 0,
54             (LPTSTR) &lpMsgBuf, 0, NULL);
55         MessageBox(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
56         LocalFree(lpMsgBuf);
57     }
58 }
59
60 /**
61  * Sets the caption of the main window according to Globals.szFileTitle:
62  *    Untitled - Notepad        if no file is open
63  *    filename - Notepad        if a file is given
64  */
65 static void UpdateWindowCaption(void)
66 {
67   WCHAR szCaption[MAX_STRING_LEN];
68   WCHAR szNotepad[MAX_STRING_LEN];
69   static const WCHAR hyphenW[] = { ' ','-',' ',0 };
70
71   if (Globals.szFileTitle[0] != '\0')
72       lstrcpy(szCaption, Globals.szFileTitle);
73   else
74       LoadString(Globals.hInstance, STRING_UNTITLED, szCaption, SIZEOF(szCaption));
75
76   LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
77   lstrcat(szCaption, hyphenW);
78   lstrcat(szCaption, szNotepad);
79
80   SetWindowText(Globals.hMainWnd, szCaption);
81 }
82
83 int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
84 {
85    WCHAR szMessage[MAX_STRING_LEN];
86    WCHAR szResource[MAX_STRING_LEN];
87
88    /* Load and format szMessage */
89    LoadString(Globals.hInstance, formatId, szResource, SIZEOF(szResource));
90    wnsprintf(szMessage, SIZEOF(szMessage), szResource, szString);
91
92    /* Load szCaption */
93    if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
94      LoadString(Globals.hInstance, STRING_ERROR,  szResource, SIZEOF(szResource));
95    else
96      LoadString(Globals.hInstance, STRING_NOTEPAD,  szResource, SIZEOF(szResource));
97
98    /* Display Modal Dialog */
99    if (hParent == NULL)
100      hParent = Globals.hMainWnd;
101    return MessageBox(hParent, szMessage, szResource, dwFlags);
102 }
103
104 static void AlertFileNotFound(LPCWSTR szFileName)
105 {
106    DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
107 }
108
109 static int AlertFileNotSaved(LPCWSTR szFileName)
110 {
111    WCHAR szUntitled[MAX_STRING_LEN];
112
113    LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
114    return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
115      MB_ICONQUESTION|MB_YESNOCANCEL);
116 }
117
118 /**
119  * Returns:
120  *   TRUE  - if file exists
121  *   FALSE - if file does not exist
122  */
123 BOOL FileExists(LPCWSTR szFilename)
124 {
125    WIN32_FIND_DATA entry;
126    HANDLE hFile;
127
128    hFile = FindFirstFile(szFilename, &entry);
129    FindClose(hFile);
130
131    return (hFile != INVALID_HANDLE_VALUE);
132 }
133
134
135 static VOID DoSaveFile(VOID)
136 {
137     HANDLE hFile;
138     DWORD dwNumWrite;
139     LPSTR pTemp;
140     DWORD size;
141
142     hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
143                        NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
144     if(hFile == INVALID_HANDLE_VALUE)
145     {
146         ShowLastError();
147         return;
148     }
149
150     size = GetWindowTextLengthA(Globals.hEdit) + 1;
151     pTemp = HeapAlloc(GetProcessHeap(), 0, size);
152     if (!pTemp)
153     {
154         CloseHandle(hFile);
155         ShowLastError();
156         return;
157     }
158     size = GetWindowTextA(Globals.hEdit, pTemp, size);
159
160     if (!WriteFile(hFile, pTemp, size, &dwNumWrite, NULL))
161         ShowLastError();
162     else
163         SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
164
165     SetEndOfFile(hFile);
166     CloseHandle(hFile);
167     HeapFree(GetProcessHeap(), 0, pTemp);
168 }
169
170 /**
171  * Returns:
172  *   TRUE  - User agreed to close (both save/don't save)
173  *   FALSE - User cancelled close by selecting "Cancel"
174  */
175 BOOL DoCloseFile(void)
176 {
177     int nResult;
178     static const WCHAR empty_strW[] = { 0 };
179
180     if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
181     {
182         /* prompt user to save changes */
183         nResult = AlertFileNotSaved(Globals.szFileName);
184         switch (nResult) {
185             case IDYES:     DIALOG_FileSave();
186                             break;
187
188             case IDNO:      break;
189
190             case IDCANCEL:  return(FALSE);
191
192             default:        return(FALSE);
193         } /* switch */
194     } /* if */
195
196     SetFileName(empty_strW);
197
198     UpdateWindowCaption();
199     return(TRUE);
200 }
201
202
203 void DoOpenFile(LPCWSTR szFileName)
204 {
205     static const WCHAR dotlog[] = { '.','L','O','G',0 };
206     HANDLE hFile;
207     LPSTR pTemp;
208     DWORD size;
209     DWORD dwNumRead;
210     WCHAR log[5];
211
212     /* Close any files and prompt to save changes */
213     if (!DoCloseFile())
214         return;
215
216     hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
217         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
218     if(hFile == INVALID_HANDLE_VALUE)
219     {
220         AlertFileNotFound(szFileName);
221         return;
222     }
223
224     size = GetFileSize(hFile, NULL);
225     if (size == INVALID_FILE_SIZE)
226     {
227         CloseHandle(hFile);
228         ShowLastError();
229         return;
230     }
231     size++;
232
233     pTemp = HeapAlloc(GetProcessHeap(), 0, size);
234     if (!pTemp)
235     {
236         CloseHandle(hFile);
237         ShowLastError();
238         return;
239     }
240
241     if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
242     {
243         CloseHandle(hFile);
244         HeapFree(GetProcessHeap(), 0, pTemp);
245         ShowLastError();
246         return;
247     }
248
249     CloseHandle(hFile);
250     pTemp[dwNumRead] = 0;
251
252     if((size -1) >= 2 && (BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
253         SetWindowTextW(Globals.hEdit, (LPWSTR)pTemp + 1);
254     else
255         SetWindowTextA(Globals.hEdit, pTemp);
256
257     HeapFree(GetProcessHeap(), 0, pTemp);
258
259     SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
260     SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
261     SetFocus(Globals.hEdit);
262     
263     /*  If the file starts with .LOG, add a time/date at the end and set cursor after */
264     if (GetWindowTextW(Globals.hEdit, log, sizeof(log)/sizeof(log[0])) && !lstrcmp(log, dotlog))
265     {
266         static const WCHAR lfW[] = { '\r','\n',0 };
267         SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
268         SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
269         DIALOG_EditTimeDate();
270         SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
271     }
272
273     SetFileName(szFileName);
274     UpdateWindowCaption();
275 }
276
277 VOID DIALOG_FileNew(VOID)
278 {
279     static const WCHAR empty_strW[] = { 0 };
280
281     /* Close any files and prompt to save changes */
282     if (DoCloseFile()) {
283         SetWindowText(Globals.hEdit, empty_strW);
284         SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
285         SetFocus(Globals.hEdit);
286     }
287 }
288
289 VOID DIALOG_FileOpen(VOID)
290 {
291     OPENFILENAME openfilename;
292     WCHAR szPath[MAX_PATH];
293     WCHAR szDir[MAX_PATH];
294     static const WCHAR szDefaultExt[] = { 't','x','t',0 };
295     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
296
297     ZeroMemory(&openfilename, sizeof(openfilename));
298
299     GetCurrentDirectory(SIZEOF(szDir), szDir);
300     lstrcpy(szPath, txt_files);
301
302     openfilename.lStructSize       = sizeof(openfilename);
303     openfilename.hwndOwner         = Globals.hMainWnd;
304     openfilename.hInstance         = Globals.hInstance;
305     openfilename.lpstrFilter       = Globals.szFilter;
306     openfilename.lpstrFile         = szPath;
307     openfilename.nMaxFile          = SIZEOF(szPath);
308     openfilename.lpstrInitialDir   = szDir;
309     openfilename.Flags             = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
310         OFN_HIDEREADONLY;
311     openfilename.lpstrDefExt       = szDefaultExt;
312
313
314     if (GetOpenFileName(&openfilename))
315         DoOpenFile(openfilename.lpstrFile);
316 }
317
318
319 VOID DIALOG_FileSave(VOID)
320 {
321     if (Globals.szFileName[0] == '\0')
322         DIALOG_FileSaveAs();
323     else
324         DoSaveFile();
325 }
326
327 VOID DIALOG_FileSaveAs(VOID)
328 {
329     OPENFILENAME saveas;
330     WCHAR szPath[MAX_PATH];
331     WCHAR szDir[MAX_PATH];
332     static const WCHAR szDefaultExt[] = { 't','x','t',0 };
333     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
334
335     ZeroMemory(&saveas, sizeof(saveas));
336
337     GetCurrentDirectory(SIZEOF(szDir), szDir);
338     lstrcpy(szPath, txt_files);
339
340     saveas.lStructSize       = sizeof(OPENFILENAME);
341     saveas.hwndOwner         = Globals.hMainWnd;
342     saveas.hInstance         = Globals.hInstance;
343     saveas.lpstrFilter       = Globals.szFilter;
344     saveas.lpstrFile         = szPath;
345     saveas.nMaxFile          = SIZEOF(szPath);
346     saveas.lpstrInitialDir   = szDir;
347     saveas.Flags             = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
348         OFN_HIDEREADONLY;
349     saveas.lpstrDefExt       = szDefaultExt;
350
351     if (GetSaveFileName(&saveas)) {
352         SetFileName(szPath);
353         UpdateWindowCaption();
354         DoSaveFile();
355     }
356 }
357
358 typedef struct {
359     LPWSTR mptr;
360     LPWSTR mend;
361     LPWSTR lptr;
362     DWORD len;
363 } TEXTINFO, *LPTEXTINFO;
364
365 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
366 {
367     SIZE szMetric;
368
369     if (*text)
370     {
371         /* Write the header or footer */
372         GetTextExtentPoint32(hdc, text, lstrlen(text), &szMetric);
373         if (dopage)
374             ExtTextOut(hdc, (rc->left + rc->right - szMetric.cx) / 2,
375                        header ? rc->top : rc->bottom - szMetric.cy,
376                        ETO_CLIPPED, rc, text, lstrlen(text), NULL);
377         return 1;
378     }
379     return 0;
380 }
381
382 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
383 {
384     int b, y;
385     TEXTMETRIC tm;
386     SIZE szMetrics;
387
388     if (dopage)
389     {
390         if (StartPage(hdc) <= 0)
391         {
392             static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
393             static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
394             MessageBox(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
395             return FALSE;
396         }
397     }
398
399     GetTextMetrics(hdc, &tm);
400     y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
401     b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
402
403     do {
404         INT m, n;
405
406         if (!tInfo->len)
407         {
408             /* find the end of the line */
409             while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
410             {
411                 if (*tInfo->mptr == '\t')
412                 {
413                     /* replace tabs with spaces */
414                     for (m = 0; m < SPACES_IN_TAB; m++)
415                     {
416                         if (tInfo->len < PRINT_LEN_MAX)
417                             tInfo->lptr[tInfo->len++] = ' ';
418                         else if (Globals.bWrapLongLines)
419                             break;
420                     }
421                 }
422                 else if (tInfo->len < PRINT_LEN_MAX)
423                     tInfo->lptr[tInfo->len++] = *tInfo->mptr;
424
425                 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
426                      break;
427
428                 tInfo->mptr++;
429             }
430         }
431
432         /* Find out how much we should print if line wrapping is enabled */
433         if (Globals.bWrapLongLines)
434         {
435             GetTextExtentExPoint(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
436             if (n < tInfo->len && tInfo->lptr[n] != ' ')
437             {
438                 m = n;
439                 /* Don't wrap words unless it's a single word over the entire line */
440                 while (m  && tInfo->lptr[m] != ' ') m--;
441                 if (m > 0) n = m + 1;
442             }
443         }
444         else
445             n = tInfo->len;
446
447         if (dopage)
448             ExtTextOut(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
449
450         tInfo->len -= n;
451
452         if (tInfo->len)
453         {
454             memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
455             y += tm.tmHeight + tm.tmExternalLeading;
456         }
457         else
458         {
459             /* find the next line */
460             while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
461             {
462                 if (*tInfo->mptr == '\n')
463                     y += tm.tmHeight + tm.tmExternalLeading;
464                 tInfo->mptr++;
465             }
466         }
467     } while (tInfo->mptr < tInfo->mend && y < b);
468
469     notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
470     if (dopage)
471     {
472         EndPage(hdc);
473     }
474     return TRUE;
475 }
476
477 VOID DIALOG_FilePrint(VOID)
478 {
479     DOCINFO di;
480     PRINTDLG printer;
481     int page, dopage, copy;
482     LOGFONT lfFont;
483     HFONT hTextFont, old_font = 0;
484     DWORD size;
485     BOOL ret = FALSE;
486     RECT rc;
487     LPWSTR pTemp;
488     TEXTINFO tInfo;
489     WCHAR cTemp[PRINT_LEN_MAX];
490
491     /* Get Current Settings */
492     ZeroMemory(&printer, sizeof(printer));
493     printer.lStructSize           = sizeof(printer);
494     printer.hwndOwner             = Globals.hMainWnd;
495     printer.hDevMode              = Globals.hDevMode;
496     printer.hDevNames             = Globals.hDevNames;
497     printer.hInstance             = Globals.hInstance;
498
499     /* Set some default flags */
500     printer.Flags                 = PD_RETURNDC | PD_NOSELECTION;
501     printer.nFromPage             = 0;
502     printer.nMinPage              = 1;
503     /* we really need to calculate number of pages to set nMaxPage and nToPage */
504     printer.nToPage               = 0;
505     printer.nMaxPage              = -1;
506     /* Let commdlg manage copy settings */
507     printer.nCopies               = (WORD)PD_USEDEVMODECOPIES;
508
509     if (!PrintDlg(&printer)) return;
510
511     Globals.hDevMode = printer.hDevMode;
512     Globals.hDevNames = printer.hDevNames;
513
514     SetMapMode(printer.hDC, MM_TEXT);
515
516     /* initialize DOCINFO */
517     di.cbSize = sizeof(DOCINFO);
518     di.lpszDocName = Globals.szFileTitle;
519     di.lpszOutput = NULL;
520     di.lpszDatatype = NULL;
521     di.fwType = 0; 
522
523     /* Get the file text */
524     size = GetWindowTextLength(Globals.hEdit) + 1;
525     pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
526     if (!pTemp)
527     {
528        DeleteDC(printer.hDC);
529        ShowLastError();
530        return;
531     }
532     size = GetWindowText(Globals.hEdit, pTemp, size);
533
534     if (StartDoc(printer.hDC, &di) > 0)
535     {
536         /* Get the page margins in pixels. */
537         rc.top =    MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
538                     GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
539         rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
540                     MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
541         rc.left =   MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
542                     GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
543         rc.right =  GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
544                     MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
545
546         /* Create a font for the printer resolution */
547         lfFont = Globals.lfFont;
548         lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
549         /* Make the font a bit lighter */
550         lfFont.lfWeight -= 100;
551         hTextFont = CreateFontIndirect(&lfFont);
552         old_font = SelectObject(printer.hDC, hTextFont);
553
554         for (copy = 1; copy <= printer.nCopies; copy++)
555         {
556             page = 1;
557
558             tInfo.mptr = pTemp;
559             tInfo.mend = pTemp + size;
560             tInfo.lptr = cTemp;
561             tInfo.len = 0;
562
563             do {
564                 if (printer.Flags & PD_PAGENUMS)
565                 {
566                     /* a specific range of pages is selected, so
567                      * skip pages that are not to be printed
568                      */
569                     if (page > printer.nToPage)
570                         break;
571                     else if (page >= printer.nFromPage)
572                         dopage = 1;
573                     else
574                         dopage = 0;
575                 }
576                 else
577                     dopage = 1;
578
579                 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
580                 page++;
581             } while (ret && tInfo.mptr < tInfo.mend);
582
583             if (!ret) break;
584         }
585         EndDoc(printer.hDC);
586         SelectObject(printer.hDC, old_font);
587         DeleteObject(hTextFont);
588     }
589     DeleteDC(printer.hDC);
590     HeapFree(GetProcessHeap(), 0, pTemp);
591 }
592
593 VOID DIALOG_FilePrinterSetup(VOID)
594 {
595     PRINTDLG printer;
596
597     ZeroMemory(&printer, sizeof(printer));
598     printer.lStructSize         = sizeof(printer);
599     printer.hwndOwner           = Globals.hMainWnd;
600     printer.hDevMode            = Globals.hDevMode;
601     printer.hDevNames           = Globals.hDevNames;
602     printer.hInstance           = Globals.hInstance;
603     printer.Flags               = PD_PRINTSETUP;
604     printer.nCopies             = 1;
605
606     PrintDlg(&printer);
607
608     Globals.hDevMode = printer.hDevMode;
609     Globals.hDevNames = printer.hDevNames;
610 }
611
612 VOID DIALOG_FileExit(VOID)
613 {
614     PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
615 }
616
617 VOID DIALOG_EditUndo(VOID)
618 {
619     SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
620 }
621
622 VOID DIALOG_EditCut(VOID)
623 {
624     SendMessage(Globals.hEdit, WM_CUT, 0, 0);
625 }
626
627 VOID DIALOG_EditCopy(VOID)
628 {
629     SendMessage(Globals.hEdit, WM_COPY, 0, 0);
630 }
631
632 VOID DIALOG_EditPaste(VOID)
633 {
634     SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
635 }
636
637 VOID DIALOG_EditDelete(VOID)
638 {
639     SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
640 }
641
642 VOID DIALOG_EditSelectAll(VOID)
643 {
644     SendMessage(Globals.hEdit, EM_SETSEL, 0, (LPARAM)-1);
645 }
646
647 VOID DIALOG_EditTimeDate(VOID)
648 {
649     SYSTEMTIME   st;
650     WCHAR        szDate[MAX_STRING_LEN];
651     static const WCHAR spaceW[] = { ' ',0 };
652
653     GetLocalTime(&st);
654
655     GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
656     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
657
658     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
659
660     GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
661     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
662 }
663
664 VOID DIALOG_EditWrap(VOID)
665 {
666     BOOL modify = FALSE;
667     static const WCHAR editW[] = { 'e','d','i','t',0 };
668     DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
669                     ES_AUTOVSCROLL | ES_MULTILINE;
670     RECT rc;
671     DWORD size;
672     LPWSTR pTemp;
673
674     size = GetWindowTextLength(Globals.hEdit) + 1;
675     pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
676     if (!pTemp)
677     {
678         ShowLastError();
679         return;
680     }
681     GetWindowText(Globals.hEdit, pTemp, size);
682     modify = SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0);
683     DestroyWindow(Globals.hEdit);
684     GetClientRect(Globals.hMainWnd, &rc);
685     if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
686     Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
687                          0, 0, rc.right, rc.bottom, Globals.hMainWnd,
688                          NULL, Globals.hInstance, NULL);
689     SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
690     SetWindowTextW(Globals.hEdit, pTemp);
691     SendMessage(Globals.hEdit, EM_SETMODIFY, (WPARAM)modify, 0);
692     SetFocus(Globals.hEdit);
693     HeapFree(GetProcessHeap(), 0, pTemp);
694     
695     Globals.bWrapLongLines = !Globals.bWrapLongLines;
696     CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
697         MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
698 }
699
700 VOID DIALOG_SelectFont(VOID)
701 {
702     CHOOSEFONT cf;
703     LOGFONT lf=Globals.lfFont;
704
705     ZeroMemory( &cf, sizeof(cf) );
706     cf.lStructSize=sizeof(cf);
707     cf.hwndOwner=Globals.hMainWnd;
708     cf.lpLogFont=&lf;
709     cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
710
711     if( ChooseFont(&cf) )
712     {
713         HFONT currfont=Globals.hFont;
714
715         Globals.hFont=CreateFontIndirect( &lf );
716         Globals.lfFont=lf;
717         SendMessage( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE );
718         if( currfont!=NULL )
719             DeleteObject( currfont );
720     }
721 }
722
723 VOID DIALOG_Search(VOID)
724 {
725         ZeroMemory(&Globals.find, sizeof(Globals.find));
726         Globals.find.lStructSize      = sizeof(Globals.find);
727         Globals.find.hwndOwner        = Globals.hMainWnd;
728         Globals.find.hInstance        = Globals.hInstance;
729         Globals.find.lpstrFindWhat    = Globals.szFindText;
730         Globals.find.wFindWhatLen     = SIZEOF(Globals.szFindText);
731         Globals.find.Flags            = FR_DOWN|FR_HIDEWHOLEWORD;
732
733         /* We only need to create the modal FindReplace dialog which will */
734         /* notify us of incoming events using hMainWnd Window Messages    */
735
736         Globals.hFindReplaceDlg = FindText(&Globals.find);
737         assert(Globals.hFindReplaceDlg !=0);
738 }
739
740 VOID DIALOG_SearchNext(VOID)
741 {
742     if (Globals.lastFind.lpstrFindWhat == NULL)
743         DIALOG_Search();
744     else                /* use the last find data */
745         NOTEPAD_DoFind(&Globals.lastFind);
746 }
747
748 VOID DIALOG_HelpContents(VOID)
749 {
750     WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
751 }
752
753 VOID DIALOG_HelpSearch(VOID)
754 {
755         /* Search Help */
756 }
757
758 VOID DIALOG_HelpHelp(VOID)
759 {
760     WinHelp(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
761 }
762
763 VOID DIALOG_HelpAboutNotepad(VOID)
764 {
765     static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
766     WCHAR szNotepad[MAX_STRING_LEN];
767     HICON icon = LoadImageW( Globals.hInstance, MAKEINTRESOURCE(IDI_NOTEPAD),
768                              IMAGE_ICON, 48, 48, LR_SHARED );
769
770     LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
771     ShellAbout(Globals.hMainWnd, szNotepad, notepadW, icon);
772 }
773
774
775 /***********************************************************************
776  *
777  *           DIALOG_FilePageSetup
778  */
779 VOID DIALOG_FilePageSetup(void)
780 {
781   DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
782             Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
783 }
784
785
786 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
787  *
788  *           DIALOG_PAGESETUP_DlgProc
789  */
790
791 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
792 {
793
794    switch (msg)
795     {
796     case WM_COMMAND:
797       switch (wParam)
798         {
799         case IDOK:
800           /* save user input and close dialog */
801           GetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, SIZEOF(Globals.szHeader));
802           GetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, SIZEOF(Globals.szFooter));
803
804           Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
805           Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
806           Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
807           Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
808           EndDialog(hDlg, IDOK);
809           return TRUE;
810
811         case IDCANCEL:
812           /* discard user input and close dialog */
813           EndDialog(hDlg, IDCANCEL);
814           return TRUE;
815
816         case IDHELP:
817         {
818           /* FIXME: Bring this to work */
819           static const WCHAR sorryW[] = { 'S','o','r','r','y',',',' ','n','o',' ','h','e','l','p',' ','a','v','a','i','l','a','b','l','e',0 };
820           static const WCHAR helpW[] = { 'H','e','l','p',0 };
821           MessageBox(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
822           return TRUE;
823         }
824
825         default:
826             break;
827         }
828       break;
829
830     case WM_INITDIALOG:
831        /* fetch last user input prior to display dialog */
832        SetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
833        SetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
834        SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
835        SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
836        SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
837        SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
838        break;
839     }
840
841   return FALSE;
842 }