home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c10 / lab02 / baseline / mdbview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  10.4 KB  |  374 lines

  1. // MDBView.cpp : implementation of the CMDBView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MDB.h"
  6.  
  7. #include "MDBDoc.h"
  8. #include "MDBView.h"
  9. #include "DlgField.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMDBView
  19.  
  20. IMPLEMENT_DYNCREATE(CMDBView, CTreeView)
  21.  
  22. BEGIN_MESSAGE_MAP(CMDBView, CTreeView)
  23.     //{{AFX_MSG_MAP(CMDBView)
  24.     ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndlabeledit)
  25.     ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnBeginlabeledit)
  26.     ON_COMMAND(ID_FILE_TABLE, OnFileTable)
  27.     ON_COMMAND(ID_FILE_FIELD, OnFileField)
  28.     ON_UPDATE_COMMAND_UI(ID_FILE_FIELD, OnUpdateFileField)
  29.     ON_UPDATE_COMMAND_UI(ID_FILE_FIELD_DELETE, OnUpdateFileFieldDelete)
  30.     ON_UPDATE_COMMAND_UI(ID_FILE_TABLE_DELETE, OnUpdateFileTableDelete)
  31.     ON_COMMAND(ID_FILE_TABLE_DELETE, OnFileTableDelete)
  32.     ON_COMMAND(ID_FILE_FIELD_DELETE, OnFileFieldDelete)
  33.     ON_COMMAND(ID_EDIT_EDITSELECTEDITEM, OnEditSelectedItem)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_EDITSELECTEDITEM, OnUpdateFileField)
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CMDBView construction/destruction
  40.  
  41. CMDBView::CMDBView()
  42. {
  43.     // TODO: add construction code here
  44.  
  45. }
  46.  
  47. CMDBView::~CMDBView()
  48. {
  49. }
  50.  
  51. BOOL CMDBView::PreCreateWindow(CREATESTRUCT& cs)
  52. {
  53.     // TODO: Modify the Window class or styles here by modifying
  54.     //  the CREATESTRUCT cs
  55.  
  56.     return CTreeView::PreCreateWindow(cs);
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMDBView drawing
  61.  
  62. void CMDBView::OnDraw(CDC* pDC)
  63. {
  64.     CMDBDoc* pDoc = GetDocument();
  65.     ASSERT_VALID(pDoc);
  66.  
  67.     // TODO: add draw code for native data here
  68. }
  69.  
  70. void CMDBView::OnInitialUpdate()
  71. {
  72.     CTreeView::OnInitialUpdate();
  73.  
  74.     // TODO: You may populate your TreeView with items by directly accessing
  75.     //  its tree control through a call to GetTreeCtrl().
  76.     CTreeCtrl & tree = GetTreeCtrl( );
  77.     tree.ModifyStyle( NULL,                //Don't remove any styles
  78.         TVS_HASBUTTONS |                
  79.         TVS_HASLINES |
  80.         TVS_EDITLABELS |                //Allow label editing
  81.         TVS_LINESATROOT
  82.     );
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CMDBView diagnostics
  87.  
  88. #ifdef _DEBUG
  89. void CMDBView::AssertValid() const
  90. {
  91.     CTreeView::AssertValid();
  92. }
  93.  
  94. void CMDBView::Dump(CDumpContext& dc) const
  95. {
  96.     CTreeView::Dump(dc);
  97. }
  98.  
  99. CMDBDoc* CMDBView::GetDocument() // non-debug version is inline
  100. {
  101.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDBDoc)));
  102.     return (CMDBDoc*)m_pDocument;
  103. }
  104. #endif //_DEBUG
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CMDBView message handlers
  108.  
  109. void CMDBView::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) 
  110. {
  111.     TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
  112.     // TODO: Add your control notification handler code here
  113.     CTreeCtrl & tree = GetTreeCtrl( );
  114.     CString str;
  115.     CEdit * pEdit = tree.GetEditControl( );
  116.     pEdit->GetWindowText( str );
  117.  
  118.     tree.SetItemText( pTVDispInfo->item.hItem, str );
  119.  
  120.     *pResult = 0;
  121. }
  122.  
  123. void CMDBView::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult) 
  124. {
  125.     TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
  126.     // TODO: Add your control notification handler code here
  127.     
  128.     CTreeCtrl & tree = GetTreeCtrl( );
  129.     HTREEITEM hItem ( pTVDispInfo->item.hItem );
  130.  
  131.     if ( ! tree.GetParentItem( hItem ) )
  132.     {
  133.         * pResult = 0;  //Allow editing
  134.         return;            //Let the user change the table name
  135.     }
  136.  
  137.     * pResult = 1;  //Do not allow editing
  138.     
  139.     CDlgField dlg;
  140.     
  141.     //Have to determine whether on a field attribute or field name
  142.     HTREEITEM hField =
  143.         tree.ItemHasChildren( hItem ) 
  144.         ? 
  145.             hItem  //Must be the field name
  146.         :
  147.             tree.GetParentItem( hItem ); //Must have been attribute
  148.     
  149.     dlg.m_strCaption = 
  150.         tree.GetItemText( tree.GetParentItem( hField ) ) 
  151.         + ": Edit field";
  152.     dlg.m_strFieldName = tree.GetItemText( hField );
  153.     HTREEITEM hChild = tree.GetChildItem( hField );
  154.     dlg.m_nFieldType = tree.GetItemData( hChild );
  155.     HTREEITEM hTextLength = NULL;  
  156.     
  157.     if ( 0 == dlg.m_nFieldType )
  158.     {
  159.         hTextLength = tree.GetNextSiblingItem( hChild );
  160.         dlg.m_nTextLength = tree.GetItemData( hTextLength );
  161.     }
  162.     
  163.     //Show a dialog to find out what they want
  164.     if ( IDOK == dlg.DoModal( ) )
  165.     {
  166.         tree.SetItemText( hField, dlg.m_strFieldName );//Field name
  167.         tree.SetItemData( hChild, dlg.m_nFieldType ); //Field datatype
  168.     
  169.         if ( hTextLength )        //Get rid of length node
  170.             tree.DeleteItem( hTextLength );  //I'll put it back if needed
  171.         switch( dlg.m_nFieldType )
  172.         {
  173.         case 0    :    //Text
  174.             {
  175.             CString str;
  176.             str.Format( "%d", dlg.m_nTextLength );
  177.             
  178.             tree.SetItemText( hChild, "Text" );
  179.             hTextLength = tree.InsertItem( str, hField );
  180.             tree.SetItemData( hTextLength, ( DWORD ) dlg.m_nTextLength );
  181.             break;
  182.             }
  183.         case 1    :    //Integer
  184.             tree.SetItemText( hChild, "Integer" );
  185.             break;
  186.         case 2    :    //Floating point
  187.             tree.SetItemText( hChild, "Floating point" );
  188.             break;
  189.         }
  190.     }
  191. }
  192.  
  193. void CMDBView::OnFileTable() 
  194. {
  195.     // TODO: Add your command handler code here
  196.     CTreeCtrl & tree = GetTreeCtrl( );    
  197.     HTREEITEM hItem;
  198.     hItem = tree.InsertItem( "Table Name" );
  199.     tree.EditLabel( hItem )->SetSel( 0, 50 );
  200.     tree.SelectItem( hItem );
  201.     SetModified( );
  202. }
  203.  
  204. void CMDBView::OnFileField() 
  205. {
  206.     // TODO: Add your command handler code here
  207.     CTreeCtrl & tree = GetTreeCtrl( );
  208.     HTREEITEM hItem = tree.GetSelectedItem( );
  209.     if ( tree.GetParentItem( hItem ) )   //Only insert into table
  210.         hItem = tree.GetParentItem( hItem );
  211.     CDlgField dlg;
  212.     CString strCaption = tree.GetItemText( hItem ) + ": Create field";
  213.     dlg.m_strCaption = strCaption;
  214.  
  215.     if ( IDOK == dlg.DoModal( ) )
  216.     {
  217.         HTREEITEM hChild =
  218.             tree.InsertItem( dlg.m_strFieldName, hItem );
  219.         HTREEITEM hType;
  220.         HTREEITEM hLen;
  221.         CString strLen;
  222.  
  223.         switch ( dlg.m_nFieldType )
  224.         {
  225.         case 0    :    //Text
  226.             hType = tree.InsertItem( "Text", hChild );
  227.             tree.SetItemData( hType, 0 );
  228.             strLen.Format( "%d", dlg.m_nTextLength );
  229.             hLen = tree.InsertItem( strLen, hChild );
  230.             tree.SetItemData( hLen, ( DWORD ) dlg.m_nTextLength );
  231.             break;
  232.         case 1    :    //Integer
  233.             hType = tree.InsertItem( "Integer", hChild );
  234.             tree.SetItemData( hType, 1 );
  235.             break;
  236.         case 2    :    //Floating point
  237.             hType = tree.InsertItem( "Floating point", hChild );
  238.             tree.SetItemData( hType, 2 );
  239.             break;
  240.         }
  241.         tree.SetItemData( hType, ( DWORD ) dlg.m_nFieldType );
  242.         tree.SelectItem( hChild );
  243.         SetModified( );
  244.     }
  245. }
  246.  
  247. void CMDBView::OnUpdateFileField(CCmdUI* pCmdUI) 
  248. {
  249.     // TODO: Add your command update UI handler code here
  250.     CTreeCtrl & tree = GetTreeCtrl( );
  251.     pCmdUI->Enable( tree.GetCount( ) );
  252. }
  253.  
  254. void CMDBView::OnUpdateFileFieldDelete(CCmdUI* pCmdUI) 
  255. {
  256.     // TODO: Add your command update UI handler code here
  257.     CTreeCtrl & tree = GetTreeCtrl( );
  258.     HTREEITEM hSel = tree.GetSelectedItem( );
  259.     pCmdUI->Enable( ( int ) tree.GetParentItem( hSel ) ); //Table has null parent
  260. }
  261.  
  262. void CMDBView::OnUpdateFileTableDelete(CCmdUI* pCmdUI) 
  263. {
  264.     // TODO: Add your command update UI handler code here
  265.     CTreeCtrl & tree = GetTreeCtrl( );
  266.     HTREEITEM hSel = tree.GetSelectedItem( );
  267.     BOOLEAN b = ! ( int ) tree.GetParentItem( hSel ) && tree.GetCount( );
  268.     pCmdUI->Enable( b ); //Table has null parent and exists
  269. }
  270.  
  271. void CMDBView::OnFileTableDelete() 
  272. {
  273.     // TODO: Add your command handler code here
  274.     //Only enabled when on table item
  275.     CTreeCtrl & tree = GetTreeCtrl( );
  276.     HTREEITEM hSel = tree.GetSelectedItem( );
  277.  
  278.     tree.DeleteItem( hSel ); 
  279. }
  280.  
  281. void CMDBView::OnFileFieldDelete() 
  282. {
  283.     // TODO: Add your command handler code here
  284.     //Only enabled when on field item
  285.     CTreeCtrl & tree = GetTreeCtrl( );
  286.     HTREEITEM hSel = tree.GetSelectedItem( );
  287.     
  288.     if ( ! tree. ItemHasChildren( hSel ) )  //No child: must be attribute
  289.         hSel = tree.GetParentItem( hSel );  //Field item
  290.     tree.DeleteItem( hSel );           //Field and children
  291. }
  292.  
  293. void CMDBView::OnEditSelectedItem() 
  294. {
  295.     // TODO: Add your command handler code here
  296.     CTreeCtrl & tree = GetTreeCtrl( );
  297.     HTREEITEM hSel = tree.GetSelectedItem( );
  298.     tree.EditLabel( hSel );
  299.     SetModified( );
  300. }
  301.  
  302. void CMDBView::SetModified( )
  303. {
  304.     CTreeCtrl & tree = GetTreeCtrl( );
  305.     int b = tree.GetCount( );
  306.     GetDocument( )->SetModifiedFlag( b );
  307. }
  308.  
  309. //Iterate the tree control's nodes for table names
  310. //If reset is true, start with first, otherwise get next table name
  311. //Returns handle to tree item
  312. HTREEITEM CMDBView::TableNameIterator( CString & strName, BOOLEAN bReset )
  313. {
  314.     static HTREEITEM hCurrent = NULL; //Start with reset to tree top
  315.     if ( bReset )
  316.         hCurrent = NULL;              //Reset to tree top
  317.     CTreeCtrl & tree = GetTreeCtrl( );
  318.     if ( NULL == hCurrent )
  319.         hCurrent = tree.GetChildItem( NULL ); //First tree item
  320.     else
  321.         hCurrent = tree.GetNextSiblingItem( hCurrent );    //Next tree item
  322.     strName = tree.GetItemText( hCurrent );
  323.  
  324.     return hCurrent;    //Current node's handle
  325. }
  326.  
  327. //Pass in a Table handle to start field iteration.
  328. //The iterator returns NULL to indicate 
  329. //no remaining fields in the table, otherwise returns
  330. //handle to tree item.
  331. //
  332. //nFieldType modified to indicate datatype of field:
  333. //    0 == Text field
  334. //    1 == integer field
  335. //    2 == floating point
  336. //The above are NOT the same values used by Microsoft Access
  337. //
  338. //nTextFieldLength modified to indicate the length of a text field.
  339. //nTextFieldLength set to -1 for non-text fields.
  340. HTREEITEM CMDBView::FieldInfoIterator( CString & strFieldName, 
  341.                                       int & nFieldType,     
  342.                                       int & nTextFieldLength,
  343.                                       HTREEITEM hTable )
  344. {
  345.     static HTREEITEM hFieldItem;
  346.     static HTREEITEM hCurrentTable = NULL;
  347.     HTREEITEM hAttribute;
  348.     CTreeCtrl & tree = GetTreeCtrl( );
  349.     
  350.     if ( hCurrentTable != hTable )  //Use table as flag to start iteration
  351.     {
  352.         hCurrentTable = hTable;
  353.         hFieldItem = tree.GetChildItem( hTable ); //First field item
  354.     }
  355.     else   //Get next field item from tree
  356.         hFieldItem = tree.GetNextSiblingItem( hFieldItem );
  357.  
  358.     if ( NULL == hFieldItem )
  359.         return NULL;    //Done with this table
  360.  
  361.     strFieldName = tree.GetItemText( hFieldItem );  //Got name
  362.     hAttribute = tree.GetChildItem( hFieldItem );   //Got attribute
  363.     nFieldType = tree.GetItemData( hAttribute );
  364.     
  365.     nTextFieldLength = -1;    //Assume length parameter not needed
  366.     if ( 0 == nFieldType )  //Text field does need length
  367.     {
  368.         hAttribute = tree.GetNextSiblingItem( hAttribute );
  369.         nTextFieldLength = tree.GetItemData( hAttribute );
  370.     }
  371.     return hFieldItem;    //Current field node's handle
  372. }
  373.  
  374.