UIRichEdit.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. #include "stdafx.h"
  2. // These constants are for backward compatibility. They are the
  3. // sizes used for initialization and reset in RichEdit 1.0
  4. namespace DuiLib {
  5. const LONG cInitTextMax = (32 * 1024) - 1;
  6. EXTERN_C const IID IID_ITextServices = { // 8d33f740-cf58-11ce-a89d-00aa006cadc5
  7. 0x8d33f740,
  8. 0xcf58,
  9. 0x11ce,
  10. {0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
  11. };
  12. EXTERN_C const IID IID_ITextHost = { /* c5bdd8d0-d26e-11ce-a89e-00aa006cadc5 */
  13. 0xc5bdd8d0,
  14. 0xd26e,
  15. 0x11ce,
  16. {0xa8, 0x9e, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
  17. };
  18. #ifndef LY_PER_INCH
  19. #define LY_PER_INCH 1440
  20. #endif
  21. #ifndef HIMETRIC_PER_INCH
  22. #define HIMETRIC_PER_INCH 2540
  23. #endif
  24. #include <textserv.h>
  25. class CTxtWinHost : public ITextHost
  26. {
  27. public:
  28. CTxtWinHost();
  29. BOOL Init(CRichEditUI *re , const CREATESTRUCT *pcs);
  30. virtual ~CTxtWinHost();
  31. ITextServices* GetTextServices(void) { return pserv; }
  32. void SetClientRect(RECT *prc);
  33. RECT* GetClientRect() { return &rcClient; }
  34. BOOL GetWordWrap(void) { return fWordWrap; }
  35. void SetWordWrap(BOOL fWordWrap);
  36. BOOL GetReadOnly();
  37. void SetReadOnly(BOOL fReadOnly);
  38. void SetFont(HFONT hFont);
  39. void SetColor(DWORD dwColor);
  40. SIZEL* GetExtent();
  41. void SetExtent(SIZEL *psizelExtent);
  42. void LimitText(LONG nChars);
  43. BOOL IsCaptured();
  44. BOOL IsShowCaret();
  45. void NeedFreshCaret();
  46. INT GetCaretWidth();
  47. INT GetCaretHeight();
  48. BOOL GetAllowBeep();
  49. void SetAllowBeep(BOOL fAllowBeep);
  50. WORD GetDefaultAlign();
  51. void SetDefaultAlign(WORD wNewAlign);
  52. BOOL GetRichTextFlag();
  53. void SetRichTextFlag(BOOL fNew);
  54. LONG GetDefaultLeftIndent();
  55. void SetDefaultLeftIndent(LONG lNewIndent);
  56. BOOL SetSaveSelection(BOOL fSaveSelection);
  57. HRESULT OnTxInPlaceDeactivate();
  58. HRESULT OnTxInPlaceActivate(LPCRECT prcClient);
  59. BOOL GetActiveState(void) { return fInplaceActive; }
  60. BOOL DoSetCursor(RECT *prc, POINT *pt);
  61. void SetTransparent(BOOL fTransparent);
  62. void GetControlRect(LPRECT prc);
  63. LONG SetAccelPos(LONG laccelpos);
  64. WCHAR SetPasswordChar(WCHAR chPasswordChar);
  65. void SetDisabled(BOOL fOn);
  66. LONG SetSelBarWidth(LONG lSelBarWidth);
  67. BOOL GetTimerState();
  68. void SetCharFormat(CHARFORMAT2W &c);
  69. void SetParaFormat(PARAFORMAT2 &p);
  70. // -----------------------------
  71. // IUnknown interface
  72. // -----------------------------
  73. virtual HRESULT _stdcall QueryInterface(REFIID riid, void **ppvObject);
  74. virtual ULONG _stdcall AddRef(void);
  75. virtual ULONG _stdcall Release(void);
  76. // -----------------------------
  77. // ITextHost interface
  78. // -----------------------------
  79. virtual HDC TxGetDC();
  80. virtual INT TxReleaseDC(HDC hdc);
  81. virtual BOOL TxShowScrollBar(INT fnBar, BOOL fShow);
  82. virtual BOOL TxEnableScrollBar (INT fuSBFlags, INT fuArrowflags);
  83. virtual BOOL TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw);
  84. virtual BOOL TxSetScrollPos (INT fnBar, INT nPos, BOOL fRedraw);
  85. virtual void TxInvalidateRect(LPCRECT prc, BOOL fMode);
  86. virtual void TxViewChange(BOOL fUpdate);
  87. virtual BOOL TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight);
  88. virtual BOOL TxShowCaret(BOOL fShow);
  89. virtual BOOL TxSetCaretPos(INT x, INT y);
  90. virtual BOOL TxSetTimer(UINT idTimer, UINT uTimeout);
  91. virtual void TxKillTimer(UINT idTimer);
  92. virtual void TxScrollWindowEx (INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll);
  93. virtual void TxSetCapture(BOOL fCapture);
  94. virtual void TxSetFocus();
  95. virtual void TxSetCursor(HCURSOR hcur, BOOL fText);
  96. virtual BOOL TxScreenToClient (LPPOINT lppt);
  97. virtual BOOL TxClientToScreen (LPPOINT lppt);
  98. virtual HRESULT TxActivate( LONG * plOldState );
  99. virtual HRESULT TxDeactivate( LONG lNewState );
  100. virtual HRESULT TxGetClientRect(LPRECT prc);
  101. virtual HRESULT TxGetViewInset(LPRECT prc);
  102. virtual HRESULT TxGetCharFormat(const CHARFORMATW **ppCF );
  103. virtual HRESULT TxGetParaFormat(const PARAFORMAT **ppPF);
  104. virtual COLORREF TxGetSysColor(int nIndex);
  105. virtual HRESULT TxGetBackStyle(TXTBACKSTYLE *pstyle);
  106. virtual HRESULT TxGetMaxLength(DWORD *plength);
  107. virtual HRESULT TxGetScrollBars(DWORD *pdwScrollBar);
  108. virtual HRESULT TxGetPasswordChar(TCHAR *pch);
  109. virtual HRESULT TxGetAcceleratorPos(LONG *pcp);
  110. virtual HRESULT TxGetExtent(LPSIZEL lpExtent);
  111. virtual HRESULT OnTxCharFormatChange (const CHARFORMATW * pcf);
  112. virtual HRESULT OnTxParaFormatChange (const PARAFORMAT * ppf);
  113. virtual HRESULT TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits);
  114. virtual HRESULT TxNotify(DWORD iNotify, void *pv);
  115. virtual HIMC TxImmGetContext(void);
  116. virtual void TxImmReleaseContext(HIMC himc);
  117. virtual HRESULT TxGetSelectionBarWidth (LONG *lSelBarWidth);
  118. private:
  119. CRichEditUI *m_re;
  120. ULONG cRefs; // Reference Count
  121. ITextServices *pserv; // pointer to Text Services object
  122. // Properties
  123. DWORD dwStyle; // style bits
  124. unsigned fEnableAutoWordSel :1; // enable Word style auto word selection?
  125. unsigned fWordWrap :1; // Whether control should word wrap
  126. unsigned fAllowBeep :1; // Whether beep is allowed
  127. unsigned fRich :1; // Whether control is rich text
  128. unsigned fSaveSelection :1; // Whether to save the selection when inactive
  129. unsigned fInplaceActive :1; // Whether control is inplace active
  130. unsigned fTransparent :1; // Whether control is transparent
  131. unsigned fTimer :1; // A timer is set
  132. unsigned fCaptured :1;
  133. unsigned fShowCaret :1;
  134. unsigned fNeedFreshCaret :1; // 修正改变大小后点击其他位置原来光标不能消除的问题
  135. INT iCaretWidth;
  136. INT iCaretHeight;
  137. INT iCaretLastWidth;
  138. INT iCaretLastHeight;
  139. LONG lSelBarWidth; // Width of the selection bar
  140. LONG cchTextMost; // maximum text size
  141. DWORD dwEventMask; // DoEvent mask to pass on to parent window
  142. LONG icf;
  143. LONG ipf;
  144. RECT rcClient; // Client Rect for this control
  145. SIZEL sizelExtent; // Extent array
  146. CHARFORMAT2W cf; // Default character format
  147. PARAFORMAT2 pf; // Default paragraph format
  148. LONG laccelpos; // Accelerator position
  149. WCHAR chPasswordChar; // Password character
  150. };
  151. // Convert Pixels on the X axis to Himetric
  152. LONG DXtoHimetricX(LONG dx, LONG xPerInch)
  153. {
  154. return (LONG) MulDiv(dx, HIMETRIC_PER_INCH, xPerInch);
  155. }
  156. // Convert Pixels on the Y axis to Himetric
  157. LONG DYtoHimetricY(LONG dy, LONG yPerInch)
  158. {
  159. return (LONG) MulDiv(dy, HIMETRIC_PER_INCH, yPerInch);
  160. }
  161. HRESULT InitDefaultCharFormat(CRichEditUI* re, CHARFORMAT2W* pcf, HFONT hfont)
  162. {
  163. memset(pcf, 0, sizeof(CHARFORMAT2W));
  164. LOGFONT lf;
  165. if( !hfont )
  166. hfont = re->GetManager()->GetFont(re->GetFont());
  167. ::GetObject(hfont, sizeof(LOGFONT), &lf);
  168. DWORD dwColor = re->GetTextColor();
  169. pcf->cbSize = sizeof(CHARFORMAT2W);
  170. pcf->crTextColor = RGB(GetBValue(dwColor), GetGValue(dwColor), GetRValue(dwColor));
  171. LONG yPixPerInch = GetDeviceCaps(re->GetManager()->GetPaintDC(), LOGPIXELSY);
  172. pcf->yHeight = -lf.lfHeight * LY_PER_INCH / yPixPerInch;
  173. pcf->yOffset = 0;
  174. pcf->dwEffects = 0;
  175. pcf->dwMask = CFM_SIZE | CFM_OFFSET | CFM_FACE | CFM_CHARSET | CFM_COLOR | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
  176. if(lf.lfWeight >= FW_BOLD)
  177. pcf->dwEffects |= CFE_BOLD;
  178. if(lf.lfItalic)
  179. pcf->dwEffects |= CFE_ITALIC;
  180. if(lf.lfUnderline)
  181. pcf->dwEffects |= CFE_UNDERLINE;
  182. pcf->bCharSet = lf.lfCharSet;
  183. pcf->bPitchAndFamily = lf.lfPitchAndFamily;
  184. #ifdef _UNICODE
  185. _tcscpy(pcf->szFaceName, lf.lfFaceName);
  186. #else
  187. //need to thunk pcf->szFaceName to a standard char string.in this case it's easy because our thunk is also our copy
  188. MultiByteToWideChar(CP_ACP, 0, lf.lfFaceName, LF_FACESIZE, pcf->szFaceName, LF_FACESIZE) ;
  189. #endif
  190. return S_OK;
  191. }
  192. HRESULT InitDefaultParaFormat(CRichEditUI* re, PARAFORMAT2* ppf)
  193. {
  194. memset(ppf, 0, sizeof(PARAFORMAT2));
  195. ppf->cbSize = sizeof(PARAFORMAT2);
  196. ppf->dwMask = PFM_ALL;
  197. ppf->wAlignment = PFA_LEFT;
  198. ppf->cTabCount = 1;
  199. ppf->rgxTabs[0] = lDefaultTab;
  200. return S_OK;
  201. }
  202. HRESULT CreateHost(CRichEditUI *re, const CREATESTRUCT *pcs, CTxtWinHost **pptec)
  203. {
  204. HRESULT hr = E_FAIL;
  205. //GdiSetBatchLimit(1);
  206. CTxtWinHost *phost = new CTxtWinHost();
  207. if(phost)
  208. {
  209. if (phost->Init(re, pcs))
  210. {
  211. *pptec = phost;
  212. hr = S_OK;
  213. }
  214. }
  215. if (FAILED(hr))
  216. {
  217. delete phost;
  218. }
  219. return TRUE;
  220. }
  221. CTxtWinHost::CTxtWinHost() : m_re(NULL)
  222. {
  223. ::ZeroMemory(&cRefs, sizeof(CTxtWinHost) - offsetof(CTxtWinHost, cRefs));
  224. cchTextMost = cInitTextMax;
  225. laccelpos = -1;
  226. }
  227. CTxtWinHost::~CTxtWinHost()
  228. {
  229. pserv->OnTxInPlaceDeactivate();
  230. pserv->Release();
  231. }
  232. ////////////////////// Create/Init/Destruct Commands ///////////////////////
  233. BOOL CTxtWinHost::Init(CRichEditUI *re, const CREATESTRUCT *pcs)
  234. {
  235. IUnknown *pUnk;
  236. HRESULT hr;
  237. m_re = re;
  238. // Initialize Reference count
  239. cRefs = 1;
  240. // Create and cache CHARFORMAT for this control
  241. if(FAILED(InitDefaultCharFormat(re, &cf, NULL)))
  242. goto err;
  243. // Create and cache PARAFORMAT for this control
  244. if(FAILED(InitDefaultParaFormat(re, &pf)))
  245. goto err;
  246. // edit controls created without a window are multiline by default
  247. // so that paragraph formats can be
  248. dwStyle = ES_MULTILINE;
  249. // edit controls are rich by default
  250. fRich = re->IsRich();
  251. cchTextMost = re->GetLimitText();
  252. if (pcs )
  253. {
  254. dwStyle = pcs->style;
  255. if ( !(dwStyle & (ES_AUTOHSCROLL | WS_HSCROLL)) )
  256. {
  257. fWordWrap = TRUE;
  258. }
  259. }
  260. if( !(dwStyle & ES_LEFT) )
  261. {
  262. if(dwStyle & ES_CENTER)
  263. pf.wAlignment = PFA_CENTER;
  264. else if(dwStyle & ES_RIGHT)
  265. pf.wAlignment = PFA_RIGHT;
  266. }
  267. fInplaceActive = TRUE;
  268. // Create Text Services component
  269. //if(FAILED(CreateTextServices(NULL, this, &pUnk)))
  270. // goto err;
  271. PCreateTextServices TextServicesProc;
  272. HMODULE hmod = LoadLibrary(_T("msftedit.dll"));
  273. if (hmod)
  274. {
  275. TextServicesProc = (PCreateTextServices)GetProcAddress(hmod,"CreateTextServices");
  276. }
  277. if (TextServicesProc)
  278. {
  279. HRESULT hr = TextServicesProc(NULL, this, &pUnk);
  280. }
  281. hr = pUnk->QueryInterface(IID_ITextServices,(void **)&pserv);
  282. // Whether the previous call succeeded or failed we are done
  283. // with the private interface.
  284. pUnk->Release();
  285. if(FAILED(hr))
  286. {
  287. goto err;
  288. }
  289. // Set window text
  290. if(pcs && pcs->lpszName)
  291. {
  292. #ifdef _UNICODE
  293. if(FAILED(pserv->TxSetText((TCHAR *)pcs->lpszName)))
  294. goto err;
  295. #else
  296. size_t iLen = _tcslen(pcs->lpszName);
  297. LPWSTR lpText = new WCHAR[iLen + 1];
  298. ::ZeroMemory(lpText, (iLen + 1) * sizeof(WCHAR));
  299. ::MultiByteToWideChar(CP_ACP, 0, pcs->lpszName, -1, (LPWSTR)lpText, iLen) ;
  300. if(FAILED(pserv->TxSetText((LPWSTR)lpText))) {
  301. delete[] lpText;
  302. goto err;
  303. }
  304. delete[] lpText;
  305. #endif
  306. }
  307. return TRUE;
  308. err:
  309. return FALSE;
  310. }
  311. ///////////////////////////////// IUnknown ////////////////////////////////
  312. HRESULT CTxtWinHost::QueryInterface(REFIID riid, void **ppvObject)
  313. {
  314. HRESULT hr = E_NOINTERFACE;
  315. *ppvObject = NULL;
  316. if (IsEqualIID(riid, IID_IUnknown)
  317. || IsEqualIID(riid, IID_ITextHost))
  318. {
  319. AddRef();
  320. *ppvObject = (ITextHost *) this;
  321. hr = S_OK;
  322. }
  323. return hr;
  324. }
  325. ULONG CTxtWinHost::AddRef(void)
  326. {
  327. return ++cRefs;
  328. }
  329. ULONG CTxtWinHost::Release(void)
  330. {
  331. ULONG c_Refs = --cRefs;
  332. if (c_Refs == 0)
  333. {
  334. delete this;
  335. }
  336. return c_Refs;
  337. }
  338. ///////////////////////////////// Far East Support //////////////////////////////////////
  339. HIMC CTxtWinHost::TxImmGetContext(void)
  340. {
  341. return NULL;
  342. }
  343. void CTxtWinHost::TxImmReleaseContext(HIMC himc)
  344. {
  345. //::ImmReleaseContext( hwnd, himc );
  346. }
  347. //////////////////////////// ITextHost Interface ////////////////////////////
  348. HDC CTxtWinHost::TxGetDC()
  349. {
  350. return m_re->GetManager()->GetPaintDC();
  351. }
  352. int CTxtWinHost::TxReleaseDC(HDC hdc)
  353. {
  354. return 1;
  355. }
  356. BOOL CTxtWinHost::TxShowScrollBar(INT fnBar, BOOL fShow)
  357. {
  358. CScrollBarUI* pVerticalScrollBar = m_re->GetVerticalScrollBar();
  359. CScrollBarUI* pHorizontalScrollBar = m_re->GetHorizontalScrollBar();
  360. if( fnBar == SB_VERT && pVerticalScrollBar ) {
  361. pVerticalScrollBar->SetVisible(fShow == TRUE);
  362. }
  363. else if( fnBar == SB_HORZ && pHorizontalScrollBar ) {
  364. pHorizontalScrollBar->SetVisible(fShow == TRUE);
  365. }
  366. else if( fnBar == SB_BOTH ) {
  367. if( pVerticalScrollBar ) pVerticalScrollBar->SetVisible(fShow == TRUE);
  368. if( pHorizontalScrollBar ) pHorizontalScrollBar->SetVisible(fShow == TRUE);
  369. }
  370. return TRUE;
  371. }
  372. BOOL CTxtWinHost::TxEnableScrollBar (INT fuSBFlags, INT fuArrowflags)
  373. {
  374. if( fuSBFlags == SB_VERT ) {
  375. m_re->EnableScrollBar(true, m_re->GetHorizontalScrollBar() != NULL);
  376. m_re->GetVerticalScrollBar()->SetVisible(fuArrowflags != ESB_DISABLE_BOTH);
  377. }
  378. else if( fuSBFlags == SB_HORZ ) {
  379. m_re->EnableScrollBar(m_re->GetVerticalScrollBar() != NULL, true);
  380. m_re->GetHorizontalScrollBar()->SetVisible(fuArrowflags != ESB_DISABLE_BOTH);
  381. }
  382. else if( fuSBFlags == SB_BOTH ) {
  383. m_re->EnableScrollBar(true, true);
  384. m_re->GetVerticalScrollBar()->SetVisible(fuArrowflags != ESB_DISABLE_BOTH);
  385. m_re->GetHorizontalScrollBar()->SetVisible(fuArrowflags != ESB_DISABLE_BOTH);
  386. }
  387. return TRUE;
  388. }
  389. BOOL CTxtWinHost::TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw)
  390. {
  391. CScrollBarUI* pVerticalScrollBar = m_re->GetVerticalScrollBar();
  392. CScrollBarUI* pHorizontalScrollBar = m_re->GetHorizontalScrollBar();
  393. if( fnBar == SB_VERT && pVerticalScrollBar ) {
  394. if( nMaxPos - nMinPos - rcClient.bottom + rcClient.top <= 0 ) {
  395. pVerticalScrollBar->SetVisible(false);
  396. }
  397. else {
  398. pVerticalScrollBar->SetVisible(true);
  399. pVerticalScrollBar->SetScrollRange(nMaxPos - nMinPos - rcClient.bottom + rcClient.top);
  400. }
  401. }
  402. else if( fnBar == SB_HORZ && pHorizontalScrollBar ) {
  403. if( nMaxPos - nMinPos - rcClient.right + rcClient.left <= 0 ) {
  404. pHorizontalScrollBar->SetVisible(false);
  405. }
  406. else {
  407. pHorizontalScrollBar->SetVisible(true);
  408. pHorizontalScrollBar->SetScrollRange(nMaxPos - nMinPos - rcClient.right + rcClient.left);
  409. }
  410. }
  411. return TRUE;
  412. }
  413. BOOL CTxtWinHost::TxSetScrollPos (INT fnBar, INT nPos, BOOL fRedraw)
  414. {
  415. CScrollBarUI* pVerticalScrollBar = m_re->GetVerticalScrollBar();
  416. CScrollBarUI* pHorizontalScrollBar = m_re->GetHorizontalScrollBar();
  417. if( fnBar == SB_VERT && pVerticalScrollBar ) {
  418. pVerticalScrollBar->SetScrollPos(nPos);
  419. }
  420. else if( fnBar == SB_HORZ && pHorizontalScrollBar ) {
  421. pHorizontalScrollBar->SetScrollPos(nPos);
  422. }
  423. return TRUE;
  424. }
  425. void CTxtWinHost::TxInvalidateRect(LPCRECT prc, BOOL fMode)
  426. {
  427. if( prc == NULL ) {
  428. m_re->GetManager()->Invalidate(rcClient);
  429. return;
  430. }
  431. RECT rc = *prc;
  432. m_re->GetManager()->Invalidate(rc);
  433. }
  434. void CTxtWinHost::TxViewChange(BOOL fUpdate)
  435. {
  436. if( m_re->OnTxViewChanged() ) m_re->Invalidate();
  437. }
  438. BOOL CTxtWinHost::TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight)
  439. {
  440. iCaretWidth = xWidth;
  441. iCaretHeight = yHeight;
  442. return ::CreateCaret(m_re->GetManager()->GetPaintWindow(), hbmp, xWidth, yHeight);
  443. }
  444. BOOL CTxtWinHost::TxShowCaret(BOOL fShow)
  445. {
  446. fShowCaret = fShow;
  447. if(fShow)
  448. return ::ShowCaret(m_re->GetManager()->GetPaintWindow());
  449. else
  450. return ::HideCaret(m_re->GetManager()->GetPaintWindow());
  451. }
  452. BOOL CTxtWinHost::TxSetCaretPos(INT x, INT y)
  453. {
  454. POINT ptCaret = { 0 };
  455. ::GetCaretPos(&ptCaret);
  456. RECT rcCaret = { ptCaret.x, ptCaret.y, ptCaret.x + iCaretLastWidth, ptCaret.y + iCaretLastHeight };
  457. if( m_re->GetManager()->IsLayered() ) m_re->GetManager()->Invalidate(rcCaret);
  458. else if( fNeedFreshCaret == TRUE ) {
  459. m_re->GetManager()->Invalidate(rcCaret);
  460. fNeedFreshCaret = FALSE;
  461. }
  462. rcCaret.left = x;
  463. rcCaret.top = y;
  464. rcCaret.right = x + iCaretWidth;
  465. rcCaret.bottom = y + iCaretHeight;
  466. if( m_re->GetManager()->IsLayered() ) m_re->GetManager()->Invalidate(rcCaret);
  467. iCaretLastWidth = iCaretWidth;
  468. iCaretLastHeight = iCaretHeight;
  469. return ::SetCaretPos(x, y);
  470. }
  471. BOOL CTxtWinHost::TxSetTimer(UINT idTimer, UINT uTimeout)
  472. {
  473. fTimer = TRUE;
  474. return m_re->GetManager()->SetTimer(m_re, idTimer, uTimeout) == TRUE;
  475. }
  476. void CTxtWinHost::TxKillTimer(UINT idTimer)
  477. {
  478. m_re->GetManager()->KillTimer(m_re, idTimer);
  479. fTimer = FALSE;
  480. }
  481. void CTxtWinHost::TxScrollWindowEx (INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll)
  482. {
  483. return;
  484. }
  485. void CTxtWinHost::TxSetCapture(BOOL fCapture)
  486. {
  487. if (fCapture) m_re->GetManager()->SetCapture();
  488. else m_re->GetManager()->ReleaseCapture();
  489. fCaptured = fCapture;
  490. }
  491. void CTxtWinHost::TxSetFocus()
  492. {
  493. m_re->SetFocus();
  494. }
  495. void CTxtWinHost::TxSetCursor(HCURSOR hcur, BOOL fText)
  496. {
  497. ::SetCursor(hcur);
  498. }
  499. BOOL CTxtWinHost::TxScreenToClient(LPPOINT lppt)
  500. {
  501. return ::ScreenToClient(m_re->GetManager()->GetPaintWindow(), lppt);
  502. }
  503. BOOL CTxtWinHost::TxClientToScreen(LPPOINT lppt)
  504. {
  505. return ::ClientToScreen(m_re->GetManager()->GetPaintWindow(), lppt);
  506. }
  507. HRESULT CTxtWinHost::TxActivate(LONG *plOldState)
  508. {
  509. return S_OK;
  510. }
  511. HRESULT CTxtWinHost::TxDeactivate(LONG lNewState)
  512. {
  513. return S_OK;
  514. }
  515. HRESULT CTxtWinHost::TxGetClientRect(LPRECT prc)
  516. {
  517. *prc = rcClient;
  518. GetControlRect(prc);
  519. return NOERROR;
  520. }
  521. HRESULT CTxtWinHost::TxGetViewInset(LPRECT prc)
  522. {
  523. prc->left = prc->right = prc->top = prc->bottom = 0;
  524. return NOERROR;
  525. }
  526. HRESULT CTxtWinHost::TxGetCharFormat(const CHARFORMATW **ppCF)
  527. {
  528. *ppCF = &cf;
  529. return NOERROR;
  530. }
  531. HRESULT CTxtWinHost::TxGetParaFormat(const PARAFORMAT **ppPF)
  532. {
  533. *ppPF = &pf;
  534. return NOERROR;
  535. }
  536. COLORREF CTxtWinHost::TxGetSysColor(int nIndex)
  537. {
  538. return ::GetSysColor(nIndex);
  539. }
  540. HRESULT CTxtWinHost::TxGetBackStyle(TXTBACKSTYLE *pstyle)
  541. {
  542. *pstyle = !fTransparent ? TXTBACK_OPAQUE : TXTBACK_TRANSPARENT;
  543. return NOERROR;
  544. }
  545. HRESULT CTxtWinHost::TxGetMaxLength(DWORD *pLength)
  546. {
  547. *pLength = cchTextMost;
  548. return NOERROR;
  549. }
  550. HRESULT CTxtWinHost::TxGetScrollBars(DWORD *pdwScrollBar)
  551. {
  552. *pdwScrollBar = dwStyle & (WS_VSCROLL | WS_HSCROLL | ES_AUTOVSCROLL |
  553. ES_AUTOHSCROLL | ES_DISABLENOSCROLL);
  554. return NOERROR;
  555. }
  556. HRESULT CTxtWinHost::TxGetPasswordChar(TCHAR *pch)
  557. {
  558. #ifdef _UNICODE
  559. *pch = chPasswordChar;
  560. #else
  561. ::WideCharToMultiByte(CP_ACP, 0, &chPasswordChar, 1, pch, 1, NULL, NULL) ;
  562. #endif
  563. return NOERROR;
  564. }
  565. HRESULT CTxtWinHost::TxGetAcceleratorPos(LONG *pcp)
  566. {
  567. *pcp = laccelpos;
  568. return S_OK;
  569. }
  570. HRESULT CTxtWinHost::OnTxCharFormatChange(const CHARFORMATW *pcf)
  571. {
  572. return S_OK;
  573. }
  574. HRESULT CTxtWinHost::OnTxParaFormatChange(const PARAFORMAT *ppf)
  575. {
  576. return S_OK;
  577. }
  578. HRESULT CTxtWinHost::TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits)
  579. {
  580. DWORD dwProperties = 0;
  581. if (fRich)
  582. {
  583. dwProperties = TXTBIT_RICHTEXT;
  584. }
  585. if (dwStyle & ES_MULTILINE)
  586. {
  587. dwProperties |= TXTBIT_MULTILINE;
  588. }
  589. if (dwStyle & ES_READONLY)
  590. {
  591. dwProperties |= TXTBIT_READONLY;
  592. }
  593. if (dwStyle & ES_PASSWORD)
  594. {
  595. dwProperties |= TXTBIT_USEPASSWORD;
  596. }
  597. if (!(dwStyle & ES_NOHIDESEL))
  598. {
  599. dwProperties |= TXTBIT_HIDESELECTION;
  600. }
  601. if (fEnableAutoWordSel)
  602. {
  603. dwProperties |= TXTBIT_AUTOWORDSEL;
  604. }
  605. if (fWordWrap)
  606. {
  607. dwProperties |= TXTBIT_WORDWRAP;
  608. }
  609. if (fAllowBeep)
  610. {
  611. dwProperties |= TXTBIT_ALLOWBEEP;
  612. }
  613. if (fSaveSelection)
  614. {
  615. dwProperties |= TXTBIT_SAVESELECTION;
  616. }
  617. *pdwBits = dwProperties & dwMask;
  618. return NOERROR;
  619. }
  620. HRESULT CTxtWinHost::TxNotify(DWORD iNotify, void *pv)
  621. {
  622. if( iNotify == EN_REQUESTRESIZE ) {
  623. RECT rc;
  624. REQRESIZE *preqsz = (REQRESIZE *)pv;
  625. GetControlRect(&rc);
  626. rc.bottom = rc.top + preqsz->rc.bottom;
  627. rc.right = rc.left + preqsz->rc.right;
  628. SetClientRect(&rc);
  629. return S_OK;
  630. }
  631. m_re->OnTxNotify(iNotify, pv);
  632. return S_OK;
  633. }
  634. HRESULT CTxtWinHost::TxGetExtent(LPSIZEL lpExtent)
  635. {
  636. *lpExtent = sizelExtent;
  637. return S_OK;
  638. }
  639. HRESULT CTxtWinHost::TxGetSelectionBarWidth (LONG *plSelBarWidth)
  640. {
  641. *plSelBarWidth = lSelBarWidth;
  642. return S_OK;
  643. }
  644. void CTxtWinHost::SetWordWrap(BOOL fWordWrap)
  645. {
  646. fWordWrap = fWordWrap;
  647. pserv->OnTxPropertyBitsChange(TXTBIT_WORDWRAP, fWordWrap ? TXTBIT_WORDWRAP : 0);
  648. }
  649. BOOL CTxtWinHost::GetReadOnly()
  650. {
  651. return (dwStyle & ES_READONLY) != 0;
  652. }
  653. void CTxtWinHost::SetReadOnly(BOOL fReadOnly)
  654. {
  655. if (fReadOnly)
  656. {
  657. dwStyle |= ES_READONLY;
  658. }
  659. else
  660. {
  661. dwStyle &= ~ES_READONLY;
  662. }
  663. pserv->OnTxPropertyBitsChange(TXTBIT_READONLY,
  664. fReadOnly ? TXTBIT_READONLY : 0);
  665. }
  666. void CTxtWinHost::SetFont(HFONT hFont)
  667. {
  668. if( hFont == NULL ) return;
  669. LOGFONT lf;
  670. ::GetObject(hFont, sizeof(LOGFONT), &lf);
  671. LONG yPixPerInch = ::GetDeviceCaps(m_re->GetManager()->GetPaintDC(), LOGPIXELSY);
  672. cf.yHeight = -lf.lfHeight * LY_PER_INCH / yPixPerInch;
  673. if(lf.lfWeight >= FW_BOLD) cf.dwEffects |= CFE_BOLD;
  674. else cf.dwEffects &= ~CFE_BOLD;
  675. if(lf.lfItalic) cf.dwEffects |= CFE_ITALIC;
  676. else cf.dwEffects &= ~CFE_ITALIC;
  677. if(lf.lfUnderline) cf.dwEffects |= CFE_UNDERLINE;
  678. else cf.dwEffects &= ~CFE_UNDERLINE;
  679. cf.bCharSet = lf.lfCharSet;
  680. cf.bPitchAndFamily = lf.lfPitchAndFamily;
  681. #ifdef _UNICODE
  682. _tcscpy(cf.szFaceName, lf.lfFaceName);
  683. #else
  684. //need to thunk pcf->szFaceName to a standard char string.in this case it's easy because our thunk is also our copy
  685. MultiByteToWideChar(CP_ACP, 0, lf.lfFaceName, LF_FACESIZE, cf.szFaceName, LF_FACESIZE) ;
  686. #endif
  687. pserv->OnTxPropertyBitsChange(TXTBIT_CHARFORMATCHANGE,
  688. TXTBIT_CHARFORMATCHANGE);
  689. }
  690. void CTxtWinHost::SetColor(DWORD dwColor)
  691. {
  692. cf.crTextColor = RGB(GetBValue(dwColor), GetGValue(dwColor), GetRValue(dwColor));
  693. pserv->OnTxPropertyBitsChange(TXTBIT_CHARFORMATCHANGE,
  694. TXTBIT_CHARFORMATCHANGE);
  695. }
  696. SIZEL* CTxtWinHost::GetExtent()
  697. {
  698. return &sizelExtent;
  699. }
  700. void CTxtWinHost::SetExtent(SIZEL *psizelExtent)
  701. {
  702. sizelExtent = *psizelExtent;
  703. pserv->OnTxPropertyBitsChange(TXTBIT_EXTENTCHANGE, TXTBIT_EXTENTCHANGE);
  704. }
  705. void CTxtWinHost::LimitText(LONG nChars)
  706. {
  707. cchTextMost = nChars;
  708. if( cchTextMost <= 0 ) cchTextMost = cInitTextMax;
  709. pserv->OnTxPropertyBitsChange(TXTBIT_MAXLENGTHCHANGE, TXTBIT_MAXLENGTHCHANGE);
  710. }
  711. BOOL CTxtWinHost::IsCaptured()
  712. {
  713. return fCaptured;
  714. }
  715. BOOL CTxtWinHost::IsShowCaret()
  716. {
  717. return fShowCaret;
  718. }
  719. void CTxtWinHost::NeedFreshCaret()
  720. {
  721. fNeedFreshCaret = TRUE;
  722. }
  723. INT CTxtWinHost::GetCaretWidth()
  724. {
  725. return iCaretWidth;
  726. }
  727. INT CTxtWinHost::GetCaretHeight()
  728. {
  729. return iCaretHeight;
  730. }
  731. BOOL CTxtWinHost::GetAllowBeep()
  732. {
  733. return fAllowBeep;
  734. }
  735. void CTxtWinHost::SetAllowBeep(BOOL fAllowBeep)
  736. {
  737. fAllowBeep = fAllowBeep;
  738. pserv->OnTxPropertyBitsChange(TXTBIT_ALLOWBEEP,
  739. fAllowBeep ? TXTBIT_ALLOWBEEP : 0);
  740. }
  741. WORD CTxtWinHost::GetDefaultAlign()
  742. {
  743. return pf.wAlignment;
  744. }
  745. void CTxtWinHost::SetDefaultAlign(WORD wNewAlign)
  746. {
  747. pf.wAlignment = wNewAlign;
  748. // Notify control of property change
  749. pserv->OnTxPropertyBitsChange(TXTBIT_PARAFORMATCHANGE, 0);
  750. }
  751. BOOL CTxtWinHost::GetRichTextFlag()
  752. {
  753. return fRich;
  754. }
  755. void CTxtWinHost::SetRichTextFlag(BOOL fNew)
  756. {
  757. fRich = fNew;
  758. pserv->OnTxPropertyBitsChange(TXTBIT_RICHTEXT,
  759. fNew ? TXTBIT_RICHTEXT : 0);
  760. }
  761. LONG CTxtWinHost::GetDefaultLeftIndent()
  762. {
  763. return pf.dxOffset;
  764. }
  765. void CTxtWinHost::SetDefaultLeftIndent(LONG lNewIndent)
  766. {
  767. pf.dxOffset = lNewIndent;
  768. pserv->OnTxPropertyBitsChange(TXTBIT_PARAFORMATCHANGE, 0);
  769. }
  770. void CTxtWinHost::SetClientRect(RECT *prc)
  771. {
  772. rcClient = *prc;
  773. LONG xPerInch = ::GetDeviceCaps(m_re->GetManager()->GetPaintDC(), LOGPIXELSX);
  774. LONG yPerInch = ::GetDeviceCaps(m_re->GetManager()->GetPaintDC(), LOGPIXELSY);
  775. sizelExtent.cx = DXtoHimetricX(rcClient.right - rcClient.left, xPerInch);
  776. sizelExtent.cy = DYtoHimetricY(rcClient.bottom - rcClient.top, yPerInch);
  777. pserv->OnTxPropertyBitsChange(TXTBIT_VIEWINSETCHANGE, TXTBIT_VIEWINSETCHANGE);
  778. }
  779. BOOL CTxtWinHost::SetSaveSelection(BOOL f_SaveSelection)
  780. {
  781. BOOL fResult = f_SaveSelection;
  782. fSaveSelection = f_SaveSelection;
  783. // notify text services of property change
  784. pserv->OnTxPropertyBitsChange(TXTBIT_SAVESELECTION,
  785. fSaveSelection ? TXTBIT_SAVESELECTION : 0);
  786. return fResult;
  787. }
  788. HRESULT CTxtWinHost::OnTxInPlaceDeactivate()
  789. {
  790. HRESULT hr = pserv->OnTxInPlaceDeactivate();
  791. if (SUCCEEDED(hr))
  792. {
  793. fInplaceActive = FALSE;
  794. }
  795. return hr;
  796. }
  797. HRESULT CTxtWinHost::OnTxInPlaceActivate(LPCRECT prcClient)
  798. {
  799. fInplaceActive = TRUE;
  800. HRESULT hr = pserv->OnTxInPlaceActivate(prcClient);
  801. if (FAILED(hr))
  802. {
  803. fInplaceActive = FALSE;
  804. }
  805. return hr;
  806. }
  807. BOOL CTxtWinHost::DoSetCursor(RECT *prc, POINT *pt)
  808. {
  809. RECT rc = prc ? *prc : rcClient;
  810. // Is this in our rectangle?
  811. if (PtInRect(&rc, *pt))
  812. {
  813. RECT *prcClient = (!fInplaceActive || prc) ? &rc : NULL;
  814. pserv->OnTxSetCursor(DVASPECT_CONTENT, -1, NULL, NULL, m_re->GetManager()->GetPaintDC(),
  815. NULL, prcClient, pt->x, pt->y);
  816. return TRUE;
  817. }
  818. return FALSE;
  819. }
  820. void CTxtWinHost::GetControlRect(LPRECT prc)
  821. {
  822. prc->top = rcClient.top;
  823. prc->bottom = rcClient.bottom;
  824. prc->left = rcClient.left;
  825. prc->right = rcClient.right;
  826. }
  827. void CTxtWinHost::SetTransparent(BOOL f_Transparent)
  828. {
  829. fTransparent = f_Transparent;
  830. // notify text services of property change
  831. pserv->OnTxPropertyBitsChange(TXTBIT_BACKSTYLECHANGE, 0);
  832. }
  833. LONG CTxtWinHost::SetAccelPos(LONG l_accelpos)
  834. {
  835. LONG laccelposOld = l_accelpos;
  836. laccelpos = l_accelpos;
  837. // notify text services of property change
  838. pserv->OnTxPropertyBitsChange(TXTBIT_SHOWACCELERATOR, 0);
  839. return laccelposOld;
  840. }
  841. WCHAR CTxtWinHost::SetPasswordChar(WCHAR ch_PasswordChar)
  842. {
  843. WCHAR chOldPasswordChar = chPasswordChar;
  844. chPasswordChar = ch_PasswordChar;
  845. // notify text services of property change
  846. pserv->OnTxPropertyBitsChange(TXTBIT_USEPASSWORD,
  847. (chPasswordChar != 0) ? TXTBIT_USEPASSWORD : 0);
  848. return chOldPasswordChar;
  849. }
  850. void CTxtWinHost::SetDisabled(BOOL fOn)
  851. {
  852. cf.dwMask |= CFM_COLOR | CFM_DISABLED;
  853. cf.dwEffects |= CFE_AUTOCOLOR | CFE_DISABLED;
  854. if( !fOn )
  855. {
  856. cf.dwEffects &= ~CFE_DISABLED;
  857. }
  858. pserv->OnTxPropertyBitsChange(TXTBIT_CHARFORMATCHANGE,
  859. TXTBIT_CHARFORMATCHANGE);
  860. }
  861. LONG CTxtWinHost::SetSelBarWidth(LONG l_SelBarWidth)
  862. {
  863. LONG lOldSelBarWidth = lSelBarWidth;
  864. lSelBarWidth = l_SelBarWidth;
  865. if (lSelBarWidth)
  866. {
  867. dwStyle |= ES_SELECTIONBAR;
  868. }
  869. else
  870. {
  871. dwStyle &= (~ES_SELECTIONBAR);
  872. }
  873. pserv->OnTxPropertyBitsChange(TXTBIT_SELBARCHANGE, TXTBIT_SELBARCHANGE);
  874. return lOldSelBarWidth;
  875. }
  876. BOOL CTxtWinHost::GetTimerState()
  877. {
  878. return fTimer;
  879. }
  880. void CTxtWinHost::SetCharFormat(CHARFORMAT2W &c)
  881. {
  882. cf = c;
  883. }
  884. void CTxtWinHost::SetParaFormat(PARAFORMAT2 &p)
  885. {
  886. pf = p;
  887. }
  888. /////////////////////////////////////////////////////////////////////////////////////
  889. //
  890. //
  891. CRichEditUI::CRichEditUI() : m_pTwh(NULL), m_bVScrollBarFixing(false), m_bWantTab(true), m_bWantReturn(true),
  892. m_bWantCtrlReturn(true), m_bTransparent(true), m_bRich(true), m_bReadOnly(false), m_bWordWrap(false), m_dwTextColor(0), m_iFont(-1),
  893. m_iLimitText(cInitTextMax), m_lTwhStyle(ES_MULTILINE), m_bDrawCaret(true), m_bInited(false)
  894. {
  895. }
  896. CRichEditUI::~CRichEditUI()
  897. {
  898. if( m_pTwh ) {
  899. m_pTwh->Release();
  900. m_pManager->RemoveMessageFilter(this);
  901. }
  902. }
  903. LPCTSTR CRichEditUI::GetClass() const
  904. {
  905. return _T("RichEditUI");
  906. }
  907. LPVOID CRichEditUI::GetInterface(LPCTSTR pstrName)
  908. {
  909. if( _tcscmp(pstrName, DUI_CTR_RICHEDIT) == 0 ) return static_cast<CRichEditUI*>(this);
  910. return CContainerUI::GetInterface(pstrName);
  911. }
  912. UINT CRichEditUI::GetControlFlags() const
  913. {
  914. if( !IsEnabled() ) return CControlUI::GetControlFlags();
  915. return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
  916. }
  917. bool CRichEditUI::IsWantTab()
  918. {
  919. return m_bWantTab;
  920. }
  921. void CRichEditUI::SetWantTab(bool bWantTab)
  922. {
  923. m_bWantTab = bWantTab;
  924. }
  925. bool CRichEditUI::IsWantReturn()
  926. {
  927. return m_bWantReturn;
  928. }
  929. void CRichEditUI::SetWantReturn(bool bWantReturn)
  930. {
  931. m_bWantReturn = bWantReturn;
  932. }
  933. bool CRichEditUI::IsWantCtrlReturn()
  934. {
  935. return m_bWantCtrlReturn;
  936. }
  937. void CRichEditUI::SetWantCtrlReturn(bool bWantCtrlReturn)
  938. {
  939. m_bWantCtrlReturn = bWantCtrlReturn;
  940. }
  941. bool CRichEditUI::IsTransparent()
  942. {
  943. return m_bTransparent;
  944. }
  945. void CRichEditUI::SetTransparent(bool bTransparent)
  946. {
  947. m_bTransparent = bTransparent;
  948. if( m_pTwh ) m_pTwh->SetTransparent(bTransparent);
  949. }
  950. bool CRichEditUI::IsRich()
  951. {
  952. return m_bRich;
  953. }
  954. void CRichEditUI::SetRich(bool bRich)
  955. {
  956. m_bRich = bRich;
  957. if( m_pTwh ) m_pTwh->SetRichTextFlag(bRich);
  958. }
  959. bool CRichEditUI::IsReadOnly()
  960. {
  961. return m_bReadOnly;
  962. }
  963. void CRichEditUI::SetReadOnly(bool bReadOnly)
  964. {
  965. m_bReadOnly = bReadOnly;
  966. if( m_pTwh ) m_pTwh->SetReadOnly(bReadOnly);
  967. }
  968. bool CRichEditUI::GetWordWrap()
  969. {
  970. return m_bWordWrap;
  971. }
  972. void CRichEditUI::SetWordWrap(bool bWordWrap)
  973. {
  974. m_bWordWrap = bWordWrap;
  975. if( m_pTwh ) m_pTwh->SetWordWrap(bWordWrap);
  976. }
  977. int CRichEditUI::GetFont()
  978. {
  979. return m_iFont;
  980. }
  981. void CRichEditUI::SetFont(int index)
  982. {
  983. m_iFont = index;
  984. if( m_pTwh ) {
  985. m_pTwh->SetFont(GetManager()->GetFont(m_iFont));
  986. }
  987. }
  988. void CRichEditUI::SetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic)
  989. {
  990. if( m_pTwh ) {
  991. LOGFONT lf = { 0 };
  992. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  993. _tcsncpy(lf.lfFaceName, pStrFontName, LF_FACESIZE);
  994. lf.lfCharSet = DEFAULT_CHARSET;
  995. lf.lfHeight = -nSize;
  996. if( bBold ) lf.lfWeight += FW_BOLD;
  997. if( bUnderline ) lf.lfUnderline = TRUE;
  998. if( bItalic ) lf.lfItalic = TRUE;
  999. HFONT hFont = ::CreateFontIndirect(&lf);
  1000. if( hFont == NULL ) return;
  1001. m_pTwh->SetFont(hFont);
  1002. ::DeleteObject(hFont);
  1003. }
  1004. }
  1005. LONG CRichEditUI::GetWinStyle()
  1006. {
  1007. return m_lTwhStyle;
  1008. }
  1009. void CRichEditUI::SetWinStyle(LONG lStyle)
  1010. {
  1011. m_lTwhStyle = lStyle;
  1012. }
  1013. DWORD CRichEditUI::GetTextColor()
  1014. {
  1015. return m_dwTextColor;
  1016. }
  1017. void CRichEditUI::SetTextColor(DWORD dwTextColor)
  1018. {
  1019. m_dwTextColor = dwTextColor;
  1020. if( m_pTwh ) {
  1021. m_pTwh->SetColor(dwTextColor);
  1022. }
  1023. }
  1024. int CRichEditUI::GetLimitText()
  1025. {
  1026. return m_iLimitText;
  1027. }
  1028. void CRichEditUI::SetLimitText(int iChars)
  1029. {
  1030. m_iLimitText = iChars;
  1031. if( m_pTwh ) {
  1032. m_pTwh->LimitText(m_iLimitText);
  1033. }
  1034. }
  1035. long CRichEditUI::GetTextLength(DWORD dwFlags) const
  1036. {
  1037. GETTEXTLENGTHEX textLenEx;
  1038. textLenEx.flags = dwFlags;
  1039. #ifdef _UNICODE
  1040. textLenEx.codepage = 1200;
  1041. #else
  1042. textLenEx.codepage = CP_ACP;
  1043. #endif
  1044. LRESULT lResult;
  1045. TxSendMessage(EM_GETTEXTLENGTHEX, (WPARAM)&textLenEx, 0, &lResult);
  1046. return (long)lResult;
  1047. }
  1048. CDuiString CRichEditUI::GetText() const
  1049. {
  1050. long lLen = GetTextLength(GTL_DEFAULT);
  1051. LPTSTR lpText = NULL;
  1052. GETTEXTEX gt;
  1053. gt.flags = GT_DEFAULT;
  1054. #ifdef _UNICODE
  1055. gt.cb = sizeof(TCHAR) * (lLen + 1) ;
  1056. gt.codepage = 1200;
  1057. lpText = new TCHAR[lLen + 1];
  1058. ::ZeroMemory(lpText, (lLen + 1) * sizeof(TCHAR));
  1059. #else
  1060. gt.cb = sizeof(TCHAR) * lLen * 2 + 1;
  1061. gt.codepage = CP_ACP;
  1062. lpText = new TCHAR[lLen * 2 + 1];
  1063. ::ZeroMemory(lpText, (lLen * 2 + 1) * sizeof(TCHAR));
  1064. #endif
  1065. gt.lpDefaultChar = NULL;
  1066. gt.lpUsedDefChar = NULL;
  1067. TxSendMessage(EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)lpText, 0);
  1068. CDuiString sText(lpText);
  1069. delete[] lpText;
  1070. return sText;
  1071. }
  1072. void CRichEditUI::SetText(LPCTSTR pstrText)
  1073. {
  1074. m_sText = pstrText;
  1075. if( !m_pTwh ) return;
  1076. SetSel(0, -1);
  1077. ReplaceSel(pstrText, FALSE);
  1078. }
  1079. bool CRichEditUI::GetModify() const
  1080. {
  1081. if( !m_pTwh ) return false;
  1082. LRESULT lResult;
  1083. TxSendMessage(EM_GETMODIFY, 0, 0, &lResult);
  1084. return (BOOL)lResult == TRUE;
  1085. }
  1086. void CRichEditUI::SetModify(bool bModified) const
  1087. {
  1088. TxSendMessage(EM_SETMODIFY, bModified, 0, 0);
  1089. }
  1090. void CRichEditUI::GetSel(CHARRANGE &cr) const
  1091. {
  1092. TxSendMessage(EM_EXGETSEL, 0, (LPARAM)&cr, 0);
  1093. }
  1094. void CRichEditUI::GetSel(long& nStartChar, long& nEndChar) const
  1095. {
  1096. CHARRANGE cr;
  1097. TxSendMessage(EM_EXGETSEL, 0, (LPARAM)&cr, 0);
  1098. nStartChar = cr.cpMin;
  1099. nEndChar = cr.cpMax;
  1100. }
  1101. int CRichEditUI::SetSel(CHARRANGE &cr)
  1102. {
  1103. LRESULT lResult;
  1104. TxSendMessage(EM_EXSETSEL, 0, (LPARAM)&cr, &lResult);
  1105. return (int)lResult;
  1106. }
  1107. int CRichEditUI::SetSel(long nStartChar, long nEndChar)
  1108. {
  1109. CHARRANGE cr;
  1110. cr.cpMin = nStartChar;
  1111. cr.cpMax = nEndChar;
  1112. LRESULT lResult;
  1113. TxSendMessage(EM_EXSETSEL, 0, (LPARAM)&cr, &lResult);
  1114. return (int)lResult;
  1115. }
  1116. void CRichEditUI::ReplaceSel(LPCTSTR lpszNewText, bool bCanUndo)
  1117. {
  1118. #ifdef _UNICODE
  1119. TxSendMessage(EM_REPLACESEL, (WPARAM) bCanUndo, (LPARAM)lpszNewText, 0);
  1120. #else
  1121. int iLen = _tcslen(lpszNewText);
  1122. LPWSTR lpText = new WCHAR[iLen + 1];
  1123. ::ZeroMemory(lpText, (iLen + 1) * sizeof(WCHAR));
  1124. ::MultiByteToWideChar(CP_ACP, 0, lpszNewText, -1, (LPWSTR)lpText, iLen) ;
  1125. TxSendMessage(EM_REPLACESEL, (WPARAM) bCanUndo, (LPARAM)lpText, 0);
  1126. delete[] lpText;
  1127. #endif
  1128. }
  1129. void CRichEditUI::ReplaceSelW(LPCWSTR lpszNewText, bool bCanUndo)
  1130. {
  1131. TxSendMessage(EM_REPLACESEL, (WPARAM) bCanUndo, (LPARAM)lpszNewText, 0);
  1132. }
  1133. CDuiString CRichEditUI::GetSelText() const
  1134. {
  1135. if( !m_pTwh ) return CDuiString();
  1136. CHARRANGE cr;
  1137. cr.cpMin = cr.cpMax = 0;
  1138. TxSendMessage(EM_EXGETSEL, 0, (LPARAM)&cr, 0);
  1139. LPWSTR lpText = NULL;
  1140. lpText = new WCHAR[cr.cpMax - cr.cpMin + 1];
  1141. ::ZeroMemory(lpText, (cr.cpMax - cr.cpMin + 1) * sizeof(WCHAR));
  1142. TxSendMessage(EM_GETSELTEXT, 0, (LPARAM)lpText, 0);
  1143. CDuiString sText;
  1144. sText = (LPCWSTR)lpText;
  1145. delete[] lpText;
  1146. return sText;
  1147. }
  1148. int CRichEditUI::SetSelAll()
  1149. {
  1150. return SetSel(0, -1);
  1151. }
  1152. int CRichEditUI::SetSelNone()
  1153. {
  1154. return SetSel(-1, 0);
  1155. }
  1156. bool CRichEditUI::GetZoom(int& nNum, int& nDen) const
  1157. {
  1158. LRESULT lResult;
  1159. TxSendMessage(EM_GETZOOM, (WPARAM)&nNum, (LPARAM)&nDen, &lResult);
  1160. return (BOOL)lResult == TRUE;
  1161. }
  1162. bool CRichEditUI::SetZoom(int nNum, int nDen)
  1163. {
  1164. if (nNum < 0 || nNum > 64) return false;
  1165. if (nDen < 0 || nDen > 64) return false;
  1166. LRESULT lResult;
  1167. TxSendMessage(EM_SETZOOM, nNum, nDen, &lResult);
  1168. return (BOOL)lResult == TRUE;
  1169. }
  1170. bool CRichEditUI::SetZoomOff()
  1171. {
  1172. LRESULT lResult;
  1173. TxSendMessage(EM_SETZOOM, 0, 0, &lResult);
  1174. return (BOOL)lResult == TRUE;
  1175. }
  1176. WORD CRichEditUI::GetSelectionType() const
  1177. {
  1178. LRESULT lResult;
  1179. TxSendMessage(EM_SELECTIONTYPE, 0, 0, &lResult);
  1180. return (WORD)lResult;
  1181. }
  1182. bool CRichEditUI::GetAutoURLDetect() const
  1183. {
  1184. LRESULT lResult;
  1185. TxSendMessage(EM_GETAUTOURLDETECT, 0, 0, &lResult);
  1186. return (BOOL)lResult == TRUE;
  1187. }
  1188. bool CRichEditUI::SetAutoURLDetect(bool bAutoDetect)
  1189. {
  1190. LRESULT lResult;
  1191. TxSendMessage(EM_AUTOURLDETECT, bAutoDetect, 0, &lResult);
  1192. return (BOOL)lResult == FALSE;
  1193. }
  1194. DWORD CRichEditUI::GetEventMask() const
  1195. {
  1196. LRESULT lResult;
  1197. TxSendMessage(EM_GETEVENTMASK, 0, 0, &lResult);
  1198. return (DWORD)lResult;
  1199. }
  1200. DWORD CRichEditUI::SetEventMask(DWORD dwEventMask)
  1201. {
  1202. LRESULT lResult;
  1203. TxSendMessage(EM_SETEVENTMASK, 0, dwEventMask, &lResult);
  1204. return (DWORD)lResult;
  1205. }
  1206. CDuiString CRichEditUI::GetTextRange(long nStartChar, long nEndChar) const
  1207. {
  1208. TEXTRANGEW tr = { 0 };
  1209. tr.chrg.cpMin = nStartChar;
  1210. tr.chrg.cpMax = nEndChar;
  1211. LPWSTR lpText = NULL;
  1212. lpText = new WCHAR[nEndChar - nStartChar + 1];
  1213. ::ZeroMemory(lpText, (nEndChar - nStartChar + 1) * sizeof(WCHAR));
  1214. tr.lpstrText = lpText;
  1215. TxSendMessage(EM_GETTEXTRANGE, 0, (LPARAM)&tr, 0);
  1216. CDuiString sText;
  1217. sText = (LPCWSTR)lpText;
  1218. delete[] lpText;
  1219. return sText;
  1220. }
  1221. void CRichEditUI::HideSelection(bool bHide, bool bChangeStyle)
  1222. {
  1223. TxSendMessage(EM_HIDESELECTION, bHide, bChangeStyle, 0);
  1224. }
  1225. void CRichEditUI::ScrollCaret()
  1226. {
  1227. TxSendMessage(EM_SCROLLCARET, 0, 0, 0);
  1228. }
  1229. int CRichEditUI::InsertText(long nInsertAfterChar, LPCTSTR lpstrText, bool bCanUndo)
  1230. {
  1231. int nRet = SetSel(nInsertAfterChar, nInsertAfterChar);
  1232. ReplaceSel(lpstrText, bCanUndo);
  1233. return nRet;
  1234. }
  1235. int CRichEditUI::AppendText(LPCTSTR lpstrText, bool bCanUndo)
  1236. {
  1237. int nRet = SetSel(-1, -1);
  1238. ReplaceSel(lpstrText, bCanUndo);
  1239. return nRet;
  1240. }
  1241. DWORD CRichEditUI::GetDefaultCharFormat(CHARFORMAT2 &cf) const
  1242. {
  1243. cf.cbSize = sizeof(CHARFORMAT2);
  1244. LRESULT lResult;
  1245. TxSendMessage(EM_GETCHARFORMAT, 0, (LPARAM)&cf, &lResult);
  1246. return (DWORD)lResult;
  1247. }
  1248. bool CRichEditUI::SetDefaultCharFormat(CHARFORMAT2 &cf)
  1249. {
  1250. if( !m_pTwh ) return false;
  1251. cf.cbSize = sizeof(CHARFORMAT2);
  1252. LRESULT lResult;
  1253. TxSendMessage(EM_SETCHARFORMAT, 0, (LPARAM)&cf, &lResult);
  1254. if( (BOOL)lResult == TRUE ) {
  1255. CHARFORMAT2W cfw;
  1256. cfw.cbSize = sizeof(CHARFORMAT2W);
  1257. TxSendMessage(EM_GETCHARFORMAT, 1, (LPARAM)&cfw, 0);
  1258. m_pTwh->SetCharFormat(cfw);
  1259. return true;
  1260. }
  1261. return false;
  1262. }
  1263. DWORD CRichEditUI::GetSelectionCharFormat(CHARFORMAT2 &cf) const
  1264. {
  1265. cf.cbSize = sizeof(CHARFORMAT2);
  1266. LRESULT lResult;
  1267. TxSendMessage(EM_GETCHARFORMAT, 1, (LPARAM)&cf, &lResult);
  1268. return (DWORD)lResult;
  1269. }
  1270. bool CRichEditUI::SetSelectionCharFormat(CHARFORMAT2 &cf)
  1271. {
  1272. if( !m_pTwh ) return false;
  1273. cf.cbSize = sizeof(CHARFORMAT2);
  1274. LRESULT lResult;
  1275. TxSendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf, &lResult);
  1276. return (BOOL)lResult == TRUE;
  1277. }
  1278. bool CRichEditUI::SetWordCharFormat(CHARFORMAT2 &cf)
  1279. {
  1280. if( !m_pTwh ) return false;
  1281. cf.cbSize = sizeof(CHARFORMAT2);
  1282. LRESULT lResult;
  1283. TxSendMessage(EM_SETCHARFORMAT, SCF_SELECTION|SCF_WORD, (LPARAM)&cf, &lResult);
  1284. return (BOOL)lResult == TRUE;
  1285. }
  1286. DWORD CRichEditUI::GetParaFormat(PARAFORMAT2 &pf) const
  1287. {
  1288. pf.cbSize = sizeof(PARAFORMAT2);
  1289. LRESULT lResult;
  1290. TxSendMessage(EM_GETPARAFORMAT, 0, (LPARAM)&pf, &lResult);
  1291. return (DWORD)lResult;
  1292. }
  1293. bool CRichEditUI::SetParaFormat(PARAFORMAT2 &pf)
  1294. {
  1295. if( !m_pTwh ) return false;
  1296. pf.cbSize = sizeof(PARAFORMAT2);
  1297. LRESULT lResult;
  1298. TxSendMessage(EM_SETPARAFORMAT, 0, (LPARAM)&pf, &lResult);
  1299. if( (BOOL)lResult == TRUE ) {
  1300. m_pTwh->SetParaFormat(pf);
  1301. return true;
  1302. }
  1303. return false;
  1304. }
  1305. bool CRichEditUI::Redo()
  1306. {
  1307. if( !m_pTwh ) return false;
  1308. LRESULT lResult;
  1309. TxSendMessage(EM_REDO, 0, 0, &lResult);
  1310. return (BOOL)lResult == TRUE;
  1311. }
  1312. bool CRichEditUI::Undo()
  1313. {
  1314. if( !m_pTwh ) return false;
  1315. LRESULT lResult;
  1316. TxSendMessage(EM_UNDO, 0, 0, &lResult);
  1317. return (BOOL)lResult == TRUE;
  1318. }
  1319. void CRichEditUI::Clear()
  1320. {
  1321. TxSendMessage(WM_CLEAR, 0, 0, 0);
  1322. }
  1323. void CRichEditUI::Copy()
  1324. {
  1325. TxSendMessage(WM_COPY, 0, 0, 0);
  1326. }
  1327. void CRichEditUI::Cut()
  1328. {
  1329. TxSendMessage(WM_CUT, 0, 0, 0);
  1330. }
  1331. void CRichEditUI::Paste()
  1332. {
  1333. TxSendMessage(WM_PASTE, 0, 0, 0);
  1334. }
  1335. int CRichEditUI::GetLineCount() const
  1336. {
  1337. if( !m_pTwh ) return 0;
  1338. LRESULT lResult;
  1339. TxSendMessage(EM_GETLINECOUNT, 0, 0, &lResult);
  1340. return (int)lResult;
  1341. }
  1342. CDuiString CRichEditUI::GetLine(int nIndex, int nMaxLength) const
  1343. {
  1344. LPWSTR lpText = NULL;
  1345. lpText = new WCHAR[nMaxLength + 1];
  1346. ::ZeroMemory(lpText, (nMaxLength + 1) * sizeof(WCHAR));
  1347. *(LPWORD)lpText = (WORD)nMaxLength;
  1348. TxSendMessage(EM_GETLINE, nIndex, (LPARAM)lpText, 0);
  1349. CDuiString sText;
  1350. sText = (LPCWSTR)lpText;
  1351. delete[] lpText;
  1352. return sText;
  1353. }
  1354. int CRichEditUI::LineIndex(int nLine) const
  1355. {
  1356. LRESULT lResult;
  1357. TxSendMessage(EM_LINEINDEX, nLine, 0, &lResult);
  1358. return (int)lResult;
  1359. }
  1360. int CRichEditUI::LineLength(int nLine) const
  1361. {
  1362. LRESULT lResult;
  1363. TxSendMessage(EM_LINELENGTH, nLine, 0, &lResult);
  1364. return (int)lResult;
  1365. }
  1366. bool CRichEditUI::LineScroll(int nLines, int nChars)
  1367. {
  1368. LRESULT lResult;
  1369. TxSendMessage(EM_LINESCROLL, nChars, nLines, &lResult);
  1370. return (BOOL)lResult == TRUE;
  1371. }
  1372. CDuiPoint CRichEditUI::GetCharPos(long lChar) const
  1373. {
  1374. CDuiPoint pt;
  1375. TxSendMessage(EM_POSFROMCHAR, (WPARAM)&pt, (LPARAM)lChar, 0);
  1376. return pt;
  1377. }
  1378. long CRichEditUI::LineFromChar(long nIndex) const
  1379. {
  1380. if( !m_pTwh ) return 0L;
  1381. LRESULT lResult;
  1382. TxSendMessage(EM_EXLINEFROMCHAR, 0, nIndex, &lResult);
  1383. return (long)lResult;
  1384. }
  1385. CDuiPoint CRichEditUI::PosFromChar(UINT nChar) const
  1386. {
  1387. POINTL pt;
  1388. TxSendMessage(EM_POSFROMCHAR, (WPARAM)&pt, nChar, 0);
  1389. return CDuiPoint(pt.x, pt.y);
  1390. }
  1391. int CRichEditUI::CharFromPos(CDuiPoint pt) const
  1392. {
  1393. POINTL ptl = {pt.x, pt.y};
  1394. if( !m_pTwh ) return 0;
  1395. LRESULT lResult;
  1396. TxSendMessage(EM_CHARFROMPOS, 0, (LPARAM)&ptl, &lResult);
  1397. return (int)lResult;
  1398. }
  1399. void CRichEditUI::EmptyUndoBuffer()
  1400. {
  1401. TxSendMessage(EM_EMPTYUNDOBUFFER, 0, 0, 0);
  1402. }
  1403. UINT CRichEditUI::SetUndoLimit(UINT nLimit)
  1404. {
  1405. if( !m_pTwh ) return 0;
  1406. LRESULT lResult;
  1407. TxSendMessage(EM_SETUNDOLIMIT, (WPARAM) nLimit, 0, &lResult);
  1408. return (UINT)lResult;
  1409. }
  1410. long CRichEditUI::StreamIn(int nFormat, EDITSTREAM &es)
  1411. {
  1412. if( !m_pTwh ) return 0L;
  1413. LRESULT lResult;
  1414. TxSendMessage(EM_STREAMIN, nFormat, (LPARAM)&es, &lResult);
  1415. return (long)lResult;
  1416. }
  1417. long CRichEditUI::StreamOut(int nFormat, EDITSTREAM &es)
  1418. {
  1419. if( !m_pTwh ) return 0L;
  1420. LRESULT lResult;
  1421. TxSendMessage(EM_STREAMOUT, nFormat, (LPARAM)&es, &lResult);
  1422. return (long)lResult;
  1423. }
  1424. void CRichEditUI::DoInit()
  1425. {
  1426. if(m_bInited)
  1427. return ;
  1428. CREATESTRUCT cs;
  1429. cs.style = m_lTwhStyle;
  1430. cs.x = 0;
  1431. cs.y = 0;
  1432. cs.cy = 0;
  1433. cs.cx = 0;
  1434. cs.lpszName = m_sText.GetData();
  1435. CreateHost(this, &cs, &m_pTwh);
  1436. if( m_pTwh ) {
  1437. if( m_bTransparent ) m_pTwh->SetTransparent(TRUE);
  1438. LRESULT lResult;
  1439. m_pTwh->GetTextServices()->TxSendMessage(EM_SETLANGOPTIONS, 0, 0, &lResult);
  1440. m_pTwh->OnTxInPlaceActivate(NULL);
  1441. m_pManager->AddMessageFilter(this);
  1442. if( m_pManager->IsLayered() ) m_pManager->SetTimer(this, DEFAULT_TIMERID, ::GetCaretBlinkTime());
  1443. }
  1444. m_bInited= true;
  1445. }
  1446. HRESULT CRichEditUI::TxSendMessage(UINT msg, WPARAM wparam, LPARAM lparam, LRESULT *plresult) const
  1447. {
  1448. if( m_pTwh ) {
  1449. if( msg == WM_KEYDOWN && TCHAR(wparam) == VK_RETURN ) {
  1450. if( !m_bWantReturn || (::GetKeyState(VK_CONTROL) < 0 && !m_bWantCtrlReturn) ) {
  1451. if( m_pManager != NULL ) m_pManager->SendNotify((CControlUI*)this, DUI_MSGTYPE_RETURN);
  1452. return S_OK;
  1453. }
  1454. }
  1455. return m_pTwh->GetTextServices()->TxSendMessage(msg, wparam, lparam, plresult);
  1456. }
  1457. return S_FALSE;
  1458. }
  1459. IDropTarget* CRichEditUI::GetTxDropTarget()
  1460. {
  1461. IDropTarget *pdt = NULL;
  1462. if( m_pTwh->GetTextServices()->TxGetDropTarget(&pdt) == NOERROR ) return pdt;
  1463. return NULL;
  1464. }
  1465. bool CRichEditUI::OnTxViewChanged()
  1466. {
  1467. return true;
  1468. }
  1469. bool CRichEditUI::SetDropAcceptFile(bool bAccept)
  1470. {
  1471. LRESULT lResult;
  1472. TxSendMessage(EM_SETEVENTMASK, 0,ENM_DROPFILES|ENM_LINK, // ENM_CHANGE| ENM_CORRECTTEXT | ENM_DRAGDROPDONE | ENM_DROPFILES | ENM_IMECHANGE | ENM_LINK | ENM_OBJECTPOSITIONS | ENM_PROTECTED | ENM_REQUESTRESIZE | ENM_SCROLL | ENM_SELCHANGE | ENM_UPDATE,
  1473. &lResult);
  1474. return (BOOL)lResult == FALSE;
  1475. }
  1476. void CRichEditUI::OnTxNotify(DWORD iNotify, void *pv)
  1477. {
  1478. switch(iNotify)
  1479. {
  1480. case EN_CHANGE:
  1481. {
  1482. GetManager()->SendNotify(this, DUI_MSGTYPE_TEXTCHANGED);
  1483. }
  1484. break;
  1485. case EN_DROPFILES:
  1486. case EN_MSGFILTER:
  1487. case EN_OLEOPFAILED:
  1488. case EN_PROTECTED:
  1489. case EN_SAVECLIPBOARD:
  1490. case EN_SELCHANGE:
  1491. case EN_STOPNOUNDO:
  1492. case EN_LINK:
  1493. case EN_OBJECTPOSITIONS:
  1494. case EN_DRAGDROPDONE:
  1495. {
  1496. if(pv) // Fill out NMHDR portion of pv
  1497. {
  1498. LONG nId = GetWindowLong(this->GetManager()->GetPaintWindow(), GWL_ID);
  1499. NMHDR *phdr = (NMHDR *)pv;
  1500. phdr->hwndFrom = this->GetManager()->GetPaintWindow();
  1501. phdr->idFrom = nId;
  1502. phdr->code = iNotify;
  1503. if(SendMessage(this->GetManager()->GetPaintWindow(), WM_NOTIFY, (WPARAM) nId, (LPARAM) pv))
  1504. {
  1505. //hr = S_FALSE;
  1506. }
  1507. }
  1508. }
  1509. break;
  1510. }
  1511. }
  1512. // 多行非rich格式的richedit有一个滚动条bug,在最后一行是空行时,LineDown和SetScrollPos无法滚动到最后
  1513. // 引入iPos就是为了修正这个bug
  1514. void CRichEditUI::SetScrollPos(SIZE szPos)
  1515. {
  1516. int cx = 0;
  1517. int cy = 0;
  1518. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  1519. int iLastScrollPos = m_pVerticalScrollBar->GetScrollPos();
  1520. m_pVerticalScrollBar->SetScrollPos(szPos.cy);
  1521. cy = m_pVerticalScrollBar->GetScrollPos() - iLastScrollPos;
  1522. }
  1523. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  1524. int iLastScrollPos = m_pHorizontalScrollBar->GetScrollPos();
  1525. m_pHorizontalScrollBar->SetScrollPos(szPos.cx);
  1526. cx = m_pHorizontalScrollBar->GetScrollPos() - iLastScrollPos;
  1527. }
  1528. if( cy != 0 ) {
  1529. int iPos = 0;
  1530. if( m_pTwh && !m_bRich && m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() )
  1531. iPos = m_pVerticalScrollBar->GetScrollPos();
  1532. WPARAM wParam = MAKEWPARAM(SB_THUMBPOSITION, m_pVerticalScrollBar->GetScrollPos());
  1533. TxSendMessage(WM_VSCROLL, wParam, 0L, 0);
  1534. if( m_pTwh && !m_bRich && m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  1535. if( cy > 0 && m_pVerticalScrollBar->GetScrollPos() <= iPos )
  1536. m_pVerticalScrollBar->SetScrollPos(iPos);
  1537. }
  1538. }
  1539. if( cx != 0 ) {
  1540. WPARAM wParam = MAKEWPARAM(SB_THUMBPOSITION, m_pHorizontalScrollBar->GetScrollPos());
  1541. TxSendMessage(WM_HSCROLL, wParam, 0L, 0);
  1542. }
  1543. }
  1544. void CRichEditUI::LineUp()
  1545. {
  1546. TxSendMessage(WM_VSCROLL, SB_LINEUP, 0L, 0);
  1547. }
  1548. void CRichEditUI::LineDown()
  1549. {
  1550. int iPos = 0;
  1551. if( m_pTwh && !m_bRich && m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() )
  1552. iPos = m_pVerticalScrollBar->GetScrollPos();
  1553. TxSendMessage(WM_VSCROLL, SB_LINEDOWN, 0L, 0);
  1554. if( m_pTwh && !m_bRich && m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  1555. if( m_pVerticalScrollBar->GetScrollPos() <= iPos )
  1556. m_pVerticalScrollBar->SetScrollPos(m_pVerticalScrollBar->GetScrollRange());
  1557. }
  1558. }
  1559. void CRichEditUI::PageUp()
  1560. {
  1561. TxSendMessage(WM_VSCROLL, SB_PAGEUP, 0L, 0);
  1562. }
  1563. void CRichEditUI::PageDown()
  1564. {
  1565. TxSendMessage(WM_VSCROLL, SB_PAGEDOWN, 0L, 0);
  1566. }
  1567. void CRichEditUI::HomeUp()
  1568. {
  1569. TxSendMessage(WM_VSCROLL, SB_TOP, 0L, 0);
  1570. }
  1571. void CRichEditUI::EndDown()
  1572. {
  1573. TxSendMessage(WM_VSCROLL, SB_BOTTOM, 0L, 0);
  1574. }
  1575. void CRichEditUI::LineLeft()
  1576. {
  1577. TxSendMessage(WM_HSCROLL, SB_LINELEFT, 0L, 0);
  1578. }
  1579. void CRichEditUI::LineRight()
  1580. {
  1581. TxSendMessage(WM_HSCROLL, SB_LINERIGHT, 0L, 0);
  1582. }
  1583. void CRichEditUI::PageLeft()
  1584. {
  1585. TxSendMessage(WM_HSCROLL, SB_PAGELEFT, 0L, 0);
  1586. }
  1587. void CRichEditUI::PageRight()
  1588. {
  1589. TxSendMessage(WM_HSCROLL, SB_PAGERIGHT, 0L, 0);
  1590. }
  1591. void CRichEditUI::HomeLeft()
  1592. {
  1593. TxSendMessage(WM_HSCROLL, SB_LEFT, 0L, 0);
  1594. }
  1595. void CRichEditUI::EndRight()
  1596. {
  1597. TxSendMessage(WM_HSCROLL, SB_RIGHT, 0L, 0);
  1598. }
  1599. void CRichEditUI::DoEvent(TEventUI& event)
  1600. {
  1601. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  1602. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  1603. else CControlUI::DoEvent(event);
  1604. return;
  1605. }
  1606. if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
  1607. {
  1608. if( m_pTwh && m_pTwh->DoSetCursor(NULL, &event.ptMouse) ) {
  1609. return;
  1610. }
  1611. }
  1612. else if( event.Type == UIEVENT_WINDOWSIZE ) {
  1613. if( m_pTwh ) m_pTwh->NeedFreshCaret();
  1614. }
  1615. else if( event.Type == UIEVENT_SETFOCUS ) {
  1616. if( m_pTwh ) {
  1617. m_pTwh->OnTxInPlaceActivate(NULL);
  1618. m_pTwh->GetTextServices()->TxSendMessage(WM_SETFOCUS, 0, 0, 0);
  1619. }
  1620. m_bFocused = true;
  1621. Invalidate();
  1622. return;
  1623. }
  1624. if( event.Type == UIEVENT_KILLFOCUS ) {
  1625. if( m_pTwh ) {
  1626. m_pTwh->OnTxInPlaceActivate(NULL);
  1627. m_pTwh->GetTextServices()->TxSendMessage(WM_KILLFOCUS, 0, 0, 0);
  1628. }
  1629. m_bFocused = false;
  1630. Invalidate();
  1631. return;
  1632. }
  1633. else if( event.Type == UIEVENT_TIMER ) {
  1634. if( event.wParam == DEFAULT_TIMERID ) {
  1635. if( m_pTwh && m_pManager->IsLayered() && IsFocused() ) {
  1636. if (::GetFocus() != m_pManager->GetPaintWindow()) return;
  1637. m_bDrawCaret = !m_bDrawCaret;
  1638. POINT ptCaret;
  1639. ::GetCaretPos(&ptCaret);
  1640. RECT rcCaret = { ptCaret.x, ptCaret.y, ptCaret.x + m_pTwh->GetCaretWidth(),
  1641. ptCaret.y + m_pTwh->GetCaretHeight() };
  1642. RECT rcTemp = rcCaret;
  1643. if( !::IntersectRect(&rcCaret, &rcTemp, &m_rcItem) ) return;
  1644. CControlUI* pParent = this;
  1645. RECT rcParent;
  1646. while( pParent = pParent->GetParent() ) {
  1647. rcTemp = rcCaret;
  1648. rcParent = pParent->GetPos();
  1649. if( !::IntersectRect(&rcCaret, &rcTemp, &rcParent) ) {
  1650. return;
  1651. }
  1652. }
  1653. m_pManager->Invalidate(rcCaret);
  1654. }
  1655. return;
  1656. }
  1657. if( m_pTwh ) {
  1658. m_pTwh->GetTextServices()->TxSendMessage(WM_TIMER, event.wParam, event.lParam, 0);
  1659. }
  1660. return;
  1661. }
  1662. if( event.Type == UIEVENT_SCROLLWHEEL ) {
  1663. if( (event.wKeyState & MK_CONTROL) != 0 ) {
  1664. return;
  1665. }
  1666. }
  1667. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
  1668. {
  1669. return;
  1670. }
  1671. if( event.Type == UIEVENT_MOUSEMOVE )
  1672. {
  1673. return;
  1674. }
  1675. if( event.Type == UIEVENT_BUTTONUP )
  1676. {
  1677. return;
  1678. }
  1679. if( event.Type == UIEVENT_MOUSEENTER )
  1680. {
  1681. return;
  1682. }
  1683. if( event.Type == UIEVENT_MOUSELEAVE )
  1684. {
  1685. return;
  1686. }
  1687. if( event.Type > UIEVENT__KEYBEGIN && event.Type < UIEVENT__KEYEND )
  1688. {
  1689. return;
  1690. }
  1691. CContainerUI::DoEvent(event);
  1692. }
  1693. SIZE CRichEditUI::EstimateSize(SIZE szAvailable)
  1694. {
  1695. //return CDuiSize(m_rcItem); // 这种方式在第一次设置大小之后就大小不变了
  1696. return CContainerUI::EstimateSize(szAvailable);
  1697. }
  1698. void CRichEditUI::SetPos(RECT rc, bool bNeedInvalidate)
  1699. {
  1700. CControlUI::SetPos(rc, bNeedInvalidate);
  1701. rc = m_rcItem;
  1702. rc.left += m_rcInset.left;
  1703. rc.top += m_rcInset.top;
  1704. rc.right -= m_rcInset.right;
  1705. rc.bottom -= m_rcInset.bottom;
  1706. RECT rcScrollView = rc;
  1707. bool bVScrollBarVisiable = false;
  1708. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) {
  1709. bVScrollBarVisiable = true;
  1710. rc.top -= m_pVerticalScrollBar->GetScrollPos();
  1711. rc.bottom -= m_pVerticalScrollBar->GetScrollPos();
  1712. rc.bottom += m_pVerticalScrollBar->GetScrollRange();
  1713. rc.right -= m_pVerticalScrollBar->GetFixedWidth();
  1714. rcScrollView.right -= m_pVerticalScrollBar->GetFixedWidth();
  1715. }
  1716. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
  1717. rc.left -= m_pHorizontalScrollBar->GetScrollPos();
  1718. rc.right -= m_pHorizontalScrollBar->GetScrollPos();
  1719. rc.right += m_pHorizontalScrollBar->GetScrollRange();
  1720. rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
  1721. rcScrollView.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
  1722. }
  1723. if( m_pTwh != NULL ) {
  1724. m_pTwh->SetClientRect(&rcScrollView);
  1725. if( bVScrollBarVisiable && (!m_pVerticalScrollBar->IsVisible() || m_bVScrollBarFixing) ) {
  1726. LONG lWidth = rc.right - rc.left + m_pVerticalScrollBar->GetFixedWidth();
  1727. LONG lHeight = 0;
  1728. SIZEL szExtent = { -1, -1 };
  1729. m_pTwh->GetTextServices()->TxGetNaturalSize(
  1730. DVASPECT_CONTENT,
  1731. GetManager()->GetPaintDC(),
  1732. NULL,
  1733. NULL,
  1734. TXTNS_FITTOCONTENT,
  1735. &szExtent,
  1736. &lWidth,
  1737. &lHeight);
  1738. if( lHeight > rc.bottom - rc.top ) {
  1739. m_pVerticalScrollBar->SetVisible(true);
  1740. m_pVerticalScrollBar->SetScrollPos(0);
  1741. m_bVScrollBarFixing = true;
  1742. }
  1743. else {
  1744. if( m_bVScrollBarFixing ) {
  1745. m_pVerticalScrollBar->SetVisible(false);
  1746. m_bVScrollBarFixing = false;
  1747. }
  1748. }
  1749. }
  1750. }
  1751. if( m_pVerticalScrollBar != NULL && m_pVerticalScrollBar->IsVisible() ) {
  1752. RECT rcScrollBarPos = { rcScrollView.right, rcScrollView.top,
  1753. rcScrollView.right + m_pVerticalScrollBar->GetFixedWidth(), rcScrollView.bottom};
  1754. m_pVerticalScrollBar->SetPos(rcScrollBarPos, false);
  1755. }
  1756. if( m_pHorizontalScrollBar != NULL && m_pHorizontalScrollBar->IsVisible() ) {
  1757. RECT rcScrollBarPos = { rcScrollView.left, rcScrollView.bottom, rcScrollView.right,
  1758. rcScrollView.bottom + m_pHorizontalScrollBar->GetFixedHeight()};
  1759. m_pHorizontalScrollBar->SetPos(rcScrollBarPos, false);
  1760. }
  1761. for( int it = 0; it < m_items.GetSize(); it++ ) {
  1762. CControlUI* pControl = static_cast<CControlUI*>(m_items[it]);
  1763. if( !pControl->IsVisible() ) continue;
  1764. if( pControl->IsFloat() ) {
  1765. SetFloatPos(it);
  1766. }
  1767. else {
  1768. SIZE sz = { rc.right - rc.left, rc.bottom - rc.top };
  1769. if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
  1770. if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
  1771. if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
  1772. if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();
  1773. RECT rcCtrl = { rc.left, rc.top, rc.left + sz.cx, rc.top + sz.cy };
  1774. pControl->SetPos(rcCtrl, false);
  1775. }
  1776. }
  1777. }
  1778. void CRichEditUI::Move(SIZE szOffset, bool bNeedInvalidate)
  1779. {
  1780. CContainerUI::Move(szOffset, bNeedInvalidate);
  1781. if( m_pTwh != NULL ) {
  1782. RECT rc = m_rcItem;
  1783. rc.left += m_rcInset.left;
  1784. rc.top += m_rcInset.top;
  1785. rc.right -= m_rcInset.right;
  1786. rc.bottom -= m_rcInset.bottom;
  1787. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) rc.right -= m_pVerticalScrollBar->GetFixedWidth();
  1788. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
  1789. m_pTwh->SetClientRect(&rc);
  1790. }
  1791. }
  1792. void CRichEditUI::DoPaint(HDC hDC, const RECT& rcPaint)
  1793. {
  1794. RECT rcTemp = { 0 };
  1795. if( !::IntersectRect(&rcTemp, &rcPaint, &m_rcItem) ) return;
  1796. CRenderClip clip;
  1797. CRenderClip::GenerateClip(hDC, rcTemp, clip);
  1798. CControlUI::DoPaint(hDC, rcPaint);
  1799. if( m_pTwh ) {
  1800. RECT rc;
  1801. m_pTwh->GetControlRect(&rc);
  1802. // Remember wparam is actually the hdc and lparam is the update
  1803. // rect because this message has been preprocessed by the window.
  1804. m_pTwh->GetTextServices()->TxDraw(
  1805. DVASPECT_CONTENT, // Draw Aspect
  1806. /*-1*/0, // Lindex
  1807. NULL, // Info for drawing optimazation
  1808. NULL, // target device information
  1809. hDC, // Draw device HDC
  1810. NULL, // Target device HDC
  1811. (RECTL*)&rc, // Bounding client rectangle
  1812. NULL, // Clipping rectangle for metafiles
  1813. (RECT*)&rcPaint, // Update rectangle
  1814. NULL, // Call back function
  1815. NULL, // Call back parameter
  1816. 0); // What view of the object
  1817. if( m_bVScrollBarFixing ) {
  1818. LONG lWidth = rc.right - rc.left + m_pVerticalScrollBar->GetFixedWidth();
  1819. LONG lHeight = 0;
  1820. SIZEL szExtent = { -1, -1 };
  1821. m_pTwh->GetTextServices()->TxGetNaturalSize(
  1822. DVASPECT_CONTENT,
  1823. GetManager()->GetPaintDC(),
  1824. NULL,
  1825. NULL,
  1826. TXTNS_FITTOCONTENT,
  1827. &szExtent,
  1828. &lWidth,
  1829. &lHeight);
  1830. if( lHeight <= rc.bottom - rc.top ) {
  1831. NeedUpdate();
  1832. }
  1833. }
  1834. }
  1835. if( m_items.GetSize() > 0 ) {
  1836. RECT rc = m_rcItem;
  1837. rc.left += m_rcInset.left;
  1838. rc.top += m_rcInset.top;
  1839. rc.right -= m_rcInset.right;
  1840. rc.bottom -= m_rcInset.bottom;
  1841. if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) rc.right -= m_pVerticalScrollBar->GetFixedWidth();
  1842. if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
  1843. if( !::IntersectRect(&rcTemp, &rcPaint, &rc) ) {
  1844. for( int it = 0; it < m_items.GetSize(); it++ ) {
  1845. CControlUI* pControl = static_cast<CControlUI*>(m_items[it]);
  1846. if( !pControl->IsVisible() ) continue;
  1847. if( !::IntersectRect(&rcTemp, &rcPaint, &pControl->GetPos()) ) continue;
  1848. if( pControl ->IsFloat() ) {
  1849. if( !::IntersectRect(&rcTemp, &m_rcItem, &pControl->GetPos()) ) continue;
  1850. pControl->Paint(hDC, rcPaint);
  1851. }
  1852. }
  1853. }
  1854. else {
  1855. CRenderClip childClip;
  1856. CRenderClip::GenerateClip(hDC, rcTemp, childClip);
  1857. for( int it = 0; it < m_items.GetSize(); it++ ) {
  1858. CControlUI* pControl = static_cast<CControlUI*>(m_items[it]);
  1859. if( !pControl->IsVisible() ) continue;
  1860. if( !::IntersectRect(&rcTemp, &rcPaint, &pControl->GetPos()) ) continue;
  1861. if( pControl ->IsFloat() ) {
  1862. if( !::IntersectRect(&rcTemp, &m_rcItem, &pControl->GetPos()) ) continue;
  1863. CRenderClip::UseOldClipBegin(hDC, childClip);
  1864. pControl->Paint(hDC, rcPaint);
  1865. CRenderClip::UseOldClipEnd(hDC, childClip);
  1866. }
  1867. else {
  1868. if( !::IntersectRect(&rcTemp, &rc, &pControl->GetPos()) ) continue;
  1869. pControl->Paint(hDC, rcPaint);
  1870. }
  1871. }
  1872. }
  1873. }
  1874. if( m_pTwh && m_pTwh->IsShowCaret() && m_pManager->IsLayered() && IsFocused() && m_bDrawCaret ) {
  1875. POINT ptCaret;
  1876. ::GetCaretPos(&ptCaret);
  1877. if( ::PtInRect(&m_rcItem, ptCaret) ) {
  1878. RECT rcCaret = { ptCaret.x, ptCaret.y, ptCaret.x, ptCaret.y + m_pTwh->GetCaretHeight() };
  1879. CRenderEngine::DrawLine(hDC, rcCaret, m_pTwh->GetCaretWidth(), 0xFF000000);
  1880. }
  1881. }
  1882. if( m_pVerticalScrollBar != NULL && m_pVerticalScrollBar->IsVisible() ) {
  1883. if( ::IntersectRect(&rcTemp, &rcPaint, &m_pVerticalScrollBar->GetPos()) ) {
  1884. m_pVerticalScrollBar->Paint(hDC, rcPaint);
  1885. }
  1886. }
  1887. if( m_pHorizontalScrollBar != NULL && m_pHorizontalScrollBar->IsVisible() ) {
  1888. if( ::IntersectRect(&rcTemp, &rcPaint, &m_pHorizontalScrollBar->GetPos()) ) {
  1889. m_pHorizontalScrollBar->Paint(hDC, rcPaint);
  1890. }
  1891. }
  1892. }
  1893. void CRichEditUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  1894. {
  1895. if( _tcscmp(pstrName, _T("vscrollbar")) == 0 ) {
  1896. if( _tcscmp(pstrValue, _T("true")) == 0 ) m_lTwhStyle |= ES_DISABLENOSCROLL | WS_VSCROLL;
  1897. }
  1898. if( _tcscmp(pstrName, _T("autovscroll")) == 0 ) {
  1899. if( _tcscmp(pstrValue, _T("true")) == 0 ) m_lTwhStyle |= ES_AUTOVSCROLL;
  1900. }
  1901. else if( _tcscmp(pstrName, _T("hscrollbar")) == 0 ) {
  1902. if( _tcscmp(pstrValue, _T("true")) == 0 ) m_lTwhStyle |= ES_DISABLENOSCROLL | WS_HSCROLL;
  1903. }
  1904. if( _tcscmp(pstrName, _T("autohscroll")) == 0 ) {
  1905. if( _tcscmp(pstrValue, _T("true")) == 0 ) m_lTwhStyle |= ES_AUTOHSCROLL;
  1906. }
  1907. else if( _tcscmp(pstrName, _T("wanttab")) == 0 ) {
  1908. SetWantTab(_tcscmp(pstrValue, _T("true")) == 0);
  1909. }
  1910. else if( _tcscmp(pstrName, _T("wantreturn")) == 0 ) {
  1911. SetWantReturn(_tcscmp(pstrValue, _T("true")) == 0);
  1912. }
  1913. else if( _tcscmp(pstrName, _T("wantctrlreturn")) == 0 ) {
  1914. SetWantCtrlReturn(_tcscmp(pstrValue, _T("true")) == 0);
  1915. }
  1916. else if( _tcscmp(pstrName, _T("transparent")) == 0 ) {
  1917. SetTransparent(_tcscmp(pstrValue, _T("true")) == 0);
  1918. }
  1919. else if( _tcscmp(pstrName, _T("rich")) == 0 ) {
  1920. SetRich(_tcscmp(pstrValue, _T("true")) == 0);
  1921. }
  1922. else if( _tcscmp(pstrName, _T("multiline")) == 0 ) {
  1923. if( _tcscmp(pstrValue, _T("false")) == 0 ) m_lTwhStyle &= ~ES_MULTILINE;
  1924. }
  1925. else if( _tcscmp(pstrName, _T("readonly")) == 0 ) {
  1926. if( _tcscmp(pstrValue, _T("true")) == 0 ) { m_lTwhStyle |= ES_READONLY; m_bReadOnly = true; }
  1927. }
  1928. else if( _tcscmp(pstrName, _T("password")) == 0 ) {
  1929. if( _tcscmp(pstrValue, _T("true")) == 0 ) m_lTwhStyle |= ES_PASSWORD;
  1930. }
  1931. else if( _tcscmp(pstrName, _T("align")) == 0 ) {
  1932. if( _tcsstr(pstrValue, _T("left")) != NULL ) {
  1933. m_lTwhStyle &= ~(ES_CENTER | ES_RIGHT);
  1934. m_lTwhStyle |= ES_LEFT;
  1935. }
  1936. if( _tcsstr(pstrValue, _T("center")) != NULL ) {
  1937. m_lTwhStyle &= ~(ES_LEFT | ES_RIGHT);
  1938. m_lTwhStyle |= ES_CENTER;
  1939. }
  1940. if( _tcsstr(pstrValue, _T("right")) != NULL ) {
  1941. m_lTwhStyle &= ~(ES_LEFT | ES_CENTER);
  1942. m_lTwhStyle |= ES_RIGHT;
  1943. }
  1944. }
  1945. else if( _tcscmp(pstrName, _T("font")) == 0 ) SetFont(_ttoi(pstrValue));
  1946. else if( _tcscmp(pstrName, _T("textcolor")) == 0 ) {
  1947. while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
  1948. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1949. LPTSTR pstr = NULL;
  1950. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1951. SetTextColor(clrColor);
  1952. }
  1953. else CContainerUI::SetAttribute(pstrName, pstrValue);
  1954. }
  1955. LRESULT CRichEditUI::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  1956. {
  1957. if( !IsVisible() || !IsEnabled() ) return 0;
  1958. if( !IsMouseEnabled() && uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST ) return 0;
  1959. if( uMsg == WM_MOUSEWHEEL && (LOWORD(wParam) & MK_CONTROL) == 0 ) return 0;
  1960. bool bWasHandled = true;
  1961. if( (uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST) || uMsg == WM_SETCURSOR ) {
  1962. if( !m_pTwh->IsCaptured() ) {
  1963. switch (uMsg) {
  1964. case WM_LBUTTONDOWN:
  1965. case WM_LBUTTONUP:
  1966. case WM_LBUTTONDBLCLK:
  1967. case WM_RBUTTONDOWN:
  1968. case WM_RBUTTONUP:
  1969. {
  1970. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1971. CControlUI* pHover = GetManager()->FindControl(pt);
  1972. if(pHover != this) {
  1973. bWasHandled = false;
  1974. return 0;
  1975. }
  1976. }
  1977. break;
  1978. }
  1979. }
  1980. // Mouse message only go when captured or inside rect
  1981. DWORD dwHitResult = m_pTwh->IsCaptured() ? HITRESULT_HIT : HITRESULT_OUTSIDE;
  1982. if( dwHitResult == HITRESULT_OUTSIDE ) {
  1983. RECT rc;
  1984. m_pTwh->GetControlRect(&rc);
  1985. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1986. if( uMsg == WM_SETCURSOR ) {
  1987. ::GetCursorPos(&pt);
  1988. ::ScreenToClient(GetManager()->GetPaintWindow(), &pt);
  1989. }
  1990. else if( uMsg == WM_MOUSEWHEEL ) ::ScreenToClient(GetManager()->GetPaintWindow(), &pt);
  1991. if( ::PtInRect(&rc, pt) && !GetManager()->IsCaptured() ) dwHitResult = HITRESULT_HIT;
  1992. }
  1993. if( dwHitResult != HITRESULT_HIT ) return 0;
  1994. if( uMsg == WM_SETCURSOR ) bWasHandled = false;
  1995. else if( uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK || uMsg == WM_RBUTTONDOWN ) {
  1996. ::SetFocus(GetManager()->GetPaintWindow());
  1997. SetFocus();
  1998. }
  1999. }
  2000. #ifdef _UNICODE
  2001. else if( uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST ) {
  2002. #else
  2003. else if( (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST) || uMsg == WM_CHAR || uMsg == WM_IME_CHAR ) {
  2004. #endif
  2005. if( !IsFocused() ) return 0;
  2006. }
  2007. else if( uMsg == WM_CONTEXTMENU ) {
  2008. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2009. ::ScreenToClient(GetManager()->GetPaintWindow(), &pt);
  2010. CControlUI* pHover = GetManager()->FindControl(pt);
  2011. if(pHover != this) {
  2012. bWasHandled = false;
  2013. return 0;
  2014. }
  2015. }
  2016. else
  2017. {
  2018. switch( uMsg ) {
  2019. case WM_HELP:
  2020. bWasHandled = false;
  2021. break;
  2022. default:
  2023. return 0;
  2024. }
  2025. }
  2026. LRESULT lResult = 0;
  2027. HRESULT Hr = TxSendMessage(uMsg, wParam, lParam, &lResult);
  2028. if( Hr == S_OK ) bHandled = bWasHandled;
  2029. else if( (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST) || uMsg == WM_CHAR || uMsg == WM_IME_CHAR )
  2030. bHandled = bWasHandled;
  2031. else if( uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST ) {
  2032. if( m_pTwh->IsCaptured() ) bHandled = bWasHandled;
  2033. }
  2034. return lResult;
  2035. }
  2036. } // namespace DuiLib