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