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

对话框与窗口

开发平台:

Visual C++

  1. // PaneControls.cpp : implementation file
  2. //
  3. // This file is a part of the XTREME TOOLKIT PRO 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 "CommandBarsDesigner.h"
  22. #include "PaneControls.h"
  23. #include "DialogNewControl.h"
  24. #include "MainFrm.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. #define ID_PANECONTROLS_COMBO 1010
  31. class CControlsListBox::CListBoxDropTarget : public COleDropTarget
  32. {
  33. virtual DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
  34. {
  35. if (pDataObject->IsDataAvailable(CPaneLibrary::m_cfItem))
  36. return DROPEFFECT_COPY;
  37. return DROPEFFECT_NONE;
  38. }
  39. virtual BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject,
  40. DROPEFFECT dropEffect, CPoint point)
  41. {
  42. return ((CControlsListBox*)pWnd)->m_pPaneControls->OnDropLibrary();
  43. }
  44. };
  45. CControlsListBox::CControlsListBox()
  46. {
  47. m_pDropTarget = NULL;
  48. }
  49. CControlsListBox::~CControlsListBox()
  50. {
  51. SAFE_DELETE(m_pDropTarget);
  52. }
  53. void CControlsListBox::RegisterDropTarget()
  54. {
  55. m_pDropTarget = new CListBoxDropTarget();
  56. m_pDropTarget->Register(this);
  57. }
  58. void CControlsListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  59. {
  60. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  61. CRect rc(lpDrawItemStruct->rcItem);
  62. CXTPControl* pControl = (CXTPControl*)lpDrawItemStruct->itemData;
  63. if (pControl)
  64. {
  65. BOOL bSelected = ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED));
  66. XTPPaintManager()->DrawListBoxControl(pDC, pControl, rc, bSelected, TRUE);
  67. }
  68. }
  69. BEGIN_MESSAGE_MAP(CControlsListBox, CListBox)
  70. //{{AFX_MSG_MAP(CXTPCustomizeCommandsPage)
  71. ON_WM_LBUTTONDOWN()
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. LRESULT CControlsListBox::WindowProc(UINT message,WPARAM wParam,LPARAM lParam)
  75. {
  76. switch (message)
  77. {
  78. case WM_NCPAINT:
  79. {
  80. CListBox::WindowProc(message, wParam, lParam);
  81. CRect rc;
  82. GetWindowRect(&rc);
  83. CWindowDC dc(this);
  84. rc.OffsetRect(-rc.TopLeft());
  85. COLORREF clr = GetStaticFrameColor();
  86. dc.Draw3dRect(rc, clr, clr);
  87. return TRUE;
  88. }
  89. case WM_PAINT:
  90. {
  91. CPaintDC dc(this);
  92. CXTPClientRect rc(this);
  93. CXTPBufferDC memDC(dc, rc);
  94. XTPPaintManager()->DrawListBoxControl(&memDC, NULL, CXTPClientRect(this), FALSE, TRUE);
  95. return CListBox::DefWindowProc( WM_PAINT, (WPARAM)memDC.m_hDC, 0 );
  96. }
  97. case WM_KEYDOWN:
  98. if( GetKeyState(VK_CONTROL) & 0x8000 )
  99. {
  100. if( wParam == VK_UP )
  101. {
  102. GetOwner()->SendMessage(WM_COMMAND, ID_PANECONTROLS_UP);
  103. return TRUE;
  104. }
  105. if( wParam == VK_DOWN )
  106. {
  107. GetOwner()->SendMessage(WM_COMMAND, ID_PANECONTROLS_DOWN );
  108. return TRUE;
  109. }
  110. }
  111. break;
  112. case WM_ERASEBKGND:
  113. CDC* pDC = CDC::FromHandle((HDC)wParam);
  114. XTPPaintManager()->DrawListBoxControl(pDC, NULL, CXTPClientRect(this), FALSE, TRUE);
  115. return TRUE;
  116. }
  117. return CListBox::WindowProc(message, wParam, lParam);
  118. }
  119. void CControlsListBox::OnLButtonDown(UINT nFlags, CPoint point)
  120. {
  121. CXTPCommandBars* pCommandBars = m_pPaneControls->m_pActiveCommandBars;
  122. if (!pCommandBars)
  123. {
  124. CListBox::OnLButtonDown(nFlags, point);
  125. return;
  126. }
  127. pCommandBars->SetDragControl(NULL);
  128. BOOL bOutside;
  129. int nIndex = ( int )ItemFromPoint( point, bOutside );
  130. CXTPControl* pControl = NULL;
  131. if (!bOutside)
  132. {
  133. if ( SetCurSel( nIndex ) != LB_ERR )
  134. {
  135. // Already selected, start drag operation
  136. pControl = ( CXTPControl* )GetItemData( nIndex );
  137. // since we are eating the message, if the selection has changed, notify the owner.
  138. CWnd* pWndOwner = GetOwner();
  139. if ( pWndOwner )
  140. {
  141. SetFocus();
  142. pWndOwner->PostMessage( WM_COMMAND, MAKEWPARAM( GetDlgCtrlID( ),
  143. LBN_SELCHANGE ), ( LPARAM )CListBox::m_hWnd );
  144. }
  145. if (pControl)
  146. {
  147. pCommandBars->SetDragControl(pControl);
  148. CXTPCustomizeDropSource* pDropSource = pCommandBars->GetDropSource();
  149. pDropSource->DoDragDrop(pControl, TRUE);
  150. return;
  151. }
  152. }
  153. }
  154. // process as a click first
  155. CListBox::OnLButtonDown(nFlags, point);
  156. }
  157. /////////////////////////////////////////////////////////////////////////////
  158. // CPaneControls
  159. CPaneControls::CPaneControls()
  160. {
  161. m_pActiveCommandBars = 0;
  162. m_pControls = new CXTPControls();
  163. m_wndListBox.m_pPaneControls = this;
  164. }
  165. CPaneControls::~CPaneControls()
  166. {
  167. delete m_pControls;
  168. }
  169. BEGIN_MESSAGE_MAP(CPaneControls, CWnd)
  170. //{{AFX_MSG_MAP(CPaneControls)
  171. ON_WM_CREATE()
  172. ON_WM_SIZE()
  173. ON_WM_SETFOCUS()
  174. ON_COMMAND(ID_PANECONTROLS_ADD, OnPanecontrolsAdd)
  175. ON_UPDATE_COMMAND_UI(ID_PANECONTROLS_ADD, OnUpdatePanecontrolsAdd)
  176. ON_UPDATE_COMMAND_UI(ID_PANECONTROLS_UP, OnUpdatePanecontrolsUp)
  177. ON_COMMAND(ID_PANECONTROLS_UP, OnPanecontrolsUp)
  178. ON_COMMAND(ID_PANECONTROLS_DOWN, OnPanecontrolsDown)
  179. ON_UPDATE_COMMAND_UI(ID_PANECONTROLS_DOWN, OnUpdatePanecontrolsDown)
  180. ON_COMMAND(ID_PANECONTROLS_DELETE, OnPanecontrolsDelete)
  181. ON_UPDATE_COMMAND_UI(ID_PANECONTROLS_DELETE, OnUpdatePanecontrolsDelete)
  182. //}}AFX_MSG_MAP
  183. ON_CBN_SELCHANGE(ID_PANECONTROLS_COMBO, OnCategoryChanged)
  184. ON_LBN_SELCHANGE(AFX_IDW_PANE_FIRST, OnControlChanged)
  185. END_MESSAGE_MAP()
  186. /////////////////////////////////////////////////////////////////////////////
  187. // CPaneControls message handlers
  188. int CPaneControls::OnCreate(LPCREATESTRUCT lpCreateStruct)
  189. {
  190. if (CWnd::OnCreate(lpCreateStruct) == -1)
  191. return -1;
  192. VERIFY(m_wndToolBar.CreateToolBar(WS_VISIBLE|WS_CHILD|CBRS_TOOLTIPS, this));
  193. VERIFY(m_wndToolBar.LoadToolBar(IDR_PANE_CONTROLS));
  194. VERIFY(m_wndListBox.Create(LBS_OWNERDRAWFIXED|LBS_NOINTEGRALHEIGHT|LBS_NOTIFY |WS_VSCROLL|WS_VISIBLE|WS_CHILD, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST ));
  195. m_wndListBox.ModifyStyleEx(0, WS_EX_STATICEDGE);
  196. CSize sz = XTPPaintManager()->DrawListBoxControl(NULL, NULL, CRect(0, 0, 0, 0), FALSE, FALSE);
  197. m_wndListBox.SetItemHeight(-1, sz.cy);
  198. CFont* pFont = XTPPaintManager()->GetRegularFont();
  199. LOGFONT lf = {sizeof(LOGFONT)};
  200. pFont->GetLogFont(&lf);
  201. m_fntRegular.CreateFontIndirect(&lf);
  202. m_wndListBox.SetFont(&m_fntRegular);
  203. m_wndCategories.Create(WS_VISIBLE|WS_CHILD|CBS_DROPDOWNLIST, CRect(0, 0, 0, 200), this, ID_PANECONTROLS_COMBO);
  204. m_wndCategories.SetFont(&m_fntRegular);
  205. m_wndListBox.RegisterDropTarget();
  206. return 0;
  207. }
  208. void CPaneControls::OnSize(UINT nType, int cx, int cy)
  209. {
  210. CWnd::OnSize(nType, cx, cy);
  211. int nTop = 0;
  212. if (m_wndCategories.GetSafeHwnd())
  213. {
  214. CRect rc;
  215. m_wndCategories.GetWindowRect(&rc);
  216. m_wndCategories.MoveWindow(0, 0, cx, rc.Height());
  217. nTop += rc.Height();
  218. }
  219. if (m_wndToolBar.GetSafeHwnd())
  220. {
  221. CSize sz = m_wndToolBar.CalcDockingLayout(cx, /*LM_HIDEWRAP|*/ LM_HORZDOCK|LM_HORZ | LM_COMMIT);
  222. m_wndToolBar.MoveWindow(0, nTop, cx, sz.cy);
  223. m_wndToolBar.Invalidate(FALSE);
  224. nTop += sz.cy;
  225. }
  226. if (m_wndListBox.GetSafeHwnd())
  227. {
  228. m_wndListBox.MoveWindow(0, nTop, cx, cy - nTop);
  229. m_wndListBox.Invalidate(FALSE);
  230. }
  231. }
  232. void CPaneControls::OnSetFocus(CWnd*)
  233. {
  234. m_wndListBox.SetFocus();
  235. }
  236. void CPaneControls::Refresh(BOOL bForce)
  237. {
  238. CXTPCommandBars* pCommandBars = ((CMainFrame*)AfxGetMainWnd())->GetActiveCommandBars();
  239. if (m_wndListBox.GetSafeHwnd() == 0 )
  240. {
  241. return;
  242. }
  243. if (m_pActiveCommandBars != pCommandBars || bForce)
  244. {
  245. m_pActiveCommandBars = pCommandBars;
  246. CXTPControl* pControlSelected = NULL;
  247. int nIndex = m_wndListBox.GetCurSel();
  248. if ( nIndex != LB_ERR )
  249. {
  250. pControlSelected = ( CXTPControl* )m_wndListBox.GetItemData( nIndex );
  251. }
  252. m_wndCategories.ResetContent();
  253. if (m_pActiveCommandBars != 0)
  254. {
  255. m_wndCategories.AddString(_T("All Controls"));
  256. CXTPControls* pControls = ((CMainFrame*)AfxGetMainWnd())->GetActiveEmbeddedFrame()->m_pControls;
  257. int nTopIndex = m_wndListBox.GetTopIndex();
  258. m_wndListBox.LockWindowUpdate();
  259. m_wndListBox.ResetContent();
  260. for (int i = 0; i < pControls->GetCount(); i++)
  261. {
  262. CXTPControl* pControl = pControls->GetAt(i);
  263. CString strCategory = pControl->GetCategory();
  264. if (strCategory.IsEmpty()) strCategory = _T("[Empty]");
  265. if (m_strSelectedCategory == _T("") || m_strSelectedCategory == strCategory)
  266. {
  267. int nIndex = (int)m_wndListBox.SendMessage(LB_INSERTSTRING, m_wndListBox.GetCount(), (LPARAM)pControl);
  268. if (pControlSelected == pControl)
  269. {
  270. m_wndListBox.SetCurSel(nIndex);
  271. }
  272. }
  273. if (m_wndCategories.FindStringExact(0, strCategory) == CB_ERR)
  274. {
  275. m_wndCategories.AddString(strCategory);
  276. }
  277. }
  278. m_wndListBox.UnlockWindowUpdate();
  279. m_wndListBox.SetTopIndex(nTopIndex);
  280. if (m_wndListBox.GetCurSel() == LB_ERR)
  281. {
  282. m_wndListBox.SetCurSel(nIndex == m_wndListBox.GetCount()? nIndex - 1: nIndex);
  283. OnControlChanged();
  284. }
  285. int nIndex = m_wndCategories.FindStringExact(0, m_strSelectedCategory);
  286. m_wndCategories.SetCurSel(nIndex);
  287. if (nIndex == CB_ERR && m_wndCategories.GetCount() > 0)
  288. {
  289. m_wndCategories.SetCurSel(0);
  290. if (!m_strSelectedCategory.IsEmpty()) OnCategoryChanged();
  291. }
  292. }
  293. else
  294. {
  295. m_wndListBox.ResetContent();
  296. }
  297. }
  298. }
  299. void CPaneControls::OnPanecontrolsAdd()
  300. {
  301. CDialogNewControl dc;
  302. CEmbeddedFrame* pFrame = GetMainFrame()->GetActiveEmbeddedFrame();
  303. if (!pFrame)
  304. return;
  305. if (!m_strSelectedCategory.IsEmpty())
  306. dc.m_strCategory = m_strSelectedCategory;
  307. if (dc.DoModal() == IDOK)
  308. {
  309. XTPControlType type = xtpControlButton;
  310. CString strType = dc.m_strType;
  311. for (int i = 1; i < _countof(lpTypes); i++)
  312. {
  313. if (lpTypes[i] && strType == lpTypes[i])
  314. {
  315. type = (XTPControlType)i;
  316. break;
  317. }
  318. }
  319. CString strID = dc.m_strId;
  320. UINT nID = pFrame->ResourceManager()->GetStringID(strID);
  321. CXTPControl* pControl = pFrame->m_pControls->Add(type, nID);
  322. CString strToolTip = dc.m_strCaption;
  323. CXTPPaintManager::StripMnemonics(strToolTip);
  324. if (pControl->GetAction())
  325. {
  326. pControl->GetAction()->SetCaption(dc.m_strCaption);
  327. pControl->GetAction()->SetCategory(dc.m_strCategory);
  328. pControl->GetAction()->SetTooltip(strToolTip);
  329. pControl->GetAction()->SetKey(strID);
  330. }
  331. else
  332. {
  333. pControl->SetCaption(dc.m_strCaption);
  334. pControl->SetCategory(dc.m_strCategory);
  335. pControl->SetTooltip(strToolTip);
  336. }
  337. int nIndex = (int)m_wndListBox.SendMessage(LB_INSERTSTRING, m_wndListBox.GetCount(), (LPARAM)pControl);
  338. m_wndListBox.SetCurSel(nIndex);
  339. pFrame->GetCommandBars()->SetDragControl(pControl);
  340. GetMainFrame()->RefreshPanes(TRUE);
  341. }
  342. }
  343. void CPaneControls::OnUpdatePanecontrolsAdd(CCmdUI* pCmdUI)
  344. {
  345. pCmdUI->Enable(m_pActiveCommandBars != 0);
  346. }
  347. void CPaneControls::OnCategoryChanged()
  348. {
  349. int nIndex = m_wndCategories.GetCurSel();
  350. if (nIndex > 0)
  351. {
  352. m_wndCategories.GetLBText(nIndex, m_strSelectedCategory);
  353. }
  354. else
  355. {
  356. m_strSelectedCategory = _T("");
  357. }
  358. Refresh(TRUE);
  359. }
  360. void CPaneControls::OnControlChanged()
  361. {
  362. CXTPCommandBars* pCommandBars = m_pActiveCommandBars;
  363. if (!pCommandBars)
  364. {
  365. return;
  366. }
  367. int nIndex = m_wndListBox.GetCurSel();
  368. if ( nIndex != LB_ERR )
  369. {
  370. CXTPControl* pControl = ( CXTPControl* )m_wndListBox.GetItemData( nIndex );
  371. if (pControl)
  372. {
  373. pCommandBars->SetDragControl(pControl);
  374. }
  375. }
  376. }
  377. void CPaneControls::OnUpdatePanecontrolsUp(CCmdUI* pCmdUI)
  378. {
  379. int nIndex = m_wndListBox.GetCurSel();
  380. pCmdUI->Enable(nIndex > 0);
  381. }
  382. void CPaneControls::OnUpdatePanecontrolsDown(CCmdUI* pCmdUI)
  383. {
  384. int nIndex = m_wndListBox.GetCurSel();
  385. pCmdUI->Enable(nIndex != CB_ERR && nIndex < m_wndListBox.GetCount() - 1);
  386. }
  387. void CPaneControls::OnPanecontrolsUp()
  388. {
  389. int nIndex = m_wndListBox.GetCurSel();
  390. if (nIndex > 0)
  391. {
  392. CXTPControl* pControl = ( CXTPControl* )m_wndListBox.GetItemData( nIndex );
  393. CXTPControl* pControlBefore = ( CXTPControl* )m_wndListBox.GetItemData( nIndex - 1);
  394. pControl->GetControls()->MoveBefore(pControl, pControlBefore->GetIndex());
  395. Refresh(TRUE);
  396. }
  397. }
  398. void CPaneControls::OnPanecontrolsDown()
  399. {
  400. int nIndex = m_wndListBox.GetCurSel();
  401. if (nIndex != CB_ERR && nIndex < m_wndListBox.GetCount() - 1)
  402. {
  403. CXTPControl* pControl = ( CXTPControl* )m_wndListBox.GetItemData( nIndex );
  404. CXTPControl* pControlBefore = ( CXTPControl* )m_wndListBox.GetItemData( nIndex + 1);
  405. pControl->GetControls()->MoveBefore(pControl, pControlBefore->GetIndex() + 1);
  406. Refresh(TRUE);
  407. }
  408. }
  409. void CPaneControls::OnPanecontrolsDelete()
  410. {
  411. int nIndex = m_wndListBox.GetCurSel();
  412. if (nIndex != CB_ERR && m_pActiveCommandBars)
  413. {
  414. CXTPControl* pControl = ( CXTPControl* )m_wndListBox.GetItemData( nIndex );
  415. m_pActiveCommandBars->SetDragControl(NULL);
  416. pControl->GetControls()->Remove(pControl);
  417. Refresh(TRUE);
  418. }
  419. }
  420. void CPaneControls::OnUpdatePanecontrolsDelete(CCmdUI* pCmdUI)
  421. {
  422. int nIndex = m_wndListBox.GetCurSel();
  423. pCmdUI->Enable(nIndex != CB_ERR);
  424. }
  425. BOOL CPaneControls::OnDropLibrary()
  426. {
  427. if (!m_pActiveCommandBars)
  428. return FALSE;
  429. CEmbeddedFrame* pFrame = GetMainFrame()->GetActiveEmbeddedFrame();
  430. if (!pFrame)
  431. return FALSE;
  432. CXTPControlAction* pDragAction = GetMainFrame()->m_paneLibrary.m_pDragAction;
  433. if (!pDragAction)
  434. return FALSE;
  435. CXTPControlAction* pAction = m_pActiveCommandBars->GetActions()->FindAction(pDragAction->GetID());
  436. if (!pAction)
  437. {
  438. pAction = m_pActiveCommandBars->GetActions()->Add(pDragAction->GetID());
  439. }
  440. pAction->SetCaption(pDragAction->GetCaption());
  441. pAction->SetDescription(pDragAction->GetDescription());
  442. pAction->SetTooltip(pDragAction->GetTooltip());
  443. pAction->SetKey(pDragAction->GetKey());
  444. pAction->SetCategory(pDragAction->GetCategory());
  445. if (!pAction->GetKey().IsEmpty())
  446. {
  447. pFrame->ResourceManager()->Set(pAction->GetID(), pAction->GetKey());
  448. }
  449. CXTPImageManagerIconSet* pIcons = GetMainFrame()->m_paneLibrary.m_pIcons->GetIconSet(pDragAction->GetID());
  450. if (pIcons)
  451. {
  452. m_pActiveCommandBars->GetImageManager()->AddIcons(pIcons);
  453. }
  454. CXTPControl* pControl = pFrame->m_pControls->Add(xtpControlButton, pDragAction->GetID());
  455. int nIndex = (int)m_wndListBox.SendMessage(LB_INSERTSTRING, m_wndListBox.GetCount(), (LPARAM)pControl);
  456. m_wndListBox.SetCurSel(nIndex);
  457. pFrame->GetCommandBars()->SetDragControl(pControl);
  458. GetMainFrame()->RefreshPanes(TRUE);
  459. return TRUE;
  460. }