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