main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * Notepad
  3. *
  4. * Copyright 2000 Mike McCormack <Mike_McCormack@looksmart.com.au>
  5. * Copyright 1997,98 Marcel Baur <mbaur@g26.ethz.ch>
  6. * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
  7. * Copyright 2002 Andriy Palamarchuk
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <windows.h>
  26. #include <commctrl.h>
  27. #include <commdlg.h>
  28. #include <shellapi.h>
  29. #include <shlwapi.h>
  30. #include "main.h"
  31. #include "dialog.h"
  32. #include "notepad_res.h"
  33. NOTEPAD_GLOBALS Globals;
  34. static ATOM aFINDMSGSTRING;
  35. static RECT main_rect;
  36. static const WCHAR notepad_reg_key[] = {'S','o','f','t','w','a','r','e','\\',
  37. 'M','i','c','r','o','s','o','f','t','\\','N','o','t','e','p','a','d','\0'};
  38. static const WCHAR value_fWrap[] = {'f','W','r','a','p','\0'};
  39. static const WCHAR value_iPointSize[] = {'i','P','o','i','n','t','S','i','z','e','\0'};
  40. static const WCHAR value_iWindowPosDX[] = {'i','W','i','n','d','o','w','P','o','s','D','X','\0'};
  41. static const WCHAR value_iWindowPosDY[] = {'i','W','i','n','d','o','w','P','o','s','D','Y','\0'};
  42. static const WCHAR value_iWindowPosX[] = {'i','W','i','n','d','o','w','P','o','s','X','\0'};
  43. static const WCHAR value_iWindowPosY[] = {'i','W','i','n','d','o','w','P','o','s','Y','\0'};
  44. static const WCHAR value_lfCharSet[] = {'l','f','C','h','a','r','S','e','t','\0'};
  45. static const WCHAR value_lfClipPrecision[] = {'l','f','C','l','i','p','P','r','e','c','i','s','i','o','n','\0'};
  46. static const WCHAR value_lfEscapement[] = {'l','f','E','s','c','a','p','e','m','e','n','t','\0'};
  47. static const WCHAR value_lfItalic[] = {'l','f','I','t','a','l','i','c','\0'};
  48. static const WCHAR value_lfOrientation[] = {'l','f','O','r','i','e','n','t','a','t','i','o','n','\0'};
  49. static const WCHAR value_lfOutPrecision[] = {'l','f','O','u','t','P','r','e','c','i','s','i','o','n','\0'};
  50. static const WCHAR value_lfPitchAndFamily[] = {'l','f','P','i','t','c','h','A','n','d','F','a','m','i','l','y','\0'};
  51. static const WCHAR value_lfQuality[] = {'l','f','Q','u','a','l','i','t','y','\0'};
  52. static const WCHAR value_lfStrikeOut[] = {'l','f','S','t','r','i','k','e','O','u','t','\0'};
  53. static const WCHAR value_lfUnderline[] = {'l','f','U','n','d','e','r','l','i','n','e','\0'};
  54. static const WCHAR value_lfWeight[] = {'l','f','W','e','i','g','h','t','\0'};
  55. static const WCHAR value_lfFaceName[] = {'l','f','F','a','c','e','N','a','m','e','\0'};
  56. static const WCHAR value_iMarginTop[] = {'i','M','a','r','g','i','n','T','o','p','\0'};
  57. static const WCHAR value_iMarginBottom[] = {'i','M','a','r','g','i','n','B','o','t','t','o','m','\0'};
  58. static const WCHAR value_iMarginLeft[] = {'i','M','a','r','g','i','n','L','e','f','t','\0'};
  59. static const WCHAR value_iMarginRight[] = {'i','M','a','r','g','i','n','R','i','g','h','t','\0'};
  60. static const WCHAR value_szHeader[] = {'s','z','H','e','a','d','e','r','\0'};
  61. static const WCHAR value_szFooter[] = {'s','z','T','r','a','i','l','e','r','\0'};
  62. /***********************************************************************
  63. *
  64. * SetFileNameAndEncoding
  65. *
  66. * Sets global file name and encoding (which is used to preselect original
  67. * encoding in Save As dialog, and when saving without using the Save As
  68. * dialog).
  69. */
  70. VOID SetFileNameAndEncoding(LPCWSTR szFileName, ENCODING enc)
  71. {
  72. lstrcpyW(Globals.szFileName, szFileName);
  73. Globals.szFileTitle[0] = 0;
  74. GetFileTitleW(szFileName, Globals.szFileTitle, ARRAY_SIZE(Globals.szFileTitle));
  75. Globals.encFile = enc;
  76. }
  77. /******************************************************************************
  78. * get_dpi
  79. *
  80. * Get the dpi from registry HKCC\Software\Fonts\LogPixels.
  81. */
  82. DWORD get_dpi(void)
  83. {
  84. static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
  85. static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
  86. DWORD dpi = 96;
  87. HKEY hkey;
  88. if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
  89. {
  90. DWORD type, size, new_dpi;
  91. size = sizeof(new_dpi);
  92. if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (LPBYTE)&new_dpi, &size) == ERROR_SUCCESS)
  93. {
  94. if(type == REG_DWORD && new_dpi != 0)
  95. dpi = new_dpi;
  96. }
  97. RegCloseKey(hkey);
  98. }
  99. return dpi;
  100. }
  101. /***********************************************************************
  102. *
  103. * NOTEPAD_SaveSettingToRegistry
  104. *
  105. * Save setting to registry HKCU\Software\Microsoft\Notepad.
  106. */
  107. static VOID NOTEPAD_SaveSettingToRegistry(void)
  108. {
  109. HKEY hkey;
  110. DWORD disp;
  111. if(RegCreateKeyExW(HKEY_CURRENT_USER, notepad_reg_key, 0, NULL,
  112. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disp) == ERROR_SUCCESS)
  113. {
  114. DWORD data;
  115. WINDOWPLACEMENT wndpl;
  116. wndpl.length = sizeof(WINDOWPLACEMENT);
  117. GetWindowPlacement(Globals.hMainWnd, &wndpl);
  118. main_rect = wndpl.rcNormalPosition;
  119. #define SET_NOTEPAD_REG(hkey, value_name, value_data) do { DWORD data = value_data; RegSetValueExW(hkey, value_name, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); }while(0)
  120. SET_NOTEPAD_REG(hkey, value_fWrap, Globals.bWrapLongLines);
  121. SET_NOTEPAD_REG(hkey, value_iWindowPosX, main_rect.left);
  122. SET_NOTEPAD_REG(hkey, value_iWindowPosY, main_rect.top);
  123. SET_NOTEPAD_REG(hkey, value_iWindowPosDX, main_rect.right - main_rect.left);
  124. SET_NOTEPAD_REG(hkey, value_iWindowPosDY, main_rect.bottom - main_rect.top);
  125. SET_NOTEPAD_REG(hkey, value_lfCharSet, Globals.lfFont.lfCharSet);
  126. SET_NOTEPAD_REG(hkey, value_lfClipPrecision, Globals.lfFont.lfClipPrecision);
  127. SET_NOTEPAD_REG(hkey, value_lfEscapement, Globals.lfFont.lfEscapement);
  128. SET_NOTEPAD_REG(hkey, value_lfItalic, Globals.lfFont.lfItalic);
  129. SET_NOTEPAD_REG(hkey, value_lfOrientation, Globals.lfFont.lfOrientation);
  130. SET_NOTEPAD_REG(hkey, value_lfOutPrecision, Globals.lfFont.lfOutPrecision);
  131. SET_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
  132. SET_NOTEPAD_REG(hkey, value_lfQuality, Globals.lfFont.lfQuality);
  133. SET_NOTEPAD_REG(hkey, value_lfStrikeOut, Globals.lfFont.lfStrikeOut);
  134. SET_NOTEPAD_REG(hkey, value_lfUnderline, Globals.lfFont.lfUnderline);
  135. SET_NOTEPAD_REG(hkey, value_lfWeight, Globals.lfFont.lfWeight);
  136. SET_NOTEPAD_REG(hkey, value_iMarginTop, Globals.iMarginTop);
  137. SET_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom);
  138. SET_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft);
  139. SET_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight);
  140. #undef SET_NOTEPAD_REG
  141. /* Store the current value as 10 * twips */
  142. data = MulDiv(abs(Globals.lfFont.lfHeight), 720 , get_dpi());
  143. RegSetValueExW(hkey, value_iPointSize, 0, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
  144. RegSetValueExW(hkey, value_lfFaceName, 0, REG_SZ, (LPBYTE)&Globals.lfFont.lfFaceName,
  145. lstrlenW(Globals.lfFont.lfFaceName) * sizeof(Globals.lfFont.lfFaceName[0]));
  146. RegSetValueExW(hkey, value_szHeader, 0, REG_SZ, (LPBYTE)&Globals.szHeader,
  147. lstrlenW(Globals.szHeader) * sizeof(Globals.szHeader[0]));
  148. RegSetValueExW(hkey, value_szFooter, 0, REG_SZ, (LPBYTE)&Globals.szFooter,
  149. lstrlenW(Globals.szFooter) * sizeof(Globals.szFooter[0]));
  150. RegCloseKey(hkey);
  151. }
  152. }
  153. /***********************************************************************
  154. *
  155. * NOTEPAD_LoadSettingFromRegistry
  156. *
  157. * Load setting from registry HKCU\Software\Microsoft\Notepad.
  158. */
  159. static VOID NOTEPAD_LoadSettingFromRegistry(void)
  160. {
  161. static const WCHAR systemW[] = { 'S','y','s','t','e','m','\0' };
  162. HKEY hkey;
  163. INT base_length, dx, dy;
  164. base_length = (GetSystemMetrics(SM_CXSCREEN) > GetSystemMetrics(SM_CYSCREEN))?
  165. GetSystemMetrics(SM_CYSCREEN) : GetSystemMetrics(SM_CXSCREEN);
  166. dx = base_length * .95;
  167. dy = dx * 3 / 4;
  168. SetRect( &main_rect, 0, 0, dx, dy );
  169. Globals.bWrapLongLines = TRUE;
  170. Globals.iMarginTop = 2500;
  171. Globals.iMarginBottom = 2500;
  172. Globals.iMarginLeft = 2000;
  173. Globals.iMarginRight = 2000;
  174. Globals.lfFont.lfHeight = -12;
  175. Globals.lfFont.lfWidth = 0;
  176. Globals.lfFont.lfEscapement = 0;
  177. Globals.lfFont.lfOrientation = 0;
  178. Globals.lfFont.lfWeight = FW_REGULAR;
  179. Globals.lfFont.lfItalic = FALSE;
  180. Globals.lfFont.lfUnderline = FALSE;
  181. Globals.lfFont.lfStrikeOut = FALSE;
  182. Globals.lfFont.lfCharSet = DEFAULT_CHARSET;
  183. Globals.lfFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  184. Globals.lfFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  185. Globals.lfFont.lfQuality = DEFAULT_QUALITY;
  186. Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
  187. lstrcpyW(Globals.lfFont.lfFaceName, systemW);
  188. LoadStringW(Globals.hInstance, STRING_PAGESETUP_HEADERVALUE,
  189. Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
  190. LoadStringW(Globals.hInstance, STRING_PAGESETUP_FOOTERVALUE,
  191. Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
  192. if(RegOpenKeyW(HKEY_CURRENT_USER, notepad_reg_key, &hkey) == ERROR_SUCCESS)
  193. {
  194. WORD data_helper[MAX_PATH];
  195. DWORD type, size;
  196. int point_size;
  197. #define QUERY_NOTEPAD_REG(hkey, value_name, ret) do { DWORD type, data; DWORD size = sizeof(DWORD); if(RegQueryValueExW(hkey, value_name, 0, &type, (LPBYTE)&data, &size) == ERROR_SUCCESS) if(type == REG_DWORD) ret = data; } while(0)
  198. QUERY_NOTEPAD_REG(hkey, value_fWrap, Globals.bWrapLongLines);
  199. QUERY_NOTEPAD_REG(hkey, value_iWindowPosX, main_rect.left);
  200. QUERY_NOTEPAD_REG(hkey, value_iWindowPosY, main_rect.top);
  201. QUERY_NOTEPAD_REG(hkey, value_iWindowPosDX, dx);
  202. QUERY_NOTEPAD_REG(hkey, value_iWindowPosDY, dy);
  203. QUERY_NOTEPAD_REG(hkey, value_lfCharSet, Globals.lfFont.lfCharSet);
  204. QUERY_NOTEPAD_REG(hkey, value_lfClipPrecision, Globals.lfFont.lfClipPrecision);
  205. QUERY_NOTEPAD_REG(hkey, value_lfEscapement, Globals.lfFont.lfEscapement);
  206. QUERY_NOTEPAD_REG(hkey, value_lfItalic, Globals.lfFont.lfItalic);
  207. QUERY_NOTEPAD_REG(hkey, value_lfOrientation, Globals.lfFont.lfOrientation);
  208. QUERY_NOTEPAD_REG(hkey, value_lfOutPrecision, Globals.lfFont.lfOutPrecision);
  209. QUERY_NOTEPAD_REG(hkey, value_lfPitchAndFamily, Globals.lfFont.lfPitchAndFamily);
  210. QUERY_NOTEPAD_REG(hkey, value_lfQuality, Globals.lfFont.lfQuality);
  211. QUERY_NOTEPAD_REG(hkey, value_lfStrikeOut, Globals.lfFont.lfStrikeOut);
  212. QUERY_NOTEPAD_REG(hkey, value_lfUnderline, Globals.lfFont.lfUnderline);
  213. QUERY_NOTEPAD_REG(hkey, value_lfWeight, Globals.lfFont.lfWeight);
  214. QUERY_NOTEPAD_REG(hkey, value_iMarginTop, Globals.iMarginTop);
  215. QUERY_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom);
  216. QUERY_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft);
  217. QUERY_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight);
  218. #undef QUERY_NOTEPAD_REG
  219. main_rect.right = main_rect.left + dx;
  220. main_rect.bottom = main_rect.top + dy;
  221. size = sizeof(DWORD);
  222. if(RegQueryValueExW(hkey, value_iPointSize, 0, &type, (LPBYTE)&point_size, &size) == ERROR_SUCCESS)
  223. if(type == REG_DWORD)
  224. /* The value is stored as 10 * twips */
  225. Globals.lfFont.lfHeight = -MulDiv(abs(point_size), get_dpi(), 720);
  226. size = sizeof(Globals.lfFont.lfFaceName);
  227. if(RegQueryValueExW(hkey, value_lfFaceName, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
  228. if(type == REG_SZ)
  229. lstrcpyW(Globals.lfFont.lfFaceName, data_helper);
  230. size = sizeof(Globals.szHeader);
  231. if(RegQueryValueExW(hkey, value_szHeader, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
  232. if(type == REG_SZ)
  233. lstrcpyW(Globals.szHeader, data_helper);
  234. size = sizeof(Globals.szFooter);
  235. if(RegQueryValueExW(hkey, value_szFooter, 0, &type, (LPBYTE)&data_helper, &size) == ERROR_SUCCESS)
  236. if(type == REG_SZ)
  237. lstrcpyW(Globals.szFooter, data_helper);
  238. RegCloseKey(hkey);
  239. }
  240. }
  241. /***********************************************************************
  242. *
  243. * NOTEPAD_MenuCommand
  244. *
  245. * All handling of main menu events
  246. */
  247. static int NOTEPAD_MenuCommand(WPARAM wParam)
  248. {
  249. switch (wParam)
  250. {
  251. case CMD_NEW: DIALOG_FileNew(); break;
  252. case CMD_OPEN: DIALOG_FileOpen(); break;
  253. case CMD_SAVE: DIALOG_FileSave(); break;
  254. case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
  255. case CMD_PRINT: DIALOG_FilePrint(); break;
  256. case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
  257. case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
  258. case CMD_EXIT: DIALOG_FileExit(); break;
  259. case CMD_UNDO: DIALOG_EditUndo(); break;
  260. case CMD_CUT: DIALOG_EditCut(); break;
  261. case CMD_COPY: DIALOG_EditCopy(); break;
  262. case CMD_PASTE: DIALOG_EditPaste(); break;
  263. case CMD_DELETE: DIALOG_EditDelete(); break;
  264. case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
  265. case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
  266. case CMD_SEARCH: DIALOG_Search(); break;
  267. case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
  268. case CMD_REPLACE: DIALOG_Replace(); break;
  269. case CMD_WRAP: DIALOG_EditWrap(); break;
  270. case CMD_FONT: DIALOG_SelectFont(); break;
  271. case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
  272. case CMD_HELP_ABOUT_NOTEPAD: DIALOG_HelpAboutNotepad(); break;
  273. default:
  274. break;
  275. }
  276. return 0;
  277. }
  278. /***********************************************************************
  279. * Data Initialization
  280. */
  281. static VOID NOTEPAD_InitData(VOID)
  282. {
  283. LPWSTR p = Globals.szFilter;
  284. static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
  285. static const WCHAR all_files[] = { '*','.','*',0 };
  286. LoadStringW(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
  287. p += lstrlenW(p) + 1;
  288. lstrcpyW(p, txt_files);
  289. p += lstrlenW(p) + 1;
  290. LoadStringW(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
  291. p += lstrlenW(p) + 1;
  292. lstrcpyW(p, all_files);
  293. p += lstrlenW(p) + 1;
  294. *p = '\0';
  295. Globals.hDevMode = NULL;
  296. Globals.hDevNames = NULL;
  297. CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
  298. MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
  299. }
  300. /***********************************************************************
  301. * Enable/disable items on the menu based on control state
  302. */
  303. static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
  304. {
  305. int enable;
  306. EnableMenuItem(menu, CMD_UNDO,
  307. SendMessageW(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
  308. EnableMenuItem(menu, CMD_PASTE,
  309. IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
  310. enable = SendMessageW(Globals.hEdit, EM_GETSEL, 0, 0);
  311. enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
  312. EnableMenuItem(menu, CMD_CUT, enable);
  313. EnableMenuItem(menu, CMD_COPY, enable);
  314. EnableMenuItem(menu, CMD_DELETE, enable);
  315. EnableMenuItem(menu, CMD_SELECT_ALL,
  316. GetWindowTextLengthW(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
  317. }
  318. static LPWSTR NOTEPAD_StrRStr(LPWSTR pszSource, LPWSTR pszLast, LPWSTR pszSrch)
  319. {
  320. int len = lstrlenW(pszSrch);
  321. pszLast--;
  322. while (pszLast >= pszSource)
  323. {
  324. if (StrCmpNW(pszLast, pszSrch, len) == 0)
  325. return pszLast;
  326. pszLast--;
  327. }
  328. return NULL;
  329. }
  330. /***********************************************************************
  331. * The user activated the Find dialog
  332. */
  333. void NOTEPAD_DoFind(FINDREPLACEW *fr)
  334. {
  335. LPWSTR content;
  336. LPWSTR found;
  337. int len = lstrlenW(fr->lpstrFindWhat);
  338. int fileLen;
  339. DWORD pos;
  340. fileLen = GetWindowTextLengthW(Globals.hEdit) + 1;
  341. content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(WCHAR));
  342. if (!content) return;
  343. GetWindowTextW(Globals.hEdit, content, fileLen);
  344. SendMessageW(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
  345. switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
  346. {
  347. case 0:
  348. found = StrRStrIW(content, content+pos-len, fr->lpstrFindWhat);
  349. break;
  350. case FR_DOWN:
  351. found = StrStrIW(content+pos, fr->lpstrFindWhat);
  352. break;
  353. case FR_MATCHCASE:
  354. found = NOTEPAD_StrRStr(content, content+pos-len, fr->lpstrFindWhat);
  355. break;
  356. case FR_DOWN|FR_MATCHCASE:
  357. found = StrStrW(content+pos, fr->lpstrFindWhat);
  358. break;
  359. default: /* shouldn't happen */
  360. return;
  361. }
  362. HeapFree(GetProcessHeap(), 0, content);
  363. if (found == NULL)
  364. {
  365. DIALOG_StringMsgBox(Globals.hFindReplaceDlg, STRING_NOTFOUND, fr->lpstrFindWhat,
  366. MB_ICONINFORMATION|MB_OK);
  367. return;
  368. }
  369. SendMessageW(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
  370. }
  371. static void NOTEPAD_DoReplace(FINDREPLACEW *fr)
  372. {
  373. LPWSTR content;
  374. int len = lstrlenW(fr->lpstrFindWhat);
  375. int fileLen;
  376. DWORD pos;
  377. DWORD pos_start;
  378. fileLen = GetWindowTextLengthW(Globals.hEdit) + 1;
  379. content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(WCHAR));
  380. if (!content) return;
  381. GetWindowTextW(Globals.hEdit, content, fileLen);
  382. SendMessageW(Globals.hEdit, EM_GETSEL, (WPARAM)&pos_start, (LPARAM)&pos);
  383. switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
  384. {
  385. case FR_DOWN:
  386. if ( pos-pos_start == len && StrCmpNIW(fr->lpstrFindWhat, content+pos_start, len) == 0)
  387. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
  388. break;
  389. case FR_DOWN|FR_MATCHCASE:
  390. if ( pos-pos_start == len && StrCmpNW(fr->lpstrFindWhat, content+pos_start, len) == 0)
  391. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
  392. break;
  393. default: /* shouldn't happen */
  394. return;
  395. }
  396. HeapFree(GetProcessHeap(), 0, content);
  397. NOTEPAD_DoFind(fr);
  398. }
  399. static void NOTEPAD_DoReplaceAll(FINDREPLACEW *fr)
  400. {
  401. LPWSTR content;
  402. LPWSTR found;
  403. int len = lstrlenW(fr->lpstrFindWhat);
  404. int fileLen;
  405. DWORD pos;
  406. SendMessageW(Globals.hEdit, EM_SETSEL, 0, 0);
  407. while(TRUE){
  408. fileLen = GetWindowTextLengthW(Globals.hEdit) + 1;
  409. content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(WCHAR));
  410. if (!content) return;
  411. GetWindowTextW(Globals.hEdit, content, fileLen);
  412. SendMessageW(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
  413. switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
  414. {
  415. case FR_DOWN:
  416. found = StrStrIW(content+pos, fr->lpstrFindWhat);
  417. break;
  418. case FR_DOWN|FR_MATCHCASE:
  419. found = StrStrW(content+pos, fr->lpstrFindWhat);
  420. break;
  421. default: /* shouldn't happen */
  422. return;
  423. }
  424. HeapFree(GetProcessHeap(), 0, content);
  425. if(found == NULL)
  426. {
  427. SendMessageW(Globals.hEdit, EM_SETSEL, 0, 0);
  428. return;
  429. }
  430. SendMessageW(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
  431. SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
  432. }
  433. }
  434. /***********************************************************************
  435. *
  436. * NOTEPAD_WndProc
  437. */
  438. static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
  439. LPARAM lParam)
  440. {
  441. if (msg == aFINDMSGSTRING) /* not a constant so can't be used in switch */
  442. {
  443. FINDREPLACEW *fr = (FINDREPLACEW *)lParam;
  444. if (fr->Flags & FR_DIALOGTERM)
  445. Globals.hFindReplaceDlg = NULL;
  446. if (fr->Flags & FR_FINDNEXT)
  447. {
  448. Globals.lastFind = *fr;
  449. NOTEPAD_DoFind(fr);
  450. }
  451. if (fr->Flags & FR_REPLACE)
  452. {
  453. Globals.lastFind = *fr;
  454. NOTEPAD_DoReplace(fr);
  455. }
  456. if (fr->Flags & FR_REPLACEALL)
  457. {
  458. Globals.lastFind = *fr;
  459. NOTEPAD_DoReplaceAll(fr);
  460. }
  461. return 0;
  462. }
  463. switch (msg) {
  464. case WM_CREATE:
  465. {
  466. static const WCHAR editW[] = { 'e','d','i','t',0 };
  467. DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
  468. ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
  469. RECT rc;
  470. GetClientRect(hWnd, &rc);
  471. if (!Globals.bWrapLongLines) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
  472. Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL,
  473. dwStyle, 0, 0, rc.right, rc.bottom, hWnd,
  474. NULL, Globals.hInstance, NULL);
  475. Globals.hFont = CreateFontIndirectW(&Globals.lfFont);
  476. SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
  477. SendMessageW(Globals.hEdit, EM_LIMITTEXT, 0, 0);
  478. break;
  479. }
  480. case WM_COMMAND:
  481. NOTEPAD_MenuCommand(LOWORD(wParam));
  482. break;
  483. case WM_DESTROYCLIPBOARD:
  484. /*MessageBoxW(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
  485. break;
  486. case WM_CLOSE:
  487. if (DoCloseFile()) {
  488. DestroyWindow(hWnd);
  489. }
  490. break;
  491. case WM_QUERYENDSESSION:
  492. if (DoCloseFile()) {
  493. return 1;
  494. }
  495. break;
  496. case WM_DESTROY:
  497. NOTEPAD_SaveSettingToRegistry();
  498. PostQuitMessage(0);
  499. break;
  500. case WM_SIZE:
  501. SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
  502. SWP_NOOWNERZORDER | SWP_NOZORDER);
  503. break;
  504. case WM_SETFOCUS:
  505. SetFocus(Globals.hEdit);
  506. break;
  507. case WM_DROPFILES:
  508. {
  509. WCHAR szFileName[MAX_PATH];
  510. HANDLE hDrop = (HANDLE) wParam;
  511. DragQueryFileW(hDrop, 0, szFileName, ARRAY_SIZE(szFileName));
  512. DragFinish(hDrop);
  513. DoOpenFile(szFileName, ENCODING_AUTO);
  514. break;
  515. }
  516. case WM_INITMENUPOPUP:
  517. NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
  518. break;
  519. default:
  520. return DefWindowProcW(hWnd, msg, wParam, lParam);
  521. }
  522. return 0;
  523. }
  524. static int AlertFileDoesNotExist(LPCWSTR szFileName)
  525. {
  526. int nResult;
  527. WCHAR szMessage[MAX_STRING_LEN];
  528. WCHAR szResource[MAX_STRING_LEN];
  529. LoadStringW(Globals.hInstance, STRING_DOESNOTEXIST, szResource, ARRAY_SIZE(szResource));
  530. wsprintfW(szMessage, szResource, szFileName);
  531. LoadStringW(Globals.hInstance, STRING_ERROR, szResource, ARRAY_SIZE(szResource));
  532. nResult = MessageBoxW(Globals.hMainWnd, szMessage, szResource,
  533. MB_ICONEXCLAMATION | MB_YESNOCANCEL);
  534. return(nResult);
  535. }
  536. static void HandleCommandLine(LPWSTR cmdline)
  537. {
  538. WCHAR delimiter, *ptr;
  539. BOOL opt_print = FALSE;
  540. /* skip white space */
  541. while (*cmdline == ' ') cmdline++;
  542. /* skip executable name */
  543. delimiter = (*cmdline == '"' ? '"' : ' ');
  544. if (*cmdline == delimiter) cmdline++;
  545. while (*cmdline && *cmdline != delimiter) cmdline++;
  546. if (*cmdline == delimiter) cmdline++;
  547. while (*cmdline == ' ') cmdline++;
  548. ptr = cmdline;
  549. while (*ptr == ' ' || *ptr == '-' || *ptr == '/')
  550. {
  551. WCHAR option;
  552. if (*ptr++ == ' ') continue;
  553. option = *ptr;
  554. if (option) ptr++;
  555. while (*ptr == ' ') ptr++;
  556. switch(option)
  557. {
  558. case 'p':
  559. case 'P':
  560. {
  561. if (!opt_print)
  562. {
  563. opt_print = TRUE;
  564. cmdline = ptr;
  565. }
  566. break;
  567. }
  568. }
  569. }
  570. if (*cmdline)
  571. {
  572. /* file name is passed in the command line */
  573. LPCWSTR file_name;
  574. BOOL file_exists;
  575. WCHAR buf[MAX_PATH];
  576. if (cmdline[0] == '"')
  577. {
  578. WCHAR* wc;
  579. cmdline++;
  580. wc=cmdline;
  581. /* Note: Double-quotes are not allowed in Windows filenames */
  582. while (*wc && *wc != '"') wc++;
  583. /* On Windows notepad ignores further arguments too */
  584. *wc = 0;
  585. }
  586. if (FileExists(cmdline))
  587. {
  588. file_exists = TRUE;
  589. file_name = cmdline;
  590. }
  591. else
  592. {
  593. static const WCHAR txtW[] = { '.','t','x','t',0 };
  594. /* try to find file with ".txt" extension */
  595. if (wcschr(PathFindFileNameW(cmdline), '.'))
  596. {
  597. file_exists = FALSE;
  598. file_name = cmdline;
  599. }
  600. else
  601. {
  602. lstrcpynW(buf, cmdline, MAX_PATH - lstrlenW(txtW) - 1);
  603. lstrcatW(buf, txtW);
  604. file_name = buf;
  605. file_exists = FileExists(buf);
  606. }
  607. }
  608. if (file_exists)
  609. {
  610. DoOpenFile(file_name, ENCODING_AUTO);
  611. InvalidateRect(Globals.hMainWnd, NULL, FALSE);
  612. if (opt_print)
  613. DIALOG_FilePrint();
  614. }
  615. else
  616. {
  617. switch (AlertFileDoesNotExist(file_name)) {
  618. case IDYES:
  619. {
  620. HANDLE file;
  621. SetFileNameAndEncoding(file_name, ENCODING_ANSI);
  622. file = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_WRITE,
  623. NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  624. if (file != INVALID_HANDLE_VALUE) CloseHandle(file);
  625. UpdateWindowCaption();
  626. break;
  627. }
  628. case IDNO:
  629. break;
  630. case IDCANCEL:
  631. DestroyWindow(Globals.hMainWnd);
  632. break;
  633. }
  634. }
  635. }
  636. }
  637. /***********************************************************************
  638. *
  639. * WinMain
  640. */
  641. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
  642. {
  643. MSG msg;
  644. HACCEL hAccel;
  645. WNDCLASSEXW class;
  646. HMONITOR monitor;
  647. MONITORINFO info;
  648. INT x, y;
  649. static const WCHAR className[] = {'N','o','t','e','p','a','d',0};
  650. static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
  651. InitCommonControls();
  652. aFINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
  653. ZeroMemory(&Globals, sizeof(Globals));
  654. Globals.hInstance = hInstance;
  655. NOTEPAD_LoadSettingFromRegistry();
  656. ZeroMemory(&class, sizeof(class));
  657. class.cbSize = sizeof(class);
  658. class.lpfnWndProc = NOTEPAD_WndProc;
  659. class.hInstance = Globals.hInstance;
  660. class.hIcon = LoadIconW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD));
  661. class.hIconSm = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD), IMAGE_ICON,
  662. GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
  663. LR_SHARED);
  664. class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
  665. class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  666. class.lpszMenuName = MAKEINTRESOURCEW(MAIN_MENU);
  667. class.lpszClassName = className;
  668. if (!RegisterClassExW(&class)) return FALSE;
  669. /* Setup windows */
  670. monitor = MonitorFromRect( &main_rect, MONITOR_DEFAULTTOPRIMARY );
  671. info.cbSize = sizeof(info);
  672. GetMonitorInfoW( monitor, &info );
  673. x = main_rect.left;
  674. y = main_rect.top;
  675. if (main_rect.left >= info.rcWork.right ||
  676. main_rect.top >= info.rcWork.bottom ||
  677. main_rect.right < info.rcWork.left ||
  678. main_rect.bottom < info.rcWork.top)
  679. x = y = CW_USEDEFAULT;
  680. Globals.hMainWnd =
  681. CreateWindowW(className, winName, WS_OVERLAPPEDWINDOW, x, y,
  682. main_rect.right - main_rect.left, main_rect.bottom - main_rect.top,
  683. NULL, NULL, Globals.hInstance, NULL);
  684. if (!Globals.hMainWnd)
  685. {
  686. ShowLastError();
  687. ExitProcess(1);
  688. }
  689. NOTEPAD_InitData();
  690. DIALOG_FileNew();
  691. ShowWindow(Globals.hMainWnd, show);
  692. UpdateWindow(Globals.hMainWnd);
  693. DragAcceptFiles(Globals.hMainWnd, TRUE);
  694. HandleCommandLine(GetCommandLineW());
  695. hAccel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(ID_ACCEL));
  696. while (GetMessageW(&msg, 0, 0, 0))
  697. {
  698. if (!IsDialogMessageW(Globals.hFindReplaceDlg, &msg) && !TranslateAcceleratorW(Globals.hMainWnd, hAccel, &msg))
  699. {
  700. TranslateMessage(&msg);
  701. DispatchMessageW(&msg);
  702. }
  703. }
  704. return msg.wParam;
  705. }