Notepad will no longer add .txt if you invoke it specifying a file that does not...
[reactos.git] / reactos / subsys / system / 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 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #define UNICODE
24 #define _UNICODE
25
26 #define _CRT_SECURE_NO_DEPRECATE
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <windows.h>
31 #include <commdlg.h>
32 #include <tchar.h>
33
34 #include "main.h"
35 #include "license.h"
36 #include "dialog.h"
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 * Notepad - (untitled) if no file is open
63 * Notepad - [filename] if a file is given
64 */
65 static void UpdateWindowCaption(void)
66 {
67 WCHAR szCaption[MAX_STRING_LEN];
68 WCHAR szUntitled[MAX_STRING_LEN];
69
70 LoadString(Globals.hInstance, STRING_NOTEPAD, szCaption, SIZEOF(szCaption));
71
72 if (Globals.szFileTitle[0] != '\0') {
73 static const WCHAR bracket_lW[] = { ' ','-',' ','[',0 };
74 static const WCHAR bracket_rW[] = { ']',0 };
75 lstrcat(szCaption, bracket_lW);
76 lstrcat(szCaption, Globals.szFileTitle);
77 lstrcat(szCaption, bracket_rW);
78 }
79 else
80 {
81 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
82 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
83 lstrcat(szCaption, hyphenW);
84 lstrcat(szCaption, szUntitled);
85 }
86
87 SetWindowText(Globals.hMainWnd, szCaption);
88 }
89
90 static void AlertFileNotFound(LPCWSTR szFileName)
91 {
92 WCHAR szMessage[MAX_STRING_LEN];
93 WCHAR szResource[MAX_STRING_LEN];
94
95 /* Load and format szMessage */
96 LoadString(Globals.hInstance, STRING_NOTFOUND, szResource, SIZEOF(szResource));
97 wsprintf(szMessage, szResource, szFileName);
98
99 /* Load szCaption */
100 LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
101
102 /* Display Modal Dialog */
103 MessageBox(Globals.hMainWnd, szMessage, szResource, MB_ICONEXCLAMATION);
104 }
105
106 static int AlertFileNotSaved(LPCWSTR szFileName)
107 {
108 WCHAR szMessage[MAX_STRING_LEN];
109 WCHAR szResource[MAX_STRING_LEN];
110 WCHAR szUntitled[MAX_STRING_LEN];
111
112 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
113
114 /* Load and format Message */
115 LoadString(Globals.hInstance, STRING_NOTSAVED, szResource, SIZEOF(szResource));
116 wsprintf(szMessage, szResource, szFileName[0] ? szFileName : szUntitled);
117
118 /* Load Caption */
119 LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
120
121 /* Display modal */
122 return MessageBox(Globals.hMainWnd, szMessage, szResource, MB_ICONEXCLAMATION|MB_YESNOCANCEL);
123 }
124
125 /**
126 * Returns:
127 * TRUE - if file exists
128 * FALSE - if file does not exist
129 */
130 BOOL FileExists(LPCWSTR szFilename)
131 {
132 WIN32_FIND_DATA entry;
133 HANDLE hFile;
134
135 hFile = FindFirstFile(szFilename, &entry);
136 FindClose(hFile);
137
138 return (hFile != INVALID_HANDLE_VALUE);
139 }
140
141
142 BOOL HasFileExtension(LPCWSTR szFilename)
143 {
144 LPCWSTR s;
145
146 s = wcsrchr(szFilename, '\\');
147 if (s)
148 szFilename = s;
149 return wcsrchr(szFilename, '.') != NULL;
150 }
151
152
153 static VOID DoSaveFile(VOID)
154 {
155 HANDLE hFile;
156 LPWSTR pTemp;
157 DWORD size;
158
159 hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
160 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
161 if(hFile == INVALID_HANDLE_VALUE)
162 {
163 ShowLastError();
164 return;
165 }
166
167 size = GetWindowTextLengthW(Globals.hEdit) + 1;
168 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*pTemp));
169 if (!pTemp)
170 {
171 CloseHandle(hFile);
172 ShowLastError();
173 return;
174 }
175 size = GetWindowTextW(Globals.hEdit, pTemp, size);
176
177 if (!WriteText(hFile, pTemp, size, Globals.iEncoding, Globals.iEoln))
178 ShowLastError();
179 else
180 SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
181
182 CloseHandle(hFile);
183 HeapFree(GetProcessHeap(), 0, pTemp);
184 }
185
186 /**
187 * Returns:
188 * TRUE - User agreed to close (both save/don't save)
189 * FALSE - User cancelled close by selecting "Cancel"
190 */
191 BOOL DoCloseFile(void)
192 {
193 int nResult;
194 static const WCHAR empty_strW[] = { 0 };
195
196 if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
197 {
198 /* prompt user to save changes */
199 nResult = AlertFileNotSaved(Globals.szFileName);
200 switch (nResult) {
201 case IDYES: DIALOG_FileSave();
202 break;
203
204 case IDNO: break;
205
206 case IDCANCEL: return(FALSE);
207 break;
208
209 default: return(FALSE);
210 break;
211 } /* switch */
212 } /* if */
213
214 SetFileName(empty_strW);
215
216 UpdateWindowCaption();
217 return(TRUE);
218 }
219
220
221 void DoOpenFile(LPCWSTR szFileName)
222 {
223 static const WCHAR dotlog[] = { '.','L','O','G',0 };
224 HANDLE hFile;
225 LPWSTR pszText;
226 DWORD dwTextLen;
227 WCHAR log[5];
228
229 /* Close any files and prompt to save changes */
230 if (!DoCloseFile())
231 return;
232
233 hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
234 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
235 if (hFile == INVALID_HANDLE_VALUE)
236 {
237 ShowLastError();
238 goto done;
239 }
240
241 if (!ReadText(hFile, &pszText, &dwTextLen, &Globals.iEncoding, &Globals.iEoln))
242 {
243 ShowLastError();
244 goto done;
245 }
246
247 SetWindowTextW(Globals.hEdit, pszText);
248
249 SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
250 SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
251 SetFocus(Globals.hEdit);
252
253 /* If the file starts with .LOG, add a time/date at the end and set cursor after
254 * See http://support.microsoft.com/?kbid=260563
255 */
256 if (GetWindowTextW(Globals.hEdit, log, sizeof(log)/sizeof(log[0])) && !lstrcmp(log, dotlog))
257 {
258 static const WCHAR lfW[] = { '\r','\n',0 };
259 SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
260 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
261 DIALOG_EditTimeDate();
262 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
263 }
264
265 SetFileName(szFileName);
266 UpdateWindowCaption();
267
268 done:
269 if (hFile != INVALID_HANDLE_VALUE)
270 CloseHandle(hFile);
271 if (pszText)
272 HeapFree(GetProcessHeap(), 0, pszText);
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 SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
283 SetFocus(Globals.hEdit);
284 }
285 }
286
287 VOID DIALOG_FileOpen(VOID)
288 {
289 OPENFILENAME openfilename;
290 WCHAR szDir[MAX_PATH];
291 WCHAR szPath[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 if (Globals.szFileName[0] == 0)
299 lstrcpy(szPath, txt_files);
300 else
301 lstrcpy(szPath, Globals.szFileName);
302
303 openfilename.lStructSize = sizeof(openfilename);
304 openfilename.hwndOwner = Globals.hMainWnd;
305 openfilename.hInstance = Globals.hInstance;
306 openfilename.lpstrFilter = Globals.szFilter;
307 openfilename.lpstrFile = szPath;
308 openfilename.nMaxFile = SIZEOF(szPath);
309 openfilename.lpstrInitialDir = szDir;
310 openfilename.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
311 OFN_HIDEREADONLY;
312 openfilename.lpstrDefExt = szDefaultExt;
313
314
315 if (GetOpenFileName(&openfilename)) {
316 if (FileExists(openfilename.lpstrFile))
317 DoOpenFile(openfilename.lpstrFile);
318 else
319 AlertFileNotFound(openfilename.lpstrFile);
320 }
321 }
322
323
324 VOID DIALOG_FileSave(VOID)
325 {
326 if (Globals.szFileName[0] == '\0')
327 DIALOG_FileSaveAs();
328 else
329 DoSaveFile();
330 }
331
332 static UINT_PTR CALLBACK DIALOG_FileSaveAs_Hook(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
333 {
334 WCHAR szText[128];
335 HWND hCombo;
336 OFNOTIFY *pNotify;
337
338 switch(msg)
339 {
340 case WM_INITDIALOG:
341 hCombo = GetDlgItem(hDlg, ID_ENCODING);
342
343 LoadString(Globals.hInstance, STRING_ANSI, szText, SIZEOF(szText));
344 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
345
346 LoadString(Globals.hInstance, STRING_UNICODE, szText, SIZEOF(szText));
347 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
348
349 LoadString(Globals.hInstance, STRING_UNICODE_BE, szText, SIZEOF(szText));
350 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
351
352 LoadString(Globals.hInstance, STRING_UTF8, szText, SIZEOF(szText));
353 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
354
355 SendMessage(hCombo, CB_SETCURSEL, Globals.iEncoding, 0);
356
357 hCombo = GetDlgItem(hDlg, ID_EOLN);
358
359 LoadString(Globals.hInstance, STRING_CRLF, szText, SIZEOF(szText));
360 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
361
362 LoadString(Globals.hInstance, STRING_LF, szText, SIZEOF(szText));
363 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
364
365 LoadString(Globals.hInstance, STRING_CR, szText, SIZEOF(szText));
366 SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
367
368 SendMessage(hCombo, CB_SETCURSEL, Globals.iEoln, 0);
369 break;
370
371 case WM_NOTIFY:
372 if (((NMHDR *) lParam)->code == CDN_FILEOK)
373 {
374 pNotify = (OFNOTIFY *) lParam;
375
376 hCombo = GetDlgItem(hDlg, ID_ENCODING);
377 if (hCombo)
378 Globals.iEncoding = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
379
380 hCombo = GetDlgItem(hDlg, ID_EOLN);
381 if (hCombo)
382 Globals.iEoln = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
383 }
384 break;
385 }
386 return 0;
387 }
388
389 VOID DIALOG_FileSaveAs(VOID)
390 {
391 OPENFILENAME saveas;
392 WCHAR szDir[MAX_PATH];
393 WCHAR szPath[MAX_PATH];
394 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
395 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
396
397 ZeroMemory(&saveas, sizeof(saveas));
398
399 GetCurrentDirectory(SIZEOF(szDir), szDir);
400 if (Globals.szFileName[0] == 0)
401 lstrcpy(szPath, txt_files);
402 else
403 lstrcpy(szPath, Globals.szFileName);
404
405 saveas.lStructSize = sizeof(OPENFILENAME);
406 saveas.hwndOwner = Globals.hMainWnd;
407 saveas.hInstance = Globals.hInstance;
408 saveas.lpstrFilter = Globals.szFilter;
409 saveas.lpstrFile = szPath;
410 saveas.nMaxFile = SIZEOF(szPath);
411 saveas.lpstrInitialDir = szDir;
412 saveas.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
413 OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
414 saveas.lpstrDefExt = szDefaultExt;
415 saveas.lpTemplateName = MAKEINTRESOURCE(DIALOG_ENCODING);
416 saveas.lpfnHook = DIALOG_FileSaveAs_Hook;
417
418 if (GetSaveFileName(&saveas)) {
419 SetFileName(szPath);
420 UpdateWindowCaption();
421 DoSaveFile();
422 }
423 }
424
425 VOID DIALOG_FilePrint(VOID)
426 {
427 DOCINFO di;
428 PRINTDLG printer;
429 SIZE szMetric;
430 int cWidthPels, cHeightPels, border;
431 int xLeft, yTop, pagecount, dopage, copycount;
432 unsigned int i;
433 LOGFONT hdrFont;
434 HFONT font, old_font=0;
435 DWORD size;
436 LPWSTR pTemp;
437 static const WCHAR times_new_romanW[] = { 'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0 };
438
439 /* Get a small font and print some header info on each page */
440 hdrFont.lfHeight = 100;
441 hdrFont.lfWidth = 0;
442 hdrFont.lfEscapement = 0;
443 hdrFont.lfOrientation = 0;
444 hdrFont.lfWeight = FW_BOLD;
445 hdrFont.lfItalic = 0;
446 hdrFont.lfUnderline = 0;
447 hdrFont.lfStrikeOut = 0;
448 hdrFont.lfCharSet = ANSI_CHARSET;
449 hdrFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
450 hdrFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
451 hdrFont.lfQuality = PROOF_QUALITY;
452 hdrFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
453 lstrcpy(hdrFont.lfFaceName, times_new_romanW);
454
455 font = CreateFontIndirect(&hdrFont);
456
457 /* Get Current Settings */
458 ZeroMemory(&printer, sizeof(printer));
459 printer.lStructSize = sizeof(printer);
460 printer.hwndOwner = Globals.hMainWnd;
461 printer.hInstance = Globals.hInstance;
462
463 /* Set some default flags */
464 printer.Flags = PD_RETURNDC;
465 printer.nFromPage = 0;
466 printer.nMinPage = 1;
467 /* we really need to calculate number of pages to set nMaxPage and nToPage */
468 printer.nToPage = 0;
469 printer.nMaxPage = -1;
470
471 /* Let commdlg manage copy settings */
472 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
473
474 if (!PrintDlg(&printer)) return;
475
476 assert(printer.hDC != 0);
477
478 /* initialize DOCINFO */
479 di.cbSize = sizeof(DOCINFO);
480 di.lpszDocName = Globals.szFileTitle;
481 di.lpszOutput = NULL;
482 di.lpszDatatype = NULL;
483 di.fwType = 0;
484
485 if (StartDoc(printer.hDC, &di) <= 0) return;
486
487 /* Get the page dimensions in pixels. */
488 cWidthPels = GetDeviceCaps(printer.hDC, HORZRES);
489 cHeightPels = GetDeviceCaps(printer.hDC, VERTRES);
490
491 /* Get the file text */
492 size = GetWindowTextLengthW(Globals.hEdit) + 1;
493 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
494 if (!pTemp)
495 {
496 ShowLastError();
497 return;
498 }
499 size = GetWindowTextW(Globals.hEdit, pTemp, size);
500
501 border = 150;
502 for (copycount=1; copycount <= printer.nCopies; copycount++) {
503 i = 0;
504 pagecount = 1;
505 do {
506 static const WCHAR letterM[] = { 'M',0 };
507
508 if (pagecount >= printer.nFromPage &&
509 /* ((printer.Flags & PD_PAGENUMS) == 0 || pagecount <= printer.nToPage))*/
510 pagecount <= printer.nToPage)
511 dopage = 1;
512 else
513 dopage = 0;
514
515 old_font = SelectObject(printer.hDC, font);
516 GetTextExtentPoint32(printer.hDC, letterM, 1, &szMetric);
517
518 if (dopage) {
519 if (StartPage(printer.hDC) <= 0) {
520 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
521 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
522 MessageBox(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
523 return;
524 }
525 /* Write a rectangle and header at the top of each page */
526 Rectangle(printer.hDC, border, border, cWidthPels-border, border+szMetric.cy*2);
527 /* I don't know what's up with this TextOut command. This comes out
528 kind of mangled.
529 */
530 TextOut(printer.hDC, border*2, border+szMetric.cy/2, Globals.szFileTitle, lstrlen(Globals.szFileTitle));
531 }
532
533 /* The starting point for the main text */
534 xLeft = border*2;
535 yTop = border+szMetric.cy*4;
536
537 SelectObject(printer.hDC, old_font);
538 GetTextExtentPoint32(printer.hDC, letterM, 1, &szMetric);
539
540 /* Since outputting strings is giving me problems, output the main
541 text one character at a time.
542 */
543 do {
544 if (pTemp[i] == '\n') {
545 xLeft = border*2;
546 yTop += szMetric.cy;
547 }
548 else if (pTemp[i] != '\r') {
549 if (dopage)
550 TextOut(printer.hDC, xLeft, yTop, &pTemp[i], 1);
551 xLeft += szMetric.cx;
552 }
553 } while (i++<size && yTop<(cHeightPels-border*2));
554
555 if (dopage)
556 EndPage(printer.hDC);
557 pagecount++;
558 } while (i<size);
559 }
560
561 EndDoc(printer.hDC);
562 DeleteDC(printer.hDC);
563 HeapFree(GetProcessHeap(), 0, pTemp);
564 }
565
566 VOID DIALOG_FilePrinterSetup(VOID)
567 {
568 PRINTDLG printer;
569
570 ZeroMemory(&printer, sizeof(printer));
571 printer.lStructSize = sizeof(printer);
572 printer.hwndOwner = Globals.hMainWnd;
573 printer.hInstance = Globals.hInstance;
574 printer.Flags = PD_PRINTSETUP;
575 printer.nCopies = 1;
576
577 PrintDlg(&printer);
578 }
579
580 VOID DIALOG_FileExit(VOID)
581 {
582 PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
583 }
584
585 VOID DIALOG_EditUndo(VOID)
586 {
587 SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
588 }
589
590 VOID DIALOG_EditCut(VOID)
591 {
592 SendMessage(Globals.hEdit, WM_CUT, 0, 0);
593 }
594
595 VOID DIALOG_EditCopy(VOID)
596 {
597 SendMessage(Globals.hEdit, WM_COPY, 0, 0);
598 }
599
600 VOID DIALOG_EditPaste(VOID)
601 {
602 SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
603 }
604
605 VOID DIALOG_EditDelete(VOID)
606 {
607 SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
608 }
609
610 VOID DIALOG_EditSelectAll(VOID)
611 {
612 SendMessage(Globals.hEdit, EM_SETSEL, 0, (LPARAM)-1);
613 }
614
615 VOID DIALOG_EditTimeDate(VOID)
616 {
617 SYSTEMTIME st;
618 WCHAR szDate[MAX_STRING_LEN];
619 static const WCHAR spaceW[] = { ' ',0 };
620
621 GetLocalTime(&st);
622
623 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
624 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
625
626 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
627
628 GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
629 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
630 }
631
632 VOID DIALOG_EditWrap(VOID)
633 {
634 static const WCHAR editW[] = { 'e','d','i','t',0 };
635 DWORD dwStyle;
636 RECT rc;
637 DWORD size;
638 LPWSTR pTemp;
639
640 Globals.bWrapLongLines = !Globals.bWrapLongLines;
641
642 size = GetWindowTextLength(Globals.hEdit) + 1;
643 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
644 if (!pTemp)
645 {
646 ShowLastError();
647 return;
648 }
649 GetWindowText(Globals.hEdit, pTemp, size);
650 DestroyWindow(Globals.hEdit);
651 GetClientRect(Globals.hMainWnd, &rc);
652 dwStyle = Globals.bWrapLongLines ? EDIT_STYLE_WRAP : EDIT_STYLE;
653 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
654 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
655 NULL, Globals.hInstance, NULL);
656 SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
657 SendMessage(Globals.hEdit, EM_LIMITTEXT, 0, 0);
658 SetWindowTextW(Globals.hEdit, pTemp);
659 SetFocus(Globals.hEdit);
660 HeapFree(GetProcessHeap(), 0, pTemp);
661 }
662
663 VOID DIALOG_SelectFont(VOID)
664 {
665 CHOOSEFONT cf;
666 LOGFONT lf=Globals.lfFont;
667
668 ZeroMemory( &cf, sizeof(cf) );
669 cf.lStructSize=sizeof(cf);
670 cf.hwndOwner=Globals.hMainWnd;
671 cf.lpLogFont=&lf;
672 cf.Flags=CF_SCREENFONTS;
673
674 if( ChooseFont(&cf) )
675 {
676 HFONT currfont=Globals.hFont;
677
678 Globals.hFont=CreateFontIndirect( &lf );
679 Globals.lfFont=lf;
680 SendMessage( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE );
681 if( currfont!=NULL )
682 DeleteObject( currfont );
683 }
684 }
685
686 typedef HWND (STDCALL *FINDPROC)(LPFINDREPLACE lpfr);
687
688 static VOID DIALOG_SearchDialog(FINDPROC pfnProc)
689 {
690 ZeroMemory(&Globals.find, sizeof(Globals.find));
691 Globals.find.lStructSize = sizeof(Globals.find);
692 Globals.find.hwndOwner = Globals.hMainWnd;
693 Globals.find.hInstance = Globals.hInstance;
694 Globals.find.lpstrFindWhat = Globals.szFindText;
695 Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
696 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
697 Globals.find.wReplaceWithLen = SIZEOF(Globals.szReplaceText);
698 Globals.find.Flags = FR_DOWN;
699
700 /* We only need to create the modal FindReplace dialog which will */
701 /* notify us of incoming events using hMainWnd Window Messages */
702
703 Globals.hFindReplaceDlg = pfnProc(&Globals.find);
704 assert(Globals.hFindReplaceDlg !=0);
705 }
706
707 VOID DIALOG_Search(VOID)
708 {
709 DIALOG_SearchDialog(FindText);
710 }
711
712 VOID DIALOG_SearchNext(VOID)
713 {
714 /* FIXME: Search Next */
715 DIALOG_Search();
716 }
717
718 VOID DIALOG_Replace(VOID)
719 {
720 DIALOG_SearchDialog(ReplaceText);
721 }
722
723 static INT_PTR CALLBACK DIALOG_GoTo_DialogProc(HWND hwndDialog, UINT uMsg, WPARAM wParam, LPARAM lParam)
724 {
725 BOOL bResult = FALSE;
726 HWND hTextBox;
727 TCHAR szText[32];
728
729 switch(uMsg) {
730 case WM_INITDIALOG:
731 hTextBox = GetDlgItem(hwndDialog, ID_LINENUMBER);
732 _sntprintf(szText, sizeof(szText) / sizeof(szText[0]), _T("%d"), lParam);
733 SetWindowText(hTextBox, szText);
734 break;
735 case WM_COMMAND:
736 if (HIWORD(wParam) == BN_CLICKED)
737 {
738 if (LOWORD(wParam) == IDOK)
739 {
740 hTextBox = GetDlgItem(hwndDialog, ID_LINENUMBER);
741 GetWindowText(hTextBox, szText, sizeof(szText) / sizeof(szText[0]));
742 EndDialog(hwndDialog, _ttoi(szText));
743 bResult = TRUE;
744 }
745 else if (LOWORD(wParam) == IDCANCEL)
746 {
747 EndDialog(hwndDialog, 0);
748 bResult = TRUE;
749 }
750 }
751 break;
752 }
753
754 return bResult;
755 }
756
757 VOID DIALOG_GoTo(VOID)
758 {
759 INT_PTR nLine;
760 LPTSTR pszText;
761 int nLength, i;
762 DWORD dwStart, dwEnd;
763
764 nLength = GetWindowTextLength(Globals.hEdit);
765 pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(*pszText));
766 if (!pszText)
767 return;
768
769 /* Retrieve current text */
770 GetWindowText(Globals.hEdit, pszText, nLength + 1);
771 SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwStart, (LPARAM) &dwEnd);
772
773 nLine = 1;
774 for (i = 0; pszText[i] && (i < dwStart); i++)
775 {
776 if (pszText[i] == '\n')
777 nLine++;
778 }
779
780 nLine = DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(DIALOG_GOTO),
781 Globals.hMainWnd, DIALOG_GoTo_DialogProc, nLine);
782
783 if (nLine >= 1)
784 {
785 for (i = 0; pszText[i] && (nLine > 1) && (i < nLength - 1); i++)
786 {
787 if (pszText[i] == '\n')
788 nLine--;
789 }
790 SendMessage(Globals.hEdit, EM_SETSEL, i, i);
791 SendMessage(Globals.hEdit, EM_SCROLLCARET, 0, 0);
792 }
793 HeapFree(GetProcessHeap(), 0, pszText);
794 }
795
796 VOID DIALOG_HelpContents(VOID)
797 {
798 WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
799 }
800
801 VOID DIALOG_HelpSearch(VOID)
802 {
803 /* Search Help */
804 }
805
806 VOID DIALOG_HelpHelp(VOID)
807 {
808 WinHelp(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
809 }
810
811 VOID DIALOG_HelpLicense(VOID)
812 {
813 WineLicense(Globals.hMainWnd);
814 }
815
816 VOID DIALOG_HelpNoWarranty(VOID)
817 {
818 WineWarranty(Globals.hMainWnd);
819 }
820
821 VOID DIALOG_HelpAboutWine(VOID)
822 {
823 static const WCHAR notepadW[] = { 'N','o','t','e','p','a','d','\n',0 };
824 WCHAR szNotepad[MAX_STRING_LEN];
825
826 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
827 ShellAbout(Globals.hMainWnd, szNotepad, notepadW, 0);
828 }
829
830
831 /***********************************************************************
832 *
833 * DIALOG_FilePageSetup
834 */
835 VOID DIALOG_FilePageSetup(void)
836 {
837 DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
838 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
839 }
840
841
842 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
843 *
844 * DIALOG_PAGESETUP_DlgProc
845 */
846
847 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
848 {
849
850 switch (msg)
851 {
852 case WM_COMMAND:
853 switch (wParam)
854 {
855 case IDOK:
856 /* save user input and close dialog */
857 GetDlgItemText(hDlg, 0x141, Globals.szHeader, SIZEOF(Globals.szHeader));
858 GetDlgItemText(hDlg, 0x143, Globals.szFooter, SIZEOF(Globals.szFooter));
859 GetDlgItemText(hDlg, 0x14A, Globals.szMarginTop, SIZEOF(Globals.szMarginTop));
860 GetDlgItemText(hDlg, 0x150, Globals.szMarginBottom, SIZEOF(Globals.szMarginBottom));
861 GetDlgItemText(hDlg, 0x147, Globals.szMarginLeft, SIZEOF(Globals.szMarginLeft));
862 GetDlgItemText(hDlg, 0x14D, Globals.szMarginRight, SIZEOF(Globals.szMarginRight));
863 EndDialog(hDlg, IDOK);
864 return TRUE;
865
866 case IDCANCEL:
867 /* discard user input and close dialog */
868 EndDialog(hDlg, IDCANCEL);
869 return TRUE;
870
871 case IDHELP:
872 {
873 /* FIXME: Bring this to work */
874 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 };
875 static const WCHAR helpW[] = { 'H','e','l','p',0 };
876 MessageBox(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
877 return TRUE;
878 }
879
880 default:
881 break;
882 }
883 break;
884
885 case WM_INITDIALOG:
886 /* fetch last user input prior to display dialog */
887 SetDlgItemText(hDlg, 0x141, Globals.szHeader);
888 SetDlgItemText(hDlg, 0x143, Globals.szFooter);
889 SetDlgItemText(hDlg, 0x14A, Globals.szMarginTop);
890 SetDlgItemText(hDlg, 0x150, Globals.szMarginBottom);
891 SetDlgItemText(hDlg, 0x147, Globals.szMarginLeft);
892 SetDlgItemText(hDlg, 0x14D, Globals.szMarginRight);
893 break;
894 }
895
896 return FALSE;
897 }