home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / daoview / treeview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  12.2 KB  |  524 lines

  1. // TreeView.cpp : implementation of the CDaoTreeView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "DaoView.h"
  15. #include "dragitem.h"
  16. #include "dlgsql.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDaoTreeView
  25.  
  26. IMPLEMENT_DYNCREATE(CDaoTreeView, CTreeView)
  27.  
  28. BEGIN_MESSAGE_MAP(CDaoTreeView, CTreeView)
  29.     //{{AFX_MSG_MAP(CDaoTreeView)
  30.     ON_WM_CREATE()
  31.     ON_WM_SYSCOLORCHANGE()
  32.     ON_COMMAND(ID_EDIT_NEWQUERY, OnNewQuery)
  33.     ON_COMMAND(ID_QUERY_EDIT, OnQueryEdit)
  34.     //}}AFX_MSG_MAP
  35.     ON_COMMAND_RANGE(ID_TABLE_SCHEMA,ID_QUERY_RUN, OnPopupCommand)
  36.     ON_NOTIFY_REFLECT(TVN_KEYDOWN,OnKeyDown)
  37.     ON_NOTIFY_REFLECT(TVN_SELCHANGED,OnNodeSelect)
  38.     ON_NOTIFY_REFLECT(TVN_BEGINDRAG,OnBeginDrag)
  39.     ON_NOTIFY_REFLECT(NM_RCLICK,OnRightClick)
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CDaoTreeView construction/destruction
  44.  
  45. CDaoTreeView::CDaoTreeView() : CTreeView()
  46. {
  47.     m_nIDClipFormat = RegisterClipboardFormat(_T("DaoView"));
  48.     m_pDB = NULL;
  49.     m_bNoNotifications = FALSE;
  50. }
  51.  
  52. CDaoTreeView::~CDaoTreeView()
  53. {
  54. }
  55.  
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CDaoTreeView diagnostics
  59.  
  60. #ifdef _DEBUG
  61. void CDaoTreeView::AssertValid() const
  62. {
  63.     CTreeView::AssertValid();
  64. }
  65.  
  66. void CDaoTreeView::Dump(CDumpContext& dc) const
  67. {
  68.     CTreeView::Dump(dc);
  69. }
  70.  
  71. #endif //_DEBUG
  72. CDaoViewDoc* CDaoTreeView::GetDocument() // non-debug version is inline
  73. {
  74.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDaoViewDoc)));
  75.     return (CDaoViewDoc*)m_pDocument;
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CDaoTreeView message handlers
  80.  
  81. int CDaoTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  82. {
  83.     lpCreateStruct->style |= TVS_HASLINES | TVS_HASBUTTONS;
  84.     if (CTreeView::OnCreate(lpCreateStruct) == -1)
  85.         return -1;
  86.  
  87.     GetDocument()->m_pTreeView = this;
  88.  
  89.     // Create the Image List
  90.     m_ctlImage.Create(IDB_IMAGELIST3,16,0,RGB(255,0,255));
  91.     m_ctlImage.SetBkColor(GetSysColor(COLOR_WINDOW));
  92.  
  93.     /// Attach image list to Tree
  94.     CTreeCtrlEx& ctlTree = (CTreeCtrlEx&) GetTreeCtrl();
  95.     ctlTree.SetImageList(&m_ctlImage);
  96.  
  97.     m_dropTarget.Register(this);
  98.  
  99.     return 0;
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CDaoTreeView implementation functions
  104.  
  105. void CDaoTreeView::PopulateTree()
  106. {
  107.     CTreeCtrlEx& ctlTree = (CTreeCtrlEx&) GetTreeCtrl();
  108.  
  109.     m_bNoNotifications = TRUE;
  110.  
  111.     ctlTree.DeleteAllItems();
  112.     UpdateWindow();
  113.  
  114.     ASSERT(m_pDB);
  115.     ASSERT(m_pDB->IsOpen());
  116.  
  117.     // Insert root node by call to tree object this time
  118.     tDatabase = ctlTree.GetRootItem().AddTail(m_pDB->GetName(),IID_DATABASE);
  119.  
  120.     // From now on call via iterators
  121.     tTables = tDatabase.AddTail(_T("Tables"),IID_TABLES);
  122.     tRelations = tDatabase.AddTail(_T("Relations"),IID_RELATIONS);
  123.     tQueryDefs = tDatabase.AddTail(_T("QueryDefs"),IID_QUERYDEFS);
  124.  
  125.     BOOL bShowSystemObjects = ((CDaoViewApp *)AfxGetApp())->m_bShowSystemObjects;
  126.  
  127.     CDaoTableDefInfo tabInfo;
  128.     CDaoFieldInfo fieldInfo;
  129.     CDaoIndexInfo indexInfo;
  130.     try
  131.     {
  132.         int nTableDefCount = m_pDB->GetTableDefCount();
  133.         for (int i = 0; i < nTableDefCount; i++)
  134.         {
  135.             m_pDB->GetTableDefInfo(i,tabInfo);
  136.             if (!bShowSystemObjects)
  137.                 if (tabInfo.m_lAttributes & dbSystemObject)
  138.                     continue;
  139.             AddItem(IID_TABLE,tabInfo.m_strName);
  140.         }
  141.     }
  142.     catch (CDaoException* e)
  143.     {
  144.         e->Delete();
  145.     }
  146.  
  147.     try
  148.     {
  149.         CDaoQueryDefInfo queryInfo;
  150.         int nQueryDefCount= m_pDB->GetQueryDefCount();
  151.         for (int i = 0; i < nQueryDefCount; i++)
  152.         {
  153.             m_pDB->GetQueryDefInfo(i,queryInfo);
  154.             if (!bShowSystemObjects)
  155.                 if (queryInfo.m_nType == 5)
  156.                     continue;
  157.             tQueryDefs.AddTail(queryInfo.m_strName,IID_QUERYDEF);
  158.         }
  159.     }
  160.     catch (CDaoException* e)
  161.     {
  162.         e->Delete();
  163.     }
  164.  
  165.     try
  166.     {
  167.         CDaoRelationInfo relInfo;
  168.         int nRelationCount = m_pDB->GetRelationCount();
  169.         for (int i = 0;i < nRelationCount;i++)
  170.         {
  171.             m_pDB->GetRelationInfo(i,relInfo);
  172.             if (!bShowSystemObjects)
  173.                 if (relInfo.m_lAttributes & dbSystemObject)
  174.                     continue;
  175.             tRelations.AddTail(relInfo.m_strName,IID_RELATION);
  176.         }
  177.     }
  178.     catch (CDaoException* e)
  179.     {
  180.         e->Delete();
  181.     }
  182.  
  183.     tDatabase.Expand();
  184.     m_bNoNotifications = FALSE;
  185.     UpdateWindow();
  186. }
  187.  
  188. void CDaoTreeView::AddItem(WORD nItemType, LPCTSTR lpszName)
  189. {
  190.     switch (nItemType)
  191.     {
  192.         case IID_TABLE:
  193.         {
  194.             tTable = tTables.AddTail(lpszName,IID_TABLE);
  195.             tFields = tTable.AddTail(_T("Fields"),IID_FIELDS);
  196.             tIndexes = tTable.AddTail(_T("Indexes"),IID_INDEXES);
  197.             CDaoTableDef td(m_pDB);
  198.             try
  199.             {
  200.                 td.Open(lpszName);
  201.                 CDaoFieldInfo fieldInfo;
  202.                 int nFieldCount = td.GetFieldCount();
  203.                 for (int j=0; j < nFieldCount; j++)
  204.                 {
  205.                     td.GetFieldInfo(j,fieldInfo);
  206.                      tFields.AddTail(fieldInfo.m_strName,IID_FIELD);
  207.                 }
  208.                 CDaoIndexInfo indexInfo;
  209.                 int nIndexCount = td.GetIndexCount();
  210.                 for(j=0;j < nIndexCount;j++)
  211.                 {
  212.                     td.GetIndexInfo(j,indexInfo);
  213.                      tIndexes.AddTail(indexInfo.m_strName,IID_INDEX);
  214.                 }
  215.             }
  216.             catch(CDaoException* e)
  217.             {
  218.                 // ... Do nothing.  Used to catch security violations opening tables.
  219.                 e->Delete();
  220.             }
  221.             td.Close();
  222.             break;
  223.         }
  224.         case IID_QUERYDEF:
  225.         {
  226.             tQueryDefs.AddTail(lpszName,nItemType);
  227.             break;
  228.         }
  229.     }
  230. }
  231.  
  232. void CDaoTreeView::DeleteItem(CTreeCursor& itemDelete)
  233. {
  234.     switch (itemDelete.GetImageID())
  235.     {
  236.         case IID_TABLE:
  237.         {
  238.             try
  239.             {
  240.                 m_pDB->DeleteTableDef(itemDelete.GetText());
  241.                 itemDelete.Delete();
  242.             }
  243.             catch (CDaoException* e)
  244.             {
  245.                 MessageBox(
  246.                     _T("DaoView - Warning"),
  247.                     e->m_pErrorInfo->m_strDescription);
  248.                 e->Delete();
  249.             }
  250.             break;
  251.         }
  252.         case IID_QUERYDEF:
  253.         {
  254.             try
  255.             {
  256.                 m_pDB->DeleteQueryDef(itemDelete.GetText());
  257.                 itemDelete.Delete();
  258.             }
  259.             catch (CDaoException* e)
  260.             {
  261.                 MessageBox(
  262.                     _T("DaoView - Warning"),
  263.                     e->m_pErrorInfo->m_strDescription);
  264.                 e->Delete();
  265.             }
  266.             break;
  267.         }
  268.     }
  269. }
  270.  
  271. void CDaoTreeView::OnNodeSelect(NMHDR *pNotifyStruct,LRESULT *result)
  272. {
  273.     *result = 0;
  274.  
  275.     if (m_bNoNotifications)
  276.         return;
  277.  
  278.     CTreeCtrlEx& ctlTree = (CTreeCtrlEx&) GetTreeCtrl();
  279.  
  280.     m_ItemSel = ctlTree.GetSelectedItem();
  281.  
  282.     UINT nImageID = m_ItemSel.GetImageID();
  283.     GetDocument()->m_pListView->SetRedraw(FALSE);
  284.     switch (nImageID)
  285.     {
  286.         case IID_DATABASE:
  287.             GetDocument()->m_pListView->ShowDatabase();
  288.             break;
  289.         case IID_TABLES:
  290.             GetDocument()->m_pListView->ShowTableSchema();
  291.             break;
  292.         case IID_TABLE:
  293.             GetDocument()->m_pListView->ShowTableSchema(m_ItemSel.GetText());
  294.             break;
  295.         case IID_FIELDS:
  296.             GetDocument()->m_pListView->ShowFields(m_ItemSel.GetParent().GetText());
  297.             break;
  298.         case IID_FIELD:
  299.             GetDocument()->m_pListView->ShowFields(m_ItemSel.GetParent().GetParent().GetText(),m_ItemSel.GetText());
  300.             break;
  301.         case IID_QUERYDEFS:
  302.             GetDocument()->m_pListView->ShowQuerySchema();
  303.             break;
  304.         case IID_QUERYDEF:
  305.             GetDocument()->m_pListView->ShowQuerySchema(m_ItemSel.GetText());
  306.             break;
  307.         case IID_RELATIONS:
  308.             GetDocument()->m_pListView->ShowRelations();
  309.             break;
  310.         case IID_RELATION:
  311.             GetDocument()->m_pListView->ShowRelations(m_ItemSel.GetText());
  312.             break;
  313.         case IID_INDEXES:
  314.             GetDocument()->m_pListView->ShowIndexes(m_ItemSel.GetParent().GetText());
  315.             break;
  316.         case IID_INDEX:
  317.             GetDocument()->m_pListView->ShowIndexes(m_ItemSel.GetParent().GetParent().GetText(),m_ItemSel.GetText());
  318.             break;
  319.     }
  320.     GetDocument()->m_pListView->SetRedraw(TRUE);
  321. }
  322.  
  323. void CDaoTreeView::OnRightClick(NMHDR *pNotifyStruct,LRESULT *result)
  324. {
  325.     CTreeCtrlEx& ctlTree = (CTreeCtrlEx&) GetTreeCtrl();
  326.  
  327.     UINT nFlags;
  328.     CPoint curPoint;
  329.     GetCursorPos(&curPoint);
  330.     ScreenToClient(&curPoint);
  331.     m_ItemSel = ctlTree.HitTest(curPoint, &nFlags);
  332.  
  333.     UINT nImageID = m_ItemSel.GetImageID();
  334.     switch (nImageID)
  335.     {
  336.         case IID_TABLE:
  337.             DoPopupMenu(IDR_POPUP_TABLE);
  338.             break;
  339.         case IID_QUERYDEF:
  340.             DoPopupMenu(IDR_POPUP_QUERY);
  341.             break;
  342.     }
  343.     *result = 0;
  344. }
  345.  
  346. void CDaoTreeView::DoPopupMenu(UINT nMenuID)
  347. {
  348.     CMenu popMenu;
  349.  
  350.     popMenu.LoadMenu(nMenuID);
  351.  
  352.     CPoint posMouse;
  353.     GetCursorPos(&posMouse);
  354.  
  355.     popMenu.GetSubMenu(0)->TrackPopupMenu(0,posMouse.x,posMouse.y,this);
  356. }
  357.  
  358. void CDaoTreeView::OnPopupCommand(UINT nMenuID)
  359. {
  360.     CWaitCursor w;
  361.     GetDocument()->m_pListView->SetRedraw(FALSE);
  362.     switch (nMenuID)
  363.     {
  364.         case ID_TABLE_SCHEMA:
  365.             GetDocument()->m_pListView->ShowTableSchema(m_ItemSel.GetText());
  366.             break;
  367.         case ID_TABLE_DATA:
  368.             GetDocument()->m_pListView->ShowTableData(m_ItemSel.GetText());
  369.             break;
  370.         case ID_QUERY_DEFINITION:
  371.             GetDocument()->m_pListView->ShowQuerySchema(m_ItemSel.GetText());
  372.             break;
  373.         case ID_QUERY_RUN:
  374.             GetDocument()->m_pListView->RunQueryDef(m_ItemSel.GetText());
  375.             break;
  376.     }
  377.     GetDocument()->m_pListView->SetRedraw(TRUE);
  378. }
  379.  
  380.  
  381. void CDaoTreeView::OnBeginDrag(NMHDR *pNotifyStruct,LRESULT *result)
  382. {
  383.     int nImageID = m_ItemSel.GetImageID();
  384.  
  385.     switch (nImageID)
  386.     {
  387.         case IID_TABLE:
  388.         case IID_QUERYDEF:
  389.             CSharedFile globFile;
  390.             CArchive ar(&globFile,CArchive::store);
  391.             CDragItem dragItem(nImageID,m_pDB->GetName(),m_pDB->GetConnect(),m_ItemSel.GetText());
  392.             dragItem.Serialize(ar);
  393.             ar.Close();
  394.  
  395.             COleDataSource srcItem;
  396.  
  397.             srcItem.CacheGlobalData(m_nIDClipFormat,globFile.Detach());
  398.             srcItem.DoDragDrop();
  399.     }
  400.  
  401. }
  402.  
  403.  
  404. DROPEFFECT CDaoTreeView::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
  405. {
  406.     if (m_pDB)
  407.         if (pDataObject->IsDataAvailable(m_nIDClipFormat))
  408.             return DROPEFFECT_COPY;
  409.  
  410.     return CTreeView::OnDragEnter(pDataObject, dwKeyState, point);
  411. }
  412.  
  413. DROPEFFECT CDaoTreeView::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
  414. {
  415.     if (m_pDB)
  416.         if (pDataObject->IsDataAvailable(m_nIDClipFormat))
  417.             return DROPEFFECT_COPY;
  418.  
  419.     return CTreeView::OnDragOver(pDataObject, dwKeyState, point);
  420. }
  421.  
  422. BOOL CDaoTreeView::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
  423. {
  424.     if (!(m_pDB && pDataObject->IsDataAvailable(m_nIDClipFormat)))
  425.         return CTreeView::OnDrop(pDataObject, dropEffect, point);
  426.  
  427.     HGLOBAL hGlob = pDataObject->GetGlobalData(m_nIDClipFormat);
  428.     if (hGlob != NULL){
  429.         CSharedFile globFile;
  430.         globFile.SetHandle(hGlob,FALSE);
  431.         CArchive ar(&globFile,CArchive::load);
  432.         CDragItem dragItem;
  433.         dragItem.Serialize(ar);
  434.         ar.Close();
  435.         dragItem.Transfer(this,m_pDB);
  436.         return TRUE;
  437.     }
  438.  
  439.     return FALSE;
  440. }
  441.  
  442.  
  443. void CDaoTreeView::OnSysColorChange()
  444. {
  445.     CWnd::OnSysColorChange();
  446.  
  447.     // Reset the background color of our image list when notified
  448.     m_ctlImage.SetBkColor(GetSysColor(COLOR_WINDOW));
  449. }
  450.  
  451. void CDaoTreeView::OnKeyDown(NMHDR *pNotifyStruct,LRESULT *result)
  452. {
  453.     TV_KEYDOWN* pKeyDown = (TV_KEYDOWN *) pNotifyStruct;
  454.  
  455.     if (pKeyDown->wVKey == VK_DELETE)
  456.     {
  457.         BOOL bShowWarnings = ((CDaoViewApp *)AfxGetApp())->m_bShowWarnings;
  458.         int retCode = IDYES;
  459.         if (bShowWarnings)
  460.         {
  461.             retCode = MessageBox(
  462.                 _T("Are you sure you want to delete this item ?"),
  463.                 _T("DaoView - Warning"),MB_YESNO);
  464.         }
  465.         if (retCode == IDYES)
  466.             DeleteItem(m_ItemSel);
  467.     }
  468.     *result = 0;
  469. }
  470.  
  471. void CDaoTreeView::OnNewQuery()
  472. {
  473.     ASSERT(m_pDB);
  474.     ASSERT(m_pDB->IsOpen());
  475.  
  476.     CDlgSQL dlgSQL;
  477.  
  478.     if (dlgSQL.DoModal() != IDOK)
  479.         return;
  480.  
  481.     try
  482.     {
  483.         CDaoQueryDef qdTarget(m_pDB);
  484.  
  485.         qdTarget.Create(dlgSQL.m_strName,dlgSQL.m_strSQL);
  486.         qdTarget.Append();
  487.     }
  488.     catch (CDaoException* e)
  489.     {
  490.         DisplayDaoException(e);
  491.         e->Delete();
  492.         return;
  493.     }
  494.     AddItem(IID_QUERYDEF,dlgSQL.m_strName);
  495. }
  496.  
  497.  
  498. void CDaoTreeView::OnQueryEdit()
  499. {
  500.     try
  501.     {
  502.         CDlgSQL dlgSQL;
  503.  
  504.         dlgSQL.m_bEditMode = TRUE;
  505.  
  506.         CDaoQueryDef qdEdit(m_pDB);
  507.         qdEdit.Open(m_ItemSel.GetText());
  508.  
  509.         dlgSQL.m_strName = m_ItemSel.GetText();
  510.         dlgSQL.m_strSQL = qdEdit.GetSQL();
  511.  
  512.         if (dlgSQL.DoModal() == IDOK)
  513.             qdEdit.SetSQL(dlgSQL.m_strSQL);
  514.  
  515.         qdEdit.Close();
  516.     }
  517.     catch (CDaoException* e)
  518.     {
  519.         DisplayDaoException(e);
  520.         e->Delete();
  521.         return;
  522.     }
  523. }
  524.