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

对话框与窗口

开发平台:

Visual C++

  1. // PaneLibrary.cpp: implementation of the CPaneLibrary class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CommandBarsDesigner.h"
  6. #include "PaneLibrary.h"
  7. #include "MainFrm.h"
  8. #include "PropertyItemFlags.h"
  9. CLIPFORMAT CPaneLibrary::m_cfItem = (CLIPFORMAT)::RegisterClipboardFormat(_T("PaneLibrary"));
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. #define IDC_TREE 100
  14. BEGIN_MESSAGE_MAP(CTreeLibrary, CTreeCtrl)
  15. //{{AFX_MSG_MAP(CTreeLibrary)
  16. //}}AFX_MSG_MAP
  17. END_MESSAGE_MAP()
  18. LRESULT CTreeLibrary::WindowProc(UINT message,WPARAM wParam,LPARAM lParam)
  19. {
  20. switch (message)
  21. {
  22. case WM_NCPAINT:
  23. {
  24. CTreeCtrl::WindowProc(message, wParam, lParam);
  25. CRect rc;
  26. GetWindowRect(&rc);
  27. CWindowDC dc(this);
  28. rc.OffsetRect(-rc.TopLeft());
  29. COLORREF clr = GetStaticFrameColor();
  30. dc.Draw3dRect(rc, clr, clr);
  31. return TRUE;
  32. }
  33. }
  34. return CTreeCtrl::WindowProc(message, wParam, lParam);
  35. }
  36. void CPaneLibrary::CActions::DeleteAction(CXTPControlAction* pAction)
  37. {
  38. for (int i = 0; i < GetCount(); i++)
  39. {
  40. if (GetAt(i) == pAction)
  41. {
  42. m_arrActions.RemoveAt(i);
  43. pAction->InternalRelease();
  44. }
  45. }
  46. }
  47. void CPaneLibrary::CActions::ReplaceActionId(CXTPControlAction* pAction, int nID)
  48. {
  49. if (FindAction(nID) != 0)
  50. return;
  51. for (int i = 0; i < GetCount(); i++)
  52. {
  53. if (GetAt(i) == pAction)
  54. {
  55. m_arrActions.RemoveAt(i);
  56. SetActionId(pAction, nID);
  57. Insert(pAction);
  58. }
  59. }
  60. }
  61. //////////////////////////////////////////////////////////////////////////
  62. //
  63. CPaneLibrary::CPaneLibrary()
  64. {
  65. m_pActions = new CActions();
  66. m_pIcons = new CXTPImageManager();
  67. m_pDragAction = NULL;
  68. }
  69. CPaneLibrary::~CPaneLibrary()
  70. {
  71. m_pActions->InternalRelease();
  72. delete m_pIcons;
  73. }
  74. BEGIN_MESSAGE_MAP(CPaneLibrary, CWnd)
  75. //{{AFX_MSG_MAP(CPaneLibrary)
  76. ON_WM_CREATE()
  77. ON_WM_SIZE()
  78. ON_WM_SETFOCUS()
  79. //}}AFX_MSG_MAP
  80. ON_NOTIFY(TVN_SELCHANGED, AFX_IDW_PANE_FIRST, OnTreeSelChange)
  81. ON_NOTIFY(NM_SETFOCUS, AFX_IDW_PANE_FIRST, OnTreeSetFocus)
  82. ON_NOTIFY(NM_RCLICK, AFX_IDW_PANE_FIRST, OnTreeRightClick)
  83. ON_NOTIFY(TVN_BEGINLABELEDIT, AFX_IDW_PANE_FIRST, OnTreeBeginLabelEdit)
  84. ON_NOTIFY(TVN_ENDLABELEDIT, AFX_IDW_PANE_FIRST, OnTreeEndLabelEdit)
  85. ON_NOTIFY(TVN_BEGINDRAG, AFX_IDW_PANE_FIRST, OnTreeBeginDrag)
  86. ON_NOTIFY(NM_CUSTOMDRAW, AFX_IDW_PANE_FIRST, OnTreeCustomDraw)
  87. ON_COMMAND(ID_LIBRARY_NEW, OnLibraryNew)
  88. ON_COMMAND(ID_LIBRARY_OPEN, OnLibraryOpen)
  89. ON_COMMAND(ID_LIBRARY_SAVE, OnLibrarySave)
  90. END_MESSAGE_MAP()
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CPaneProperties message handlers
  93. int CPaneLibrary::OnCreate(LPCREATESTRUCT lpCreateStruct)
  94. {
  95. if (CWnd::OnCreate(lpCreateStruct) == -1)
  96. return -1;
  97. VERIFY(m_wndToolBar.CreateToolBar(WS_TABSTOP|WS_VISIBLE|WS_CHILD|CBRS_TOOLTIPS, this));
  98. VERIFY(m_wndToolBar.LoadToolBar(IDR_PANE_LIBRARY));
  99. if (!m_wndTreeCtrl.Create(WS_CLIPCHILDREN|WS_CLIPSIBLINGS|WS_VISIBLE|TVS_HASLINES|
  100. TVS_LINESATROOT|TVS_HASBUTTONS|TVS_SHOWSELALWAYS|TVS_EDITLABELS, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST))
  101. {
  102. TRACE0("Failed to create view windown");
  103. return -1;
  104. }
  105. m_wndTreeCtrl.ModifyStyleEx(0, WS_EX_STATICEDGE);
  106. m_ilTreeIcons.Create(16, 16, ILC_MASK|ILC_COLOR24, 0, 1);
  107. CBitmap bmp;
  108. bmp.LoadBitmap(IDB_PANE_LIBRARY_ICONS);
  109. m_ilTreeIcons.Add(&bmp, RGB(0, 255, 0));
  110. m_wndTreeCtrl.SetImageList(&m_ilTreeIcons, TVSIL_NORMAL);
  111. CreateStandardActions();
  112. return 0;
  113. }
  114. void CPaneLibrary::OnSize(UINT nType, int cx, int cy)
  115. {
  116. CWnd::OnSize(nType, cx, cy);
  117. CSize sz(0);
  118. if (m_wndToolBar.GetSafeHwnd())
  119. {
  120. sz = m_wndToolBar.CalcDockingLayout(cx, /*LM_HIDEWRAP|*/ LM_HORZDOCK|LM_HORZ | LM_COMMIT);
  121. m_wndToolBar.MoveWindow(0, 0, cx, sz.cy);
  122. m_wndToolBar.Invalidate(FALSE);
  123. }
  124. if (m_wndTreeCtrl.GetSafeHwnd())
  125. {
  126. m_wndTreeCtrl.MoveWindow(0, sz.cy, cx, cy - sz.cy);
  127. }
  128. }
  129. void CPaneLibrary::OnSetFocus(CWnd*)
  130. {
  131. m_wndTreeCtrl.SetFocus();
  132. }
  133. void CPaneLibrary::OnTreeSelChange(NMHDR* pNMHDR, LRESULT*)
  134. {  
  135. NMTREEVIEW* pNMTreeView = (NMTREEVIEW *)pNMHDR;
  136. RefreshItem(pNMTreeView->itemNew.hItem);
  137. if (GetItemType(pNMTreeView->itemNew.hItem) == treeAction)
  138. {
  139. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(pNMTreeView->itemNew.hItem);
  140. GetMainFrame()->m_paneIcons.ShowIcons(m_pIcons, pAction->GetID());
  141. }
  142. }
  143. void CPaneLibrary::OnTreeSetFocus(NMHDR* /*pNMHDR*/, LRESULT* /*pResult*/)
  144. {
  145. HTREEITEM hItem = m_wndTreeCtrl.GetSelectedItem();
  146. if (hItem) RefreshItem(hItem);
  147. }
  148. void CPaneLibrary::RefreshItem(HTREEITEM hItem)
  149. {
  150. if (GetMainFrame()->m_pActiveCommandBars)
  151. {
  152. GetMainFrame()->m_pActiveCommandBars->SetDragControl(NULL);
  153. }
  154. GetMainFrame()->m_pActivePane = this;
  155. GetMainFrame()->m_paneProperties.Refresh(this);
  156. }
  157. TreeType CPaneLibrary::GetItemType(HTREEITEM hItem)
  158. {
  159. hItem = m_wndTreeCtrl.GetParentItem(hItem);
  160. if (!hItem)
  161. return treeRoot;
  162. hItem = m_wndTreeCtrl.GetParentItem(hItem);
  163. return hItem ? treeAction : treeCategory;
  164. }
  165. int CPaneLibrary::FindLastId()
  166. {
  167. int nCount = m_pActions->GetCount();
  168. int nId = 2000;
  169. for (int i = 0; i < nCount; i++)
  170. {
  171. if (m_pActions->GetAt(i)->GetID() >= nId && m_pActions->GetAt(i)->GetID() < 30000)
  172. {
  173. nId = m_pActions->GetAt(i)->GetID() + 1;
  174. }
  175. }
  176. return nId;
  177. }
  178. CString GetCategoryName(const CString& str)
  179. {
  180. if (str == _T("(None)"))
  181. return _T("");
  182. return str;
  183. }
  184. int CPaneLibrary::GetTreeChildCount(HTREEITEM hItem)
  185. {
  186. int nCount = 0;
  187. HTREEITEM hItemChild = m_wndTreeCtrl.GetChildItem(hItem);
  188. while (hItemChild)
  189. {
  190. nCount++;
  191. hItemChild = m_wndTreeCtrl.GetNextSiblingItem(hItemChild);
  192. }
  193. return nCount;
  194. }
  195. void CPaneLibrary::OnTreeRightClick(NMHDR* pNMHDR, LRESULT*)
  196. {
  197. CPoint point, ptScreen;
  198. ::GetCursorPos(&ptScreen);
  199. point = ptScreen;
  200. m_wndTreeCtrl.ScreenToClient(&point);
  201. TV_HITTESTINFO tvhti;
  202. tvhti.pt = point;
  203. m_wndTreeCtrl.HitTest(&tvhti);
  204. if ((tvhti.flags & (TVHT_ONITEMLABEL | TVHT_ONITEMICON)) && (tvhti.hItem != NULL))
  205. {
  206. TreeType type = GetItemType(tvhti.hItem);
  207. if (type == treeRoot)
  208. {
  209. CMenu menu;
  210. menu.LoadMenu(IDR_MENU_LIBRARY);
  211. UINT nCmd = TrackPopupMenu(menu.GetSubMenu(0)->GetSafeHmenu(), TPM_RETURNCMD, ptScreen.x, ptScreen.y, 0, m_hWnd, 0);
  212. if (nCmd == ID_ROOT_INSERT)
  213. {
  214. CString strCaption;
  215. strCaption.Format(_T("Category%i"), GetTreeChildCount(m_wndTreeCtrl.GetRootItem()));
  216. HTREEITEM hItem = m_wndTreeCtrl.InsertItem(strCaption, 0, 0, tvhti.hItem);
  217. m_wndTreeCtrl.SelectItem(hItem);
  218. }
  219. return;
  220. }
  221. if (type == treeCategory)
  222. {
  223. CMenu menu;
  224. menu.LoadMenu(IDR_MENU_LIBRARY);
  225. UINT nCmd = TrackPopupMenu(menu.GetSubMenu(1)->GetSafeHmenu(), TPM_RETURNCMD, ptScreen.x, ptScreen.y, 0, m_hWnd, 0);
  226. if (nCmd == ID_CATEGORY_ADDACTION)
  227. {
  228. int nID = FindLastId();
  229. CXTPControlAction* pAction = m_pActions->Add(nID);
  230. CString strCaption;
  231. strCaption.Format(_T("Action%i"), nID);
  232. pAction->SetCaption(strCaption);
  233. pAction->SetKey(strCaption);
  234. pAction->SetCategory(GetCategoryName(m_wndTreeCtrl.GetItemText(tvhti.hItem)));
  235. HTREEITEM hItem = m_wndTreeCtrl.InsertItem(strCaption, 1, 1, tvhti.hItem);
  236. m_wndTreeCtrl.SetItemData(hItem, (DWORD_PTR)pAction);
  237. m_wndTreeCtrl.SelectItem(hItem);
  238. }
  239. if (nCmd == ID_CATEGORY_DELETECATEGORY)
  240. {
  241. m_wndTreeCtrl.DeleteItem(tvhti.hItem);
  242. }
  243. return;
  244. }
  245. if (type == treeAction)
  246. {
  247. CMenu menu;
  248. menu.LoadMenu(IDR_MENU_LIBRARY);
  249. UINT nCmd = TrackPopupMenu(menu.GetSubMenu(2)->GetSafeHmenu(), TPM_RETURNCMD, ptScreen.x, ptScreen.y, 0, m_hWnd, 0);
  250. if (nCmd == ID_ACTION_DELETEACTION)
  251. {
  252. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(tvhti.hItem);
  253. m_wndTreeCtrl.DeleteItem(tvhti.hItem);
  254. m_pActions->DeleteAction(pAction);
  255. }
  256. return;
  257. }
  258. }
  259. }
  260. void CPaneLibrary::OnTreeBeginLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
  261. {
  262. LPNMTVDISPINFO ptvdi = (LPNMTVDISPINFO)pNMHDR; 
  263. TreeType type = GetItemType(ptvdi->item.hItem);
  264. if (type == treeRoot)
  265. {
  266. *pResult = 1;
  267. }
  268. else if (type == treeCategory)
  269. {
  270. if (GetCategoryName(m_wndTreeCtrl.GetItemText(ptvdi->item.hItem)).IsEmpty())
  271. {
  272. *pResult = 1;
  273. }
  274. }
  275. }
  276. void CPaneLibrary::OnTreeEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
  277. {
  278. LPNMTVDISPINFO ptvdi = (LPNMTVDISPINFO)pNMHDR; 
  279. if (!ptvdi->item.pszText || ptvdi->item.pszText[0] == 0)
  280. {
  281. *pResult = FALSE;
  282. return;
  283. }
  284. *pResult = TRUE;
  285. TreeType type = GetItemType(ptvdi->item.hItem);
  286. if (type == treeAction)
  287. {
  288. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(ptvdi->item.hItem);
  289. if (!pAction)
  290. {
  291. *pResult = FALSE;
  292. return;
  293. }
  294. pAction->SetCaption(ptvdi->item.pszText);
  295. RefreshItem(ptvdi->item.hItem);
  296. }
  297. else if (type == treeCategory)
  298. {
  299. if (GetCategoryName(ptvdi->item.pszText).IsEmpty())
  300. {
  301. *pResult = FALSE;
  302. return;
  303. }
  304. HTREEITEM hItemChild = m_wndTreeCtrl.GetChildItem(ptvdi->item.hItem);
  305. while (hItemChild)
  306. {
  307. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(hItemChild);
  308. ASSERT(pAction);
  309. if (pAction)
  310. {
  311. pAction->SetCategory(ptvdi->item.pszText);
  312. }
  313. hItemChild = m_wndTreeCtrl.GetNextSiblingItem(hItemChild);
  314. }
  315. }
  316. }
  317. CObject* CPaneLibrary::RefreshPropertyGrid(CXTPPropertyGrid* pPropertyGrid) 
  318. {
  319. HTREEITEM hItem = m_wndTreeCtrl.GetSelectedItem();
  320. if (!hItem) 
  321. return NULL;
  322. if (GetItemType(hItem) != treeAction)
  323. return NULL;
  324. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(hItem);
  325. if (!pAction)
  326. return NULL;
  327. CXTPPropertyGridItem* pCategory = pPropertyGrid->AddCategory(ID_GRID_CATEGORY_APPEARANCE);
  328. pCategory->AddChildItem(new CXTPPropertyGridItemNumber(ID_GRID_ITEM_CONTROL_ID, pAction->GetID()));
  329. pCategory->AddChildItem(new CXTPPropertyGridItem(ID_GRID_ITEM_ACTION_KEY, pAction->GetKey()));
  330. pCategory->AddChildItem(new CXTPPropertyGridItem(ID_GRID_ITEM_ACTION_CAPTION, pAction->GetCaption()));
  331. pCategory->AddChildItem(new CXTPPropertyGridItem(ID_GRID_ITEM_ACTION_DESCRIPTION, pAction->GetDescription()));
  332. pCategory->AddChildItem(new CXTPPropertyGridItem(ID_GRID_ITEM_ACTION_TOOLTIP, pAction->GetTooltip()));
  333. CXTPPropertyGridItem* pItemControlCategory = pCategory->AddChildItem(new CXTPPropertyGridItem(ID_GRID_ITEM_ACTION_CATEGORY, pAction->GetCategory()));
  334. pItemControlCategory->SetFlags(xtpGridItemHasComboButton | xtpGridItemHasEdit);
  335. HTREEITEM hItemRoot = m_wndTreeCtrl.GetRootItem();
  336. HTREEITEM hItemChild = m_wndTreeCtrl.GetChildItem(hItemRoot);
  337. while (hItemChild)
  338. {
  339. CString strItem = m_wndTreeCtrl.GetItemText(hItemChild);
  340. if (!GetCategoryName(strItem).IsEmpty())
  341. {
  342. pItemControlCategory->GetConstraints()->AddConstraint(strItem);
  343. }
  344. hItemChild = m_wndTreeCtrl.GetNextSiblingItem(hItemChild);
  345. }
  346. pCategory->Expand();
  347. return pAction;
  348. }
  349. BOOL CPaneLibrary::OnPropertyGridValueChanged(CObject* pActiveObject, CXTPPropertyGridItem* pItem)
  350. {
  351. CXTPControlAction* pAction = DYNAMIC_DOWNCAST(CXTPControlAction, pActiveObject);
  352. if (!pAction)
  353. return FALSE;
  354. HTREEITEM hItem = m_wndTreeCtrl.GetSelectedItem();
  355. if (m_wndTreeCtrl.GetItemData(hItem) != (DWORD_PTR)pAction)
  356. return FALSE;
  357. switch (pItem->GetID())
  358. {
  359. case ID_GRID_ITEM_ACTION_KEY:
  360. pAction->SetKey(pItem->GetValue());
  361. return TRUE;
  362. case ID_GRID_ITEM_ACTION_CAPTION:
  363. pAction->SetCaption(pItem->GetValue());
  364. m_wndTreeCtrl.SetItemText(hItem, pAction->GetCaption());
  365. return TRUE;
  366. case ID_GRID_ITEM_CONTROL_ID:
  367. m_pActions->ReplaceActionId(pAction, GetNumberValue(pItem));
  368. return TRUE;
  369. case ID_GRID_ITEM_ACTION_DESCRIPTION:
  370. pAction->SetDescription(pItem->GetValue());
  371. return TRUE;
  372. case ID_GRID_ITEM_ACTION_TOOLTIP:
  373. pAction->SetTooltip(pItem->GetValue());
  374. return TRUE;
  375. case ID_GRID_ITEM_ACTION_CATEGORY:
  376. {
  377. pAction->SetCategory(pItem->GetValue());
  378. HTREEITEM hItemRoot = m_wndTreeCtrl.GetRootItem();
  379. HTREEITEM hItemChild = m_wndTreeCtrl.GetChildItem(hItemRoot);
  380. CString strCategory = pAction->GetCategory();
  381. if (strCategory.IsEmpty()) strCategory = _T("(None)");
  382. while (hItemChild)
  383. {
  384. CString strItem = m_wndTreeCtrl.GetItemText(hItemChild);
  385. if (GetCategoryName(strItem) == strCategory)
  386. {
  387. break;
  388. }
  389. hItemChild = m_wndTreeCtrl.GetNextSiblingItem(hItemChild);
  390. }
  391. if (!hItemChild)
  392. {
  393. hItemChild = m_wndTreeCtrl.InsertItem(strCategory, 0, 0, hItemRoot);
  394. }
  395. m_wndTreeCtrl.DeleteItem(hItem);
  396. HTREEITEM hNewItem = m_wndTreeCtrl.InsertItem(pAction->GetCaption(), 0, 0, hItemChild);
  397. m_wndTreeCtrl.SetItemData(hNewItem, (DWORD_PTR)pAction);
  398. m_wndTreeCtrl.SelectItem(hNewItem);
  399. }
  400. return TRUE;
  401. }
  402. return FALSE;
  403. }
  404. void CPaneLibrary::OnTreeCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
  405. {
  406. NMTVCUSTOMDRAW* lpLVCD = reinterpret_cast<NMTVCUSTOMDRAW*>(pNMHDR);
  407. *pResult = CDRF_DODEFAULT;
  408. switch (lpLVCD->nmcd.dwDrawStage)
  409. {
  410. case CDDS_PREPAINT:
  411. *pResult = CDRF_NOTIFYITEMDRAW;
  412. break;
  413. case CDDS_PREPAINT | CDDS_ITEM:
  414. {
  415. HTREEITEM hItem = (HTREEITEM)lpLVCD->nmcd.dwItemSpec;
  416. if (GetItemType(hItem) != treeAction)
  417. return;
  418. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(hItem);
  419. if (!pAction)
  420. return;
  421. *pResult |= CDRF_NOTIFYPOSTPAINT;
  422. break;
  423. }
  424. case CDDS_POSTPAINT | CDDS_ITEM:
  425. case CDDS_POSTPAINT | CDDS_ITEM  | CDDS_SUBITEM:
  426. {
  427. HTREEITEM hItem = (HTREEITEM)lpLVCD->nmcd.dwItemSpec;
  428. CXTPControlAction* pAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(hItem);
  429. if (!pAction)
  430. return;
  431. CSize sz(16, 16);
  432. CXTPImageManagerIcon* pIcon = m_pIcons->GetImage(pAction->GetIconId(), 16);
  433. if (!pIcon)
  434. return;
  435. CRect rc(0, 0, 0, 0);
  436. m_wndTreeCtrl.GetItemRect(hItem, rc, TRUE);
  437. if (!rc.IsRectEmpty())
  438. {
  439. XTPImageState imageState = xtpImageNormal;
  440. CPoint pt(rc.left - 16 - 2, (rc.top + rc.bottom - sz.cy) / 2);
  441. pIcon->Draw(CDC::FromHandle(lpLVCD->nmcd.hdc), pt, pIcon->GetIcon(imageState), sz);
  442. }
  443. break;
  444. }
  445. }
  446. }
  447. void CPaneLibrary::OnTreeBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
  448. {
  449. LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)pNMHDR;
  450. if (GetItemType(pnmtv->itemNew.hItem) != treeAction)
  451. return;
  452. m_pDragAction = (CXTPControlAction*)m_wndTreeCtrl.GetItemData(pnmtv->itemNew.hItem);
  453. if (!m_pDragAction)
  454. return;
  455. COleDataSource dataSource;
  456. HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, 1);
  457. dataSource.CacheGlobalData(m_cfItem, hGlobal);
  458. dataSource.DoDragDrop(DROPEFFECT_COPY);
  459. m_pDragAction = NULL;
  460. }
  461. void CPaneLibrary::OnLibraryNew()
  462. {
  463. if (AfxMessageBox(_T("Are you sure you want remove all actions?"), MB_YESNO) != IDYES)
  464. return;
  465. m_wndTreeCtrl.DeleteAllItems();
  466. m_pActions->RemoveAll();
  467. FillActions();
  468. }
  469. void CPaneLibrary::AddAction(int nID, LPCTSTR lpszKey, LPCTSTR lpszCategory)
  470. {
  471. CXTPControlAction* pAction = m_pActions->Add(nID);
  472. pAction->SetCategory(lpszCategory);
  473. pAction->SetKey(lpszKey);
  474. }
  475. void CPaneLibrary::CreateStandardActions()
  476. {
  477. m_wndTreeCtrl.DeleteAllItems();
  478. m_pActions->RemoveAll();
  479. m_pIcons->RemoveAll();
  480. CXTPPropExchangeXMLNode px(TRUE, NULL, _T("DesignerFile"));
  481. if (px.LoadFromResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_PANE_LIBRARY_ACTIONS), _T("XML")))
  482. {
  483. CXTPPropExchangeSection secCategories(px.GetSection(_T("Categories")));
  484. ExchangeCategories(&secCategories);
  485. CXTPPropExchangeSection secActions(px.GetSection(_T("Actions")));
  486. m_pActions->DoPropExchange(&secActions);
  487. CXTPPropExchangeSection secImages(px.GetSection(_T("Images")));
  488. m_pIcons->DoPropExchange(&secImages);
  489. }
  490. FillActions();
  491. }
  492. void CPaneLibrary::OnLibraryOpen()
  493. {
  494. CString strFilter = _T("XML Document (*.xml)|*.xml|All files (*.*)|*.*||");
  495. CFileDialog fd(TRUE, _T("xml"), NULL, OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, strFilter);
  496. if (fd.DoModal() != IDOK)
  497. return;
  498. CXTPPropExchangeXMLNode px(TRUE, NULL, _T("DesignerFile"));
  499. if (!px.LoadFromFile(fd.GetPathName()))
  500. return;
  501. m_wndTreeCtrl.SetRedraw(FALSE);
  502. m_wndTreeCtrl.DeleteAllItems();
  503. m_pActions->RemoveAll();
  504. m_pIcons->RemoveAll();
  505. CXTPPropExchangeSection secCategories(px.GetSection(_T("Categories")));
  506. ExchangeCategories(&secCategories);
  507. CXTPPropExchangeSection secActions(px.GetSection(_T("Actions")));
  508. m_pActions->DoPropExchange(&secActions);
  509. CXTPPropExchangeSection secImages(px.GetSection(_T("Images")));
  510. m_pIcons->DoPropExchange(&secImages);
  511. FillActions();
  512. m_wndTreeCtrl.SetRedraw(TRUE);
  513. }
  514. void CPaneLibrary::ExchangeCategories(CXTPPropExchange* pPX)
  515. {
  516. if (pPX->IsLoading())
  517. {
  518. HTREEITEM hItemRoot = m_wndTreeCtrl.GetRootItem();
  519. if (!hItemRoot)
  520. {
  521. m_wndTreeCtrl.InsertItem(_T("Categories"), 0, 0);
  522. }
  523. for (int i = 0; ;i++)
  524. {
  525. CString strCategory;
  526. strCategory.Format(_T("Category%i"), i);
  527. CString strItem;
  528. if (!PX_String(pPX, strCategory, strItem))
  529. break;
  530. AddCategory(strItem);
  531. }
  532. }
  533. else
  534. {
  535. HTREEITEM hItemRoot = m_wndTreeCtrl.GetRootItem();
  536. HTREEITEM hItemChild = m_wndTreeCtrl.GetChildItem(hItemRoot);
  537. int i = 0;
  538. while (hItemChild)
  539. {
  540. CString strItem = GetCategoryName(m_wndTreeCtrl.GetItemText(hItemChild));
  541. CString strCategory;
  542. strCategory.Format(_T("Category%i"), i);
  543. PX_String(pPX, strCategory, strItem);
  544. hItemChild = m_wndTreeCtrl.GetNextSiblingItem(hItemChild);
  545. i++;
  546. }
  547. }
  548. }
  549. HTREEITEM CPaneLibrary::AddCategory(CString strCategory)
  550. {
  551. HTREEITEM hItemRoot = m_wndTreeCtrl.GetRootItem();
  552. HTREEITEM hItemChild = m_wndTreeCtrl.GetChildItem(hItemRoot);
  553. while (hItemChild)
  554. {
  555. CString strItem = m_wndTreeCtrl.GetItemText(hItemChild);
  556. if (GetCategoryName(strItem) == strCategory)
  557. {
  558. break;
  559. }
  560. hItemChild = m_wndTreeCtrl.GetNextSiblingItem(hItemChild);
  561. }
  562. if (!hItemChild)
  563. {
  564. hItemChild = m_wndTreeCtrl.InsertItem(strCategory.IsEmpty() ? _T("(None)") : strCategory, 0, 0, hItemRoot);
  565. m_wndTreeCtrl.Expand(hItemChild, TVE_EXPAND);
  566. }
  567. return hItemChild;
  568. }
  569. void CPaneLibrary::FillActions()
  570. {
  571. HTREEITEM hItemRoot = m_wndTreeCtrl.GetRootItem();
  572. if (!hItemRoot)
  573. {
  574. hItemRoot = m_wndTreeCtrl.InsertItem(_T("Categories"), 0, 0);
  575. }
  576. int nCount = m_pActions->GetCount();
  577. for (int i = 0; i < nCount; i++)
  578. {
  579. CXTPControlAction* pAction = m_pActions->GetAt(i);
  580. HTREEITEM hItemChild = AddCategory(pAction->GetCategory());
  581. HTREEITEM hNewItem = m_wndTreeCtrl.InsertItem(pAction->GetCaption(), 1, 1, hItemChild);
  582. m_wndTreeCtrl.SetItemData(hNewItem, (DWORD_PTR)pAction);
  583. m_wndTreeCtrl.Expand(hItemChild, TVE_EXPAND);
  584. }
  585. m_wndTreeCtrl.Expand(hItemRoot, TVE_EXPAND);
  586. }
  587. void CPaneLibrary::OnLibrarySave()
  588. {
  589. CString strFilter = _T("XML Document (*.xml)|*.xml|All files (*.*)|*.*||");
  590. CFileDialog fd(FALSE, _T("xml"), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter);
  591. if (fd.DoModal() != IDOK)
  592. return;
  593. CXTPPropExchangeXMLNode px(FALSE, 0, _T("DesignerFile"));
  594. CXTPPropExchangeSection secCategories(px.GetSection(_T("Categories")));
  595. ExchangeCategories(&secCategories);
  596. CXTPPropExchangeSection secActions(px.GetSection(_T("Actions")));
  597. m_pActions->DoPropExchange(&secActions);
  598. CXTPPropExchangeSection secImages(px.GetSection(_T("Images")));
  599. m_pIcons->DoPropExchange(&secImages);
  600. px.SaveToFile(fd.GetFileName());
  601. }