home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / treevwx / tvx3demo.exe / data.1 / TreeViewXDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-01  |  7.5 KB  |  298 lines

  1. // TreeViewXDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "TreeViewX.h"
  6. #include "TreeViewXDlg.h"
  7. #include "pvibranch.h"
  8. #include "winxdef.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18.  
  19. class CAboutDlg : public CDialog
  20. {
  21. public:
  22.     CAboutDlg();
  23.  
  24. // Dialog Data
  25.     //{{AFX_DATA(CAboutDlg)
  26.     enum { IDD = IDD_ABOUTBOX };
  27.     //}}AFX_DATA
  28.  
  29.     // ClassWizard generated virtual function overrides
  30.     //{{AFX_VIRTUAL(CAboutDlg)
  31.     protected:
  32.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  33.     //}}AFX_VIRTUAL
  34.  
  35. // Implementation
  36. protected:
  37.     //{{AFX_MSG(CAboutDlg)
  38.     //}}AFX_MSG
  39.     DECLARE_MESSAGE_MAP()
  40. };
  41.  
  42. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  43. {
  44.     //{{AFX_DATA_INIT(CAboutDlg)
  45.     //}}AFX_DATA_INIT
  46. }
  47.  
  48. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  49. {
  50.     CDialog::DoDataExchange(pDX);
  51.     //{{AFX_DATA_MAP(CAboutDlg)
  52.     //}}AFX_DATA_MAP
  53. }
  54.  
  55. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  56.     //{{AFX_MSG_MAP(CAboutDlg)
  57.         // No message handlers
  58.     //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CTreeViewXDlg dialog
  63.  
  64. CTreeViewXDlg::CTreeViewXDlg(CWnd* pParent /*=NULL*/)
  65.     : CDialog(CTreeViewXDlg::IDD, pParent)
  66. {
  67.     //{{AFX_DATA_INIT(CTreeViewXDlg)
  68.         // NOTE: the ClassWizard will add member initialization here
  69.     //}}AFX_DATA_INIT
  70.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  71.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  72. }
  73.  
  74. void CTreeViewXDlg::DoDataExchange(CDataExchange* pDX)
  75. {
  76.     CDialog::DoDataExchange(pDX);
  77.     //{{AFX_DATA_MAP(CTreeViewXDlg)
  78.     DDX_Control(pDX, IDC_PVTREEVIEWXCTRL1, m_treeview);
  79.     //}}AFX_DATA_MAP
  80. }
  81.  
  82. BEGIN_MESSAGE_MAP(CTreeViewXDlg, CDialog)
  83.     //{{AFX_MSG_MAP(CTreeViewXDlg)
  84.     ON_WM_SYSCOMMAND()
  85.     ON_WM_PAINT()
  86.     ON_WM_QUERYDRAGICON()
  87.     ON_BN_CLICKED(IDC_SELECTPREVIOUSNODE, OnSelectpreviousnode)
  88.     ON_BN_CLICKED(IDC_SELECTNEXTNODE, OnSelectnextnode)
  89.     ON_BN_CLICKED(IDC_DELETECURRENTNODE, OnDeletecurrentnode)
  90.     ON_BN_CLICKED(IDC_INSERTNEWNODE, OnInsertnewnode)
  91.     ON_BN_CLICKED(IDC_ADDROOTNODE, OnAddrootnode)
  92.     ON_BN_CLICKED(IDC_CHANGETEXT, OnChangetext)
  93.     //}}AFX_MSG_MAP
  94. END_MESSAGE_MAP()
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CTreeViewXDlg message handlers
  98.  
  99. BOOL CTreeViewXDlg::OnInitDialog()
  100. {
  101.     CDialog::OnInitDialog();
  102.  
  103.     // Add "About..." menu item to system menu.
  104.  
  105.     // IDM_ABOUTBOX must be in the system command range.
  106.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  107.     ASSERT(IDM_ABOUTBOX < 0xF000);
  108.  
  109.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  110.     if (pSysMenu != NULL)
  111.     {
  112.         CString strAboutMenu;
  113.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  114.         if (!strAboutMenu.IsEmpty())
  115.         {
  116.             pSysMenu->AppendMenu(MF_SEPARATOR);
  117.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  118.         }
  119.     }
  120.  
  121.     // Set the icon for this dialog.  The framework does this automatically
  122.     //  when the application's main window is not a dialog
  123.     SetIcon(m_hIcon, TRUE);            // Set big icon
  124.     SetIcon(m_hIcon, FALSE);        // Set small icon
  125.     
  126.     // TODO: Add extra initialization here
  127.     CPVIBranch parent;
  128.  
  129.     m_treeview.SetAutoOpen(TRUE);
  130.     m_treeview.SetAlwaysShowSelection(TRUE);
  131.  
  132.     parent = m_treeview.GetBranches().Add(pvtPositionInOrder, 0, "Parent Node");
  133.     parent.Select(pvtNode);
  134.     parent.Add(pvtPositionInOrder, 0, "Child Node 1");
  135.     parent.Add(pvtPositionInOrder, 0, "Child Node 2");
  136.     parent.Add(pvtPositionInOrder, 0, "Child Node 3");
  137.     
  138.     parent = m_treeview.GetBranches().Add(pvtPositionInOrder, 0, "Parent Node");
  139.     parent.Add(pvtPositionInOrder, 0, "Child Node 4");
  140.  
  141.     return TRUE;  // return TRUE  unless you set the focus to a control
  142. }
  143.  
  144. void CTreeViewXDlg::OnSysCommand(UINT nID, LPARAM lParam)
  145. {
  146.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  147.     {
  148.         CAboutDlg dlgAbout;
  149.         dlgAbout.DoModal();
  150.     }
  151.     else
  152.     {
  153.         CDialog::OnSysCommand(nID, lParam);
  154.     }
  155. }
  156.  
  157. // If you add a minimize button to your dialog, you will need the code below
  158. //  to draw the icon.  For MFC applications using the document/view model,
  159. //  this is automatically done for you by the framework.
  160.  
  161. void CTreeViewXDlg::OnPaint() 
  162. {
  163.     if (IsIconic())
  164.     {
  165.         CPaintDC dc(this); // device context for painting
  166.  
  167.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  168.  
  169.         // Center icon in client rectangle
  170.         int cxIcon = GetSystemMetrics(SM_CXICON);
  171.         int cyIcon = GetSystemMetrics(SM_CYICON);
  172.         CRect rect;
  173.         GetClientRect(&rect);
  174.         int x = (rect.Width() - cxIcon + 1) / 2;
  175.         int y = (rect.Height() - cyIcon + 1) / 2;
  176.  
  177.         // Draw the icon
  178.         dc.DrawIcon(x, y, m_hIcon);
  179.     }
  180.     else
  181.     {
  182.         CDialog::OnPaint();
  183.     }
  184. }
  185.  
  186. // The system calls this to obtain the cursor to display while the user drags
  187. //  the minimized window.
  188. HCURSOR CTreeViewXDlg::OnQueryDragIcon()
  189. {
  190.     return (HCURSOR) m_hIcon;
  191. }
  192.  
  193. void CTreeViewXDlg::OnSelectpreviousnode() 
  194. {
  195.     // TODO: Add your control notification handler code here
  196.     CPVIBranch Selected;
  197.     
  198.     // gets the currently selected item
  199.     Selected = m_treeview.GetBranches().Get(pvtGetNextSelected, 0);
  200.     if (Selected.IsValid())
  201.     {
  202.         // gets the previous visible node
  203.         Selected = Selected.Get(pvtGetPrevVisible, 0);
  204.  
  205.         // selected the node
  206.         Selected.Select(pvtNode);
  207.     }
  208.     else
  209.     {
  210.         Selected = m_treeview.GetBranches().Get(pvtGetChild, 0);
  211.         if (Selected.IsValid())
  212.             Selected.Select(pvtNode);
  213.     }
  214. }
  215.  
  216. void CTreeViewXDlg::OnSelectnextnode() 
  217. {
  218.     // TODO: Add your control notification handler code here
  219.     CPVIBranch    Selected;
  220.     
  221.     // gets the currently selected item
  222.     Selected = m_treeview.GetBranches().Get(pvtGetNextSelected, 0);
  223.  
  224.     if (Selected.IsValid())
  225.     {
  226.         // gets the next visible item
  227.         Selected = Selected.Get(pvtGetNextVisible, 0);
  228.  
  229.         // selects the item
  230.         Selected.Select(pvtNode);
  231.     }
  232.     else
  233.     {
  234.         Selected = m_treeview.GetBranches().Get(pvtGetChild, 0);
  235.         if (Selected.IsValid())
  236.                 Selected.Select(pvtNode);
  237.     }
  238. }
  239.  
  240. void CTreeViewXDlg::OnDeletecurrentnode() 
  241. {
  242.     // TODO: Add your control notification handler code here
  243.     CPVIBranch Selected;
  244.  
  245.     Selected = m_treeview.GetBranches().Get(pvtGetNextSelected, 0);
  246.  
  247.     if (!Selected.IsValid())
  248.         return;
  249.  
  250.     Selected.Clear();    // remove children
  251.     Selected.Remove();    // remove node
  252. }
  253.  
  254. void CTreeViewXDlg::OnInsertnewnode() 
  255. {
  256.     // TODO: Add your control notification handler code here
  257.     CPVIBranch Selected;
  258.  
  259.     Selected = m_treeview.GetBranches().Get(pvtGetNextSelected, 0);
  260.     
  261.     if (!Selected.IsValid())
  262.         return;
  263.  
  264.     Selected.Add(pvtPositionInOrder, 0, "Newly inserted Child Node");
  265. }
  266.  
  267. void CTreeViewXDlg::OnAddrootnode() 
  268. {
  269.     // TODO: Add your control notification handler code here
  270.     m_treeview.GetBranches().Add(pvtPositionInOrder, 0, "New Parent Node");
  271. }
  272.  
  273. BEGIN_EVENTSINK_MAP(CTreeViewXDlg, CDialog)
  274.     //{{AFX_EVENTSINK_MAP(CTreeViewXDlg)
  275.     ON_EVENT(CTreeViewXDlg, IDC_PVTREEVIEWXCTRL1, -602 /* KeyDown */, OnKeyDownPvtreeviewxctrl1, VTS_PI2 VTS_I2)
  276.     //}}AFX_EVENTSINK_MAP
  277. END_EVENTSINK_MAP()
  278.  
  279. void CTreeViewXDlg::OnKeyDownPvtreeviewxctrl1(short FAR* KeyCode, short Shift) 
  280. {
  281.     // TODO: Add your control notification handler code here
  282.     if (*KeyCode == VK_RETURN)
  283.         m_treeview.BeginInPlaceEdit();
  284. }
  285.  
  286. void CTreeViewXDlg::OnChangetext() 
  287. {
  288.     // TODO: Add your control notification handler code here
  289.     CPVIBranch Selected;
  290.  
  291.     Selected = m_treeview.GetBranches().Get(pvtGetNextSelected, 0);
  292.     if(!Selected)
  293.         return;
  294.     if(Selected.IsValid() == FALSE)
  295.         return;
  296.     m_treeview.BeginInPlaceEdit();    
  297. }
  298.