blob: a99465ff394e1e8b0afae4748483dc9bd1532d0f [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
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard03468f71998-02-15 19:40:49 +000021 */
22
Dmitry Timoshkov398af162003-07-21 20:05:33 +000023#define UNICODE
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>
Marcel Baura43295d1998-10-18 14:11:42 +000029
Alexandre Julliard03468f71998-02-15 19:40:49 +000030#include "main.h"
31#include "license.h"
32#include "dialog.h"
Marcel Baura43295d1998-10-18 14:11:42 +000033
Dmitry Timoshkov398af162003-07-21 20:05:33 +000034static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
Alexandre Julliard03468f71998-02-15 19:40:49 +000035
Dmitry Timoshkov398af162003-07-21 20:05:33 +000036static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +000037
Dmitry Timoshkov398af162003-07-21 20:05:33 +000038VOID ShowLastError(void)
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000039{
40 DWORD error = GetLastError();
41 if (error != NO_ERROR)
42 {
Dmitry Timoshkov398af162003-07-21 20:05:33 +000043 LPWSTR lpMsgBuf;
44 WCHAR szTitle[MAX_STRING_LEN];
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000045
Dmitry Timoshkov398af162003-07-21 20:05:33 +000046 LoadString(Globals.hInstance, STRING_ERROR, szTitle, SIZEOF(szTitle));
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000047 FormatMessage(
48 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
49 NULL, error, 0,
50 (LPTSTR) &lpMsgBuf, 0, NULL);
Dmitry Timoshkov398af162003-07-21 20:05:33 +000051 MessageBox(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000052 LocalFree(lpMsgBuf);
53 }
54}
55
56/**
57 * Sets the caption of the main window according to Globals.szFileTitle:
58 * Notepad - (untitled) if no file is open
59 * Notepad - [filename] if a file is given
60 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +000061static void UpdateWindowCaption(void)
62{
63 WCHAR szCaption[MAX_STRING_LEN];
64 WCHAR szUntitled[MAX_STRING_LEN];
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000065
Dmitry Timoshkov398af162003-07-21 20:05:33 +000066 LoadString(Globals.hInstance, STRING_NOTEPAD, szCaption, SIZEOF(szCaption));
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000067
68 if (Globals.szFileTitle[0] != '\0') {
Dmitry Timoshkov398af162003-07-21 20:05:33 +000069 static const WCHAR bracket_lW[] = { ' ','-',' ','[',0 };
70 static const WCHAR bracket_rW[] = { ']',0 };
71 lstrcat(szCaption, bracket_lW);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000072 lstrcat(szCaption, Globals.szFileTitle);
Dmitry Timoshkov398af162003-07-21 20:05:33 +000073 lstrcat(szCaption, bracket_rW);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000074 }
75 else
76 {
Dmitry Timoshkov398af162003-07-21 20:05:33 +000077 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
78 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
79 lstrcat(szCaption, hyphenW);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +000080 lstrcat(szCaption, szUntitled);
81 }
82
83 SetWindowText(Globals.hMainWnd, szCaption);
84}
85
Dmitry Timoshkov398af162003-07-21 20:05:33 +000086static void AlertFileNotFound(LPCWSTR szFileName)
87{
88 WCHAR szMessage[MAX_STRING_LEN];
89 WCHAR szResource[MAX_STRING_LEN];
Marcel Baur03287451999-02-14 11:28:37 +000090
91 /* Load and format szMessage */
Dmitry Timoshkov398af162003-07-21 20:05:33 +000092 LoadString(Globals.hInstance, STRING_NOTFOUND, szResource, SIZEOF(szResource));
93 wsprintf(szMessage, szResource, szFileName);
Vincent Béron9a624912002-05-31 23:06:46 +000094
Marcel Baur03287451999-02-14 11:28:37 +000095 /* Load szCaption */
Dmitry Timoshkov398af162003-07-21 20:05:33 +000096 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
Marcel Baur03287451999-02-14 11:28:37 +000097
98 /* Display Modal Dialog */
Dmitry Timoshkov398af162003-07-21 20:05:33 +000099 MessageBox(Globals.hMainWnd, szMessage, szResource, MB_ICONEXCLAMATION);
Marcel Baur03287451999-02-14 11:28:37 +0000100}
101
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000102static int AlertFileNotSaved(LPCWSTR szFileName)
103{
104 WCHAR szMessage[MAX_STRING_LEN];
105 WCHAR szResource[MAX_STRING_LEN];
106 WCHAR szUntitled[MAX_STRING_LEN];
Marcel Baur03287451999-02-14 11:28:37 +0000107
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000108 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
Marcel Baur03287451999-02-14 11:28:37 +0000109
110 /* Load and format Message */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000111 LoadString(Globals.hInstance, STRING_NOTSAVED, szResource, SIZEOF(szResource));
112 wsprintf(szMessage, szResource, szFileName[0] ? szFileName : szUntitled);
Vincent Béron9a624912002-05-31 23:06:46 +0000113
Marcel Baur03287451999-02-14 11:28:37 +0000114 /* Load Caption */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000115 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
Marcel Baur03287451999-02-14 11:28:37 +0000116
117 /* Display modal */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000118 return MessageBox(Globals.hMainWnd, szMessage, szResource, MB_ICONEXCLAMATION|MB_YESNOCANCEL);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000119}
120
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000121/**
122 * Returns:
123 * TRUE - if file exists
124 * FALSE - if file does not exist
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000125 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000126BOOL FileExists(LPCWSTR szFilename)
127{
Marcel Baura43295d1998-10-18 14:11:42 +0000128 WIN32_FIND_DATA entry;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000129 HANDLE hFile;
Vincent Béron9a624912002-05-31 23:06:46 +0000130
Marcel Baur03287451999-02-14 11:28:37 +0000131 hFile = FindFirstFile(szFilename, &entry);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000132 FindClose(hFile);
Vincent Béron9a624912002-05-31 23:06:46 +0000133
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000134 return (hFile != INVALID_HANDLE_VALUE);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000135}
136
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000137
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000138static VOID DoSaveFile(VOID)
139{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000140 HANDLE hFile;
141 DWORD dwNumWrite;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000142 LPSTR pTemp;
143 DWORD size;
Marcel Baur03287451999-02-14 11:28:37 +0000144
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000145 hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
146 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
147 if(hFile == INVALID_HANDLE_VALUE)
148 {
149 ShowLastError();
150 return;
151 }
152
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000153 size = GetWindowTextLengthA(Globals.hEdit) + 1;
154 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000155 if (!pTemp)
156 {
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000157 CloseHandle(hFile);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000158 ShowLastError();
159 return;
160 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000161 size = GetWindowTextA(Globals.hEdit, pTemp, size);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000162
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000163 if (!WriteFile(hFile, pTemp, size, &dwNumWrite, NULL))
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000164 ShowLastError();
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000165
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000166 CloseHandle(hFile);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000167 HeapFree(GetProcessHeap(), 0, pTemp);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000168}
169
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000170/**
171 * Returns:
172 * TRUE - User agreed to close (both save/don't save)
173 * FALSE - User cancelled close by selecting "Cancel"
174 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000175BOOL DoCloseFile(void)
176{
Marcel Baur03287451999-02-14 11:28:37 +0000177 int nResult;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000178 static const WCHAR empty_strW[] = { 0 };
Vincent Béron9a624912002-05-31 23:06:46 +0000179
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000180 if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
181 {
Marcel Baur03287451999-02-14 11:28:37 +0000182 /* prompt user to save changes */
183 nResult = AlertFileNotSaved(Globals.szFileName);
184 switch (nResult) {
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000185 case IDYES: DIALOG_FileSave();
Marcel Baur03287451999-02-14 11:28:37 +0000186 break;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000187
Marcel Baur03287451999-02-14 11:28:37 +0000188 case IDNO: break;
Marcel Baura43295d1998-10-18 14:11:42 +0000189
Marcel Baur03287451999-02-14 11:28:37 +0000190 case IDCANCEL: return(FALSE);
191 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000192
Marcel Baur03287451999-02-14 11:28:37 +0000193 default: return(FALSE);
194 break;
195 } /* switch */
196 } /* if */
Vincent Béron9a624912002-05-31 23:06:46 +0000197
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000198 SetFileName(empty_strW);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000199
200 UpdateWindowCaption();
Marcel Baur03287451999-02-14 11:28:37 +0000201 return(TRUE);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000202}
203
204
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000205void DoOpenFile(LPCWSTR szFileName)
206{
207 HANDLE hFile;
208 LPSTR pTemp;
209 DWORD size;
210 DWORD dwNumRead;
211
Marcus Meissner73458b01998-12-26 12:54:29 +0000212 /* Close any files and prompt to save changes */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000213 if (!DoCloseFile())
214 return;
215
216 hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
217 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
218 if(hFile == INVALID_HANDLE_VALUE)
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000219 {
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000220 ShowLastError();
221 return;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000222 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000223
224 size = GetFileSize(hFile, NULL);
225 if (size == 0xFFFFFFFF)
226 {
227 CloseHandle(hFile);
228 ShowLastError();
229 return;
230 }
231 size++;
232
233 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
234 if (!pTemp)
235 {
236 CloseHandle(hFile);
237 ShowLastError();
238 return;
239 }
240
241 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
242 {
243 CloseHandle(hFile);
244 HeapFree(GetProcessHeap(), 0, pTemp);
245 ShowLastError();
246 return;
247 }
248
249 CloseHandle(hFile);
250 pTemp[dwNumRead] = 0;
251
252 if (IsTextUnicode(pTemp, dwNumRead, NULL))
253 {
254 LPWSTR p = (LPWSTR)pTemp;
255 /* We need to strip BOM Unicode character, SetWindowTextW won't do it for us. */
256 if (*p == 0xFEFF || *p == 0xFFFE) p++;
257 SetWindowTextW(Globals.hEdit, p);
258 }
259 else
260 SetWindowTextA(Globals.hEdit, pTemp);
261
262 HeapFree(GetProcessHeap(), 0, pTemp);
263
264 SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
265 SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
266 SetFocus(Globals.hEdit);
267
268 SetFileName(szFileName);
269 UpdateWindowCaption();
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000270}
271
Alexandre Julliard03468f71998-02-15 19:40:49 +0000272VOID DIALOG_FileNew(VOID)
273{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000274 static const WCHAR empty_strW[] = { 0 };
275
Marcus Meissner73458b01998-12-26 12:54:29 +0000276 /* Close any files and promt to save changes */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000277 if (DoCloseFile()) {
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000278 SetWindowText(Globals.hEdit, empty_strW);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000279 SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
280 SetFocus(Globals.hEdit);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000281 }
Alexandre Julliard03468f71998-02-15 19:40:49 +0000282}
283
284VOID DIALOG_FileOpen(VOID)
285{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000286 OPENFILENAME openfilename;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000287 WCHAR szPath[MAX_PATH];
288 WCHAR szDir[MAX_PATH];
289 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
290 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
Marcel Baura43295d1998-10-18 14:11:42 +0000291
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000292 ZeroMemory(&openfilename, sizeof(openfilename));
Alexandre Julliard03468f71998-02-15 19:40:49 +0000293
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000294 GetCurrentDirectory(SIZEOF(szDir), szDir);
295 lstrcpy(szPath, txt_files);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000296
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000297 openfilename.lStructSize = sizeof(openfilename);
298 openfilename.hwndOwner = Globals.hMainWnd;
299 openfilename.hInstance = Globals.hInstance;
300 openfilename.lpstrFilter = Globals.szFilter;
301 openfilename.lpstrFile = szPath;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000302 openfilename.nMaxFile = SIZEOF(szPath);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000303 openfilename.lpstrInitialDir = szDir;
304 openfilename.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
305 OFN_HIDEREADONLY;
306 openfilename.lpstrDefExt = szDefaultExt;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000307
Vincent Béron9a624912002-05-31 23:06:46 +0000308
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000309 if (GetOpenFileName(&openfilename)) {
310 if (FileExists(openfilename.lpstrFile))
311 DoOpenFile(openfilename.lpstrFile);
312 else
313 AlertFileNotFound(openfilename.lpstrFile);
314 }
Alexandre Julliard03468f71998-02-15 19:40:49 +0000315}
316
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000317
Alexandre Julliard03468f71998-02-15 19:40:49 +0000318VOID DIALOG_FileSave(VOID)
319{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000320 if (Globals.szFileName[0] == '\0')
Marcel Baur03287451999-02-14 11:28:37 +0000321 DIALOG_FileSaveAs();
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000322 else
323 DoSaveFile();
Alexandre Julliard03468f71998-02-15 19:40:49 +0000324}
325
326VOID DIALOG_FileSaveAs(VOID)
327{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000328 OPENFILENAME saveas;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000329 WCHAR szPath[MAX_PATH];
330 WCHAR szDir[MAX_PATH];
331 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
332 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
Vincent Béron9a624912002-05-31 23:06:46 +0000333
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000334 ZeroMemory(&saveas, sizeof(saveas));
Alexandre Julliard03468f71998-02-15 19:40:49 +0000335
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000336 GetCurrentDirectory(SIZEOF(szDir), szDir);
337 lstrcpy(szPath, txt_files);
Marcel Baura43295d1998-10-18 14:11:42 +0000338
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000339 saveas.lStructSize = sizeof(OPENFILENAME);
340 saveas.hwndOwner = Globals.hMainWnd;
341 saveas.hInstance = Globals.hInstance;
342 saveas.lpstrFilter = Globals.szFilter;
343 saveas.lpstrFile = szPath;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000344 saveas.nMaxFile = SIZEOF(szPath);
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000345 saveas.lpstrInitialDir = szDir;
346 saveas.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
347 OFN_HIDEREADONLY;
348 saveas.lpstrDefExt = szDefaultExt;
Alexandre Julliard03468f71998-02-15 19:40:49 +0000349
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000350 if (GetSaveFileName(&saveas)) {
351 SetFileName(szPath);
352 UpdateWindowCaption();
353 DoSaveFile();
354 }
Alexandre Julliard03468f71998-02-15 19:40:49 +0000355}
356
357VOID DIALOG_FilePrint(VOID)
358{
Duane Clark4eb4c042002-10-21 18:22:15 +0000359 DOCINFO di;
Duane Clark4eb4c042002-10-21 18:22:15 +0000360 PRINTDLG printer;
Duane Clark4eb4c042002-10-21 18:22:15 +0000361 SIZE szMetric;
362 int cWidthPels, cHeightPels, border;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000363 int xLeft, yTop, i, pagecount, dopage, copycount;
Duane Clark4eb4c042002-10-21 18:22:15 +0000364 LOGFONT hdrFont;
365 HFONT font, old_font=0;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000366 DWORD size;
367 LPWSTR pTemp;
368 static const WCHAR times_new_romanW[] = { 'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0 };
Marcel Baur03287451999-02-14 11:28:37 +0000369
Duane Clark4eb4c042002-10-21 18:22:15 +0000370 /* Get a small font and print some header info on each page */
371 hdrFont.lfHeight = 100;
372 hdrFont.lfWidth = 0;
373 hdrFont.lfEscapement = 0;
374 hdrFont.lfOrientation = 0;
375 hdrFont.lfWeight = FW_BOLD;
376 hdrFont.lfItalic = 0;
377 hdrFont.lfUnderline = 0;
378 hdrFont.lfStrikeOut = 0;
379 hdrFont.lfCharSet = ANSI_CHARSET;
380 hdrFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
381 hdrFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
382 hdrFont.lfQuality = PROOF_QUALITY;
383 hdrFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000384 lstrcpy(hdrFont.lfFaceName, times_new_romanW);
Duane Clark4eb4c042002-10-21 18:22:15 +0000385
386 font = CreateFontIndirect(&hdrFont);
387
388 /* Get Current Settings */
389 ZeroMemory(&printer, sizeof(printer));
390 printer.lStructSize = sizeof(printer);
391 printer.hwndOwner = Globals.hMainWnd;
392 printer.hInstance = Globals.hInstance;
393
394 /* Set some default flags */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000395 printer.Flags = PD_RETURNDC;
396 printer.nFromPage = 0;
Duane Clark4eb4c042002-10-21 18:22:15 +0000397 printer.nMinPage = 1;
398 /* we really need to calculate number of pages to set nMaxPage and nToPage */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000399 printer.nToPage = 0;
400 printer.nMaxPage = -1;
Marcel Baur03287451999-02-14 11:28:37 +0000401
Duane Clark4eb4c042002-10-21 18:22:15 +0000402 /* Let commdlg manage copy settings */
403 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
Marcel Baur03287451999-02-14 11:28:37 +0000404
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000405 if (!PrintDlg(&printer)) return;
Marcel Baur03287451999-02-14 11:28:37 +0000406
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000407 assert(printer.hDC != 0);
408
Duane Clark4eb4c042002-10-21 18:22:15 +0000409 /* initialize DOCINFO */
410 di.cbSize = sizeof(DOCINFO);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000411 di.lpszDocName = Globals.szFileTitle;
412 di.lpszOutput = NULL;
413 di.lpszDatatype = NULL;
Duane Clark4eb4c042002-10-21 18:22:15 +0000414 di.fwType = 0;
Marcel Baur03287451999-02-14 11:28:37 +0000415
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000416 if (StartDoc(printer.hDC, &di) <= 0) return;
417
Duane Clark4eb4c042002-10-21 18:22:15 +0000418 /* Get the page dimensions in pixels. */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000419 cWidthPels = GetDeviceCaps(printer.hDC, HORZRES);
420 cHeightPels = GetDeviceCaps(printer.hDC, VERTRES);
Marcel Baur03287451999-02-14 11:28:37 +0000421
Duane Clark4eb4c042002-10-21 18:22:15 +0000422 /* Get the file text */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000423 size = GetWindowTextLength(Globals.hEdit) + 1;
424 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
Duane Clark4eb4c042002-10-21 18:22:15 +0000425 if (!pTemp)
426 {
427 ShowLastError();
428 return;
429 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000430 size = GetWindowText(Globals.hEdit, pTemp, size);
Duane Clark4eb4c042002-10-21 18:22:15 +0000431
Duane Clark4eb4c042002-10-21 18:22:15 +0000432 border = 150;
433 for (copycount=1; copycount <= printer.nCopies; copycount++) {
434 i = 0;
435 pagecount = 1;
436 do {
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000437 static const WCHAR letterM[] = { 'M',0 };
438
Duane Clark4eb4c042002-10-21 18:22:15 +0000439 if (pagecount >= printer.nFromPage &&
440 /* ((printer.Flags & PD_PAGENUMS) == 0 || pagecount <= printer.nToPage))*/
441 pagecount <= printer.nToPage)
442 dopage = 1;
443 else
444 dopage = 0;
445
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000446 old_font = SelectObject(printer.hDC, font);
447 GetTextExtentPoint32(printer.hDC, letterM, 1, &szMetric);
Duane Clark4eb4c042002-10-21 18:22:15 +0000448
449 if (dopage) {
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000450 if (StartPage(printer.hDC) <= 0) {
451 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
452 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
453 MessageBox(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
Duane Clark4eb4c042002-10-21 18:22:15 +0000454 return;
455 }
456 /* Write a rectangle and header at the top of each page */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000457 Rectangle(printer.hDC, border, border, cWidthPels-border, border+szMetric.cy*2);
Duane Clark4eb4c042002-10-21 18:22:15 +0000458 /* I don't know what's up with this TextOut command. This comes out
459 kind of mangled.
460 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000461 TextOut(printer.hDC, border*2, border+szMetric.cy/2, Globals.szFileTitle, lstrlen(Globals.szFileTitle));
Duane Clark4eb4c042002-10-21 18:22:15 +0000462 }
463
464 /* The starting point for the main text */
465 xLeft = border*2;
466 yTop = border+szMetric.cy*4;
467
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000468 SelectObject(printer.hDC, old_font);
469 GetTextExtentPoint32(printer.hDC, letterM, 1, &szMetric);
Duane Clark4eb4c042002-10-21 18:22:15 +0000470
471 /* Since outputting strings is giving me problems, output the main
472 text one character at a time.
473 */
474 do {
475 if (pTemp[i] == '\n') {
476 xLeft = border*2;
477 yTop += szMetric.cy;
478 }
479 else if (pTemp[i] != '\r') {
480 if (dopage)
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000481 TextOut(printer.hDC, xLeft, yTop, &pTemp[i], 1);
Duane Clark4eb4c042002-10-21 18:22:15 +0000482 xLeft += szMetric.cx;
483 }
484 } while (i++<size && yTop<(cHeightPels-border*2));
485
486 if (dopage)
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000487 EndPage(printer.hDC);
Duane Clark4eb4c042002-10-21 18:22:15 +0000488 pagecount++;
489 } while (i<size);
490 }
Marcel Baur03287451999-02-14 11:28:37 +0000491
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000492 EndDoc(printer.hDC);
493 DeleteDC(printer.hDC);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000494}
495
496VOID DIALOG_FilePrinterSetup(VOID)
497{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000498 PRINTDLG printer;
Marcel Baur03287451999-02-14 11:28:37 +0000499
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000500 ZeroMemory(&printer, sizeof(printer));
501 printer.lStructSize = sizeof(printer);
502 printer.hwndOwner = Globals.hMainWnd;
503 printer.hInstance = Globals.hInstance;
504 printer.Flags = PD_PRINTSETUP;
505 printer.nCopies = 1;
Vincent Béron9a624912002-05-31 23:06:46 +0000506
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000507 PrintDlg(&printer);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000508}
509
510VOID DIALOG_FileExit(VOID)
511{
Andriy Palamarchuk83ad8862002-07-16 01:09:24 +0000512 PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000513}
514
515VOID DIALOG_EditUndo(VOID)
516{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000517 SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000518}
519
520VOID DIALOG_EditCut(VOID)
521{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000522 SendMessage(Globals.hEdit, WM_CUT, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000523}
524
525VOID DIALOG_EditCopy(VOID)
526{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000527 SendMessage(Globals.hEdit, WM_COPY, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000528}
529
530VOID DIALOG_EditPaste(VOID)
531{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000532 SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000533}
534
535VOID DIALOG_EditDelete(VOID)
536{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000537 SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000538}
539
540VOID DIALOG_EditSelectAll(VOID)
541{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000542 SendMessage(Globals.hEdit, EM_SETSEL, 0, (LPARAM)-1);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000543}
544
545VOID DIALOG_EditTimeDate(VOID)
546{
Marcel Baur03287451999-02-14 11:28:37 +0000547 SYSTEMTIME st;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000548 WCHAR szDate[MAX_STRING_LEN];
549 static const WCHAR spaceW[] = { ' ',0 };
Vincent Béron9a624912002-05-31 23:06:46 +0000550
Marcel Baur03287451999-02-14 11:28:37 +0000551 GetLocalTime(&st);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000552
553 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
554 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
555
556 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
557
558 GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
559 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000560}
561
562VOID DIALOG_EditWrap(VOID)
563{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000564 Globals.bWrapLongLines = !Globals.bWrapLongLines;
565 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
566 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
Alexandre Julliard03468f71998-02-15 19:40:49 +0000567}
568
Shachar Shemeshfc0d07f2003-01-28 01:10:28 +0000569VOID DIALOG_SelectFont(VOID)
570{
571 CHOOSEFONT cf;
572 LOGFONT lf=Globals.lfFont;
573
574 ZeroMemory( &cf, sizeof(cf) );
575 cf.lStructSize=sizeof(cf);
576 cf.hwndOwner=Globals.hMainWnd;
577 cf.lpLogFont=&lf;
578 cf.Flags=CF_SCREENFONTS;
579
580 if( ChooseFont(&cf) )
581 {
582 HFONT currfont=Globals.hFont;
583
584 Globals.hFont=CreateFontIndirect( &lf );
585 Globals.lfFont=lf;
586 SendMessage( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE );
587 if( currfont!=NULL )
588 DeleteObject( currfont );
589 }
590}
591
Alexandre Julliard03468f71998-02-15 19:40:49 +0000592VOID DIALOG_Search(VOID)
593{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000594 ZeroMemory(&Globals.find, sizeof(Globals.find));
Marcel Baur03287451999-02-14 11:28:37 +0000595 Globals.find.lStructSize = sizeof(Globals.find);
596 Globals.find.hwndOwner = Globals.hMainWnd;
597 Globals.find.hInstance = Globals.hInstance;
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000598 Globals.find.lpstrFindWhat = Globals.szFindText;
599 Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
Marcel Baur03287451999-02-14 11:28:37 +0000600 Globals.find.Flags = FR_DOWN;
Marcel Baura43295d1998-10-18 14:11:42 +0000601
Marcel Baur03287451999-02-14 11:28:37 +0000602 /* We only need to create the modal FindReplace dialog which will */
603 /* notify us of incoming events using hMainWnd Window Messages */
Alexandre Julliard54c27111998-03-29 19:44:57 +0000604
Marcel Baur03287451999-02-14 11:28:37 +0000605 Globals.hFindReplaceDlg = FindText(&Globals.find);
606 assert(Globals.hFindReplaceDlg !=0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000607}
608
609VOID DIALOG_SearchNext(VOID)
610{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000611 /* FIXME: Search Next */
612 DIALOG_Search();
Alexandre Julliard03468f71998-02-15 19:40:49 +0000613}
614
615VOID DIALOG_HelpContents(VOID)
616{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000617 WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000618}
619
620VOID DIALOG_HelpSearch(VOID)
621{
Marcel Baur03287451999-02-14 11:28:37 +0000622 /* Search Help */
Alexandre Julliard03468f71998-02-15 19:40:49 +0000623}
624
625VOID DIALOG_HelpHelp(VOID)
626{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000627 WinHelp(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000628}
629
630VOID DIALOG_HelpLicense(VOID)
631{
Alexandre Julliard3da872d2000-11-10 01:06:36 +0000632 WineLicense(Globals.hMainWnd);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000633}
634
635VOID DIALOG_HelpNoWarranty(VOID)
636{
Alexandre Julliard3da872d2000-11-10 01:06:36 +0000637 WineWarranty(Globals.hMainWnd);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000638}
639
640VOID DIALOG_HelpAboutWine(VOID)
641{
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000642 static const WCHAR notepadW[] = { 'N','o','t','e','p','a','d','\n',0 };
643 WCHAR szNotepad[MAX_STRING_LEN];
Marcel Baur03287451999-02-14 11:28:37 +0000644
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000645 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
646 ShellAbout(Globals.hMainWnd, szNotepad, notepadW, 0);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000647}
648
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000649
Alexandre Julliard03468f71998-02-15 19:40:49 +0000650/***********************************************************************
651 *
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000652 * DIALOG_FilePageSetup
Alexandre Julliard03468f71998-02-15 19:40:49 +0000653 */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000654VOID DIALOG_FilePageSetup(void)
Alexandre Julliard03468f71998-02-15 19:40:49 +0000655{
Andriy Palamarchukc55dce02002-07-08 19:41:09 +0000656 DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000657 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
Alexandre Julliard03468f71998-02-15 19:40:49 +0000658}
659
Alexandre Julliard03468f71998-02-15 19:40:49 +0000660
661/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
662 *
663 * DIALOG_PAGESETUP_DlgProc
664 */
665
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000666static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard03468f71998-02-15 19:40:49 +0000667{
Marcel Baur03287451999-02-14 11:28:37 +0000668
669 switch (msg)
Alexandre Julliard03468f71998-02-15 19:40:49 +0000670 {
671 case WM_COMMAND:
672 switch (wParam)
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000673 {
674 case IDOK:
Marcel Baur03287451999-02-14 11:28:37 +0000675 /* save user input and close dialog */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000676 GetDlgItemText(hDlg, 0x141, Globals.szHeader, SIZEOF(Globals.szHeader));
677 GetDlgItemText(hDlg, 0x143, Globals.szFooter, SIZEOF(Globals.szFooter));
678 GetDlgItemText(hDlg, 0x14A, Globals.szMarginTop, SIZEOF(Globals.szMarginTop));
679 GetDlgItemText(hDlg, 0x150, Globals.szMarginBottom, SIZEOF(Globals.szMarginBottom));
680 GetDlgItemText(hDlg, 0x147, Globals.szMarginLeft, SIZEOF(Globals.szMarginLeft));
681 GetDlgItemText(hDlg, 0x14D, Globals.szMarginRight, SIZEOF(Globals.szMarginRight));
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000682 EndDialog(hDlg, IDOK);
683 return TRUE;
Alexandre Julliard03468f71998-02-15 19:40:49 +0000684
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000685 case IDCANCEL:
Marcel Baur03287451999-02-14 11:28:37 +0000686 /* discard user input and close dialog */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000687 EndDialog(hDlg, IDCANCEL);
688 return TRUE;
Marcel Baur03287451999-02-14 11:28:37 +0000689
690 case IDHELP:
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000691 {
Marcel Baur03287451999-02-14 11:28:37 +0000692 /* FIXME: Bring this to work */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000693 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 };
694 static const WCHAR helpW[] = { 'H','e','l','p',0 };
695 MessageBox(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
Marcel Baur03287451999-02-14 11:28:37 +0000696 return TRUE;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000697 }
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000698
699 default:
700 break;
701 }
Marcel Baura43295d1998-10-18 14:11:42 +0000702 break;
703
704 case WM_INITDIALOG:
Marcel Baur03287451999-02-14 11:28:37 +0000705 /* fetch last user input prior to display dialog */
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000706 SetDlgItemText(hDlg, 0x141, Globals.szHeader);
707 SetDlgItemText(hDlg, 0x143, Globals.szFooter);
708 SetDlgItemText(hDlg, 0x14A, Globals.szMarginTop);
Sylvain Petreolleeaa8df62002-03-20 22:55:46 +0000709 SetDlgItemText(hDlg, 0x150, Globals.szMarginBottom);
Dmitry Timoshkov398af162003-07-21 20:05:33 +0000710 SetDlgItemText(hDlg, 0x147, Globals.szMarginLeft);
711 SetDlgItemText(hDlg, 0x14D, Globals.szMarginRight);
Marcel Baura43295d1998-10-18 14:11:42 +0000712 break;
Alexandre Julliard03468f71998-02-15 19:40:49 +0000713 }
Marcel Baur03287451999-02-14 11:28:37 +0000714
Alexandre Julliard03468f71998-02-15 19:40:49 +0000715 return FALSE;
716}