dialog.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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. }