UIEdit.cpp 19 KB

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