home *** CD-ROM | disk | FTP | other *** search
- // MDBView.cpp : implementation of the CMDBView class
- //
-
- #include "stdafx.h"
- #include "MDB.h"
-
- #include "MDBDoc.h"
- #include "MDBView.h"
- #include "DlgField.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBView
-
- IMPLEMENT_DYNCREATE(CMDBView, CTreeView)
-
- BEGIN_MESSAGE_MAP(CMDBView, CTreeView)
- //{{AFX_MSG_MAP(CMDBView)
- ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndlabeledit)
- ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnBeginlabeledit)
- ON_COMMAND(ID_FILE_TABLE, OnFileTable)
- ON_COMMAND(ID_FILE_FIELD, OnFileField)
- ON_UPDATE_COMMAND_UI(ID_FILE_FIELD, OnUpdateFileField)
- ON_UPDATE_COMMAND_UI(ID_FILE_FIELD_DELETE, OnUpdateFileFieldDelete)
- ON_UPDATE_COMMAND_UI(ID_FILE_TABLE_DELETE, OnUpdateFileTableDelete)
- ON_COMMAND(ID_FILE_TABLE_DELETE, OnFileTableDelete)
- ON_COMMAND(ID_FILE_FIELD_DELETE, OnFileFieldDelete)
- ON_COMMAND(ID_EDIT_EDITSELECTEDITEM, OnEditSelectedItem)
- ON_UPDATE_COMMAND_UI(ID_EDIT_EDITSELECTEDITEM, OnUpdateFileField)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBView construction/destruction
-
- CMDBView::CMDBView()
- {
- // TODO: add construction code here
-
- }
-
- CMDBView::~CMDBView()
- {
- }
-
- BOOL CMDBView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CTreeView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBView drawing
-
- void CMDBView::OnDraw(CDC* pDC)
- {
- CMDBDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
- // TODO: add draw code for native data here
- }
-
- void CMDBView::OnInitialUpdate()
- {
- CTreeView::OnInitialUpdate();
-
- // TODO: You may populate your TreeView with items by directly accessing
- // its tree control through a call to GetTreeCtrl().
- CTreeCtrl & tree = GetTreeCtrl( );
- tree.ModifyStyle( NULL, //Don't remove any styles
- TVS_HASBUTTONS |
- TVS_HASLINES |
- TVS_EDITLABELS | //Allow label editing
- TVS_LINESATROOT
- );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBView diagnostics
-
- #ifdef _DEBUG
- void CMDBView::AssertValid() const
- {
- CTreeView::AssertValid();
- }
-
- void CMDBView::Dump(CDumpContext& dc) const
- {
- CTreeView::Dump(dc);
- }
-
- CMDBDoc* CMDBView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDBDoc)));
- return (CMDBDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBView message handlers
-
- void CMDBView::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
- {
- TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
- // TODO: Add your control notification handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- CString str;
- CEdit * pEdit = tree.GetEditControl( );
- pEdit->GetWindowText( str );
-
- tree.SetItemText( pTVDispInfo->item.hItem, str );
-
- *pResult = 0;
- }
-
- void CMDBView::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
- {
- TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
- // TODO: Add your control notification handler code here
-
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hItem ( pTVDispInfo->item.hItem );
-
- if ( ! tree.GetParentItem( hItem ) )
- {
- * pResult = 0; //Allow editing
- return; //Let the user change the table name
- }
-
- * pResult = 1; //Do not allow editing
-
- CDlgField dlg;
-
- //Have to determine whether on a field attribute or field name
- HTREEITEM hField =
- tree.ItemHasChildren( hItem )
- ?
- hItem //Must be the field name
- :
- tree.GetParentItem( hItem ); //Must have been attribute
-
- dlg.m_strCaption =
- tree.GetItemText( tree.GetParentItem( hField ) )
- + ": Edit field";
- dlg.m_strFieldName = tree.GetItemText( hField );
- HTREEITEM hChild = tree.GetChildItem( hField );
- dlg.m_nFieldType = tree.GetItemData( hChild );
- HTREEITEM hTextLength = NULL;
-
- if ( 0 == dlg.m_nFieldType )
- {
- hTextLength = tree.GetNextSiblingItem( hChild );
- dlg.m_nTextLength = tree.GetItemData( hTextLength );
- }
-
- //Show a dialog to find out what they want
- if ( IDOK == dlg.DoModal( ) )
- {
- tree.SetItemText( hField, dlg.m_strFieldName );//Field name
- tree.SetItemData( hChild, dlg.m_nFieldType ); //Field datatype
-
- if ( hTextLength ) //Get rid of length node
- tree.DeleteItem( hTextLength ); //I'll put it back if needed
- switch( dlg.m_nFieldType )
- {
- case 0 : //Text
- {
- CString str;
- str.Format( "%d", dlg.m_nTextLength );
-
- tree.SetItemText( hChild, "Text" );
- hTextLength = tree.InsertItem( str, hField );
- tree.SetItemData( hTextLength, ( DWORD ) dlg.m_nTextLength );
- break;
- }
- case 1 : //Integer
- tree.SetItemText( hChild, "Integer" );
- break;
- case 2 : //Floating point
- tree.SetItemText( hChild, "Floating point" );
- break;
- }
- }
- }
-
- void CMDBView::OnFileTable()
- {
- // TODO: Add your command handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hItem;
- hItem = tree.InsertItem( "Table Name" );
- tree.EditLabel( hItem )->SetSel( 0, 50 );
- tree.SelectItem( hItem );
- SetModified( );
- }
-
- void CMDBView::OnFileField()
- {
- // TODO: Add your command handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hItem = tree.GetSelectedItem( );
- if ( tree.GetParentItem( hItem ) ) //Only insert into table
- hItem = tree.GetParentItem( hItem );
- CDlgField dlg;
- CString strCaption = tree.GetItemText( hItem ) + ": Create field";
- dlg.m_strCaption = strCaption;
-
- if ( IDOK == dlg.DoModal( ) )
- {
- HTREEITEM hChild =
- tree.InsertItem( dlg.m_strFieldName, hItem );
- HTREEITEM hType;
- HTREEITEM hLen;
- CString strLen;
-
- switch ( dlg.m_nFieldType )
- {
- case 0 : //Text
- hType = tree.InsertItem( "Text", hChild );
- tree.SetItemData( hType, 0 );
- strLen.Format( "%d", dlg.m_nTextLength );
- hLen = tree.InsertItem( strLen, hChild );
- tree.SetItemData( hLen, ( DWORD ) dlg.m_nTextLength );
- break;
- case 1 : //Integer
- hType = tree.InsertItem( "Integer", hChild );
- tree.SetItemData( hType, 1 );
- break;
- case 2 : //Floating point
- hType = tree.InsertItem( "Floating point", hChild );
- tree.SetItemData( hType, 2 );
- break;
- }
- tree.SetItemData( hType, ( DWORD ) dlg.m_nFieldType );
- tree.SelectItem( hChild );
- SetModified( );
- }
- }
-
- void CMDBView::OnUpdateFileField(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- pCmdUI->Enable( tree.GetCount( ) );
- }
-
- void CMDBView::OnUpdateFileFieldDelete(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hSel = tree.GetSelectedItem( );
- pCmdUI->Enable( ( int ) tree.GetParentItem( hSel ) ); //Table has null parent
- }
-
- void CMDBView::OnUpdateFileTableDelete(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hSel = tree.GetSelectedItem( );
- BOOLEAN b = ! ( int ) tree.GetParentItem( hSel ) && tree.GetCount( );
- pCmdUI->Enable( b ); //Table has null parent and exists
- }
-
- void CMDBView::OnFileTableDelete()
- {
- // TODO: Add your command handler code here
- //Only enabled when on table item
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hSel = tree.GetSelectedItem( );
-
- tree.DeleteItem( hSel );
- }
-
- void CMDBView::OnFileFieldDelete()
- {
- // TODO: Add your command handler code here
- //Only enabled when on field item
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hSel = tree.GetSelectedItem( );
-
- if ( ! tree. ItemHasChildren( hSel ) ) //No child: must be attribute
- hSel = tree.GetParentItem( hSel ); //Field item
- tree.DeleteItem( hSel ); //Field and children
- }
-
- void CMDBView::OnEditSelectedItem()
- {
- // TODO: Add your command handler code here
- CTreeCtrl & tree = GetTreeCtrl( );
- HTREEITEM hSel = tree.GetSelectedItem( );
- tree.EditLabel( hSel );
- SetModified( );
- }
-
- void CMDBView::SetModified( )
- {
- CTreeCtrl & tree = GetTreeCtrl( );
- int b = tree.GetCount( );
- GetDocument( )->SetModifiedFlag( b );
- }
-
- //Iterate the tree control's nodes for table names
- //If reset is true, start with first, otherwise get next table name
- //Returns handle to tree item
- HTREEITEM CMDBView::TableNameIterator( CString & strName, BOOLEAN bReset )
- {
- static HTREEITEM hCurrent = NULL; //Start with reset to tree top
- if ( bReset )
- hCurrent = NULL; //Reset to tree top
- CTreeCtrl & tree = GetTreeCtrl( );
- if ( NULL == hCurrent )
- hCurrent = tree.GetChildItem( NULL ); //First tree item
- else
- hCurrent = tree.GetNextSiblingItem( hCurrent ); //Next tree item
- strName = tree.GetItemText( hCurrent );
-
- return hCurrent; //Current node's handle
- }
-
- //Pass in a Table handle to start field iteration.
- //The iterator returns NULL to indicate
- //no remaining fields in the table, otherwise returns
- //handle to tree item.
- //
- //nFieldType modified to indicate datatype of field:
- // 0 == Text field
- // 1 == integer field
- // 2 == floating point
- //The above are NOT the same values used by Microsoft Access
- //
- //nTextFieldLength modified to indicate the length of a text field.
- //nTextFieldLength set to -1 for non-text fields.
- HTREEITEM CMDBView::FieldInfoIterator( CString & strFieldName,
- int & nFieldType,
- int & nTextFieldLength,
- HTREEITEM hTable )
- {
- static HTREEITEM hFieldItem;
- static HTREEITEM hCurrentTable = NULL;
- HTREEITEM hAttribute;
- CTreeCtrl & tree = GetTreeCtrl( );
-
- if ( hCurrentTable != hTable ) //Use table as flag to start iteration
- {
- hCurrentTable = hTable;
- hFieldItem = tree.GetChildItem( hTable ); //First field item
- }
- else //Get next field item from tree
- hFieldItem = tree.GetNextSiblingItem( hFieldItem );
-
- if ( NULL == hFieldItem )
- return NULL; //Done with this table
-
- strFieldName = tree.GetItemText( hFieldItem ); //Got name
- hAttribute = tree.GetChildItem( hFieldItem ); //Got attribute
- nFieldType = tree.GetItemData( hAttribute );
-
- nTextFieldLength = -1; //Assume length parameter not needed
- if ( 0 == nFieldType ) //Text field does need length
- {
- hAttribute = tree.GetNextSiblingItem( hAttribute );
- nTextFieldLength = tree.GetItemData( hAttribute );
- }
- return hFieldItem; //Current field node's handle
- }
-
-