blob: e19e4d6489714bc0cb38c7c9ffdec576002f33ef [file] [log] [blame]
Alexandre Julliard03468f71998-02-15 19:40:49 +00001/*
Marcel Baura43295d1998-10-18 14:11:42 +00002 * Notepad (dialog.c)
Alexandre Julliard03468f71998-02-15 19:40:49 +00003 *
Marcel Baur03287451999-02-14 11:28:37 +00004 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
Sylvain Petreolleeaa8df62002-03-20 22:55:46 +00005 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
Andriy Palamarchukc55dce02002-07-08 19:41:09 +00006 * Copyright 2002 Andriy Palamarchuk
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +02007 * Copyright 2007 Rolf Kalbermatter
Vitaly Perovd4e66542010-02-25 19:36:04 +03008 * Copyright 2010 Vitaly Perov
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00009 *
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.
14 *
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.
19 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020022 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard03468f71998-02-15 19:40:49 +000023 */
24
Ulrich Weigand97d05c82000-01-29 22:07:03 +000025#include <assert.h>
Alexandre Julliard03468f71998-02-15 19:40:49 +000026#include <stdio.h>
Marcel Baura43295d1998-10-18 14:11:42 +000027#include <windows.h>
28#include <commdlg.h>
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +020029#include <shlwapi.h>
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +010030#include <winternl.h>
Marcel Baura43295d1998-10-18 14:11:42 +000031
Alexandre Julliard03468f71998-02-15 19:40:49 +000032#include "main.h"
Alexandre Julliard03468f71998-02-15 19:40:49 +000033#include "dialog.h"
Marcel Baura43295d1998-10-18 14:11:42 +000034
Duane Clarka60ee9c2006-07-16 16:20:30 -070035#define SPACES_IN_TAB 8
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +020036#define PRINT_LEN_MAX 500
Duane Clarka60ee9c2006-07-16 16:20:30 -070037
Dmitry Timoshkov398af162003-07-21 20:05:33 +000038static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
Alexandre Julliard03468f71998-02-15 19:40:49 +000039
Dmitry Timoshkov398af162003-07-21 20:05:33 +000040static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +000041
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +010042/* Swap bytes of WCHAR buffer (big-endian <-> little-endian). */
43static inline void byteswap_wide_string(LPWSTR str, UINT num)
44{
45 UINT i;
46 for (i = 0; i < num; i++) str[i] = RtlUshortByteSwap(str[i]);
47}
48
Alexander Scott-Johns67766392009-06-29 23:50:39 +010049static void load_encoding_name(ENCODING enc, WCHAR* buffer, int length)
50{
51 switch (enc)
52 {
53 case ENCODING_UTF16LE:
54 LoadStringW(Globals.hInstance, STRING_UNICODE_LE, buffer, length);
55 break;
56
57 case ENCODING_UTF16BE:
58 LoadStringW(Globals.hInstance, STRING_UNICODE_BE, buffer, length);
59 break;
60
61 default:
62 {
63 CPINFOEXW cpi;
64 GetCPInfoExW((enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP, 0, &cpi);
65 lstrcpynW(buffer, cpi.CodePageName, length);
66 break;
67 }
68 }
69}
70
Dmitry Timoshkov398af162003-07-21 20:05:33 +000071VOID ShowLastError(void)
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000072{
73 DWORD error = GetLastError();
74 if (error != NO_ERROR)
75 {
Dmitry Timoshkov398af162003-07-21 20:05:33 +000076 LPWSTR lpMsgBuf;
77 WCHAR szTitle[MAX_STRING_LEN];
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000078
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +020079 LoadStringW(Globals.hInstance, STRING_ERROR, szTitle, ARRAY_SIZE(szTitle));
Michael Stefaniuc27c88392009-05-18 21:08:20 +020080 FormatMessageW(
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000081 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +020082 NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
Michael Stefaniuc27c88392009-05-18 21:08:20 +020083 MessageBoxW(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000084 LocalFree(lpMsgBuf);
85 }
86}
87
88/**
89 * Sets the caption of the main window according to Globals.szFileTitle:
Thomas Kho3ec165f2006-07-12 14:15:31 -070090 * Untitled - Notepad if no file is open
91 * filename - Notepad if a file is given
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000092 */
Alexander Scott-Johns42729bc2009-06-30 21:25:02 +010093void UpdateWindowCaption(void)
Dmitry Timoshkov398af162003-07-21 20:05:33 +000094{
95 WCHAR szCaption[MAX_STRING_LEN];
Thomas Kho3ec165f2006-07-12 14:15:31 -070096 WCHAR szNotepad[MAX_STRING_LEN];
97 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000098
Thomas Kho3ec165f2006-07-12 14:15:31 -070099 if (Globals.szFileTitle[0] != '\0')
Michael Stefaniuc1a60da72009-05-07 22:25:33 +0200100 lstrcpyW(szCaption, Globals.szFileTitle);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000101 else
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200102 LoadStringW(Globals.hInstance, STRING_UNTITLED, szCaption, ARRAY_SIZE(szCaption));
Thomas Kho3ec165f2006-07-12 14:15:31 -0700103
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200104 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
Michael Stefaniuc1a60da72009-05-07 22:25:33 +0200105 lstrcatW(szCaption, hyphenW);
106 lstrcatW(szCaption, szNotepad);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000107
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200108 SetWindowTextW(Globals.hMainWnd, szCaption);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000109}
110
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +0200111int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000112{
113 WCHAR szMessage[MAX_STRING_LEN];
114 WCHAR szResource[MAX_STRING_LEN];
Marcel Baur03287451999-02-14 11:28:37 +0000115
116 /* Load and format szMessage */
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200117 LoadStringW(Globals.hInstance, formatId, szResource, ARRAY_SIZE(szResource));
118 wnsprintfW(szMessage, ARRAY_SIZE(szMessage), szResource, szString);
Vincent Béron9a624912002-05-31 23:06:46 +0000119
Marcel Baur03287451999-02-14 11:28:37 +0000120 /* Load szCaption */
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +0200121 if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200122 LoadStringW(Globals.hInstance, STRING_ERROR, szResource, ARRAY_SIZE(szResource));
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +0200123 else
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200124 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szResource, ARRAY_SIZE(szResource));
Marcel Baur03287451999-02-14 11:28:37 +0000125
126 /* Display Modal Dialog */
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +0200127 if (hParent == NULL)
128 hParent = Globals.hMainWnd;
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200129 return MessageBoxW(hParent, szMessage, szResource, dwFlags);
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +0200130}
131
132static void AlertFileNotFound(LPCWSTR szFileName)
133{
134 DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
Marcel Baur03287451999-02-14 11:28:37 +0000135}
136
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000137static int AlertFileNotSaved(LPCWSTR szFileName)
138{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000139 WCHAR szUntitled[MAX_STRING_LEN];
Marcel Baur03287451999-02-14 11:28:37 +0000140
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200141 LoadStringW(Globals.hInstance, STRING_UNTITLED, szUntitled, ARRAY_SIZE(szUntitled));
Mikołaj Zalewskib0905ac2006-10-22 18:54:51 +0200142 return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
143 MB_ICONQUESTION|MB_YESNOCANCEL);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000144}
145
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100146static int AlertUnicodeCharactersLost(LPCWSTR szFileName)
147{
148 WCHAR szMsgFormat[MAX_STRING_LEN];
149 WCHAR szEnc[MAX_STRING_LEN];
150 WCHAR szMsg[ARRAY_SIZE(szMsgFormat) + MAX_PATH + ARRAY_SIZE(szEnc)];
151 WCHAR szCaption[MAX_STRING_LEN];
152
153 LoadStringW(Globals.hInstance, STRING_LOSS_OF_UNICODE_CHARACTERS,
154 szMsgFormat, ARRAY_SIZE(szMsgFormat));
155 load_encoding_name(ENCODING_ANSI, szEnc, ARRAY_SIZE(szEnc));
156 wnsprintfW(szMsg, ARRAY_SIZE(szMsg), szMsgFormat, szFileName, szEnc);
157 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szCaption,
158 ARRAY_SIZE(szCaption));
159 return MessageBoxW(Globals.hMainWnd, szMsg, szCaption,
160 MB_OKCANCEL|MB_ICONEXCLAMATION);
161}
162
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000163/**
164 * Returns:
165 * TRUE - if file exists
166 * FALSE - if file does not exist
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000167 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000168BOOL FileExists(LPCWSTR szFilename)
169{
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200170 WIN32_FIND_DATAW entry;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000171 HANDLE hFile;
Vincent Béron9a624912002-05-31 23:06:46 +0000172
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200173 hFile = FindFirstFileW(szFilename, &entry);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000174 FindClose(hFile);
Vincent Béron9a624912002-05-31 23:06:46 +0000175
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000176 return (hFile != INVALID_HANDLE_VALUE);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000177}
178
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100179static inline BOOL is_conversion_to_ansi_lossy(LPCWSTR textW, int lenW)
180{
181 BOOL ret = FALSE;
182 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, textW, lenW, NULL, 0,
183 NULL, &ret);
184 return ret;
185}
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000186
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100187typedef enum
188{
189 SAVED_OK,
190 SAVE_FAILED,
191 SHOW_SAVEAS_DIALOG
192} SAVE_STATUS;
193
194/* szFileName is the filename to save under; enc is the encoding to use.
195 *
196 * If the function succeeds, it returns SAVED_OK.
197 * If the function fails, it returns SAVE_FAILED.
198 * If Unicode data could be lost due to conversion to a non-Unicode character
199 * set, a warning is displayed. The user can continue (and the function carries
200 * on), or cancel (and the function returns SHOW_SAVEAS_DIALOG).
201 */
202static SAVE_STATUS DoSaveFile(LPCWSTR szFileName, ENCODING enc)
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000203{
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100204 int lenW;
205 WCHAR* textW;
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000206 HANDLE hFile;
207 DWORD dwNumWrite;
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100208 PVOID pBytes;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000209 DWORD size;
Marcel Baur03287451999-02-14 11:28:37 +0000210
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100211 /* lenW includes the byte-order mark, but not the \0. */
212 lenW = GetWindowTextLengthW(Globals.hEdit) + 1;
213 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
214 if (!textW)
215 {
216 ShowLastError();
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100217 return SAVE_FAILED;
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100218 }
219 textW[0] = (WCHAR) 0xfeff;
220 lenW = GetWindowTextW(Globals.hEdit, textW+1, lenW) + 1;
221
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100222 switch (enc)
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100223 {
224 case ENCODING_UTF16BE:
225 byteswap_wide_string(textW, lenW);
226 /* fall through */
227
228 case ENCODING_UTF16LE:
229 size = lenW * sizeof(WCHAR);
230 pBytes = textW;
231 break;
232
233 case ENCODING_UTF8:
234 size = WideCharToMultiByte(CP_UTF8, 0, textW, lenW, NULL, 0, NULL, NULL);
235 pBytes = HeapAlloc(GetProcessHeap(), 0, size);
236 if (!pBytes)
237 {
238 ShowLastError();
239 HeapFree(GetProcessHeap(), 0, textW);
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100240 return SAVE_FAILED;
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100241 }
242 WideCharToMultiByte(CP_UTF8, 0, textW, lenW, pBytes, size, NULL, NULL);
243 HeapFree(GetProcessHeap(), 0, textW);
244 break;
245
246 default:
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100247 if (is_conversion_to_ansi_lossy(textW+1, lenW-1)
248 && AlertUnicodeCharactersLost(szFileName) == IDCANCEL)
249 {
250 HeapFree(GetProcessHeap(), 0, textW);
251 return SHOW_SAVEAS_DIALOG;
252 }
253
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100254 size = WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, NULL, 0, NULL, NULL);
255 pBytes = HeapAlloc(GetProcessHeap(), 0, size);
256 if (!pBytes)
257 {
258 ShowLastError();
259 HeapFree(GetProcessHeap(), 0, textW);
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100260 return SAVE_FAILED;
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100261 }
262 WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, pBytes, size, NULL, NULL);
263 HeapFree(GetProcessHeap(), 0, textW);
264 break;
265 }
266
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100267 hFile = CreateFileW(szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000268 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
269 if(hFile == INVALID_HANDLE_VALUE)
270 {
271 ShowLastError();
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100272 HeapFree(GetProcessHeap(), 0, pBytes);
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100273 return SAVE_FAILED;
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000274 }
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100275 if (!WriteFile(hFile, pBytes, size, &dwNumWrite, NULL))
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000276 {
277 ShowLastError();
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100278 CloseHandle(hFile);
279 HeapFree(GetProcessHeap(), 0, pBytes);
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100280 return SAVE_FAILED;
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000281 }
Jan Nieuwenhuizen137e54d2005-10-26 10:07:05 +0000282 SetEndOfFile(hFile);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000283 CloseHandle(hFile);
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100284 HeapFree(GetProcessHeap(), 0, pBytes);
285
286 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100287 return SAVED_OK;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000288}
289
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000290/**
291 * Returns:
292 * TRUE - User agreed to close (both save/don't save)
293 * FALSE - User cancelled close by selecting "Cancel"
294 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000295BOOL DoCloseFile(void)
296{
Marcel Baur03287451999-02-14 11:28:37 +0000297 int nResult;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000298 static const WCHAR empty_strW[] = { 0 };
Vincent Béron9a624912002-05-31 23:06:46 +0000299
Henry Kroll IIId3369082010-03-06 21:05:21 -0800300 nResult=GetWindowTextLengthW(Globals.hEdit);
301 if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0) &&
302 (nResult || Globals.szFileName[0]))
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000303 {
Marcel Baur03287451999-02-14 11:28:37 +0000304 /* prompt user to save changes */
305 nResult = AlertFileNotSaved(Globals.szFileName);
306 switch (nResult) {
Hirofumi Katayama32b6a1a2008-09-09 12:44:36 +0900307 case IDYES: return DIALOG_FileSave();
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000308
Marcel Baur03287451999-02-14 11:28:37 +0000309 case IDNO: break;
Marcel Baura43295d1998-10-18 14:11:42 +0000310
Marcel Baur03287451999-02-14 11:28:37 +0000311 case IDCANCEL: return(FALSE);
Vincent Béron9a624912002-05-31 23:06:46 +0000312
Marcel Baur03287451999-02-14 11:28:37 +0000313 default: return(FALSE);
Marcel Baur03287451999-02-14 11:28:37 +0000314 } /* switch */
315 } /* if */
Vincent Béron9a624912002-05-31 23:06:46 +0000316
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100317 SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000318
319 UpdateWindowCaption();
Marcel Baur03287451999-02-14 11:28:37 +0000320 return(TRUE);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000321}
322
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100323static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size)
324{
325 static const char bom_utf8[] = { 0xef, 0xbb, 0xbf };
326 if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
327 return ENCODING_UTF8;
328 else
329 {
330 int flags = IS_TEXT_UNICODE_SIGNATURE |
331 IS_TEXT_UNICODE_REVERSE_SIGNATURE |
332 IS_TEXT_UNICODE_ODD_LENGTH;
333 IsTextUnicode(buffer, size, &flags);
334 if (flags & IS_TEXT_UNICODE_SIGNATURE)
335 return ENCODING_UTF16LE;
336 else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
337 return ENCODING_UTF16BE;
338 else
339 return ENCODING_ANSI;
340 }
341}
342
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100343void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000344{
Kevin Koltzau868a2b042005-05-07 12:14:05 +0000345 static const WCHAR dotlog[] = { '.','L','O','G',0 };
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000346 HANDLE hFile;
347 LPSTR pTemp;
348 DWORD size;
349 DWORD dwNumRead;
Alexander Scott-Johns93b99e02009-07-03 00:34:45 +0100350 int lenW;
351 WCHAR* textW;
Alexander Scott-Johns32c066f2009-07-03 01:04:00 +0100352 int i;
Kevin Koltzau868a2b042005-05-07 12:14:05 +0000353 WCHAR log[5];
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000354
Marcus Meissner73458b01998-12-26 12:54:29 +0000355 /* Close any files and prompt to save changes */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000356 if (!DoCloseFile())
357 return;
358
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200359 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
360 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000361 if(hFile == INVALID_HANDLE_VALUE)
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000362 {
Dmitry Timoshkov4ea5fce2007-08-31 16:02:01 +0900363 AlertFileNotFound(szFileName);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000364 return;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000365 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000366
367 size = GetFileSize(hFile, NULL);
Alexandre Julliard2aa8a832003-10-16 19:31:11 +0000368 if (size == INVALID_FILE_SIZE)
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000369 {
370 CloseHandle(hFile);
371 ShowLastError();
372 return;
373 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000374
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100375 /* Extra memory for (WCHAR)'\0'-termination. */
376 pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000377 if (!pTemp)
378 {
379 CloseHandle(hFile);
380 ShowLastError();
381 return;
382 }
383
384 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
385 {
386 CloseHandle(hFile);
387 HeapFree(GetProcessHeap(), 0, pTemp);
388 ShowLastError();
389 return;
390 }
391
392 CloseHandle(hFile);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000393
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100394 size = dwNumRead;
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100395
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100396 if (enc == ENCODING_AUTO)
397 enc = detect_encoding_of_buffer(pTemp, size);
398 else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
399 {
400 /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
401 * override the selection (like native Notepad).
402 */
403 if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
404 enc = ENCODING_UTF16LE;
405 else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
406 enc = ENCODING_UTF16BE;
407 }
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100408
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100409 switch (enc)
410 {
411 case ENCODING_UTF16BE:
412 byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100413 /* Forget whether the file is BE or LE, like native Notepad. */
414 enc = ENCODING_UTF16LE;
415
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100416 /* fall through */
417
418 case ENCODING_UTF16LE:
Alexander Scott-Johns93b99e02009-07-03 00:34:45 +0100419 textW = (LPWSTR)pTemp;
420 lenW = size/sizeof(WCHAR);
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100421 break;
422
423 default:
Alexander Scott-Johns93b99e02009-07-03 00:34:45 +0100424 {
425 int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
426 lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
427 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
428 if (!textW)
429 {
430 ShowLastError();
431 HeapFree(GetProcessHeap(), 0, pTemp);
432 return;
433 }
434 MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
435 HeapFree(GetProcessHeap(), 0, pTemp);
436 break;
437 }
Alexander Scott-Johns8b6b7b22009-06-29 22:24:59 +0100438 }
439
Alexander Scott-Johns32c066f2009-07-03 01:04:00 +0100440 /* Replace '\0's with spaces. Other than creating a custom control that
441 * can deal with '\0' characters, it's the best that can be done.
442 */
443 for (i = 0; i < lenW; i++)
444 if (textW[i] == '\0')
445 textW[i] = ' ';
Alexander Scott-Johns93b99e02009-07-03 00:34:45 +0100446 textW[lenW] = '\0';
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000447
Alexander Scott-Johns93b99e02009-07-03 00:34:45 +0100448 if (lenW >= 1 && textW[0] == 0xfeff)
449 SetWindowTextW(Globals.hEdit, textW+1);
450 else
451 SetWindowTextW(Globals.hEdit, textW);
452
453 HeapFree(GetProcessHeap(), 0, textW);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000454
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200455 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
456 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000457 SetFocus(Globals.hEdit);
Kevin Koltzau868a2b042005-05-07 12:14:05 +0000458
James Hawkinse4c12272008-02-29 22:37:21 -0600459 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
Michael Stefaniuc129889b2009-05-27 23:49:13 +0200460 if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
Kevin Koltzau868a2b042005-05-07 12:14:05 +0000461 {
462 static const WCHAR lfW[] = { '\r','\n',0 };
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200463 SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200464 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
Kevin Koltzau868a2b042005-05-07 12:14:05 +0000465 DIALOG_EditTimeDate();
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200466 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
Kevin Koltzau868a2b042005-05-07 12:14:05 +0000467 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000468
Alexander Scott-Johns080cc902009-06-29 23:03:53 +0100469 SetFileNameAndEncoding(szFileName, enc);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000470 UpdateWindowCaption();
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000471}
472
Alexandre Julliard03468f71998-02-15 19:40:49 +0000473VOID DIALOG_FileNew(VOID)
474{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000475 static const WCHAR empty_strW[] = { 0 };
476
Francois Gouget8a18e0e2008-04-07 13:01:02 +0200477 /* Close any files and prompt to save changes */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000478 if (DoCloseFile()) {
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200479 SetWindowTextW(Globals.hEdit, empty_strW);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200480 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000481 SetFocus(Globals.hEdit);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000482 }
Alexandre Julliard03468f71998-02-15 19:40:49 +0000483}
484
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100485/* Used to detect encoding of files selected in Open dialog.
486 * Returns ENCODING_AUTO if file can't be read, etc.
487 */
488static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
489{
490 DWORD size;
491 HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
492 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
493 if (hFile == INVALID_HANDLE_VALUE)
494 return ENCODING_AUTO;
495 size = GetFileSize(hFile, NULL);
496 if (size == INVALID_FILE_SIZE)
497 {
498 CloseHandle(hFile);
499 return ENCODING_AUTO;
500 }
501 else
502 {
503 DWORD dwNumRead;
504 BYTE buffer[MAX_STRING_LEN];
505 if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
506 {
507 CloseHandle(hFile);
508 return ENCODING_AUTO;
509 }
510 CloseHandle(hFile);
511 return detect_encoding_of_buffer(buffer, dwNumRead);
512 }
513}
514
Vitaly Perovd4e66542010-02-25 19:36:04 +0300515static LPWSTR dialog_print_to_file(HWND hMainWnd)
516{
517 OPENFILENAMEW ofn;
518 static WCHAR file[MAX_PATH] = {'o','u','t','p','u','t','.','p','r','n',0};
519 static const WCHAR defExt[] = {'p','r','n',0};
520
521 ZeroMemory(&ofn, sizeof(ofn));
522
523 ofn.lStructSize = sizeof(ofn);
524 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
525 ofn.hwndOwner = hMainWnd;
526 ofn.lpstrFile = file;
527 ofn.nMaxFile = MAX_PATH;
528 ofn.lpstrDefExt = defExt;
529
530 if(GetSaveFileNameW(&ofn))
531 return file;
532 else
533 return FALSE;
534}
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100535static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
536{
537 static HWND hEncCombo;
538
539 switch (uMsg)
540 {
541 case WM_INITDIALOG:
542 {
543 ENCODING enc;
544 hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
545 for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
546 {
547 WCHAR szEnc[MAX_STRING_LEN];
548 load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
549 SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
550 }
551 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
552 }
553 break;
554
555 case WM_COMMAND:
556 if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
557 HIWORD(wParam) == CBN_SELCHANGE)
558 {
559 int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
560 Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
561 }
562
563 break;
564
565 case WM_NOTIFY:
566 switch (((OFNOTIFYW*)lParam)->hdr.code)
567 {
568 case CDN_SELCHANGE:
569 if (Globals.bOfnIsOpenDialog)
570 {
571 /* Check the start of the selected file for a BOM. */
572 ENCODING enc;
573 WCHAR szFileName[MAX_PATH];
574 SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
575 ARRAY_SIZE(szFileName), (LPARAM)szFileName);
576 enc = detect_encoding_of_file(szFileName);
577 if (enc != ENCODING_AUTO)
578 {
579 Globals.encOfnCombo = enc;
580 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
581 }
582 }
583 break;
584
585 default:
586 break;
587 }
588 break;
589
590 default:
591 break;
592 }
593 return 0;
594}
595
Alexandre Julliard03468f71998-02-15 19:40:49 +0000596VOID DIALOG_FileOpen(VOID)
597{
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200598 OPENFILENAMEW openfilename;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000599 WCHAR szPath[MAX_PATH];
600 WCHAR szDir[MAX_PATH];
601 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
602 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
Marcel Baura43295d1998-10-18 14:11:42 +0000603
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000604 ZeroMemory(&openfilename, sizeof(openfilename));
Alexandre Julliard03468f71998-02-15 19:40:49 +0000605
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200606 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
Michael Stefaniuc1a60da72009-05-07 22:25:33 +0200607 lstrcpyW(szPath, txt_files);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000608
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000609 openfilename.lStructSize = sizeof(openfilename);
610 openfilename.hwndOwner = Globals.hMainWnd;
611 openfilename.hInstance = Globals.hInstance;
612 openfilename.lpstrFilter = Globals.szFilter;
613 openfilename.lpstrFile = szPath;
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200614 openfilename.nMaxFile = ARRAY_SIZE(szPath);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000615 openfilename.lpstrInitialDir = szDir;
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100616 openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
617 OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
618 OFN_HIDEREADONLY | OFN_ENABLESIZING;
619 openfilename.lpfnHook = OfnHookProc;
620 openfilename.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000621 openfilename.lpstrDefExt = szDefaultExt;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000622
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100623 Globals.encOfnCombo = ENCODING_ANSI;
624 Globals.bOfnIsOpenDialog = TRUE;
Vincent Béron9a624912002-05-31 23:06:46 +0000625
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200626 if (GetOpenFileNameW(&openfilename))
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100627 DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000628}
629
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100630/* Return FALSE to cancel close */
Hirofumi Katayama32b6a1a2008-09-09 12:44:36 +0900631BOOL DIALOG_FileSave(VOID)
Alexandre Julliard03468f71998-02-15 19:40:49 +0000632{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000633 if (Globals.szFileName[0] == '\0')
Hirofumi Katayama32b6a1a2008-09-09 12:44:36 +0900634 return DIALOG_FileSaveAs();
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000635 else
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100636 {
637 switch (DoSaveFile(Globals.szFileName, Globals.encFile))
638 {
639 case SAVED_OK: return TRUE;
640 case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
641 default: return FALSE;
642 }
643 }
Alexandre Julliard03468f71998-02-15 19:40:49 +0000644}
645
Hirofumi Katayama32b6a1a2008-09-09 12:44:36 +0900646BOOL DIALOG_FileSaveAs(VOID)
Alexandre Julliard03468f71998-02-15 19:40:49 +0000647{
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200648 OPENFILENAMEW saveas;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000649 WCHAR szPath[MAX_PATH];
650 WCHAR szDir[MAX_PATH];
651 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
652 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
Vincent Béron9a624912002-05-31 23:06:46 +0000653
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000654 ZeroMemory(&saveas, sizeof(saveas));
Alexandre Julliard03468f71998-02-15 19:40:49 +0000655
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200656 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
Michael Stefaniuc1a60da72009-05-07 22:25:33 +0200657 lstrcpyW(szPath, txt_files);
Marcel Baura43295d1998-10-18 14:11:42 +0000658
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200659 saveas.lStructSize = sizeof(OPENFILENAMEW);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000660 saveas.hwndOwner = Globals.hMainWnd;
661 saveas.hInstance = Globals.hInstance;
662 saveas.lpstrFilter = Globals.szFilter;
663 saveas.lpstrFile = szPath;
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +0200664 saveas.nMaxFile = ARRAY_SIZE(szPath);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000665 saveas.lpstrInitialDir = szDir;
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100666 saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
667 OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
668 OFN_HIDEREADONLY | OFN_ENABLESIZING;
669 saveas.lpfnHook = OfnHookProc;
670 saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000671 saveas.lpstrDefExt = szDefaultExt;
Alexandre Julliard03468f71998-02-15 19:40:49 +0000672
Alexander Scott-Johns67766392009-06-29 23:50:39 +0100673 /* Preset encoding to what file was opened/saved last with. */
674 Globals.encOfnCombo = Globals.encFile;
675 Globals.bOfnIsOpenDialog = FALSE;
676
Alexander Scott-Johns84fd1c82009-06-30 00:24:18 +0100677retry:
678 if (!GetSaveFileNameW(&saveas))
679 return FALSE;
680
681 switch (DoSaveFile(szPath, Globals.encOfnCombo))
682 {
683 case SAVED_OK:
684 SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
685 UpdateWindowCaption();
686 return TRUE;
687
688 case SHOW_SAVEAS_DIALOG:
689 goto retry;
690
691 default:
692 return FALSE;
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000693 }
Alexandre Julliard03468f71998-02-15 19:40:49 +0000694}
695
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200696typedef struct {
697 LPWSTR mptr;
698 LPWSTR mend;
699 LPWSTR lptr;
700 DWORD len;
701} TEXTINFO, *LPTEXTINFO;
702
703static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
704{
705 SIZE szMetric;
706
707 if (*text)
708 {
709 /* Write the header or footer */
Michael Stefaniuc1a60da72009-05-07 22:25:33 +0200710 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200711 if (dopage)
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200712 ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
713 header ? rc->top : rc->bottom - szMetric.cy,
714 ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200715 return 1;
716 }
717 return 0;
718}
719
720static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
721{
722 int b, y;
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200723 TEXTMETRICW tm;
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200724 SIZE szMetrics;
725
726 if (dopage)
727 {
728 if (StartPage(hdc) <= 0)
729 {
730 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
731 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200732 MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200733 return FALSE;
734 }
735 }
736
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200737 GetTextMetricsW(hdc, &tm);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200738 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
739 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
740
741 do {
742 INT m, n;
743
744 if (!tInfo->len)
745 {
746 /* find the end of the line */
747 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
748 {
749 if (*tInfo->mptr == '\t')
750 {
751 /* replace tabs with spaces */
752 for (m = 0; m < SPACES_IN_TAB; m++)
753 {
754 if (tInfo->len < PRINT_LEN_MAX)
755 tInfo->lptr[tInfo->len++] = ' ';
756 else if (Globals.bWrapLongLines)
757 break;
758 }
759 }
760 else if (tInfo->len < PRINT_LEN_MAX)
761 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
762
763 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
764 break;
765
766 tInfo->mptr++;
767 }
768 }
769
770 /* Find out how much we should print if line wrapping is enabled */
771 if (Globals.bWrapLongLines)
772 {
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200773 GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200774 if (n < tInfo->len && tInfo->lptr[n] != ' ')
775 {
776 m = n;
777 /* Don't wrap words unless it's a single word over the entire line */
778 while (m && tInfo->lptr[m] != ' ') m--;
779 if (m > 0) n = m + 1;
780 }
781 }
782 else
783 n = tInfo->len;
784
785 if (dopage)
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200786 ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200787
788 tInfo->len -= n;
789
790 if (tInfo->len)
791 {
792 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
793 y += tm.tmHeight + tm.tmExternalLeading;
794 }
795 else
796 {
797 /* find the next line */
798 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
799 {
800 if (*tInfo->mptr == '\n')
801 y += tm.tmHeight + tm.tmExternalLeading;
802 tInfo->mptr++;
803 }
804 }
805 } while (tInfo->mptr < tInfo->mend && y < b);
806
807 notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
808 if (dopage)
809 {
810 EndPage(hdc);
811 }
812 return TRUE;
813}
814
Alexandre Julliard03468f71998-02-15 19:40:49 +0000815VOID DIALOG_FilePrint(VOID)
816{
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200817 DOCINFOW di;
818 PRINTDLGW printer;
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200819 int page, dopage, copy;
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200820 LOGFONTW lfFont;
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200821 HFONT hTextFont, old_font = 0;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000822 DWORD size;
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200823 BOOL ret = FALSE;
824 RECT rc;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000825 LPWSTR pTemp;
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200826 TEXTINFO tInfo;
Duane Clarka60ee9c2006-07-16 16:20:30 -0700827 WCHAR cTemp[PRINT_LEN_MAX];
Marcel Baur03287451999-02-14 11:28:37 +0000828
Duane Clark4eb4c042002-10-21 18:22:15 +0000829 /* Get Current Settings */
830 ZeroMemory(&printer, sizeof(printer));
831 printer.lStructSize = sizeof(printer);
832 printer.hwndOwner = Globals.hMainWnd;
Robert Shearman8be66cb2005-12-08 11:57:58 +0100833 printer.hDevMode = Globals.hDevMode;
834 printer.hDevNames = Globals.hDevNames;
Duane Clark4eb4c042002-10-21 18:22:15 +0000835 printer.hInstance = Globals.hInstance;
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200836
Duane Clark4eb4c042002-10-21 18:22:15 +0000837 /* Set some default flags */
Duane Clarka60ee9c2006-07-16 16:20:30 -0700838 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000839 printer.nFromPage = 0;
Duane Clark4eb4c042002-10-21 18:22:15 +0000840 printer.nMinPage = 1;
841 /* we really need to calculate number of pages to set nMaxPage and nToPage */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000842 printer.nToPage = 0;
843 printer.nMaxPage = -1;
Duane Clark4eb4c042002-10-21 18:22:15 +0000844 /* Let commdlg manage copy settings */
845 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
Marcel Baur03287451999-02-14 11:28:37 +0000846
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200847 if (!PrintDlgW(&printer)) return;
Marcel Baur03287451999-02-14 11:28:37 +0000848
Robert Shearman8be66cb2005-12-08 11:57:58 +0100849 Globals.hDevMode = printer.hDevMode;
850 Globals.hDevNames = printer.hDevNames;
851
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200852 SetMapMode(printer.hDC, MM_TEXT);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000853
Duane Clark4eb4c042002-10-21 18:22:15 +0000854 /* initialize DOCINFO */
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200855 di.cbSize = sizeof(DOCINFOW);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000856 di.lpszDocName = Globals.szFileTitle;
857 di.lpszOutput = NULL;
858 di.lpszDatatype = NULL;
Duane Clark4eb4c042002-10-21 18:22:15 +0000859 di.fwType = 0;
Marcel Baur03287451999-02-14 11:28:37 +0000860
Vitaly Perovd4e66542010-02-25 19:36:04 +0300861 if(printer.Flags & PD_PRINTTOFILE)
862 {
863 di.lpszOutput = dialog_print_to_file(printer.hwndOwner);
864 if(!di.lpszOutput)
865 return;
866 }
867
Duane Clark4eb4c042002-10-21 18:22:15 +0000868 /* Get the file text */
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200869 size = GetWindowTextLengthW(Globals.hEdit) + 1;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000870 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
Duane Clark4eb4c042002-10-21 18:22:15 +0000871 if (!pTemp)
872 {
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200873 DeleteDC(printer.hDC);
874 ShowLastError();
875 return;
Duane Clark4eb4c042002-10-21 18:22:15 +0000876 }
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200877 size = GetWindowTextW(Globals.hEdit, pTemp, size);
Marcel Baur03287451999-02-14 11:28:37 +0000878
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200879 if (StartDocW(printer.hDC, &di) > 0)
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200880 {
881 /* Get the page margins in pixels. */
882 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
883 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
884 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
885 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
886 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
887 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
888 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
889 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
890
891 /* Create a font for the printer resolution */
892 lfFont = Globals.lfFont;
893 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
894 /* Make the font a bit lighter */
895 lfFont.lfWeight -= 100;
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200896 hTextFont = CreateFontIndirectW(&lfFont);
Rolf Kalbermatter5eb6c112007-04-06 19:50:37 +0200897 old_font = SelectObject(printer.hDC, hTextFont);
898
899 for (copy = 1; copy <= printer.nCopies; copy++)
900 {
901 page = 1;
902
903 tInfo.mptr = pTemp;
904 tInfo.mend = pTemp + size;
905 tInfo.lptr = cTemp;
906 tInfo.len = 0;
907
908 do {
909 if (printer.Flags & PD_PAGENUMS)
910 {
911 /* a specific range of pages is selected, so
912 * skip pages that are not to be printed
913 */
914 if (page > printer.nToPage)
915 break;
916 else if (page >= printer.nFromPage)
917 dopage = 1;
918 else
919 dopage = 0;
920 }
921 else
922 dopage = 1;
923
924 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
925 page++;
926 } while (ret && tInfo.mptr < tInfo.mend);
927
928 if (!ret) break;
929 }
930 EndDoc(printer.hDC);
931 SelectObject(printer.hDC, old_font);
932 DeleteObject(hTextFont);
933 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000934 DeleteDC(printer.hDC);
Michael Stefaniuc81a7aad2003-09-27 02:25:21 +0000935 HeapFree(GetProcessHeap(), 0, pTemp);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000936}
937
938VOID DIALOG_FilePrinterSetup(VOID)
939{
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +0200940 PRINTDLGW printer;
Marcel Baur03287451999-02-14 11:28:37 +0000941
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000942 ZeroMemory(&printer, sizeof(printer));
943 printer.lStructSize = sizeof(printer);
944 printer.hwndOwner = Globals.hMainWnd;
Robert Shearman8be66cb2005-12-08 11:57:58 +0100945 printer.hDevMode = Globals.hDevMode;
946 printer.hDevNames = Globals.hDevNames;
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000947 printer.hInstance = Globals.hInstance;
948 printer.Flags = PD_PRINTSETUP;
949 printer.nCopies = 1;
Vincent Béron9a624912002-05-31 23:06:46 +0000950
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200951 PrintDlgW(&printer);
Robert Shearman8be66cb2005-12-08 11:57:58 +0100952
953 Globals.hDevMode = printer.hDevMode;
954 Globals.hDevNames = printer.hDevNames;
Alexandre Julliard03468f71998-02-15 19:40:49 +0000955}
956
957VOID DIALOG_FileExit(VOID)
958{
Michael Stefaniuc27c88392009-05-18 21:08:20 +0200959 PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000960}
961
962VOID DIALOG_EditUndo(VOID)
963{
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200964 SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000965}
966
967VOID DIALOG_EditCut(VOID)
968{
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200969 SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000970}
971
972VOID DIALOG_EditCopy(VOID)
973{
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200974 SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000975}
976
977VOID DIALOG_EditPaste(VOID)
978{
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200979 SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000980}
981
982VOID DIALOG_EditDelete(VOID)
983{
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200984 SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000985}
986
987VOID DIALOG_EditSelectAll(VOID)
988{
Michael Stefaniuc0c702d12009-05-05 12:18:43 +0200989 SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000990}
991
992VOID DIALOG_EditTimeDate(VOID)
993{
Marcel Baur03287451999-02-14 11:28:37 +0000994 SYSTEMTIME st;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000995 WCHAR szDate[MAX_STRING_LEN];
996 static const WCHAR spaceW[] = { ' ',0 };
Vincent Béron9a624912002-05-31 23:06:46 +0000997
Marcel Baur03287451999-02-14 11:28:37 +0000998 GetLocalTime(&st);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000999
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001000 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001001 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001002
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001003 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001004
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001005 GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001006 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001007}
1008
1009VOID DIALOG_EditWrap(VOID)
1010{
Byeong-Sik Jeon53a48942006-09-16 09:43:07 +09001011 BOOL modify = FALSE;
Lauri Tulmina1186032005-02-14 11:51:27 +00001012 static const WCHAR editW[] = { 'e','d','i','t',0 };
1013 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
1014 ES_AUTOVSCROLL | ES_MULTILINE;
1015 RECT rc;
1016 DWORD size;
1017 LPWSTR pTemp;
1018
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001019 size = GetWindowTextLengthW(Globals.hEdit) + 1;
Lauri Tulmina1186032005-02-14 11:51:27 +00001020 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1021 if (!pTemp)
1022 {
1023 ShowLastError();
1024 return;
1025 }
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001026 GetWindowTextW(Globals.hEdit, pTemp, size);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001027 modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
Lauri Tulmina1186032005-02-14 11:51:27 +00001028 DestroyWindow(Globals.hEdit);
1029 GetClientRect(Globals.hMainWnd, &rc);
1030 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001031 Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
Lauri Tulmina1186032005-02-14 11:51:27 +00001032 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
1033 NULL, Globals.hInstance, NULL);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001034 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
Lauri Tulmina1186032005-02-14 11:51:27 +00001035 SetWindowTextW(Globals.hEdit, pTemp);
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001036 SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
Lauri Tulmina1186032005-02-14 11:51:27 +00001037 SetFocus(Globals.hEdit);
1038 HeapFree(GetProcessHeap(), 0, pTemp);
1039
Andriy Palamarchukc55dce02002-07-08 19:41:09 +00001040 Globals.bWrapLongLines = !Globals.bWrapLongLines;
1041 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
1042 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
Alexandre Julliard03468f71998-02-15 19:40:49 +00001043}
1044
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +00001045VOID DIALOG_SelectFont(VOID)
1046{
Michael Stefaniuc3e7647a2009-05-07 11:08:48 +02001047 CHOOSEFONTW cf;
1048 LOGFONTW lf=Globals.lfFont;
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +00001049
1050 ZeroMemory( &cf, sizeof(cf) );
1051 cf.lStructSize=sizeof(cf);
1052 cf.hwndOwner=Globals.hMainWnd;
1053 cf.lpLogFont=&lf;
Pavel Roskin5f5418a2005-12-01 12:35:13 +01001054 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +00001055
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001056 if( ChooseFontW(&cf) )
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +00001057 {
1058 HFONT currfont=Globals.hFont;
1059
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001060 Globals.hFont=CreateFontIndirectW( &lf );
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +00001061 Globals.lfFont=lf;
Michael Stefaniuc0c702d12009-05-05 12:18:43 +02001062 SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +00001063 if( currfont!=NULL )
1064 DeleteObject( currfont );
1065 }
1066}
1067
Alexandre Julliard03468f71998-02-15 19:40:49 +00001068VOID DIALOG_Search(VOID)
1069{
Rico Schüllerac6a18f2009-04-25 17:54:03 +02001070 /* Allow only one search/replace dialog to open */
1071 if(Globals.hFindReplaceDlg != NULL)
1072 {
1073 SetActiveWindow(Globals.hFindReplaceDlg);
1074 return;
1075 }
1076
Andriy Palamarchukc55dce02002-07-08 19:41:09 +00001077 ZeroMemory(&Globals.find, sizeof(Globals.find));
Marcel Baur03287451999-02-14 11:28:37 +00001078 Globals.find.lStructSize = sizeof(Globals.find);
1079 Globals.find.hwndOwner = Globals.hMainWnd;
1080 Globals.find.hInstance = Globals.hInstance;
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001081 Globals.find.lpstrFindWhat = Globals.szFindText;
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +02001082 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
Mikołaj Zalewski84fc75c2006-10-22 18:58:12 +02001083 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
Marcel Baura43295d1998-10-18 14:11:42 +00001084
Marcel Baur03287451999-02-14 11:28:37 +00001085 /* We only need to create the modal FindReplace dialog which will */
1086 /* notify us of incoming events using hMainWnd Window Messages */
Alexandre Julliard54c27111998-03-29 19:44:57 +00001087
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001088 Globals.hFindReplaceDlg = FindTextW(&Globals.find);
Marcel Baur03287451999-02-14 11:28:37 +00001089 assert(Globals.hFindReplaceDlg !=0);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001090}
1091
1092VOID DIALOG_SearchNext(VOID)
1093{
Mikołaj Zalewski84fc75c2006-10-22 18:58:12 +02001094 if (Globals.lastFind.lpstrFindWhat == NULL)
1095 DIALOG_Search();
1096 else /* use the last find data */
1097 NOTEPAD_DoFind(&Globals.lastFind);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001098}
1099
Rico Schüller31376002009-04-25 17:53:58 +02001100VOID DIALOG_Replace(VOID)
1101{
Rico Schüllerac6a18f2009-04-25 17:54:03 +02001102 /* Allow only one search/replace dialog to open */
1103 if(Globals.hFindReplaceDlg != NULL)
1104 {
1105 SetActiveWindow(Globals.hFindReplaceDlg);
1106 return;
1107 }
1108
Rico Schüller31376002009-04-25 17:53:58 +02001109 ZeroMemory(&Globals.find, sizeof(Globals.find));
1110 Globals.find.lStructSize = sizeof(Globals.find);
1111 Globals.find.hwndOwner = Globals.hMainWnd;
1112 Globals.find.hInstance = Globals.hInstance;
1113 Globals.find.lpstrFindWhat = Globals.szFindText;
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +02001114 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
Rico Schüller31376002009-04-25 17:53:58 +02001115 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +02001116 Globals.find.wReplaceWithLen = ARRAY_SIZE(Globals.szReplaceText);
Rico Schüller31376002009-04-25 17:53:58 +02001117 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1118
1119 /* We only need to create the modal FindReplace dialog which will */
1120 /* notify us of incoming events using hMainWnd Window Messages */
1121
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001122 Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
Rico Schüller31376002009-04-25 17:53:58 +02001123 assert(Globals.hFindReplaceDlg !=0);
1124}
1125
Alexandre Julliard03468f71998-02-15 19:40:49 +00001126VOID DIALOG_HelpContents(VOID)
1127{
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001128 WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001129}
1130
1131VOID DIALOG_HelpSearch(VOID)
1132{
Marcel Baur03287451999-02-14 11:28:37 +00001133 /* Search Help */
Alexandre Julliard03468f71998-02-15 19:40:49 +00001134}
1135
1136VOID DIALOG_HelpHelp(VOID)
1137{
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001138 WinHelpW(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001139}
1140
Rob Shearman90efadc2008-03-28 17:53:31 +00001141VOID DIALOG_HelpAboutNotepad(VOID)
Alexandre Julliard03468f71998-02-15 19:40:49 +00001142{
Alexandre Julliard247754f2008-03-21 18:39:31 +01001143 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001144 WCHAR szNotepad[MAX_STRING_LEN];
Michael Stefaniucf706c682009-05-18 21:07:49 +02001145 HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
1146 IMAGE_ICON, 48, 48, LR_SHARED);
Marcel Baur03287451999-02-14 11:28:37 +00001147
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +02001148 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001149 ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001150}
1151
Andriy Palamarchukc55dce02002-07-08 19:41:09 +00001152
Alexandre Julliard03468f71998-02-15 19:40:49 +00001153/***********************************************************************
1154 *
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001155 * DIALOG_FilePageSetup
Alexandre Julliard03468f71998-02-15 19:40:49 +00001156 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001157VOID DIALOG_FilePageSetup(void)
Alexandre Julliard03468f71998-02-15 19:40:49 +00001158{
Michael Stefaniucf706c682009-05-18 21:07:49 +02001159 DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
1160 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
Alexandre Julliard03468f71998-02-15 19:40:49 +00001161}
1162
Alexandre Julliard03468f71998-02-15 19:40:49 +00001163
1164/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1165 *
1166 * DIALOG_PAGESETUP_DlgProc
1167 */
1168
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001169static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard03468f71998-02-15 19:40:49 +00001170{
Marcel Baur03287451999-02-14 11:28:37 +00001171
1172 switch (msg)
Alexandre Julliard03468f71998-02-15 19:40:49 +00001173 {
1174 case WM_COMMAND:
1175 switch (wParam)
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001176 {
1177 case IDOK:
Marcel Baur03287451999-02-14 11:28:37 +00001178 /* save user input and close dialog */
Michael Stefaniuccdc5b0c2009-05-27 23:48:57 +02001179 GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
1180 GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
Rolf Kalbermatter3cae9202007-04-01 20:52:08 +02001181
1182 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
1183 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
1184 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
1185 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001186 EndDialog(hDlg, IDOK);
1187 return TRUE;
Alexandre Julliard03468f71998-02-15 19:40:49 +00001188
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001189 case IDCANCEL:
Marcel Baur03287451999-02-14 11:28:37 +00001190 /* discard user input and close dialog */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001191 EndDialog(hDlg, IDCANCEL);
1192 return TRUE;
Marcel Baur03287451999-02-14 11:28:37 +00001193
1194 case IDHELP:
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001195 {
Marcel Baur03287451999-02-14 11:28:37 +00001196 /* FIXME: Bring this to work */
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001197 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 };
1198 static const WCHAR helpW[] = { 'H','e','l','p',0 };
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001199 MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
Marcel Baur03287451999-02-14 11:28:37 +00001200 return TRUE;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001201 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +00001202
1203 default:
1204 break;
1205 }
Marcel Baura43295d1998-10-18 14:11:42 +00001206 break;
1207
1208 case WM_INITDIALOG:
Marcel Baur03287451999-02-14 11:28:37 +00001209 /* fetch last user input prior to display dialog */
Michael Stefaniuc27c88392009-05-18 21:08:20 +02001210 SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
1211 SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
Rolf Kalbermatter3cae9202007-04-01 20:52:08 +02001212 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
1213 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
1214 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
1215 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
Marcel Baura43295d1998-10-18 14:11:42 +00001216 break;
Alexandre Julliard03468f71998-02-15 19:40:49 +00001217 }
Marcel Baur03287451999-02-14 11:28:37 +00001218
Alexandre Julliard03468f71998-02-15 19:40:49 +00001219 return FALSE;
1220}