urlmon: Fixed misc.c tests on IE9 and make image_tiff_filter behave like IE9.
[wine/multimedia.git] / programs / notepad / dialog.c
blob94410bdb8ad67e7b328bb5bf4d81e4c054ccf300
1 /*
2 * Notepad (dialog.c)
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 * Copyright 2010 Vitaly Perov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <assert.h>
26 #include <stdio.h>
27 #include <windows.h>
28 #include <commdlg.h>
29 #include <shlwapi.h>
30 #include <winternl.h>
32 #include "main.h"
33 #include "dialog.h"
35 #define SPACES_IN_TAB 8
36 #define PRINT_LEN_MAX 500
38 static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
40 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
42 /* Swap bytes of WCHAR buffer (big-endian <-> little-endian). */
43 static inline void byteswap_wide_string(LPWSTR str, UINT num)
45 UINT i;
46 for (i = 0; i < num; i++) str[i] = RtlUshortByteSwap(str[i]);
49 static void load_encoding_name(ENCODING enc, WCHAR* buffer, int length)
51 switch (enc)
53 case ENCODING_UTF16LE:
54 LoadStringW(Globals.hInstance, STRING_UNICODE_LE, buffer, length);
55 break;
57 case ENCODING_UTF16BE:
58 LoadStringW(Globals.hInstance, STRING_UNICODE_BE, buffer, length);
59 break;
61 case ENCODING_UTF8:
62 LoadStringW(Globals.hInstance, STRING_UTF8, buffer, length);
63 break;
65 case ENCODING_ANSI:
67 CPINFOEXW cpi;
68 GetCPInfoExW(CP_ACP, 0, &cpi);
69 lstrcpynW(buffer, cpi.CodePageName, length);
70 break;
73 default:
74 assert(0 && "bad encoding in load_encoding_name");
75 break;
79 VOID ShowLastError(void)
81 DWORD error = GetLastError();
82 if (error != NO_ERROR)
84 LPWSTR lpMsgBuf;
85 WCHAR szTitle[MAX_STRING_LEN];
87 LoadStringW(Globals.hInstance, STRING_ERROR, szTitle, ARRAY_SIZE(szTitle));
88 FormatMessageW(
89 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
90 NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
91 MessageBoxW(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
92 LocalFree(lpMsgBuf);
96 /**
97 * Sets the caption of the main window according to Globals.szFileTitle:
98 * Untitled - Notepad if no file is open
99 * filename - Notepad if a file is given
101 void UpdateWindowCaption(void)
103 WCHAR szCaption[MAX_STRING_LEN];
104 WCHAR szNotepad[MAX_STRING_LEN];
105 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
107 if (Globals.szFileTitle[0] != '\0')
108 lstrcpyW(szCaption, Globals.szFileTitle);
109 else
110 LoadStringW(Globals.hInstance, STRING_UNTITLED, szCaption, ARRAY_SIZE(szCaption));
112 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
113 lstrcatW(szCaption, hyphenW);
114 lstrcatW(szCaption, szNotepad);
116 SetWindowTextW(Globals.hMainWnd, szCaption);
119 int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
121 WCHAR szMessage[MAX_STRING_LEN];
122 WCHAR szResource[MAX_STRING_LEN];
124 /* Load and format szMessage */
125 LoadStringW(Globals.hInstance, formatId, szResource, ARRAY_SIZE(szResource));
126 wnsprintfW(szMessage, ARRAY_SIZE(szMessage), szResource, szString);
128 /* Load szCaption */
129 if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
130 LoadStringW(Globals.hInstance, STRING_ERROR, szResource, ARRAY_SIZE(szResource));
131 else
132 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szResource, ARRAY_SIZE(szResource));
134 /* Display Modal Dialog */
135 if (hParent == NULL)
136 hParent = Globals.hMainWnd;
137 return MessageBoxW(hParent, szMessage, szResource, dwFlags);
140 static void AlertFileNotFound(LPCWSTR szFileName)
142 DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
145 static int AlertFileNotSaved(LPCWSTR szFileName)
147 WCHAR szUntitled[MAX_STRING_LEN];
149 LoadStringW(Globals.hInstance, STRING_UNTITLED, szUntitled, ARRAY_SIZE(szUntitled));
150 return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
151 MB_ICONQUESTION|MB_YESNOCANCEL);
154 static int AlertUnicodeCharactersLost(LPCWSTR szFileName)
156 WCHAR szMsgFormat[MAX_STRING_LEN];
157 WCHAR szEnc[MAX_STRING_LEN];
158 WCHAR szMsg[ARRAY_SIZE(szMsgFormat) + MAX_PATH + ARRAY_SIZE(szEnc)];
159 WCHAR szCaption[MAX_STRING_LEN];
161 LoadStringW(Globals.hInstance, STRING_LOSS_OF_UNICODE_CHARACTERS,
162 szMsgFormat, ARRAY_SIZE(szMsgFormat));
163 load_encoding_name(ENCODING_ANSI, szEnc, ARRAY_SIZE(szEnc));
164 wnsprintfW(szMsg, ARRAY_SIZE(szMsg), szMsgFormat, szFileName, szEnc);
165 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szCaption,
166 ARRAY_SIZE(szCaption));
167 return MessageBoxW(Globals.hMainWnd, szMsg, szCaption,
168 MB_OKCANCEL|MB_ICONEXCLAMATION);
172 * Returns:
173 * TRUE - if file exists
174 * FALSE - if file does not exist
176 BOOL FileExists(LPCWSTR szFilename)
178 WIN32_FIND_DATAW entry;
179 HANDLE hFile;
181 hFile = FindFirstFileW(szFilename, &entry);
182 FindClose(hFile);
184 return (hFile != INVALID_HANDLE_VALUE);
187 static inline BOOL is_conversion_to_ansi_lossy(LPCWSTR textW, int lenW)
189 BOOL ret = FALSE;
190 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, textW, lenW, NULL, 0,
191 NULL, &ret);
192 return ret;
195 typedef enum
197 SAVED_OK,
198 SAVE_FAILED,
199 SHOW_SAVEAS_DIALOG
200 } SAVE_STATUS;
202 /* szFileName is the filename to save under; enc is the encoding to use.
204 * If the function succeeds, it returns SAVED_OK.
205 * If the function fails, it returns SAVE_FAILED.
206 * If Unicode data could be lost due to conversion to a non-Unicode character
207 * set, a warning is displayed. The user can continue (and the function carries
208 * on), or cancel (and the function returns SHOW_SAVEAS_DIALOG).
210 static SAVE_STATUS DoSaveFile(LPCWSTR szFileName, ENCODING enc)
212 int lenW;
213 WCHAR* textW;
214 HANDLE hFile;
215 DWORD dwNumWrite;
216 PVOID pBytes;
217 DWORD size;
219 /* lenW includes the byte-order mark, but not the \0. */
220 lenW = GetWindowTextLengthW(Globals.hEdit) + 1;
221 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
222 if (!textW)
224 ShowLastError();
225 return SAVE_FAILED;
227 textW[0] = (WCHAR) 0xfeff;
228 lenW = GetWindowTextW(Globals.hEdit, textW+1, lenW) + 1;
230 switch (enc)
232 case ENCODING_UTF16BE:
233 byteswap_wide_string(textW, lenW);
234 /* fall through */
236 case ENCODING_UTF16LE:
237 size = lenW * sizeof(WCHAR);
238 pBytes = textW;
239 break;
241 case ENCODING_UTF8:
242 size = WideCharToMultiByte(CP_UTF8, 0, textW, lenW, NULL, 0, NULL, NULL);
243 pBytes = HeapAlloc(GetProcessHeap(), 0, size);
244 if (!pBytes)
246 ShowLastError();
247 HeapFree(GetProcessHeap(), 0, textW);
248 return SAVE_FAILED;
250 WideCharToMultiByte(CP_UTF8, 0, textW, lenW, pBytes, size, NULL, NULL);
251 HeapFree(GetProcessHeap(), 0, textW);
252 break;
254 default:
255 if (is_conversion_to_ansi_lossy(textW+1, lenW-1)
256 && AlertUnicodeCharactersLost(szFileName) == IDCANCEL)
258 HeapFree(GetProcessHeap(), 0, textW);
259 return SHOW_SAVEAS_DIALOG;
262 size = WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, NULL, 0, NULL, NULL);
263 pBytes = HeapAlloc(GetProcessHeap(), 0, size);
264 if (!pBytes)
266 ShowLastError();
267 HeapFree(GetProcessHeap(), 0, textW);
268 return SAVE_FAILED;
270 WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, pBytes, size, NULL, NULL);
271 HeapFree(GetProcessHeap(), 0, textW);
272 break;
275 hFile = CreateFileW(szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
276 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
277 if(hFile == INVALID_HANDLE_VALUE)
279 ShowLastError();
280 HeapFree(GetProcessHeap(), 0, pBytes);
281 return SAVE_FAILED;
283 if (!WriteFile(hFile, pBytes, size, &dwNumWrite, NULL))
285 ShowLastError();
286 CloseHandle(hFile);
287 HeapFree(GetProcessHeap(), 0, pBytes);
288 return SAVE_FAILED;
290 SetEndOfFile(hFile);
291 CloseHandle(hFile);
292 HeapFree(GetProcessHeap(), 0, pBytes);
294 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
295 return SAVED_OK;
299 * Returns:
300 * TRUE - User agreed to close (both save/don't save)
301 * FALSE - User cancelled close by selecting "Cancel"
303 BOOL DoCloseFile(void)
305 int nResult;
306 static const WCHAR empty_strW[] = { 0 };
308 nResult=GetWindowTextLengthW(Globals.hEdit);
309 if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0) &&
310 (nResult || Globals.szFileName[0]))
312 /* prompt user to save changes */
313 nResult = AlertFileNotSaved(Globals.szFileName);
314 switch (nResult) {
315 case IDYES: return DIALOG_FileSave();
317 case IDNO: break;
319 case IDCANCEL: return(FALSE);
321 default: return(FALSE);
322 } /* switch */
323 } /* if */
325 SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
327 UpdateWindowCaption();
328 return(TRUE);
331 static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size)
333 static const char bom_utf8[] = { 0xef, 0xbb, 0xbf };
334 if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
335 return ENCODING_UTF8;
336 else
338 int flags = IS_TEXT_UNICODE_SIGNATURE |
339 IS_TEXT_UNICODE_REVERSE_SIGNATURE |
340 IS_TEXT_UNICODE_ODD_LENGTH;
341 IsTextUnicode(buffer, size, &flags);
342 if (flags & IS_TEXT_UNICODE_SIGNATURE)
343 return ENCODING_UTF16LE;
344 else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
345 return ENCODING_UTF16BE;
346 else
347 return ENCODING_ANSI;
351 void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
353 static const WCHAR dotlog[] = { '.','L','O','G',0 };
354 HANDLE hFile;
355 LPSTR pTemp;
356 DWORD size;
357 DWORD dwNumRead;
358 int lenW;
359 WCHAR* textW;
360 int i;
361 WCHAR log[5];
363 /* Close any files and prompt to save changes */
364 if (!DoCloseFile())
365 return;
367 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
368 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
369 if(hFile == INVALID_HANDLE_VALUE)
371 AlertFileNotFound(szFileName);
372 return;
375 size = GetFileSize(hFile, NULL);
376 if (size == INVALID_FILE_SIZE)
378 CloseHandle(hFile);
379 ShowLastError();
380 return;
383 /* Extra memory for (WCHAR)'\0'-termination. */
384 pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
385 if (!pTemp)
387 CloseHandle(hFile);
388 ShowLastError();
389 return;
392 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
394 CloseHandle(hFile);
395 HeapFree(GetProcessHeap(), 0, pTemp);
396 ShowLastError();
397 return;
400 CloseHandle(hFile);
402 size = dwNumRead;
404 if (enc == ENCODING_AUTO)
405 enc = detect_encoding_of_buffer(pTemp, size);
406 else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
408 /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
409 * override the selection (like native Notepad).
411 if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
412 enc = ENCODING_UTF16LE;
413 else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
414 enc = ENCODING_UTF16BE;
417 switch (enc)
419 case ENCODING_UTF16BE:
420 byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
421 /* Forget whether the file is BE or LE, like native Notepad. */
422 enc = ENCODING_UTF16LE;
424 /* fall through */
426 case ENCODING_UTF16LE:
427 textW = (LPWSTR)pTemp;
428 lenW = size/sizeof(WCHAR);
429 break;
431 default:
433 int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
434 lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
435 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
436 if (!textW)
438 ShowLastError();
439 HeapFree(GetProcessHeap(), 0, pTemp);
440 return;
442 MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
443 HeapFree(GetProcessHeap(), 0, pTemp);
444 break;
448 /* Replace '\0's with spaces. Other than creating a custom control that
449 * can deal with '\0' characters, it's the best that can be done.
451 for (i = 0; i < lenW; i++)
452 if (textW[i] == '\0')
453 textW[i] = ' ';
454 textW[lenW] = '\0';
456 if (lenW >= 1 && textW[0] == 0xfeff)
457 SetWindowTextW(Globals.hEdit, textW+1);
458 else
459 SetWindowTextW(Globals.hEdit, textW);
461 HeapFree(GetProcessHeap(), 0, textW);
463 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
464 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
465 SetFocus(Globals.hEdit);
467 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
468 if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
470 static const WCHAR lfW[] = { '\r','\n',0 };
471 SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
472 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
473 DIALOG_EditTimeDate();
474 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
477 SetFileNameAndEncoding(szFileName, enc);
478 UpdateWindowCaption();
481 VOID DIALOG_FileNew(VOID)
483 static const WCHAR empty_strW[] = { 0 };
485 /* Close any files and prompt to save changes */
486 if (DoCloseFile()) {
487 SetWindowTextW(Globals.hEdit, empty_strW);
488 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
489 SetFocus(Globals.hEdit);
493 /* Used to detect encoding of files selected in Open dialog.
494 * Returns ENCODING_AUTO if file can't be read, etc.
496 static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
498 DWORD size;
499 HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
500 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
501 if (hFile == INVALID_HANDLE_VALUE)
502 return ENCODING_AUTO;
503 size = GetFileSize(hFile, NULL);
504 if (size == INVALID_FILE_SIZE)
506 CloseHandle(hFile);
507 return ENCODING_AUTO;
509 else
511 DWORD dwNumRead;
512 BYTE buffer[MAX_STRING_LEN];
513 if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
515 CloseHandle(hFile);
516 return ENCODING_AUTO;
518 CloseHandle(hFile);
519 return detect_encoding_of_buffer(buffer, dwNumRead);
523 static LPWSTR dialog_print_to_file(HWND hMainWnd)
525 OPENFILENAMEW ofn;
526 static WCHAR file[MAX_PATH] = {'o','u','t','p','u','t','.','p','r','n',0};
527 static const WCHAR defExt[] = {'p','r','n',0};
529 ZeroMemory(&ofn, sizeof(ofn));
531 ofn.lStructSize = sizeof(ofn);
532 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
533 ofn.hwndOwner = hMainWnd;
534 ofn.lpstrFile = file;
535 ofn.nMaxFile = MAX_PATH;
536 ofn.lpstrDefExt = defExt;
538 if(GetSaveFileNameW(&ofn))
539 return file;
540 else
541 return FALSE;
543 static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
545 static HWND hEncCombo;
547 switch (uMsg)
549 case WM_INITDIALOG:
551 ENCODING enc;
552 hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
553 for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
555 WCHAR szEnc[MAX_STRING_LEN];
556 load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
557 SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
559 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
561 break;
563 case WM_COMMAND:
564 if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
565 HIWORD(wParam) == CBN_SELCHANGE)
567 int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
568 Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
571 break;
573 case WM_NOTIFY:
574 switch (((OFNOTIFYW*)lParam)->hdr.code)
576 case CDN_SELCHANGE:
577 if (Globals.bOfnIsOpenDialog)
579 /* Check the start of the selected file for a BOM. */
580 ENCODING enc;
581 WCHAR szFileName[MAX_PATH];
582 SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
583 ARRAY_SIZE(szFileName), (LPARAM)szFileName);
584 enc = detect_encoding_of_file(szFileName);
585 if (enc != ENCODING_AUTO)
587 Globals.encOfnCombo = enc;
588 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
591 break;
593 default:
594 break;
596 break;
598 default:
599 break;
601 return 0;
604 VOID DIALOG_FileOpen(VOID)
606 OPENFILENAMEW openfilename;
607 WCHAR szPath[MAX_PATH];
608 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
609 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
611 ZeroMemory(&openfilename, sizeof(openfilename));
613 lstrcpyW(szPath, txt_files);
615 openfilename.lStructSize = sizeof(openfilename);
616 openfilename.hwndOwner = Globals.hMainWnd;
617 openfilename.hInstance = Globals.hInstance;
618 openfilename.lpstrFilter = Globals.szFilter;
619 openfilename.lpstrFile = szPath;
620 openfilename.nMaxFile = ARRAY_SIZE(szPath);
621 openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
622 OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
623 OFN_HIDEREADONLY | OFN_ENABLESIZING;
624 openfilename.lpfnHook = OfnHookProc;
625 openfilename.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
626 openfilename.lpstrDefExt = szDefaultExt;
628 Globals.encOfnCombo = ENCODING_ANSI;
629 Globals.bOfnIsOpenDialog = TRUE;
631 if (GetOpenFileNameW(&openfilename))
632 DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
635 /* Return FALSE to cancel close */
636 BOOL DIALOG_FileSave(VOID)
638 if (Globals.szFileName[0] == '\0')
639 return DIALOG_FileSaveAs();
640 else
642 switch (DoSaveFile(Globals.szFileName, Globals.encFile))
644 case SAVED_OK: return TRUE;
645 case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
646 default: return FALSE;
651 BOOL DIALOG_FileSaveAs(VOID)
653 OPENFILENAMEW saveas;
654 WCHAR szPath[MAX_PATH];
655 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
656 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
658 ZeroMemory(&saveas, sizeof(saveas));
660 lstrcpyW(szPath, txt_files);
662 saveas.lStructSize = sizeof(OPENFILENAMEW);
663 saveas.hwndOwner = Globals.hMainWnd;
664 saveas.hInstance = Globals.hInstance;
665 saveas.lpstrFilter = Globals.szFilter;
666 saveas.lpstrFile = szPath;
667 saveas.nMaxFile = ARRAY_SIZE(szPath);
668 saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
669 OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
670 OFN_HIDEREADONLY | OFN_ENABLESIZING;
671 saveas.lpfnHook = OfnHookProc;
672 saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
673 saveas.lpstrDefExt = szDefaultExt;
675 /* Preset encoding to what file was opened/saved last with. */
676 Globals.encOfnCombo = Globals.encFile;
677 Globals.bOfnIsOpenDialog = FALSE;
679 retry:
680 if (!GetSaveFileNameW(&saveas))
681 return FALSE;
683 switch (DoSaveFile(szPath, Globals.encOfnCombo))
685 case SAVED_OK:
686 SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
687 UpdateWindowCaption();
688 return TRUE;
690 case SHOW_SAVEAS_DIALOG:
691 goto retry;
693 default:
694 return FALSE;
698 typedef struct {
699 LPWSTR mptr;
700 LPWSTR mend;
701 LPWSTR lptr;
702 DWORD len;
703 } TEXTINFO, *LPTEXTINFO;
705 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
707 SIZE szMetric;
709 if (*text)
711 /* Write the header or footer */
712 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
713 if (dopage)
714 ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
715 header ? rc->top : rc->bottom - szMetric.cy,
716 ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
717 return 1;
719 return 0;
722 static WCHAR *expand_header_vars(WCHAR *pattern, int page)
724 int length = 0;
725 int i;
726 BOOL inside = FALSE;
727 WCHAR *buffer = NULL;
729 for (i = 0; pattern[i]; i++)
731 if (inside)
733 if (pattern[i] == '&')
734 length++;
735 else if (pattern[i] == 'p')
736 length += 11;
737 inside = FALSE;
739 else if (pattern[i] == '&')
740 inside = TRUE;
741 else
742 length++;
745 buffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
746 if (buffer)
748 int j = 0;
749 inside = FALSE;
750 for (i = 0; pattern[i]; i++)
752 if (inside)
754 if (pattern[i] == '&')
755 buffer[j++] = '&';
756 else if (pattern[i] == 'p')
758 static const WCHAR percent_dW[] = {'%','d',0};
759 j += wnsprintfW(&buffer[j], 11, percent_dW, page);
761 inside = FALSE;
763 else if (pattern[i] == '&')
764 inside = TRUE;
765 else
766 buffer[j++] = pattern[i];
768 buffer[j++] = 0;
770 return buffer;
773 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
775 int b, y;
776 TEXTMETRICW tm;
777 SIZE szMetrics;
778 WCHAR *footer_text = NULL;
780 footer_text = expand_header_vars(Globals.szFooter, page);
781 if (footer_text == NULL)
782 return FALSE;
784 if (dopage)
786 if (StartPage(hdc) <= 0)
788 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
789 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
790 MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
791 HeapFree(GetProcessHeap(), 0, footer_text);
792 return FALSE;
796 GetTextMetricsW(hdc, &tm);
797 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
798 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, footer_text) * tm.tmHeight;
800 do {
801 INT m, n;
803 if (!tInfo->len)
805 /* find the end of the line */
806 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
808 if (*tInfo->mptr == '\t')
810 /* replace tabs with spaces */
811 for (m = 0; m < SPACES_IN_TAB; m++)
813 if (tInfo->len < PRINT_LEN_MAX)
814 tInfo->lptr[tInfo->len++] = ' ';
815 else if (Globals.bWrapLongLines)
816 break;
819 else if (tInfo->len < PRINT_LEN_MAX)
820 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
822 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
823 break;
825 tInfo->mptr++;
829 /* Find out how much we should print if line wrapping is enabled */
830 if (Globals.bWrapLongLines)
832 GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
833 if (n < tInfo->len && tInfo->lptr[n] != ' ')
835 m = n;
836 /* Don't wrap words unless it's a single word over the entire line */
837 while (m && tInfo->lptr[m] != ' ') m--;
838 if (m > 0) n = m + 1;
841 else
842 n = tInfo->len;
844 if (dopage)
845 ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
847 tInfo->len -= n;
849 if (tInfo->len)
851 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
852 y += tm.tmHeight + tm.tmExternalLeading;
854 else
856 /* find the next line */
857 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
859 if (*tInfo->mptr == '\n')
860 y += tm.tmHeight + tm.tmExternalLeading;
861 tInfo->mptr++;
864 } while (tInfo->mptr < tInfo->mend && y < b);
866 notepad_print_header(hdc, rc, dopage, FALSE, page, footer_text);
867 if (dopage)
869 EndPage(hdc);
871 HeapFree(GetProcessHeap(), 0, footer_text);
872 return TRUE;
875 VOID DIALOG_FilePrint(VOID)
877 DOCINFOW di;
878 PRINTDLGW printer;
879 int page, dopage, copy;
880 LOGFONTW lfFont;
881 HFONT hTextFont, old_font = 0;
882 DWORD size;
883 BOOL ret = FALSE;
884 RECT rc;
885 LPWSTR pTemp;
886 TEXTINFO tInfo;
887 WCHAR cTemp[PRINT_LEN_MAX];
889 /* Get Current Settings */
890 ZeroMemory(&printer, sizeof(printer));
891 printer.lStructSize = sizeof(printer);
892 printer.hwndOwner = Globals.hMainWnd;
893 printer.hDevMode = Globals.hDevMode;
894 printer.hDevNames = Globals.hDevNames;
895 printer.hInstance = Globals.hInstance;
897 /* Set some default flags */
898 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
899 printer.nFromPage = 0;
900 printer.nMinPage = 1;
901 /* we really need to calculate number of pages to set nMaxPage and nToPage */
902 printer.nToPage = 0;
903 printer.nMaxPage = -1;
904 /* Let commdlg manage copy settings */
905 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
907 if (!PrintDlgW(&printer)) return;
909 Globals.hDevMode = printer.hDevMode;
910 Globals.hDevNames = printer.hDevNames;
912 SetMapMode(printer.hDC, MM_TEXT);
914 /* initialize DOCINFO */
915 di.cbSize = sizeof(DOCINFOW);
916 di.lpszDocName = Globals.szFileTitle;
917 di.lpszOutput = NULL;
918 di.lpszDatatype = NULL;
919 di.fwType = 0;
921 if(printer.Flags & PD_PRINTTOFILE)
923 di.lpszOutput = dialog_print_to_file(printer.hwndOwner);
924 if(!di.lpszOutput)
925 return;
928 /* Get the file text */
929 size = GetWindowTextLengthW(Globals.hEdit) + 1;
930 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
931 if (!pTemp)
933 DeleteDC(printer.hDC);
934 ShowLastError();
935 return;
937 size = GetWindowTextW(Globals.hEdit, pTemp, size);
939 if (StartDocW(printer.hDC, &di) > 0)
941 /* Get the page margins in pixels. */
942 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
943 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
944 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
945 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
946 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
947 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
948 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
949 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
951 /* Create a font for the printer resolution */
952 lfFont = Globals.lfFont;
953 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
954 /* Make the font a bit lighter */
955 lfFont.lfWeight -= 100;
956 hTextFont = CreateFontIndirectW(&lfFont);
957 old_font = SelectObject(printer.hDC, hTextFont);
959 for (copy = 1; copy <= printer.nCopies; copy++)
961 page = 1;
963 tInfo.mptr = pTemp;
964 tInfo.mend = pTemp + size;
965 tInfo.lptr = cTemp;
966 tInfo.len = 0;
968 do {
969 if (printer.Flags & PD_PAGENUMS)
971 /* a specific range of pages is selected, so
972 * skip pages that are not to be printed
974 if (page > printer.nToPage)
975 break;
976 else if (page >= printer.nFromPage)
977 dopage = 1;
978 else
979 dopage = 0;
981 else
982 dopage = 1;
984 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
985 page++;
986 } while (ret && tInfo.mptr < tInfo.mend);
988 if (!ret) break;
990 EndDoc(printer.hDC);
991 SelectObject(printer.hDC, old_font);
992 DeleteObject(hTextFont);
994 DeleteDC(printer.hDC);
995 HeapFree(GetProcessHeap(), 0, pTemp);
998 VOID DIALOG_FilePrinterSetup(VOID)
1000 PRINTDLGW printer;
1002 ZeroMemory(&printer, sizeof(printer));
1003 printer.lStructSize = sizeof(printer);
1004 printer.hwndOwner = Globals.hMainWnd;
1005 printer.hDevMode = Globals.hDevMode;
1006 printer.hDevNames = Globals.hDevNames;
1007 printer.hInstance = Globals.hInstance;
1008 printer.Flags = PD_PRINTSETUP;
1009 printer.nCopies = 1;
1011 PrintDlgW(&printer);
1013 Globals.hDevMode = printer.hDevMode;
1014 Globals.hDevNames = printer.hDevNames;
1017 VOID DIALOG_FileExit(VOID)
1019 PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
1022 VOID DIALOG_EditUndo(VOID)
1024 SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
1027 VOID DIALOG_EditCut(VOID)
1029 SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
1032 VOID DIALOG_EditCopy(VOID)
1034 SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
1037 VOID DIALOG_EditPaste(VOID)
1039 SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
1042 VOID DIALOG_EditDelete(VOID)
1044 SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
1047 VOID DIALOG_EditSelectAll(VOID)
1049 SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
1052 VOID DIALOG_EditTimeDate(VOID)
1054 SYSTEMTIME st;
1055 WCHAR szDate[MAX_STRING_LEN];
1056 static const WCHAR spaceW[] = { ' ',0 };
1058 GetLocalTime(&st);
1060 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
1061 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
1063 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
1065 GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
1066 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
1069 VOID DIALOG_EditWrap(VOID)
1071 BOOL modify = FALSE;
1072 static const WCHAR editW[] = { 'e','d','i','t',0 };
1073 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
1074 ES_AUTOVSCROLL | ES_MULTILINE;
1075 RECT rc;
1076 DWORD size;
1077 LPWSTR pTemp;
1079 size = GetWindowTextLengthW(Globals.hEdit) + 1;
1080 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1081 if (!pTemp)
1083 ShowLastError();
1084 return;
1086 GetWindowTextW(Globals.hEdit, pTemp, size);
1087 modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
1088 DestroyWindow(Globals.hEdit);
1089 GetClientRect(Globals.hMainWnd, &rc);
1090 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
1091 Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
1092 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
1093 NULL, Globals.hInstance, NULL);
1094 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
1095 SetWindowTextW(Globals.hEdit, pTemp);
1096 SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
1097 SetFocus(Globals.hEdit);
1098 HeapFree(GetProcessHeap(), 0, pTemp);
1100 Globals.bWrapLongLines = !Globals.bWrapLongLines;
1101 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
1102 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
1105 VOID DIALOG_SelectFont(VOID)
1107 CHOOSEFONTW cf;
1108 LOGFONTW lf=Globals.lfFont;
1110 ZeroMemory( &cf, sizeof(cf) );
1111 cf.lStructSize=sizeof(cf);
1112 cf.hwndOwner=Globals.hMainWnd;
1113 cf.lpLogFont=&lf;
1114 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
1116 if( ChooseFontW(&cf) )
1118 HFONT currfont=Globals.hFont;
1120 Globals.hFont=CreateFontIndirectW( &lf );
1121 Globals.lfFont=lf;
1122 SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
1123 if( currfont!=NULL )
1124 DeleteObject( currfont );
1128 VOID DIALOG_Search(VOID)
1130 /* Allow only one search/replace dialog to open */
1131 if(Globals.hFindReplaceDlg != NULL)
1133 SetActiveWindow(Globals.hFindReplaceDlg);
1134 return;
1137 ZeroMemory(&Globals.find, sizeof(Globals.find));
1138 Globals.find.lStructSize = sizeof(Globals.find);
1139 Globals.find.hwndOwner = Globals.hMainWnd;
1140 Globals.find.hInstance = Globals.hInstance;
1141 Globals.find.lpstrFindWhat = Globals.szFindText;
1142 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1143 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1145 /* We only need to create the modal FindReplace dialog which will */
1146 /* notify us of incoming events using hMainWnd Window Messages */
1148 Globals.hFindReplaceDlg = FindTextW(&Globals.find);
1149 assert(Globals.hFindReplaceDlg !=0);
1152 VOID DIALOG_SearchNext(VOID)
1154 if (Globals.lastFind.lpstrFindWhat == NULL)
1155 DIALOG_Search();
1156 else /* use the last find data */
1157 NOTEPAD_DoFind(&Globals.lastFind);
1160 VOID DIALOG_Replace(VOID)
1162 /* Allow only one search/replace dialog to open */
1163 if(Globals.hFindReplaceDlg != NULL)
1165 SetActiveWindow(Globals.hFindReplaceDlg);
1166 return;
1169 ZeroMemory(&Globals.find, sizeof(Globals.find));
1170 Globals.find.lStructSize = sizeof(Globals.find);
1171 Globals.find.hwndOwner = Globals.hMainWnd;
1172 Globals.find.hInstance = Globals.hInstance;
1173 Globals.find.lpstrFindWhat = Globals.szFindText;
1174 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1175 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
1176 Globals.find.wReplaceWithLen = ARRAY_SIZE(Globals.szReplaceText);
1177 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1179 /* We only need to create the modal FindReplace dialog which will */
1180 /* notify us of incoming events using hMainWnd Window Messages */
1182 Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
1183 assert(Globals.hFindReplaceDlg !=0);
1186 VOID DIALOG_HelpContents(VOID)
1188 WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
1191 VOID DIALOG_HelpAboutNotepad(VOID)
1193 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1194 WCHAR szNotepad[MAX_STRING_LEN];
1195 HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
1196 IMAGE_ICON, 48, 48, LR_SHARED);
1198 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
1199 ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
1203 /***********************************************************************
1205 * DIALOG_FilePageSetup
1207 VOID DIALOG_FilePageSetup(void)
1209 DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
1210 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
1214 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1216 * DIALOG_PAGESETUP_DlgProc
1219 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1222 switch (msg)
1224 case WM_COMMAND:
1225 switch (wParam)
1227 case IDOK:
1228 /* save user input and close dialog */
1229 GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
1230 GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
1232 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
1233 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
1234 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
1235 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
1236 EndDialog(hDlg, IDOK);
1237 return TRUE;
1239 case IDCANCEL:
1240 /* discard user input and close dialog */
1241 EndDialog(hDlg, IDCANCEL);
1242 return TRUE;
1244 case IDHELP:
1246 /* FIXME: Bring this to work */
1247 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 };
1248 static const WCHAR helpW[] = { 'H','e','l','p',0 };
1249 MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
1250 return TRUE;
1253 default:
1254 break;
1256 break;
1258 case WM_INITDIALOG:
1259 /* fetch last user input prior to display dialog */
1260 SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
1261 SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
1262 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
1263 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
1264 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
1265 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
1266 break;
1269 return FALSE;