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

对话框与窗口

开发平台:

Visual C++

  1. // XTPControlListBox.cpp : implementation of the CXTPControlListBox 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/XTPPropExchange.h"
  23. #include "XTPControl.h"
  24. #include "XTPCommandBar.h"
  25. #include "XTPPaintManager.h"
  26. #include "XTPMouseManager.h"
  27. #include "XTPControlListBox.h"
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #define new DEBUG_NEW
  32. #endif
  33. class CXTPControlListBoxCtrl : public CListBox
  34. {
  35. friend class CXTPControlListBox;
  36. protected:
  37. void OnSelChanged();
  38. protected:
  39. //{{AFX_CODEJOCK_PRIVATE
  40. DECLARE_MESSAGE_MAP()
  41. //{{AFX_MSG(CXTPControlListBoxCtrl)
  42. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  43. afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
  44. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  45. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  46. afx_msg void OnPaint();
  47. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  48. afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
  49. //}}AFX_MSG
  50. //}}AFX_CODEJOCK_PRIVATE
  51. public:
  52. CXTPControlListBox* m_pControl;
  53. };
  54. IMPLEMENT_XTP_CONTROL(CXTPControlListBox, CXTPControl)
  55. //////////////////////////////////////////////////////////////////////
  56. // Construction/Destruction
  57. //////////////////////////////////////////////////////////////////////
  58. CXTPControlListBox::CXTPControlListBox()
  59. {
  60. m_pListBox = new CXTPControlListBoxCtrl;
  61. m_pListBox->m_pControl = this;
  62. m_pListBox->CreateEx(0, _T("LISTBOX"), NULL, WS_POPUP | WS_VSCROLL | WS_CLIPCHILDREN | LBS_NOTIFY, CRect(0, 0, 0, 0), 0, 0);
  63. m_nListBoxLinesMin = 1;
  64. m_nListBoxLinesMax = 12;
  65. m_nWidth = 100;
  66. m_bMultiplSel = FALSE;
  67. m_bMouseLocked = FALSE;
  68. }
  69. CXTPControlListBox::~CXTPControlListBox()
  70. {
  71. if (m_pListBox)
  72. {
  73. delete m_pListBox;
  74. }
  75. }
  76. CListBox* CXTPControlListBox::GetListCtrl() const
  77. {
  78. ASSERT_VALID(this);
  79. return m_pListBox;
  80. }
  81. void CXTPControlListBox::SetWidth(int nWidth)
  82. {
  83. m_nWidth = nWidth;
  84. }
  85. void CXTPControlListBox::SetMultiplSel(BOOL bMultiplSel)
  86. {
  87. m_bMultiplSel = bMultiplSel;
  88. m_pListBox->DestroyWindow();
  89. m_pListBox->CreateEx(0, _T("LISTBOX"), NULL, WS_POPUP | WS_VSCROLL | WS_CLIPCHILDREN | LBS_NOTIFY | (m_bMultiplSel ? LBS_MULTIPLESEL : 0), CRect(0, 0, 0, 0), 0, 0);
  90. }
  91. void CXTPControlListBox::SetLinesMinMax(int nMin, int nMax)
  92. {
  93. m_nListBoxLinesMin = nMin;
  94. m_nListBoxLinesMax = nMax;
  95. }
  96. void CXTPControlListBox::SetRect(CRect rcControl)
  97. {
  98. ASSERT_VALID(this);
  99. if (m_rcControl == rcControl && m_pListBox->GetParent() == m_pParent)
  100. {
  101. return;
  102. }
  103. m_rcControl = rcControl;
  104. m_pListBox->EnableWindow(GetEnabled());
  105. m_pListBox->ModifyStyle(WS_POPUP, WS_CHILD);
  106. m_pListBox->SetParent(m_pParent);
  107. m_pListBox->MoveWindow(rcControl);
  108. m_pListBox->SetWindowPos(0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | (!IsVisible() ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
  109. }
  110. void CXTPControlListBox::SetParent(CXTPCommandBar* pParent)
  111. {
  112. if (pParent != m_pParent && (pParent && pParent->GetSafeHwnd()))
  113. {
  114. m_pListBox->ModifyStyle(WS_POPUP, WS_CHILD);
  115. m_pListBox->SetParent(pParent);
  116. }
  117. CXTPControl::SetParent(pParent);
  118. }
  119. void CXTPControlListBox::SetEnabled(BOOL bEnabled)
  120. {
  121. ASSERT_VALID(this);
  122. if (bEnabled != m_bEnabled)
  123. {
  124. m_bEnabled = bEnabled;
  125. m_pListBox->EnableWindow(bEnabled);
  126. }
  127. }
  128. CSize CXTPControlListBox::GetSize(CDC* /*pDC*/)
  129. {
  130. ASSERT_VALID(this);
  131. m_pListBox->SetFont(GetPaintManager()->GetIconFont(), FALSE);
  132. int nItemHeight = m_pListBox->GetItemHeight(0);
  133. int nHeight = min (m_nListBoxLinesMax, max(m_nListBoxLinesMin, m_pListBox->GetCount())) * nItemHeight;
  134. int nWidth = m_nWidth;
  135. return CSize(nWidth, nHeight);
  136. }
  137. void CXTPControlListBox::Draw(CDC* /*pDC*/)
  138. {
  139. }
  140. BOOL CXTPControlListBox::IsFocused() const
  141. {
  142. return m_bSelected;
  143. }
  144. BOOL  CXTPControlListBox::OnSetSelected(int bSelected)
  145. {
  146. if (!CXTPControl::OnSetSelected(bSelected))
  147. return FALSE;
  148. if (bSelected && !m_bMouseLocked)
  149. {
  150. XTPMouseManager()->LockMouseMove();
  151. m_bMouseLocked = TRUE;
  152. }
  153. else if (!bSelected && m_bMouseLocked)
  154. {
  155. XTPMouseManager()->UnlockMouseMove();
  156. m_bMouseLocked = FALSE;
  157. }
  158. return TRUE;
  159. }
  160. BOOL CXTPControlListBox::OnHookKeyDown(UINT nChar, LPARAM lParam)
  161. {
  162. ASSERT_VALID(this);
  163. if (nChar == VK_RETURN || nChar == VK_TAB)
  164. return FALSE;
  165. if (nChar == VK_ESCAPE)
  166. return FALSE;
  167. int nSel = m_pListBox->GetCurSel();
  168. m_pListBox->SendMessage(WM_KEYDOWN, nChar, lParam);
  169. if (nSel != m_pListBox->GetCurSel())
  170. {
  171. m_pListBox->OnSelChanged();
  172. }
  173. return TRUE;
  174. }
  175. void CXTPControlListBox::OnClick(BOOL bKeyboard , CPoint pt)
  176. {
  177. if (bKeyboard)
  178. {
  179. OnExecute();
  180. }
  181. else CXTPControl::OnClick(bKeyboard, pt);
  182. }
  183. void CXTPControlListBox::OnCalcDynamicSize(DWORD dwMode)
  184. {
  185. CXTPControl::OnCalcDynamicSize(dwMode);
  186. if (m_bMultiplSel)
  187. {
  188. m_pListBox->SelItemRange(FALSE, 0, m_pListBox->GetCount() - 1);
  189. }
  190. else
  191. {
  192. m_pListBox->SetCurSel(-1);
  193. }
  194. NotifySite(XTP_LBN_POPUP);
  195. }
  196. void CXTPControlListBox::Copy(CXTPControl* pControl, BOOL bRecursive)
  197. {
  198. ASSERT_KINDOF(CXTPControlListBox, pControl);
  199. CXTPControl::Copy(pControl, bRecursive);
  200. m_nWidth = ((CXTPControlListBox*)pControl)->m_nWidth;
  201. SetMultiplSel(((CXTPControlListBox*)pControl)->m_bMultiplSel);
  202. m_nListBoxLinesMax = ((CXTPControlListBox*)pControl)->m_nListBoxLinesMax;
  203. m_nListBoxLinesMin = ((CXTPControlListBox*)pControl)->m_nListBoxLinesMin;
  204. CListBox* pListBox = ((CXTPControlListBox*)pControl)->GetListCtrl();
  205. DWORD dwCount = pListBox->m_hWnd ? pListBox->GetCount() : 0;
  206. m_pListBox->ResetContent();
  207. for (UINT i = 0; i < dwCount; i++)
  208. {
  209. CString str;
  210. pListBox->GetText(i, str);
  211. m_pListBox->AddString(str);
  212. }
  213. }
  214. void CXTPControlListBox::DoPropExchange(CXTPPropExchange* pPX)
  215. {
  216. CXTPControl::DoPropExchange(pPX);
  217. CString str;
  218. PX_Int(pPX, _T("Width"), m_nWidth, 100);
  219. PX_Bool(pPX, _T("MultiplSel"), m_bMultiplSel, FALSE);
  220. PX_Int(pPX, _T("ListBoxLinesMax"), m_nListBoxLinesMax, 12);
  221. PX_Int(pPX, _T("ListBoxLinesMin"), m_nListBoxLinesMin, 1);
  222. CXTPPropExchangeSection secItems(pPX->GetSection(_T("Items")));
  223. if (pPX->IsStoring())
  224. {
  225. DWORD dwCount = m_pListBox->m_hWnd ? m_pListBox->GetCount() : 0;
  226. secItems->WriteCount(dwCount);
  227. for (UINT i = 0; i < dwCount; i++)
  228. {
  229. m_pListBox->GetText(i, str);
  230. CString strSection;
  231. strSection.Format(_T("Item%i"), i);
  232. PX_String(&secItems, strSection, str, _T(""));
  233. }
  234. }
  235. else
  236. {
  237. SetMultiplSel(m_bMultiplSel);
  238. DWORD nNewCount = secItems->ReadCount();
  239. for (DWORD i = 0; i < nNewCount; i++)
  240. {
  241. CString strSection;
  242. strSection.Format(_T("Item%i"), i);
  243. PX_String(&secItems, strSection, str, _T(""));
  244. m_pListBox->AddString(str);
  245. }
  246. }
  247. }
  248. // mini hack.
  249. BEGIN_MESSAGE_MAP(CXTPControlListBoxCtrl, CListBox)
  250. ON_WM_LBUTTONDOWN()
  251. ON_WM_LBUTTONUP()
  252. ON_WM_LBUTTONDBLCLK()
  253. ON_WM_MOUSEMOVE()
  254. ON_WM_ERASEBKGND()
  255. ON_WM_PAINT()
  256. ON_WM_VSCROLL()
  257. END_MESSAGE_MAP()
  258. void CXTPControlListBoxCtrl::OnLButtonDown(UINT /*nFlags*/, CPoint /*point*/)
  259. {
  260. }
  261. void CXTPControlListBoxCtrl::OnLButtonUp(UINT /*nFlags*/, CPoint /*point*/)
  262. {
  263. m_pControl->OnExecute();
  264. }
  265. void CXTPControlListBoxCtrl::OnLButtonDblClk(UINT /*nFlags*/, CPoint /*point*/)
  266. {
  267. }
  268. void CXTPControlListBoxCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  269. {
  270. SetRedraw(FALSE);
  271. CListBox::OnVScroll(nSBCode, nPos, pScrollBar);
  272. SetRedraw(TRUE);
  273. Invalidate();
  274. UpdateWindow();
  275. // repaint the scroll bar.
  276. ::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0,
  277. SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
  278. }
  279. void CXTPControlListBoxCtrl::OnPaint()
  280. {
  281. CPaintDC dcPaint(this);
  282. CXTPClientRect rc(this);
  283. CXTPBufferDC dc(dcPaint, rc);
  284. dc.FillSolidRect(rc, GetXtremeColor(COLOR_WINDOW));
  285. CWnd::DefWindowProc(WM_PAINT, (WPARAM)dc.m_hDC, 0);
  286. }
  287. BOOL CXTPControlListBoxCtrl::OnEraseBkgnd(CDC* /*pDC*/)
  288. {
  289. return TRUE;
  290. }
  291. void CXTPControlListBoxCtrl::OnMouseMove(UINT nFlags, CPoint point)
  292. {
  293. BOOL bOutside;
  294. UINT nItem = ItemFromPoint(point, bOutside);
  295. if (!bOutside)
  296. {
  297. SetCurSel(nItem);
  298. SetSel(nItem, TRUE);
  299. OnSelChanged();
  300. }
  301. MapWindowPoints(m_pControl->GetParent(), &point, 1);
  302. m_pControl->GetParent()->OnMouseMove(nFlags, point);
  303. }
  304. void CXTPControlListBoxCtrl::OnSelChanged()
  305. {
  306. if (m_pControl->m_bMultiplSel)
  307. {
  308. int nSel = GetCurSel();
  309. if (nSel != LB_ERR)
  310. {
  311. SelItemRange(FALSE, nSel + 1, GetCount() - 1);
  312. if (nSel != 0) SelItemRange(TRUE, 0, nSel);
  313. else  SetSel(0, TRUE);
  314. }
  315. else
  316. {
  317. SelItemRange(FALSE, 0, GetCount() - 1);
  318. }
  319. }
  320. m_pControl->NotifySite(XTP_LBN_SELCHANGE);
  321. }
  322. ///////////////////////////////////////////////////////////////////
  323. // CXTPControlListBoxInfo
  324. IMPLEMENT_XTP_CONTROL(CXTPControlStatic, CXTPControl)
  325. CXTPControlStatic::CXTPControlStatic()
  326. {
  327. m_nWidth = 0;
  328. }
  329. void CXTPControlStatic::SetWidth(int nWidth)
  330. {
  331. m_nWidth = nWidth;
  332. }
  333. CSize CXTPControlStatic::GetSize(CDC* pDC)
  334. {
  335. BOOL bVert = GetParent()->GetPosition() == xtpBarRight || GetParent()->GetPosition() == xtpBarLeft;
  336. CXTPEmptyRect rcText;
  337. CSize sz = GetPaintManager()->DrawControlText(pDC, this, &rcText, FALSE, bVert, FALSE, FALSE);
  338. if (bVert) return CSize(max(22, sz.cx), max(m_nWidth, sz.cy));
  339. return CSize(max(m_nWidth, sz.cx), max(22, sz.cy));
  340. }
  341. void CXTPControlStatic::Draw(CDC* pDC)
  342. {
  343. BOOL bVert = GetParent()->GetPosition() == xtpBarRight || GetParent()->GetPosition() == xtpBarLeft;
  344. CRect rcText = GetRect();
  345. pDC->SetTextColor(GetPaintManager()->GetControlTextColor(this));
  346. GetPaintManager()->DrawControlText(pDC, this, &rcText, TRUE, bVert, TRUE, FALSE);
  347. }
  348. void CXTPControlStatic::Copy(CXTPControl* pControl, BOOL bRecursive)
  349. {
  350. ASSERT_KINDOF(CXTPControlStatic, pControl);
  351. CXTPControl::Copy(pControl, bRecursive);
  352. m_nWidth = ((CXTPControlStatic*)pControl)->m_nWidth;
  353. }
  354. void CXTPControlStatic::DoPropExchange(CXTPPropExchange* pPX)
  355. {
  356. CXTPControl::DoPropExchange(pPX);
  357. PX_Int(pPX, _T("Width"), m_nWidth, 0);
  358. }