X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/704c34908646c5eee35a11974f8d1016f54cc909..6f3f9b50aa275243ade3541ecccb95de22cf57a9:/src/generic/datavgen.cpp diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 2a471425b8..f47731e5e4 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -42,6 +42,8 @@ #include "wx/renderer.h" #include "wx/dcbuffer.h" #include "wx/icon.h" +#include "wx/list.h" +#include "wx/listimpl.cpp" //----------------------------------------------------------------------------- // classes @@ -179,7 +181,7 @@ public: } virtual void UpdateDisplay() { Refresh(); } - + // event handlers: void OnPaint( wxPaintEvent &event ); @@ -251,37 +253,50 @@ public: //----------------------------------------------------------------------------- // wxDataViewTreeNode //----------------------------------------------------------------------------- -class wxDataViewTreeNode; -WX_DEFINE_ARRAY_PTR( wxDataViewTreeNode *, wxDataViewTreeNodes ); +class wxDataViewTreeNode; +WX_DEFINE_SORTED_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes ); +WX_DEFINE_SORTED_ARRAY( void* , wxDataViewTreeLeaves); + +int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode * node1, wxDataViewTreeNode * node2); +int LINKAGEMODE wxGenericTreeModelItemCmp( void * id1, void * id2); class wxDataViewTreeNode { public: wxDataViewTreeNode( wxDataViewTreeNode * parent ) - { this->parent = parent; + :leaves( wxGenericTreeModelItemCmp ), + nodes(wxGenericTreeModelNodeCmp) + { this->parent = parent; if( parent == NULL ) open = true; else open = false; hasChildren = false; + subTreeCount = 0; } //I don't know what I need to do in the destructure ~wxDataViewTreeNode() - { } + { + + } wxDataViewTreeNode * GetParent() { return parent; } void SetParent( wxDataViewTreeNode * parent ) { this->parent = parent; } - wxDataViewTreeNodes & GetChildren() { return children; } - void SetChildren( wxDataViewTreeNodes children ) { this->children = children; } + wxDataViewTreeNodes & GetNodes() { return nodes; } + wxDataViewTreeLeaves & GetChildren() { return leaves; } - wxDataViewTreeNode * GetChild( unsigned int n ) { return children.Item( n ); } - void InsertChild( wxDataViewTreeNode * child, unsigned int n) { children.Insert( child, n); } - void AppendChild( wxDataViewTreeNode * child ) { children.Add( child ); } + void AddNode( wxDataViewTreeNode * node ) + { + nodes.Add( node ); + leaves.Add( node->GetItem().GetID() ); + } + void AddLeaf( void * leaf ) { leaves.Add( leaf ); } wxDataViewItem & GetItem() { return item; } - void SetItem( wxDataViewItem & item ) { this->item = item; } + void SetItem( const wxDataViewItem & item ) { this->item = item; } - unsigned int GetChildrenNumber() { return children.GetCount(); } + unsigned int GetChildrenNumber() { return leaves.GetCount(); } + unsigned int GetNodeNumber() { return nodes.GetCount(); } int GetIndentLevel() { int ret = 0 ; @@ -294,24 +309,103 @@ public: return ret; } - bool IsOpen() { return open; } - void ToggleOpen(){ open = !open; } + bool IsOpen() + { + return open ; + } + + void ToggleOpen() + { + int len = nodes.GetCount(); + int sum = 0; + for ( int i = 0 ;i < len ; i ++) + sum += nodes[i]->GetSubTreeCount(); + + sum += leaves.GetCount(); + if( open ) + { + ChangeSubTreeCount(-sum); + open = !open; + } + else + { + open = !open; + ChangeSubTreeCount(sum); + } + } bool HasChildren() { return hasChildren; } void SetHasChildren( bool has ){ hasChildren = has; } + + void SetSubTreeCount( int num ) { subTreeCount = num; } + int GetSubTreeCount() { return subTreeCount; } + void ChangeSubTreeCount( int num ) + { + if( !open ) + return ; + subTreeCount += num; + if( parent ) + parent->ChangeSubTreeCount(num); + } + + void Resort() + { + wxDataViewTreeNodes nds = nodes; + wxDataViewTreeLeaves lvs = leaves; + nodes.Empty(); + leaves.Empty(); + + int len = nds.GetCount(); + if(len > 0) + { + int i; + for(i = 0; i < len; i ++) + nodes.Add(nds[i]); + for(i = 0; i < len; i ++) + nodes[i]->Resort(); + } + + len = lvs.GetCount(); + if(len > 0) + { + for(int i = 0; i < len; i++) + leaves.Add(lvs[i]); + } + } + private: wxDataViewTreeNode * parent; - wxDataViewTreeNodes children; - wxDataViewItem item; + wxDataViewTreeNodes nodes; + wxDataViewTreeLeaves leaves; + wxDataViewItem item; bool open; bool hasChildren; + int subTreeCount; }; +//Below is the compare stuff +//For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed +static wxDataViewModel * g_model; + +int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode * node1, wxDataViewTreeNode * node2) +{ + return g_model->Compare( node1->GetItem(), node2->GetItem() ); +} + +int LINKAGEMODE wxGenericTreeModelItemCmp( void * id1, void * id2) +{ + return g_model->Compare( id1, id2 ); +} + + + //----------------------------------------------------------------------------- // wxDataViewMainWindow //----------------------------------------------------------------------------- WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection, WXDLLIMPEXP_ADV); +WX_DECLARE_LIST(wxDataViewItem, ItemList); +WX_DEFINE_LIST(ItemList); class wxDataViewMainWindow: public wxWindow { @@ -325,10 +419,12 @@ public: // notifications from wxDataViewModel bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); - bool ItemDeleted( const wxDataViewItem &item ); + bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); bool ItemChanged( const wxDataViewItem &item ); bool ValueChanged( const wxDataViewItem &item, unsigned int col ); bool Cleared(); + void Resort() + { g_model = GetOwner()->GetModel(); m_root->Resort(); UpdateDisplay(); } void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } wxDataViewCtrl *GetOwner() { return m_owner; } @@ -401,6 +497,8 @@ private: void OnExpanding( unsigned int row ); void OnCollapsing( unsigned int row ); + wxDataViewTreeNode * FindNode( const wxDataViewItem & item ); + private: wxDataViewCtrl *m_owner; int m_lineHeight; @@ -430,7 +528,7 @@ private: wxPen m_penExpander; //This is the tree structure of the model - wxDataViewTreeNode * m_root; + wxDataViewTreeNode * m_root; int m_count; private: DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) @@ -449,14 +547,16 @@ public: virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item ) { return m_mainWindow->ItemAdded( parent , item ); } - virtual bool ItemDeleted( const wxDataViewItem & item ) - { return m_mainWindow->ItemDeleted( item ); } + virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) + { return m_mainWindow->ItemDeleted( parent, item ); } virtual bool ItemChanged( const wxDataViewItem & item ) { return m_mainWindow->ItemChanged(item); } virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col ) { return m_mainWindow->ValueChanged( item, col ); } virtual bool Cleared() { return m_mainWindow->Cleared(); } + virtual void Resort() + { m_mainWindow->Resort(); } wxDataViewMainWindow *m_mainWindow; }; @@ -534,14 +634,14 @@ bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const } bool wxDataViewTextRenderer::HasEditorCtrl() -{ +{ return true; } wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ) { - return new wxTextCtrl( parent, wxID_ANY, value, + return new wxTextCtrl( parent, wxID_ANY, value, wxPoint(labelRect.x,labelRect.y), wxSize(labelRect.width,labelRect.height) ); } @@ -927,7 +1027,7 @@ void wxDataViewColumn::SetSortable( bool sortable ) m_flags |= wxDATAVIEW_COL_SORTABLE; else m_flags &= ~wxDATAVIEW_COL_SORTABLE; - + // Update header button if (GetOwner()) GetOwner()->OnColumnChange(); @@ -936,7 +1036,7 @@ void wxDataViewColumn::SetSortable( bool sortable ) void wxDataViewColumn::SetSortOrder( bool ascending ) { m_ascending = ascending; - + // Update header button if (GetOwner()) GetOwner()->OnColumnChange(); @@ -1078,6 +1178,7 @@ void wxDataViewHeaderWindowMSW::UpdateDisplay() // add the updated array of columns to the header control unsigned int cols = GetOwner()->GetColumnCount(); unsigned int added = 0; + wxDataViewModel * model = GetOwner()->GetModel(); for (unsigned int i = 0; i < cols; i++) { wxDataViewColumn *col = GetColumn( i ); @@ -1211,6 +1312,30 @@ bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARA case HDN_ITEMCLICK: { unsigned int idx = GetColumnIdxFromHeader(nmHDR); + wxDataViewModel * model = GetOwner()->GetModel(); + + if(nmHDR->iButton == 0) + { + wxDataViewColumn *col = GetColumn(idx); + if(col->IsSortable()) + { + if(model && model->GetSortingColumn() == idx) + { + bool order = col->IsSortOrderAscending(); + col->SetSortOrder(!order); + model->SetSortOrderAscending(!order); + } + else if(model) + { + model->SetSortingColumn(idx); + model->SetSortOrderAscending(true); + } + } + UpdateDisplay(); + if(model) + model->Resort(); + } + wxEventType evt = nmHDR->iButton == 0 ? wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK : wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK; @@ -1693,7 +1818,8 @@ void wxDataViewMainWindow::OnRenameTimer() GetOwner()->CalcScrolledPosition( labelRect.x, labelRect.y, &labelRect.x, &labelRect.y); - m_currentCol->GetRenderer()->StartEditing( m_currentRow, labelRect ); + wxDataViewItem item = GetItemByRow( m_currentRow ); + m_currentCol->GetRenderer()->StartEditing( item, labelRect ); } //------------------------------------------------------------------ @@ -1711,8 +1837,10 @@ public: // 2: Job not done, continue enum { OK = 0 , IGR = 1, CONT = 2 }; virtual int operator() ( wxDataViewTreeNode * node ) = 0 ; + virtual int operator() ( void * n ) = 0; }; +#if 0 class ItemAddJob: public DoJob { public: @@ -1738,43 +1866,86 @@ private: int * m_count; wxDataViewItem parent, item; }; +#endif bool Walker( wxDataViewTreeNode * node, DoJob & func ) { if( node==NULL || !node->HasChildren()) return false; - wxDataViewTreeNodes nodes = node->GetChildren(); - int len = node->GetChildrenNumber(); - int i = 0 ; + wxDataViewTreeNodes nodes = node->GetNodes(); + wxDataViewTreeLeaves leaves = node->GetChildren(); + + int len_nodes = nodes.GetCount(); + int len = leaves.GetCount(); + int i = 0, nodes_i = 0; for( ; i < len ; i ++ ) { - wxDataViewTreeNode * n = nodes[i]; - switch( func( n ) ) + void * n = leaves[i]; + if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() ) { - case DoJob::OK : - return true ; - case DoJob::IGR: - continue; - case DoJob::CONT: - default: - ; + wxDataViewTreeNode * nd = nodes[nodes_i]; + nodes_i++; + + switch( func( nd ) ) + { + case DoJob::OK : + return true ; + case DoJob::IGR: + continue; + case DoJob::CONT: + default: + ; + } + + if( Walker( nd , func ) ) + return true; + } - - if( Walker( n , func ) ) - return true; + else + switch( func( n ) ) + { + case DoJob::OK : + return true ; + case DoJob::IGR: + continue; + case DoJob::CONT: + default: + ; + } } return false; } bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) { - ItemAddJob job( parent, item, &m_count); - Walker( m_root , job); + g_model = GetOwner()->GetModel(); + + wxDataViewTreeNode * node; + node = FindNode(parent); + + if( node == NULL ) + return false; + + node->SetHasChildren( true ); + + if( g_model->IsContainer( item ) ) + { + wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); + newnode->SetItem(item); + node->AddNode( newnode); + } + else + node->AddLeaf( item.GetID() ); + + node->ChangeSubTreeCount(1); + + m_count = -1; UpdateDisplay(); return true; } +#if 0 class ItemDeleteJob: public DoJob { public: @@ -1796,17 +1967,65 @@ private: int * m_count; wxDataViewItem m_item; }; +#endif + +void DestroyTreeHelper( wxDataViewTreeNode * node); -bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem & item) +bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, + const wxDataViewItem& item) { - ItemDeleteJob job( item, &m_count); - Walker( m_root, job); + g_model = GetOwner()->GetModel(); + + wxDataViewTreeNode * node; + node = FindNode(parent); + + if( node == NULL || node->GetChildren().Index( item.GetID() ) == wxNOT_FOUND ) + { + return false; + } + + int sub = -1; + node->GetChildren().Remove( item.GetID() ); + if( GetOwner()->GetModel()->IsContainer( item ) ) + { + wxDataViewTreeNode * n = NULL; + wxDataViewTreeNodes nodes = node->GetNodes(); + int len = nodes.GetCount(); + for( int i = 0 ; i < len; i ++) + { + if( nodes[i]->GetItem() == item ) + { + n = nodes[i]; + break; + } + } + + if (!n) + return false; + + node->GetNodes().Remove( n ); + sub -= n->GetSubTreeCount(); + DestroyTreeHelper(n); + } + + if( node->GetChildrenNumber() == 0) + node->SetHasChildren( false ); + + //Make the row number invalid and get a new valid one when user call GetRowCount + m_count = -1; + node->ChangeSubTreeCount(sub); + //Change the current row to the last row if the current exceed the max row number + if( m_currentRow > GetRowCount() ) + m_currentRow = m_count - 1; + UpdateDisplay(); return true; } bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) { + g_model = GetOwner()->GetModel(); + unsigned int row = GetRowByItem(item); RefreshRow( row ); return true; @@ -1823,6 +2042,8 @@ bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned i return true; */ + g_model = GetOwner()->GetModel(); + unsigned int row = GetRowByItem(item); RefreshRow( row ); return true; @@ -1830,6 +2051,8 @@ bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned i bool wxDataViewMainWindow::Cleared() { + g_model = GetOwner()->GetModel(); + DestroyTree(); UpdateDisplay(); return true; @@ -2008,8 +2231,10 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) wxVariant value; wxDataViewTreeNode * node = GetTreeNodeByRow(item); if( node == NULL ) + { continue; - + } + wxDataViewItem dataitem = node->GetItem(); model->GetValue( value, dataitem, col->GetModelColumn()); cell->SetValue( value ); @@ -2017,8 +2242,7 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) // update the y offset cell_rect.y = item * m_lineHeight; - //Draw the expander here. Please notice that I use const number for all pixel data. When the final API are determined - //I will change this to the data member of the class wxDataViewCtrl + //Draw the expander here. int indent = node->GetIndentLevel(); if( col->GetModelColumn() == GetOwner()->GetExpanderColumn() ) { @@ -2043,15 +2267,17 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) } else { - // I am wandering whether we should draw dot lines between tree nodes + // I am wandering whether we should draw dot lines between tree nodes + delete node; + //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow } //force the expander column to left-center align cell->SetAlignment( wxALIGN_CENTER_VERTICAL ); } - - // cannot be bigger than allocated space + + // cannot be bigger than allocated space wxSize size = cell->GetSize(); // Because of the tree structure indent, here we should minus the width of the cell for drawing size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent ); @@ -2084,7 +2310,7 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) //Here we add the tree indent item_rect.x += indent; - + int state = 0; if (m_selection.Index(item) != wxNOT_FOUND) state |= wxDATAVIEW_CELL_SELECTED; @@ -2135,7 +2361,7 @@ unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const return y / m_lineHeight; } -unsigned int wxDataViewMainWindow::GetLastVisibleRow() +unsigned int wxDataViewMainWindow::GetLastVisibleRow() { wxSize client_size = GetClientSize(); m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, @@ -2144,7 +2370,7 @@ unsigned int wxDataViewMainWindow::GetLastVisibleRow() return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 ); } -unsigned int wxDataViewMainWindow::GetRowCount() +unsigned int wxDataViewMainWindow::GetRowCount() { if ( m_count == -1 ) { @@ -2375,19 +2601,33 @@ public: virtual ~RowToItemJob(){}; virtual int operator() ( wxDataViewTreeNode * node ) - { - if( current == static_cast(row)) - { - ret = node->GetItem() ; - return DoJob::OK; - } - current ++; - if ( node->IsOpen()) - return DoJob::CONT; - else - return DoJob::IGR; - } + { + current ++; + if( current == static_cast(row)) + { + ret = node->GetItem() ; + return DoJob::OK; + } + + if( node->GetSubTreeCount() + current < static_cast(row) ) + { + current += node->GetSubTreeCount(); + return DoJob::IGR; + } + else + return DoJob::CONT; + } + virtual int operator() ( void * n ) + { + current ++; + if( current == static_cast(row)) + { + ret = wxDataViewItem( n ) ; + return DoJob::OK; + } + return DoJob::CONT; + } wxDataViewItem GetResult(){ return ret; } private: unsigned int row; @@ -2397,7 +2637,7 @@ private: wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) { - RowToItemJob job( row, 0 ); + RowToItemJob job( row, -1 ); Walker( m_root , job ); return job.GetResult(); } @@ -2405,44 +2645,73 @@ wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) class RowToTreeNodeJob: public DoJob { public: - RowToTreeNodeJob( unsigned int row , int current ) { this->row = row; this->current = current ; ret = NULL ; } + RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node ) + { + this->row = row; + this->current = current ; + ret = NULL ; + parent = node; + } virtual ~RowToTreeNodeJob(){}; virtual int operator() ( wxDataViewTreeNode * node ) { + current ++; if( current == static_cast(row)) { ret = node ; return DoJob::OK; } - current ++; - if ( node->IsOpen()) - return DoJob::CONT; + + if( node->GetSubTreeCount() + current < static_cast(row) ) + { + current += node->GetSubTreeCount(); + return DoJob::IGR; + } else - return DoJob::IGR; + { + parent = node; + return DoJob::CONT; + } + } + virtual int operator() ( void * n ) + { + current ++; + if( current == static_cast(row)) + { + ret = new wxDataViewTreeNode( parent ) ; + ret->SetItem( wxDataViewItem( n )); + ret->SetHasChildren(false); + return DoJob::OK; + } + + return DoJob::CONT; + } wxDataViewTreeNode * GetResult(){ return ret; } private: unsigned int row; int current ; wxDataViewTreeNode * ret; + wxDataViewTreeNode * parent ; }; wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) { - RowToTreeNodeJob job( row , 0 ); + RowToTreeNodeJob job( row , -1, m_root ); Walker( m_root , job ); return job.GetResult(); } +#if 0 class CountJob : public DoJob { public: CountJob(){ count = 0 ; } virtual ~CountJob(){}; - + virtual int operator () ( wxDataViewTreeNode * node ) { count ++; @@ -2459,6 +2728,7 @@ public: private: unsigned int count; }; +#endif void wxDataViewMainWindow::OnExpanding( unsigned int row ) { @@ -2473,8 +2743,16 @@ void wxDataViewMainWindow::OnExpanding( unsigned int row ) if( node->GetChildrenNumber() == 0 ) BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node); m_count = -1; - Refresh(); + UpdateDisplay(); } + else + { + SelectRow( row, false ); + SelectRow( row + 1, true ); + ChangeCurrentRow( row + 1 ); + } + else + delete node; } } @@ -2483,11 +2761,13 @@ void wxDataViewMainWindow::OnCollapsing(unsigned int row) wxDataViewTreeNode * node = GetTreeNodeByRow(row); if( node != NULL ) { + wxDataViewTreeNode * nd = node; + if( node->HasChildren() && node->IsOpen() ) { node->ToggleOpen(); m_count = -1; - Refresh(); + UpdateDisplay(); //RefreshRows(row,GetLastVisibleRow()); } else @@ -2501,80 +2781,180 @@ void wxDataViewMainWindow::OnCollapsing(unsigned int row) ChangeCurrentRow( parent ); } } + if( !nd->HasChildren()) + delete nd; } } -int wxDataViewMainWindow::RecalculateCount() +wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item ) { - CountJob job; - Walker( m_root, job ); - return job.GetResult(); + wxDataViewModel * model = GetOwner()->GetModel(); + if( model == NULL ) + return NULL; + + //Compose the a parent-chain of the finding item + ItemList list; + list.DeleteContents( true ); + wxDataViewItem it( item ); + while( it.IsOk() ) + { + wxDataViewItem * pItem = new wxDataViewItem( it ); + list.Insert( pItem ); + it = model->GetParent( it ); + } + + //Find the item along the parent-chain. + //This algorithm is designed to speed up the node-finding method + bool found = true; + wxDataViewTreeNode * node = m_root; + for( ItemList::Node * n = list.GetFirst(); n; n = n->GetNext() ) + { + if( node->HasChildren() ) + { + if( node->GetChildrenNumber() == 0 ) + BuildTreeHelper(model, node->GetItem(), node); + + int len = node->GetNodeNumber(); + wxDataViewTreeNodes nodes = node->GetNodes(); + int j = 0; + for( ; j < len; j ++) + { + if( nodes[j]->GetItem() == *(n->GetData())) + { + node = nodes[j]; + break; + } + } + // Whenever we can't find the node in any level, return NULL to indicate the item can't be found + if( j == len ) + { + found = false; + return NULL; + } + } + else + return NULL; + } + return node; +} + +int wxDataViewMainWindow::RecalculateCount() +{ + return m_root->GetSubTreeCount(); } class ItemToRowJob : public DoJob { public: - ItemToRowJob(const wxDataViewItem & item){ this->item = item ; ret = 0 ; } + ItemToRowJob(const wxDataViewItem & item, ItemList::Node * node ) + { this->item = item ; ret = 0 ; nd = node ; } virtual ~ItemToRowJob(){}; virtual int operator() ( wxDataViewTreeNode * node) { ret ++; if( node->GetItem() == item ) + { return DoJob::OK; + } - if( node->IsOpen()) + if( nd && node->GetItem() == *(nd->GetData())) + { + nd = nd->GetNext(); return DoJob::CONT; + } else + { + ret += node->GetSubTreeCount(); return DoJob::IGR; + } + } + virtual int operator() ( void * n ) + { + ret ++; + if( n == item.GetID() ) + return DoJob::OK; + return DoJob::CONT; + } //the row number is begin from zero int GetResult(){ return ret -1 ; } private: + ItemList::Node * nd; wxDataViewItem item; int ret; + }; unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) { - ItemToRowJob job( item ); + wxDataViewModel * model = GetOwner()->GetModel(); + if( model == NULL ) + return 0; + + //Compose the a parent-chain of the finding item + ItemList list; + list.DeleteContents( true ); + wxDataViewItem it( item ); + while( it.IsOk() ) + { + wxDataViewItem * pItem = new wxDataViewItem( it ); + list.Insert( pItem ); + it = model->GetParent( it ); + } + + ItemToRowJob job( item, list.GetFirst() ); Walker(m_root , job ); return job.GetResult(); } void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node) { - if( !model->HasChildren( item ) ) + if( !model->IsContainer( item ) ) return ; - + wxDataViewItem i = model->GetFirstChild( item ); + int num = 0; while( i.IsOk() ) { - wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); - n->SetItem(i); - n->SetHasChildren( model->HasChildren( i )) ; - node->AppendChild(n); - //BuildTreeHelper( model, i, n) ; + num ++; + if( model->IsContainer( i ) ) + { + wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); + n->SetItem(i); + n->SetHasChildren( true ) ; + node->AddNode( n ); + } + else + { + node->AddLeaf( i.GetID() ); + } i = model->GetNextSibling( i ); } + node->SetSubTreeCount( num ); + wxDataViewTreeNode * n = node->GetParent(); + if( n != NULL) + n->ChangeSubTreeCount(num); + } void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) { //First we define a invalid item to fetch the top-level elements wxDataViewItem item; + g_model = GetOwner()->GetModel(); BuildTreeHelper( model, item, m_root); m_count = -1 ; } void DestroyTreeHelper( wxDataViewTreeNode * node ) { - if( node->HasChildren() ) + if( node->GetNodeNumber() != 0 ) { - int len = node->GetChildrenNumber(); + int len = node->GetNodeNumber(); int i = 0 ; - wxDataViewTreeNodes nodes = node->GetChildren(); + wxDataViewTreeNodes nodes = node->GetNodes(); for( ; i < len; i ++ ) { DestroyTreeHelper(nodes[i]); @@ -2586,6 +2966,7 @@ void DestroyTreeHelper( wxDataViewTreeNode * node ) void wxDataViewMainWindow::DestroyTree() { DestroyTreeHelper(m_root); + m_root->SetSubTreeCount(0); m_count = 0 ; } @@ -2754,13 +3135,13 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) { if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) { + wxDataViewItem item = GetItemByRow(current); wxVariant value; - model->GetValue( value, col->GetModelColumn(), current ); + model->GetValue( value, item, col->GetModelColumn() ); cell->SetValue( value ); wxRect cell_rect( xpos, current * m_lineHeight, col->GetWidth(), m_lineHeight ); - wxDataViewItem dataitem = GetItemByRow(current); - cell->Activate( cell_rect, model, dataitem, col->GetModelColumn() ); + cell->Activate( cell_rect, model, item, col->GetModelColumn() ); } return; } @@ -2942,7 +3323,8 @@ wxDataViewItem wxDataViewMainWindow::GetSelection() { if( m_selection.GetCount() != 1 ) return wxDataViewItem(); - return GetItemByRow( m_selection.Item( 0 ) ); + + return GetItemByRow( m_selection.Item(0)); } //----------------------------------------------------------------------------- @@ -3074,7 +3456,7 @@ void wxDataViewCtrl::DoSetIndent() m_clientArea->UpdateDisplay(); } -wxDataViewItem wxDataViewCtrl::GetSelection() +wxDataViewItem wxDataViewCtrl::GetSelection() { return m_clientArea->GetSelection(); }