UIEdit.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include "stdafx.h"
  2. #include "UIEdit.h"
  3. namespace DuiLib
  4. {
  5. class CEditWnd : public CWindowWnd
  6. {
  7. public:
  8. CEditWnd();
  9. void Init(CEditUI* pOwner);
  10. RECT CalPos();
  11. LPCTSTR GetWindowClassName() const;
  12. LPCTSTR GetSuperClassName() const;
  13. void OnFinalMessage(HWND hWnd);
  14. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  15. LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  16. LRESULT OnEditChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  17. protected:
  18. enum {
  19. DEFAULT_TIMERID = 20,
  20. };
  21. CEditUI* m_pOwner;
  22. HBRUSH m_hBkBrush;
  23. bool m_bInit;
  24. bool m_bDrawCaret;
  25. };
  26. CEditWnd::CEditWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false), m_bDrawCaret(false)
  27. {
  28. }
  29. void CEditWnd::Init(CEditUI* pOwner)
  30. {
  31. m_pOwner = pOwner;
  32. RECT rcPos = CalPos();
  33. UINT uStyle = WS_CHILD | ES_AUTOHSCROLL | pOwner->GetWindowStyls();
  34. if( m_pOwner->IsPasswordMode() ) uStyle |= ES_PASSWORD;
  35. Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
  36. HFONT hFont=NULL;
  37. int iFontIndex=m_pOwner->GetFont();
  38. if (iFontIndex!=-1)
  39. hFont=m_pOwner->GetManager()->GetFont(iFontIndex);
  40. if (hFont==NULL)
  41. hFont=m_pOwner->GetManager()->GetDefaultFontInfo()->hFont;
  42. SetWindowFont(m_hWnd, hFont, TRUE);
  43. Edit_LimitText(m_hWnd, m_pOwner->GetMaxChar());
  44. if( m_pOwner->IsPasswordMode() ) Edit_SetPasswordChar(m_hWnd, m_pOwner->GetPasswordChar());
  45. Edit_SetText(m_hWnd, m_pOwner->GetText());
  46. Edit_SetModify(m_hWnd, FALSE);
  47. SendMessage(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELPARAM(0, 0));
  48. Edit_Enable(m_hWnd, m_pOwner->IsEnabled() == true);
  49. Edit_SetReadOnly(m_hWnd, m_pOwner->IsReadOnly() == true);
  50. //Styls
  51. ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  52. ::SetFocus(m_hWnd);
  53. if (m_pOwner->IsAutoSelAll()) {
  54. int nSize = GetWindowTextLength(m_hWnd);
  55. if( nSize == 0 ) nSize = 1;
  56. Edit_SetSel(m_hWnd, 0, nSize);
  57. }
  58. else {
  59. int nSize = GetWindowTextLength(m_hWnd);
  60. Edit_SetSel(m_hWnd, nSize, nSize);
  61. }
  62. m_bInit = true;
  63. }
  64. RECT CEditWnd::CalPos()
  65. {
  66. CDuiRect rcPos = m_pOwner->GetPos();
  67. RECT rcInset = m_pOwner->GetTextPadding();
  68. rcPos.left += rcInset.left;
  69. rcPos.top += rcInset.top;
  70. rcPos.right -= rcInset.right;
  71. rcPos.bottom -= rcInset.bottom;
  72. LONG lEditHeight = m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->tm.tmHeight;
  73. if( lEditHeight < rcPos.GetHeight() ) {
  74. rcPos.top += (rcPos.GetHeight() - lEditHeight) / 2;
  75. rcPos.bottom = rcPos.top + lEditHeight;
  76. }
  77. CControlUI* pParent = m_pOwner;
  78. RECT rcParent;
  79. while( pParent = pParent->GetParent() ) {
  80. if( !pParent->IsVisible() ) {
  81. rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0;
  82. break;
  83. }
  84. rcParent = pParent->GetClientPos();
  85. if( !::IntersectRect(&rcPos, &rcPos, &rcParent) ) {
  86. rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0;
  87. break;
  88. }
  89. }
  90. return rcPos;
  91. }
  92. LPCTSTR CEditWnd::GetWindowClassName() const
  93. {
  94. return _T("EditWnd");
  95. }
  96. LPCTSTR CEditWnd::GetSuperClassName() const
  97. {
  98. return WC_EDIT;
  99. }
  100. void CEditWnd::OnFinalMessage(HWND hWnd)
  101. {
  102. m_pOwner->Invalidate();
  103. // Clear reference and die
  104. if( m_hBkBrush != NULL ) ::DeleteObject(m_hBkBrush);
  105. m_pOwner->GetManager()->RemoveNativeWindow(hWnd);
  106. m_pOwner->m_pWindow = NULL;
  107. delete this;
  108. }
  109. LRESULT CEditWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  110. {
  111. LRESULT lRes = 0;
  112. BOOL bHandled = TRUE;
  113. if( uMsg == WM_CREATE ) {
  114. m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd);
  115. if( m_pOwner->GetManager()->IsLayered() ) {
  116. ::SetTimer(m_hWnd, DEFAULT_TIMERID, ::GetCaretBlinkTime(), NULL);
  117. }
  118. bHandled = FALSE;
  119. }
  120. else if( uMsg == WM_KILLFOCUS ) lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
  121. else if( uMsg == OCM_COMMAND ) {
  122. if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE ) lRes = OnEditChanged(uMsg, wParam, lParam, bHandled);
  123. else if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_UPDATE ) {
  124. RECT rcClient;
  125. ::GetClientRect(m_hWnd, &rcClient);
  126. ::InvalidateRect(m_hWnd, &rcClient, FALSE);
  127. }
  128. }
  129. else if( uMsg == WM_KEYDOWN && TCHAR(wParam) == VK_RETURN ) {
  130. m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_RETURN);
  131. }
  132. else if( uMsg == OCM__BASE + WM_CTLCOLOREDIT || uMsg == OCM__BASE + WM_CTLCOLORSTATIC ) {
  133. if (m_pOwner->GetManager()->IsLayered() && !m_pOwner->GetManager()->IsPainting()) {
  134. m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd);
  135. }
  136. if( m_pOwner->GetNativeEditBkColor() == 0xFFFFFFFF ) return NULL;
  137. ::SetBkMode((HDC)wParam, TRANSPARENT);
  138. DWORD dwTextColor = m_pOwner->GetTextColor();
  139. ::SetTextColor((HDC)wParam, RGB(GetBValue(dwTextColor),GetGValue(dwTextColor),GetRValue(dwTextColor)));
  140. if( m_hBkBrush == NULL ) {
  141. DWORD clrColor = m_pOwner->GetNativeEditBkColor();
  142. m_hBkBrush = ::CreateSolidBrush(RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  143. }
  144. return (LRESULT)m_hBkBrush;
  145. }
  146. else if( uMsg == WM_PAINT) {
  147. if (m_pOwner->GetManager()->IsLayered()) {
  148. m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd);
  149. }
  150. bHandled = FALSE;
  151. }
  152. else if( uMsg == WM_PRINT ) {
  153. if (m_pOwner->GetManager()->IsLayered()) {
  154. lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  155. if( m_pOwner->IsEnabled() && m_bDrawCaret ) { // todo:ÅжÏÊÇ·ñenabled
  156. RECT rcClient;
  157. ::GetClientRect(m_hWnd, &rcClient);
  158. POINT ptCaret;
  159. ::GetCaretPos(&ptCaret);
  160. RECT rcCaret = { ptCaret.x, ptCaret.y, ptCaret.x, ptCaret.y+rcClient.bottom-rcClient.top };
  161. CRenderEngine::DrawLine((HDC)wParam, rcCaret, 1, 0xFF000000);
  162. }
  163. return lRes;
  164. }
  165. bHandled = FALSE;
  166. }
  167. else if( uMsg == WM_TIMER ) {
  168. if (wParam == DEFAULT_TIMERID) {
  169. m_bDrawCaret = !m_bDrawCaret;
  170. RECT rcClient;
  171. ::GetClientRect(m_hWnd, &rcClient);
  172. ::InvalidateRect(m_hWnd, &rcClient, FALSE);
  173. return 0;
  174. }
  175. bHandled = FALSE;
  176. }
  177. else bHandled = FALSE;
  178. if( !bHandled ) return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  179. return lRes;
  180. }
  181. LRESULT CEditWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  182. {
  183. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  184. if ((HWND)wParam != m_pOwner->GetManager()->GetPaintWindow()) {
  185. ::SendMessage(m_pOwner->GetManager()->GetPaintWindow(), WM_KILLFOCUS, wParam, lParam);
  186. }
  187. SendMessage(WM_CLOSE);
  188. return lRes;
  189. }
  190. LRESULT CEditWnd::OnEditChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  191. {
  192. if( !m_bInit ) return 0;
  193. if( m_pOwner == NULL ) return 0;
  194. // Copy text back
  195. int cchLen = ::GetWindowTextLength(m_hWnd) + 1;
  196. LPTSTR pstr = static_cast<LPTSTR>(_alloca(cchLen * sizeof(TCHAR)));
  197. ASSERT(pstr);
  198. if( pstr == NULL ) return 0;
  199. ::GetWindowText(m_hWnd, pstr, cchLen);
  200. m_pOwner->m_sText = pstr;
  201. m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_TEXTCHANGED);
  202. if( m_pOwner->GetManager()->IsLayered() ) m_pOwner->Invalidate();
  203. return 0;
  204. }
  205. /////////////////////////////////////////////////////////////////////////////////////
  206. //
  207. //
  208. CEditUI::CEditUI() : m_pWindow(NULL), m_uMaxChar(255), m_bReadOnly(false),
  209. m_bPasswordMode(false), m_cPasswordChar(_T('*')), m_bAutoSelAll(false), m_uButtonState(0),
  210. m_dwEditbkColor(0xFFFFFFFF), m_iWindowStyls(0)
  211. {
  212. SetTextPadding(CDuiRect(4, 3, 4, 3));
  213. SetBkColor(0xFFFFFFFF);
  214. }
  215. LPCTSTR CEditUI::GetClass() const
  216. {
  217. return _T("EditUI");
  218. }
  219. LPVOID CEditUI::GetInterface(LPCTSTR pstrName)
  220. {
  221. if( _tcscmp(pstrName, DUI_CTR_EDIT) == 0 ) return static_cast<CEditUI*>(this);
  222. return CLabelUI::GetInterface(pstrName);
  223. }
  224. UINT CEditUI::GetControlFlags() const
  225. {
  226. if( !IsEnabled() ) return CControlUI::GetControlFlags();
  227. return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
  228. }
  229. HWND CEditUI::GetNativeWindow() const
  230. {
  231. if (m_pWindow) return m_pWindow->GetHWND();
  232. return NULL;
  233. }
  234. void CEditUI::DoEvent(TEventUI& event)
  235. {
  236. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  237. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  238. else CLabelUI::DoEvent(event);
  239. return;
  240. }
  241. if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
  242. {
  243. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
  244. return;
  245. }
  246. if( event.Type == UIEVENT_WINDOWSIZE )
  247. {
  248. if( m_pWindow != NULL ) m_pManager->SetFocusNeeded(this);
  249. }
  250. if( event.Type == UIEVENT_SCROLLWHEEL )
  251. {
  252. if( m_pWindow != NULL ) return;
  253. }
  254. if( event.Type == UIEVENT_SETFOCUS && IsEnabled() )
  255. {
  256. if( m_pWindow ) return;
  257. m_pWindow = new CEditWnd();
  258. ASSERT(m_pWindow);
  259. m_pWindow->Init(this);
  260. Invalidate();
  261. }
  262. if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() )
  263. {
  264. Invalidate();
  265. }
  266. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
  267. {
  268. if( IsEnabled() ) {
  269. GetManager()->ReleaseCapture();
  270. if( IsFocused() && m_pWindow == NULL )
  271. {
  272. m_pWindow = new CEditWnd();
  273. ASSERT(m_pWindow);
  274. m_pWindow->Init(this);
  275. }
  276. else if( m_pWindow != NULL )
  277. {
  278. if (!m_bAutoSelAll) {
  279. POINT pt = event.ptMouse;
  280. pt.x -= m_rcItem.left + m_rcTextPadding.left;
  281. pt.y -= m_rcItem.top + m_rcTextPadding.top;
  282. ::SendMessage(*m_pWindow, WM_LBUTTONDOWN, event.wParam, MAKELPARAM(pt.x, pt.y));
  283. }
  284. }
  285. }
  286. return;
  287. }
  288. if( event.Type == UIEVENT_MOUSEMOVE )
  289. {
  290. return;
  291. }
  292. if( event.Type == UIEVENT_BUTTONUP )
  293. {
  294. return;
  295. }
  296. if( event.Type == UIEVENT_CONTEXTMENU )
  297. {
  298. return;
  299. }
  300. if( event.Type == UIEVENT_MOUSEENTER )
  301. {
  302. if( IsEnabled() ) {
  303. m_uButtonState |= UISTATE_HOT;
  304. Invalidate();
  305. }
  306. return;
  307. }
  308. if( event.Type == UIEVENT_MOUSELEAVE )
  309. {
  310. if( IsEnabled() ) {
  311. m_uButtonState &= ~UISTATE_HOT;
  312. Invalidate();
  313. }
  314. return;
  315. }
  316. CLabelUI::DoEvent(event);
  317. }
  318. void CEditUI::SetEnabled(bool bEnable)
  319. {
  320. CControlUI::SetEnabled(bEnable);
  321. if( !IsEnabled() ) {
  322. m_uButtonState = 0;
  323. }
  324. }
  325. void CEditUI::SetText(LPCTSTR pstrText)
  326. {
  327. m_sText = pstrText;
  328. if( m_pWindow != NULL ) Edit_SetText(*m_pWindow, m_sText);
  329. Invalidate();
  330. }
  331. void CEditUI::SetMaxChar(UINT uMax)
  332. {
  333. m_uMaxChar = uMax;
  334. if( m_pWindow != NULL ) Edit_LimitText(*m_pWindow, m_uMaxChar);
  335. }
  336. UINT CEditUI::GetMaxChar()
  337. {
  338. return m_uMaxChar;
  339. }
  340. void CEditUI::SetReadOnly(bool bReadOnly)
  341. {
  342. if( m_bReadOnly == bReadOnly ) return;
  343. m_bReadOnly = bReadOnly;
  344. if( m_pWindow != NULL ) Edit_SetReadOnly(*m_pWindow, m_bReadOnly);
  345. Invalidate();
  346. }
  347. bool CEditUI::IsReadOnly() const
  348. {
  349. return m_bReadOnly;
  350. }
  351. void CEditUI::SetNumberOnly(bool bNumberOnly)
  352. {
  353. if( bNumberOnly )
  354. {
  355. m_iWindowStyls |= ES_NUMBER;
  356. }
  357. else
  358. {
  359. m_iWindowStyls &= ~ES_NUMBER;
  360. }
  361. }
  362. bool CEditUI::IsNumberOnly() const
  363. {
  364. return m_iWindowStyls&ES_NUMBER ? true:false;
  365. }
  366. int CEditUI::GetWindowStyls() const
  367. {
  368. return m_iWindowStyls;
  369. }
  370. HWND CEditUI::GetNativeEditHWND() const
  371. {
  372. if (m_pWindow == NULL) return NULL;
  373. return m_pWindow->GetHWND();
  374. }
  375. void CEditUI::SetPasswordMode(bool bPasswordMode)
  376. {
  377. if( m_bPasswordMode == bPasswordMode ) return;
  378. m_bPasswordMode = bPasswordMode;
  379. Invalidate();
  380. }
  381. bool CEditUI::IsPasswordMode() const
  382. {
  383. return m_bPasswordMode;
  384. }
  385. void CEditUI::SetPasswordChar(TCHAR cPasswordChar)
  386. {
  387. if( m_cPasswordChar == cPasswordChar ) return;
  388. m_cPasswordChar = cPasswordChar;
  389. if( m_pWindow != NULL ) Edit_SetPasswordChar(*m_pWindow, m_cPasswordChar);
  390. Invalidate();
  391. }
  392. TCHAR CEditUI::GetPasswordChar() const
  393. {
  394. return m_cPasswordChar;
  395. }
  396. bool CEditUI::IsAutoSelAll()
  397. {
  398. return m_bAutoSelAll;
  399. }
  400. void CEditUI::SetAutoSelAll(bool bAutoSelAll)
  401. {
  402. m_bAutoSelAll = bAutoSelAll;
  403. }
  404. LPCTSTR CEditUI::GetNormalImage()
  405. {
  406. return m_diNormal.sDrawString;
  407. }
  408. void CEditUI::SetNormalImage(LPCTSTR pStrImage)
  409. {
  410. if( m_diNormal.sDrawString == pStrImage && m_diNormal.pImageInfo != NULL ) return;
  411. m_diNormal.Clear();
  412. m_diNormal.sDrawString = pStrImage;
  413. Invalidate();
  414. }
  415. LPCTSTR CEditUI::GetHotImage()
  416. {
  417. return m_diHot.sDrawString;
  418. }
  419. void CEditUI::SetHotImage(LPCTSTR pStrImage)
  420. {
  421. if( m_diHot.sDrawString == pStrImage && m_diHot.pImageInfo != NULL ) return;
  422. m_diHot.Clear();
  423. m_diHot.sDrawString = pStrImage;
  424. Invalidate();
  425. }
  426. LPCTSTR CEditUI::GetFocusedImage()
  427. {
  428. return m_diFocused.sDrawString;
  429. }
  430. void CEditUI::SetFocusedImage(LPCTSTR pStrImage)
  431. {
  432. if( m_diFocused.sDrawString == pStrImage && m_diFocused.pImageInfo != NULL ) return;
  433. m_diFocused.Clear();
  434. m_diFocused.sDrawString = pStrImage;
  435. Invalidate();
  436. }
  437. LPCTSTR CEditUI::GetDisabledImage()
  438. {
  439. return m_diDisabled.sDrawString;
  440. }
  441. void CEditUI::SetDisabledImage(LPCTSTR pStrImage)
  442. {
  443. if( m_diDisabled.sDrawString == pStrImage && m_diDisabled.pImageInfo != NULL ) return;
  444. m_diDisabled.Clear();
  445. m_diDisabled.sDrawString = pStrImage;
  446. Invalidate();
  447. }
  448. void CEditUI::SetNativeEditBkColor(DWORD dwBkColor)
  449. {
  450. m_dwEditbkColor = dwBkColor;
  451. }
  452. DWORD CEditUI::GetNativeEditBkColor() const
  453. {
  454. return m_dwEditbkColor;
  455. }
  456. void CEditUI::SetSel(long nStartChar, long nEndChar)
  457. {
  458. if( m_pWindow != NULL ) Edit_SetSel(*m_pWindow, nStartChar,nEndChar);
  459. }
  460. void CEditUI::SetSelAll()
  461. {
  462. SetSel(0,-1);
  463. }
  464. void CEditUI::SetReplaceSel(LPCTSTR lpszReplace)
  465. {
  466. if( m_pWindow != NULL ) Edit_ReplaceSel(*m_pWindow, lpszReplace);
  467. }
  468. void CEditUI::SetPos(RECT rc, bool bNeedInvalidate)
  469. {
  470. CControlUI::SetPos(rc, bNeedInvalidate);
  471. if( m_pWindow != NULL ) {
  472. RECT rcPos = m_pWindow->CalPos();
  473. ::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
  474. rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE);
  475. }
  476. }
  477. void CEditUI::Move(SIZE szOffset, bool bNeedInvalidate)
  478. {
  479. CControlUI::Move(szOffset, bNeedInvalidate);
  480. if( m_pWindow != NULL ) {
  481. RECT rcPos = m_pWindow->CalPos();
  482. ::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
  483. rcPos.bottom - rcPos.top, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  484. }
  485. }
  486. void CEditUI::SetVisible(bool bVisible)
  487. {
  488. CControlUI::SetVisible(bVisible);
  489. if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
  490. }
  491. void CEditUI::SetInternVisible(bool bVisible)
  492. {
  493. if( !IsVisible() && m_pWindow != NULL ) m_pManager->SetFocus(NULL);
  494. }
  495. SIZE CEditUI::EstimateSize(SIZE szAvailable)
  496. {
  497. if( m_cxyFixed.cy == 0 ) return CDuiSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 6);
  498. return CControlUI::EstimateSize(szAvailable);
  499. }
  500. void CEditUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  501. {
  502. if( _tcscmp(pstrName, _T("readonly")) == 0 ) SetReadOnly(_tcscmp(pstrValue, _T("true")) == 0);
  503. else if( _tcscmp(pstrName, _T("numberonly")) == 0 ) SetNumberOnly(_tcscmp(pstrValue, _T("true")) == 0);
  504. else if( _tcscmp(pstrName, _T("password")) == 0 ) SetPasswordMode(_tcscmp(pstrValue, _T("true")) == 0);
  505. else if( _tcscmp(pstrName, _T("autoselall")) == 0 ) SetAutoSelAll(_tcscmp(pstrValue, _T("true")) == 0);
  506. else if( _tcscmp(pstrName, _T("maxchar")) == 0 ) SetMaxChar(_ttoi(pstrValue));
  507. else if( _tcscmp(pstrName, _T("normalimage")) == 0 ) SetNormalImage(pstrValue);
  508. else if( _tcscmp(pstrName, _T("hotimage")) == 0 ) SetHotImage(pstrValue);
  509. else if( _tcscmp(pstrName, _T("focusedimage")) == 0 ) SetFocusedImage(pstrValue);
  510. else if( _tcscmp(pstrName, _T("disabledimage")) == 0 ) SetDisabledImage(pstrValue);
  511. else if( _tcscmp(pstrName, _T("nativebkcolor")) == 0 ) {
  512. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  513. LPTSTR pstr = NULL;
  514. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  515. SetNativeEditBkColor(clrColor);
  516. }
  517. else CLabelUI::SetAttribute(pstrName, pstrValue);
  518. }
  519. void CEditUI::PaintStatusImage(HDC hDC)
  520. {
  521. if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
  522. else m_uButtonState &= ~ UISTATE_FOCUSED;
  523. if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
  524. else m_uButtonState &= ~ UISTATE_DISABLED;
  525. if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
  526. if( DrawImage(hDC, m_diDisabled) ) return;
  527. }
  528. else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
  529. if( DrawImage(hDC, m_diFocused) ) return;
  530. }
  531. else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  532. if( DrawImage(hDC, m_diHot) ) return;
  533. }
  534. if( DrawImage(hDC, m_diNormal) ) return;
  535. }
  536. void CEditUI::PaintText(HDC hDC)
  537. {
  538. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  539. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  540. if( m_sText.IsEmpty() ) return;
  541. CDuiString sText = m_sText;
  542. if( m_bPasswordMode ) {
  543. sText.Empty();
  544. LPCTSTR p = m_sText.GetData();
  545. while( *p != _T('\0') ) {
  546. sText += m_cPasswordChar;
  547. p = ::CharNext(p);
  548. }
  549. }
  550. RECT rc = m_rcItem;
  551. rc.left += m_rcTextPadding.left;
  552. rc.right -= m_rcTextPadding.right;
  553. rc.top += m_rcTextPadding.top;
  554. rc.bottom -= m_rcTextPadding.bottom;
  555. if( IsEnabled() ) {
  556. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, \
  557. m_iFont, DT_SINGLELINE | m_uTextStyle);
  558. }
  559. else {
  560. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, \
  561. m_iFont, DT_SINGLELINE | m_uTextStyle);
  562. }
  563. }
  564. }