wordpad.c 92 KB

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