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