dialog.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Notepad (dialog.c)
  3. *
  4. * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
  5. * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
  6. * Copyright 2002 Andriy Palamarchuk
  7. * Copyright 2007 Rolf Kalbermatter
  8. * Copyright 2010 Vitaly Perov
  9. *
  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
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include <windows.h>
  27. #include <shellapi.h>
  28. #include <commdlg.h>
  29. #include <shlwapi.h>
  30. #include <winternl.h>
  31. #include "main.h"
  32. #include "dialog.h"
  33. #define SPACES_IN_TAB 8
  34. #define PRINT_LEN_MAX 500
  35. static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
  36. static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
  37. /* Swap bytes of WCHAR buffer (big-endian <-> little-endian). */
  38. static inline void byteswap_wide_string(LPWSTR str, UINT num)
  39. {
  40. UINT i;
  41. for (i = 0; i < num; i++) str[i] = RtlUshortByteSwap(str[i]);
  42. }
  43. static void load_encoding_name(ENCODING enc, WCHAR* buffer, int length)
  44. {
  45. switch (enc)
  46. {
  47. case ENCODING_UTF16LE:
  48. LoadStringW(Globals.hInstance, STRING_UNICODE_LE, buffer, length);
  49. break;
  50. case ENCODING_UTF16BE:
  51. LoadStringW(Globals.hInstance, STRING_UNICODE_BE, buffer, length);
  52. break;
  53. case ENCODING_UTF8:
  54. LoadStringW(Globals.hInstance, STRING_UTF8, buffer, length);
  55. break;
  56. case ENCODING_ANSI:
  57. {
  58. CPINFOEXW cpi;
  59. GetCPInfoExW(CP_ACP, 0, &cpi);
  60. lstrcpynW(buffer, cpi.CodePageName, length);
  61. break;
  62. }
  63. default:
  64. assert(0 && "bad encoding in load_encoding_name");
  65. break;
  66. }
  67. }
  68. VOID ShowLastError(void)
  69. {
  70. DWORD error = GetLastError();
  71. if (error != NO_ERROR)
  72. {
  73. LPWSTR lpMsgBuf;
  74. WCHAR szTitle[MAX_STRING_LEN];
  75. LoadStringW(Globals.hInstance, STRING_ERROR, szTitle, ARRAY_SIZE(szTitle));
  76. FormatMessageW(
  77. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  78. NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
  79. MessageBoxW(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
  80. LocalFree(lpMsgBuf);
  81. }
  82. }
  83. /**
  84. * Sets the caption of the main window according to Globals.szFileTitle:
  85. * Untitled - Notepad if no file is open
  86. * filename - Notepad if a file is given
  87. */
  88. void UpdateWindowCaption(void)
  89. {
  90. static const WCHAR hyphenW[] = { ' ','-',' ',0 };
  91. WCHAR szNotepad[64];
  92. WCHAR szCaption[ARRAY_SIZE(Globals.szFileTitle) + ARRAY_SIZE(hyphenW) + ARRAY_SIZE(szNotepad)];
  93. if (Globals.szFileTitle[0] != '\0')
  94. lstrcpyW(szCaption, Globals.szFileTitle);
  95. else
  96. LoadStringW(Globals.hInstance, STRING_UNTITLED, szCaption, ARRAY_SIZE(szCaption));
  97. LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
  98. lstrcatW(szCaption, hyphenW);
  99. lstrcatW(szCaption, szNotepad);
  100. SetWindowTextW(Globals.hMainWnd, szCaption);
  101. }
  102. int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
  103. {
  104. WCHAR szMessage[MAX_STRING_LEN];
  105. WCHAR szResource[MAX_STRING_LEN];
  106. /* Load and format szMessage */
  107. LoadStringW(Globals.hInstance, formatId, szResource, ARRAY_SIZE(szResource));
  108. wnsprintfW(szMessage, ARRAY_SIZE(szMessage), szResource, szString);
  109. /* Load szCaption */
  110. if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
  111. LoadStringW(Globals.hInstance, STRING_ERROR, szResource, ARRAY_SIZE(szResource));
  112. else
  113. LoadStringW(Globals.hInstance, STRING_NOTEPAD, szResource, ARRAY_SIZE(szResource));
  114. /* Display Modal Dialog */
  115. if (hParent == NULL)
  116. hParent = Globals.hMainWnd;
  117. return MessageBoxW(hParent, szMessage, szResource, dwFlags);
  118. }
  119. static void AlertFileNotFound(LPCWSTR szFileName)
  120. {
  121. DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
  122. }
  123. static int AlertFileNotSaved(LPCWSTR szFileName)
  124. {
  125. WCHAR szUntitled[MAX_STRING_LEN];
  126. LoadStringW(Globals.hInstance, STRING_UNTITLED, szUntitled, ARRAY_SIZE(szUntitled));
  127. return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
  128. MB_ICONQUESTION|MB_YESNOCANCEL);
  129. }
  130. static int AlertUnicodeCharactersLost(LPCWSTR szFileName)
  131. {
  132. WCHAR szCaption[MAX_STRING_LEN];
  133. WCHAR szMsgFormat[MAX_STRING_LEN];
  134. WCHAR szEnc[MAX_STRING_LEN];
  135. WCHAR* szMsg;
  136. DWORD_PTR args[2];
  137. int rc;
  138. LoadStringW(Globals.hInstance, STRING_NOTEPAD, szCaption,
  139. ARRAY_SIZE(szCaption));
  140. LoadStringW(Globals.hInstance, STRING_LOSS_OF_UNICODE_CHARACTERS,
  141. szMsgFormat, ARRAY_SIZE(szMsgFormat));
  142. load_encoding_name(ENCODING_ANSI, szEnc, ARRAY_SIZE(szEnc));
  143. args[0] = (DWORD_PTR)szFileName;
  144. args[1] = (DWORD_PTR)szEnc;
  145. FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_ARGUMENT_ARRAY, szMsgFormat, 0, 0, (LPWSTR)&szMsg, 0, (__ms_va_list*)args);
  146. rc = MessageBoxW(Globals.hMainWnd, szMsg, szCaption,
  147. MB_OKCANCEL|MB_ICONEXCLAMATION);
  148. LocalFree(szMsg);
  149. return rc;
  150. }
  151. /**
  152. * Returns:
  153. * TRUE - if file exists
  154. * FALSE - if file does not exist
  155. */
  156. BOOL FileExists(LPCWSTR szFilename)
  157. {
  158. WIN32_FIND_DATAW entry;
  159. HANDLE hFile;
  160. hFile = FindFirstFileW(szFilename, &entry);
  161. FindClose(hFile);
  162. return (hFile != INVALID_HANDLE_VALUE);
  163. }
  164. static inline BOOL is_conversion_to_ansi_lossy(LPCWSTR textW, int lenW)
  165. {
  166. BOOL ret = FALSE;
  167. WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, textW, lenW, NULL, 0,
  168. NULL, &ret);
  169. return ret;
  170. }
  171. typedef enum
  172. {
  173. SAVED_OK,
  174. SAVE_FAILED,
  175. SHOW_SAVEAS_DIALOG
  176. } SAVE_STATUS;
  177. /* szFileName is the filename to save under; enc is the encoding to use.
  178. *
  179. * If the function succeeds, it returns SAVED_OK.
  180. * If the function fails, it returns SAVE_FAILED.
  181. * If Unicode data could be lost due to conversion to a non-Unicode character
  182. * set, a warning is displayed. The user can continue (and the function carries
  183. * on), or cancel (and the function returns SHOW_SAVEAS_DIALOG).
  184. */
  185. static SAVE_STATUS DoSaveFile(LPCWSTR szFileName, ENCODING enc)
  186. {
  187. int lenW;
  188. WCHAR* textW;
  189. HANDLE hFile;
  190. DWORD dwNumWrite;
  191. PVOID pBytes;
  192. DWORD size;
  193. /* lenW includes the byte-order mark, but not the \0. */
  194. lenW = GetWindowTextLengthW(Globals.hEdit) + 1;
  195. textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
  196. if (!textW)
  197. {
  198. ShowLastError();
  199. return SAVE_FAILED;
  200. }
  201. textW[0] = (WCHAR) 0xfeff;
  202. lenW = GetWindowTextW(Globals.hEdit, textW+1, lenW) + 1;
  203. switch (enc)
  204. {
  205. case ENCODING_UTF16BE:
  206. byteswap_wide_string(textW, lenW);
  207. /* fall through */
  208. case ENCODING_UTF16LE:
  209. size = lenW * sizeof(WCHAR);
  210. pBytes = textW;
  211. break;
  212. case ENCODING_UTF8:
  213. size = WideCharToMultiByte(CP_UTF8, 0, textW, lenW, NULL, 0, NULL, NULL);
  214. pBytes = HeapAlloc(GetProcessHeap(), 0, size);
  215. if (!pBytes)
  216. {
  217. ShowLastError();
  218. HeapFree(GetProcessHeap(), 0, textW);
  219. return SAVE_FAILED;
  220. }
  221. WideCharToMultiByte(CP_UTF8, 0, textW, lenW, pBytes, size, NULL, NULL);
  222. HeapFree(GetProcessHeap(), 0, textW);
  223. break;
  224. default:
  225. if (is_conversion_to_ansi_lossy(textW+1, lenW-1)
  226. && AlertUnicodeCharactersLost(szFileName) == IDCANCEL)
  227. {
  228. HeapFree(GetProcessHeap(), 0, textW);
  229. return SHOW_SAVEAS_DIALOG;
  230. }
  231. size = WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, NULL, 0, NULL, NULL);
  232. pBytes = HeapAlloc(GetProcessHeap(), 0, size);
  233. if (!pBytes)
  234. {
  235. ShowLastError();
  236. HeapFree(GetProcessHeap(), 0, textW);
  237. return SAVE_FAILED;
  238. }
  239. WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, pBytes, size, NULL, NULL);
  240. HeapFree(GetProcessHeap(), 0, textW);
  241. break;
  242. }
  243. hFile = CreateFileW(szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
  244. NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  245. if(hFile == INVALID_HANDLE_VALUE)
  246. {
  247. ShowLastError();
  248. HeapFree(GetProcessHeap(), 0, pBytes);
  249. return SAVE_FAILED;
  250. }
  251. if (!WriteFile(hFile, pBytes, size, &dwNumWrite, NULL))
  252. {
  253. ShowLastError();
  254. CloseHandle(hFile);
  255. HeapFree(GetProcessHeap(), 0, pBytes);
  256. return SAVE_FAILED;
  257. }
  258. SetEndOfFile(hFile);
  259. CloseHandle(hFile);
  260. HeapFree(GetProcessHeap(), 0, pBytes);
  261. SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
  262. return SAVED_OK;
  263. }
  264. /**
  265. * Returns:
  266. * TRUE - User agreed to close (both save/don't save)
  267. * FALSE - User cancelled close by selecting "Cancel"
  268. */
  269. BOOL DoCloseFile(void)
  270. {
  271. int nResult;
  272. static const WCHAR empty_strW[] = { 0 };
  273. nResult=GetWindowTextLengthW(Globals.hEdit);
  274. if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0) &&
  275. (nResult || Globals.szFileName[0]))
  276. {
  277. /* prompt user to save changes */
  278. nResult = AlertFileNotSaved(Globals.szFileName);
  279. switch (nResult) {
  280. case IDYES: return DIALOG_FileSave();
  281. case IDNO: break;
  282. case IDCANCEL: return(FALSE);
  283. default: return(FALSE);
  284. } /* switch */
  285. } /* if */
  286. SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
  287. UpdateWindowCaption();
  288. return(TRUE);
  289. }
  290. static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size)
  291. {
  292. static const char bom_utf8[] = { 0xef, 0xbb, 0xbf };
  293. if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
  294. return ENCODING_UTF8;
  295. else
  296. {
  297. int flags = IS_TEXT_UNICODE_SIGNATURE |
  298. IS_TEXT_UNICODE_REVERSE_SIGNATURE |
  299. IS_TEXT_UNICODE_ODD_LENGTH;
  300. IsTextUnicode(buffer, size, &flags);
  301. if (flags & IS_TEXT_UNICODE_SIGNATURE)
  302. return ENCODING_UTF16LE;
  303. else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
  304. return ENCODING_UTF16BE;
  305. else
  306. return ENCODING_ANSI;
  307. }
  308. }
  309. void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
  310. {
  311. static const WCHAR dotlog[] = { '.','L','O','G',0 };
  312. HANDLE hFile;
  313. LPSTR pTemp;
  314. DWORD size;
  315. DWORD dwNumRead;
  316. int lenW;
  317. WCHAR* textW;
  318. int i;
  319. WCHAR log[5];
  320. /* Close any files and prompt to save changes */
  321. if (!DoCloseFile())
  322. return;
  323. hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  324. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  325. if(hFile == INVALID_HANDLE_VALUE)
  326. {
  327. AlertFileNotFound(szFileName);
  328. return;
  329. }
  330. size = GetFileSize(hFile, NULL);
  331. if (size == INVALID_FILE_SIZE)
  332. {
  333. CloseHandle(hFile);
  334. ShowLastError();
  335. return;
  336. }
  337. /* Extra memory for (WCHAR)'\0'-termination. */
  338. pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
  339. if (!pTemp)
  340. {
  341. CloseHandle(hFile);
  342. ShowLastError();
  343. return;
  344. }
  345. if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
  346. {
  347. CloseHandle(hFile);
  348. HeapFree(GetProcessHeap(), 0, pTemp);
  349. ShowLastError();
  350. return;
  351. }
  352. CloseHandle(hFile);
  353. size = dwNumRead;
  354. if (enc == ENCODING_AUTO)
  355. enc = detect_encoding_of_buffer(pTemp, size);
  356. else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
  357. {
  358. /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
  359. * override the selection (like native Notepad).
  360. */
  361. if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
  362. enc = ENCODING_UTF16LE;
  363. else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
  364. enc = ENCODING_UTF16BE;
  365. }
  366. switch (enc)
  367. {
  368. case ENCODING_UTF16BE:
  369. byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
  370. /* Forget whether the file is BE or LE, like native Notepad. */
  371. enc = ENCODING_UTF16LE;
  372. /* fall through */
  373. case ENCODING_UTF16LE:
  374. textW = (LPWSTR)pTemp;
  375. lenW = size/sizeof(WCHAR);
  376. break;
  377. default:
  378. {
  379. int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
  380. lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
  381. textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
  382. if (!textW)
  383. {
  384. ShowLastError();
  385. HeapFree(GetProcessHeap(), 0, pTemp);
  386. return;
  387. }
  388. MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
  389. HeapFree(GetProcessHeap(), 0, pTemp);
  390. break;
  391. }
  392. }
  393. /* Replace '\0's with spaces. Other than creating a custom control that
  394. * can deal with '\0' characters, it's the best that can be done.
  395. */
  396. for (i = 0; i < lenW; i++)
  397. if (textW[i] == '\0')
  398. textW[i] = ' ';
  399. textW[lenW] = '\0';
  400. if (lenW >= 1 && textW[0] == 0xfeff)
  401. SetWindowTextW(Globals.hEdit, textW+1);
  402. else
  403. SetWindowTextW(Globals.hEdit, textW);
  404. HeapFree(GetProcessHeap(), 0, textW);
  405. SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
  406. SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
  407. SetFocus(Globals.hEdit);
  408. /* If the file starts with .LOG, add a time/date at the end and set cursor after */
  409. if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
  410. {
  411. static const WCHAR lfW[] = { '\r','\n',0 };
  412. SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
  413. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
  414. DIALOG_EditTimeDate();
  415. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
  416. }
  417. SetFileNameAndEncoding(szFileName, enc);
  418. UpdateWindowCaption();
  419. }
  420. VOID DIALOG_FileNew(VOID)
  421. {
  422. static const WCHAR empty_strW[] = { 0 };
  423. /* Close any files and prompt to save changes */
  424. if (DoCloseFile()) {
  425. SetWindowTextW(Globals.hEdit, empty_strW);
  426. SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
  427. SetFocus(Globals.hEdit);
  428. }
  429. }
  430. /* Used to detect encoding of files selected in Open dialog.
  431. * Returns ENCODING_AUTO if file can't be read, etc.
  432. */
  433. static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
  434. {
  435. DWORD size;
  436. HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  437. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  438. if (hFile == INVALID_HANDLE_VALUE)
  439. return ENCODING_AUTO;
  440. size = GetFileSize(hFile, NULL);
  441. if (size == INVALID_FILE_SIZE)
  442. {
  443. CloseHandle(hFile);
  444. return ENCODING_AUTO;
  445. }
  446. else
  447. {
  448. DWORD dwNumRead;
  449. BYTE buffer[MAX_STRING_LEN];
  450. if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
  451. {
  452. CloseHandle(hFile);
  453. return ENCODING_AUTO;
  454. }
  455. CloseHandle(hFile);
  456. return detect_encoding_of_buffer(buffer, dwNumRead);
  457. }
  458. }
  459. static LPWSTR dialog_print_to_file(HWND hMainWnd)
  460. {
  461. OPENFILENAMEW ofn;
  462. static WCHAR file[MAX_PATH] = {'o','u','t','p','u','t','.','p','r','n',0};
  463. static const WCHAR defExt[] = {'p','r','n',0};
  464. ZeroMemory(&ofn, sizeof(ofn));
  465. ofn.lStructSize = sizeof(ofn);
  466. ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  467. ofn.hwndOwner = hMainWnd;
  468. ofn.lpstrFile = file;
  469. ofn.nMaxFile = MAX_PATH;
  470. ofn.lpstrDefExt = defExt;
  471. if(GetSaveFileNameW(&ofn))
  472. return file;
  473. else
  474. return FALSE;
  475. }
  476. static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  477. {
  478. static HWND hEncCombo;
  479. switch (uMsg)
  480. {
  481. case WM_INITDIALOG:
  482. {
  483. ENCODING enc;
  484. hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
  485. for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
  486. {
  487. WCHAR szEnc[MAX_STRING_LEN];
  488. load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
  489. SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
  490. }
  491. SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
  492. }
  493. break;
  494. case WM_COMMAND:
  495. if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
  496. HIWORD(wParam) == CBN_SELCHANGE)
  497. {
  498. int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
  499. Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
  500. }
  501. break;
  502. case WM_NOTIFY:
  503. switch (((OFNOTIFYW*)lParam)->hdr.code)
  504. {
  505. case CDN_SELCHANGE:
  506. if (Globals.bOfnIsOpenDialog)
  507. {
  508. /* Check the start of the selected file for a BOM. */
  509. ENCODING enc;
  510. WCHAR szFileName[MAX_PATH];
  511. SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
  512. ARRAY_SIZE(szFileName), (LPARAM)szFileName);
  513. enc = detect_encoding_of_file(szFileName);
  514. if (enc != ENCODING_AUTO)
  515. {
  516. Globals.encOfnCombo = enc;
  517. SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
  518. }
  519. }
  520. break;
  521. default:
  522. break;
  523. }
  524. break;
  525. default:
  526. break;
  527. }
  528. return 0;
  529. }
  530. VOID DIALOG_FileOpen(VOID)
  531. {
  532. OPENFILENAMEW openfilename;
  533. WCHAR szPath[MAX_PATH];
  534. static const WCHAR szDefaultExt[] = { 't','x','t',0 };
  535. static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
  536. ZeroMemory(&openfilename, sizeof(openfilename));
  537. lstrcpyW(szPath, txt_files);
  538. openfilename.lStructSize = sizeof(openfilename);
  539. openfilename.hwndOwner = Globals.hMainWnd;
  540. openfilename.hInstance = Globals.hInstance;
  541. openfilename.lpstrFilter = Globals.szFilter;
  542. openfilename.lpstrFile = szPath;
  543. openfilename.nMaxFile = ARRAY_SIZE(szPath);
  544. openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
  545. OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
  546. OFN_HIDEREADONLY | OFN_ENABLESIZING;
  547. openfilename.lpfnHook = OfnHookProc;
  548. openfilename.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
  549. openfilename.lpstrDefExt = szDefaultExt;
  550. Globals.encOfnCombo = ENCODING_ANSI;
  551. Globals.bOfnIsOpenDialog = TRUE;
  552. if (GetOpenFileNameW(&openfilename))
  553. DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
  554. }
  555. /* Return FALSE to cancel close */
  556. BOOL DIALOG_FileSave(VOID)
  557. {
  558. if (Globals.szFileName[0] == '\0')
  559. return DIALOG_FileSaveAs();
  560. else
  561. {
  562. switch (DoSaveFile(Globals.szFileName, Globals.encFile))
  563. {
  564. case SAVED_OK: return TRUE;
  565. case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
  566. default: return FALSE;
  567. }
  568. }
  569. }
  570. BOOL DIALOG_FileSaveAs(VOID)
  571. {
  572. OPENFILENAMEW saveas;
  573. WCHAR szPath[MAX_PATH];
  574. static const WCHAR szDefaultExt[] = { 't','x','t',0 };
  575. static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
  576. ZeroMemory(&saveas, sizeof(saveas));
  577. lstrcpyW(szPath, txt_files);
  578. saveas.lStructSize = sizeof(OPENFILENAMEW);
  579. saveas.hwndOwner = Globals.hMainWnd;
  580. saveas.hInstance = Globals.hInstance;
  581. saveas.lpstrFilter = Globals.szFilter;
  582. saveas.lpstrFile = szPath;
  583. saveas.nMaxFile = ARRAY_SIZE(szPath);
  584. saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
  585. OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
  586. OFN_HIDEREADONLY | OFN_ENABLESIZING;
  587. saveas.lpfnHook = OfnHookProc;
  588. saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
  589. saveas.lpstrDefExt = szDefaultExt;
  590. /* Preset encoding to what file was opened/saved last with. */
  591. Globals.encOfnCombo = Globals.encFile;
  592. Globals.bOfnIsOpenDialog = FALSE;
  593. retry:
  594. if (!GetSaveFileNameW(&saveas))
  595. return FALSE;
  596. switch (DoSaveFile(szPath, Globals.encOfnCombo))
  597. {
  598. case SAVED_OK:
  599. SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
  600. UpdateWindowCaption();
  601. return TRUE;
  602. case SHOW_SAVEAS_DIALOG:
  603. goto retry;
  604. default:
  605. return FALSE;
  606. }
  607. }
  608. typedef struct {
  609. LPWSTR mptr;
  610. LPWSTR mend;
  611. LPWSTR lptr;
  612. DWORD len;
  613. } TEXTINFO, *LPTEXTINFO;
  614. static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
  615. {
  616. SIZE szMetric;
  617. if (*text)
  618. {
  619. /* Write the header or footer */
  620. GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
  621. if (dopage)
  622. ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
  623. header ? rc->top : rc->bottom - szMetric.cy,
  624. ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
  625. return 1;
  626. }
  627. return 0;
  628. }
  629. static WCHAR *expand_header_vars(WCHAR *pattern, int page)
  630. {
  631. int length = 0;
  632. int i;
  633. BOOL inside = FALSE;
  634. WCHAR *buffer = NULL;
  635. for (i = 0; pattern[i]; i++)
  636. {
  637. if (inside)
  638. {
  639. if (pattern[i] == '&')
  640. length++;
  641. else if (pattern[i] == 'p')
  642. length += 11;
  643. inside = FALSE;
  644. }
  645. else if (pattern[i] == '&')
  646. inside = TRUE;
  647. else
  648. length++;
  649. }
  650. buffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
  651. if (buffer)
  652. {
  653. int j = 0;
  654. inside = FALSE;
  655. for (i = 0; pattern[i]; i++)
  656. {
  657. if (inside)
  658. {
  659. if (pattern[i] == '&')
  660. buffer[j++] = '&';
  661. else if (pattern[i] == 'p')
  662. {
  663. static const WCHAR percent_dW[] = {'%','d',0};
  664. j += wnsprintfW(&buffer[j], 11, percent_dW, page);
  665. }
  666. inside = FALSE;
  667. }
  668. else if (pattern[i] == '&')
  669. inside = TRUE;
  670. else
  671. buffer[j++] = pattern[i];
  672. }
  673. buffer[j++] = 0;
  674. }
  675. return buffer;
  676. }
  677. static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
  678. {
  679. int b, y;
  680. TEXTMETRICW tm;
  681. SIZE szMetrics;
  682. WCHAR *footer_text = NULL;
  683. footer_text = expand_header_vars(Globals.szFooter, page);
  684. if (footer_text == NULL)
  685. return FALSE;
  686. if (dopage)
  687. {
  688. if (StartPage(hdc) <= 0)
  689. {
  690. static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
  691. static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
  692. MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
  693. HeapFree(GetProcessHeap(), 0, footer_text);
  694. return FALSE;
  695. }
  696. }
  697. GetTextMetricsW(hdc, &tm);
  698. y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
  699. b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, footer_text) * tm.tmHeight;
  700. do {
  701. INT m, n;
  702. if (!tInfo->len)
  703. {
  704. /* find the end of the line */
  705. while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
  706. {
  707. if (*tInfo->mptr == '\t')
  708. {
  709. /* replace tabs with spaces */
  710. for (m = 0; m < SPACES_IN_TAB; m++)
  711. {
  712. if (tInfo->len < PRINT_LEN_MAX)
  713. tInfo->lptr[tInfo->len++] = ' ';
  714. else if (Globals.bWrapLongLines)
  715. break;
  716. }
  717. }
  718. else if (tInfo->len < PRINT_LEN_MAX)
  719. tInfo->lptr[tInfo->len++] = *tInfo->mptr;
  720. if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
  721. break;
  722. tInfo->mptr++;
  723. }
  724. }
  725. /* Find out how much we should print if line wrapping is enabled */
  726. if (Globals.bWrapLongLines)
  727. {
  728. GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
  729. if (n < tInfo->len && tInfo->lptr[n] != ' ')
  730. {
  731. m = n;
  732. /* Don't wrap words unless it's a single word over the entire line */
  733. while (m && tInfo->lptr[m] != ' ') m--;
  734. if (m > 0) n = m + 1;
  735. }
  736. }
  737. else
  738. n = tInfo->len;
  739. if (dopage)
  740. ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
  741. tInfo->len -= n;
  742. if (tInfo->len)
  743. {
  744. memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
  745. y += tm.tmHeight + tm.tmExternalLeading;
  746. }
  747. else
  748. {
  749. /* find the next line */
  750. while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
  751. {
  752. if (*tInfo->mptr == '\n')
  753. y += tm.tmHeight + tm.tmExternalLeading;
  754. tInfo->mptr++;
  755. }
  756. }
  757. } while (tInfo->mptr < tInfo->mend && y < b);
  758. notepad_print_header(hdc, rc, dopage, FALSE, page, footer_text);
  759. if (dopage)
  760. {
  761. EndPage(hdc);
  762. }
  763. HeapFree(GetProcessHeap(), 0, footer_text);
  764. return TRUE;
  765. }
  766. VOID DIALOG_FilePrint(VOID)
  767. {
  768. DOCINFOW di;
  769. PRINTDLGW printer;
  770. int page, dopage, copy;
  771. LOGFONTW lfFont;
  772. HFONT hTextFont, old_font = 0;
  773. DWORD size;
  774. BOOL ret = FALSE;
  775. RECT rc;
  776. LPWSTR pTemp;
  777. TEXTINFO tInfo;
  778. WCHAR cTemp[PRINT_LEN_MAX];
  779. /* Get Current Settings */
  780. ZeroMemory(&printer, sizeof(printer));
  781. printer.lStructSize = sizeof(printer);
  782. printer.hwndOwner = Globals.hMainWnd;
  783. printer.hDevMode = Globals.hDevMode;
  784. printer.hDevNames = Globals.hDevNames;
  785. printer.hInstance = Globals.hInstance;
  786. /* Set some default flags */
  787. printer.Flags = PD_RETURNDC | PD_NOSELECTION;
  788. printer.nFromPage = 0;
  789. printer.nMinPage = 1;
  790. /* we really need to calculate number of pages to set nMaxPage and nToPage */
  791. printer.nToPage = 0;
  792. printer.nMaxPage = -1;
  793. /* Let commdlg manage copy settings */
  794. printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
  795. if (!PrintDlgW(&printer)) return;
  796. Globals.hDevMode = printer.hDevMode;
  797. Globals.hDevNames = printer.hDevNames;
  798. SetMapMode(printer.hDC, MM_TEXT);
  799. /* initialize DOCINFO */
  800. di.cbSize = sizeof(DOCINFOW);
  801. di.lpszDocName = Globals.szFileTitle;
  802. di.lpszOutput = NULL;
  803. di.lpszDatatype = NULL;
  804. di.fwType = 0;
  805. if(printer.Flags & PD_PRINTTOFILE)
  806. {
  807. di.lpszOutput = dialog_print_to_file(printer.hwndOwner);
  808. if(!di.lpszOutput)
  809. return;
  810. }
  811. /* Get the file text */
  812. size = GetWindowTextLengthW(Globals.hEdit) + 1;
  813. pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  814. if (!pTemp)
  815. {
  816. DeleteDC(printer.hDC);
  817. ShowLastError();
  818. return;
  819. }
  820. size = GetWindowTextW(Globals.hEdit, pTemp, size);
  821. if (StartDocW(printer.hDC, &di) > 0)
  822. {
  823. /* Get the page margins in pixels. */
  824. rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
  825. GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
  826. rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
  827. MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
  828. rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
  829. GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
  830. rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
  831. MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
  832. /* Create a font for the printer resolution */
  833. lfFont = Globals.lfFont;
  834. lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
  835. /* Make the font a bit lighter */
  836. lfFont.lfWeight -= 100;
  837. hTextFont = CreateFontIndirectW(&lfFont);
  838. old_font = SelectObject(printer.hDC, hTextFont);
  839. for (copy = 1; copy <= printer.nCopies; copy++)
  840. {
  841. page = 1;
  842. tInfo.mptr = pTemp;
  843. tInfo.mend = pTemp + size;
  844. tInfo.lptr = cTemp;
  845. tInfo.len = 0;
  846. do {
  847. if (printer.Flags & PD_PAGENUMS)
  848. {
  849. /* a specific range of pages is selected, so
  850. * skip pages that are not to be printed
  851. */
  852. if (page > printer.nToPage)
  853. break;
  854. else if (page >= printer.nFromPage)
  855. dopage = 1;
  856. else
  857. dopage = 0;
  858. }
  859. else
  860. dopage = 1;
  861. ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
  862. page++;
  863. } while (ret && tInfo.mptr < tInfo.mend);
  864. if (!ret) break;
  865. }
  866. EndDoc(printer.hDC);
  867. SelectObject(printer.hDC, old_font);
  868. DeleteObject(hTextFont);
  869. }
  870. DeleteDC(printer.hDC);
  871. HeapFree(GetProcessHeap(), 0, pTemp);
  872. }
  873. VOID DIALOG_FilePrinterSetup(VOID)
  874. {
  875. PRINTDLGW printer;
  876. ZeroMemory(&printer, sizeof(printer));
  877. printer.lStructSize = sizeof(printer);
  878. printer.hwndOwner = Globals.hMainWnd;
  879. printer.hDevMode = Globals.hDevMode;
  880. printer.hDevNames = Globals.hDevNames;
  881. printer.hInstance = Globals.hInstance;
  882. printer.Flags = PD_PRINTSETUP;
  883. printer.nCopies = 1;
  884. PrintDlgW(&printer);
  885. Globals.hDevMode = printer.hDevMode;
  886. Globals.hDevNames = printer.hDevNames;
  887. }
  888. VOID DIALOG_FileExit(VOID)
  889. {
  890. PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
  891. }
  892. VOID DIALOG_EditUndo(VOID)
  893. {
  894. SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
  895. }
  896. VOID DIALOG_EditCut(VOID)
  897. {
  898. SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
  899. }
  900. VOID DIALOG_EditCopy(VOID)
  901. {
  902. SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
  903. }
  904. VOID DIALOG_EditPaste(VOID)
  905. {
  906. SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
  907. }
  908. VOID DIALOG_EditDelete(VOID)
  909. {
  910. SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
  911. }
  912. VOID DIALOG_EditSelectAll(VOID)
  913. {
  914. SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
  915. }
  916. VOID DIALOG_EditTimeDate(VOID)
  917. {
  918. SYSTEMTIME st;
  919. WCHAR szDate[MAX_STRING_LEN];
  920. static const WCHAR spaceW[] = { ' ',0 };
  921. GetLocalTime(&st);
  922. GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
  923. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
  924. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
  925. GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
  926. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
  927. }
  928. VOID DIALOG_EditWrap(VOID)
  929. {
  930. BOOL modify = FALSE;
  931. static const WCHAR editW[] = { 'e','d','i','t',0 };
  932. DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
  933. ES_AUTOVSCROLL | ES_MULTILINE;
  934. RECT rc;
  935. DWORD size;
  936. LPWSTR pTemp;
  937. size = GetWindowTextLengthW(Globals.hEdit) + 1;
  938. pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  939. if (!pTemp)
  940. {
  941. ShowLastError();
  942. return;
  943. }
  944. GetWindowTextW(Globals.hEdit, pTemp, size);
  945. modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
  946. DestroyWindow(Globals.hEdit);
  947. GetClientRect(Globals.hMainWnd, &rc);
  948. if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
  949. Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
  950. 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
  951. NULL, Globals.hInstance, NULL);
  952. SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
  953. SetWindowTextW(Globals.hEdit, pTemp);
  954. SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
  955. SetFocus(Globals.hEdit);
  956. HeapFree(GetProcessHeap(), 0, pTemp);
  957. Globals.bWrapLongLines = !Globals.bWrapLongLines;
  958. CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
  959. MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
  960. }
  961. VOID DIALOG_SelectFont(VOID)
  962. {
  963. CHOOSEFONTW cf;
  964. LOGFONTW lf=Globals.lfFont;
  965. ZeroMemory( &cf, sizeof(cf) );
  966. cf.lStructSize=sizeof(cf);
  967. cf.hwndOwner=Globals.hMainWnd;
  968. cf.lpLogFont=&lf;
  969. cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_NOVERTFONTS;
  970. if( ChooseFontW(&cf) )
  971. {
  972. HFONT currfont=Globals.hFont;
  973. Globals.hFont=CreateFontIndirectW( &lf );
  974. Globals.lfFont=lf;
  975. SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
  976. if( currfont!=NULL )
  977. DeleteObject( currfont );
  978. }
  979. }
  980. VOID DIALOG_Search(VOID)
  981. {
  982. /* Allow only one search/replace dialog to open */
  983. if(Globals.hFindReplaceDlg != NULL)
  984. {
  985. SetActiveWindow(Globals.hFindReplaceDlg);
  986. return;
  987. }
  988. ZeroMemory(&Globals.find, sizeof(Globals.find));
  989. Globals.find.lStructSize = sizeof(Globals.find);
  990. Globals.find.hwndOwner = Globals.hMainWnd;
  991. Globals.find.hInstance = Globals.hInstance;
  992. Globals.find.lpstrFindWhat = Globals.szFindText;
  993. Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
  994. Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
  995. /* We only need to create the modal FindReplace dialog which will */
  996. /* notify us of incoming events using hMainWnd Window Messages */
  997. Globals.hFindReplaceDlg = FindTextW(&Globals.find);
  998. assert(Globals.hFindReplaceDlg !=0);
  999. }
  1000. VOID DIALOG_SearchNext(VOID)
  1001. {
  1002. if (Globals.lastFind.lpstrFindWhat == NULL)
  1003. DIALOG_Search();
  1004. else /* use the last find data */
  1005. NOTEPAD_DoFind(&Globals.lastFind);
  1006. }
  1007. VOID DIALOG_Replace(VOID)
  1008. {
  1009. /* Allow only one search/replace dialog to open */
  1010. if(Globals.hFindReplaceDlg != NULL)
  1011. {
  1012. SetActiveWindow(Globals.hFindReplaceDlg);
  1013. return;
  1014. }
  1015. ZeroMemory(&Globals.find, sizeof(Globals.find));
  1016. Globals.find.lStructSize = sizeof(Globals.find);
  1017. Globals.find.hwndOwner = Globals.hMainWnd;
  1018. Globals.find.hInstance = Globals.hInstance;
  1019. Globals.find.lpstrFindWhat = Globals.szFindText;
  1020. Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
  1021. Globals.find.lpstrReplaceWith = Globals.szReplaceText;
  1022. Globals.find.wReplaceWithLen = ARRAY_SIZE(Globals.szReplaceText);
  1023. Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
  1024. /* We only need to create the modal FindReplace dialog which will */
  1025. /* notify us of incoming events using hMainWnd Window Messages */
  1026. Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
  1027. assert(Globals.hFindReplaceDlg !=0);
  1028. }
  1029. VOID DIALOG_HelpContents(VOID)
  1030. {
  1031. WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
  1032. }
  1033. VOID DIALOG_HelpAboutNotepad(VOID)
  1034. {
  1035. static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
  1036. WCHAR szNotepad[MAX_STRING_LEN];
  1037. HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
  1038. IMAGE_ICON, 48, 48, LR_SHARED);
  1039. LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
  1040. ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
  1041. }
  1042. /***********************************************************************
  1043. *
  1044. * DIALOG_FilePageSetup
  1045. */
  1046. VOID DIALOG_FilePageSetup(void)
  1047. {
  1048. DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
  1049. Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
  1050. }
  1051. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1052. *
  1053. * DIALOG_PAGESETUP_DlgProc
  1054. */
  1055. static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  1056. {
  1057. switch (msg)
  1058. {
  1059. case WM_COMMAND:
  1060. switch (wParam)
  1061. {
  1062. case IDOK:
  1063. /* save user input and close dialog */
  1064. GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
  1065. GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
  1066. Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
  1067. Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
  1068. Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
  1069. Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
  1070. EndDialog(hDlg, IDOK);
  1071. return TRUE;
  1072. case IDCANCEL:
  1073. /* discard user input and close dialog */
  1074. EndDialog(hDlg, IDCANCEL);
  1075. return TRUE;
  1076. case IDHELP:
  1077. {
  1078. /* FIXME: Bring this to work */
  1079. 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 };
  1080. static const WCHAR helpW[] = { 'H','e','l','p',0 };
  1081. MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
  1082. return TRUE;
  1083. }
  1084. default:
  1085. break;
  1086. }
  1087. break;
  1088. case WM_INITDIALOG:
  1089. /* fetch last user input prior to display dialog */
  1090. SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
  1091. SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
  1092. SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
  1093. SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
  1094. SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
  1095. SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
  1096. break;
  1097. }
  1098. return FALSE;
  1099. }