// the cell padding on the top/bottom
static const int PADDING_TOPBOTTOM = 1;
-
-bool operator == ( const wxDataViewItem & left, const wxDataViewItem & right )
-{
- return left.GetID() == right.GetID();
-}
+// the expander space margin
+static const int EXPANDER_MARGIN = 4;
//-----------------------------------------------------------------------------
// wxDataViewHeaderWindow
open = true;
else
open = false;
+ hasChildren = false;
}
//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; }
+ wxDataViewTreeNodes & GetChildren() { return children; }
void SetChildren( wxDataViewTreeNodes children ) { this->children = children; }
wxDataViewTreeNode * GetChild( unsigned int n ) { return children.Item( n ); }
void SetItem( wxDataViewItem & item ) { this->item = item; }
unsigned int GetChildrenNumber() { return children.GetCount(); }
+ int GetIndentLevel()
+ {
+ int ret = 0 ;
+ wxDataViewTreeNode * node = this;
+ while( node->GetParent()->GetParent() != NULL )
+ {
+ node = node->GetParent();
+ ret ++;
+ }
+ return ret;
+ }
bool IsOpen() { return open; }
- bool HasChildren() { return children.GetCount() != 0; }
+ void ToggleOpen(){ open = !open; }
+ bool HasChildren() { return hasChildren; }
+ void SetHasChildren( bool has ){ hasChildren = has; }
private:
wxDataViewTreeNode * parent;
wxDataViewTreeNodes children;
wxDataViewItem item;
bool open;
+ bool hasChildren;
};
//-----------------------------------------------------------------------------
int GetCountPerPage() const;
int GetEndOfLastCol() const;
unsigned int GetFirstVisibleRow() const;
- unsigned int GetLastVisibleRow() const;
- unsigned int GetRowCount() const;
+ //I change this method to un const because in the tree view, the displaying number of the tree are changing along with the expanding/collapsing of the tree nodes
+ unsigned int GetLastVisibleRow();
+ unsigned int GetRowCount() ;
+
+ wxDataViewItem GetSelection();
void Select( const wxArrayInt& aSelections );
void SelectAllRows( bool on );
//Methods for building the mapping tree
void BuildTree( wxDataViewModel * model );
void DestroyTree();
+private:
+ wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row );
+ //We did not need this temporarily
+ //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
+
+ int RecalculateCount() ;
+
+ void OnExpanding( unsigned int row );
+ void OnCollapsing( unsigned int row );
private:
wxDataViewCtrl *m_owner;
// the pen used to draw horiz/vertical rules
wxPen m_penRule;
+ // the pen used to draw the expander and the lines
+ wxPen m_penExpander;
+
//This is the tree structure of the model
wxDataViewTreeNode * m_root;
- unsigned int m_count;
+ int m_count;
private:
DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
DECLARE_EVENT_TABLE()
// wxDataViewMainWindow
//-----------------------------------------------------------------------------
+//The tree building helper, declared firstly
+void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node);
+
int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
{
if (row1 > row2) return 1;
m_penRule = wxPen(GetRuleColour(), 1, wxSOLID);
+ //Here I compose a pen can draw black lines, maybe there are something system colour to use
+ m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID );
//Some new added code to deal with the tree structure
m_root = new wxDataViewTreeNode( NULL );
- m_count = 0 ;
+ m_root->SetHasChildren(true);
+
+ //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
+ m_count = -1 ;
UpdateDisplay();
}
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 );
}
+//------------------------------------------------------------------
+// Helper class for do operation on the tree node
+//------------------------------------------------------------------
class DoJob
{
public:
DoJob(){};
virtual ~DoJob(){};
- virtual bool operator() ( wxDataViewTreeNode * node ) = 0 ;
+ //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 ;
};
class ItemAddJob: public DoJob
{
public:
- ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item )
- { this->parent = parent ; this->item = item ; }
+ ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item, int * count )
+ { this->parent = parent ; this->item = item ; m_count = count; }
virtual ~ItemAddJob(){};
- virtual bool operator() ( wxDataViewTreeNode * node )
+ virtual int operator() ( wxDataViewTreeNode * node )
{
if( node->GetItem() == parent )
{
+ node->SetHasChildren( true );
wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
- newnode->SetItem(item);
- node->AppendChild( newnode);
- return true;
+ newnode->SetItem(item);
+ node->AppendChild( newnode);
+ *m_count = -1;
+ return OK;
}
- return false;
+ return CONT;
}
+
private:
+ int * m_count;
wxDataViewItem parent, item;
};
bool Walker( wxDataViewTreeNode * node, DoJob & func )
{
- if( !node->HasChildren())
+ if( node==NULL || !node->HasChildren())
return false;
wxDataViewTreeNodes nodes = node->GetChildren();
for( ; i < len ; i ++ )
{
wxDataViewTreeNode * n = nodes[i];
- if( func( n ) )
- return true;
- if( Walker( n , func ) )
- return true;
+ switch( func( n ) )
+ {
+ case DoJob::OK :
+ return true ;
+ case DoJob::IGR:
+ continue;
+ case DoJob::CONT:
+ default:
+ ;
+ }
+
+ if( Walker( n , func ) )
+ return true;
}
return false;
}
-
-
bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
{
- ItemAddJob job( parent, item);
+ ItemAddJob job( parent, item, &m_count);
Walker( m_root , job);
UpdateDisplay();
return true;
}
+class ItemDeleteJob: public DoJob
+{
+public:
+ ItemDeleteJob( const wxDataViewItem & item, int * count ) { m_item = item; m_count = count; }
+ virtual ~ItemDeleteJob(){}
+ virtual int operator() ( wxDataViewTreeNode * node )
+ {
+ if( node->GetItem() == m_item )
+ {
+ node->GetParent()->GetChildren().Remove( node );
+ delete node;
+ *m_count = -1;
+ return DoJob::OK;
+ }
+ return DoJob::CONT;
+ }
+
+private:
+ int * m_count;
+ wxDataViewItem m_item;
+};
+
bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem & item)
{
+ ItemDeleteJob job( item, &m_count);
+ Walker( m_root, job);
UpdateDisplay();
return true;
}
bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
{
- UpdateDisplay();
+ unsigned int row = GetRowByItem(item);
+ RefreshRow( row );
return true;
}
return true;
*/
- UpdateDisplay();
+ unsigned int row = GetRowByItem(item);
+ RefreshRow( row );
return true;
}
bool wxDataViewMainWindow::Cleared()
{
+ DestroyTree();
UpdateDisplay();
return true;
}
unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) );
unsigned int item_count =
wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1),
- (int)(m_count- item_start) );
+ (int)(GetRowCount( )- item_start) );
unsigned int item_last = item_start + item_count;
// compute which columns needs to be redrawn
{
// get the cell value and set it into the renderer
wxVariant value;
- wxDataViewItem dataitem = GetItemByRow(item);
- model->GetValue( value, dataitem, col->GetModelColumn());
+ wxDataViewTreeNode * node = GetTreeNodeByRow(item);
+ if( node == NULL )
+ continue;
+
+ wxDataViewItem dataitem = node->GetItem();
+ model->GetValue( value, dataitem, col->GetModelColumn());
cell->SetValue( value );
// update the y offset
cell_rect.y = item * m_lineHeight;
- // cannot be bigger than allocated space
+ //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
+ int indent = node->GetIndentLevel();
+ if( col->GetModelColumn() == GetOwner()->GetExpanderColumn() )
+ {
+ //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 , expander_y = cell_rect.y + EXPANDER_MARGIN ;
+ indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space
+ dc.SetPen( m_penExpander );
+ dc.SetBrush( wxNullBrush );
+ if( node->HasChildren() )
+ {
+ //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
+ //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
+ wxRect rect( expander_x , expander_y, expander_width, expander_width);
+ if( node->IsOpen() )
+ wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, wxCONTROL_EXPANDED );
+ else
+ wxRendererNative::Get().DrawTreeItemButton( this, dc, rect );
+ }
+ else
+ {
+ // I am wandering whether we should draw dot lines between tree nodes
+ }
+
+ //force the expander column to left-center align
+ cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
+ }
+
+
+ // cannot be bigger than allocated space
wxSize size = cell->GetSize();
- size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width );
+ // 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 + 2*PADDING_TOPBOTTOM, cell_rect.height );
wxRect item_rect(cell_rect.GetTopLeft(), size);
item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
item_rect.height = size.y - 2 * PADDING_TOPBOTTOM;
+ //Here we add the tree indent
+ item_rect.x += indent;
+
int state = 0;
if (m_selection.Index(item) != wxNOT_FOUND)
state |= wxDATAVIEW_CELL_SELECTED;
return y / m_lineHeight;
}
-unsigned int wxDataViewMainWindow::GetLastVisibleRow() const
+unsigned int wxDataViewMainWindow::GetLastVisibleRow()
{
wxSize client_size = GetClientSize();
m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 );
}
-unsigned int wxDataViewMainWindow::GetRowCount() const
+unsigned int wxDataViewMainWindow::GetRowCount()
{
+ if ( m_count == -1 )
+ {
+ m_count = RecalculateCount();
+ int width, height;
+ GetVirtualSize( &width, &height );
+ height = m_count * m_lineHeight;
+
+ SetVirtualSize( width, height );
+ }
return m_count;
- //return wx_const_cast(wxDataViewCtrl*, GetOwner())->GetModel()->GetRowCount();
}
void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
return rect;
}
-/*
-static int tree_walk_current ;
-wxDataViewTreeNode * TreeWalk( unsigned int row , wxDataViewTreeNode * node )
-{
- wxDataViewTreeNode * ret ;
- if( tree_walk_current == row )
- return node;
-
- if( node->HasChildren() && node->IsOpen())
- {
- wxDataViewTreeNodes nodes = node->GetChildren();
- int len = nodes.GetCount();
- int i = 0 ;
- for( ; i < len; i ++)
- {
- tree_walk_current ++;
- ret = TreeWalk( row, nodes[i] );
- if( ret != NULL )
- return ret;
- }
- }
- return NULL;
-}
-*/
-
class RowToItemJob: public DoJob
{
public:
RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;}
virtual ~RowToItemJob(){};
- virtual bool operator() ( wxDataViewTreeNode * node )
+ virtual int operator() ( wxDataViewTreeNode * node )
{
- if( current == row)
+ if( current == static_cast<int>(row))
{
- ret = node->GetItem() ;
- return true;
- }
- current ++;
- return false;
+ ret = node->GetItem() ;
+ return DoJob::OK;
+ }
+ current ++;
+ if ( node->IsOpen())
+ return DoJob::CONT;
+ else
+ return DoJob::IGR;
}
wxDataViewItem GetResult(){ return ret; }
return job.GetResult();
}
-class ItemToRowJob : public DoJob
+class RowToTreeNodeJob: public DoJob
{
public:
- ItemToRowJob(const wxDataViewItem & item){ this->item = item ; }
- virtual ~ItemToRowJob(){};
+ RowToTreeNodeJob( unsigned int row , int current ) { this->row = row; this->current = current ; ret = NULL ; }
+ virtual ~RowToTreeNodeJob(){};
+
+ virtual int operator() ( wxDataViewTreeNode * node )
+ {
+ if( current == static_cast<int>(row))
+ {
+ ret = node ;
+ return DoJob::OK;
+ }
+ current ++;
+ if ( node->IsOpen())
+ return DoJob::CONT;
+ else
+ return DoJob::IGR;
+ }
+
+ wxDataViewTreeNode * GetResult(){ return ret; }
+private:
+ unsigned int row;
+ int current ;
+ wxDataViewTreeNode * ret;
+};
+
+
+wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row)
+{
+ RowToTreeNodeJob job( row , 0 );
+ Walker( m_root , job );
+ return job.GetResult();
+}
- virtual bool operator() ( wxDataViewTreeNode * node)
+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;
+};
+
+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;
+ Refresh();
+ }
+ }
+}
+
+void wxDataViewMainWindow::OnCollapsing(unsigned int row)
+{
+ wxDataViewTreeNode * node = GetTreeNodeByRow(row);
+ if( node != NULL )
+ {
+ if( node->HasChildren() && node->IsOpen() )
{
- ret ++;
- if( node->GetItem() == item )
- return true;
+ node->ToggleOpen();
+ m_count = -1;
+ Refresh();
+ //RefreshRows(row,GetLastVisibleRow());
+ }
+ else
+ {
+ node = node->GetParent();
+ if( node != NULL )
+ {
+ int parent = GetRowByItem( node->GetItem()) ;
+ SelectRow( row, false);
+ SelectRow(parent , true );
+ ChangeCurrentRow( parent );
+ }
+ }
+ }
+}
- return false;
- }
+int wxDataViewMainWindow::RecalculateCount()
+{
+ CountJob job;
+ Walker( m_root, job );
+ return job.GetResult();
+}
+
+class ItemToRowJob : public DoJob
+{
+public:
+ ItemToRowJob(const wxDataViewItem & item){ this->item = item ; ret = 0 ; }
+ virtual ~ItemToRowJob(){};
+
+ virtual int operator() ( wxDataViewTreeNode * node)
+ {
+ ret ++;
+ if( node->GetItem() == item )
+ return DoJob::OK;
+
+ if( node->IsOpen())
+ return DoJob::CONT;
+ else
+ return DoJob::IGR;
+ }
- int GetResult(){ return ret; }
+ //the row number is begin from zero
+ int GetResult(){ return ret -1 ; }
private:
wxDataViewItem item;
int ret;
return job.GetResult();
}
-unsigned int BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node)
+void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node)
{
- int sum = 0 ;
if( !model->HasChildren( item ) )
- return 0;
+ return ;
wxDataViewItem i = model->GetFirstChild( item );
while( i.IsOk() )
{
wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
- n->SetItem(i);
- node->AppendChild(n);
- int num = BuildTreeHelper( model, i, n) + 1;
- sum += num ;
- i = model->GetNextSibling( i );
+ n->SetItem(i);
+ n->SetHasChildren( model->HasChildren( i )) ;
+ node->AppendChild(n);
+ //BuildTreeHelper( model, i, n) ;
+ i = model->GetNextSibling( i );
}
- return sum;
}
void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
{
//First we define a invalid item to fetch the top-level elements
wxDataViewItem item;
- m_count = BuildTreeHelper( model, item, m_root);
+ BuildTreeHelper( model, item, m_root);
+ m_count = -1 ;
}
void DestroyTreeHelper( wxDataViewTreeNode * node )
if( node->HasChildren() )
{
int len = node->GetChildrenNumber();
- int i = 0 ;
- wxDataViewTreeNodes nodes = node->GetChildren();
- for( ; i < len; i ++ )
+ int i = 0 ;
+ wxDataViewTreeNodes nodes = node->GetChildren();
+ for( ; i < len; i ++ )
{
DestroyTreeHelper(nodes[i]);
}
if ( m_currentRow < GetRowCount() - 1 )
OnArrowChar( m_currentRow + 1, event );
break;
-
+ //Add the process for tree expanding/collapsing
+ case WXK_LEFT:
+ OnCollapsing(m_currentRow);
+ break;
+ case WXK_RIGHT:
+ OnExpanding( m_currentRow);
+ break;
case WXK_END:
if (!IsEmpty())
OnArrowChar( GetRowCount() - 1, 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;
}
SelectRow( m_lineSelectSingleOnUp, true );
}
- if (m_lastOnSame)
+ //Process the event of user clicking the expander
+ bool expander = false;
+ wxDataViewTreeNode * node = GetTreeNodeByRow(current);
+ if( node!=NULL && node->HasChildren() )
+ {
+ int indent = node->GetIndentLevel();
+ indent = GetOwner()->GetIndent()*indent;
+ wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN);
+ if( rect.Contains( x, y) )
+ {
+ expander = true;
+ if( node->IsOpen() )
+ OnCollapsing(current);
+ else
+ OnExpanding( current );
+ }
+ }
+
+ //If the user click the expander, we do not do editing even if the column with expander are editable
+ if (m_lastOnSame && !expander )
{
if ((col == m_currentCol) && (current == m_currentRow) &&
(cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
event.Skip();
}
+wxDataViewItem wxDataViewMainWindow::GetSelection()
+{
+ if( m_selection.GetCount() != 1 )
+ return wxDataViewItem();
+ return GetItemByRow( m_selection.Item( 0 ) );
+}
+
//-----------------------------------------------------------------------------
// wxDataViewCtrl
//-----------------------------------------------------------------------------
m_clientArea->UpdateDisplay();
}
+
+void wxDataViewCtrl::DoSetExpanderColumn()
+{
+ m_clientArea->UpdateDisplay();
+}
+
+void wxDataViewCtrl::DoSetIndent()
+{
+ m_clientArea->UpdateDisplay();
+}
+
+wxDataViewItem wxDataViewCtrl::GetSelection()
+{
+ return m_clientArea->GetSelection();
+}
+
/********************************************************************
void wxDataViewCtrl::SetSelection( int row )
{