oleaut32: Fix a signed/unsigned mismatch.
[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         ShowLastError();
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 (IsTextUnicode(pTemp, dwNumRead, NULL))
253     {
254         LPWSTR p = (LPWSTR)pTemp;
255         /* We need to strip BOM Unicode character, SetWindowTextW won't do it for us. */
256         if (*p == 0xFEFF || *p == 0xFFFE) p++;
257         SetWindowTextW(Globals.hEdit, p);
258     }
259     else
260         SetWindowTextA(Globals.hEdit, pTemp);
261
262     HeapFree(GetProcessHeap(), 0, pTemp);
263
264     SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
265     SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
266     SetFocus(Globals.hEdit);
267     
268     /*  If the file starts with .LOG, add a time/date at the end and set cursor after
269      *  See http://support.microsoft.com/?kbid=260563
270      */
271     if (GetWindowTextW(Globals.hEdit, log, sizeof(log)/sizeof(log[0])) && !lstrcmp(log, dotlog))
272     {
273         static const WCHAR lfW[] = { '\r','\n',0 };
274         SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
275         SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
276         DIALOG_EditTimeDate();
277         SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
278     }
279
280     SetFileName(szFileName);
281     UpdateWindowCaption();
282 }
283
284 VOID DIALOG_FileNew(VOID)
285 {
286     static const WCHAR empty_strW[] = { 0 };
287
288     /* Close any files and promt to save changes */
289     if (DoCloseFile()) {
290         SetWindowText(Globals.hEdit, empty_strW);
291         SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
292         SetFocus(Globals.hEdit);
293     }
294 }
295
296 VOID DIALOG_FileOpen(VOID)
297 {
298     OPENFILENAME openfilename;
299     WCHAR szPath[MAX_PATH];
300     WCHAR szDir[MAX_PATH];
301     static const WCHAR szDefaultExt[] = { 't','x','t',0 };
302     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
303
304     ZeroMemory(&openfilename, sizeof(openfilename));
305
306     GetCurrentDirectory(SIZEOF(szDir), szDir);
307     lstrcpy(szPath, txt_files);
308
309     openfilename.lStructSize       = sizeof(openfilename);
310     openfilename.hwndOwner         = Globals.hMainWnd;
311     openfilename.hInstance         = Globals.hInstance;
312     openfilename.lpstrFilter       = Globals.szFilter;
313     openfilename.lpstrFile         = szPath;
314     openfilename.nMaxFile          = SIZEOF(szPath);
315     openfilename.lpstrInitialDir   = szDir;
316     openfilename.Flags             = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
317         OFN_HIDEREADONLY;
318     openfilename.lpstrDefExt       = szDefaultExt;
319
320
321     if (GetOpenFileName(&openfilename)) {
322         if (FileExists(openfilename.lpstrFile))
323             DoOpenFile(openfilename.lpstrFile);
324         else
325             AlertFileNotFound(openfilename.lpstrFile);
326     }
327 }
328
329
330 VOID DIALOG_FileSave(VOID)
331 {
332     if (Globals.szFileName[0] == '\0')
333         DIALOG_FileSaveAs();
334     else
335         DoSaveFile();
336 }
337
338 VOID DIALOG_FileSaveAs(VOID)
339 {
340     OPENFILENAME saveas;
341     WCHAR szPath[MAX_PATH];
342     WCHAR szDir[MAX_PATH];
343     static const WCHAR szDefaultExt[] = { 't','x','t',0 };
344     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
345
346     ZeroMemory(&saveas, sizeof(saveas));
347
348     GetCurrentDirectory(SIZEOF(szDir), szDir);
349     lstrcpy(szPath, txt_files);
350
351     saveas.lStructSize       = sizeof(OPENFILENAME);
352     saveas.hwndOwner         = Globals.hMainWnd;
353     saveas.hInstance         = Globals.hInstance;
354     saveas.lpstrFilter       = Globals.szFilter;
355     saveas.lpstrFile         = szPath;
356     saveas.nMaxFile          = SIZEOF(szPath);
357     saveas.lpstrInitialDir   = szDir;
358     saveas.Flags             = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
359         OFN_HIDEREADONLY;
360     saveas.lpstrDefExt       = szDefaultExt;
361
362     if (GetSaveFileName(&saveas)) {
363         SetFileName(szPath);
364         UpdateWindowCaption();
365         DoSaveFile();
366     }
367 }
368
369 typedef struct {
370     LPWSTR mptr;
371     LPWSTR mend;
372     LPWSTR lptr;
373     DWORD len;
374 } TEXTINFO, *LPTEXTINFO;
375
376 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
377 {
378     SIZE szMetric;
379
380     if (*text)
381     {
382         /* Write the header or footer */
383         GetTextExtentPoint32(hdc, text, lstrlen(text), &szMetric);
384         if (dopage)
385             ExtTextOut(hdc, (rc->left + rc->right - szMetric.cx) / 2,
386                        header ? rc->top : rc->bottom - szMetric.cy,
387                        ETO_CLIPPED, rc, text, lstrlen(text), NULL);
388         return 1;
389     }
390     return 0;
391 }
392
393 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
394 {
395     int b, y;
396     TEXTMETRIC tm;
397     SIZE szMetrics;
398
399     if (dopage)
400     {
401         if (StartPage(hdc) <= 0)
402         {
403             static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
404             static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
405             MessageBox(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
406             return FALSE;
407         }
408     }
409
410     GetTextMetrics(hdc, &tm);
411     y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
412     b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
413
414     do {
415         INT m, n;
416
417         if (!tInfo->len)
418         {
419             /* find the end of the line */
420             while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
421             {
422                 if (*tInfo->mptr == '\t')
423                 {
424                     /* replace tabs with spaces */
425                     for (m = 0; m < SPACES_IN_TAB; m++)
426                     {
427                         if (tInfo->len < PRINT_LEN_MAX)
428                             tInfo->lptr[tInfo->len++] = ' ';
429                         else if (Globals.bWrapLongLines)
430                             break;
431                     }
432                 }
433                 else if (tInfo->len < PRINT_LEN_MAX)
434                     tInfo->lptr[tInfo->len++] = *tInfo->mptr;
435
436                 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
437                      break;
438
439                 tInfo->mptr++;
440             }
441         }
442
443         /* Find out how much we should print if line wrapping is enabled */
444         if (Globals.bWrapLongLines)
445         {
446             GetTextExtentExPoint(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
447             if (n < tInfo->len && tInfo->lptr[n] != ' ')
448             {
449                 m = n;
450                 /* Don't wrap words unless it's a single word over the entire line */
451                 while (m  && tInfo->lptr[m] != ' ') m--;
452                 if (m > 0) n = m + 1;
453             }
454         }
455         else
456             n = tInfo->len;
457
458         if (dopage)
459             ExtTextOut(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
460
461         tInfo->len -= n;
462
463         if (tInfo->len)
464         {
465             memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
466             y += tm.tmHeight + tm.tmExternalLeading;
467         }
468         else
469         {
470             /* find the next line */
471             while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
472             {
473                 if (*tInfo->mptr == '\n')
474                     y += tm.tmHeight + tm.tmExternalLeading;
475                 tInfo->mptr++;
476             }
477         }
478     } while (tInfo->mptr < tInfo->mend && y < b);
479
480     notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
481     if (dopage)
482     {
483         EndPage(hdc);
484     }
485     return TRUE;
486 }
487
488 VOID DIALOG_FilePrint(VOID)
489 {
490     DOCINFO di;
491     PRINTDLG printer;
492     int page, dopage, copy;
493     LOGFONT lfFont;
494     HFONT hTextFont, old_font = 0;
495     DWORD size;
496     BOOL ret = FALSE;
497     RECT rc;
498     LPWSTR pTemp;
499     TEXTINFO tInfo;
500     WCHAR cTemp[PRINT_LEN_MAX];
501
502     /* Get Current Settings */
503     ZeroMemory(&printer, sizeof(printer));
504     printer.lStructSize           = sizeof(printer);
505     printer.hwndOwner             = Globals.hMainWnd;
506     printer.hDevMode              = Globals.hDevMode;
507     printer.hDevNames             = Globals.hDevNames;
508     printer.hInstance             = Globals.hInstance;
509
510     /* Set some default flags */
511     printer.Flags                 = PD_RETURNDC | PD_NOSELECTION;
512     printer.nFromPage             = 0;
513     printer.nMinPage              = 1;
514     /* we really need to calculate number of pages to set nMaxPage and nToPage */
515     printer.nToPage               = 0;
516     printer.nMaxPage              = -1;
517     /* Let commdlg manage copy settings */
518     printer.nCopies               = (WORD)PD_USEDEVMODECOPIES;
519
520     if (!PrintDlg(&printer)) return;
521
522     Globals.hDevMode = printer.hDevMode;
523     Globals.hDevNames = printer.hDevNames;
524
525     SetMapMode(printer.hDC, MM_TEXT);
526
527     /* initialize DOCINFO */
528     di.cbSize = sizeof(DOCINFO);
529     di.lpszDocName = Globals.szFileTitle;
530     di.lpszOutput = NULL;
531     di.lpszDatatype = NULL;
532     di.fwType = 0; 
533
534     /* Get the file text */
535     size = GetWindowTextLength(Globals.hEdit) + 1;
536     pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
537     if (!pTemp)
538     {
539        DeleteDC(printer.hDC);
540        ShowLastError();
541        return;
542     }
543     size = GetWindowText(Globals.hEdit, pTemp, size);
544
545     if (StartDoc(printer.hDC, &di) > 0)
546     {
547         /* Get the page margins in pixels. */
548         rc.top =    MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
549                     GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
550         rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
551                     MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
552         rc.left =   MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
553                     GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
554         rc.right =  GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
555                     MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
556
557         /* Create a font for the printer resolution */
558         lfFont = Globals.lfFont;
559         lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
560         /* Make the font a bit lighter */
561         lfFont.lfWeight -= 100;
562         hTextFont = CreateFontIndirect(&lfFont);
563         old_font = SelectObject(printer.hDC, hTextFont);
564
565         for (copy = 1; copy <= printer.nCopies; copy++)
566         {
567             page = 1;
568
569             tInfo.mptr = pTemp;
570             tInfo.mend = pTemp + size;
571             tInfo.lptr = cTemp;
572             tInfo.len = 0;
573
574             do {
575                 if (printer.Flags & PD_PAGENUMS)
576                 {
577                     /* a specific range of pages is selected, so
578                      * skip pages that are not to be printed
579                      */
580                     if (page > printer.nToPage)
581                         break;
582                     else if (page >= printer.nFromPage)
583                         dopage = 1;
584                     else
585                         dopage = 0;
586                 }
587                 else
588                     dopage = 1;
589
590                 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
591                 page++;
592             } while (ret && tInfo.mptr < tInfo.mend);
593
594             if (!ret) break;
595         }
596         EndDoc(printer.hDC);
597         SelectObject(printer.hDC, old_font);
598         DeleteObject(hTextFont);
599     }
600     DeleteDC(printer.hDC);
601     HeapFree(GetProcessHeap(), 0, pTemp);
602 }
603
604 VOID DIALOG_FilePrinterSetup(VOID)
605 {
606     PRINTDLG printer;
607
608     ZeroMemory(&printer, sizeof(printer));
609     printer.lStructSize         = sizeof(printer);
610     printer.hwndOwner           = Globals.hMainWnd;
611     printer.hDevMode            = Globals.hDevMode;
612     printer.hDevNames           = Globals.hDevNames;
613     printer.hInstance           = Globals.hInstance;
614     printer.Flags               = PD_PRINTSETUP;
615     printer.nCopies             = 1;
616
617     PrintDlg(&printer);
618
619     Globals.hDevMode = printer.hDevMode;
620     Globals.hDevNames = printer.hDevNames;
621 }
622
623 VOID DIALOG_FileExit(VOID)
624 {
625     PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
626 }
627
628 VOID DIALOG_EditUndo(VOID)
629 {
630     SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
631 }
632
633 VOID DIALOG_EditCut(VOID)
634 {
635     SendMessage(Globals.hEdit, WM_CUT, 0, 0);
636 }
637
638 VOID DIALOG_EditCopy(VOID)
639 {
640     SendMessage(Globals.hEdit, WM_COPY, 0, 0);
641 }
642
643 VOID DIALOG_EditPaste(VOID)
644 {
645     SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
646 }
647
648 VOID DIALOG_EditDelete(VOID)
649 {
650     SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
651 }
652
653 VOID DIALOG_EditSelectAll(VOID)
654 {
655     SendMessage(Globals.hEdit, EM_SETSEL, 0, (LPARAM)-1);
656 }
657
658 VOID DIALOG_EditTimeDate(VOID)
659 {
660     SYSTEMTIME   st;
661     WCHAR        szDate[MAX_STRING_LEN];
662     static const WCHAR spaceW[] = { ' ',0 };
663
664     GetLocalTime(&st);
665
666     GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
667     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
668
669     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
670
671     GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
672     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
673 }
674
675 VOID DIALOG_EditWrap(VOID)
676 {
677     BOOL modify = FALSE;
678     static const WCHAR editW[] = { 'e','d','i','t',0 };
679     DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
680                     ES_AUTOVSCROLL | ES_MULTILINE;
681     RECT rc;
682     DWORD size;
683     LPWSTR pTemp;
684
685     size = GetWindowTextLength(Globals.hEdit) + 1;
686     pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
687     if (!pTemp)
688     {
689         ShowLastError();
690         return;
691     }
692     GetWindowText(Globals.hEdit, pTemp, size);
693     modify = SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0);
694     DestroyWindow(Globals.hEdit);
695     GetClientRect(Globals.hMainWnd, &rc);
696     if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
697     Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
698                          0, 0, rc.right, rc.bottom, Globals.hMainWnd,
699                          NULL, Globals.hInstance, NULL);
700     SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
701     SetWindowTextW(Globals.hEdit, pTemp);
702     SendMessage(Globals.hEdit, EM_SETMODIFY, (WPARAM)modify, 0);
703     SetFocus(Globals.hEdit);
704     HeapFree(GetProcessHeap(), 0, pTemp);
705     
706     Globals.bWrapLongLines = !Globals.bWrapLongLines;
707     CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
708         MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
709 }
710
711 VOID DIALOG_SelectFont(VOID)
712 {
713     CHOOSEFONT cf;
714     LOGFONT lf=Globals.lfFont;
715
716     ZeroMemory( &cf, sizeof(cf) );
717     cf.lStructSize=sizeof(cf);
718     cf.hwndOwner=Globals.hMainWnd;
719     cf.lpLogFont=&lf;
720     cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
721
722     if( ChooseFont(&cf) )
723     {
724         HFONT currfont=Globals.hFont;
725
726         Globals.hFont=CreateFontIndirect( &lf );
727         Globals.lfFont=lf;
728         SendMessage( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE );
729         if( currfont!=NULL )
730             DeleteObject( currfont );
731     }
732 }
733
734 VOID DIALOG_Search(VOID)
735 {
736         ZeroMemory(&Globals.find, sizeof(Globals.find));
737         Globals.find.lStructSize      = sizeof(Globals.find);
738         Globals.find.hwndOwner        = Globals.hMainWnd;
739         Globals.find.hInstance        = Globals.hInstance;
740         Globals.find.lpstrFindWhat    = Globals.szFindText;
741         Globals.find.wFindWhatLen     = SIZEOF(Globals.szFindText);
742         Globals.find.Flags            = FR_DOWN|FR_HIDEWHOLEWORD;
743
744         /* We only need to create the modal FindReplace dialog which will */
745         /* notify us of incoming events using hMainWnd Window Messages    */
746
747         Globals.hFindReplaceDlg = FindText(&Globals.find);
748         assert(Globals.hFindReplaceDlg !=0);
749 }
750
751 VOID DIALOG_SearchNext(VOID)
752 {
753     if (Globals.lastFind.lpstrFindWhat == NULL)
754         DIALOG_Search();
755     else                /* use the last find data */
756         NOTEPAD_DoFind(&Globals.lastFind);
757 }
758
759 VOID DIALOG_HelpContents(VOID)
760 {
761     WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
762 }
763
764 VOID DIALOG_HelpSearch(VOID)
765 {
766         /* Search Help */
767 }
768
769 VOID DIALOG_HelpHelp(VOID)
770 {
771     WinHelp(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
772 }
773
774 VOID DIALOG_HelpLicense(VOID)
775 {
776     TCHAR cap[20], text[1024];
777     LoadString(Globals.hInstance, IDS_LICENSE, text, 1024);
778     LoadString(Globals.hInstance, IDS_LICENSE_CAPTION, cap, 20);
779     MessageBox(Globals.hMainWnd, text, cap, MB_ICONINFORMATION | MB_OK);
780 }
781
782 VOID DIALOG_HelpNoWarranty(VOID)
783 {
784     TCHAR cap[20], text[1024];
785     LoadString(Globals.hInstance, IDS_WARRANTY, text, 1024);
786     LoadString(Globals.hInstance, IDS_WARRANTY_CAPTION, cap, 20);
787     MessageBox(Globals.hMainWnd, text, cap, MB_ICONEXCLAMATION | MB_OK);
788 }
789
790 VOID DIALOG_HelpAboutWine(VOID)
791 {
792     static const WCHAR notepadW[] = { 'N','o','t','e','p','a','d','\n',0 };
793     WCHAR szNotepad[MAX_STRING_LEN];
794
795     LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
796     ShellAbout(Globals.hMainWnd, szNotepad, notepadW, 0);
797 }
798
799
800 /***********************************************************************
801  *
802  *           DIALOG_FilePageSetup
803  */
804 VOID DIALOG_FilePageSetup(void)
805 {
806   DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
807             Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
808 }
809
810
811 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
812  *
813  *           DIALOG_PAGESETUP_DlgProc
814  */
815
816 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
817 {
818
819    switch (msg)
820     {
821     case WM_COMMAND:
822       switch (wParam)
823         {
824         case IDOK:
825           /* save user input and close dialog */
826           GetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, SIZEOF(Globals.szHeader));
827           GetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, SIZEOF(Globals.szFooter));
828
829           Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
830           Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
831           Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
832           Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
833           EndDialog(hDlg, IDOK);
834           return TRUE;
835
836         case IDCANCEL:
837           /* discard user input and close dialog */
838           EndDialog(hDlg, IDCANCEL);
839           return TRUE;
840
841         case IDHELP:
842         {
843           /* FIXME: Bring this to work */
844           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 };
845           static const WCHAR helpW[] = { 'H','e','l','p',0 };
846           MessageBox(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
847           return TRUE;
848         }
849
850         default:
851             break;
852         }
853       break;
854
855     case WM_INITDIALOG:
856        /* fetch last user input prior to display dialog */
857        SetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
858        SetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
859        SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
860        SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
861        SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
862        SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
863        break;
864     }
865
866   return FALSE;
867 }