+ wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
+ newnode->SetItem(item);
+ newnode->SetHasChildren( true );
+ node->AddNode( newnode);
+ }
+ else
+ node->AddLeaf( item.GetID() );
+
+ node->ChangeSubTreeCount(1);
+
+ m_count = -1;
+ UpdateDisplay();
+
+ return true;
+}
+
+static void DestroyTreeHelper( wxDataViewTreeNode * node);
+
+bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
+ const wxDataViewItem& item)
+{
+ GetOwner()->InvalidateColBestWidths();
+
+ if (IsVirtualList())
+ {
+ wxDataViewVirtualListModel *list_model =
+ (wxDataViewVirtualListModel*) GetOwner()->GetModel();
+ m_count = list_model->GetCount();
+
+ if( m_currentRow > GetRowCount() )
+ m_currentRow = m_count - 1;
+
+ // TODO: why empty the entire selection?
+ m_selection.Empty();
+
+ UpdateDisplay();
+
+ return true;
+ }
+
+ wxDataViewTreeNode * node = FindNode(parent);
+
+ // Notice that it is possible that the item being deleted is not in the
+ // tree at all, for example we could be deleting a never shown (because
+ // collapsed) item in a tree model. So it's not an error if we don't know
+ // about this item, just return without doing anything then.
+ if ( !node || node->GetChildren().Index(item.GetID()) == wxNOT_FOUND )
+ return false;
+
+ int sub = -1;
+ node->GetChildren().Remove( item.GetID() );
+ // Manipolate selection
+ if( m_selection.GetCount() > 1 )
+ {
+ m_selection.Empty();
+ }
+ bool isContainer = false;
+ wxDataViewTreeNodes nds = node->GetNodes();
+ for (size_t i = 0; i < nds.GetCount(); i ++)
+ {
+ if (nds[i]->GetItem() == item)
+ {
+ isContainer = true;
+ break;
+ }
+ }
+ if( isContainer )
+ {
+ 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;
+ }
+ }
+
+ wxCHECK_MSG( n != NULL, false, "item not found" );
+
+ node->GetNodes().Remove( n );
+ sub -= n->GetSubTreeCount();
+ ::DestroyTreeHelper(n);
+ }
+ // 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)
+{
+ GetOwner()->InvalidateColBestWidths();
+
+ SortPrepare();
+ g_model->Resort();
+
+ // Send event
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
+ le.SetEventObject(parent);
+ le.SetModel(GetOwner()->GetModel());
+ le.SetItem(item);
+ parent->GetEventHandler()->ProcessEvent(le);
+
+ return true;
+}
+
+bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int model_column )
+{
+ int view_column = -1;
+ unsigned int n_col = m_owner->GetColumnCount();
+ for (unsigned i = 0; i < n_col; i++)
+ {
+ wxDataViewColumn *column = m_owner->GetColumn( i );
+ if (column->GetModelColumn() == model_column)
+ {
+ view_column = (int) i;
+ break;
+ }
+ }
+ if (view_column == -1)
+ return false;
+
+ GetOwner()->InvalidateColBestWidth(view_column);
+
+ // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
+/*#define MAX_VIRTUAL_WIDTH 100000
+
+ wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
+ m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+ Refresh( true, &rect );
+
+ return true;
+*/
+ SortPrepare();
+ g_model->Resort();
+
+ // Send event
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
+ le.SetEventObject(parent);
+ le.SetModel(GetOwner()->GetModel());
+ le.SetItem(item);
+ le.SetColumn(view_column);
+ le.SetDataViewColumn(GetOwner()->GetColumn(view_column));
+ parent->GetEventHandler()->ProcessEvent(le);
+
+ return true;
+}
+
+bool wxDataViewMainWindow::Cleared()
+{
+ GetOwner()->InvalidateColBestWidths();
+
+ DestroyTree();
+ m_selection.Clear();
+
+ SortPrepare();
+ BuildTree( GetOwner()->GetModel() );
+
+ UpdateDisplay();
+
+ return true;
+}
+
+void wxDataViewMainWindow::UpdateDisplay()
+{
+ m_dirty = true;
+ m_underMouse = NULL;
+}
+
+void wxDataViewMainWindow::OnInternalIdle()
+{
+ wxWindow::OnInternalIdle();
+
+ if (m_dirty)
+ {
+ RecalculateDisplay();
+ m_dirty = false;
+ }
+}
+
+void wxDataViewMainWindow::RecalculateDisplay()
+{
+ wxDataViewModel *model = GetOwner()->GetModel();
+ if (!model)
+ {
+ Refresh();
+ return;
+ }
+
+ int width = GetEndOfLastCol();
+ int height = GetLineStart( GetRowCount() );
+
+ SetVirtualSize( width, height );
+ GetOwner()->SetScrollRate( 10, m_lineHeight );
+
+ Refresh();
+}
+
+void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
+{
+ m_underMouse = NULL;
+
+ wxWindow::ScrollWindow( dx, dy, rect );
+
+ if (GetOwner()->m_headerArea)
+ GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
+}
+
+void wxDataViewMainWindow::ScrollTo( int rows, int column )
+{
+ m_underMouse = NULL;
+
+ int x, y;
+ m_owner->GetScrollPixelsPerUnit( &x, &y );
+ int sy = GetLineStart( rows )/y;
+ int sx = 0;
+ if( column != -1 )
+ {
+ wxRect rect = GetClientRect();
+ int colnum = 0;
+ int x_start, w = 0;
+ int xx, yy, xe;
+ m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
+ for (x_start = 0; colnum < column; colnum++)
+ {
+ wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum);
+ if (col->IsHidden())
+ continue; // skip it!
+
+ w = col->GetWidth();
+ x_start += w;
+ }
+
+ int x_end = x_start + w;
+ xe = xx + rect.width;
+ if( x_end > xe )
+ {
+ sx = ( xx + x_end - xe )/x;
+ }
+ if( x_start < xx )
+ {
+ sx = x_start/x;
+ }
+ }
+ m_owner->Scroll( sx, sy );
+}
+
+int wxDataViewMainWindow::GetCountPerPage() const
+{
+ wxSize size = GetClientSize();
+ return size.y / m_lineHeight;
+}
+
+int wxDataViewMainWindow::GetEndOfLastCol() const
+{
+ int width = 0;
+ unsigned int i;
+ for (i = 0; i < GetOwner()->GetColumnCount(); i++)
+ {
+ const wxDataViewColumn *c =
+ const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i );
+
+ if (!c->IsHidden())
+ width += c->GetWidth();
+ }
+ return width;
+}
+
+unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
+{
+ int x = 0;
+ int y = 0;
+ m_owner->CalcUnscrolledPosition( x, y, &x, &y );
+
+ return GetLineAt( y );
+}
+
+unsigned int wxDataViewMainWindow::GetLastVisibleRow()
+{
+ wxSize client_size = GetClientSize();
+ m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
+ &client_size.x, &client_size.y );
+
+ // we should deal with the pixel here
+ unsigned int row = GetLineAt(client_size.y) - 1;
+
+ return wxMin( GetRowCount()-1, row );
+}
+
+unsigned int wxDataViewMainWindow::GetRowCount()
+{
+ if ( m_count == -1 )
+ {
+ m_count = RecalculateCount();
+ UpdateDisplay();
+ }
+ return m_count;
+}
+
+void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
+{
+ m_currentRow = row;
+
+ // send event
+}
+
+void wxDataViewMainWindow::SelectAllRows( bool on )
+{
+ if (IsEmpty())
+ return;
+
+ if (on)
+ {
+ m_selection.Clear();
+ for (unsigned int i = 0; i < GetRowCount(); i++)
+ m_selection.Add( i );
+ Refresh();
+ }
+ else
+ {
+ unsigned int first_visible = GetFirstVisibleRow();
+ unsigned int last_visible = GetLastVisibleRow();
+ unsigned int i;
+ for (i = 0; i < m_selection.GetCount(); i++)
+ {
+ unsigned int row = m_selection[i];
+ if ((row >= first_visible) && (row <= last_visible))
+ RefreshRow( row );
+ }
+ m_selection.Clear();
+ }
+}
+
+void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
+{
+ if (m_selection.Index( row ) == wxNOT_FOUND)
+ {
+ if (on)
+ {
+ m_selection.Add( row );
+ RefreshRow( row );
+ }
+ }
+ else
+ {
+ if (!on)
+ {
+ m_selection.Remove( row );
+ RefreshRow( row );
+ }