+ // align as required the column title:
+ int x = xpos;
+ wxSize titleSz = dc.GetTextExtent(col->GetTitle());
+ switch (col->GetAlignment())
+ {
+ case wxALIGN_LEFT:
+ x += HEADER_HORIZ_BORDER;
+ break;
+ case wxALIGN_RIGHT:
+ x += cw - titleSz.GetWidth() - HEADER_HORIZ_BORDER;
+ break;
+ default:
+ case wxALIGN_CENTER:
+ case wxALIGN_CENTER_HORIZONTAL:
+ x += (cw - titleSz.GetWidth() - 2 * HEADER_HORIZ_BORDER)/2;
+ break;
+ }
+
+ // always center the title vertically:
+ int y = wxMax((ch - titleSz.GetHeight()) / 2, HEADER_VERT_BORDER);
+
+ dc.SetClippingRegion( xpos+HEADER_HORIZ_BORDER,
+ HEADER_VERT_BORDER,
+ wxMax(cw - 2 * HEADER_HORIZ_BORDER, 1), // width
+ wxMax(ch - 2 * HEADER_VERT_BORDER, 1)); // height
+ dc.DrawText( col->GetTitle(), x, y );
+ dc.DestroyClippingRegion();
+
+ xpos += cw;
+ }
+}
+
+void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event )
+{
+ GetParent()->SetFocus();
+ event.Skip();
+}
+
+void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent &event )
+{
+ // we want to work with logical coords
+ int x;
+ m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
+ int y = event.GetY();
+
+ if (m_isDragging)
+ {
+ // we don't draw the line beyond our window,
+ // but we allow dragging it there
+ int w = 0;
+ GetClientSize( &w, NULL );
+ m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
+ w -= 6;
+
+ if (event.ButtonUp())
+ {
+ m_isDragging = false;
+ if (HasCapture())
+ ReleaseMouse();
+
+ m_dirty = true;
+ }
+ m_currentX = wxMax(m_minX + 7, x);
+
+ if (m_currentX < w)
+ {
+ GetColumn(m_column)->SetWidth(m_currentX - m_minX);
+ Refresh();
+ GetOwner()->Refresh();
+ }
+
+ }
+ else // not dragging
+ {
+ m_minX = 0;
+ m_column = wxNOT_FOUND;
+
+ bool hit_border = false;
+
+ // end of the current column
+ int xpos = 0;
+
+ // find the column where this event occured
+ int countCol = m_owner->GetColumnCount();
+ for (int column = 0; column < countCol; column++)
+ {
+ wxDataViewColumn *p = GetColumn(column);
+
+ if (p->IsHidden())
+ continue; // skip if not shown
+
+ xpos += p->GetWidth();
+ m_column = column;
+ if ((abs(x-xpos) < 3) && (y < 22))
+ {
+ hit_border = true;
+ break;
+ }
+
+ if (x < xpos)
+ {
+ // inside the column
+ break;
+ }
+
+ m_minX = xpos;
+ }
+
+ int old_hover = m_hover;
+ m_hover = m_column;
+ if (event.Leaving())
+ m_hover = wxNOT_FOUND;
+ if (old_hover != m_hover)
+ Refresh();
+
+ if (m_column == wxNOT_FOUND)
+ return;
+
+ bool resizeable = GetColumn(m_column)->IsResizeable();
+ if (event.LeftDClick() && resizeable)
+ {
+ GetColumn(m_column)->SetWidth(GetOwner()->GetBestColumnWidth(m_column));
+ Refresh();
+ }
+ else if (event.LeftDown() || event.RightUp())
+ {
+ if (hit_border && event.LeftDown() && resizeable)
+ {
+ m_isDragging = true;
+ CaptureMouse();
+ m_currentX = x;
+ }
+ else // click on a column
+ {
+ wxDataViewModel * model = GetOwner()->GetModel();
+ wxEventType evt = event.LeftDown() ?
+ wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
+ wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
+ SendEvent(evt, m_column);
+
+ //Left click the header
+ if(event.LeftDown())
+ {
+ wxDataViewColumn *col = GetColumn(m_column);
+ if(col->IsSortable())
+ {
+ wxDataViewColumn* sortCol = m_owner->GetSortingColumn();
+ if(model && sortCol == col)
+ {
+ bool order = col->IsSortOrderAscending();
+ col->SetSortOrder(!order);
+ }
+ else if(model)
+ {
+ m_owner->SetSortingColumn(col);
+ }
+ }
+ UpdateDisplay();
+ if(model)
+ model->Resort();
+ //Send the column sorted event
+ SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, m_column);
+ }
+ }
+ }
+ else if (event.Moving())
+ {
+ if (hit_border && resizeable)
+ m_currentCursor = m_resizeCursor;
+ else
+ m_currentCursor = wxSTANDARD_CURSOR;
+
+ SetCursor(*m_currentCursor);
+ }
+ }
+}
+
+void wxGenericDataViewHeaderWindow::AdjustDC(wxDC& dc)
+{
+ int xpix, x;
+
+ m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
+ m_owner->GetViewStart( &x, NULL );
+
+ // shift the DC origin to match the position of the main window horizontal
+ // scrollbar: this allows us to always use logical coords
+ dc.SetDeviceOrigin( -x * xpix, 0 );
+}
+
+#endif // defined(__WXMSW__)
+
+//-----------------------------------------------------------------------------
+// wxDataViewRenameTimer
+//-----------------------------------------------------------------------------
+
+wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
+{
+ m_owner = owner;
+}
+
+void wxDataViewRenameTimer::Notify()
+{
+ m_owner->OnRenameTimer();
+}
+
+//-----------------------------------------------------------------------------
+// wxDataViewMainWindow
+//-----------------------------------------------------------------------------
+
+//The tree building helper, declared firstly
+static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node);
+
+int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
+{
+ if (row1 > row2) return 1;
+ if (row1 == row2) return 0;
+ return -1;
+}
+
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
+
+BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
+ EVT_PAINT (wxDataViewMainWindow::OnPaint)
+ EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
+ EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
+ EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
+ EVT_CHAR (wxDataViewMainWindow::OnChar)
+END_EVENT_TABLE()
+
+wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
+ const wxPoint &pos, const wxSize &size, const wxString &name ) :
+ wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ),
+ m_selection( wxDataViewSelectionCmp )
+
+{
+ SetOwner( parent );
+
+ m_lastOnSame = false;
+ m_renameTimer = new wxDataViewRenameTimer( this );
+
+ // TODO: user better initial values/nothing selected
+ m_currentCol = NULL;
+ m_currentRow = 0;
+
+ m_lineHeight = wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
+
+ m_dragCount = 0;
+ m_dragStart = wxPoint(0,0);
+ m_lineLastClicked = (unsigned int) -1;
+ m_lineBeforeLastClicked = (unsigned int) -1;
+ m_lineSelectSingleOnUp = (unsigned int) -1;
+
+ m_hasFocus = false;
+
+ SetBackgroundColour( *wxWHITE );
+
+ SetBackgroundStyle(wxBG_STYLE_CUSTOM);
+
+ m_penRule = wxPen(GetRuleColour());
+
+ //Here I compose a pen can draw black lines, maybe there are something system colour to use
+ m_penExpander = wxPen(wxColour(0,0,0));
+ //Some new added code to deal with the tree structure
+ m_root = new wxDataViewTreeNode( NULL );
+ m_root->SetHasChildren(true);
+
+ //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
+ m_count = -1 ;
+ m_underMouse = NULL;
+ UpdateDisplay();
+}
+
+wxDataViewMainWindow::~wxDataViewMainWindow()
+{
+ DestroyTree();
+ delete m_renameTimer;
+}
+
+void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
+{
+ wxDataViewModel *model = GetOwner()->GetModel();
+ wxAutoBufferedPaintDC dc( this );
+
+#ifdef __WXMSW__
+ dc.SetPen( *wxTRANSPARENT_PEN );
+ dc.SetBrush( wxBrush( GetBackgroundColour()) );
+ dc.SetBrush( *wxWHITE_BRUSH );
+ wxSize size( GetClientSize() );
+ dc.DrawRectangle( 0,0,size.x,size.y );
+#endif
+
+ // prepare the DC
+ GetOwner()->PrepareDC( dc );
+ dc.SetFont( GetFont() );
+
+ wxRect update = GetUpdateRegion().GetBox();
+ m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
+
+ // compute which items needs to be redrawn
+ unsigned int item_start = GetLineAt( wxMax(0,update.y) );
+ unsigned int item_count =
+ wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1),
+ (int)(GetRowCount( ) - item_start));
+ unsigned int item_last = item_start + item_count;
+
+ // compute which columns needs to be redrawn
+ unsigned int cols = GetOwner()->GetColumnCount();
+ unsigned int col_start = 0;
+ unsigned int x_start = 0;
+ for (x_start = 0; col_start < cols; col_start++)
+ {
+ wxDataViewColumn *col = GetOwner()->GetColumn(col_start);
+ if (col->IsHidden())
+ continue; // skip it!
+
+ unsigned int w = col->GetWidth();
+ if (x_start+w >= (unsigned int)update.x)
+ break;
+
+ x_start += w;
+ }
+
+ unsigned int col_last = col_start;
+ unsigned int x_last = x_start;
+ for (; col_last < cols; col_last++)
+ {
+ wxDataViewColumn *col = GetOwner()->GetColumn(col_last);
+ if (col->IsHidden())
+ continue; // skip it!
+
+ if (x_last > (unsigned int)update.GetRight())
+ break;
+
+ x_last += col->GetWidth();
+ }
+
+ // Draw horizontal rules if required
+ if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
+ {
+ dc.SetPen(m_penRule);
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+
+ for (unsigned int i = item_start; i <= item_last+1; i++)
+ {
+ int y = GetLineStart( i );
+ dc.DrawLine(x_start, y, x_last, y);
+ }
+ }
+
+ // Draw vertical rules if required
+ if ( m_owner->HasFlag(wxDV_VERT_RULES) )
+ {
+ dc.SetPen(m_penRule);
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+
+ int x = x_start;
+ for (unsigned int i = col_start; i < col_last; i++)
+ {
+ wxDataViewColumn *col = GetOwner()->GetColumn(i);
+ if (col->IsHidden())
+ continue; // skip it
+
+ dc.DrawLine(x, GetLineStart( item_start ),
+ x, GetLineStart( item_last ) );
+
+ x += col->GetWidth();
+ }
+
+ // Draw last vertical rule
+ dc.DrawLine(x, GetLineStart( item_start ),
+ x, GetLineStart( item_last ) );
+ }
+
+ // redraw the background for the items which are selected/current
+ for (unsigned int item = item_start; item < item_last; item++)
+ {
+ bool selected = m_selection.Index( item ) != wxNOT_FOUND;
+ if (selected || item == m_currentRow)
+ {
+ int flags = selected ? (int)wxCONTROL_SELECTED : 0;
+ if (item == m_currentRow)
+ flags |= wxCONTROL_CURRENT;
+ if (m_hasFocus)
+ flags |= wxCONTROL_FOCUSED;
+
+ wxRect rect( x_start, GetLineStart( item ), x_last, GetLineHeight( item ) );
+ wxRendererNative::Get().DrawItemSelectionRect
+ (
+ this,
+ dc,
+ rect,
+ flags
+ );
+ }
+ }
+
+ wxDataViewColumn *expander = GetOwner()->GetExpanderColumn();
+ if (!expander)
+ {
+ // TODO: last column for RTL support
+ expander = GetOwner()->GetColumn( 0 );
+ GetOwner()->SetExpanderColumn(expander);
+ }
+
+ // redraw all cells for all rows which must be repainted and all columns
+ wxRect cell_rect;
+ cell_rect.x = x_start;
+ for (unsigned int i = col_start; i < col_last; i++)
+ {
+
+ wxDataViewColumn *col = GetOwner()->GetColumn( i );
+ wxDataViewRenderer *cell = col->GetRenderer();
+ cell_rect.width = col->GetWidth();
+
+ if (col->IsHidden())
+ continue; // skip it!
+
+ for (unsigned int item = item_start; item < item_last; item++)
+ {
+ // get the cell value and set it into the renderer
+ wxVariant value;
+ wxDataViewTreeNode *node = NULL;
+ wxDataViewItem dataitem;
+
+ if (!IsVirtualList())
+ {
+ node = GetTreeNodeByRow(item);
+ if( node == NULL )
+ continue;
+
+ dataitem = node->GetItem();
+
+ if ((i > 0) && model->IsContainer(dataitem) && !model->HasContainerColumns(dataitem))
+ continue;
+ }
+ else
+ {
+ dataitem = wxDataViewItem( wxUIntToPtr(item) );
+ }
+
+ model->GetValue( value, dataitem, col->GetModelColumn());
+ cell->SetValue( value );
+
+ if (cell->GetWantsAttr())
+ {
+ wxDataViewItemAttr attr;
+ bool ret = model->GetAttr( dataitem, col->GetModelColumn(), attr );
+ if (ret)
+ cell->SetAttr( attr );
+ cell->SetHasAttr( ret );
+ }
+
+ // update cell_rect
+ cell_rect.y = GetLineStart( item );
+ cell_rect.height = GetLineHeight( item ); // -1 is for the horizontal rules (?)
+
+ //Draw the expander here.
+ int indent = 0;
+ if ((!IsVirtualList()) && (col == expander))
+ {
+ indent = node->GetIndentLevel();
+
+ //Calculate the indent first
+ indent = cell_rect.x + GetOwner()->GetIndent() * indent;
+
+ int expander_width = m_lineHeight - 2*EXPANDER_MARGIN;
+ // change the cell_rect.x to the appropriate pos
+ int expander_x = indent + EXPANDER_MARGIN;
+ int expander_y = cell_rect.y + EXPANDER_MARGIN + (GetLineHeight(item) / 2) - (expander_width/2) - EXPANDER_OFFSET;
+ indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space
+ dc.SetPen( m_penExpander );
+ dc.SetBrush( wxNullBrush );
+ if( node->HasChildren() )
+ {
+ wxRect rect( expander_x , expander_y, expander_width, expander_width);
+ int flag = 0;
+ if (m_underMouse == node)
+ {
+ flag |= wxCONTROL_CURRENT;
+ }
+ if( node->IsOpen() )
+ wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag|wxCONTROL_EXPANDED );
+ else
+ wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag);
+ }
+ //force the expander column to left-center align
+ cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
+ }
+ 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)
+ }
+
+ // 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 );
+ // size.y = wxMin( size.y, cell_rect.height );
+ size.y = cell_rect.height;
+
+ wxRect item_rect(cell_rect.GetTopLeft(), size);
+ int align = cell->CalculateAlignment();
+
+ // horizontal alignment:
+ item_rect.x = cell_rect.x;
+ if (align & wxALIGN_CENTER_HORIZONTAL)
+ item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
+ else if (align & wxALIGN_RIGHT)
+ item_rect.x = cell_rect.x + cell_rect.width - size.x;
+ //else: wxALIGN_LEFT is the default
+
+ // vertical alignment:
+ item_rect.y = cell_rect.y;
+ if (align & wxALIGN_CENTER_VERTICAL)
+ item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
+ else if (align & wxALIGN_BOTTOM)
+ item_rect.y = cell_rect.y + cell_rect.height - size.y;
+ //else: wxALIGN_TOP is the default
+
+ // add padding
+ item_rect.x += PADDING_RIGHTLEFT;
+ item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
+
+ //Here we add the tree indent
+ item_rect.x += indent;
+
+ int state = 0;
+ if (m_hasFocus && (m_selection.Index(item) != wxNOT_FOUND))
+ state |= wxDATAVIEW_CELL_SELECTED;
+
+ // TODO: it would be much more efficient to create a clipping
+ // region for the entire column being rendered (in the OnPaint
+ // of wxDataViewMainWindow) instead of a single clip region for
+ // each cell. However it would mean that each renderer should
+ // respect the given wxRect's top & bottom coords, eventually
+ // violating only the left & right coords - however the user can
+ // make its own renderer and thus we cannot be sure of that.
+ dc.SetClippingRegion( item_rect );
+ cell->Render( item_rect, &dc, state );
+ dc.DestroyClippingRegion();
+ }
+
+ cell_rect.x += cell_rect.width;
+ }
+}
+
+void wxDataViewMainWindow::OnRenameTimer()
+{
+ // We have to call this here because changes may just have
+ // been made and no screen update taken place.
+ if ( m_dirty )
+ wxSafeYield();
+
+ int xpos = 0;
+ unsigned int cols = GetOwner()->GetColumnCount();
+ unsigned int i;
+ for (i = 0; i < cols; i++)
+ {
+ wxDataViewColumn *c = GetOwner()->GetColumn( i );
+ if (c->IsHidden())
+ continue; // skip it!
+
+ if (c == m_currentCol)
+ break;
+ xpos += c->GetWidth();
+ }
+ wxRect labelRect( xpos,
+ GetLineStart( m_currentRow ),
+ m_currentCol->GetWidth(),
+ GetLineHeight( m_currentRow ) );
+
+ GetOwner()->CalcScrolledPosition( labelRect.x, labelRect.y,
+ &labelRect.x, &labelRect.y);
+
+ wxDataViewItem item = GetItemByRow( m_currentRow );
+ m_currentCol->GetRenderer()->StartEditing( item, labelRect );
+
+}
+
+//------------------------------------------------------------------
+// Helper class for do operation on the tree node
+//------------------------------------------------------------------
+class DoJob
+{
+public:
+ DoJob(){};
+ virtual ~DoJob(){};
+
+ //The return value control how the tree-walker tranverse the tree
+ // 0: Job done, stop tranverse and return
+ // 1: Ignore the current node's subtree and continue
+ // 2: Job not done, continue
+ enum { OK = 0 , IGR = 1, CONT = 2 };
+ virtual int operator() ( wxDataViewTreeNode * node ) = 0 ;
+ virtual int operator() ( void * n ) = 0;
+};
+
+bool Walker( wxDataViewTreeNode * node, DoJob & func )
+{
+ if( node==NULL )
+ return false;
+
+ switch( func( node ) )
+ {
+ case DoJob::OK :
+ return true ;
+ case DoJob::IGR:
+ return false;
+ case DoJob::CONT:
+ default:
+ ;
+ }
+
+ 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 ++ )
+ {
+ void * n = leaves[i];
+ if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() )
+ {
+ wxDataViewTreeNode * nd = nodes[nodes_i];
+ nodes_i++;
+
+ if( Walker( nd , 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)
+{
+ if (!m_root)
+ {
+ m_count++;
+ UpdateDisplay();
+ return true;
+ }
+
+ SortPrepare();
+
+ 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);
+ 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)
+{
+ if (!m_root)
+ {
+ m_count--;
+ if( m_currentRow > GetRowCount() )
+ m_currentRow = m_count - 1;
+
+ m_selection.Empty();
+
+ UpdateDisplay();
+
+ return true;
+ }
+
+ wxDataViewTreeNode * node = FindNode(parent);
+
+ wxCHECK_MSG( node != NULL, false, "item not found" );
+ wxCHECK_MSG( node->GetChildren().Index( item.GetID() ) != wxNOT_FOUND, false, "item not found" );
+
+ int sub = -1;
+ node->GetChildren().Remove( item.GetID() );
+ //Manuplate 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)
+{
+ 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 col )
+{
+ // 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(col);
+ le.SetDataViewColumn(GetOwner()->GetColumn(col));
+ parent->GetEventHandler()->ProcessEvent(le);
+
+ return true;
+}
+
+bool wxDataViewMainWindow::Cleared()
+{
+ DestroyTree();
+
+ SortPrepare();
+ BuildTree( GetOwner()->GetModel() );
+
+ UpdateDisplay();
+
+ return true;
+}
+
+void wxDataViewMainWindow::UpdateDisplay()
+{
+ m_dirty = true;
+}
+
+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 )
+{
+ wxWindow::ScrollWindow( dx, dy, rect );
+
+ if (GetOwner()->m_headerArea)
+ GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
+}
+
+void wxDataViewMainWindow::ScrollTo( int rows, int column )
+{
+ 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 = 0, x_end = 0, 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()->GetColumn(colnum);
+ if (col->IsHidden())
+ continue; // skip it!
+
+ w = col->GetWidth();
+ x_start += w;
+ }
+
+ 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 =
+ wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( 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 );
+ }
+ }
+}
+
+void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
+{
+ if (from > to)
+ {
+ unsigned int tmp = from;
+ from = to;
+ to = tmp;
+ }
+
+ unsigned int i;
+ for (i = from; i <= to; i++)
+ {
+ if (m_selection.Index( i ) == wxNOT_FOUND)
+ {
+ if (on)
+ m_selection.Add( i );
+ }
+ else
+ {
+ if (!on)
+ m_selection.Remove( i );
+ }
+ }
+ RefreshRows( from, to );
+}
+
+void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
+{
+ for (size_t i=0; i < aSelections.GetCount(); i++)
+ {
+ int n = aSelections[i];
+
+ m_selection.Add( n );
+ RefreshRow( n );
+ }
+}
+
+void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
+{
+ if (m_selection.Index( row ) == wxNOT_FOUND)
+ m_selection.Add( row );
+ else
+ m_selection.Remove( row );
+ RefreshRow( row );
+}
+
+bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
+{
+ return (m_selection.Index( row ) != wxNOT_FOUND);
+}
+
+void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item)
+{
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId());
+
+ le.SetEventObject(parent);
+ le.SetModel(GetOwner()->GetModel());
+ le.SetItem( item );
+
+ parent->GetEventHandler()->ProcessEvent(le);
+}
+
+void wxDataViewMainWindow::RefreshRow( unsigned int row )
+{
+ wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) );
+ m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+
+ wxSize client_size = GetClientSize();
+ wxRect client_rect( 0, 0, client_size.x, client_size.y );
+ wxRect intersect_rect = client_rect.Intersect( rect );
+ if (intersect_rect.width > 0)
+ Refresh( true, &intersect_rect );
+}
+
+void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
+{
+ if (from > to)
+ {
+ unsigned int tmp = to;
+ to = from;
+ from = tmp;
+ }
+
+ wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) );
+ m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+
+ wxSize client_size = GetClientSize();
+ wxRect client_rect( 0, 0, client_size.x, client_size.y );
+ wxRect intersect_rect = client_rect.Intersect( rect );
+ if (intersect_rect.width > 0)
+ Refresh( true, &intersect_rect );
+}
+
+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(),
+ _T("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!
+
+ const wxDataViewRenderer *renderer = column->GetRenderer();
+ wxVariant value;
+ model->GetValue( value, item, column->GetModelColumn() );
+ wxDataViewRenderer *renderer2 = const_cast<wxDataViewRenderer*>(renderer);
+ renderer2->SetValue( value );
+ height = wxMax( height, renderer->GetSize().y );