main.c 29 KB

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