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"
31 #include "wx/ptr_shrd.h"
32 #include "wx/vector.h"
35 #include "../sample.xpm"
41 static const char *small1_xpm
[] = {
42 /* columns rows colors chars-per-pixel */
70 #define DEFAULT_ALIGN wxALIGN_LEFT
71 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
73 // -------------------------------------
75 // -------------------------------------
78 Implement this data model
80 -------------------------------------------------------------
83 3: You are not alone Michael Jackson 1995
84 4: Take a bow Madonna 1994
86 6: Ninth Symphony Ludwig v. Beethoven 1824
87 7: German Requiem Johannes Brahms 1868
92 class MyMusicModelNode
;
93 WX_DEFINE_ARRAY_PTR( MyMusicModelNode
*, MyMusicModelNodes
);
95 class MyMusicModelNode
98 MyMusicModelNode( MyMusicModelNode
* parent
,
99 const wxString
&title
, const wxString
&artist
, int year
)
105 m_isContainer
= false;
108 MyMusicModelNode( MyMusicModelNode
* parent
,
109 const wxString
&branch
)
114 m_isContainer
= true;
119 size_t count
= m_children
.GetCount();
121 for (i
= 0; i
< count
; i
++)
123 MyMusicModelNode
*child
= m_children
[i
];
128 bool IsContainer() { return m_isContainer
; }
130 MyMusicModelNode
* GetParent() { return m_parent
; }
131 MyMusicModelNodes
&GetChildren() { return m_children
; }
132 MyMusicModelNode
* GetNthChild( unsigned int n
) { return m_children
.Item( n
); }
133 void Insert( MyMusicModelNode
* child
, unsigned int n
) { m_children
.Insert( child
, n
); }
134 void Append( MyMusicModelNode
* child
) { m_children
.Add( child
); }
135 unsigned int GetChildCount() { return m_children
.GetCount(); }
143 MyMusicModelNode
*m_parent
;
144 MyMusicModelNodes m_children
;
149 class MyMusicModel
: public wxDataViewModel
157 m_root
= new MyMusicModelNode( NULL
, wxT("My Music" ));
158 m_pop
= new MyMusicModelNode( m_root
, wxT("Pop music") );
159 m_root
->Append( m_pop
);
160 m_pop
->Append( new MyMusicModelNode( m_pop
,
161 wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
162 m_pop
->Append( new MyMusicModelNode( m_pop
,
163 wxT("Take a bow"), wxT("Madonna"), 1994 ) );
164 m_classical
= new MyMusicModelNode( m_root
, wxT("Classical music") );
165 m_root
->Append( m_classical
);
166 m_classical
->Append( new MyMusicModelNode( m_classical
,
167 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 ) );
168 m_classical
->Append( new MyMusicModelNode( m_classical
,
169 wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
170 m_classicalMusicIsKnownToControl
= false;
178 // helper method for wxLog
180 wxString
GetTitle( const wxDataViewItem
&item
)
182 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
184 return wxEmptyString
;
186 return node
->m_title
;
189 // helper methods to change the model
191 void AddToClassical( const wxString
&title
, const wxString
&artist
, int year
)
194 MyMusicModelNode
*child_node
=
195 new MyMusicModelNode( m_classical
, title
, artist
, year
);
197 m_classical
->Append( child_node
);
199 if (m_classicalMusicIsKnownToControl
)
202 wxDataViewItem
child( (void*) child_node
);
203 wxDataViewItem
parent( (void*) m_classical
);
204 ItemAdded( parent
, child
);
208 void Delete( const wxDataViewItem
&item
)
210 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
211 wxDataViewItem
parent( node
->GetParent() );
213 node
->GetParent()->GetChildren().Remove( node
);
217 ItemDeleted( parent
, item
);
220 // override sorting to always sort branches ascendingly
222 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
223 unsigned int column
, bool ascending
)
225 if (IsContainer(item1
) && IsContainer(item2
))
227 wxVariant value1
,value2
;
228 GetValue( value1
, item1
, 0 );
229 GetValue( value2
, item2
, 0 );
231 wxString str1
= value1
.GetString();
232 wxString str2
= value2
.GetString();
233 int res
= str1
.Cmp( str2
);
236 // items must be different
237 unsigned long litem1
= (unsigned long) item1
.GetID();
238 unsigned long litem2
= (unsigned long) item2
.GetID();
240 return litem1
-litem2
;
243 return wxDataViewModel::Compare( item1
, item2
, column
, ascending
);
246 // implementation of base class virtuals to define model
248 virtual unsigned int GetColumnCount() const
253 virtual wxString
GetColumnType( unsigned int col
) const
258 return wxT("string");
261 virtual void GetValue( wxVariant
&variant
,
262 const wxDataViewItem
&item
, unsigned int col
) const
264 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
267 case 0: variant
= node
->m_title
; break;
268 case 1: variant
= node
->m_artist
; break;
269 case 2: variant
= (long) node
->m_year
; break;
272 wxLogError( wxT("MyMusicModel::GetValue: wrong column" ));
274 // provoke a crash when mouse button down
275 wxMouseState state
= wxGetMouseState();
276 if (state
.ShiftDown())
285 virtual bool SetValue( const wxVariant
&variant
,
286 const wxDataViewItem
&item
, unsigned int col
)
288 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
291 case 0: node
->m_title
= variant
.GetString(); return true;
292 case 1: node
->m_artist
= variant
.GetString(); return true;
293 case 2: node
->m_year
= variant
.GetLong(); return true;
294 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
299 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
301 // the invisble root node has no parent
303 return wxDataViewItem(0);
305 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
307 // "MyMusic" also has no parent
309 return wxDataViewItem(0);
311 return wxDataViewItem( (void*) node
->GetParent() );
314 virtual bool IsContainer( const wxDataViewItem
&item
) const
316 // the invisble root node can have children (in
317 // our model always "MyMusic")
321 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
322 return node
->IsContainer();
325 virtual unsigned int GetChildren( const wxDataViewItem
&parent
, wxDataViewItemArray
&array
) const
327 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
330 array
.Add( wxDataViewItem( (void*) m_root
) );
334 if (node
== m_classical
)
336 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
337 model
->m_classicalMusicIsKnownToControl
= true;
340 if (node
->GetChildCount() == 0)
345 unsigned int count
= node
->GetChildren().GetCount();
347 for (pos
= 0; pos
< count
; pos
++)
349 MyMusicModelNode
*child
= node
->GetChildren().Item( pos
);
350 array
.Add( wxDataViewItem( (void*) child
) );
357 virtual bool IsDraggable( const wxDataViewItem
&item
)
360 return (!IsContainer(item
));
363 virtual size_t GetDragDataSize( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
) )
365 wxPrintf( "GetDragDataSize\n" );
367 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
369 data
+= node
->m_title
; data
+= wxT(" ");
370 data
+= node
->m_artist
;
371 return strlen( data
.utf8_str() ) + 1;
373 virtual bool GetDragData( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
),
374 void* dest
, size_t WXUNUSED(size
) )
376 wxPrintf( "GetDragData\n" );
378 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
380 data
+= node
->m_title
; data
+= wxT(" ");
381 data
+= node
->m_artist
;
382 wxCharBuffer
buffer( data
.utf8_str() );
383 memcpy( dest
, buffer
, strlen(buffer
)+1 );
388 MyMusicModelNode
* m_root
;
389 MyMusicModelNode
* m_pop
;
390 MyMusicModelNode
* m_classical
;
391 bool m_classicalMusicIsKnownToControl
;
395 static int my_sort_reverse( int *v1
, int *v2
)
400 static int my_sort( int *v1
, int *v2
)
405 class MyListModel
: public wxDataViewIndexListModel
410 wxDataViewIndexListModel( 1000 + 100 )
412 wxDataViewIndexListModel( 100000 + 100 )
416 m_virtualItems
= 1000;
418 m_virtualItems
= 100000;
422 for (i
= 0; i
< 100; i
++)
425 str
.Printf( wxT("row number %d"), i
);
429 m_icon
= wxIcon( null_xpm
);
432 // helper methods to change the model
434 void Prepend( const wxString
&text
)
436 m_array
.Insert( text
, 0 );
440 void DeleteItem( const wxDataViewItem
&item
)
442 unsigned int row
= GetRow( item
);
443 if (row
>= m_array
.GetCount())
446 m_array
.RemoveAt( row
);
450 void DeleteItems( const wxDataViewItemArray
&items
)
454 for (i
= 0; i
< items
.GetCount(); i
++)
456 unsigned int row
= GetRow( items
[i
] );
457 if (row
< m_array
.GetCount())
461 // Sort in descending order so that the last
462 // row will be deleted first. Otherwise the
463 // remaining indeces would all be wrong.
464 rows
.Sort( my_sort_reverse
);
465 for (i
= 0; i
< rows
.GetCount(); i
++)
466 m_array
.RemoveAt( rows
[i
] );
468 // This is just to test if wxDataViewCtrl can
469 // cope with removing rows not sorted in
471 rows
.Sort( my_sort
);
477 m_virtualItems
+= 1000;
478 Reset( m_array
.GetCount() + m_virtualItems
);
481 // implementation of base class virtuals to define model
483 virtual unsigned int GetColumnCount() const
488 virtual wxString
GetColumnType( unsigned int col
) const
491 return wxT("wxDataViewIconText");
493 return wxT("string");
496 virtual unsigned int GetRowCount()
498 return m_array
.GetCount();
501 virtual void GetValue( wxVariant
&variant
,
502 unsigned int row
, unsigned int col
) const
506 if (row
>= m_array
.GetCount())
509 str
.Printf(wxT("row %d"), row
- m_array
.GetCount() );
514 variant
= m_array
[ row
];
519 wxDataViewIconText
data( wxT("test"), m_icon
);
524 if (row
>= m_array
.GetCount())
525 variant
= wxT("plain");
527 variant
= wxT("blue");
531 virtual bool GetAttr( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
)
536 if (row
< m_array
.GetCount())
538 attr
.SetColour( *wxBLUE
);
539 attr
.SetItalic( true );
545 virtual bool SetValue( const wxVariant
&variant
,
546 unsigned int row
, unsigned int col
)
550 if (row
>= m_array
.GetCount())
553 m_array
[row
] = variant
.GetString();
560 wxArrayString m_array
;
565 // -------------------------------------
567 // -------------------------------------
569 class MyCustomRenderer
: public wxDataViewCustomRenderer
572 MyCustomRenderer( wxDataViewCellMode mode
= wxDATAVIEW_CELL_ACTIVATABLE
,
573 // MyCustomRenderer( wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
574 int alignment
= wxDVR_DEFAULT_ALIGNMENT
) :
575 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
) { }
577 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
579 dc
->SetBrush( *wxRED_BRUSH
);
580 dc
->SetPen( *wxTRANSPARENT_PEN
);
581 dc
->DrawRectangle( rect
);
586 virtual bool Activate( wxRect
WXUNUSED(cell
),
587 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
589 wxLogMessage( wxT("MyCustomRenderer Activate()") );
593 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
594 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
596 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor
.x
, cursor
.y
);
600 virtual wxSize
GetSize() const
602 return wxSize(60,20);
605 virtual bool SetValue( const wxVariant
&WXUNUSED(value
) ) { return true; }
606 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
609 // -------------------------------------
611 // -------------------------------------
613 class MyApp
: public wxApp
620 // -------------------------------------
622 // -------------------------------------
624 class MyFrame
: public wxFrame
627 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
631 void OnQuit(wxCommandEvent
& event
);
632 void OnAbout(wxCommandEvent
& event
);
634 void OnAddMozart(wxCommandEvent
& event
);
635 void OnDeleteMusic(wxCommandEvent
& event
);
636 void OnDeleteYear(wxCommandEvent
& event
);
638 void OnPrependList(wxCommandEvent
& event
);
639 void OnDeleteList(wxCommandEvent
& event
);
641 void OnValueChanged( wxDataViewEvent
&event
);
643 void OnActivated( wxDataViewEvent
&event
);
644 void OnExpanding( wxDataViewEvent
&event
);
645 void OnExpanded( wxDataViewEvent
&event
);
646 void OnCollapsing( wxDataViewEvent
&event
);
647 void OnCollapsed( wxDataViewEvent
&event
);
648 void OnSelectionChanged( wxDataViewEvent
&event
);
650 void OnEditingStarted( wxDataViewEvent
&event
);
651 void OnEditingDone( wxDataViewEvent
&event
);
653 void OnHeaderClick( wxDataViewEvent
&event
);
654 void OnHeaderRightClick( wxDataViewEvent
&event
);
655 void OnSorted( wxDataViewEvent
&event
);
657 void OnContextMenu( wxDataViewEvent
&event
);
659 void OnRightClick( wxMouseEvent
&event
);
660 void OnGoto( wxCommandEvent
&event
);
661 void OnAddMany( wxCommandEvent
&event
);
664 wxDataViewCtrl
* m_musicCtrl
;
665 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
667 wxDataViewCtrl
* m_listCtrl
;
668 wxObjectDataPtr
<MyListModel
> m_list_model
;
670 wxDataViewColumn
* m_col
;
676 DECLARE_EVENT_TABLE()
679 // -------------------------------------
681 // -------------------------------------
685 bool MyApp::OnInit(void)
687 if ( !wxApp::OnInit() )
690 // build the first frame
692 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
705 // -------------------------------------
707 // -------------------------------------
712 ID_ABOUT
= wxID_ABOUT
,
718 ID_DELETE_MUSIC
= 101,
719 ID_DELETE_YEAR
= 102,
721 ID_PREPEND_LIST
= 200,
722 ID_DELETE_LIST
= 201,
727 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
728 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
729 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
730 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
731 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
732 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
733 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
734 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
735 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
736 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
738 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
740 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
741 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
742 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
743 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
744 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
745 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
747 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
748 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
750 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
751 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
752 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
754 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
756 EVT_RIGHT_UP(MyFrame::OnRightClick
)
759 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
760 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
765 SetIcon(wxICON(sample
));
769 wxMenu
*file_menu
= new wxMenu
;
770 file_menu
->Append(ID_ABOUT
, wxT("&About"));
771 file_menu
->AppendSeparator();
772 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
774 wxMenuBar
*menu_bar
= new wxMenuBar
;
775 menu_bar
->Append(file_menu
, wxT("&File"));
777 SetMenuBar(menu_bar
);
780 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
782 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
786 m_musicCtrl
= new wxDataViewCtrl( this, ID_MUSIC_CTRL
, wxDefaultPosition
,
787 wxDefaultSize
, wxDV_MULTIPLE
);
789 m_music_model
= new MyMusicModel
;
790 m_musicCtrl
->AssociateModel( m_music_model
.get() );
793 // Call this and sorting is enabled
794 // immediatly upon start up.
795 wxDataViewColumn
*col
= m_musicCtrl
->AppendTextColumn( wxT("Title"), 0, wxDATAVIEW_CELL_INERT
, 200, DEFAULT_ALIGN
,
796 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
797 col
->SetSortOrder( true );
799 m_musicCtrl
->AppendTextColumn( wxT("Title"), 0, wxDATAVIEW_CELL_INERT
, 200, wxALIGN_RIGHT
,
800 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
803 m_musicCtrl
->AppendTextColumn( wxT("Artist"), 1, wxDATAVIEW_CELL_EDITABLE
, 150, wxALIGN_RIGHT
,
804 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
806 wxDataViewSpinRenderer
*sr
= new wxDataViewSpinRenderer( 0, 2010 );
807 wxDataViewColumn
*column1
= new wxDataViewColumn( wxT("year"), sr
, 2, 80, wxALIGN_CENTRE
,
808 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
809 m_musicCtrl
->AppendColumn( column1
);
811 MyCustomRenderer
*cr
= new MyCustomRenderer
;
812 wxDataViewColumn
*column2
= new wxDataViewColumn( wxT("custom"), cr
, 2, -1, wxALIGN_RIGHT
,
813 wxDATAVIEW_COL_RESIZABLE
);
814 m_musicCtrl
->AppendColumn( column2
);
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(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
, 60 );
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
*column3
= new wxDataViewColumn(wxT("attributes"), ra
, 2 );
838 m_listCtrl
->AppendColumn( column3
);
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());
1043 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1046 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1051 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1053 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
1056 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1061 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1063 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
1066 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1071 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1073 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
1076 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1081 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event
.GetX(), event
.GetY());
1084 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1086 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1087 m_listCtrl
->EnsureVisible(item
,m_col
);
1090 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1092 m_list_model
->AddMany();
1096 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1098 wxAboutDialogInfo info
;
1099 info
.SetName(_("DataView sample"));
1100 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
1101 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));