+ //EnsureVisible( m_currentRow );
+}
+
+wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
+{
+ wxRect rect;
+ rect.x = 0;
+ rect.y = m_lineHeight * row;
+ rect.width = GetEndOfLastCol();
+ rect.height = m_lineHeight;
+
+ return rect;
+}
+
+class RowToItemJob: public DoJob
+{
+public:
+ RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;}
+ virtual ~RowToItemJob(){};
+
+ virtual int operator() ( wxDataViewTreeNode * node )
+ {
+ current ++;
+ if( current == static_cast<int>(row))
+ {
+ ret = node->GetItem() ;
+ return DoJob::OK;
+ }
+
+ if( node->GetSubTreeCount() + current < static_cast<int>(row) )
+ {
+ current += node->GetSubTreeCount();
+ return DoJob::IGR;
+ }
+ else
+ return DoJob::CONT;
+ }
+
+ virtual int operator() ( void * n )
+ {
+ current ++;
+ if( current == static_cast<int>(row))
+ {
+ ret = wxDataViewItem( n ) ;
+ return DoJob::OK;
+ }
+ return DoJob::CONT;
+ }
+ wxDataViewItem GetResult(){ return ret; }
+private:
+ unsigned int row;
+ int current ;
+ wxDataViewItem ret;
+};
+
+wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row)
+{
+ RowToItemJob job( row, -1 );
+ Walker( m_root , job );
+ return job.GetResult();
+}
+
+class RowToTreeNodeJob: public DoJob
+{
+public:
+ 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<int>(row))
+ {
+ ret = node ;
+ return DoJob::OK;
+ }
+
+ if( node->GetSubTreeCount() + current < static_cast<int>(row) )
+ {
+ current += node->GetSubTreeCount();
+ return DoJob::IGR;
+ }
+ else
+ {
+ parent = node;
+ return DoJob::CONT;
+ }
+
+ }
+
+ virtual int operator() ( void * n )
+ {
+ current ++;
+ if( current == static_cast<int>(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 , -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 ++;
+ if ( node->IsOpen())
+ return DoJob::CONT;
+ else
+ return DoJob::IGR;
+ }
+
+ unsigned int GetResult()
+ {
+ return count ;
+ }
+private:
+ unsigned int count;
+};
+#endif
+
+void wxDataViewMainWindow::OnExpanding( unsigned int row )
+{
+ wxDataViewTreeNode * node = GetTreeNodeByRow(row);
+ if( node != NULL )
+ {
+ if( node->HasChildren())
+ if( !node->IsOpen())
+ {
+ node->ToggleOpen();
+ //Here I build the children of current node
+ if( node->GetChildrenNumber() == 0 )
+ BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
+ m_count = -1;
+ UpdateDisplay();
+ }
+ else
+ {
+ SelectRow( row, false );
+ SelectRow( row + 1, true );
+ ChangeCurrentRow( row + 1 );
+ }
+ else
+ delete node;
+ }
+}
+
+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;
+ UpdateDisplay();
+ //RefreshRows(row,GetLastVisibleRow());
+ }
+ else
+ {
+ node = node->GetParent();
+ if( node != NULL )
+ {
+ int parent = GetRowByItem( node->GetItem()) ;
+ SelectRow( row, false);
+ SelectRow(parent , true );
+ ChangeCurrentRow( parent );
+ }
+ }
+ if( !nd->HasChildren())
+ delete nd;
+ }
+}
+
+wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
+{
+ 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, 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( 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)
+{
+ 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->IsContainer( item ) )
+ return ;
+
+ wxDataViewItem i = model->GetFirstChild( item );
+ int num = 0;
+ while( i.IsOk() )
+ {
+ 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->GetNodeNumber() != 0 )
+ {
+ int len = node->GetNodeNumber();
+ int i = 0 ;
+ wxDataViewTreeNodes nodes = node->GetNodes();
+ for( ; i < len; i ++ )
+ {
+ DestroyTreeHelper(nodes[i]);
+ }
+ }
+ delete node;
+}
+
+void wxDataViewMainWindow::DestroyTree()
+{
+ DestroyTreeHelper(m_root);
+ m_root->SetSubTreeCount(0);
+ m_count = 0 ;