UIEdit.cpp 18 KB

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