1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/datetime.h"
24 #include "wx/splitter.h"
25 #include "wx/aboutdlg.h"
26 #include "wx/choicdlg.h"
27 #include "wx/numdlg.h"
28 #include "wx/dataview.h"
29 #include "wx/spinctrl.h"
33 #include "../sample.xpm"
39 static const char *small1_xpm
[] = {
40 /* columns rows colors chars-per-pixel */
68 #define DEFAULT_ALIGN wxALIGN_LEFT
69 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
71 // -------------------------------------
73 // -------------------------------------
76 Implement this data model
78 -------------------------------------------------------------
81 3: You are not alone Michael Jackson 1995
82 4: Take a bow Madonna 1994
84 6: Ninth Symphony Ludwig v. Beethoven 1824
85 7: German Requiem Johannes Brahms 1868
90 class MyMusicModelNode
;
91 WX_DEFINE_ARRAY_PTR( MyMusicModelNode
*, MyMusicModelNodes
);
93 class MyMusicModelNode
96 MyMusicModelNode( MyMusicModelNode
* parent
,
97 const wxString
&title
, const wxString
&artist
, int year
)
103 m_isContainer
= false;
106 MyMusicModelNode( MyMusicModelNode
* parent
,
107 const wxString
&branch
)
112 m_isContainer
= true;
117 size_t count
= m_children
.GetCount();
119 for (i
= 0; i
< count
; i
++)
121 MyMusicModelNode
*child
= m_children
[i
];
126 bool IsContainer() { return m_isContainer
; }
128 MyMusicModelNode
* GetParent() { return m_parent
; }
129 MyMusicModelNodes
&GetChildren() { return m_children
; }
130 MyMusicModelNode
* GetNthChild( unsigned int n
) { return m_children
.Item( n
); }
131 void Insert( MyMusicModelNode
* child
, unsigned int n
) { m_children
.Insert( child
, n
); }
132 void Append( MyMusicModelNode
* child
) { m_children
.Add( child
); }
133 unsigned int GetChildCount() { return m_children
.GetCount(); }
141 MyMusicModelNode
*m_parent
;
142 MyMusicModelNodes m_children
;
147 class MyMusicModel
: public wxDataViewModel
155 m_root
= new MyMusicModelNode( NULL
, wxT("My Music" ));
156 m_pop
= new MyMusicModelNode( m_root
, wxT("Pop music") );
157 m_root
->Append( m_pop
);
158 m_pop
->Append( new MyMusicModelNode( m_pop
,
159 wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
160 m_pop
->Append( new MyMusicModelNode( m_pop
,
161 wxT("Take a bow"), wxT("Madonna"), 1994 ) );
162 m_classical
= new MyMusicModelNode( m_root
, wxT("Classical music") );
163 m_root
->Append( m_classical
);
164 m_classical
->Append( new MyMusicModelNode( m_classical
,
165 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 ) );
166 m_classical
->Append( new MyMusicModelNode( m_classical
,
167 wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
168 m_classicalMusicIsKnownToControl
= false;
176 // helper method for wxLog
178 wxString
GetTitle( const wxDataViewItem
&item
)
180 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
182 return wxEmptyString
;
184 return node
->m_title
;
187 // helper methods to change the model
189 void AddToClassical( const wxString
&title
, const wxString
&artist
, int year
)
192 MyMusicModelNode
*child_node
=
193 new MyMusicModelNode( m_classical
, title
, artist
, year
);
195 m_classical
->Append( child_node
);
197 if (m_classicalMusicIsKnownToControl
)
200 wxDataViewItem
child( (void*) child_node
);
201 wxDataViewItem
parent( (void*) m_classical
);
202 ItemAdded( parent
, child
);
206 void Delete( const wxDataViewItem
&item
)
208 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
209 wxDataViewItem
parent( node
->GetParent() );
211 node
->GetParent()->GetChildren().Remove( node
);
215 ItemDeleted( parent
, item
);
218 // override sorting to always sort branches ascendingly
220 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
221 unsigned int column
, bool ascending
)
223 if (IsContainer(item1
) && IsContainer(item2
))
225 wxVariant value1
,value2
;
226 GetValue( value1
, item1
, 0 );
227 GetValue( value2
, item2
, 0 );
229 wxString str1
= value1
.GetString();
230 wxString str2
= value2
.GetString();
231 int res
= str1
.Cmp( str2
);
234 // items must be different
235 wxUIntPtr litem1
= (wxUIntPtr
) item1
.GetID();
236 wxUIntPtr litem2
= (wxUIntPtr
) item2
.GetID();
238 return litem1
-litem2
;
241 return wxDataViewModel
::Compare( item1
, item2
, column
, ascending
);
244 // implementation of base class virtuals to define model
246 virtual unsigned int GetColumnCount() const
251 virtual wxString
GetColumnType( unsigned int col
) const
256 return wxT("string");
259 virtual void GetValue( wxVariant
&variant
,
260 const wxDataViewItem
&item
, unsigned int col
) const
262 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
265 case 0: variant
= node
->m_title
; break;
266 case 1: variant
= node
->m_artist
; break;
267 case 2: variant
= (long) node
->m_year
; break;
268 case 3: if (IsContainer(item
)) variant
= (long) 0; else variant
= (long) 80; break; // popularity
271 wxLogError( wxT("MyMusicModel::GetValue: wrong column %d"), col
);
273 // provoke a crash when mouse button down
274 wxMouseState state
= wxGetMouseState();
275 if (state
.ShiftDown())
284 virtual bool SetValue( const wxVariant
&variant
,
285 const wxDataViewItem
&item
, unsigned int col
)
287 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
290 case 0: node
->m_title
= variant
.GetString(); return true;
291 case 1: node
->m_artist
= variant
.GetString(); return true;
292 case 2: node
->m_year
= variant
.GetLong(); return true;
293 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
298 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
300 // the invisble root node has no parent
302 return wxDataViewItem(0);
304 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
306 // "MyMusic" also has no parent
308 return wxDataViewItem(0);
310 return wxDataViewItem( (void*) node
->GetParent() );
313 virtual bool IsContainer( const wxDataViewItem
&item
) const
315 // the invisble root node can have children (in
316 // our model always "MyMusic")
320 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
321 return node
->IsContainer();
324 virtual unsigned int GetChildren( const wxDataViewItem
&parent
, wxDataViewItemArray
&array
) const
326 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
329 array
.Add( wxDataViewItem( (void*) m_root
) );
333 if (node
== m_classical
)
335 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
336 model
->m_classicalMusicIsKnownToControl
= true;
339 if (node
->GetChildCount() == 0)
344 unsigned int count
= node
->GetChildren().GetCount();
346 for (pos
= 0; pos
< count
; pos
++)
348 MyMusicModelNode
*child
= node
->GetChildren().Item( pos
);
349 array
.Add( wxDataViewItem( (void*) child
) );
356 virtual bool IsDraggable( const wxDataViewItem
&item
)
359 return (!IsContainer(item
));
362 virtual size_t GetDragDataSize( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
) )
364 wxPrintf( "GetDragDataSize\n" );
366 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
368 data
+= node
->m_title
; data
+= wxT(" ");
369 data
+= node
->m_artist
;
370 return strlen( data
.utf8_str() ) + 1;
372 virtual bool GetDragData( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
),
373 void* dest
, size_t WXUNUSED(size
) )
375 wxPrintf( "GetDragData\n" );
377 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
379 data
+= node
->m_title
; data
+= wxT(" ");
380 data
+= node
->m_artist
;
381 wxCharBuffer
buffer( data
.utf8_str() );
382 memcpy( dest
, buffer
, strlen(buffer
)+1 );
387 MyMusicModelNode
* m_root
;
388 MyMusicModelNode
* m_pop
;
389 MyMusicModelNode
* m_classical
;
390 bool m_classicalMusicIsKnownToControl
;
394 static int my_sort_reverse( int *v1
, int *v2
)
399 static int my_sort( int *v1
, int *v2
)
404 class MyListModel
: public wxDataViewVirtualListModel
409 wxDataViewVirtualListModel( 1000 + 100 )
411 wxDataViewVirtualListModel( 100000 + 100 )
415 m_virtualItems
= 1000;
417 m_virtualItems
= 100000;
421 for (i
= 0; i
< 100; i
++)
424 str
.Printf( wxT("row number %d"), i
);
428 m_icon
= wxIcon( null_xpm
);
431 // helper methods to change the model
433 void Prepend( const wxString
&text
)
435 m_array
.Insert( text
, 0 );
439 void DeleteItem( const wxDataViewItem
&item
)
441 unsigned int row
= GetRow( item
);
442 if (row
>= m_array
.GetCount())
445 m_array
.RemoveAt( row
);
449 void DeleteItems( const wxDataViewItemArray
&items
)
453 for (i
= 0; i
< items
.GetCount(); i
++)
455 unsigned int row
= GetRow( items
[i
] );
456 if (row
< m_array
.GetCount())
460 // Sort in descending order so that the last
461 // row will be deleted first. Otherwise the
462 // remaining indeces would all be wrong.
463 rows
.Sort( my_sort_reverse
);
464 for (i
= 0; i
< rows
.GetCount(); i
++)
465 m_array
.RemoveAt( rows
[i
] );
467 // This is just to test if wxDataViewCtrl can
468 // cope with removing rows not sorted in
470 rows
.Sort( my_sort
);
476 m_virtualItems
+= 1000;
477 Reset( m_array
.GetCount() + m_virtualItems
);
480 // implementation of base class virtuals to define model
482 virtual unsigned int GetColumnCount() const
487 virtual wxString
GetColumnType( unsigned int col
) const
490 return wxT("wxDataViewIconText");
492 return wxT("string");
495 virtual unsigned int GetRowCount()
497 return m_array
.GetCount();
500 virtual void GetValue( wxVariant
&variant
,
501 unsigned int row
, unsigned int col
) const
505 if (row
>= m_array
.GetCount())
508 str
.Printf(wxT("row %d"), row
- m_array
.GetCount() );
513 variant
= m_array
[ row
];
518 wxDataViewIconText
data( wxT("test"), m_icon
);
523 if (row
>= m_array
.GetCount())
524 variant
= wxT("plain");
526 variant
= wxT("blue");
530 virtual bool GetAttr( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
)
535 if (row
< m_array
.GetCount())
537 attr
.SetColour( *wxBLUE
);
538 attr
.SetItalic( true );
544 virtual bool SetValue( const wxVariant
&variant
,
545 unsigned int row
, unsigned int col
)
549 if (row
>= m_array
.GetCount())
552 m_array
[row
] = variant
.GetString();
559 wxArrayString m_array
;
564 // -------------------------------------
566 // -------------------------------------
568 class MyCustomRenderer
: public wxDataViewCustomRenderer
571 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
572 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
) { }
574 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
576 dc
->SetBrush( *wxRED_BRUSH
);
577 dc
->SetPen( *wxTRANSPARENT_PEN
);
578 dc
->DrawRectangle( rect
);
583 virtual bool Activate( wxRect
WXUNUSED(cell
),
584 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
586 wxLogMessage( wxT("MyCustomRenderer Activate()") );
590 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
591 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
593 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor
.x
, cursor
.y
);
597 virtual wxSize
GetSize() const
599 return wxSize(60,30);
602 virtual bool SetValue( const wxVariant
&WXUNUSED(value
) ) { return true; }
603 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
606 // -------------------------------------
608 // -------------------------------------
610 class MyApp
: public wxApp
617 // -------------------------------------
619 // -------------------------------------
621 class MyFrame
: public wxFrame
624 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
628 void OnQuit(wxCommandEvent
& event
);
629 void OnAbout(wxCommandEvent
& event
);
631 void OnAddMozart(wxCommandEvent
& event
);
632 void OnDeleteMusic(wxCommandEvent
& event
);
633 void OnDeleteYear(wxCommandEvent
& event
);
635 void OnPrependList(wxCommandEvent
& event
);
636 void OnDeleteList(wxCommandEvent
& event
);
638 void OnValueChanged( wxDataViewEvent
&event
);
640 void OnActivated( wxDataViewEvent
&event
);
641 void OnExpanding( wxDataViewEvent
&event
);
642 void OnExpanded( wxDataViewEvent
&event
);
643 void OnCollapsing( wxDataViewEvent
&event
);
644 void OnCollapsed( wxDataViewEvent
&event
);
645 void OnSelectionChanged( wxDataViewEvent
&event
);
647 void OnEditingStarted( wxDataViewEvent
&event
);
648 void OnEditingDone( wxDataViewEvent
&event
);
650 void OnHeaderClick( wxDataViewEvent
&event
);
651 void OnHeaderRightClick( wxDataViewEvent
&event
);
652 void OnSorted( wxDataViewEvent
&event
);
654 void OnContextMenu( wxDataViewEvent
&event
);
656 void OnRightClick( wxMouseEvent
&event
);
657 void OnGoto( wxCommandEvent
&event
);
658 void OnAddMany( wxCommandEvent
&event
);
661 wxDataViewCtrl
* m_musicCtrl
;
662 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
664 wxDataViewCtrl
* m_listCtrl
;
665 wxObjectDataPtr
<MyListModel
> m_list_model
;
667 wxDataViewColumn
* m_col
;
673 DECLARE_EVENT_TABLE()
676 // -------------------------------------
678 // -------------------------------------
682 bool MyApp
::OnInit(void)
684 if ( !wxApp
::OnInit() )
687 // build the first frame
689 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
702 // -------------------------------------
704 // -------------------------------------
709 ID_ABOUT
= wxID_ABOUT
,
715 ID_DELETE_MUSIC
= 101,
716 ID_DELETE_YEAR
= 102,
718 ID_PREPEND_LIST
= 200,
719 ID_DELETE_LIST
= 201,
724 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
725 EVT_MENU( ID_ABOUT
, MyFrame
::OnAbout
)
726 EVT_MENU( ID_EXIT
, MyFrame
::OnQuit
)
727 EVT_BUTTON( ID_ADD_MOZART
, MyFrame
::OnAddMozart
)
728 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame
::OnDeleteMusic
)
729 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame
::OnDeleteYear
)
730 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame
::OnPrependList
)
731 EVT_BUTTON( ID_DELETE_LIST
, MyFrame
::OnDeleteList
)
732 EVT_BUTTON( ID_GOTO
, MyFrame
::OnGoto
)
733 EVT_BUTTON( ID_ADD_MANY
, MyFrame
::OnAddMany
)
735 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame
::OnValueChanged
)
737 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame
::OnActivated
)
738 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame
::OnExpanding
)
739 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame
::OnExpanded
)
740 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame
::OnCollapsing
)
741 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame
::OnCollapsed
)
742 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame
::OnSelectionChanged
)
744 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame
::OnEditingStarted
)
745 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame
::OnEditingDone
)
747 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame
::OnHeaderClick
)
748 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame
::OnHeaderRightClick
)
749 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame
::OnSorted
)
751 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame
::OnContextMenu
)
753 EVT_RIGHT_UP(MyFrame
::OnRightClick
)
756 MyFrame
::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
757 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
762 SetIcon(wxICON(sample
));
766 wxMenu
*file_menu
= new wxMenu
;
767 file_menu
->Append(ID_ABOUT
, wxT("&About"));
768 file_menu
->AppendSeparator();
769 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
771 wxMenuBar
*menu_bar
= new wxMenuBar
;
772 menu_bar
->Append(file_menu
, wxT("&File"));
774 SetMenuBar(menu_bar
);
777 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
779 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
783 m_musicCtrl
= new wxDataViewCtrl( this, ID_MUSIC_CTRL
, wxDefaultPosition
,
784 wxDefaultSize
, wxDV_MULTIPLE
|wxDV_VARIABLE_LINE_HEIGHT
);
786 m_music_model
= new MyMusicModel
;
787 m_musicCtrl
->AssociateModel( m_music_model
.get() );
789 wxDataViewTextRenderer
*tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
790 wxDataViewColumn
*column0
= new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
791 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
792 m_musicCtrl
->AppendColumn( column0
);
794 // Call this and sorting is enabled
795 // immediatly upon start up.
796 column0
->SetSortOrder( true );
799 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
800 wxDataViewColumn
*column1
= new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_RIGHT
,
801 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
802 m_musicCtrl
->AppendColumn( column1
);
804 wxDataViewSpinRenderer
*sr
= new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
805 wxDataViewColumn
*column2
= new wxDataViewColumn( wxT("year"), sr
, 2, 80, wxALIGN_LEFT
,
806 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
807 m_musicCtrl
->AppendColumn( column2
);
809 m_musicCtrl
->AppendProgressColumn( wxT("popularity"), 3, wxDATAVIEW_CELL_INERT
, 80 );
811 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
812 wxDataViewColumn
*column3
= new wxDataViewColumn( wxT("custom"), cr
, 2, -1, wxALIGN_LEFT
,
813 wxDATAVIEW_COL_RESIZABLE
);
814 m_musicCtrl
->AppendColumn( column3
);
816 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
822 m_listCtrl
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
823 wxDefaultSize
, wxDV_MULTIPLE
| wxDV_ROW_LINES
);
825 m_list_model
= new MyListModel
;
826 m_listCtrl
->AssociateModel( m_list_model
.get() );
829 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
830 m_listCtrl
->AppendIconTextColumn(wxIcon(small1_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
832 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
833 m_listCtrl
->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
836 wxDataViewTextRendererAttr
*ra
= new wxDataViewTextRendererAttr
;
837 wxDataViewColumn
*column4
= new wxDataViewColumn(wxT("attributes"), ra
, 2 );
838 m_listCtrl
->AppendColumn( column4
);
840 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
844 main_sizer
->Add( data_sizer
, 2, wxGROW
);
846 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
848 button_sizer
->Add( new wxButton( this, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
849 button_sizer
->Add( new wxButton( this, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
850 button_sizer
->Add( new wxButton( this, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
851 button_sizer
->Add( 10, 10, 1 );
852 wxFlexGridSizer
*grid_sizer
= new wxFlexGridSizer( 2, 2 );
853 grid_sizer
->Add( new wxButton( this, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 2 );
854 grid_sizer
->Add( new wxButton( this, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 2 );
855 grid_sizer
->Add( new wxButton( this, ID_GOTO
, _("Goto 50")), 0, wxALL
, 2 );
856 grid_sizer
->Add( new wxButton( this, ID_ADD_MANY
, _("Add 1000")), 0, wxALL
, 2 );
857 button_sizer
->Add( grid_sizer
, 0, wxALL
, 10 );
859 main_sizer
->Add( button_sizer
, 0, wxGROW
, 0 );
861 wxBoxSizer
*bottom_sizer
= new wxBoxSizer( wxHORIZONTAL
);
863 m_log
= new wxTextCtrl( this, -1, wxString(), wxDefaultPosition
, wxSize(100,200), wxTE_MULTILINE
);
864 m_logOld
= wxLog
::SetActiveTarget(new wxLogTextCtrl(m_log
));
865 wxLogMessage(_("This is the log window"));
867 bottom_sizer
->Add( m_log
, 1, wxGROW
);
869 // wxDataViewTreeStore
871 wxDataViewCtrl
*treectrl
= new wxDataViewCtrl( this, -1,
872 wxDefaultPosition
, wxSize(100,200), wxDV_NO_HEADER
);
874 wxDataViewTreeStore
*store
= new wxDataViewTreeStore
;
875 wxDataViewItem parent
= store
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm
) );
876 wxDataViewItem child
= store
->AppendItem( parent
,wxT("Child 1"), wxIcon(small1_xpm
) );
877 child
= store
->AppendItem( parent
,wxT("Child 2"), wxIcon(small1_xpm
) );
878 child
= store
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), wxIcon(small1_xpm
) );
879 treectrl
->AssociateModel( store
);
882 treectrl
->AppendIconTextColumn( wxT("no label"), 0, wxDATAVIEW_CELL_INERT
, -1, (wxAlignment
) 0,
883 wxDATAVIEW_COL_RESIZABLE
);
885 bottom_sizer
->Add( treectrl
, 1 );
887 // wxDataViewTreeCtrl
889 wxDataViewTreeCtrl
*treectrl2
= new wxDataViewTreeCtrl( this, -1, wxDefaultPosition
, wxSize(100,200) );
891 wxImageList
*ilist
= new wxImageList( 16, 16 );
892 ilist
->Add( wxIcon(small1_xpm
) );
893 treectrl2
->SetImageList( ilist
);
895 parent
= treectrl2
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
896 child
= treectrl2
->AppendItem( parent
,wxT("Child 1"), 0 );
897 child
= treectrl2
->AppendItem( parent
,wxT("Child 2"), 0 );
898 child
= treectrl2
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), 0 );
900 bottom_sizer
->Add( treectrl2
, 1 );
904 main_sizer
->Add( bottom_sizer
, 0, wxGROW
);
906 SetSizer( main_sizer
);
911 delete wxLog
::SetActiveTarget(m_logOld
);
914 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
919 void MyFrame
::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
921 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
924 void MyFrame
::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
926 wxDataViewItemArray items
;
927 int len
= m_musicCtrl
->GetSelections( items
);
928 for( int i
= 0; i
< len
; i
++ )
930 m_music_model
->Delete( items
[i
] );
933 void MyFrame
::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
935 m_musicCtrl
->DeleteColumn( m_musicCtrl
->GetColumn( 2 ) );
936 FindWindow( ID_DELETE_YEAR
)->Disable();
939 void MyFrame
::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
941 m_list_model
->Prepend(wxT("Test"));
944 void MyFrame
::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
946 wxDataViewItemArray items
;
947 int len
= m_listCtrl
->GetSelections( items
);
949 m_list_model
->DeleteItems( items
);
952 void MyFrame
::OnValueChanged( wxDataViewEvent
&event
)
957 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event
.GetItem().GetID(), event
.GetColumn() );
960 void MyFrame
::OnActivated( wxDataViewEvent
&event
)
965 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
966 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
.GetData());
969 void MyFrame
::OnSelectionChanged( wxDataViewEvent
&event
)
974 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
978 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
.GetData() );
981 void MyFrame
::OnExpanding( wxDataViewEvent
&event
)
986 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
987 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
.GetData() );
991 void MyFrame
::OnEditingStarted( wxDataViewEvent
&event
)
996 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
997 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
.GetData() );
1000 void MyFrame
::OnEditingDone( wxDataViewEvent
&event
)
1005 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1006 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
.GetData() );
1009 void MyFrame
::OnExpanded( wxDataViewEvent
&event
)
1014 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1015 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
.GetData() );
1018 void MyFrame
::OnCollapsing( wxDataViewEvent
&event
)
1023 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1024 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
.GetData() );
1027 void MyFrame
::OnCollapsed( wxDataViewEvent
&event
)
1032 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1033 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
.GetData());
1036 void MyFrame
::OnContextMenu( wxDataViewEvent
&event
)
1041 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1042 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
.GetData());
1044 wxMenu
*menu
= new wxMenu
;
1045 menu
->Append( 1, wxT("entry 1") );
1046 menu
->Append( 2, wxT("entry 2") );
1047 menu
->Append( 3, wxT("entry 3") );
1049 m_musicCtrl
->PopupMenu( menu
);
1051 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1054 void MyFrame
::OnHeaderClick( wxDataViewEvent
&event
)
1059 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1061 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
1064 void MyFrame
::OnHeaderRightClick( wxDataViewEvent
&event
)
1069 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1071 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
1074 void MyFrame
::OnSorted( wxDataViewEvent
&event
)
1079 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1081 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
1084 void MyFrame
::OnRightClick( wxMouseEvent
&event
)
1089 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event
.GetX(), event
.GetY());
1092 void MyFrame
::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1094 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1095 m_listCtrl
->EnsureVisible(item
,m_col
);
1098 void MyFrame
::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1100 m_list_model
->AddMany();
1104 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1106 wxAboutDialogInfo info
;
1107 info
.SetName(_("DataView sample"));
1108 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
1109 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));