+void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
+{
+ wxSize client_size = GetClientSize();
+ int start = GetLineStart( firstRow );
+ m_owner->CalcScrolledPosition( start, 0, &start, NULL );
+ if (start > client_size.y) return;
+
+ wxRect rect( 0, start, client_size.x, client_size.y - start );
+
+ Refresh( true, &rect );
+}
+
+void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
+{
+ wxCHECK_RET( newCurrent < GetRowCount(),
+ wxT("invalid item index in OnArrowChar()") );
+
+ // if there is no selection, we cannot move it anywhere
+ if (!HasCurrentRow())
+ return;
+
+ unsigned int oldCurrent = m_currentRow;
+
+ // in single selection we just ignore Shift as we can't select several
+ // items anyhow
+ if ( event.ShiftDown() && !IsSingleSel() )
+ {
+ RefreshRow( oldCurrent );
+
+ ChangeCurrentRow( newCurrent );
+
+ // select all the items between the old and the new one
+ if ( oldCurrent > newCurrent )
+ {
+ newCurrent = oldCurrent;
+ oldCurrent = m_currentRow;
+ }
+
+ SelectRows( oldCurrent, newCurrent, true );
+ if (oldCurrent!=newCurrent)
+ SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
+ }
+ else // !shift
+ {
+ RefreshRow( oldCurrent );
+
+ // all previously selected items are unselected unless ctrl is held
+ if ( !event.ControlDown() )
+ SelectAllRows(false);
+
+ ChangeCurrentRow( newCurrent );
+
+ if ( !event.ControlDown() )
+ {
+ SelectRow( m_currentRow, true );
+ SendSelectionChangedEvent(GetItemByRow(m_currentRow));
+ }
+ else
+ RefreshRow( m_currentRow );
+ }
+
+ GetOwner()->EnsureVisible( m_currentRow, -1 );
+}
+
+wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
+{
+ wxRect rect;
+ rect.x = 0;
+ rect.y = GetLineStart( row );
+ rect.width = GetEndOfLastCol();
+ rect.height = GetLineHeight( row );
+
+ return rect;
+}
+
+int wxDataViewMainWindow::GetLineStart( unsigned int row ) const
+{
+ const wxDataViewModel *model = GetOwner()->GetModel();
+
+ if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
+ {
+ // TODO make more efficient
+
+ int start = 0;
+
+ unsigned int r;
+ for (r = 0; r < row; r++)
+ {
+ const wxDataViewTreeNode* node = GetTreeNodeByRow(r);
+ if (!node) return start;
+
+ wxDataViewItem item = node->GetItem();
+
+ if (node && !node->HasChildren())
+ {
+ // Yes, if the node does not have any child, it must be a leaf which
+ // mean that it is a temporarily created by GetTreeNodeByRow
+ wxDELETE(node);
+ }
+
+ unsigned int cols = GetOwner()->GetColumnCount();
+ unsigned int col;
+ int height = m_lineHeight;
+ for (col = 0; col < cols; col++)
+ {
+ const wxDataViewColumn *column = GetOwner()->GetColumn(col);
+ if (column->IsHidden())
+ continue; // skip it!
+
+ if ((col != 0) &&
+ model->IsContainer(item) &&
+ !model->HasContainerColumns(item))
+ continue; // skip it!
+
+ wxVariant value;
+ model->GetValue( value, item, column->GetModelColumn() );
+
+ wxDataViewRenderer *renderer =
+ const_cast<wxDataViewRenderer*>(column->GetRenderer());
+ renderer->SetValue( value );
+ height = wxMax( height, renderer->GetSize().y );
+ }
+
+ start += height;
+ }
+
+ return start;
+ }
+ else
+ {
+ return row * m_lineHeight;
+ }
+}
+
+int wxDataViewMainWindow::GetLineAt( unsigned int y ) const
+{
+ const wxDataViewModel *model = GetOwner()->GetModel();
+
+ // check for the easy case first
+ if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
+ return y / m_lineHeight;
+
+ // TODO make more efficient
+ unsigned int row = 0;
+ unsigned int yy = 0;
+ for (;;)
+ {
+ const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
+ if (!node)
+ {
+ // not really correct...
+ return row + ((y-yy) / m_lineHeight);
+ }
+
+ wxDataViewItem item = node->GetItem();
+
+ if (node && !node->HasChildren())
+ {
+ // Yes, if the node does not have any child, it must be a leaf which
+ // mean that it is a temporarily created by GetTreeNodeByRow
+ wxDELETE(node);
+ }
+
+ unsigned int cols = GetOwner()->GetColumnCount();
+ unsigned int col;
+ int height = m_lineHeight;
+ for (col = 0; col < cols; col++)
+ {
+ const wxDataViewColumn *column = GetOwner()->GetColumn(col);
+ if (column->IsHidden())
+ continue; // skip it!
+
+ if ((col != 0) &&
+ model->IsContainer(item) &&
+ !model->HasContainerColumns(item))
+ continue; // skip it!
+
+ wxVariant value;
+ model->GetValue( value, item, column->GetModelColumn() );
+
+ wxDataViewRenderer *renderer =
+ const_cast<wxDataViewRenderer*>(column->GetRenderer());
+ renderer->SetValue( value );
+ height = wxMax( height, renderer->GetSize().y );
+ }
+
+ yy += height;
+ if (y < yy)
+ return row;
+
+ row++;
+ }
+}
+
+int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const
+{
+ const wxDataViewModel *model = GetOwner()->GetModel();
+
+ if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
+ {
+ wxASSERT( !IsVirtualList() );
+
+ const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
+ // wxASSERT( node );
+ if (!node) return m_lineHeight;
+
+ wxDataViewItem item = node->GetItem();
+
+ if (node && !node->HasChildren())
+ {
+ // Yes, if the node does not have any child, it must be a leaf which
+ // mean that it is a temporarily created by GetTreeNodeByRow
+ wxDELETE(node);
+ }
+
+ int height = m_lineHeight;
+
+ unsigned int cols = GetOwner()->GetColumnCount();
+ unsigned int col;
+ for (col = 0; col < cols; col++)
+ {
+ const wxDataViewColumn *column = GetOwner()->GetColumn(col);
+ if (column->IsHidden())
+ continue; // skip it!
+
+ if ((col != 0) &&
+ model->IsContainer(item) &&
+ !model->HasContainerColumns(item))
+ continue; // skip it!
+
+ wxVariant value;
+ model->GetValue( value, item, column->GetModelColumn() );
+
+ wxDataViewRenderer *renderer =
+ const_cast<wxDataViewRenderer*>(column->GetRenderer());
+ renderer->SetValue( value );
+ height = wxMax( height, renderer->GetSize().y );
+ }
+
+ return height;
+ }
+ else
+ {
+ return m_lineHeight;
+ }
+}
+
+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
+ {
+ // If the current has no child node, we can find the desired item of the row
+ // number directly.
+ // This if can speed up finding in some case, and will has a very good effect
+ // when it comes to list view
+ if( node->GetNodes().GetCount() == 0)
+ {
+ int index = static_cast<int>(row) - current - 1;
+ ret = node->GetChildren().Item( index );
+ return DoJob::OK;
+ }
+ 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() const
+ { return ret; }
+
+private:
+ unsigned int row;
+ int current;
+ wxDataViewItem ret;
+};
+
+wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
+{
+ if (IsVirtualList())
+ {
+ return wxDataViewItem( wxUIntToPtr(row+1) );
+ }
+ else
+ {
+ RowToItemJob job( row, -2 );
+ 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;
+
+ // If the current node has no children, we can find the desired item of the
+ // row number directly.
+ // This if can speed up finding in some case, and will have a very good
+ // effect for list views.
+ if( node->GetNodes().GetCount() == 0)
+ {
+ int index = static_cast<int>(row) - current - 1;
+ void * n = node->GetChildren().Item( index );
+ ret = new wxDataViewTreeNode( parent );
+ ret->SetItem( wxDataViewItem( n ));
+ ret->SetHasChildren(false);
+ return DoJob::OK;
+ }
+ 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() const
+ { return ret; }
+
+private:
+ unsigned int row;
+ int current;
+ wxDataViewTreeNode * ret;
+ wxDataViewTreeNode * parent;
+};
+
+wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const
+{
+ wxASSERT( !IsVirtualList() );
+
+ RowToTreeNodeJob job( row , -2, m_root );
+ Walker( m_root , job );
+ return job.GetResult();
+}
+
+wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type,
+ const wxDataViewItem & item )
+{
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(type, parent->GetId());
+
+ le.SetEventObject(parent);
+ le.SetModel(GetOwner()->GetModel());
+ le.SetItem( item );
+
+ parent->GetEventHandler()->ProcessEvent(le);
+ return le;
+}
+
+bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const
+{
+ if (IsVirtualList())
+ return false;
+
+ wxDataViewTreeNode * node = GetTreeNodeByRow(row);
+ if (!node)
+ return false;
+
+ if (!node->HasChildren())
+ {
+ delete node;
+ return false;
+ }
+
+ return node->IsOpen();
+}
+
+bool wxDataViewMainWindow::HasChildren( unsigned int row ) const
+{
+ if (IsVirtualList())
+ return false;
+
+ wxDataViewTreeNode * node = GetTreeNodeByRow(row);
+ if (!node)
+ return false;
+
+ if (!node->HasChildren())
+ {
+ delete node;
+ return false;
+ }
+
+ return true;
+}
+
+void wxDataViewMainWindow::Expand( unsigned int row )
+{
+ if (IsVirtualList())
+ return;
+
+ wxDataViewTreeNode * node = GetTreeNodeByRow(row);
+ if (!node)
+ return;
+
+ if (!node->HasChildren())