Patch from Bo for generic wxDataViewCtrl

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47492 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2007-07-16 10:37:04 +00:00
parent 1c5eeec392
commit 704c349086
2 changed files with 80 additions and 29 deletions

View File

@@ -52,26 +52,26 @@ public:
virtual bool Activate( wxRect WXUNUSED(cell), virtual bool Activate( wxRect WXUNUSED(cell),
wxDataViewModel *WXUNUSED(model), wxDataViewModel *WXUNUSED(model),
const wxDataViewItem & item, const wxDataViewItem & WXUNUSED(item),
unsigned int WXUNUSED(col) ) unsigned int WXUNUSED(col) )
{ return false; } { return false; }
virtual bool LeftClick( wxPoint WXUNUSED(cursor), virtual bool LeftClick( wxPoint WXUNUSED(cursor),
wxRect WXUNUSED(cell), wxRect WXUNUSED(cell),
wxDataViewModel *WXUNUSED(model), wxDataViewModel *WXUNUSED(model),
const wxDataViewItem & item, const wxDataViewItem & WXUNUSED(item),
unsigned int WXUNUSED(col) ) unsigned int WXUNUSED(col) )
{ return false; } { return false; }
virtual bool RightClick( wxPoint WXUNUSED(cursor), virtual bool RightClick( wxPoint WXUNUSED(cursor),
wxRect WXUNUSED(cell), wxRect WXUNUSED(cell),
wxDataViewModel *WXUNUSED(model), wxDataViewModel *WXUNUSED(model),
const wxDataViewItem & item, const wxDataViewItem & WXUNUSED(item),
unsigned int WXUNUSED(col) ) unsigned int WXUNUSED(col) )
{ return false; } { return false; }
virtual bool StartDrag( wxPoint WXUNUSED(cursor), virtual bool StartDrag( wxPoint WXUNUSED(cursor),
wxRect WXUNUSED(cell), wxRect WXUNUSED(cell),
wxDataViewModel *WXUNUSED(model), wxDataViewModel *WXUNUSED(model),
const wxDataViewItem & item, const wxDataViewItem & WXUNUSED(item),
unsigned int WXUNUSED(col) ) unsigned int WXUNUSED(col) )
{ return false; } { return false; }
@@ -352,6 +352,8 @@ public:
virtual void DoSetExpanderColumn(); virtual void DoSetExpanderColumn();
virtual void DoSetIndent(); virtual void DoSetIndent();
virtual wxDataViewItem GetSelection() ;
/********************selection code********************* /********************selection code*********************
virtual void SetSelection( int row ); // -1 for unselect virtual void SetSelection( int row ); // -1 for unselect
virtual void SetSelectionRange( unsigned int from, unsigned int to ); virtual void SetSelectionRange( unsigned int from, unsigned int to );

View File

@@ -263,6 +263,7 @@ public:
open = true; open = true;
else else
open = false; open = false;
hasChildren = false;
} }
//I don't know what I need to do in the destructure //I don't know what I need to do in the destructure
~wxDataViewTreeNode() ~wxDataViewTreeNode()
@@ -270,7 +271,7 @@ public:
wxDataViewTreeNode * GetParent() { return parent; } wxDataViewTreeNode * GetParent() { return parent; }
void SetParent( wxDataViewTreeNode * parent ) { this->parent = parent; } void SetParent( wxDataViewTreeNode * parent ) { this->parent = parent; }
wxDataViewTreeNodes GetChildren() { return children; } wxDataViewTreeNodes & GetChildren() { return children; }
void SetChildren( wxDataViewTreeNodes children ) { this->children = children; } void SetChildren( wxDataViewTreeNodes children ) { this->children = children; }
wxDataViewTreeNode * GetChild( unsigned int n ) { return children.Item( n ); } wxDataViewTreeNode * GetChild( unsigned int n ) { return children.Item( n ); }
@@ -295,12 +296,14 @@ public:
bool IsOpen() { return open; } bool IsOpen() { return open; }
void ToggleOpen(){ open = !open; } void ToggleOpen(){ open = !open; }
bool HasChildren() { return children.GetCount() != 0; } bool HasChildren() { return hasChildren; }
void SetHasChildren( bool has ){ hasChildren = has; }
private: private:
wxDataViewTreeNode * parent; wxDataViewTreeNode * parent;
wxDataViewTreeNodes children; wxDataViewTreeNodes children;
wxDataViewItem item; wxDataViewItem item;
bool open; bool open;
bool hasChildren;
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -359,6 +362,8 @@ public:
unsigned int GetLastVisibleRow(); unsigned int GetLastVisibleRow();
unsigned int GetRowCount() ; unsigned int GetRowCount() ;
wxDataViewItem GetSelection();
void Select( const wxArrayInt& aSelections ); void Select( const wxArrayInt& aSelections );
void SelectAllRows( bool on ); void SelectAllRows( bool on );
void SelectRow( unsigned int row, bool on ); void SelectRow( unsigned int row, bool on );
@@ -388,7 +393,8 @@ public:
void DestroyTree(); void DestroyTree();
private: private:
wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ); wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row );
wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item ){}; //We did not need this temporarily
//wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
int RecalculateCount() ; int RecalculateCount() ;
@@ -1586,6 +1592,9 @@ void wxDataViewRenameTimer::Notify()
// wxDataViewMainWindow // wxDataViewMainWindow
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
//The tree building helper, declared firstly
void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node);
int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 ) int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
{ {
if (row1 > row2) return 1; if (row1 > row2) return 1;
@@ -1645,6 +1654,8 @@ wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID i
m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID ); m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID );
//Some new added code to deal with the tree structure //Some new added code to deal with the tree structure
m_root = new wxDataViewTreeNode( NULL ); m_root = new wxDataViewTreeNode( NULL );
m_root->SetHasChildren(true);
//Make m_count = -1 will cause the class recaculate the real displaying number of rows. //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
m_count = -1 ; m_count = -1 ;
UpdateDisplay(); UpdateDisplay();
@@ -1705,22 +1716,26 @@ public:
class ItemAddJob: public DoJob class ItemAddJob: public DoJob
{ {
public: public:
ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item ) ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item, int * count )
{ this->parent = parent ; this->item = item ; } { this->parent = parent ; this->item = item ; m_count = count; }
virtual ~ItemAddJob(){}; virtual ~ItemAddJob(){};
virtual int operator() ( wxDataViewTreeNode * node ) virtual int operator() ( wxDataViewTreeNode * node )
{ {
if( node->GetItem() == parent ) if( node->GetItem() == parent )
{ {
node->SetHasChildren( true );
wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
newnode->SetItem(item); newnode->SetItem(item);
node->AppendChild( newnode); node->AppendChild( newnode);
*m_count = -1;
return OK; return OK;
} }
return CONT; return CONT;
} }
private: private:
int * m_count;
wxDataViewItem parent, item; wxDataViewItem parent, item;
}; };
@@ -1752,25 +1767,48 @@ bool Walker( wxDataViewTreeNode * node, DoJob & func )
return false; return false;
} }
bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
{ {
ItemAddJob job( parent, item); ItemAddJob job( parent, item, &m_count);
Walker( m_root , job); Walker( m_root , job);
UpdateDisplay(); UpdateDisplay();
return true; 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) bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem & item)
{ {
ItemDeleteJob job( item, &m_count);
Walker( m_root, job);
UpdateDisplay(); UpdateDisplay();
return true; return true;
} }
bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
{ {
UpdateDisplay(); unsigned int row = GetRowByItem(item);
RefreshRow( row );
return true; return true;
} }
@@ -1785,12 +1823,14 @@ bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned i
return true; return true;
*/ */
UpdateDisplay(); unsigned int row = GetRowByItem(item);
RefreshRow( row );
return true; return true;
} }
bool wxDataViewMainWindow::Cleared() bool wxDataViewMainWindow::Cleared()
{ {
DestroyTree();
UpdateDisplay(); UpdateDisplay();
return true; return true;
} }
@@ -2336,7 +2376,7 @@ public:
virtual int operator() ( wxDataViewTreeNode * node ) virtual int operator() ( wxDataViewTreeNode * node )
{ {
if( current == row) if( current == static_cast<int>(row))
{ {
ret = node->GetItem() ; ret = node->GetItem() ;
return DoJob::OK; return DoJob::OK;
@@ -2370,7 +2410,7 @@ public:
virtual int operator() ( wxDataViewTreeNode * node ) virtual int operator() ( wxDataViewTreeNode * node )
{ {
if( current == row) if( current == static_cast<int>(row))
{ {
ret = node ; ret = node ;
return DoJob::OK; return DoJob::OK;
@@ -2429,9 +2469,11 @@ void wxDataViewMainWindow::OnExpanding( unsigned int row )
if( !node->IsOpen()) if( !node->IsOpen())
{ {
node->ToggleOpen(); node->ToggleOpen();
//Here I build the children of current node
if( node->GetChildrenNumber() == 0 )
BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
m_count = -1; m_count = -1;
Refresh(); Refresh();
//RefreshRows(row,GetLastVisibleRow());
} }
} }
} }
@@ -2501,23 +2543,21 @@ unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item)
return job.GetResult(); 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 ) ) if( !model->HasChildren( item ) )
return 0; return ;
wxDataViewItem i = model->GetFirstChild( item ); wxDataViewItem i = model->GetFirstChild( item );
while( i.IsOk() ) while( i.IsOk() )
{ {
wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
n->SetItem(i); n->SetItem(i);
n->SetHasChildren( model->HasChildren( i )) ;
node->AppendChild(n); node->AppendChild(n);
int num = BuildTreeHelper( model, i, n) + 1; //BuildTreeHelper( model, i, n) ;
sum += num ;
i = model->GetNextSibling( i ); i = model->GetNextSibling( i );
} }
return sum;
} }
void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
@@ -2752,12 +2792,10 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
if( rect.Contains( x, y) ) if( rect.Contains( x, y) )
{ {
expander = true; expander = true;
node->ToggleOpen(); if( node->IsOpen() )
m_count = -1; //make the current row number fail OnCollapsing(current);
else
Refresh(); OnExpanding( current );
//int last_row = GetLastVisibleRow();
//RefreshRows( current, last_row );
} }
} }
@@ -2900,6 +2938,13 @@ void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
event.Skip(); event.Skip();
} }
wxDataViewItem wxDataViewMainWindow::GetSelection()
{
if( m_selection.GetCount() != 1 )
return wxDataViewItem();
return GetItemByRow( m_selection.Item( 0 ) );
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxDataViewCtrl // wxDataViewCtrl
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -3029,6 +3074,10 @@ void wxDataViewCtrl::DoSetIndent()
m_clientArea->UpdateDisplay(); m_clientArea->UpdateDisplay();
} }
wxDataViewItem wxDataViewCtrl::GetSelection()
{
return m_clientArea->GetSelection();
}
/******************************************************************** /********************************************************************
void wxDataViewCtrl::SetSelection( int row ) void wxDataViewCtrl::SetSelection( int row )