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