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 unsigned long litem1
= (unsigned long) item1
.GetID();
236 unsigned long litem2
= (unsigned long) 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;
270 wxLogError( wxT("MyMusicModel::GetValue: wrong column" ));
272 // provoke a crash when mouse button down
273 wxMouseState state
= wxGetMouseState();
274 if (state
.ShiftDown())
283 virtual bool SetValue( const wxVariant
&variant
,
284 const wxDataViewItem
&item
, unsigned int col
)
286 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
289 case 0: node
->m_title
= variant
.GetString(); return true;
290 case 1: node
->m_artist
= variant
.GetString(); return true;
291 case 2: node
->m_year
= variant
.GetLong(); return true;
292 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
297 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
299 // the invisble root node has no parent
301 return wxDataViewItem(0);
303 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
305 // "MyMusic" also has no parent
307 return wxDataViewItem(0);
309 return wxDataViewItem( (void*) node
->GetParent() );
312 virtual bool IsContainer( const wxDataViewItem
&item
) const
314 // the invisble root node can have children (in
315 // our model always "MyMusic")
319 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
320 return node
->IsContainer();
323 virtual unsigned int GetChildren( const wxDataViewItem
&parent
, wxDataViewItemArray
&array
) const
325 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
328 array
.Add( wxDataViewItem( (void*) m_root
) );
332 if (node
== m_classical
)
334 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
335 model
->m_classicalMusicIsKnownToControl
= true;
338 if (node
->GetChildCount() == 0)
343 unsigned int count
= node
->GetChildren().GetCount();
345 for (pos
= 0; pos
< count
; pos
++)
347 MyMusicModelNode
*child
= node
->GetChildren().Item( pos
);
348 array
.Add( wxDataViewItem( (void*) child
) );
355 virtual bool IsDraggable( const wxDataViewItem
&item
)
358 return (!IsContainer(item
));
361 virtual size_t GetDragDataSize( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
) )
363 wxPrintf( "GetDragDataSize\n" );
365 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
367 data
+= node
->m_title
; data
+= wxT(" ");
368 data
+= node
->m_artist
;
369 return strlen( data
.utf8_str() ) + 1;
371 virtual bool GetDragData( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
),
372 void* dest
, size_t WXUNUSED(size
) )
374 wxPrintf( "GetDragData\n" );
376 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
378 data
+= node
->m_title
; data
+= wxT(" ");
379 data
+= node
->m_artist
;
380 wxCharBuffer
buffer( data
.utf8_str() );
381 memcpy( dest
, buffer
, strlen(buffer
)+1 );
386 MyMusicModelNode
* m_root
;
387 MyMusicModelNode
* m_pop
;
388 MyMusicModelNode
* m_classical
;
389 bool m_classicalMusicIsKnownToControl
;
393 static int my_sort_reverse( int *v1
, int *v2
)
398 static int my_sort( int *v1
, int *v2
)
403 class MyListModel
: public wxDataViewVirtualListModel
408 wxDataViewVirtualListModel( 1000 + 100 )
410 wxDataViewVirtualListModel( 100000 + 100 )
414 m_virtualItems
= 1000;
416 m_virtualItems
= 100000;
420 for (i
= 0; i
< 100; i
++)
423 str
.Printf( wxT("row number %d"), i
);
427 m_icon
= wxIcon( null_xpm
);
430 // helper methods to change the model
432 void Prepend( const wxString
&text
)
434 m_array
.Insert( text
, 0 );
438 void DeleteItem( const wxDataViewItem
&item
)
440 unsigned int row
= GetRow( item
);
441 if (row
>= m_array
.GetCount())
444 m_array
.RemoveAt( row
);
448 void DeleteItems( const wxDataViewItemArray
&items
)
452 for (i
= 0; i
< items
.GetCount(); i
++)
454 unsigned int row
= GetRow( items
[i
] );
455 if (row
< m_array
.GetCount())
459 // Sort in descending order so that the last
460 // row will be deleted first. Otherwise the
461 // remaining indeces would all be wrong.
462 rows
.Sort( my_sort_reverse
);
463 for (i
= 0; i
< rows
.GetCount(); i
++)
464 m_array
.RemoveAt( rows
[i
] );
466 // This is just to test if wxDataViewCtrl can
467 // cope with removing rows not sorted in
469 rows
.Sort( my_sort
);
475 m_virtualItems
+= 1000;
476 Reset( m_array
.GetCount() + m_virtualItems
);
479 // implementation of base class virtuals to define model
481 virtual unsigned int GetColumnCount() const
486 virtual wxString
GetColumnType( unsigned int col
) const
489 return wxT("wxDataViewIconText");
491 return wxT("string");
494 virtual unsigned int GetRowCount()
496 return m_array
.GetCount();
499 virtual void GetValue( wxVariant
&variant
,
500 unsigned int row
, unsigned int col
) const
504 if (row
>= m_array
.GetCount())
507 str
.Printf(wxT("row %d"), row
- m_array
.GetCount() );
512 variant
= m_array
[ row
];
517 wxDataViewIconText
data( wxT("test"), m_icon
);
522 if (row
>= m_array
.GetCount())
523 variant
= wxT("plain");
525 variant
= wxT("blue");
529 virtual bool GetAttr( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
)
534 if (row
< m_array
.GetCount())
536 attr
.SetColour( *wxBLUE
);
537 attr
.SetItalic( true );
543 virtual bool SetValue( const wxVariant
&variant
,
544 unsigned int row
, unsigned int col
)
548 if (row
>= m_array
.GetCount())
551 m_array
[row
] = variant
.GetString();
558 wxArrayString m_array
;
563 // -------------------------------------
565 // -------------------------------------
567 class MyCustomRenderer
: public wxDataViewCustomRenderer
570 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
571 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
) { }
573 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
575 dc
->SetBrush( *wxRED_BRUSH
);
576 dc
->SetPen( *wxTRANSPARENT_PEN
);
577 dc
->DrawRectangle( rect
);
582 virtual bool Activate( wxRect
WXUNUSED(cell
),
583 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
585 wxLogMessage( wxT("MyCustomRenderer Activate()") );
589 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
590 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
592 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor
.x
, cursor
.y
);
596 virtual wxSize
GetSize() const
598 return wxSize(60,20);
601 virtual bool SetValue( const wxVariant
&WXUNUSED(value
) ) { return true; }
602 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
605 // -------------------------------------
607 // -------------------------------------
609 class MyApp
: public wxApp
616 // -------------------------------------
618 // -------------------------------------
620 class MyFrame
: public wxFrame
623 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
627 void OnQuit(wxCommandEvent
& event
);
628 void OnAbout(wxCommandEvent
& event
);
630 void OnAddMozart(wxCommandEvent
& event
);
631 void OnDeleteMusic(wxCommandEvent
& event
);
632 void OnDeleteYear(wxCommandEvent
& event
);
634 void OnPrependList(wxCommandEvent
& event
);
635 void OnDeleteList(wxCommandEvent
& event
);
637 void OnValueChanged( wxDataViewEvent
&event
);
639 void OnActivated( wxDataViewEvent
&event
);
640 void OnExpanding( wxDataViewEvent
&event
);
641 void OnExpanded( wxDataViewEvent
&event
);
642 void OnCollapsing( wxDataViewEvent
&event
);
643 void OnCollapsed( wxDataViewEvent
&event
);
644 void OnSelectionChanged( wxDataViewEvent
&event
);
646 void OnEditingStarted( wxDataViewEvent
&event
);
647 void OnEditingDone( wxDataViewEvent
&event
);
649 void OnHeaderClick( wxDataViewEvent
&event
);
650 void OnHeaderRightClick( wxDataViewEvent
&event
);
651 void OnSorted( wxDataViewEvent
&event
);
653 void OnContextMenu( wxDataViewEvent
&event
);
655 void OnRightClick( wxMouseEvent
&event
);
656 void OnGoto( wxCommandEvent
&event
);
657 void OnAddMany( wxCommandEvent
&event
);
660 wxDataViewCtrl
* m_musicCtrl
;
661 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
663 wxDataViewCtrl
* m_listCtrl
;
664 wxObjectDataPtr
<MyListModel
> m_list_model
;
666 wxDataViewColumn
* m_col
;
672 DECLARE_EVENT_TABLE()
675 // -------------------------------------
677 // -------------------------------------
681 bool MyApp::OnInit(void)
683 if ( !wxApp::OnInit() )
686 // build the first frame
688 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
701 // -------------------------------------
703 // -------------------------------------
708 ID_ABOUT
= wxID_ABOUT
,
714 ID_DELETE_MUSIC
= 101,
715 ID_DELETE_YEAR
= 102,
717 ID_PREPEND_LIST
= 200,
718 ID_DELETE_LIST
= 201,
723 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
724 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
725 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
726 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
727 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
728 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
729 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
730 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
731 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
732 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
734 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
736 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
737 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
738 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
739 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
740 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
741 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
743 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
744 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
746 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
747 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
748 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
750 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
752 EVT_RIGHT_UP(MyFrame::OnRightClick
)
755 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
756 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
761 SetIcon(wxICON(sample
));
765 wxMenu
*file_menu
= new wxMenu
;
766 file_menu
->Append(ID_ABOUT
, wxT("&About"));
767 file_menu
->AppendSeparator();
768 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
770 wxMenuBar
*menu_bar
= new wxMenuBar
;
771 menu_bar
->Append(file_menu
, wxT("&File"));
773 SetMenuBar(menu_bar
);
776 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
778 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
782 m_musicCtrl
= new wxDataViewCtrl( this, ID_MUSIC_CTRL
, wxDefaultPosition
,
783 wxDefaultSize
, wxDV_MULTIPLE
);
785 m_music_model
= new MyMusicModel
;
786 m_musicCtrl
->AssociateModel( m_music_model
.get() );
788 wxDataViewTextRenderer
*tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
789 wxDataViewColumn
*column0
= new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
790 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
791 m_musicCtrl
->AppendColumn( column0
);
793 // Call this and sorting is enabled
794 // immediatly upon start up.
795 column0
->SetSortOrder( true );
798 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
799 wxDataViewColumn
*column1
= new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_RIGHT
,
800 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
801 m_musicCtrl
->AppendColumn( column1
);
803 wxDataViewSpinRenderer
*sr
= new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
804 wxDataViewColumn
*column2
= new wxDataViewColumn( wxT("year"), sr
, 2, 80, wxALIGN_LEFT
,
805 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
806 m_musicCtrl
->AppendColumn( column2
);
808 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
809 wxDataViewColumn
*column3
= new wxDataViewColumn( wxT("custom"), cr
, 2, -1, wxALIGN_LEFT
,
810 wxDATAVIEW_COL_RESIZABLE
);
811 m_musicCtrl
->AppendColumn( column3
);
813 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
819 m_listCtrl
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
820 wxDefaultSize
, wxDV_MULTIPLE
| wxDV_ROW_LINES
);
822 m_list_model
= new MyListModel
;
823 m_listCtrl
->AssociateModel( m_list_model
.get() );
826 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
827 m_listCtrl
->AppendIconTextColumn(wxIcon(small1_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
829 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
830 m_listCtrl
->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
833 wxDataViewTextRendererAttr
*ra
= new wxDataViewTextRendererAttr
;
834 wxDataViewColumn
*column4
= new wxDataViewColumn(wxT("attributes"), ra
, 2 );
835 m_listCtrl
->AppendColumn( column4
);
837 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
841 main_sizer
->Add( data_sizer
, 2, wxGROW
);
843 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
845 button_sizer
->Add( new wxButton( this, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
846 button_sizer
->Add( new wxButton( this, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
847 button_sizer
->Add( new wxButton( this, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
848 button_sizer
->Add( 10, 10, 1 );
849 wxFlexGridSizer
*grid_sizer
= new wxFlexGridSizer( 2, 2 );
850 grid_sizer
->Add( new wxButton( this, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 2 );
851 grid_sizer
->Add( new wxButton( this, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 2 );
852 grid_sizer
->Add( new wxButton( this, ID_GOTO
, _("Goto 50")), 0, wxALL
, 2 );
853 grid_sizer
->Add( new wxButton( this, ID_ADD_MANY
, _("Add 1000")), 0, wxALL
, 2 );
854 button_sizer
->Add( grid_sizer
, 0, wxALL
, 10 );
856 main_sizer
->Add( button_sizer
, 0, wxGROW
, 0 );
858 wxBoxSizer
*bottom_sizer
= new wxBoxSizer( wxHORIZONTAL
);
860 m_log
= new wxTextCtrl( this, -1, wxString(), wxDefaultPosition
, wxSize(100,200), wxTE_MULTILINE
);
861 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
862 wxLogMessage(_("This is the log window"));
864 bottom_sizer
->Add( m_log
, 1, wxGROW
);
866 // wxDataViewTreeStore
868 wxDataViewCtrl
*treectrl
= new wxDataViewCtrl( this, -1,
869 wxDefaultPosition
, wxSize(100,200), wxDV_NO_HEADER
);
871 wxDataViewTreeStore
*store
= new wxDataViewTreeStore
;
872 wxDataViewItem parent
= store
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm
) );
873 wxDataViewItem child
= store
->AppendItem( parent
,wxT("Child 1"), wxIcon(small1_xpm
) );
874 child
= store
->AppendItem( parent
,wxT("Child 2"), wxIcon(small1_xpm
) );
875 child
= store
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), wxIcon(small1_xpm
) );
876 treectrl
->AssociateModel( store
);
879 treectrl
->AppendIconTextColumn( wxT("no label"), 0, wxDATAVIEW_CELL_INERT
, -1, (wxAlignment
) 0,
880 wxDATAVIEW_COL_RESIZABLE
);
882 bottom_sizer
->Add( treectrl
, 1 );
884 // wxDataViewTreeCtrl
886 wxDataViewTreeCtrl
*treectrl2
= new wxDataViewTreeCtrl( this, -1, wxDefaultPosition
, wxSize(100,200) );
888 wxImageList
*ilist
= new wxImageList( 16, 16 );
889 ilist
->Add( wxIcon(small1_xpm
) );
890 treectrl2
->SetImageList( ilist
);
892 parent
= treectrl2
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
893 child
= treectrl2
->AppendItem( parent
,wxT("Child 1"), 0 );
894 child
= treectrl2
->AppendItem( parent
,wxT("Child 2"), 0 );
895 child
= treectrl2
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), 0 );
897 bottom_sizer
->Add( treectrl2
, 1 );
901 main_sizer
->Add( bottom_sizer
, 0, wxGROW
);
903 SetSizer( main_sizer
);
908 delete wxLog::SetActiveTarget(m_logOld
);
911 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
916 void MyFrame::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
918 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
921 void MyFrame::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
923 wxDataViewItemArray items
;
924 int len
= m_musicCtrl
->GetSelections( items
);
925 for( int i
= 0; i
< len
; i
++ )
927 m_music_model
->Delete( items
[i
] );
930 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
932 m_musicCtrl
->DeleteColumn( m_musicCtrl
->GetColumn( 2 ) );
933 FindWindow( ID_DELETE_YEAR
)->Disable();
936 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
938 m_list_model
->Prepend(wxT("Test"));
941 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
943 wxDataViewItemArray items
;
944 int len
= m_listCtrl
->GetSelections( items
);
946 m_list_model
->DeleteItems( items
);
949 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
954 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event
.GetItem().GetID(), event
.GetColumn() );
957 void MyFrame::OnActivated( wxDataViewEvent
&event
)
962 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
963 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
.GetData());
966 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
971 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
975 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
.GetData() );
978 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
983 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
984 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
.GetData() );
988 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
993 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
994 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
.GetData() );
997 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
1002 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1003 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
.GetData() );
1006 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
1011 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1012 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
.GetData() );
1015 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1020 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1021 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
.GetData() );
1024 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1029 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1030 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
.GetData());
1033 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1038 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1039 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
.GetData());
1041 wxMenu
*menu
= new wxMenu
;
1042 menu
->Append( 1, wxT("entry 1") );
1043 menu
->Append( 2, wxT("entry 2") );
1044 menu
->Append( 3, wxT("entry 3") );
1046 m_musicCtrl
->PopupMenu( menu
);
1048 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1051 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1056 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1058 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
1061 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1066 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1068 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
1071 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1076 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1078 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
1081 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1086 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event
.GetX(), event
.GetY());
1089 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1091 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1092 m_listCtrl
->EnsureVisible(item
,m_col
);
1095 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1097 m_list_model
->AddMany();
1101 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1103 wxAboutDialogInfo info
;
1104 info
.SetName(_("DataView sample"));
1105 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
1106 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));