wordpad.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. /*
  2. * Wordpad implementation
  3. *
  4. * Copyright 2004 by Krzysztof Foltman
  5. * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #define WIN32_LEAN_AND_MEAN
  22. #include <stdarg.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <windows.h>
  28. #include <richedit.h>
  29. #include <commctrl.h>
  30. #include <commdlg.h>
  31. #include <shellapi.h>
  32. #include <math.h>
  33. #include <errno.h>
  34. #include "wordpad.h"
  35. #ifdef NONAMELESSUNION
  36. # define U(x) (x).u
  37. # define U2(x) (x).u2
  38. # define U3(x) (x).u3
  39. #else
  40. # define U(x) (x)
  41. # define U2(x) (x)
  42. # define U3(x) (x)
  43. #endif
  44. /* use LoadString */
  45. static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
  46. static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
  47. static const WCHAR stringFormat[] = {'%','2','d','\0'};
  48. const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
  49. LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  50. static HWND hMainWnd;
  51. static HWND hEditorWnd;
  52. static HWND hFindWnd;
  53. static HMENU hColorPopupMenu;
  54. static UINT ID_FINDMSGSTRING;
  55. static DWORD wordWrap[2];
  56. static DWORD barState[2];
  57. static WPARAM fileFormat = SF_RTF;
  58. static WCHAR wszFileName[MAX_PATH];
  59. static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
  60. static WCHAR wszDefaultFileName[MAX_STRING_LEN];
  61. static WCHAR wszSaveChanges[MAX_STRING_LEN];
  62. static WCHAR units_cmW[MAX_STRING_LEN];
  63. static WCHAR units_inW[MAX_STRING_LEN];
  64. static WCHAR units_inchW[MAX_STRING_LEN];
  65. static WCHAR units_ptW[MAX_STRING_LEN];
  66. static int last_bullet = PFN_BULLET;
  67. static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
  68. typedef enum
  69. {
  70. UNIT_CM,
  71. UNIT_INCH,
  72. UNIT_PT
  73. } UNIT;
  74. typedef struct
  75. {
  76. int endPos;
  77. BOOL wrapped;
  78. WCHAR findBuffer[128];
  79. } FINDREPLACE_custom;
  80. /* Load string resources */
  81. static void DoLoadStrings(void)
  82. {
  83. LPWSTR p = wszFilter;
  84. static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
  85. static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
  86. static const WCHAR files_all[] = {'*','.','*','\0'};
  87. HINSTANCE hInstance = GetModuleHandleW(0);
  88. p += 1 + LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
  89. lstrcpyW(p, files_rtf);
  90. p += lstrlenW(p) + 1;
  91. p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
  92. lstrcpyW(p, files_txt);
  93. p += lstrlenW(p) + 1;
  94. p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
  95. lstrcpyW(p, files_txt);
  96. p += lstrlenW(p) + 1;
  97. p += 1 + LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
  98. lstrcpyW(p, files_all);
  99. p += lstrlenW(p) + 1;
  100. *p = '\0';
  101. p = wszDefaultFileName;
  102. LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
  103. p = wszSaveChanges;
  104. LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
  105. LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
  106. LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
  107. LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
  108. LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
  109. }
  110. /* Show a message box with resource strings */
  111. static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
  112. {
  113. MSGBOXPARAMSW params;
  114. params.cbSize = sizeof(params);
  115. params.hwndOwner = hWnd;
  116. params.hInstance = GetModuleHandleW(0);
  117. params.lpszText = lpText;
  118. params.lpszCaption = lpCaption;
  119. params.dwStyle = uType;
  120. params.lpszIcon = NULL;
  121. params.dwContextHelpId = 0;
  122. params.lpfnMsgBoxCallback = NULL;
  123. params.dwLanguageId = 0;
  124. return MessageBoxIndirectW(&params);
  125. }
  126. static void AddButtonStyle(HWND hwndToolBar, int nImage, int nCommand, BYTE style)
  127. {
  128. TBBUTTON button;
  129. ZeroMemory(&button, sizeof(button));
  130. button.iBitmap = nImage;
  131. button.idCommand = nCommand;
  132. button.fsState = TBSTATE_ENABLED;
  133. button.fsStyle = style;
  134. button.dwData = 0;
  135. button.iString = -1;
  136. SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
  137. }
  138. static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
  139. {
  140. AddButtonStyle(hwndToolBar, nImage, nCommand, BTNS_BUTTON);
  141. }
  142. static void AddSeparator(HWND hwndToolBar)
  143. {
  144. TBBUTTON button;
  145. ZeroMemory(&button, sizeof(button));
  146. button.iBitmap = -1;
  147. button.idCommand = 0;
  148. button.fsState = 0;
  149. button.fsStyle = BTNS_SEP;
  150. button.dwData = 0;
  151. button.iString = -1;
  152. SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
  153. }
  154. static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
  155. {
  156. HANDLE hFile = (HANDLE)cookie;
  157. DWORD read;
  158. if(!ReadFile(hFile, buffer, cb, &read, 0))
  159. return 1;
  160. *pcb = read;
  161. return 0;
  162. }
  163. static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
  164. {
  165. DWORD written;
  166. int ret;
  167. HANDLE hFile = (HANDLE)cookie;
  168. ret = WriteFile(hFile, buffer, cb, &written, 0);
  169. if(!ret || (cb != written))
  170. return 1;
  171. *pcb = cb;
  172. return 0;
  173. }
  174. LPWSTR file_basename(LPWSTR path)
  175. {
  176. LPWSTR pos = path + lstrlenW(path);
  177. while(pos > path)
  178. {
  179. if(*pos == '\\' || *pos == '/')
  180. {
  181. pos++;
  182. break;
  183. }
  184. pos--;
  185. }
  186. return pos;
  187. }
  188. static void set_caption(LPCWSTR wszNewFileName)
  189. {
  190. static const WCHAR wszSeparator[] = {' ','-',' '};
  191. WCHAR *wszCaption;
  192. SIZE_T length = 0;
  193. if(!wszNewFileName)
  194. wszNewFileName = wszDefaultFileName;
  195. else
  196. wszNewFileName = file_basename((LPWSTR)wszNewFileName);
  197. wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  198. lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
  199. if(!wszCaption)
  200. return;
  201. memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
  202. length += lstrlenW(wszNewFileName);
  203. memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
  204. length += ARRAY_SIZE(wszSeparator);
  205. memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
  206. SetWindowTextW(hMainWnd, wszCaption);
  207. HeapFree(GetProcessHeap(), 0, wszCaption);
  208. }
  209. static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
  210. {
  211. if(punit != NULL)
  212. *punit = UNIT_CM;
  213. if(!endptr)
  214. return FALSE;
  215. if(!*endptr)
  216. return TRUE;
  217. while(*endptr == ' ')
  218. endptr++;
  219. if(punit == NULL)
  220. return *endptr == '\0';
  221. if(!lstrcmpW(endptr, units_cmW))
  222. {
  223. *punit = UNIT_CM;
  224. endptr += lstrlenW(units_cmW);
  225. }
  226. else if (!lstrcmpW(endptr, units_inW))
  227. {
  228. *punit = UNIT_INCH;
  229. endptr += lstrlenW(units_inW);
  230. }
  231. else if (!lstrcmpW(endptr, units_inchW))
  232. {
  233. *punit = UNIT_INCH;
  234. endptr += lstrlenW(units_inchW);
  235. }
  236. else if (!lstrcmpW(endptr, units_ptW))
  237. {
  238. *punit = UNIT_PT;
  239. endptr += lstrlenW(units_ptW);
  240. }
  241. return *endptr == '\0';
  242. }
  243. static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
  244. {
  245. double ret;
  246. WCHAR *endptr;
  247. *num = 0;
  248. errno = 0;
  249. ret = wcstod(string, &endptr);
  250. if (punit != NULL)
  251. *punit = UNIT_CM;
  252. if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
  253. {
  254. return FALSE;
  255. } else
  256. {
  257. *num = (float)ret;
  258. return TRUE;
  259. }
  260. }
  261. static void set_size(float size)
  262. {
  263. CHARFORMAT2W fmt;
  264. ZeroMemory(&fmt, sizeof(fmt));
  265. fmt.cbSize = sizeof(fmt);
  266. fmt.dwMask = CFM_SIZE;
  267. fmt.yHeight = (int)(size * 20.0);
  268. SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  269. }
  270. static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
  271. {
  272. WCHAR sizeBuffer[MAX_STRING_LEN];
  273. CHARFORMAT2W format;
  274. ZeroMemory(&format, sizeof(format));
  275. format.cbSize = sizeof(format);
  276. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
  277. wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
  278. if(lstrcmpW(sizeBuffer, wszNewFontSize))
  279. {
  280. float size = 0;
  281. if(number_from_string(wszNewFontSize, &size, NULL)
  282. && size > 0)
  283. {
  284. set_size(size);
  285. } else
  286. {
  287. SetWindowTextW(hwndSizeList, sizeBuffer);
  288. MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
  289. wszAppTitle, MB_OK | MB_ICONINFORMATION);
  290. }
  291. }
  292. }
  293. static void add_size(HWND hSizeListWnd, unsigned size)
  294. {
  295. WCHAR buffer[3];
  296. COMBOBOXEXITEMW cbItem;
  297. cbItem.mask = CBEIF_TEXT;
  298. cbItem.iItem = -1;
  299. wsprintfW(buffer, stringFormat, size);
  300. cbItem.pszText = buffer;
  301. SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
  302. }
  303. static void populate_size_list(HWND hSizeListWnd)
  304. {
  305. HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
  306. HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
  307. COMBOBOXEXITEMW cbFontItem;
  308. CHARFORMAT2W fmt;
  309. HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
  310. HDC hdc = GetDC(hMainWnd);
  311. static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
  312. WCHAR buffer[3];
  313. size_t i;
  314. DWORD fontStyle;
  315. ZeroMemory(&fmt, sizeof(fmt));
  316. fmt.cbSize = sizeof(fmt);
  317. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  318. cbFontItem.mask = CBEIF_LPARAM;
  319. cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
  320. SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
  321. fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
  322. SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
  323. if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
  324. {
  325. add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
  326. GetDeviceCaps(hdc, LOGPIXELSY)));
  327. } else
  328. {
  329. for(i = 0; i < ARRAY_SIZE(choices); i++)
  330. add_size(hSizeListWnd, choices[i]);
  331. }
  332. wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
  333. SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
  334. }
  335. static void update_size_list(void)
  336. {
  337. HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
  338. HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
  339. HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
  340. WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
  341. CHARFORMAT2W fmt;
  342. ZeroMemory(&fmt, sizeof(fmt));
  343. fmt.cbSize = sizeof(fmt);
  344. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  345. SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
  346. wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
  347. if(lstrcmpW(fontSize, sizeBuffer))
  348. SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
  349. }
  350. static void update_font_list(void)
  351. {
  352. HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
  353. HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
  354. HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
  355. WCHAR fontName[MAX_STRING_LEN];
  356. CHARFORMAT2W fmt;
  357. ZeroMemory(&fmt, sizeof(fmt));
  358. fmt.cbSize = sizeof(fmt);
  359. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  360. if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
  361. if(lstrcmpW(fontName, fmt.szFaceName))
  362. {
  363. SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
  364. populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
  365. } else
  366. {
  367. update_size_list();
  368. }
  369. }
  370. static void clear_formatting(void)
  371. {
  372. PARAFORMAT2 pf;
  373. pf.cbSize = sizeof(pf);
  374. pf.dwMask = PFM_ALIGNMENT;
  375. pf.wAlignment = PFA_LEFT;
  376. SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
  377. }
  378. static int fileformat_number(WPARAM format)
  379. {
  380. int number = 0;
  381. if(format == SF_TEXT)
  382. {
  383. number = 1;
  384. } else if (format == (SF_TEXT | SF_UNICODE))
  385. {
  386. number = 2;
  387. }
  388. return number;
  389. }
  390. static WPARAM fileformat_flags(int format)
  391. {
  392. WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
  393. return flags[format];
  394. }
  395. static void set_font(LPCWSTR wszFaceName)
  396. {
  397. HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
  398. HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
  399. HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
  400. HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
  401. CHARFORMAT2W fmt;
  402. ZeroMemory(&fmt, sizeof(fmt));
  403. fmt.cbSize = sizeof(fmt);
  404. fmt.dwMask = CFM_FACE;
  405. lstrcpyW(fmt.szFaceName, wszFaceName);
  406. SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  407. populate_size_list(hSizeListWnd);
  408. SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
  409. }
  410. static void set_default_font(void)
  411. {
  412. static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
  413. 'R','o','m','a','n',0};
  414. static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
  415. CHARFORMAT2W fmt;
  416. LPCWSTR font;
  417. ZeroMemory(&fmt, sizeof(fmt));
  418. fmt.cbSize = sizeof(fmt);
  419. fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
  420. fmt.dwEffects = 0;
  421. if(fileFormat & SF_RTF)
  422. font = richTextFont;
  423. else
  424. font = plainTextFont;
  425. lstrcpyW(fmt.szFaceName, font);
  426. SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
  427. }
  428. static void on_fontlist_modified(LPWSTR wszNewFaceName)
  429. {
  430. CHARFORMAT2W format;
  431. ZeroMemory(&format, sizeof(format));
  432. format.cbSize = sizeof(format);
  433. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
  434. if(lstrcmpW(format.szFaceName, wszNewFaceName))
  435. set_font(wszNewFaceName);
  436. }
  437. static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
  438. {
  439. COMBOBOXEXITEMW cbItem;
  440. WCHAR buffer[MAX_PATH];
  441. int fontHeight = 0;
  442. cbItem.mask = CBEIF_TEXT;
  443. cbItem.pszText = buffer;
  444. cbItem.cchTextMax = MAX_STRING_LEN;
  445. cbItem.iItem = 0;
  446. while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
  447. {
  448. if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
  449. cbItem.iItem++;
  450. else
  451. break;
  452. }
  453. cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
  454. lstrcpyW( cbItem.pszText, fontName );
  455. cbItem.mask |= CBEIF_LPARAM;
  456. if(fontType & RASTER_FONTTYPE)
  457. fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
  458. cbItem.lParam = MAKELONG(fontType,fontHeight);
  459. SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
  460. HeapFree( GetProcessHeap(), 0, cbItem.pszText );
  461. }
  462. static void dialog_choose_font(void)
  463. {
  464. CHOOSEFONTW cf;
  465. LOGFONTW lf;
  466. CHARFORMAT2W fmt;
  467. HDC hDC = GetDC(hMainWnd);
  468. ZeroMemory(&cf, sizeof(cf));
  469. cf.lStructSize = sizeof(cf);
  470. cf.hwndOwner = hMainWnd;
  471. cf.lpLogFont = &lf;
  472. cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
  473. ZeroMemory(&fmt, sizeof(fmt));
  474. fmt.cbSize = sizeof(fmt);
  475. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  476. lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
  477. cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
  478. cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
  479. cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
  480. cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
  481. cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
  482. cf.rgbColors = fmt.crTextColor;
  483. if(ChooseFontW(&cf))
  484. {
  485. ZeroMemory(&fmt, sizeof(fmt));
  486. fmt.cbSize = sizeof(fmt);
  487. fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
  488. fmt.yHeight = cf.iPointSize * 2;
  489. if(cf.nFontType & BOLD_FONTTYPE)
  490. fmt.dwEffects |= CFE_BOLD;
  491. if(cf.nFontType & ITALIC_FONTTYPE)
  492. fmt.dwEffects |= CFE_ITALIC;
  493. if(cf.lpLogFont->lfUnderline)
  494. fmt.dwEffects |= CFE_UNDERLINE;
  495. if(cf.lpLogFont->lfStrikeOut)
  496. fmt.dwEffects |= CFE_STRIKEOUT;
  497. fmt.crTextColor = cf.rgbColors;
  498. SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  499. set_font(cf.lpLogFont->lfFaceName);
  500. }
  501. }
  502. static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
  503. DWORD FontType, LPARAM lParam)
  504. {
  505. HWND hListWnd = (HWND) lParam;
  506. if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
  507. if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
  508. {
  509. add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
  510. }
  511. return 1;
  512. }
  513. static void populate_font_list(HWND hListWnd)
  514. {
  515. HDC hdc = GetDC(hMainWnd);
  516. LOGFONTW fontinfo;
  517. HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
  518. CHARFORMAT2W fmt;
  519. fontinfo.lfCharSet = DEFAULT_CHARSET;
  520. *fontinfo.lfFaceName = '\0';
  521. fontinfo.lfPitchAndFamily = 0;
  522. EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
  523. (LPARAM)hListWnd, 0);
  524. ZeroMemory(&fmt, sizeof(fmt));
  525. fmt.cbSize = sizeof(fmt);
  526. SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
  527. SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
  528. }
  529. static void update_window(void)
  530. {
  531. RECT rect;
  532. GetClientRect(hMainWnd, &rect);
  533. OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
  534. }
  535. static BOOL is_bar_visible(int bandId)
  536. {
  537. return barState[reg_formatindex(fileFormat)] & (1 << bandId);
  538. }
  539. static void store_bar_state(int bandId, BOOL show)
  540. {
  541. int formatIndex = reg_formatindex(fileFormat);
  542. if(show)
  543. barState[formatIndex] |= (1 << bandId);
  544. else
  545. barState[formatIndex] &= ~(1 << bandId);
  546. }
  547. static void set_toolbar_state(int bandId, BOOL show)
  548. {
  549. HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
  550. SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
  551. if(bandId == BANDID_TOOLBAR)
  552. {
  553. REBARBANDINFOW rbbinfo;
  554. int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
  555. rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
  556. rbbinfo.fMask = RBBIM_STYLE;
  557. SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
  558. if(!show)
  559. rbbinfo.fStyle &= ~RBBS_BREAK;
  560. else
  561. rbbinfo.fStyle |= RBBS_BREAK;
  562. SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
  563. }
  564. if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
  565. store_bar_state(bandId, show);
  566. }
  567. static void set_statusbar_state(BOOL show)
  568. {
  569. HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
  570. ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
  571. store_bar_state(BANDID_STATUSBAR, show);
  572. }
  573. static void set_bar_states(void)
  574. {
  575. set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
  576. set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
  577. set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
  578. set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
  579. set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
  580. set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
  581. update_window();
  582. }
  583. static void preview_exit(HWND hMainWnd)
  584. {
  585. HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
  586. HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
  587. set_bar_states();
  588. ShowWindow(hEditorWnd, TRUE);
  589. close_preview(hMainWnd);
  590. SetMenu(hMainWnd, hMenu);
  591. registry_read_filelist(hMainWnd);
  592. update_window();
  593. }
  594. static void set_fileformat(WPARAM format)
  595. {
  596. fileFormat = format;
  597. set_bar_states();
  598. set_default_font();
  599. target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
  600. }
  601. static void ShowOpenError(DWORD Code)
  602. {
  603. LPWSTR Message;
  604. switch(Code)
  605. {
  606. case ERROR_ACCESS_DENIED:
  607. Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
  608. break;
  609. default:
  610. Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
  611. }
  612. MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
  613. }
  614. static void DoOpenFile(LPCWSTR szOpenFileName)
  615. {
  616. HANDLE hFile;
  617. EDITSTREAM es;
  618. char fileStart[5];
  619. DWORD readOut;
  620. WPARAM format = SF_TEXT;
  621. hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  622. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  623. if (hFile == INVALID_HANDLE_VALUE)
  624. {
  625. ShowOpenError(GetLastError());
  626. return;
  627. }
  628. ReadFile(hFile, fileStart, 5, &readOut, NULL);
  629. SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
  630. if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
  631. {
  632. format = SF_TEXT | SF_UNICODE;
  633. SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
  634. } else if(readOut >= 5)
  635. {
  636. static const char header[] = "{\\rtf";
  637. static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
  638. if(!memcmp(header, fileStart, 5))
  639. format = SF_RTF;
  640. else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
  641. {
  642. CloseHandle(hFile);
  643. MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
  644. wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
  645. return;
  646. }
  647. }
  648. es.dwCookie = (DWORD_PTR)hFile;
  649. es.pfnCallback = stream_in;
  650. clear_formatting();
  651. set_fileformat(format);
  652. SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
  653. CloseHandle(hFile);
  654. SetFocus(hEditorWnd);
  655. set_caption(szOpenFileName);
  656. lstrcpyW(wszFileName, szOpenFileName);
  657. SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
  658. registry_set_filelist(szOpenFileName, hMainWnd);
  659. update_font_list();
  660. }
  661. static void ShowWriteError(DWORD Code)
  662. {
  663. LPWSTR Message;
  664. switch(Code)
  665. {
  666. case ERROR_ACCESS_DENIED:
  667. Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
  668. break;
  669. default:
  670. Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
  671. }
  672. MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
  673. }
  674. static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
  675. {
  676. HANDLE hFile;
  677. EDITSTREAM stream;
  678. LRESULT ret;
  679. hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  680. FILE_ATTRIBUTE_NORMAL, NULL);
  681. if(hFile == INVALID_HANDLE_VALUE)
  682. {
  683. ShowWriteError(GetLastError());
  684. return FALSE;
  685. }
  686. if(format == (SF_TEXT | SF_UNICODE))
  687. {
  688. static const BYTE unicode[] = {0xff,0xfe};
  689. DWORD writeOut;
  690. WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
  691. if(writeOut != sizeof(unicode))
  692. {
  693. CloseHandle(hFile);
  694. return FALSE;
  695. }
  696. }
  697. stream.dwCookie = (DWORD_PTR)hFile;
  698. stream.pfnCallback = stream_out;
  699. ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
  700. CloseHandle(hFile);
  701. SetFocus(hEditorWnd);
  702. if(!ret)
  703. {
  704. GETTEXTLENGTHEX gt;
  705. gt.flags = GTL_DEFAULT;
  706. gt.codepage = 1200;
  707. if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
  708. return FALSE;
  709. }
  710. lstrcpyW(wszFileName, wszSaveFileName);
  711. set_caption(wszFileName);
  712. SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
  713. set_fileformat(format);
  714. return TRUE;
  715. }
  716. static BOOL DialogSaveFile(void)
  717. {
  718. OPENFILENAMEW sfn;
  719. WCHAR wszFile[MAX_PATH] = {'\0'};
  720. static const WCHAR wszDefExt[] = {'r','t','f','\0'};
  721. ZeroMemory(&sfn, sizeof(sfn));
  722. sfn.lStructSize = sizeof(sfn);
  723. sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
  724. sfn.hwndOwner = hMainWnd;
  725. sfn.lpstrFilter = wszFilter;
  726. sfn.lpstrFile = wszFile;
  727. sfn.nMaxFile = MAX_PATH;
  728. sfn.lpstrDefExt = wszDefExt;
  729. sfn.nFilterIndex = fileformat_number(fileFormat)+1;
  730. while(GetSaveFileNameW(&sfn))
  731. {
  732. if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
  733. {
  734. if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
  735. wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
  736. continue;
  737. }
  738. return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
  739. }
  740. return FALSE;
  741. }
  742. static BOOL prompt_save_changes(void)
  743. {
  744. if(!wszFileName[0])
  745. {
  746. GETTEXTLENGTHEX gt;
  747. gt.flags = GTL_NUMCHARS;
  748. gt.codepage = 1200;
  749. if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
  750. return TRUE;
  751. }
  752. if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
  753. {
  754. return TRUE;
  755. } else
  756. {
  757. LPWSTR displayFileName;
  758. WCHAR *text;
  759. int ret;
  760. if(!wszFileName[0])
  761. displayFileName = wszDefaultFileName;
  762. else
  763. displayFileName = file_basename(wszFileName);
  764. text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  765. (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
  766. if(!text)
  767. return FALSE;
  768. wsprintfW(text, wszSaveChanges, displayFileName);
  769. ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  770. HeapFree(GetProcessHeap(), 0, text);
  771. switch(ret)
  772. {
  773. case IDNO:
  774. return TRUE;
  775. case IDYES:
  776. if(wszFileName[0])
  777. return DoSaveFile(wszFileName, fileFormat);
  778. return DialogSaveFile();
  779. default:
  780. return FALSE;
  781. }
  782. }
  783. }
  784. static void DialogOpenFile(void)
  785. {
  786. OPENFILENAMEW ofn;
  787. WCHAR wszFile[MAX_PATH] = {'\0'};
  788. static const WCHAR wszDefExt[] = {'r','t','f','\0'};
  789. ZeroMemory(&ofn, sizeof(ofn));
  790. ofn.lStructSize = sizeof(ofn);
  791. ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
  792. ofn.hwndOwner = hMainWnd;
  793. ofn.lpstrFilter = wszFilter;
  794. ofn.lpstrFile = wszFile;
  795. ofn.nMaxFile = MAX_PATH;
  796. ofn.lpstrDefExt = wszDefExt;
  797. ofn.nFilterIndex = fileformat_number(fileFormat)+1;
  798. if(GetOpenFileNameW(&ofn))
  799. {
  800. if(prompt_save_changes())
  801. DoOpenFile(ofn.lpstrFile);
  802. }
  803. }
  804. static void dialog_about(void)
  805. {
  806. HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
  807. ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
  808. }
  809. static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  810. {
  811. switch(message)
  812. {
  813. case WM_INITDIALOG:
  814. {
  815. LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
  816. int wrap = -1;
  817. char id[4];
  818. HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
  819. sprintf(id, "%d\n", (int)ps->lParam);
  820. SetWindowTextA(hIdWnd, id);
  821. if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
  822. wrap = IDC_PAGEFMT_WN;
  823. else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
  824. wrap = IDC_PAGEFMT_WW;
  825. else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
  826. wrap = IDC_PAGEFMT_WM;
  827. if(wrap != -1)
  828. CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
  829. IDC_PAGEFMT_WM, wrap);
  830. if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
  831. CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
  832. if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
  833. CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
  834. if(barState[ps->lParam] & (1 << BANDID_RULER))
  835. CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
  836. if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
  837. CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
  838. }
  839. break;
  840. case WM_COMMAND:
  841. switch(LOWORD(wParam))
  842. {
  843. case IDC_PAGEFMT_WN:
  844. case IDC_PAGEFMT_WW:
  845. case IDC_PAGEFMT_WM:
  846. CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
  847. LOWORD(wParam));
  848. break;
  849. case IDC_PAGEFMT_TB:
  850. case IDC_PAGEFMT_FB:
  851. case IDC_PAGEFMT_RU:
  852. case IDC_PAGEFMT_SB:
  853. CheckDlgButton(hWnd, LOWORD(wParam),
  854. !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
  855. break;
  856. }
  857. break;
  858. case WM_NOTIFY:
  859. {
  860. LPNMHDR header = (LPNMHDR)lParam;
  861. if(header->code == PSN_APPLY)
  862. {
  863. HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
  864. char sid[4];
  865. int id;
  866. GetWindowTextA(hIdWnd, sid, 4);
  867. id = atoi(sid);
  868. if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
  869. wordWrap[id] = ID_WORDWRAP_NONE;
  870. else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
  871. wordWrap[id] = ID_WORDWRAP_WINDOW;
  872. else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
  873. wordWrap[id] = ID_WORDWRAP_MARGIN;
  874. if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
  875. barState[id] |= (1 << BANDID_TOOLBAR);
  876. else
  877. barState[id] &= ~(1 << BANDID_TOOLBAR);
  878. if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
  879. barState[id] |= (1 << BANDID_FORMATBAR);
  880. else
  881. barState[id] &= ~(1 << BANDID_FORMATBAR);
  882. if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
  883. barState[id] |= (1 << BANDID_RULER);
  884. else
  885. barState[id] &= ~(1 << BANDID_RULER);
  886. if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
  887. barState[id] |= (1 << BANDID_STATUSBAR);
  888. else
  889. barState[id] &= ~(1 << BANDID_STATUSBAR);
  890. }
  891. }
  892. break;
  893. }
  894. return FALSE;
  895. }
  896. static void dialog_viewproperties(void)
  897. {
  898. PROPSHEETPAGEW psp[2];
  899. PROPSHEETHEADERW psh;
  900. size_t i;
  901. HINSTANCE hInstance = GetModuleHandleW(0);
  902. LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
  903. psp[0].dwSize = sizeof(PROPSHEETPAGEW);
  904. psp[0].dwFlags = PSP_USETITLE;
  905. U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
  906. psp[0].pfnDlgProc = formatopts_proc;
  907. psp[0].hInstance = hInstance;
  908. psp[0].lParam = reg_formatindex(SF_TEXT);
  909. psp[0].pfnCallback = NULL;
  910. psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
  911. for(i = 1; i < ARRAY_SIZE(psp); i++)
  912. {
  913. psp[i].dwSize = psp[0].dwSize;
  914. psp[i].dwFlags = psp[0].dwFlags;
  915. U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
  916. psp[i].pfnDlgProc = psp[0].pfnDlgProc;
  917. psp[i].hInstance = psp[0].hInstance;
  918. psp[i].lParam = reg_formatindex(SF_RTF);
  919. psp[i].pfnCallback = psp[0].pfnCallback;
  920. psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
  921. }
  922. psh.dwSize = sizeof(psh);
  923. psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
  924. psh.hwndParent = hMainWnd;
  925. psh.hInstance = hInstance;
  926. psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
  927. psh.nPages = ARRAY_SIZE(psp);
  928. U3(psh).ppsp = ppsp;
  929. U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
  930. if(fileFormat & SF_RTF)
  931. U2(psh).nStartPage = 1;
  932. else
  933. U2(psh).nStartPage = 0;
  934. PropertySheetW(&psh);
  935. set_bar_states();
  936. target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
  937. }
  938. static void HandleCommandLine(LPWSTR cmdline)
  939. {
  940. WCHAR delimiter;
  941. BOOL opt_print = FALSE;
  942. /* skip white space */
  943. while (*cmdline == ' ') cmdline++;
  944. /* skip executable name */
  945. delimiter = (*cmdline == '"' ? '"' : ' ');
  946. if (*cmdline == delimiter) cmdline++;
  947. while (*cmdline && *cmdline != delimiter) cmdline++;
  948. if (*cmdline == delimiter) cmdline++;
  949. while (*cmdline)
  950. {
  951. while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
  952. if (*cmdline == '-' || *cmdline == '/')
  953. {
  954. if (!cmdline[2] || isspace(cmdline[2]))
  955. {
  956. switch (cmdline[1])
  957. {
  958. case 'P':
  959. case 'p':
  960. opt_print = TRUE;
  961. cmdline += 2;
  962. continue;
  963. }
  964. }
  965. /* a filename starting by / */
  966. }
  967. break;
  968. }
  969. if (*cmdline)
  970. {
  971. /* file name is passed on the command line */
  972. if (cmdline[0] == '"')
  973. {
  974. cmdline++;
  975. cmdline[lstrlenW(cmdline) - 1] = 0;
  976. }
  977. DoOpenFile(cmdline);
  978. InvalidateRect(hMainWnd, NULL, FALSE);
  979. }
  980. if (opt_print)
  981. MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
  982. }
  983. static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
  984. {
  985. if(pFr->Flags & FR_DIALOGTERM)
  986. {
  987. hFindWnd = 0;
  988. pFr->Flags = FR_FINDNEXT;
  989. return 0;
  990. }
  991. if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
  992. {
  993. FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
  994. DWORD flags;
  995. FINDTEXTEXW ft;
  996. CHARRANGE sel;
  997. LRESULT ret = -1;
  998. HMENU hMenu = GetMenu(hMainWnd);
  999. MENUITEMINFOW mi;
  1000. mi.cbSize = sizeof(mi);
  1001. mi.fMask = MIIM_DATA;
  1002. mi.dwItemData = 1;
  1003. SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
  1004. /* Make sure find field is saved. */
  1005. if (pFr->lpstrFindWhat != custom_data->findBuffer)
  1006. {
  1007. lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
  1008. ARRAY_SIZE(custom_data->findBuffer));
  1009. pFr->lpstrFindWhat = custom_data->findBuffer;
  1010. }
  1011. SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
  1012. if(custom_data->endPos == -1) {
  1013. custom_data->endPos = sel.cpMin;
  1014. custom_data->wrapped = FALSE;
  1015. }
  1016. flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
  1017. ft.lpstrText = pFr->lpstrFindWhat;
  1018. /* Only replace the existing selection if it is an exact match. */
  1019. if (sel.cpMin != sel.cpMax &&
  1020. (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
  1021. {
  1022. ft.chrg = sel;
  1023. SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
  1024. if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
  1025. SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
  1026. SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
  1027. }
  1028. }
  1029. /* Search from the start of the selection, but exclude the first character
  1030. * from search if there is a selection. */
  1031. ft.chrg.cpMin = sel.cpMin;
  1032. if (sel.cpMin != sel.cpMax)
  1033. ft.chrg.cpMin++;
  1034. /* Search to the end, then wrap around and search from the start. */
  1035. if (!custom_data->wrapped) {
  1036. ft.chrg.cpMax = -1;
  1037. ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
  1038. if (ret == -1) {
  1039. custom_data->wrapped = TRUE;
  1040. ft.chrg.cpMin = 0;
  1041. }
  1042. }
  1043. if (ret == -1) {
  1044. ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
  1045. if (ft.chrg.cpMax > ft.chrg.cpMin)
  1046. ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
  1047. }
  1048. if (ret == -1) {
  1049. custom_data->endPos = -1;
  1050. EnableWindow(hMainWnd, FALSE);
  1051. MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
  1052. wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
  1053. EnableWindow(hMainWnd, TRUE);
  1054. } else {
  1055. SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
  1056. SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
  1057. if (pFr->Flags & FR_REPLACEALL)
  1058. return handle_findmsg(pFr);
  1059. }
  1060. }
  1061. return 0;
  1062. }
  1063. static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
  1064. {
  1065. static WCHAR selBuffer[128];
  1066. static WCHAR replaceBuffer[128];
  1067. static FINDREPLACE_custom custom_data;
  1068. static const WCHAR endl = '\r';
  1069. FINDTEXTW ft;
  1070. /* Allow only one search/replace dialog to open */
  1071. if(hFindWnd != NULL)
  1072. {
  1073. SetActiveWindow(hFindWnd);
  1074. return;
  1075. }
  1076. ZeroMemory(fr, sizeof(FINDREPLACEW));
  1077. fr->lStructSize = sizeof(FINDREPLACEW);
  1078. fr->hwndOwner = hMainWnd;
  1079. fr->Flags = FR_HIDEUPDOWN;
  1080. /* Find field is filled with the selected text if it is non-empty
  1081. * and stays within the same paragraph, otherwise the previous
  1082. * find field is used. */
  1083. SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
  1084. (LPARAM)&ft.chrg.cpMax);
  1085. ft.lpstrText = &endl;
  1086. if (ft.chrg.cpMin != ft.chrg.cpMax &&
  1087. SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
  1088. {
  1089. /* Use a temporary buffer for the selected text so that the saved
  1090. * find field is only overwritten when a find/replace is clicked. */
  1091. GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
  1092. SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
  1093. fr->lpstrFindWhat = selBuffer;
  1094. } else {
  1095. fr->lpstrFindWhat = custom_data.findBuffer;
  1096. }
  1097. fr->lpstrReplaceWith = replaceBuffer;
  1098. custom_data.endPos = -1;
  1099. custom_data.wrapped = FALSE;
  1100. fr->lCustData = (LPARAM)&custom_data;
  1101. fr->wFindWhatLen = sizeof(custom_data.findBuffer);
  1102. fr->wReplaceWithLen = sizeof(replaceBuffer);
  1103. if(replace)
  1104. hFindWnd = ReplaceTextW(fr);
  1105. else
  1106. hFindWnd = FindTextW(fr);
  1107. }
  1108. static int units_to_twips(UNIT unit, float number)
  1109. {
  1110. int twips = 0;
  1111. switch(unit)
  1112. {
  1113. case UNIT_CM:
  1114. twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
  1115. break;
  1116. case UNIT_INCH:
  1117. twips = (int)(number * (float)TWIPS_PER_INCH);
  1118. break;
  1119. case UNIT_PT:
  1120. twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
  1121. break;
  1122. }
  1123. return twips;
  1124. }
  1125. static void append_current_units(LPWSTR buffer)
  1126. {
  1127. static const WCHAR space[] = {' ', 0};
  1128. lstrcatW(buffer, space);
  1129. lstrcatW(buffer, units_cmW);
  1130. }
  1131. static void number_with_units(LPWSTR buffer, int number)
  1132. {
  1133. static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
  1134. float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
  1135. swprintf(buffer, MAX_STRING_LEN, fmt, converted, units_cmW);
  1136. }
  1137. static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
  1138. {
  1139. COMBOBOXEXITEMW cbItem;
  1140. COMBOBOXINFO cbInfo;
  1141. HWND hCombo, hList;
  1142. int idx, result;
  1143. hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
  1144. if (!hCombo)
  1145. return FALSE;
  1146. cbInfo.cbSize = sizeof(COMBOBOXINFO);
  1147. result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
  1148. if (!result)
  1149. return FALSE;
  1150. hList = cbInfo.hwndList;
  1151. idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
  1152. if (idx < 0)
  1153. return FALSE;
  1154. ZeroMemory(&cbItem, sizeof(cbItem));
  1155. cbItem.mask = CBEIF_TEXT;
  1156. cbItem.iItem = idx;
  1157. cbItem.pszText = wszBuffer;
  1158. cbItem.cchTextMax = bufferLength-1;
  1159. result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
  1160. return result != 0;
  1161. }
  1162. static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1163. {
  1164. switch(message)
  1165. {
  1166. case WM_INITDIALOG:
  1167. {
  1168. WCHAR buffer[MAX_STRING_LEN];
  1169. SYSTEMTIME st;
  1170. HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
  1171. GetLocalTime(&st);
  1172. GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
  1173. MAX_STRING_LEN);
  1174. SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
  1175. GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
  1176. MAX_STRING_LEN);
  1177. SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
  1178. GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
  1179. SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
  1180. SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
  1181. }
  1182. break;
  1183. case WM_COMMAND:
  1184. switch(LOWORD(wParam))
  1185. {
  1186. case IDC_DATETIME:
  1187. if (HIWORD(wParam) != LBN_DBLCLK)
  1188. break;
  1189. /* Fall through */
  1190. case IDOK:
  1191. {
  1192. LRESULT index;
  1193. HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
  1194. index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
  1195. if(index != LB_ERR)
  1196. {
  1197. WCHAR buffer[MAX_STRING_LEN];
  1198. SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
  1199. SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
  1200. }
  1201. }
  1202. /* Fall through */
  1203. case IDCANCEL:
  1204. EndDialog(hWnd, wParam);
  1205. return TRUE;
  1206. }
  1207. }
  1208. return FALSE;
  1209. }
  1210. static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1211. {
  1212. switch(message)
  1213. {
  1214. case WM_INITDIALOG:
  1215. {
  1216. HINSTANCE hInstance = GetModuleHandleW(0);
  1217. WCHAR buffer[MAX_STRING_LEN];
  1218. HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
  1219. LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
  1220. SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
  1221. LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
  1222. SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
  1223. LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
  1224. SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
  1225. SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
  1226. }
  1227. break;
  1228. case WM_COMMAND:
  1229. switch(LOWORD(wParam))
  1230. {
  1231. case IDOK:
  1232. {
  1233. LRESULT index;
  1234. HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
  1235. index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
  1236. if(index != LB_ERR)
  1237. EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
  1238. }
  1239. return TRUE;
  1240. case IDCANCEL:
  1241. EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
  1242. return TRUE;
  1243. }
  1244. }
  1245. return FALSE;
  1246. }
  1247. static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1248. {
  1249. static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
  1250. switch(message)
  1251. {
  1252. case WM_INITDIALOG:
  1253. {
  1254. HINSTANCE hInstance = GetModuleHandleW(0);
  1255. WCHAR buffer[MAX_STRING_LEN];
  1256. HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
  1257. HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
  1258. HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
  1259. HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
  1260. PARAFORMAT2 pf;
  1261. int index = 0;
  1262. LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
  1263. MAX_STRING_LEN);
  1264. SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
  1265. LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
  1266. MAX_STRING_LEN);
  1267. SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
  1268. LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
  1269. MAX_STRING_LEN);
  1270. SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
  1271. pf.cbSize = sizeof(pf);
  1272. pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
  1273. PFM_STARTINDENT;
  1274. SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  1275. if(pf.wAlignment == PFA_RIGHT)
  1276. index ++;
  1277. else if(pf.wAlignment == PFA_CENTER)
  1278. index += 2;
  1279. SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
  1280. number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
  1281. SetWindowTextW(hLeftWnd, buffer);
  1282. number_with_units(buffer, pf.dxRightIndent);
  1283. SetWindowTextW(hRightWnd, buffer);
  1284. number_with_units(buffer, -pf.dxOffset);
  1285. SetWindowTextW(hFirstWnd, buffer);
  1286. }
  1287. break;
  1288. case WM_COMMAND:
  1289. switch(LOWORD(wParam))
  1290. {
  1291. case IDOK:
  1292. {
  1293. HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
  1294. HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
  1295. HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
  1296. HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
  1297. WCHAR buffer[MAX_STRING_LEN];
  1298. int index;
  1299. float num;
  1300. int ret = 0;
  1301. PARAFORMAT2 pf;
  1302. UNIT unit;
  1303. BOOL in_list = FALSE;
  1304. pf.cbSize = sizeof(pf);
  1305. pf.dwMask = PFM_NUMBERING;
  1306. SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  1307. if ((pf.dwMask & PFM_NUMBERING) && pf.wNumbering)
  1308. in_list = TRUE;
  1309. index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
  1310. pf.wAlignment = ALIGNMENT_VALUES[index];
  1311. GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
  1312. if(number_from_string(buffer, &num, &unit))
  1313. ret++;
  1314. pf.dxOffset = units_to_twips(unit, num);
  1315. GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
  1316. if(number_from_string(buffer, &num, &unit))
  1317. ret++;
  1318. pf.dxRightIndent = units_to_twips(unit, num);
  1319. GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
  1320. if(number_from_string(buffer, &num, &unit))
  1321. ret++;
  1322. pf.dxStartIndent = units_to_twips(unit, num);
  1323. if(ret != 3)
  1324. {
  1325. MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
  1326. wszAppTitle, MB_OK | MB_ICONASTERISK);
  1327. return FALSE;
  1328. } else
  1329. {
  1330. if (pf.dxOffset + pf.dxStartIndent < 0
  1331. && pf.dxStartIndent < 0)
  1332. {
  1333. /* The first line is before the left edge, so
  1334. * make sure it is at the left edge. */
  1335. pf.dxOffset = -pf.dxStartIndent;
  1336. } else if (pf.dxOffset < 0) {
  1337. /* The second and following lines are before
  1338. * the left edge, so set it to be at the left
  1339. * edge, and adjust the first line since it
  1340. * is relative to it. */
  1341. pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
  1342. pf.dxOffset = 0;
  1343. }
  1344. /* Internally the dxStartIndent is the absolute
  1345. * offset for the first line and dxOffset is
  1346. * to it value as opposed how it is displayed with
  1347. * the first line being the relative value.
  1348. * These two lines make the adjustments. */
  1349. pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
  1350. pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
  1351. pf.cbSize = sizeof(pf);
  1352. pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
  1353. PFM_STARTINDENT;
  1354. if (in_list)
  1355. {
  1356. pf.wNumberingTab = max(pf.dxOffset, 0);
  1357. pf.dwMask |= PFM_NUMBERINGTAB;
  1358. }
  1359. SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
  1360. }
  1361. }
  1362. /* Fall through */
  1363. case IDCANCEL:
  1364. EndDialog(hWnd, wParam);
  1365. return TRUE;
  1366. }
  1367. }
  1368. return FALSE;
  1369. }
  1370. static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  1371. {
  1372. switch(message)
  1373. {
  1374. case WM_INITDIALOG:
  1375. {
  1376. HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
  1377. PARAFORMAT pf;
  1378. WCHAR buffer[MAX_STRING_LEN];
  1379. int i;
  1380. pf.cbSize = sizeof(pf);
  1381. pf.dwMask = PFM_TABSTOPS;
  1382. SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  1383. SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
  1384. for(i = 0; i < pf.cTabCount; i++)
  1385. {
  1386. number_with_units(buffer, pf.rgxTabs[i]);
  1387. SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
  1388. }
  1389. SetFocus(hTabWnd);
  1390. }
  1391. break;
  1392. case WM_COMMAND:
  1393. switch(LOWORD(wParam))
  1394. {
  1395. case IDC_TABSTOPS:
  1396. {
  1397. HWND hTabWnd = (HWND)lParam;
  1398. HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
  1399. HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
  1400. HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
  1401. if(GetWindowTextLengthW(hTabWnd))
  1402. EnableWindow(hAddWnd, TRUE);
  1403. else
  1404. EnableWindow(hAddWnd, FALSE);
  1405. if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
  1406. {
  1407. EnableWindow(hEmptyWnd, TRUE);
  1408. if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
  1409. EnableWindow(hDelWnd, FALSE);
  1410. else
  1411. EnableWindow(hDelWnd, TRUE);
  1412. } else
  1413. {
  1414. EnableWindow(hEmptyWnd, FALSE);
  1415. }
  1416. }
  1417. break;
  1418. case ID_TAB_ADD:
  1419. {
  1420. HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
  1421. WCHAR buffer[MAX_STRING_LEN];
  1422. UNIT unit;
  1423. GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
  1424. append_current_units(buffer);
  1425. if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
  1426. {
  1427. float number = 0;
  1428. int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
  1429. if(!number_from_string(buffer, &number, &unit))
  1430. {
  1431. MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
  1432. wszAppTitle, MB_OK | MB_ICONINFORMATION);
  1433. } else if (item_count >= MAX_TAB_STOPS) {
  1434. MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
  1435. wszAppTitle, MB_OK | MB_ICONINFORMATION);
  1436. } else {
  1437. int i;
  1438. float next_number = -1;
  1439. int next_number_in_twips = -1;
  1440. int insert_number = units_to_twips(unit, number);
  1441. /* linear search for position to insert the string */
  1442. for(i = 0; i < item_count; i++)
  1443. {
  1444. SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
  1445. number_from_string(buffer, &next_number, &unit);
  1446. next_number_in_twips = units_to_twips(unit, next_number);
  1447. if (insert_number <= next_number_in_twips)
  1448. break;
  1449. }
  1450. if (insert_number != next_number_in_twips)
  1451. {
  1452. number_with_units(buffer, insert_number);
  1453. SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
  1454. SetWindowTextW(hTabWnd, 0);
  1455. }
  1456. }
  1457. }
  1458. SetFocus(hTabWnd);
  1459. }
  1460. break;
  1461. case ID_TAB_DEL:
  1462. {
  1463. HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
  1464. LRESULT ret;
  1465. ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
  1466. if(ret != CB_ERR)
  1467. SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
  1468. }
  1469. break;
  1470. case ID_TAB_EMPTY:
  1471. {
  1472. HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
  1473. SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
  1474. SetFocus(hTabWnd);
  1475. }
  1476. break;
  1477. case IDOK:
  1478. {
  1479. HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
  1480. int i;
  1481. WCHAR buffer[MAX_STRING_LEN];
  1482. PARAFORMAT pf;
  1483. float number;
  1484. UNIT unit;
  1485. pf.cbSize = sizeof(pf);
  1486. pf.dwMask = PFM_TABSTOPS;
  1487. for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
  1488. (LPARAM)&buffer) != CB_ERR &&
  1489. i < MAX_TAB_STOPS; i++)
  1490. {
  1491. number_from_string(buffer, &number, &unit);
  1492. pf.rgxTabs[i] = units_to_twips(unit, number);
  1493. }
  1494. pf.cTabCount = i;
  1495. SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
  1496. }
  1497. /* Fall through */
  1498. case IDCANCEL:
  1499. EndDialog(hWnd, wParam);
  1500. return TRUE;
  1501. }
  1502. }
  1503. return FALSE;
  1504. }
  1505. static LRESULT OnCreate( HWND hWnd )
  1506. {
  1507. HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
  1508. HINSTANCE hInstance = GetModuleHandleW(0);
  1509. HANDLE hDLL;
  1510. TBADDBITMAP ab;
  1511. int nStdBitmaps = 0;
  1512. REBARINFO rbi;
  1513. REBARBANDINFOW rbb;
  1514. RECT rect;
  1515. HFONT font;
  1516. HDC hdc;
  1517. SIZE name_sz, size_sz;
  1518. int height;
  1519. static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
  1520. static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
  1521. static const WCHAR font_text[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0}; /* a long font name */
  1522. static const WCHAR size_text[] = {' ','0','0',0}; /* enough for two digits */
  1523. CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
  1524. hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
  1525. CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
  1526. CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
  1527. rbi.cbSize = sizeof(rbi);
  1528. rbi.fMask = 0;
  1529. rbi.himl = NULL;
  1530. if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
  1531. return -1;
  1532. hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,
  1533. IDC_TOOLBAR,
  1534. 1, hInstance, IDB_TOOLBAR,
  1535. NULL, 0,
  1536. 24, 24, 16, 16, sizeof(TBBUTTON));
  1537. ab.hInst = HINST_COMMCTRL;
  1538. ab.nID = IDB_STD_SMALL_COLOR;
  1539. nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
  1540. AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
  1541. AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
  1542. AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
  1543. AddSeparator(hToolBarWnd);
  1544. AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
  1545. AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
  1546. AddSeparator(hToolBarWnd);
  1547. AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
  1548. AddSeparator(hToolBarWnd);
  1549. AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
  1550. AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
  1551. AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
  1552. AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
  1553. AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
  1554. AddSeparator(hToolBarWnd);
  1555. AddButton(hToolBarWnd, 0, ID_DATETIME);
  1556. SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
  1557. height = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
  1558. hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
  1559. WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
  1560. 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
  1561. GetWindowRect(hFontListWnd, &rect);
  1562. height = max(height, rect.bottom - rect.top);
  1563. rbb.cbSize = REBARBANDINFOW_V6_SIZE;
  1564. rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
  1565. rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
  1566. rbb.cx = 0;
  1567. rbb.hwndChild = hToolBarWnd;
  1568. rbb.cxMinChild = 0;
  1569. rbb.cyChild = rbb.cyMinChild = height;
  1570. rbb.wID = BANDID_TOOLBAR;
  1571. SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
  1572. font = (HFONT)SendMessageW(hFontListWnd, WM_GETFONT, 0, 0);
  1573. hdc = GetDC(hFontListWnd);
  1574. font = SelectObject(hdc, font);
  1575. GetTextExtentPointW(hdc, font_text, ARRAY_SIZE(font_text) - 1, &name_sz);
  1576. GetTextExtentPointW(hdc, size_text, ARRAY_SIZE(size_text) - 1, &size_sz);
  1577. font = SelectObject(hdc, font);
  1578. ReleaseDC(hFontListWnd, hdc);
  1579. rbb.hwndChild = hFontListWnd;
  1580. rbb.cx = MulDiv(name_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
  1581. rbb.wID = BANDID_FONTLIST;
  1582. SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
  1583. hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
  1584. WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
  1585. 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
  1586. rbb.hwndChild = hSizeListWnd;
  1587. rbb.cx = MulDiv(size_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
  1588. rbb.fStyle ^= RBBS_BREAK;
  1589. rbb.wID = BANDID_SIZELIST;
  1590. SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
  1591. hFormatBarWnd = CreateToolbarEx(hReBarWnd,
  1592. CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS,
  1593. IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
  1594. SendMessageW(hFormatBarWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
  1595. AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
  1596. AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
  1597. AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
  1598. AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
  1599. AddSeparator(hFormatBarWnd);
  1600. AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
  1601. AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
  1602. AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
  1603. AddSeparator(hFormatBarWnd);
  1604. AddButtonStyle(hFormatBarWnd, 7, ID_BULLETONOFF, BTNS_DROPDOWN);
  1605. SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
  1606. rbb.hwndChild = hFormatBarWnd;
  1607. rbb.wID = BANDID_FORMATBAR;
  1608. SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
  1609. hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
  1610. 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
  1611. rbb.hwndChild = hRulerWnd;
  1612. rbb.wID = BANDID_RULER;
  1613. rbb.fStyle |= RBBS_BREAK;
  1614. SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
  1615. hDLL = LoadLibraryW(wszRichEditDll);
  1616. if(!hDLL)
  1617. {
  1618. MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
  1619. MB_OK | MB_ICONEXCLAMATION);
  1620. PostQuitMessage(1);
  1621. }
  1622. hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
  1623. WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
  1624. |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
  1625. 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
  1626. if (!hEditorWnd)
  1627. {
  1628. fprintf(stderr, "Error code %u\n", GetLastError());
  1629. return -1;
  1630. }
  1631. assert(hEditorWnd);
  1632. setup_richedit_olecallback(hEditorWnd);
  1633. SetFocus(hEditorWnd);
  1634. SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
  1635. set_default_font();
  1636. populate_font_list(hFontListWnd);
  1637. populate_size_list(hSizeListWnd);
  1638. DoLoadStrings();
  1639. SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
  1640. ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
  1641. registry_read_filelist(hWnd);
  1642. registry_read_formatopts_all(barState, wordWrap);
  1643. registry_read_options();
  1644. DragAcceptFiles(hWnd, TRUE);
  1645. return 0;
  1646. }
  1647. static LRESULT OnUser( HWND hWnd )
  1648. {
  1649. HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
  1650. HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
  1651. HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
  1652. HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
  1653. int from, to;
  1654. CHARFORMAT2W fmt;
  1655. PARAFORMAT2 pf;
  1656. GETTEXTLENGTHEX gt;
  1657. ZeroMemory(&fmt, sizeof(fmt));
  1658. fmt.cbSize = sizeof(fmt);
  1659. ZeroMemory(&pf, sizeof(pf));
  1660. pf.cbSize = sizeof(pf);
  1661. gt.flags = GTL_NUMCHARS;
  1662. gt.codepage = 1200;
  1663. SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
  1664. SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
  1665. SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
  1666. SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
  1667. SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
  1668. SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
  1669. SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
  1670. SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
  1671. SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
  1672. SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
  1673. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
  1674. (fmt.dwEffects & CFE_BOLD));
  1675. SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
  1676. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
  1677. (fmt.dwEffects & CFE_ITALIC));
  1678. SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
  1679. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
  1680. (fmt.dwEffects & CFE_UNDERLINE));
  1681. SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
  1682. SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  1683. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
  1684. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
  1685. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
  1686. SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLETONOFF, pf.wNumbering != 0);
  1687. return 0;
  1688. }
  1689. static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
  1690. {
  1691. HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
  1692. HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
  1693. NMHDR *pHdr = (NMHDR *)lParam;
  1694. HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
  1695. HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
  1696. HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
  1697. if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
  1698. {
  1699. if (pHdr->code == CBEN_ENDEDITW)
  1700. {
  1701. NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
  1702. if(pHdr->hwndFrom == hwndFontList)
  1703. {
  1704. on_fontlist_modified(endEdit->szText);
  1705. } else if (pHdr->hwndFrom == hwndSizeList)
  1706. {
  1707. on_sizelist_modified(hwndSizeList,endEdit->szText);
  1708. }
  1709. }
  1710. return 0;
  1711. }
  1712. if (pHdr->hwndFrom == hwndFormatBar)
  1713. {
  1714. if (pHdr->code == TBN_DROPDOWN)
  1715. {
  1716. NMTOOLBARW *tb_notify = (NMTOOLBARW *)lParam;
  1717. HMENU menu = GetMenu( hWnd );
  1718. MENUITEMINFOW info;
  1719. TPMPARAMS params;
  1720. RECT rc;
  1721. if (!menu) return 0;
  1722. info.cbSize = sizeof(info);
  1723. info.fMask = MIIM_SUBMENU;
  1724. GetMenuItemInfoW( menu, ID_LISTMENU, FALSE, &info );
  1725. if (!info.hSubMenu) return 0;
  1726. SendMessageW( tb_notify->hdr.hwndFrom, TB_GETRECT, (WPARAM)tb_notify->iItem, (LPARAM)&rc );
  1727. MapWindowPoints( tb_notify->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2 );
  1728. params.cbSize = sizeof(params);
  1729. params.rcExclude = rc;
  1730. TrackPopupMenuEx( info.hSubMenu,
  1731. TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
  1732. rc.left, rc.bottom, hWnd, &params );
  1733. }
  1734. return 0;
  1735. }
  1736. if (pHdr->hwndFrom == hwndEditor)
  1737. {
  1738. if (pHdr->code == EN_SELCHANGE)
  1739. {
  1740. SELCHANGE *pSC = (SELCHANGE *)lParam;
  1741. char buf[128];
  1742. update_font_list();
  1743. sprintf( buf,"selection = %d..%d, line count=%ld",
  1744. pSC->chrg.cpMin, pSC->chrg.cpMax,
  1745. SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
  1746. SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
  1747. SendMessageW(hWnd, WM_USER, 0, 0);
  1748. return 1;
  1749. }
  1750. }
  1751. return 0;
  1752. }
  1753. /* Copied from dlls/comdlg32/fontdlg.c */
  1754. static const COLORREF textcolors[]=
  1755. {
  1756. 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
  1757. 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
  1758. 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
  1759. 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
  1760. };
  1761. static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
  1762. {
  1763. HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
  1764. static FINDREPLACEW findreplace;
  1765. if ((HWND)lParam == hwndEditor)
  1766. return 0;
  1767. switch(LOWORD(wParam))
  1768. {
  1769. case ID_FILE_EXIT:
  1770. PostMessageW(hWnd, WM_CLOSE, 0, 0);
  1771. break;
  1772. case ID_FILE_NEW:
  1773. {
  1774. HINSTANCE hInstance = GetModuleHandleW(0);
  1775. int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
  1776. if(ret != ID_NEWFILE_ABORT)
  1777. {
  1778. if(prompt_save_changes())
  1779. {
  1780. SETTEXTEX st;
  1781. set_caption(NULL);
  1782. wszFileName[0] = '\0';
  1783. clear_formatting();
  1784. st.flags = ST_DEFAULT;
  1785. st.codepage = 1200;
  1786. SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
  1787. SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
  1788. set_fileformat(ret);
  1789. update_font_list();
  1790. }
  1791. }
  1792. }
  1793. break;
  1794. case ID_FILE_OPEN:
  1795. DialogOpenFile();
  1796. break;
  1797. case ID_FILE_SAVE:
  1798. if(wszFileName[0])
  1799. {
  1800. DoSaveFile(wszFileName, fileFormat);
  1801. break;
  1802. }
  1803. /* Fall through */
  1804. case ID_FILE_SAVEAS:
  1805. DialogSaveFile();
  1806. break;
  1807. case ID_FILE_RECENT1:
  1808. case ID_FILE_RECENT2:
  1809. case ID_FILE_RECENT3:
  1810. case ID_FILE_RECENT4:
  1811. {
  1812. HMENU hMenu = GetMenu(hWnd);
  1813. MENUITEMINFOW mi;
  1814. mi.cbSize = sizeof(MENUITEMINFOW);
  1815. mi.fMask = MIIM_DATA;
  1816. if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
  1817. DoOpenFile((LPWSTR)mi.dwItemData);
  1818. }
  1819. break;
  1820. case ID_FIND:
  1821. dialog_find(&findreplace, FALSE);
  1822. break;
  1823. case ID_FIND_NEXT:
  1824. handle_findmsg(&findreplace);
  1825. break;
  1826. case ID_REPLACE:
  1827. dialog_find(&findreplace, TRUE);
  1828. break;
  1829. case ID_FONTSETTINGS:
  1830. dialog_choose_font();
  1831. break;
  1832. case ID_PRINT:
  1833. dialog_print(hWnd, wszFileName);
  1834. target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
  1835. break;
  1836. case ID_PRINT_QUICK:
  1837. print_quick(hMainWnd, wszFileName);
  1838. target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
  1839. break;
  1840. case ID_PREVIEW:
  1841. {
  1842. int index = reg_formatindex(fileFormat);
  1843. DWORD tmp = barState[index];
  1844. barState[index] = 1 << BANDID_STATUSBAR;
  1845. set_bar_states();
  1846. barState[index] = tmp;
  1847. ShowWindow(hEditorWnd, FALSE);
  1848. init_preview(hWnd, wszFileName);
  1849. SetMenu(hWnd, NULL);
  1850. InvalidateRect(0, 0, TRUE);
  1851. }
  1852. break;
  1853. case ID_PRINTSETUP:
  1854. dialog_printsetup(hWnd);
  1855. target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
  1856. break;
  1857. case ID_FORMAT_BOLD:
  1858. case ID_FORMAT_ITALIC:
  1859. case ID_FORMAT_UNDERLINE:
  1860. {
  1861. CHARFORMAT2W fmt;
  1862. int effects = CFE_BOLD;
  1863. ZeroMemory(&fmt, sizeof(fmt));
  1864. fmt.cbSize = sizeof(fmt);
  1865. SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  1866. fmt.dwMask = CFM_BOLD;
  1867. if (LOWORD(wParam) == ID_FORMAT_ITALIC)
  1868. {
  1869. effects = CFE_ITALIC;
  1870. fmt.dwMask = CFM_ITALIC;
  1871. } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
  1872. {
  1873. effects = CFE_UNDERLINE;
  1874. fmt.dwMask = CFM_UNDERLINE;
  1875. }
  1876. fmt.dwEffects ^= effects;
  1877. SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  1878. break;
  1879. }
  1880. case ID_FORMAT_COLOR:
  1881. {
  1882. HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
  1883. HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
  1884. HMENU hPop;
  1885. RECT itemrc;
  1886. POINT pt;
  1887. int mid;
  1888. int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
  1889. SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
  1890. pt.x = itemrc.left;
  1891. pt.y = itemrc.bottom;
  1892. ClientToScreen(hFormatBarWnd, &pt);
  1893. hPop = GetSubMenu(hColorPopupMenu, 0);
  1894. mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
  1895. TPM_RETURNCMD | TPM_NONOTIFY,
  1896. pt.x, pt.y, 0, hWnd, 0);
  1897. if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
  1898. {
  1899. CHARFORMAT2W fmt;
  1900. ZeroMemory(&fmt, sizeof(fmt));
  1901. fmt.cbSize = sizeof(fmt);
  1902. SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  1903. fmt.dwMask = CFM_COLOR;
  1904. if (mid < ID_COLOR_AUTOMATIC) {
  1905. fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
  1906. fmt.dwEffects &= ~CFE_AUTOCOLOR;
  1907. } else {
  1908. fmt.dwEffects |= CFE_AUTOCOLOR;
  1909. }
  1910. SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
  1911. }
  1912. break;
  1913. }
  1914. case ID_EDIT_CUT:
  1915. PostMessageW(hwndEditor, WM_CUT, 0, 0);
  1916. break;
  1917. case ID_EDIT_COPY:
  1918. PostMessageW(hwndEditor, WM_COPY, 0, 0);
  1919. break;
  1920. case ID_EDIT_PASTE:
  1921. PostMessageW(hwndEditor, WM_PASTE, 0, 0);
  1922. break;
  1923. case ID_EDIT_CLEAR:
  1924. PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
  1925. break;
  1926. case ID_EDIT_SELECTALL:
  1927. {
  1928. CHARRANGE range = {0, -1};
  1929. SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
  1930. /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
  1931. return 0;
  1932. }
  1933. case ID_EDIT_GETTEXT:
  1934. {
  1935. int nLen = GetWindowTextLengthW(hwndEditor);
  1936. LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
  1937. TEXTRANGEW tr;
  1938. GetWindowTextW(hwndEditor, data, nLen+1);
  1939. MessageBoxW(NULL, data, wszAppTitle, MB_OK);
  1940. HeapFree( GetProcessHeap(), 0, data);
  1941. data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
  1942. tr.chrg.cpMin = 0;
  1943. tr.chrg.cpMax = nLen;
  1944. tr.lpstrText = data;
  1945. SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
  1946. MessageBoxW(NULL, data, wszAppTitle, MB_OK);
  1947. HeapFree( GetProcessHeap(), 0, data );
  1948. /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
  1949. return 0;
  1950. }
  1951. case ID_EDIT_CHARFORMAT:
  1952. case ID_EDIT_DEFCHARFORMAT:
  1953. {
  1954. CHARFORMAT2W cf;
  1955. ZeroMemory(&cf, sizeof(cf));
  1956. cf.cbSize = sizeof(cf);
  1957. cf.dwMask = 0;
  1958. SendMessageW(hwndEditor, EM_GETCHARFORMAT,
  1959. LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
  1960. return 0;
  1961. }
  1962. case ID_EDIT_PARAFORMAT:
  1963. {
  1964. PARAFORMAT2 pf;
  1965. ZeroMemory(&pf, sizeof(pf));
  1966. pf.cbSize = sizeof(pf);
  1967. SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  1968. return 0;
  1969. }
  1970. case ID_EDIT_SELECTIONINFO:
  1971. {
  1972. CHARRANGE range = {0, -1};
  1973. char buf[128];
  1974. WCHAR *data = NULL;
  1975. SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
  1976. data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
  1977. SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
  1978. sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
  1979. MessageBoxA(hWnd, buf, "Editor", MB_OK);
  1980. MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
  1981. HeapFree( GetProcessHeap(), 0, data);
  1982. /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
  1983. return 0;
  1984. }
  1985. case ID_EDIT_READONLY:
  1986. {
  1987. LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
  1988. if (nStyle & ES_READONLY)
  1989. SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
  1990. else
  1991. SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
  1992. return 0;
  1993. }
  1994. case ID_EDIT_MODIFIED:
  1995. if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
  1996. SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
  1997. else
  1998. SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
  1999. return 0;
  2000. case ID_EDIT_UNDO:
  2001. SendMessageW(hwndEditor, EM_UNDO, 0, 0);
  2002. return 0;
  2003. case ID_EDIT_REDO:
  2004. SendMessageW(hwndEditor, EM_REDO, 0, 0);
  2005. return 0;
  2006. case ID_BULLETONOFF:
  2007. case ID_BULLET:
  2008. case ID_NUMBERING:
  2009. case ID_LCLETTER:
  2010. case ID_UCLETTER:
  2011. case ID_LCROMAN:
  2012. case ID_UCROMAN:
  2013. {
  2014. PARAFORMAT2 pf;
  2015. WORD new_number = LOWORD(wParam) - ID_BULLET + PFN_BULLET;
  2016. pf.cbSize = sizeof(pf);
  2017. pf.dwMask = PFM_NUMBERING;
  2018. SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  2019. pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB | PFM_OFFSET | PFM_OFFSETINDENT;
  2020. if(pf.wNumbering && ((pf.wNumbering == new_number) || (LOWORD(wParam) == ID_BULLETONOFF)))
  2021. {
  2022. pf.wNumbering = 0;
  2023. pf.wNumberingStart = 0;
  2024. pf.wNumberingStyle = 0;
  2025. pf.wNumberingTab = 0;
  2026. pf.dxOffset = 0;
  2027. pf.dxStartIndent = -360;
  2028. } else
  2029. {
  2030. pf.dxStartIndent = pf.wNumbering ? 0 : 360;
  2031. if (LOWORD(wParam) == ID_BULLETONOFF)
  2032. pf.wNumbering = last_bullet;
  2033. else
  2034. {
  2035. pf.wNumbering = new_number;
  2036. last_bullet = pf.wNumbering;
  2037. }
  2038. pf.wNumberingStart = 1;
  2039. pf.wNumberingStyle = PFNS_PERIOD;
  2040. pf.wNumberingTab = 360;
  2041. pf.dxOffset = 360;
  2042. }
  2043. SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
  2044. }
  2045. break;
  2046. case ID_ALIGN_LEFT:
  2047. case ID_ALIGN_CENTER:
  2048. case ID_ALIGN_RIGHT:
  2049. {
  2050. PARAFORMAT2 pf;
  2051. pf.cbSize = sizeof(pf);
  2052. pf.dwMask = PFM_ALIGNMENT;
  2053. switch(LOWORD(wParam)) {
  2054. case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
  2055. case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
  2056. case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
  2057. }
  2058. SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
  2059. break;
  2060. }
  2061. case ID_BACK_1:
  2062. SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
  2063. break;
  2064. case ID_BACK_2:
  2065. SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
  2066. break;
  2067. case ID_TOGGLE_TOOLBAR:
  2068. set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
  2069. update_window();
  2070. break;
  2071. case ID_TOGGLE_FORMATBAR:
  2072. set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
  2073. set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
  2074. set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
  2075. update_window();
  2076. break;
  2077. case ID_TOGGLE_STATUSBAR:
  2078. set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
  2079. update_window();
  2080. break;
  2081. case ID_TOGGLE_RULER:
  2082. set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
  2083. update_window();
  2084. break;
  2085. case ID_DATETIME:
  2086. DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
  2087. break;
  2088. case ID_PARAFORMAT:
  2089. DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
  2090. break;
  2091. case ID_TABSTOPS:
  2092. DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
  2093. break;
  2094. case ID_ABOUT:
  2095. dialog_about();
  2096. break;
  2097. case ID_VIEWPROPERTIES:
  2098. dialog_viewproperties();
  2099. break;
  2100. case IDC_FONTLIST:
  2101. if (HIWORD(wParam) == CBN_SELENDOK)
  2102. {
  2103. WCHAR buffer[LF_FACESIZE];
  2104. HWND hwndFontList = (HWND)lParam;
  2105. get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
  2106. on_fontlist_modified(buffer);
  2107. }
  2108. break;
  2109. case IDC_SIZELIST:
  2110. if (HIWORD(wParam) == CBN_SELENDOK)
  2111. {
  2112. WCHAR buffer[MAX_STRING_LEN+1];
  2113. HWND hwndSizeList = (HWND)lParam;
  2114. get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
  2115. on_sizelist_modified(hwndSizeList, buffer);
  2116. }
  2117. break;
  2118. default:
  2119. SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
  2120. break;
  2121. }
  2122. return 0;
  2123. }
  2124. static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
  2125. {
  2126. HMENU hMenu = (HMENU)wParam;
  2127. HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
  2128. HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
  2129. PARAFORMAT pf;
  2130. int nAlignment = -1;
  2131. int selFrom, selTo;
  2132. GETTEXTLENGTHEX gt;
  2133. LRESULT textLength;
  2134. MENUITEMINFOW mi;
  2135. SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
  2136. EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
  2137. EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
  2138. pf.cbSize = sizeof(PARAFORMAT);
  2139. SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
  2140. CheckMenuItem(hMenu, ID_EDIT_READONLY,
  2141. (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
  2142. CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
  2143. SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
  2144. if (pf.dwMask & PFM_ALIGNMENT)
  2145. nAlignment = pf.wAlignment;
  2146. CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
  2147. CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
  2148. CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
  2149. CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
  2150. CheckMenuItem(hMenu, ID_NUMBERING, ((pf.wNumbering == PFN_ARABIC) ? MF_CHECKED : MF_UNCHECKED));
  2151. CheckMenuItem(hMenu, ID_LCLETTER, ((pf.wNumbering == PFN_LCLETTER) ? MF_CHECKED : MF_UNCHECKED));
  2152. CheckMenuItem(hMenu, ID_UCLETTER, ((pf.wNumbering == PFN_UCLETTER) ? MF_CHECKED : MF_UNCHECKED));
  2153. CheckMenuItem(hMenu, ID_LCROMAN, ((pf.wNumbering == PFN_LCROMAN) ? MF_CHECKED : MF_UNCHECKED));
  2154. CheckMenuItem(hMenu, ID_UCROMAN, ((pf.wNumbering == PFN_UCROMAN) ? MF_CHECKED : MF_UNCHECKED));
  2155. EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
  2156. MF_ENABLED : MF_GRAYED);
  2157. EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
  2158. MF_ENABLED : MF_GRAYED);
  2159. CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
  2160. MF_CHECKED : MF_UNCHECKED);
  2161. CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
  2162. MF_CHECKED : MF_UNCHECKED);
  2163. CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
  2164. MF_CHECKED : MF_UNCHECKED);
  2165. CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
  2166. gt.flags = GTL_NUMCHARS;
  2167. gt.codepage = 1200;
  2168. textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
  2169. EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
  2170. mi.cbSize = sizeof(mi);
  2171. mi.fMask = MIIM_DATA;
  2172. GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
  2173. EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
  2174. EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
  2175. return 0;
  2176. }
  2177. static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
  2178. {
  2179. int nStatusSize = 0;
  2180. RECT rc;
  2181. HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
  2182. HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
  2183. HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
  2184. HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
  2185. int rebarHeight = 0;
  2186. if (hwndStatusBar)
  2187. {
  2188. SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
  2189. if (IsWindowVisible(hwndStatusBar))
  2190. {
  2191. GetClientRect(hwndStatusBar, &rc);
  2192. nStatusSize = rc.bottom - rc.top;
  2193. } else
  2194. {
  2195. nStatusSize = 0;
  2196. }
  2197. }
  2198. if (hwndReBar)
  2199. {
  2200. rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
  2201. MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
  2202. }
  2203. if (hwndEditor)
  2204. {
  2205. GetClientRect(hWnd, &rc);
  2206. MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
  2207. }
  2208. redraw_ruler(hRulerWnd);
  2209. return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
  2210. }
  2211. static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  2212. {
  2213. if(msg == ID_FINDMSGSTRING)
  2214. return handle_findmsg((LPFINDREPLACEW)lParam);
  2215. switch(msg)
  2216. {
  2217. case WM_CREATE:
  2218. return OnCreate( hWnd );
  2219. case WM_USER:
  2220. return OnUser( hWnd );
  2221. case WM_NOTIFY:
  2222. return OnNotify( hWnd, lParam );
  2223. case WM_COMMAND:
  2224. if(preview_isactive())
  2225. {
  2226. return preview_command( hWnd, wParam );
  2227. }
  2228. return OnCommand( hWnd, wParam, lParam );
  2229. case WM_DESTROY:
  2230. PostQuitMessage(0);
  2231. break;
  2232. case WM_CLOSE:
  2233. if(preview_isactive())
  2234. {
  2235. preview_exit(hWnd);
  2236. } else if(prompt_save_changes())
  2237. {
  2238. registry_set_options(hMainWnd);
  2239. registry_set_formatopts_all(barState, wordWrap);
  2240. PostQuitMessage(0);
  2241. }
  2242. break;
  2243. case WM_ACTIVATE:
  2244. if (LOWORD(wParam))
  2245. SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
  2246. return 0;
  2247. case WM_INITMENUPOPUP:
  2248. return OnInitPopupMenu( hWnd, wParam );
  2249. case WM_SIZE:
  2250. return OnSize( hWnd, wParam, lParam );
  2251. case WM_CONTEXTMENU:
  2252. return DefWindowProcW(hWnd, msg, wParam, lParam);
  2253. case WM_DROPFILES:
  2254. {
  2255. WCHAR file[MAX_PATH];
  2256. DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
  2257. DragFinish((HDROP)wParam);
  2258. if(prompt_save_changes())
  2259. DoOpenFile(file);
  2260. }
  2261. break;
  2262. case WM_PAINT:
  2263. if(!preview_isactive())
  2264. return DefWindowProcW(hWnd, msg, wParam, lParam);
  2265. default:
  2266. return DefWindowProcW(hWnd, msg, wParam, lParam);
  2267. }
  2268. return 0;
  2269. }
  2270. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
  2271. {
  2272. INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
  2273. HACCEL hAccel;
  2274. WNDCLASSEXW wc;
  2275. MSG msg;
  2276. RECT rc;
  2277. UINT_PTR hPrevRulerProc;
  2278. HWND hRulerWnd;
  2279. POINTL EditPoint;
  2280. DWORD bMaximized;
  2281. MONITORINFO info;
  2282. HMONITOR monitor;
  2283. int x, y;
  2284. static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
  2285. 'T','A','B','L','E','\0'};
  2286. InitCommonControlsEx(&classes);
  2287. hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
  2288. wc.cbSize = sizeof(wc);
  2289. wc.style = 0;
  2290. wc.lpfnWndProc = WndProc;
  2291. wc.cbClsExtra = 0;
  2292. wc.cbWndExtra = 4;
  2293. wc.hInstance = hInstance;
  2294. wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
  2295. wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
  2296. GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
  2297. wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
  2298. wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
  2299. wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
  2300. wc.lpszClassName = wszMainWndClass;
  2301. RegisterClassExW(&wc);
  2302. wc.style = 0;
  2303. wc.lpfnWndProc = preview_proc;
  2304. wc.cbClsExtra = 0;
  2305. wc.cbWndExtra = 0;
  2306. wc.hInstance = hInstance;
  2307. wc.hIcon = NULL;
  2308. wc.hIconSm = NULL;
  2309. wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
  2310. wc.hbrBackground = NULL;
  2311. wc.lpszMenuName = NULL;
  2312. wc.lpszClassName = wszPreviewWndClass;
  2313. RegisterClassExW(&wc);
  2314. registry_read_winrect(&rc);
  2315. monitor = MonitorFromRect(&rc, MONITOR_DEFAULTTOPRIMARY);
  2316. info.cbSize = sizeof(info);
  2317. GetMonitorInfoW(monitor, &info);
  2318. x = rc.left;
  2319. y = rc.top;
  2320. IntersectRect(&info.rcWork, &info.rcWork, &rc);
  2321. if (IsRectEmpty(&info.rcWork))
  2322. x = y = CW_USEDEFAULT;
  2323. hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
  2324. x, y, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
  2325. registry_read_maximized(&bMaximized);
  2326. if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
  2327. && bMaximized)
  2328. nCmdShow = SW_SHOWMAXIMIZED;
  2329. ShowWindow(hMainWnd, nCmdShow);
  2330. set_caption(NULL);
  2331. set_bar_states();
  2332. set_fileformat(SF_RTF);
  2333. hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
  2334. get_default_printer_opts();
  2335. target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
  2336. hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
  2337. SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
  2338. hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
  2339. SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
  2340. HandleCommandLine(GetCommandLineW());
  2341. while(GetMessageW(&msg,0,0,0))
  2342. {
  2343. if (IsDialogMessageW(hFindWnd, &msg))
  2344. continue;
  2345. if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
  2346. continue;
  2347. TranslateMessage(&msg);
  2348. DispatchMessageW(&msg);
  2349. if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
  2350. SendMessageW(hMainWnd, WM_USER, 0, 0);
  2351. }
  2352. return 0;
  2353. }