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
) const
180 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
182 return wxEmptyString
;
184 return node
->m_title
;
187 int GetYear( const wxDataViewItem
&item
) const
189 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
196 // helper methods to change the model
198 void AddToClassical( const wxString
&title
, const wxString
&artist
, int year
)
201 MyMusicModelNode
*child_node
=
202 new MyMusicModelNode( m_classical
, title
, artist
, year
);
204 m_classical
->Append( child_node
);
206 if (m_classicalMusicIsKnownToControl
)
209 wxDataViewItem
child( (void*) child_node
);
210 wxDataViewItem
parent( (void*) m_classical
);
211 ItemAdded( parent
, child
);
215 void Delete( const wxDataViewItem
&item
)
217 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
218 wxDataViewItem
parent( node
->GetParent() );
220 node
->GetParent()->GetChildren().Remove( node
);
224 ItemDeleted( parent
, item
);
227 // override sorting to always sort branches ascendingly
229 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
230 unsigned int column
, bool ascending
)
232 if (IsContainer(item1
) && IsContainer(item2
))
234 wxVariant value1
,value2
;
235 GetValue( value1
, item1
, 0 );
236 GetValue( value2
, item2
, 0 );
238 wxString str1
= value1
.GetString();
239 wxString str2
= value2
.GetString();
240 int res
= str1
.Cmp( str2
);
243 // items must be different
244 wxUIntPtr litem1
= (wxUIntPtr
) item1
.GetID();
245 wxUIntPtr litem2
= (wxUIntPtr
) item2
.GetID();
247 return litem1
-litem2
;
250 return wxDataViewModel::Compare( item1
, item2
, column
, ascending
);
253 // implementation of base class virtuals to define model
255 virtual unsigned int GetColumnCount() const
260 virtual wxString
GetColumnType( unsigned int col
) const
265 return wxT("string");
268 virtual void GetValue( wxVariant
&variant
,
269 const wxDataViewItem
&item
, unsigned int col
) const
271 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
274 case 0: variant
= node
->m_title
; break;
275 case 1: variant
= node
->m_artist
; break;
276 case 2: variant
= (long) node
->m_year
; break;
278 // wxMac doesn't conceal the popularity progress renderer, return 0 for containers
279 if (IsContainer(item
))
282 variant
= (long) 80; // all music is very 80% popular
285 // Make size of red square depend on year
286 if (GetYear(item
) < 1900)
293 wxLogError( wxT("MyMusicModel::GetValue: wrong column %d"), col
);
295 // provoke a crash when mouse button down
296 wxMouseState state
= wxGetMouseState();
297 if (state
.ShiftDown())
306 virtual bool SetValue( const wxVariant
&variant
,
307 const wxDataViewItem
&item
, unsigned int col
)
309 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
312 case 0: node
->m_title
= variant
.GetString(); return true;
313 case 1: node
->m_artist
= variant
.GetString(); return true;
314 case 2: node
->m_year
= variant
.GetLong(); return true;
315 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
320 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
322 // the invisble root node has no parent
324 return wxDataViewItem(0);
326 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
328 // "MyMusic" also has no parent
330 return wxDataViewItem(0);
332 return wxDataViewItem( (void*) node
->GetParent() );
335 virtual bool IsContainer( const wxDataViewItem
&item
) const
337 // the invisble root node can have children (in
338 // our model always "MyMusic")
342 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
343 return node
->IsContainer();
346 virtual unsigned int GetChildren( const wxDataViewItem
&parent
, wxDataViewItemArray
&array
) const
348 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
351 array
.Add( wxDataViewItem( (void*) m_root
) );
355 if (node
== m_classical
)
357 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
358 model
->m_classicalMusicIsKnownToControl
= true;
361 if (node
->GetChildCount() == 0)
366 unsigned int count
= node
->GetChildren().GetCount();
368 for (pos
= 0; pos
< count
; pos
++)
370 MyMusicModelNode
*child
= node
->GetChildren().Item( pos
);
371 array
.Add( wxDataViewItem( (void*) child
) );
378 virtual bool IsDraggable( const wxDataViewItem
&item
)
381 return (!IsContainer(item
));
384 virtual size_t GetDragDataSize( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
) )
386 wxPrintf( "GetDragDataSize\n" );
388 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
390 data
+= node
->m_title
; data
+= wxT(" ");
391 data
+= node
->m_artist
;
392 return strlen( data
.utf8_str() ) + 1;
394 virtual bool GetDragData( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
),
395 void* dest
, size_t WXUNUSED(size
) )
397 wxPrintf( "GetDragData\n" );
399 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
401 data
+= node
->m_title
; data
+= wxT(" ");
402 data
+= node
->m_artist
;
403 wxCharBuffer
buffer( data
.utf8_str() );
404 memcpy( dest
, buffer
, strlen(buffer
)+1 );
409 MyMusicModelNode
* m_root
;
410 MyMusicModelNode
* m_pop
;
411 MyMusicModelNode
* m_classical
;
412 bool m_classicalMusicIsKnownToControl
;
416 static int my_sort_reverse( int *v1
, int *v2
)
421 static int my_sort( int *v1
, int *v2
)
426 class MyListModel
: public wxDataViewVirtualListModel
431 wxDataViewVirtualListModel( 1000 + 100 )
433 wxDataViewVirtualListModel( 100000 + 100 )
437 m_virtualItems
= 1000;
439 m_virtualItems
= 100000;
443 for (i
= 0; i
< 100; i
++)
446 str
.Printf( wxT("row number %d"), i
);
450 m_icon
= wxIcon( null_xpm
);
453 // helper methods to change the model
455 void Prepend( const wxString
&text
)
457 m_array
.Insert( text
, 0 );
461 void DeleteItem( const wxDataViewItem
&item
)
463 unsigned int row
= GetRow( item
);
464 if (row
>= m_array
.GetCount())
467 m_array
.RemoveAt( row
);
471 void DeleteItems( const wxDataViewItemArray
&items
)
475 for (i
= 0; i
< items
.GetCount(); i
++)
477 unsigned int row
= GetRow( items
[i
] );
478 if (row
< m_array
.GetCount())
482 // Sort in descending order so that the last
483 // row will be deleted first. Otherwise the
484 // remaining indeces would all be wrong.
485 rows
.Sort( my_sort_reverse
);
486 for (i
= 0; i
< rows
.GetCount(); i
++)
487 m_array
.RemoveAt( rows
[i
] );
489 // This is just to test if wxDataViewCtrl can
490 // cope with removing rows not sorted in
492 rows
.Sort( my_sort
);
498 m_virtualItems
+= 1000;
499 Reset( m_array
.GetCount() + m_virtualItems
);
502 // implementation of base class virtuals to define model
504 virtual unsigned int GetColumnCount() const
509 virtual wxString
GetColumnType( unsigned int col
) const
512 return wxT("wxDataViewIconText");
514 return wxT("string");
517 virtual unsigned int GetRowCount()
519 return m_array
.GetCount();
522 virtual void GetValue( wxVariant
&variant
,
523 unsigned int row
, unsigned int col
) const
527 if (row
>= m_array
.GetCount())
530 str
.Printf(wxT("row %d"), row
- m_array
.GetCount() );
535 variant
= m_array
[ row
];
540 wxDataViewIconText
data( wxT("test"), m_icon
);
545 if (row
>= m_array
.GetCount())
546 variant
= wxT("plain");
548 variant
= wxT("blue");
552 virtual bool GetAttr( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
)
557 if (row
< m_array
.GetCount())
559 attr
.SetColour( *wxBLUE
);
560 attr
.SetItalic( true );
566 virtual bool SetValue( const wxVariant
&variant
,
567 unsigned int row
, unsigned int col
)
571 if (row
>= m_array
.GetCount())
574 m_array
[row
] = variant
.GetString();
581 wxArrayString m_array
;
586 // -------------------------------------
588 // -------------------------------------
590 class MyCustomRenderer
: public wxDataViewCustomRenderer
593 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
594 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
597 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
599 dc
->SetBrush( *wxRED_BRUSH
);
600 dc
->SetPen( *wxTRANSPARENT_PEN
);
601 dc
->DrawRectangle( rect
);
606 virtual bool Activate( wxRect
WXUNUSED(cell
),
607 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
609 wxLogMessage( wxT("MyCustomRenderer Activate()") );
613 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
614 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
616 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor
.x
, cursor
.y
);
620 virtual wxSize
GetSize() const
622 return wxSize(60,m_height
);
625 virtual bool SetValue( const wxVariant
&value
)
631 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
637 // -------------------------------------
639 // -------------------------------------
641 class MyApp
: public wxApp
648 // -------------------------------------
650 // -------------------------------------
652 class MyFrame
: public wxFrame
655 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
659 void OnQuit(wxCommandEvent
& event
);
660 void OnAbout(wxCommandEvent
& event
);
662 void OnAddMozart(wxCommandEvent
& event
);
663 void OnDeleteMusic(wxCommandEvent
& event
);
664 void OnDeleteYear(wxCommandEvent
& event
);
666 void OnPrependList(wxCommandEvent
& event
);
667 void OnDeleteList(wxCommandEvent
& event
);
669 void OnValueChanged( wxDataViewEvent
&event
);
671 void OnActivated( wxDataViewEvent
&event
);
672 void OnExpanding( wxDataViewEvent
&event
);
673 void OnExpanded( wxDataViewEvent
&event
);
674 void OnCollapsing( wxDataViewEvent
&event
);
675 void OnCollapsed( wxDataViewEvent
&event
);
676 void OnSelectionChanged( wxDataViewEvent
&event
);
678 void OnEditingStarted( wxDataViewEvent
&event
);
679 void OnEditingDone( wxDataViewEvent
&event
);
681 void OnHeaderClick( wxDataViewEvent
&event
);
682 void OnHeaderRightClick( wxDataViewEvent
&event
);
683 void OnSorted( wxDataViewEvent
&event
);
685 void OnContextMenu( wxDataViewEvent
&event
);
687 void OnRightClick( wxMouseEvent
&event
);
688 void OnGoto( wxCommandEvent
&event
);
689 void OnAddMany( wxCommandEvent
&event
);
692 wxDataViewCtrl
* m_musicCtrl
;
693 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
695 wxDataViewCtrl
* m_listCtrl
;
696 wxObjectDataPtr
<MyListModel
> m_list_model
;
698 wxDataViewColumn
* m_col
;
704 DECLARE_EVENT_TABLE()
707 // -------------------------------------
709 // -------------------------------------
713 bool MyApp::OnInit(void)
715 if ( !wxApp::OnInit() )
718 // build the first frame
720 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
733 // -------------------------------------
735 // -------------------------------------
740 ID_ABOUT
= wxID_ABOUT
,
746 ID_DELETE_MUSIC
= 101,
747 ID_DELETE_YEAR
= 102,
749 ID_PREPEND_LIST
= 200,
750 ID_DELETE_LIST
= 201,
755 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
756 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
757 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
758 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
759 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
760 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
761 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
762 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
763 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
764 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
766 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
768 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
769 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
770 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
771 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
772 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
773 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
775 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
776 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
778 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
779 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
780 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
782 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
784 EVT_RIGHT_UP(MyFrame::OnRightClick
)
787 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
788 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
793 SetIcon(wxICON(sample
));
797 wxMenu
*file_menu
= new wxMenu
;
798 file_menu
->Append(ID_ABOUT
, wxT("&About"));
799 file_menu
->AppendSeparator();
800 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
802 wxMenuBar
*menu_bar
= new wxMenuBar
;
803 menu_bar
->Append(file_menu
, wxT("&File"));
805 SetMenuBar(menu_bar
);
808 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
810 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
814 m_musicCtrl
= new wxDataViewCtrl( this, ID_MUSIC_CTRL
, wxDefaultPosition
,
815 wxDefaultSize
, wxDV_MULTIPLE
|wxDV_VARIABLE_LINE_HEIGHT
);
817 m_music_model
= new MyMusicModel
;
818 m_musicCtrl
->AssociateModel( m_music_model
.get() );
820 wxDataViewTextRenderer
*tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
821 wxDataViewColumn
*column0
= new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
822 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
823 m_musicCtrl
->AppendColumn( column0
);
825 // Call this and sorting is enabled
826 // immediatly upon start up.
827 column0
->SetSortOrder( true );
830 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
831 wxDataViewColumn
*column1
= new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_RIGHT
,
832 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
833 m_musicCtrl
->AppendColumn( column1
);
835 wxDataViewSpinRenderer
*sr
= new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
836 wxDataViewColumn
*column2
= new wxDataViewColumn( wxT("year"), sr
, 2, 80, wxALIGN_LEFT
,
837 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
838 m_musicCtrl
->AppendColumn( column2
);
840 m_musicCtrl
->AppendProgressColumn( wxT("popularity"), 3, wxDATAVIEW_CELL_INERT
, 80 );
842 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
843 wxDataViewColumn
*column3
= new wxDataViewColumn( wxT("custom"), cr
, 4, -1, wxALIGN_LEFT
,
844 wxDATAVIEW_COL_RESIZABLE
);
845 m_musicCtrl
->AppendColumn( column3
);
847 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
853 m_listCtrl
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
854 wxDefaultSize
, wxDV_MULTIPLE
| wxDV_ROW_LINES
);
856 m_list_model
= new MyListModel
;
857 m_listCtrl
->AssociateModel( m_list_model
.get() );
860 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
861 m_listCtrl
->AppendIconTextColumn(wxIcon(small1_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
863 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
864 m_listCtrl
->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
867 wxDataViewTextRendererAttr
*ra
= new wxDataViewTextRendererAttr
;
868 wxDataViewColumn
*column4
= new wxDataViewColumn(wxT("attributes"), ra
, 2 );
869 m_listCtrl
->AppendColumn( column4
);
871 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
875 main_sizer
->Add( data_sizer
, 2, wxGROW
);
877 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
879 button_sizer
->Add( new wxButton( this, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
880 button_sizer
->Add( new wxButton( this, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
881 button_sizer
->Add( new wxButton( this, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
882 button_sizer
->Add( 10, 10, 1 );
883 wxFlexGridSizer
*grid_sizer
= new wxFlexGridSizer( 2, 2 );
884 grid_sizer
->Add( new wxButton( this, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 2 );
885 grid_sizer
->Add( new wxButton( this, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 2 );
886 grid_sizer
->Add( new wxButton( this, ID_GOTO
, _("Goto 50")), 0, wxALL
, 2 );
887 grid_sizer
->Add( new wxButton( this, ID_ADD_MANY
, _("Add 1000")), 0, wxALL
, 2 );
888 button_sizer
->Add( grid_sizer
, 0, wxALL
, 10 );
890 main_sizer
->Add( button_sizer
, 0, wxGROW
, 0 );
892 wxBoxSizer
*bottom_sizer
= new wxBoxSizer( wxHORIZONTAL
);
894 m_log
= new wxTextCtrl( this, -1, wxString(), wxDefaultPosition
, wxSize(100,200), wxTE_MULTILINE
);
895 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
896 wxLogMessage(_("This is the log window"));
898 bottom_sizer
->Add( m_log
, 1, wxGROW
);
900 // wxDataViewTreeStore
902 wxDataViewCtrl
*treectrl
= new wxDataViewCtrl( this, -1,
903 wxDefaultPosition
, wxSize(100,200), wxDV_NO_HEADER
);
905 wxDataViewTreeStore
*store
= new wxDataViewTreeStore
;
906 wxDataViewItem parent
= store
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm
) );
907 wxDataViewItem child
= store
->AppendItem( parent
,wxT("Child 1"), wxIcon(small1_xpm
) );
908 child
= store
->AppendItem( parent
,wxT("Child 2"), wxIcon(small1_xpm
) );
909 child
= store
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), wxIcon(small1_xpm
) );
910 treectrl
->AssociateModel( store
);
913 treectrl
->AppendIconTextColumn( wxT("no label"), 0, wxDATAVIEW_CELL_INERT
, -1, (wxAlignment
) 0,
914 wxDATAVIEW_COL_RESIZABLE
);
916 bottom_sizer
->Add( treectrl
, 1 );
918 // wxDataViewTreeCtrl
920 wxDataViewTreeCtrl
*treectrl2
= new wxDataViewTreeCtrl( this, -1, wxDefaultPosition
, wxSize(100,200) );
922 wxImageList
*ilist
= new wxImageList( 16, 16 );
923 ilist
->Add( wxIcon(small1_xpm
) );
924 treectrl2
->SetImageList( ilist
);
926 parent
= treectrl2
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
927 child
= treectrl2
->AppendItem( parent
,wxT("Child 1"), 0 );
928 child
= treectrl2
->AppendItem( parent
,wxT("Child 2"), 0 );
929 child
= treectrl2
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), 0 );
931 bottom_sizer
->Add( treectrl2
, 1 );
935 main_sizer
->Add( bottom_sizer
, 0, wxGROW
);
937 SetSizer( main_sizer
);
942 delete wxLog::SetActiveTarget(m_logOld
);
945 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
950 void MyFrame::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
952 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
955 void MyFrame::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
957 wxDataViewItemArray items
;
958 int len
= m_musicCtrl
->GetSelections( items
);
959 for( int i
= 0; i
< len
; i
++ )
961 m_music_model
->Delete( items
[i
] );
964 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
966 m_musicCtrl
->DeleteColumn( m_musicCtrl
->GetColumn( 2 ) );
967 FindWindow( ID_DELETE_YEAR
)->Disable();
970 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
972 m_list_model
->Prepend(wxT("Test"));
975 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
977 wxDataViewItemArray items
;
978 int len
= m_listCtrl
->GetSelections( items
);
980 m_list_model
->DeleteItems( items
);
983 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
988 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event
.GetItem().GetID(), event
.GetColumn() );
991 void MyFrame::OnActivated( wxDataViewEvent
&event
)
996 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
997 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
.GetData());
1000 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
1005 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1007 title
= wxT("None");
1009 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
.GetData() );
1012 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
1017 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1018 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
.GetData() );
1022 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
1027 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1028 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
.GetData() );
1031 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
1036 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1037 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
.GetData() );
1040 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
1045 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1046 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
.GetData() );
1049 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1054 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1055 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
.GetData() );
1058 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1063 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1064 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
.GetData());
1067 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1072 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1073 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
.GetData());
1076 menu
.Append( 1, wxT("entry 1") );
1077 menu
.Append( 2, wxT("entry 2") );
1078 menu
.Append( 3, wxT("entry 3") );
1080 m_musicCtrl
->PopupMenu(&menu
);
1082 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1085 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1090 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1092 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
1095 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1100 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1102 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
1105 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1110 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1112 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
1115 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1120 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event
.GetX(), event
.GetY());
1123 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1125 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1126 m_listCtrl
->EnsureVisible(item
,m_col
);
1129 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1131 m_list_model
->AddMany();
1135 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1137 wxAboutDialogInfo info
;
1138 info
.SetName(_("DataView sample"));
1139 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
1140 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));