// Name: dataview.cpp
// Purpose: wxDataViewCtrl wxWidgets sample
// Author: Robert Roebling
-// Modified by: Francesco Montorsi
+// Modified by: Francesco Montorsi, Bo Yang
// Created: 06/01/06
// RCS-ID: $Id$
// Copyright: (c) Robert Roebling
// add to data
MyMusicModelNode *child_node =
new MyMusicModelNode( m_classical, title, artist, year );
+
m_classical->Append( child_node );
if (m_classicalMusicIsKnownToControl)
// notify control
ItemDeleted( parent, item );
+ //We must delete the node after we call ItemDeleted
+ //The reason is that:
+ //When we use wxSortedArray, the array find a node through binary search for speed.
+ //And when the array is searching for some node, it call the model's compare function.
+ //The compare function need the node to be compared. So we should delete the node later, here.
node->GetParent()->GetChildren().Remove( node );
delete node;
}
void OnValueChanged( wxDataViewEvent &event );
void OnItemAdded( wxDataViewEvent &event );
void OnItemDeleted( wxDataViewEvent &event );
+ void OnActivated( wxDataViewEvent &event );
+ void OnHeaderClick( wxDataViewEvent &event );
+ void OnHeaderRightClick( wxDataViewEvent &event );
+ void OnSorted( wxDataViewEvent &event );
+
+ void OnRightClick( wxMouseEvent &event );
+ void OnGoto( wxCommandEvent &event);
private:
wxDataViewCtrl* m_musicCtrl;
wxObjectDataPtr<MyListModel> m_list_model;
wxTextCtrl * m_log;
+ wxLog *m_logOld;
private:
DECLARE_EVENT_TABLE()
ID_DELETE_MUSIC = 101,
ID_PREPEND_LIST = 200,
- ID_DELETE_LIST = 201
+ ID_DELETE_LIST = 201,
+ ID_GOTO = 202
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
+ EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL, MyFrame::OnItemAdded )
EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL, MyFrame::OnItemDeleted )
EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
+ EVT_DATAVIEW_MODEL_ITEM_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
+ EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
+ EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
+ EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
+ EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
+ EVT_RIGHT_UP(MyFrame::OnRightClick)
END_EVENT_TABLE()
MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
// MyMusic
m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
- wxDefaultSize );
+ wxDefaultSize, wxDV_MULTIPLE );
m_music_model = new MyMusicModel;
m_musicCtrl->AssociateModel( m_music_model.get() );
// MyList
m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
- wxDefaultSize );
+ wxDefaultSize, wxDV_MULTIPLE );
m_list_model = new MyListModel;
m_listCtrl->AssociateModel( m_list_model.get() );
button_sizer->Add( 10, 10, 1 );
button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 );
button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
+ button_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
main_sizer->Add( button_sizer, 0, wxGROW, 0 );
m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
-
+ m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
+ wxLogMessage("This is the log window");
+
main_sizer->Add( m_log, 1, wxGROW );
SetSizer( main_sizer );
void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
{
- wxDataViewItem item = m_musicCtrl->GetSelection();
- if (item.IsOk())
- m_music_model->Delete( item );
+ wxDataViewItemArray items;
+ int len = m_musicCtrl->GetSelections( items );
+ for( int i = 0; i < len; i ++ )
+ if (items[i].IsOk())
+ m_music_model->Delete( items[i] );
}
void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
{
- wxDataViewItem item = m_listCtrl->GetSelection();
- if (item.IsOk())
- m_list_model->DeleteItem( item );
+ wxDataViewItemArray items;
+ int len = m_listCtrl->GetSelections( items );
+ for( int i = 0; i < len; i ++ )
+ if (items[i].IsOk())
+ m_list_model->DeleteItem( items[i] );
}
void MyFrame::OnItemAdded( wxDataViewEvent &event )
if (!m_log)
return;
- m_log->AppendText( "EVT_DATAVIEW_MODEL_ITEM_ADDED\n" );
+ wxLogMessage("wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, Item Id: %d",event.GetItem().GetID());
}
void MyFrame::OnItemDeleted( wxDataViewEvent &event )
if (!m_log)
return;
- m_log->AppendText( "EVT_DATAVIEW_MODEL_ITEM_DELETED\n" );
+ wxLogMessage( "EVT_DATAVIEW_MODEL_ITEM_DELETED, Item Id: %d", event.GetItem().GetID() );
}
void MyFrame::OnValueChanged( wxDataViewEvent &event )
if (!m_log)
return;
- m_log->AppendText( "EVT_DATAVIEW_MODEL_VALUE_CHANGED\n" );
+ wxLogMessage( "EVT_DATAVIEW_MODEL_VALUE_CHANGED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn() );
+}
+
+void MyFrame::OnActivated( wxDataViewEvent &event )
+{
+ if(!m_log)
+ return;
+
+ wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn());
+}
+
+void MyFrame::OnHeaderClick( wxDataViewEvent &event )
+{
+ if(!m_log)
+ return;
+
+ wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column: %d", event.GetColumn());
+}
+
+void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
+{
+ if(!m_log)
+ return;
+
+ wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column: %d", event.GetColumn());
+}
+
+void MyFrame::OnSorted( wxDataViewEvent &event )
+{
+ if(!m_log)
+ return;
+
+ wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column: %d", event.GetColumn());
+}
+
+void MyFrame::OnRightClick( wxMouseEvent &event )
+{
+ if(!m_log)
+ return;
+
+ wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event.GetX(), event.GetY());
+}
+
+void MyFrame::OnGoto( wxCommandEvent &event)
+{
+ m_listCtrl->EnsureVisible(50);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
// wxDataViewHeaderWindow
//-----------------------------------------------------------------------------
-#define USE_NATIVE_HEADER_WINDOW 1
+#define USE_NATIVE_HEADER_WINDOW 0
// NB: for some reason, this class must be dllexport'ed or we get warnings from
// MSVC in DLL build
class wxDataViewTreeNode
{
public:
- wxDataViewTreeNode( wxDataViewTreeNode * parent )
+ wxDataViewTreeNode( wxDataViewTreeNode * parent = NULL )
:leaves( wxGenericTreeModelItemCmp ),
nodes(wxGenericTreeModelNodeCmp)
- { this->parent = parent;
+ { this->parent = parent;
if( parent == NULL )
open = true;
- else
- open = false;
+ else
+ open = false;
hasChildren = false;
subTreeCount = 0;
- }
+ }
//I don't know what I need to do in the destructure
~wxDataViewTreeNode()
{
int GetIndentLevel()
{
int ret = 0 ;
- wxDataViewTreeNode * node = this;
- while( node->GetParent()->GetParent() != NULL )
- {
- node = node->GetParent();
- ret ++;
- }
- return ret;
+ wxDataViewTreeNode * node = this;
+ while( node->GetParent()->GetParent() != NULL )
+ {
+ node = node->GetParent();
+ ret ++;
+ }
+ return ret;
}
bool IsOpen()
int len = nds.GetCount();
if(len > 0)
{
- int i;
+ int i;
for(i = 0; i < len; i ++)
nodes.Add(nds[i]);
for(i = 0; i < len; i ++)
virtual ~wxDataViewMainWindow();
// notifications from wxDataViewModel
+ void SendModelEvent( wxEventType type, const wxDataViewItem & item);
bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
bool ItemChanged( const wxDataViewItem &item );
void OnRenameTimer();
void ScrollWindow( int dx, int dy, const wxRect *rect = NULL );
+ void ScrollTo( int rows );
bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
void ChangeCurrentRow( unsigned int row );
unsigned int GetRowCount() ;
wxDataViewItem GetSelection();
-
+ wxDataViewSelection GetSelections(){ return m_selection; }
+ void SetSelections( const wxDataViewSelection & sel ) { m_selection = sel; UpdateDisplay(); }
void Select( const wxArrayInt& aSelections );
void SelectAllRows( bool on );
void SelectRow( unsigned int row, bool on );
//Some useful functions for row and item mapping
wxDataViewItem GetItemByRow( unsigned int row );
- unsigned int GetRowByItem( const wxDataViewItem & item );
+ int GetRowByItem( const wxDataViewItem & item );
//Methods for building the mapping tree
void BuildTree( wxDataViewModel * model );
virtual bool Cleared()
{ return m_mainWindow->Cleared(); }
virtual void Resort()
- { m_mainWindow->Resort(); }
+ { m_mainWindow->Resort(); }
wxDataViewMainWindow *m_mainWindow;
};
hdi.cxy = col->GetWidth();
hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]);
hdi.fmt = HDF_LEFT | HDF_STRING;
+ //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
+
+ //sorting support
+ if(model && model->GetSortingColumn() == i)
+ {
+ //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
+ //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
+ //hdi.fmt |= model->GetSortOrderAscending()? HDF_SORTUP:HDF_SORTDOWN;
+ ;
+ }
// lParam is reserved for application's use:
// we store there the column index to use it later in MSWOnNotify
{
bool order = col->IsSortOrderAscending();
col->SetSortOrder(!order);
- // model->SetSortOrderAscending(!order);
+ model->SetSortOrderAscending(!order);
}
else if(model)
{
model->SetSortingColumn(idx);
- // model->SetSortOrderAscending(true);
+ model->SetSortOrderAscending(col->IsSortOrderAscending());
}
}
UpdateDisplay();
int ch = h;
wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE;
- if (col->IsSortable())
+ if (col->IsSortable() && GetOwner()->GetModel()->GetSortingColumn() == i)
{
if (col->IsSortOrderAscending())
sortArrow = wxHDR_SORT_ICON_UP;
}
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())
+ {
+ unsigned int colnum = model->GetSortingColumn();
+ if(model && static_cast<int>(colnum) == m_column)
+ {
+ bool order = col->IsSortOrderAscending();
+ col->SetSortOrder(!order);
+ model->SetSortOrderAscending(!order);
+ }
+ else if(model)
+ {
+ model->SetSortingColumn(m_column);
+ model->SetSortOrderAscending(col->IsSortOrderAscending());
+ }
+ }
+ UpdateDisplay();
+ if(model)
+ model->Resort();
+ //Send the column sorted event
+ SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, m_column);
+ }
}
}
else if (event.Moving())
virtual int operator() ( void * n ) = 0;
};
-#if 0
-class ItemAddJob: public DoJob
-{
-public:
- ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item, int * count )
- { this->parent = parent ; this->item = item ; m_count = count; }
- virtual ~ItemAddJob(){};
-
- virtual int operator() ( wxDataViewTreeNode * node )
- {
- if( node->GetItem() == parent )
- {
- node->SetHasChildren( true );
- wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
- newnode->SetItem(item);
- node->AppendChild( newnode);
- *m_count = -1;
- return OK;
- }
- return CONT;
- }
-
-private:
- int * m_count;
- wxDataViewItem parent, item;
-};
-#endif
-
bool Walker( wxDataViewTreeNode * node, DoJob & func )
{
if( node==NULL || !node->HasChildren())
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];
wxDataViewTreeNode * nd = nodes[nodes_i];
nodes_i++;
- switch( func( nd ) )
- {
- case DoJob::OK :
- return true ;
- case DoJob::IGR:
- continue;
- case DoJob::CONT:
- default:
- ;
- }
-
if( Walker( nd , func ) )
return true;
return false;
}
+void wxDataViewMainWindow::SendModelEvent( 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);
+}
+
bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
{
g_model = GetOwner()->GetModel();
wxDataViewTreeNode * node;
node = FindNode(parent);
+ SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, item );
if( node == NULL )
return false;
{
wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
newnode->SetItem(item);
+ newnode->SetHasChildren( true );
node->AddNode( newnode);
}
else
m_count = -1;
UpdateDisplay();
+
return true;
}
-#if 0
-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;
-};
-#endif
-
void DestroyTreeHelper( wxDataViewTreeNode * node);
bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
wxDataViewTreeNode * node;
node = FindNode(parent);
+ SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED, item);
if( node == NULL || node->GetChildren().Index( item.GetID() ) == wxNOT_FOUND )
{
bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
{
g_model = GetOwner()->GetModel();
+ g_model->Resort();
+
+ SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED,item);
- unsigned int row = GetRowByItem(item);
- RefreshRow( row );
return true;
}
-bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int WXUNUSED(col) )
+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
return true;
*/
g_model = GetOwner()->GetModel();
+ g_model->Resort();
+
+ //Send event
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_MODEL_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);
- unsigned int row = GetRowByItem(item);
- RefreshRow( row );
return true;
}
DestroyTree();
UpdateDisplay();
+
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED, parent->GetId());
+ le.SetEventObject(parent);
+ le.SetModel(GetOwner()->GetModel());
+ parent->GetEventHandler()->ProcessEvent(le);
+
return true;
}
GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
}
+void wxDataViewMainWindow::ScrollTo( int rows )
+{
+ int x, y;
+ m_owner->GetScrollPixelsPerUnit( &x, &y );
+ int sc = rows*m_lineHeight/y;
+ m_owner->Scroll(0, sc );
+}
+
void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
{
wxDataViewModel *model = GetOwner()->GetModel();
{
// get the cell value and set it into the renderer
wxVariant value;
- wxDataViewTreeNode * node = GetTreeNodeByRow(item);
- if( node == NULL )
- {
- continue;
- }
+ wxDataViewTreeNode * node = GetTreeNodeByRow(item);
+ if( node == NULL )
+ {
+ continue;
+ }
wxDataViewItem dataitem = node->GetItem();
- model->GetValue( value, dataitem, col->GetModelColumn());
+ model->GetValue( value, dataitem, col->GetModelColumn());
cell->SetValue( value );
// update the y offset
{
current ++;
if( current == static_cast<int>(row))
- {
+ {
ret = node->GetItem() ;
return DoJob::OK;
}
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;
}
wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row)
{
- RowToItemJob job( row, -1 );
+ RowToItemJob job( row, -2 );
Walker( m_root , job );
return job.GetResult();
}
{
current ++;
if( current == static_cast<int>(row))
- {
+ {
ret = node ;
return DoJob::OK;
}
else
{
parent = node;
+ //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;
+ 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);
wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row)
{
- RowToTreeNodeJob job( row , -1, m_root );
+ RowToTreeNodeJob job( row , -2, m_root );
Walker( m_root , job );
return job.GetResult();
}
-#if 0
-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;
-};
-#endif
-
void wxDataViewMainWindow::OnExpanding( unsigned int row )
{
wxDataViewTreeNode * node = GetTreeNodeByRow(row);
node = node->GetParent();
if( node != NULL )
{
- int parent = GetRowByItem( node->GetItem()) ;
- SelectRow( row, false);
- SelectRow(parent , true );
- ChangeCurrentRow( parent );
+ int parent = GetRowByItem( node->GetItem() ) ;
+ if( parent >= 0 )
+ {
+ SelectRow( row, false);
+ SelectRow(parent , true );
+ ChangeCurrentRow( parent );
+ }
}
}
if( !nd->HasChildren())
//Find the item along the parent-chain.
//This algorithm is designed to speed up the node-finding method
- bool found = true;
wxDataViewTreeNode * node = m_root;
for( ItemList::Node * n = list.GetFirst(); n; n = n->GetNext() )
{
if( node->GetChildrenNumber() == 0 )
BuildTreeHelper(model, node->GetItem(), node);
- int len = node->GetNodeNumber();
wxDataViewTreeNodes nodes = node->GetNodes();
- int j = 0;
- for( ; j < len; j ++)
- {
- if( nodes[j]->GetItem() == *(n->GetData()))
- {
- node = nodes[j];
- break;
- }
- }
- // Whenever we can't find the node in any level, return NULL to indicate the item can't be found
- if( j == len )
- {
- found = false;
+ //The wxSortedArray search a node in binary search, so using Item() is more efficient
+ wxDataViewTreeNode temp;
+ temp.SetItem(*(n->GetData()));
+ int index = nodes.Index( &temp );
+ if( index == wxNOT_FOUND )
return NULL;
- }
+ node = nodes[index];
}
else
return NULL;
{
public:
ItemToRowJob(const wxDataViewItem & item, ItemList::Node * node )
- { this->item = item ; ret = 0 ; nd = node ; }
+ { this->item = item ; ret = -1 ; nd = node ; }
virtual ~ItemToRowJob(){};
+ //Maybe binary search will help to speed up this process
virtual int operator() ( wxDataViewTreeNode * node)
{
ret ++;
};
-unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item)
+int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item)
{
wxDataViewModel * model = GetOwner()->GetModel();
if( model == NULL )
return 0;
+ if( !item.IsOk() )
+ return -1;
+
//Compose the a parent-chain of the finding item
ItemList list;
+ wxDataViewItem * pItem = NULL;
list.DeleteContents( true );
wxDataViewItem it( item );
while( it.IsOk() )
{
- wxDataViewItem * pItem = new wxDataViewItem( it );
+ pItem = new wxDataViewItem( it );
list.Insert( pItem );
it = model->GetParent( it );
}
+ pItem = new wxDataViewItem( );
+ list.Insert( pItem );
ItemToRowJob job( item, list.GetFirst() );
Walker(m_root , job );
break;
//Add the process for tree expanding/collapsing
case WXK_LEFT:
- OnCollapsing(m_currentRow);
- break;
- case WXK_RIGHT:
- OnExpanding( m_currentRow);
- break;
+ OnCollapsing(m_currentRow);
+ break;
+ case WXK_RIGHT:
+ OnExpanding( m_currentRow);
+ break;
case WXK_END:
if (!IsEmpty())
OnArrowChar( GetRowCount() - 1, event );
wxRect cell_rect( xpos, current * m_lineHeight,
col->GetWidth(), m_lineHeight );
cell->Activate( cell_rect, model, item, col->GetModelColumn() );
+
+ wxWindow *parent = GetParent();
+ wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
+
+ le.SetEventObject(parent);
+ le.SetColumn(col->GetModelColumn());
+ le.SetDataViewColumn(col);
+ le.SetModel(GetOwner()->GetModel());
+
+ parent->GetEventHandler()->ProcessEvent(le);
}
return;
}
m_clientArea->UpdateDisplay();
}
-wxDataViewItem wxDataViewCtrl::GetSelection()
+//Selection code with wxDataViewItem as parameters
+int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
+{
+ sel.Empty();
+ wxDataViewSelection selection = m_clientArea->GetSelections();
+ int len = selection.GetCount();
+ for( int i = 0; i < len; i ++)
+ {
+ unsigned int row = selection[i];
+ sel.Add( m_clientArea->GetItemByRow( row ) );
+ }
+ return len;
+}
+
+void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
+{
+ wxDataViewSelection selection(wxDataViewSelectionCmp) ;
+ int len = sel.GetCount();
+ for( int i = 0; i < len; i ++ )
+ {
+ int row = m_clientArea->GetRowByItem( sel[i] );
+ if( row >= 0 )
+ selection.Add( static_cast<unsigned int>(row) );
+ }
+ m_clientArea->SetSelections( selection );
+}
+
+void wxDataViewCtrl::Select( const wxDataViewItem & item )
{
- return m_clientArea->GetSelection();
+ int row = m_clientArea->GetRowByItem( item );
+ if( row >= 0 )
+ m_clientArea->SelectRow(row, true);
}
-/********************************************************************
-void wxDataViewCtrl::SetSelection( int row )
+void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
{
- m_clientArea->SelectRow(row, true);
+ int row = m_clientArea->GetRowByItem( item );
+ if( row >= 0 )
+ m_clientArea->SelectRow(row, false);
}
-void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
+bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
{
- m_clientArea->SelectRows(from, to, true);
+ int row = m_clientArea->GetRowByItem( item );
+ if( row >= 0 )
+ {
+ return m_clientArea->IsRowSelected(row);
+ }
+ return false;
}
-void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
+//Selection code with row number as parameter
+int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
{
- m_clientArea->Select(aSelections);
+ sel.Empty();
+ wxDataViewSelection selection = m_clientArea->GetSelections();
+ int len = selection.GetCount();
+ for( int i = 0; i < len; i ++)
+ {
+ unsigned int row = selection[i];
+ sel.Add( row );
+ }
+ return len;
}
-void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
+void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
{
- // FIXME - TODO
+ wxDataViewSelection selection(wxDataViewSelectionCmp) ;
+ int len = sel.GetCount();
+ for( int i = 0; i < len; i ++ )
+ {
+ int row = sel[i];
+ if( row >= 0 )
+ selection.Add( static_cast<unsigned int>(row) );
+ }
+ m_clientArea->SetSelections( selection );
}
-bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
+void wxDataViewCtrl::Select( int row )
{
- // FIXME - TODO
+ if( row >= 0 )
+ m_clientArea->SelectRow( row, true );
+}
+void wxDataViewCtrl::Unselect( int row )
+{
+ if( row >= 0 )
+ m_clientArea->SelectRow(row, false);
+}
+
+bool wxDataViewCtrl::IsSelected( int row ) const
+{
+ if( row >= 0 )
+ return m_clientArea->IsRowSelected(row);
return false;
}
-int wxDataViewCtrl::GetSelection() const
+void wxDataViewCtrl::SelectRange( int from, int to )
{
- // FIXME - TODO
+ wxArrayInt sel;
+ for( int i = from; i < to; i ++ )
+ sel.Add( i );
+ m_clientArea->Select(sel);
+}
- return -1;
+void wxDataViewCtrl::UnselectRange( int from, int to )
+{
+ wxDataViewSelection sel = m_clientArea->GetSelections();
+ for( int i = from; i < to; i ++ )
+ if( sel.Index( i ) != wxNOT_FOUND )
+ sel.Remove( i );
+ m_clientArea->SetSelections(sel);
}
-int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
+void wxDataViewCtrl::SelectAll()
{
- // FIXME - TODO
+ m_clientArea->SelectAllRows(true);
+}
- return 0;
+void wxDataViewCtrl::UnselectAll()
+{
+ m_clientArea->SelectAllRows(false);
}
-*********************************************************************/
-#endif
+
+void wxDataViewCtrl::EnsureVisible( int row )
+{
+ m_clientArea->ScrollTo( row );
+}
+
+void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item )
+{
+ int row = m_clientArea->GetRowByItem(item);
+ if( row >= 0 )
+ EnsureVisible(row);
+}
+
+wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
+{
+ return m_clientArea->GetItemByRow( row );
+}
+
+int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
+{
+ return m_clientArea->GetRowByItem( item );
+}
+
+ #endif
// !wxUSE_GENERICDATAVIEWCTRL
#endif