XTPControlEdit.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:20k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPControlEdit.cpp : implementation of the CXTPControlEdit class.
  2. //
  3. // This file is a part of the XTREME COMMANDBARS MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "Common/XTPDrawHelpers.h"
  22. #include "Common/XTPColorManager.h"
  23. #include "Common/XTPToolTipContext.h"
  24. #include "Common/XTPImageManager.h"
  25. #include "XTPControlEdit.h"
  26. #include "XTPCommandBar.h"
  27. #include "XTPToolBar.h"
  28. #include "XTPCommandBars.h"
  29. #include "XTPPaintManager.h"
  30. #include "XTPMouseManager.h"
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. // CXTPControlEdit
  37. IMPLEMENT_XTP_CONTROL(CXTPControlEdit, CXTPControl)
  38. CXTPControlEdit::CXTPControlEdit()
  39. {
  40. EnableAutomation();
  41. m_controlType = xtpControlEdit;
  42. m_nWidth = 100;
  43. m_nLabelWidth = 0;
  44. m_bLabel = FALSE;
  45. m_strEditText = _T("");
  46. m_pEdit = NULL;
  47. m_bReadOnly = FALSE;
  48. m_bDelayReposition = FALSE;
  49. m_bDelayDestroy = FALSE;
  50. m_bFocused = FALSE;
  51. m_pAutoCompleteWnd = NULL;
  52. m_dwShellAutoCompleteFlags = 0;
  53. m_bEditChanged = TRUE;
  54. m_dwEditStyle = 0;
  55. m_nEditIconId = 0;
  56. m_bShowSpinButtons = FALSE;
  57. }
  58. CXTPControlEdit::~CXTPControlEdit()
  59. {
  60. if (m_pEdit)
  61. {
  62. m_pEdit->m_pControl = NULL;
  63. delete m_pEdit;
  64. }
  65. SAFE_DELETE(m_pAutoCompleteWnd);
  66. }
  67. void CXTPControlEdit::OnEditChanged()
  68. {
  69. NotifySite(EN_CHANGE);
  70. }
  71. CString CXTPControlEdit::GetEditText() const
  72. {
  73. CString strEditText = _GetEditText();
  74. return strEditText == GetEditHint() ? _T("") : strEditText;
  75. }
  76. CString CXTPControlEdit::_GetEditText() const
  77. {
  78. if (m_pEdit && m_pEdit->GetSafeHwnd() && m_bEditChanged)
  79. {
  80. m_pEdit->GetWindowTextEx(m_strEditText);
  81. m_bEditChanged = FALSE;
  82. }
  83. return m_strEditText;
  84. }
  85. void CXTPControlEdit::SetEditText(const CString& strText)
  86. {
  87. _SetEditText(strText.IsEmpty() ? GetEditHint() : strText);
  88. }
  89. void CXTPControlEdit::_SetEditText(const CString& strText)
  90. {
  91. if (m_pEdit && m_pEdit->GetSafeHwnd() && _GetEditText() != strText)
  92. {
  93. m_pEdit->SetWindowTextEx(strText);
  94. }
  95. m_strEditText = strText;
  96. m_bEditChanged = FALSE;
  97. }
  98. BOOL CXTPControlEdit::OnHookKeyDown(UINT nChar, LPARAM lParam)
  99. {
  100. ASSERT(IsFocused());
  101. if (!IsFocused() || !m_pEdit->GetSafeHwnd())
  102. return FALSE;
  103. if (m_pEdit && m_pEdit->m_bImeMode)
  104. return FALSE_EXIT;
  105. if (nChar == VK_RETURN || nChar == VK_TAB)
  106. return FALSE;
  107. if (nChar == VK_ESCAPE)
  108. {
  109. SetEditText(m_strLastText);
  110. SetFocused(FALSE);
  111. return GetParent()->GetType() == xtpBarTypePopup;
  112. }
  113. if (m_pEdit && m_pEdit->GetSafeHwnd() && GetKeyState(VK_MENU) < 0 && GetKeyState(VK_CONTROL) >= 0)
  114. {
  115. CXTPCommandBars* pCommandBars = m_pParent->GetCommandBars();
  116. if (pCommandBars && pCommandBars->OnFrameAccel((TCHAR)nChar))
  117. return TRUE;
  118. }
  119. if (GetEnabled() && IsSpinButtonsVisible() && (nChar == VK_UP || nChar == VK_DOWN) && !GetReadOnly())
  120. {
  121. NotifySpinChanged(1, nChar == VK_UP ? +1 : -1);
  122. if (m_pEdit && ::GetFocus() == m_pEdit->GetSafeHwnd()) m_pEdit->SetSel(0, -1);
  123. return TRUE;
  124. }
  125. if (m_pAutoCompleteWnd && m_pAutoCompleteWnd->IsDialogCode(nChar, lParam))
  126. return TRUE;
  127. if (m_pEdit->IsDialogCode(nChar, lParam))
  128. return TRUE;
  129. return FALSE_EXIT;
  130. }
  131. CXTPControlEditCtrl* CXTPControlEdit::CreateEditControl()
  132. {
  133. return new CXTPControlEditCtrl();
  134. }
  135. void CXTPControlEdit::EnableShellAutoComplete(DWORD dwFlags /*= SHACF_FILESYSTEM | SHACF_URLALL*/)
  136. {
  137. m_dwShellAutoCompleteFlags = dwFlags;
  138. SAFE_DELETE(m_pAutoCompleteWnd);
  139. if (m_pEdit->GetSafeHwnd() && dwFlags)
  140. {
  141. m_pAutoCompleteWnd = new CXTPControlComboBoxAutoCompleteWnd();
  142. if (FAILED(m_pAutoCompleteWnd->ShellAutoComplete(m_pEdit->GetSafeHwnd(), m_dwShellAutoCompleteFlags)))
  143. {
  144. SAFE_DELETE(m_pAutoCompleteWnd);
  145. }
  146. }
  147. }
  148. BOOL CXTPControlEdit::HasFocus() const
  149. {
  150. return IsFocused();
  151. }
  152. BOOL CXTPControlEdit::IsFocusable() const
  153. {
  154. return m_pEdit && m_pEdit->GetSafeHwnd();
  155. }
  156. void CXTPControlEdit::SetRect(CRect rcControl)
  157. {
  158. CString strEditText = GetEditText();
  159. if (!m_pEdit)
  160. {
  161. m_pEdit = CreateEditControl();
  162. m_pEdit->m_pControl = this;
  163. }
  164. if (m_bDelayDestroy && m_pEdit && m_pEdit->GetSafeHwnd())
  165. {
  166. m_pEdit->DestroyWindow();
  167. m_bDelayDestroy = FALSE;
  168. }
  169. if (m_pEdit && m_pEdit->GetSafeHwnd() && m_pEdit->GetExStyle() & WS_EX_LAYOUTRTL)
  170. {
  171. m_pEdit->DestroyWindow();
  172. }
  173. if (m_pEdit && m_pEdit->GetSafeHwnd() && m_pEdit->GetParent() != m_pParent)
  174. {
  175. m_pEdit->DestroyWindow();
  176. }
  177. if (m_rcControl == rcControl && !(m_pEdit && !m_pEdit->GetSafeHwnd()) && (m_bDelayReposition == FALSE))
  178. {
  179. if (!(m_pEdit && m_pEdit->GetSafeHwnd() && m_pEdit->GetParent() != m_pParent))
  180. return;
  181. }
  182. m_rcControl = rcControl;
  183. if (m_pEdit)
  184. {
  185. if (!m_pEdit->GetSafeHwnd())
  186. {
  187. m_pEdit->CreateEdit(WS_CHILD | ES_AUTOHSCROLL | m_dwEditStyle, m_pParent);
  188. if (!GetEnabled()) m_pEdit->EnableWindow(FALSE);
  189. if (m_dwShellAutoCompleteFlags != 0) EnableShellAutoComplete(m_dwShellAutoCompleteFlags);
  190. }
  191. m_pEdit->SetFont(GetPaintManager()->GetIconFont(), FALSE);
  192. m_pEdit->SetMargins(0, 0);
  193. m_pEdit->UpdateCharFormat();
  194. if (m_pEdit->GetSafeHwnd() != ::GetFocus()) SetEditText(strEditText);
  195. m_pEdit->m_bComposited = HasDwmCompositedRect();
  196. m_pEdit->SetReadOnly(m_bReadOnly);
  197. rcControl.DeflateRect(m_nLabelWidth + 1 + 3, 3, 3 + (m_bShowSpinButtons ? 17 : 0), 3);
  198. DeflateEditRect(rcControl);
  199. m_pEdit->MoveWindow(rcControl);
  200. ShowHideEditControl();
  201. }
  202. m_bDelayReposition = FALSE;
  203. }
  204. void CXTPControlEdit::DrawEditText(CDC* pDC, CRect rcText)
  205. {
  206. if (m_nEditIconId > 0)
  207. {
  208. CXTPImageManagerIcon* pIcon = m_pParent->GetImageManager()->GetImage(m_nEditIconId, 0);
  209. if (pIcon)
  210. {
  211. pIcon->Draw(pDC, CPoint(rcText.left, rcText.CenterPoint().y - pIcon->GetHeight() / 2), GetEnabled() ? pIcon->GetIcon() : pIcon->GetDisabledIcon());
  212. }
  213. }
  214. }
  215. void CXTPControlEdit::DeflateEditRect(CRect& rcControl)
  216. {
  217. if (m_nEditIconId > 0)
  218. {
  219. CXTPImageManagerIcon* pIcon = m_pParent->GetImageManager()->GetImage(m_nEditIconId, 0);
  220. if (pIcon)
  221. rcControl.left += pIcon->GetWidth() + 2;
  222. }
  223. }
  224. void CXTPControlEdit::ShowHideEditControl()
  225. {
  226. if (m_pEdit && m_pEdit->GetSafeHwnd())
  227. {
  228. m_pEdit->SetWindowPos(0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE
  229. | (m_pControls && m_pParent && IsVisible() && m_pParent->GetSafeHwnd() ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
  230. }
  231. }
  232. void CXTPControlEdit::OnRemoved()
  233. {
  234. ShowHideEditControl();
  235. if (!m_pControls && m_pEdit && m_pEdit->GetSafeHwnd())
  236. {
  237. m_pEdit->DestroyWindow();
  238. }
  239. CXTPControl::OnRemoved();
  240. }
  241. void CXTPControlEdit::SetReadOnly(BOOL bReadOnly /* = TRUE */)
  242. {
  243. m_bReadOnly = bReadOnly;
  244. if (m_pEdit && m_pEdit->GetSafeHwnd())
  245. {
  246. m_pEdit->SetReadOnly(bReadOnly);
  247. }
  248. }
  249. BOOL CXTPControlEdit::GetReadOnly() const
  250. {
  251. return m_bReadOnly;
  252. }
  253. void CXTPControlEdit::SetEnabled(BOOL bEnabled)
  254. {
  255. if (bEnabled != m_bEnabled)
  256. {
  257. m_bEnabled = bEnabled;
  258. if (m_pEdit && m_pEdit->GetSafeHwnd())
  259. {
  260. m_pEdit->EnableWindow(GetEnabled());
  261. m_pEdit->UpdateCharFormat();
  262. }
  263. RedrawParent();
  264. }
  265. }
  266. void CXTPControlEdit::SetParent(CXTPCommandBar* pParent)
  267. {
  268. CXTPControl::SetParent(pParent);
  269. if (!pParent && m_pEdit && m_pEdit->GetSafeHwnd())
  270. {
  271. m_pEdit->DestroyWindow();
  272. }
  273. }
  274. BOOL  CXTPControlEdit::OnSetSelected(int bSelected)
  275. {
  276. if (!CXTPControl::OnSetSelected(bSelected))
  277. return FALSE;
  278. if (!bSelected)
  279. {
  280. SetFocused(FALSE);
  281. }
  282. if (m_pEdit->GetSafeHwnd())
  283. {
  284. m_pEdit->UpdateCharFormat();
  285. }
  286. return TRUE;
  287. }
  288. void CXTPControlEdit::OnUnderlineActivate()
  289. {
  290. m_pParent->HideKeyboardTips();
  291. SetFocused(TRUE);
  292. }
  293. void CXTPControlEdit::OnClick(BOOL bKeyboard, CPoint pt)
  294. {
  295. if (IsCustomizeMode())
  296. {
  297. m_pParent->SetPopuped(-1);
  298. m_pParent->SetSelected(-1);
  299. CustomizeStartDrag(pt);
  300. }
  301. else
  302. {
  303. if (bKeyboard && CWnd::GetFocus() == m_pEdit)
  304. {
  305. OnExecute();
  306. }
  307. else if (!bKeyboard && GetSpinButtonsRect().PtInRect(pt) && GetEnabled() && !GetReadOnly())
  308. {
  309. TrackSpinButton(pt);
  310. }
  311. else
  312. {
  313. m_pEdit->SetFocus();
  314. }
  315. }
  316. }
  317. void CXTPControlEdit::NotifySpinChanged(int increment, int direction)
  318. {
  319. NMXTPUPDOWN nm;
  320. nm.iDelta = increment * direction;
  321. if (NotifySite(direction > 0 ? XTP_FN_SPINUP : XTP_FN_SPINDOWN, &nm) != 0)
  322. return;
  323. int nPos = _ttoi(GetEditText());
  324. nPos += nm.iDelta;
  325. while (nPos % increment)
  326. {
  327. nPos += direction;
  328. }
  329. CString strEditText;
  330. strEditText.Format(_T("%i"), nPos);
  331. SetEditText(strEditText);
  332. NotifySite(CBN_XTP_EXECUTE);
  333. }
  334. void CXTPControlEdit::TrackSpinButton(CPoint pt)
  335. {
  336. m_pParent->SetSelected(-1);
  337. m_pParent->SetSelected(GetIndex(), FALSE);
  338. #define TID_TIMER 435
  339. CRect rc = GetSpinButtonsRect();
  340. CRect rcTop(rc.left, rc.top, rc.right, rc.CenterPoint().y);
  341. CRect rcBottom(rc.left, rcTop.bottom, rc.right, rc.bottom);
  342. const BOOL bTop = rcTop.PtInRect(pt);
  343. const CRect rcActiveRect = bTop ? rcTop : rcBottom;
  344. const int direction = bTop ? +1 : -1;
  345. int increment = 1;
  346. m_bSelected = m_bPressed = bTop ? XTP_EDITSPIN_UP : XTP_EDITSPIN_DOWN;
  347. RedrawParent(FALSE);
  348. NotifySpinChanged(increment, direction);
  349. m_pParent->SetCapture();
  350. m_pParent->SetTimer(TID_TIMER, 530, NULL);
  351. BOOL bFirst = TRUE;
  352. DWORD dwStart = GetTickCount();
  353. for (;;)
  354. {
  355. MSG msg;
  356. VERIFY(::GetMessage(&msg, NULL, 0, 0));
  357. if (msg.message == WM_LBUTTONUP)
  358. {
  359. break;
  360. }
  361. if (msg.message == WM_MOUSEMOVE)
  362. {
  363. pt = CPoint(LOWORD(msg.lParam), HIWORD(msg.lParam));
  364. if (OnSetSelected(rcActiveRect.PtInRect(pt)))
  365. {
  366. dwStart = GetTickCount();
  367. RedrawParent();
  368. }
  369. continue;
  370. }
  371. if (msg.message == WM_TIMER && msg.wParam == TID_TIMER)
  372. {
  373. if (m_bSelected)
  374. {
  375. UINT uElapsed = (UINT)((GetTickCount() - dwStart) / 1024);
  376. increment = 1;
  377. if (uElapsed >= 5)
  378. increment = 20;
  379. else if (uElapsed >= 2)
  380. increment = 5;
  381. NotifySpinChanged(increment, direction);
  382. }
  383. if (bFirst)
  384. {
  385. m_pParent->SetTimer(TID_TIMER, 100, NULL);
  386. }
  387. bFirst = FALSE;
  388. }
  389. DispatchMessage (&msg);
  390. if (CWnd::GetCapture() != m_pParent)
  391. break;
  392. }
  393. m_pParent->KillTimer(TID_TIMER);
  394. m_bPressed = FALSE;
  395. ReleaseCapture();
  396. RedrawParent(FALSE);
  397. if (GetParent()->GetPosition() != xtpBarPopup)
  398. GetParent()->SetTrackingMode(FALSE);
  399. }
  400. CRect CXTPControlEdit::GetSpinButtonsRect() const
  401. {
  402. if (!IsSpinButtonsVisible())
  403. return CRect(0, 0, 0, 0);
  404. CRect rc(GetRect());
  405. return CRect(rc.right - 18, rc.top + 1, rc.right - 1, rc.bottom - 1);
  406. }
  407. void CXTPControlEdit::OnMouseMove(CPoint point)
  408. {
  409. CXTPControl::OnMouseMove(point);
  410. if (GetEnabled() && IsSpinButtonsVisible() && m_bSelected)
  411. {
  412. CRect rc(GetSpinButtonsRect());
  413. CRect rcTop(rc.left, rc.top, rc.right, rc.CenterPoint().y);
  414. CRect rcBottom(rc.left, rcTop.bottom, rc.right, rc.bottom);
  415. if (rcTop.PtInRect(point) && m_bSelected != XTP_EDITSPIN_UP)
  416. {
  417. m_bSelected = XTP_EDITSPIN_UP;
  418. RedrawParent(FALSE);
  419. }
  420. else if (rcBottom.PtInRect(point) && m_bSelected != XTP_EDITSPIN_DOWN)
  421. {
  422. m_bSelected = XTP_EDITSPIN_DOWN;
  423. RedrawParent(FALSE);
  424. }
  425. else if ((m_bSelected == XTP_EDITSPIN_UP || m_bSelected == XTP_EDITSPIN_DOWN) && !rc.PtInRect(point))
  426. {
  427. m_bSelected = 1;
  428. RedrawParent(FALSE);
  429. }
  430. }
  431. }
  432. void CXTPControlEdit::OnMouseHover()
  433. {
  434. if (!XTPMouseManager()->IsMouseLocked())
  435. CXTPControl::OnMouseHover();
  436. }
  437. void CXTPControlEdit::SetHideFlags(DWORD dwFlags)
  438. {
  439. if (m_dwHideFlags != dwFlags)
  440. {
  441. m_dwHideFlags = dwFlags;
  442. ShowHideEditControl();
  443. DelayLayoutParent();
  444. }
  445. }
  446. void CXTPControlEdit::OnActionChanged(int nProperty)
  447. {
  448. if (nProperty == 2)
  449. {
  450. ShowHideEditControl();
  451. }
  452. if (nProperty == 0 && m_pEdit && m_pEdit->GetSafeHwnd())    // Enabled
  453. {
  454. m_pEdit->EnableWindow(GetEnabled());
  455. m_pEdit->UpdateCharFormat();
  456. }
  457. if (nProperty == 4)
  458. {
  459. if (_GetEditText().IsEmpty() && !GetEditHint().IsEmpty())
  460. {
  461. _SetEditText(GetEditHint());
  462. }
  463. }
  464. }
  465. void CXTPControlEdit::OnActionChanging(int nProperty)
  466. {
  467. if (nProperty == 4 && !GetEditHint().IsEmpty() && _GetEditText() == GetEditHint())
  468. {
  469. _SetEditText(_T(""));
  470. }
  471. }
  472. void CXTPControlEdit::OnCalcDynamicSize(DWORD dwMode)
  473. {
  474. if (dwMode & LM_VERTDOCK) SetHideFlags(m_dwHideFlags | xtpHideDockingPosition);
  475. else SetHideFlags(m_dwHideFlags & ~xtpHideDockingPosition);
  476. }
  477. void CXTPControlEdit::SetEditHint(LPCTSTR lpszEditHint)
  478. {
  479. if (!GetEditHint().IsEmpty() && _GetEditText() == GetEditHint())
  480. {
  481. _SetEditText(_T(""));
  482. }
  483. m_strEditHint = lpszEditHint;
  484. if (_GetEditText().IsEmpty() && !GetEditHint().IsEmpty())
  485. {
  486. _SetEditText(GetEditHint());
  487. }
  488. }
  489. CString CXTPControlEdit::GetEditHint() const
  490. {
  491. return !m_strEditHint.IsEmpty() ? m_strEditHint : m_pAction ? m_pAction->GetEditHint() : _T("");
  492. }
  493. DWORD CXTPControlEdit::GetEditStyle() const
  494. {
  495. return m_dwEditStyle;
  496. }
  497. void CXTPControlEdit::SetEditStyle(DWORD dwStyle)
  498. {
  499. m_dwEditStyle = dwStyle;
  500. if (m_pEdit && m_pEdit->GetSafeHwnd())
  501. {
  502. m_bDelayDestroy = TRUE;
  503. DelayLayoutParent();
  504. }
  505. }
  506. void CXTPControlEdit::Copy(CXTPControl* pControl, BOOL bRecursive)
  507. {
  508. CXTPControl::Copy(pControl, bRecursive);
  509. CXTPControlEdit* pControlEdit = DYNAMIC_DOWNCAST(CXTPControlEdit, pControl);
  510. if (!pControlEdit)
  511. return;
  512. m_dwEditStyle = pControlEdit->m_dwEditStyle;
  513. SetEditText(pControlEdit->GetEditText());
  514. m_bLabel = pControlEdit->m_bLabel;
  515. SetReadOnly(pControlEdit->m_bReadOnly);
  516. SetEditHint(pControlEdit->m_strEditHint);
  517. EnableShellAutoComplete(pControlEdit->m_dwShellAutoCompleteFlags);
  518. m_bShowSpinButtons = pControlEdit->m_bShowSpinButtons;
  519. }
  520. BOOL CXTPControlEdit::Compare(CXTPControl* pOther)
  521. {
  522. if (!CXTPControl ::Compare(pOther))
  523. return FALSE;
  524. if (!pOther->IsKindOf(RUNTIME_CLASS(CXTPControlEdit)))
  525. return FALSE;
  526. return TRUE;
  527. }
  528. BOOL CXTPControlEdit::IsFocused() const
  529. {
  530. return m_bFocused;
  531. }
  532. void CXTPControlEdit::SetFocused(BOOL bFocused)
  533. {
  534. if (m_bFocused == bFocused)
  535. return;
  536. m_bFocused = bFocused;
  537. if (bFocused)
  538. {
  539. if (m_pEdit->GetSafeHwnd() && ::GetFocus() != m_pEdit->GetSafeHwnd())
  540. m_pEdit->SetFocus();
  541. m_pParent->SetTrackingMode(TRUE, FALSE);
  542. m_pParent->SetSelected(m_nIndex);
  543. XTPMouseManager()->LockMouseMove();
  544. NotifySite(EN_SETFOCUS);
  545. }
  546. else
  547. {
  548. if (m_pEdit->GetSafeHwnd() && ::GetFocus() == m_pEdit->GetSafeHwnd())
  549. ::SetFocus(m_pParent->GetTrackFocus());
  550. XTPMouseManager()->UnlockMouseMove();
  551. NotifySite(EN_KILLFOCUS);
  552. }
  553. RedrawParent();
  554. }
  555. void CXTPControlEdit::OnSetFocus(CWnd* pOldWnd)
  556. {
  557. if (!GetEditHint().IsEmpty() && m_pEdit->GetSafeHwnd() && GetEditHint() == _GetEditText())
  558. {
  559. _SetEditText(_T(""));
  560. }
  561. m_strLastText = GetEditText();
  562. if (m_pParent->GetTrackFocus() == 0)
  563. m_pParent->SetTrackFocus(pOldWnd ? pOldWnd->GetSafeHwnd() : m_pParent->GetSite()->GetSafeHwnd());
  564. m_pParent->SetPopuped(-1);
  565. SetFocused(TRUE);
  566. if (m_pAutoCompleteWnd) m_pAutoCompleteWnd->SetupMessageHook(TRUE);
  567. }
  568. void CXTPControlEdit::OnKillFocus()
  569. {
  570. if (!GetEditHint().IsEmpty() && m_pEdit->GetSafeHwnd() && _GetEditText().IsEmpty())
  571. {
  572. _SetEditText(GetEditHint());
  573. }
  574. SetFocused(FALSE);
  575. if (m_pAutoCompleteWnd) m_pAutoCompleteWnd->SetupMessageHook(FALSE);
  576. }
  577. BOOL CXTPControlEdit::IsImageVisible() const
  578. {
  579. if (GetParent()->GetType() == xtpBarTypePopup)
  580. return GetImage(0) != NULL;
  581. BOOL bImageVisible = (CXTPControl::GetStyle() == xtpButtonIcon || CXTPControl::GetStyle() == xtpButtonIconAndCaption)
  582. && (GetImage(0) != NULL);
  583. return bImageVisible;
  584. }
  585. BOOL CXTPControlEdit::IsCaptionVisible() const
  586. {
  587. if (GetParent()->GetType() == xtpBarTypePopup)
  588. return TRUE;
  589. if (GetCaption().IsEmpty())
  590. return FALSE;
  591. XTPButtonStyle buttonStyle = GetStyle();
  592. if ((buttonStyle == xtpButtonCaption) || (buttonStyle == xtpButtonIconAndCaption) || (buttonStyle == xtpButtonIconAndCaptionBelow))
  593. return TRUE;
  594. return m_bLabel;
  595. }
  596. BEGIN_MESSAGE_MAP(CXTPControlEditCtrl, CXTPEdit)
  597. ON_WM_SETFOCUS()
  598. ON_WM_MOUSEMOVE()
  599. ON_WM_KILLFOCUS()
  600. ON_WM_DESTROY()
  601. ON_WM_LBUTTONDOWN()
  602. ON_WM_RBUTTONDOWN()
  603. ON_WM_CONTEXTMENU()
  604. ON_WM_KEYDOWN()
  605. ON_MESSAGE(XTP_TTM_WINDOWFROMPOINT, OnWindowFromPoint)
  606. ON_CONTROL_REFLECT(EN_CHANGE, OnEditChanged)
  607. END_MESSAGE_MAP()
  608. CXTPControlEditCtrl::CXTPControlEditCtrl()
  609. {
  610. m_pControl = NULL;
  611. }
  612. void CXTPControlEditCtrl::OnEditChanged()
  613. {
  614. m_pControl->m_bEditChanged = TRUE;
  615. if (::GetFocus() == m_hWnd && !m_bIgonoreEditChanged)
  616. m_pControl->OnEditChanged();
  617. SetMargins(0, 0);
  618. UpdateCharFormat();
  619. }
  620. void CXTPControlEditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  621. {
  622. if (nChar == VK_RETURN && ::GetFocus() == m_hWnd)
  623. {
  624. m_pControl->OnExecute();
  625. return;
  626. }
  627. CXTPEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  628. }
  629. void CXTPControlEditCtrl::UpdateCharFormat()
  630. {
  631. CHARFORMAT2 cf = m_pControl->GetDefaultCharFormat();
  632. cf.cbSize = GetRichEditContext().m_bRichEdit2 ? sizeof(CHARFORMAT2) : sizeof(CHARFORMAT);
  633. ::SendMessage(m_hWnd, EM_SETCHARFORMAT, 0, (LPARAM)&cf);
  634. ::SendMessage(m_hWnd, EM_SETBKGNDCOLOR, FALSE, cf.crBackColor);
  635. }
  636. void CXTPControlEditCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/)
  637. {
  638. }
  639. void CXTPControlEditCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  640. {
  641. if (m_pControl->IsCustomizeMode())
  642. {
  643. if (m_pControl->IsCustomizeMovable())
  644. m_pControl->OnClick();
  645. }
  646. else CEdit::OnLButtonDown(nFlags, point);
  647. }
  648. void CXTPControlEditCtrl::OnRButtonDown(UINT nFlags, CPoint point)
  649. {
  650. if (m_pControl->IsCustomizeMode())
  651. {
  652. ClientToScreen(&point);
  653. m_pControl->GetParent()->ScreenToClient(&point);
  654. m_pControl->GetParent()->OnRButtonDown(nFlags, point);
  655. return;
  656. }
  657. m_pControl->SetFocused(TRUE);
  658. if (!ShowContextMenu(m_pControl, point))
  659. CXTPEdit::OnRButtonDown(nFlags, point);
  660. }
  661. void CXTPControlEditCtrl::OnDestroy()
  662. {
  663. if (m_pControl) GetWindowTextEx(m_pControl->m_strEditText);
  664. CEdit::OnDestroy();
  665. }
  666. void CXTPControlEditCtrl::OnSetFocus(CWnd* pOldWnd)
  667. {
  668. if (!m_pControl->IsCustomizeMode())
  669. {
  670. m_pControl->OnSetFocus(pOldWnd);
  671. CEdit::OnSetFocus(pOldWnd);
  672. SetSel(0, 0);
  673. ::PostMessage(m_hWnd, EM_SETSEL, 0, -1);
  674. }
  675. }
  676. void CXTPControlEditCtrl::OnKillFocus(CWnd* pNewWnd)
  677. {
  678. CXTPEdit::OnKillFocus(pNewWnd);
  679. m_pControl->OnKillFocus();
  680. }
  681. void CXTPControlEditCtrl::OnMouseMove(UINT nFlags, CPoint point)
  682. {
  683. CEdit::OnMouseMove(nFlags, point);
  684. MapWindowPoints(m_pControl->GetParent(), &point, 1);
  685. m_pControl->GetParent()->OnMouseMove(nFlags, point);
  686. }
  687. CHARFORMAT2 CXTPControlEdit::GetDefaultCharFormat()
  688. {
  689. CHARFORMAT2 cf;
  690. ZeroMemory(&cf, sizeof(CHARFORMAT2));
  691. cf.dwMask = CFM_COLOR | CFM_BACKCOLOR;
  692. if (GetEnabled())
  693. {
  694. cf.crTextColor = GetXtremeColor(COLOR_WINDOWTEXT);
  695. if (!GetEditHint().IsEmpty() && GetEditHint() == _GetEditText())
  696. {
  697. cf.crTextColor = GetXtremeColor(COLOR_GRAYTEXT);
  698. }
  699. }
  700. else
  701. {
  702. cf.crTextColor = GetXtremeColor(COLOR_GRAYTEXT);
  703. }
  704. cf.crBackColor = GetPaintManager()->GetControlEditBackColor(this);
  705. return cf;
  706. }
  707. BOOL CXTPControlEditCtrl::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  708. {
  709. CXTPCommandBar* pCommandBar = m_pControl->GetParent();
  710. if (pCommandBar)
  711. {
  712. pCommandBar->FilterToolTipMessage(message, wParam, lParam);
  713. }
  714. return CWnd::OnWndMsg(message, wParam, lParam, pResult);
  715. }
  716. LRESULT CXTPControlEditCtrl::OnWindowFromPoint(WPARAM, LPARAM)
  717. {
  718. return 1;
  719. }