UIRichEdit.cpp 66 KB

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