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
77 Title Artist Year Judgement
78 --------------------------------------------------------------------------
81 3: You are not alone Michael Jackson 1995 good
82 4: Take a bow Madonna 1994 good
84 6: Ninth Symphony Ludwig v. Beethoven 1824 good
85 7: German Requiem Johannes Brahms 1868 good
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
)
104 m_isContainer
= false;
107 MyMusicModelNode( MyMusicModelNode
* parent
,
108 const wxString
&branch
)
113 m_isContainer
= true;
118 size_t count
= m_children
.GetCount();
120 for (i
= 0; i
< count
; i
++)
122 MyMusicModelNode
*child
= m_children
[i
];
127 bool IsContainer() { return m_isContainer
; }
129 MyMusicModelNode
* GetParent() { return m_parent
; }
130 MyMusicModelNodes
&GetChildren() { return m_children
; }
131 MyMusicModelNode
* GetNthChild( unsigned int n
) { return m_children
.Item( n
); }
132 void Insert( MyMusicModelNode
* child
, unsigned int n
) { m_children
.Insert( child
, n
); }
133 void Append( MyMusicModelNode
* child
) { m_children
.Add( child
); }
134 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_ninth
= new MyMusicModelNode( m_classical
,
167 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 );
168 m_classical
->Append( m_ninth
);
169 m_classical
->Append( new MyMusicModelNode( m_classical
,
170 wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
171 m_classicalMusicIsKnownToControl
= false;
179 // helper method for wxLog
181 wxString
GetTitle( const wxDataViewItem
&item
) const
183 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
185 return wxEmptyString
;
187 return node
->m_title
;
190 int GetYear( const wxDataViewItem
&item
) const
192 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
199 // helper methods to change the model
201 void AddToClassical( const wxString
&title
, const wxString
&artist
, int year
)
204 MyMusicModelNode
*child_node
=
205 new MyMusicModelNode( m_classical
, title
, artist
, year
);
207 m_classical
->Append( child_node
);
209 if (m_classicalMusicIsKnownToControl
)
212 wxDataViewItem
child( (void*) child_node
);
213 wxDataViewItem
parent( (void*) m_classical
);
214 ItemAdded( parent
, child
);
218 void Delete( const wxDataViewItem
&item
)
220 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
221 wxDataViewItem
parent( node
->GetParent() );
223 node
->GetParent()->GetChildren().Remove( node
);
227 ItemDeleted( parent
, item
);
230 // override sorting to always sort branches ascendingly
232 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
233 unsigned int column
, bool ascending
)
235 if (IsContainer(item1
) && IsContainer(item2
))
237 wxVariant value1
,value2
;
238 GetValue( value1
, item1
, 0 );
239 GetValue( value2
, item2
, 0 );
241 wxString str1
= value1
.GetString();
242 wxString str2
= value2
.GetString();
243 int res
= str1
.Cmp( str2
);
246 // items must be different
247 wxUIntPtr litem1
= (wxUIntPtr
) item1
.GetID();
248 wxUIntPtr litem2
= (wxUIntPtr
) item2
.GetID();
250 return litem1
-litem2
;
253 return wxDataViewModel::Compare( item1
, item2
, column
, ascending
);
256 // implementation of base class virtuals to define model
258 virtual unsigned int GetColumnCount() const
263 virtual wxString
GetColumnType( unsigned int col
) const
268 return wxT("string");
271 virtual void GetValue( wxVariant
&variant
,
272 const wxDataViewItem
&item
, unsigned int col
) const
274 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
277 case 0: variant
= node
->m_title
; break;
278 case 1: variant
= node
->m_artist
; break;
279 case 2: variant
= (long) node
->m_year
; break;
280 case 3: variant
= node
->m_quality
; break;
282 // wxMac doesn't conceal the popularity progress renderer, return 0 for containers
283 if (IsContainer(item
))
286 variant
= (long) 80; // all music is very 80% popular
289 // Make size of red square depend on year
290 if (GetYear(item
) < 1900)
297 wxLogError( wxT("MyMusicModel::GetValue: wrong column %d"), col
);
299 // provoke a crash when mouse button down
300 wxMouseState state
= wxGetMouseState();
301 if (state
.ShiftDown())
310 virtual bool SetValue( const wxVariant
&variant
,
311 const wxDataViewItem
&item
, unsigned int col
)
313 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
316 case 0: node
->m_title
= variant
.GetString(); return true;
317 case 1: node
->m_artist
= variant
.GetString(); return true;
318 case 2: node
->m_year
= variant
.GetLong(); return true;
319 case 3: node
->m_quality
= variant
.GetString(); return true;
320 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
325 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
327 // the invisble root node has no parent
329 return wxDataViewItem(0);
331 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
333 // "MyMusic" also has no parent
335 return wxDataViewItem(0);
337 return wxDataViewItem( (void*) node
->GetParent() );
340 virtual bool IsContainer( const wxDataViewItem
&item
) const
342 // the invisble root node can have children (in
343 // our model always "MyMusic")
347 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
348 return node
->IsContainer();
351 virtual unsigned int GetChildren( const wxDataViewItem
&parent
, wxDataViewItemArray
&array
) const
353 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
356 array
.Add( wxDataViewItem( (void*) m_root
) );
360 if (node
== m_classical
)
362 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
363 model
->m_classicalMusicIsKnownToControl
= true;
366 if (node
->GetChildCount() == 0)
371 unsigned int count
= node
->GetChildren().GetCount();
373 for (pos
= 0; pos
< count
; pos
++)
375 MyMusicModelNode
*child
= node
->GetChildren().Item( pos
);
376 array
.Add( wxDataViewItem( (void*) child
) );
383 virtual bool IsDraggable( const wxDataViewItem
&item
)
386 return (!IsContainer(item
));
389 virtual size_t GetDragDataSize( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
) )
391 wxPrintf( "GetDragDataSize\n" );
393 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
395 data
+= node
->m_title
; data
+= wxT(" ");
396 data
+= node
->m_artist
;
397 return strlen( data
.utf8_str() ) + 1;
399 virtual bool GetDragData( const wxDataViewItem
&item
, const wxDataFormat
&WXUNUSED(format
),
400 void* dest
, size_t WXUNUSED(size
) )
402 wxPrintf( "GetDragData\n" );
404 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
406 data
+= node
->m_title
; data
+= wxT(" ");
407 data
+= node
->m_artist
;
408 wxCharBuffer
buffer( data
.utf8_str() );
409 memcpy( dest
, buffer
, strlen(buffer
)+1 );
413 wxDataViewItem
GetNinthItem()
415 return wxDataViewItem( m_ninth
);
419 MyMusicModelNode
* m_root
;
420 MyMusicModelNode
* m_pop
;
421 MyMusicModelNode
* m_classical
;
422 MyMusicModelNode
* m_ninth
;
423 bool m_classicalMusicIsKnownToControl
;
427 static int my_sort_reverse( int *v1
, int *v2
)
432 static int my_sort( int *v1
, int *v2
)
437 class MyListModel
: public wxDataViewVirtualListModel
442 wxDataViewVirtualListModel( 1000 + 100 )
444 wxDataViewVirtualListModel( 100000 + 100 )
448 m_virtualItems
= 1000;
450 m_virtualItems
= 100000;
454 for (i
= 0; i
< 100; i
++)
457 str
.Printf( wxT("row number %d"), i
);
461 m_icon
= wxIcon( null_xpm
);
464 // helper methods to change the model
466 void Prepend( const wxString
&text
)
468 m_array
.Insert( text
, 0 );
472 void DeleteItem( const wxDataViewItem
&item
)
474 unsigned int row
= GetRow( item
);
475 if (row
>= m_array
.GetCount())
478 m_array
.RemoveAt( row
);
482 void DeleteItems( const wxDataViewItemArray
&items
)
486 for (i
= 0; i
< items
.GetCount(); i
++)
488 unsigned int row
= GetRow( items
[i
] );
489 if (row
< m_array
.GetCount())
493 // Sort in descending order so that the last
494 // row will be deleted first. Otherwise the
495 // remaining indeces would all be wrong.
496 rows
.Sort( my_sort_reverse
);
497 for (i
= 0; i
< rows
.GetCount(); i
++)
498 m_array
.RemoveAt( rows
[i
] );
500 // This is just to test if wxDataViewCtrl can
501 // cope with removing rows not sorted in
503 rows
.Sort( my_sort
);
509 m_virtualItems
+= 1000;
510 Reset( m_array
.GetCount() + m_virtualItems
);
513 // implementation of base class virtuals to define model
515 virtual unsigned int GetColumnCount() const
520 virtual wxString
GetColumnType( unsigned int col
) const
523 return wxT("wxDataViewIconText");
525 return wxT("string");
528 virtual unsigned int GetRowCount()
530 return m_array
.GetCount();
533 virtual void GetValue( wxVariant
&variant
,
534 unsigned int row
, unsigned int col
) const
538 if (row
>= m_array
.GetCount())
541 str
.Printf(wxT("row %d"), row
- m_array
.GetCount() );
546 variant
= m_array
[ row
];
551 wxDataViewIconText
data( wxT("test"), m_icon
);
556 if (row
>= m_array
.GetCount())
557 variant
= wxT("plain");
559 variant
= wxT("blue");
563 virtual bool GetAttr( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
)
568 if (row
< m_array
.GetCount())
570 attr
.SetColour( *wxBLUE
);
571 attr
.SetItalic( true );
577 virtual bool SetValue( const wxVariant
&variant
,
578 unsigned int row
, unsigned int col
)
582 if (row
>= m_array
.GetCount())
585 m_array
[row
] = variant
.GetString();
592 wxArrayString m_array
;
597 // -------------------------------------
599 // -------------------------------------
601 class MyCustomRenderer
: public wxDataViewCustomRenderer
604 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
605 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
608 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
610 dc
->SetBrush( *wxRED_BRUSH
);
611 dc
->SetPen( *wxTRANSPARENT_PEN
);
612 dc
->DrawRectangle( rect
);
617 virtual bool Activate( wxRect
WXUNUSED(cell
),
618 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
620 wxLogMessage( wxT("MyCustomRenderer Activate()") );
624 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
625 wxDataViewModel
*WXUNUSED(model
), const wxDataViewItem
&WXUNUSED(item
), unsigned int WXUNUSED(col
) )
627 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor
.x
, cursor
.y
);
631 virtual wxSize
GetSize() const
633 //return wxSize(60,m_height);
634 return wxSize(60,20);
637 virtual bool SetValue( const wxVariant
&value
)
643 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
649 // -------------------------------------
651 // -------------------------------------
653 class MyApp
: public wxApp
660 // -------------------------------------
662 // -------------------------------------
664 class MyFrame
: public wxFrame
667 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
671 void OnQuit(wxCommandEvent
& event
);
672 void OnAbout(wxCommandEvent
& event
);
674 void OnAddMozart(wxCommandEvent
& event
);
675 void OnDeleteMusic(wxCommandEvent
& event
);
676 void OnDeleteYear(wxCommandEvent
& event
);
677 void OnSelectNinth(wxCommandEvent
& event
);
679 void OnPrependList(wxCommandEvent
& event
);
680 void OnDeleteList(wxCommandEvent
& event
);
682 void OnValueChanged( wxDataViewEvent
&event
);
684 void OnActivated( wxDataViewEvent
&event
);
685 void OnExpanding( wxDataViewEvent
&event
);
686 void OnExpanded( wxDataViewEvent
&event
);
687 void OnCollapsing( wxDataViewEvent
&event
);
688 void OnCollapsed( wxDataViewEvent
&event
);
689 void OnSelectionChanged( wxDataViewEvent
&event
);
691 void OnEditingStarted( wxDataViewEvent
&event
);
692 void OnEditingDone( wxDataViewEvent
&event
);
694 void OnHeaderClick( wxDataViewEvent
&event
);
695 void OnHeaderRightClick( wxDataViewEvent
&event
);
696 void OnSorted( wxDataViewEvent
&event
);
698 void OnContextMenu( wxDataViewEvent
&event
);
700 void OnRightClick( wxMouseEvent
&event
);
701 void OnGoto( wxCommandEvent
&event
);
702 void OnAddMany( wxCommandEvent
&event
);
705 wxDataViewCtrl
* m_musicCtrl
;
706 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
708 wxDataViewCtrl
* m_listCtrl
;
709 wxObjectDataPtr
<MyListModel
> m_list_model
;
711 wxDataViewColumn
* m_col
;
717 DECLARE_EVENT_TABLE()
720 // -------------------------------------
722 // -------------------------------------
726 bool MyApp::OnInit(void)
728 if ( !wxApp::OnInit() )
731 // build the first frame
733 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
746 // -------------------------------------
748 // -------------------------------------
753 ID_ABOUT
= wxID_ABOUT
,
759 ID_DELETE_MUSIC
= 101,
760 ID_DELETE_YEAR
= 102,
761 ID_SELECT_NINTH
= 103,
763 ID_PREPEND_LIST
= 200,
764 ID_DELETE_LIST
= 201,
769 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
770 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
771 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
772 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
773 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
774 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
775 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
776 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
777 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
778 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
779 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
781 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
783 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
784 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
785 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
786 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
787 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
788 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
790 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
791 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
793 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
794 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
795 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
797 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
799 EVT_RIGHT_UP(MyFrame::OnRightClick
)
802 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
803 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
808 SetIcon(wxICON(sample
));
812 wxMenu
*file_menu
= new wxMenu
;
813 file_menu
->Append(ID_ABOUT
, wxT("&About"));
814 file_menu
->AppendSeparator();
815 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
817 wxMenuBar
*menu_bar
= new wxMenuBar
;
818 menu_bar
->Append(file_menu
, wxT("&File"));
820 SetMenuBar(menu_bar
);
823 wxPanel
*panel
= new wxPanel( this, -1 );
825 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
827 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
831 m_musicCtrl
= new wxDataViewCtrl( panel
, ID_MUSIC_CTRL
, wxDefaultPosition
,
832 wxSize(400,200), wxDV_MULTIPLE
|wxDV_VARIABLE_LINE_HEIGHT
);
834 m_music_model
= new MyMusicModel
;
835 m_musicCtrl
->AssociateModel( m_music_model
.get() );
837 wxDataViewTextRenderer
*tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
838 wxDataViewColumn
*column0
= new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
839 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
840 m_musicCtrl
->AppendColumn( column0
);
842 // Call this and sorting is enabled
843 // immediatly upon start up.
844 column0
->SetAsSortKey();
847 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
848 wxDataViewColumn
*column1
= new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_LEFT
,
849 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
850 column1
->SetMinWidth(150); // this column can't be resized to be smaller
851 m_musicCtrl
->AppendColumn( column1
);
853 wxDataViewSpinRenderer
*sr
= new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
854 wxDataViewColumn
*column2
= new wxDataViewColumn( wxT("year"), sr
, 2, 60, wxALIGN_LEFT
,
855 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
856 m_musicCtrl
->AppendColumn( column2
);
858 wxArrayString choices
;
859 choices
.Add( "good" );
860 choices
.Add( "bad" );
861 choices
.Add( "lousy" );
862 wxDataViewChoiceRenderer
*c
= new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
863 wxDataViewColumn
*column3
= new wxDataViewColumn( wxT("rating"), c
, 3, 100, wxALIGN_LEFT
,
864 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
865 m_musicCtrl
->AppendColumn( column3
);
867 m_musicCtrl
->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT
, 80 );
869 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
870 wxDataViewColumn
*column4
= new wxDataViewColumn( wxT("custom"), cr
, 5, -1, wxALIGN_LEFT
,
871 wxDATAVIEW_COL_RESIZABLE
);
872 m_musicCtrl
->AppendColumn( column4
);
874 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
878 m_listCtrl
= new wxDataViewCtrl( panel
, wxID_ANY
, wxDefaultPosition
,
879 wxSize(400,200), wxDV_MULTIPLE
| wxDV_ROW_LINES
);
881 m_list_model
= new MyListModel
;
882 m_listCtrl
->AssociateModel( m_list_model
.get() );
885 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
886 m_listCtrl
->AppendIconTextColumn(wxIcon(small1_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
888 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
889 m_listCtrl
->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
892 wxDataViewTextRendererAttr
*ra
= new wxDataViewTextRendererAttr
;
893 wxDataViewColumn
*column5
= new wxDataViewColumn(wxT("attributes"), ra
, 2 );
894 m_listCtrl
->AppendColumn( column5
);
896 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
898 main_sizer
->Add( data_sizer
, 2, wxGROW
);
900 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
902 button_sizer
->Add( new wxButton( panel
, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
903 button_sizer
->Add( new wxButton( panel
, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
904 button_sizer
->Add( new wxButton( panel
, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
905 button_sizer
->Add( new wxButton( panel
, ID_SELECT_NINTH
, _("Select Ninth")), 0, wxALL
, 10 );
906 button_sizer
->Add( 10, 10, 1 );
907 wxFlexGridSizer
*grid_sizer
= new wxFlexGridSizer( 2, 2 );
908 grid_sizer
->Add( new wxButton( panel
, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 2 );
909 grid_sizer
->Add( new wxButton( panel
, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 2 );
910 grid_sizer
->Add( new wxButton( panel
, ID_GOTO
, _("Goto 50")), 0, wxALL
, 2 );
911 grid_sizer
->Add( new wxButton( panel
, ID_ADD_MANY
, _("Add 1000")), 0, wxALL
, 2 );
912 button_sizer
->Add( grid_sizer
, 0, wxALL
, 10 );
914 main_sizer
->Add( button_sizer
, 0, wxGROW
, 0 );
916 wxBoxSizer
*bottom_sizer
= new wxBoxSizer( wxHORIZONTAL
);
918 m_log
= new wxTextCtrl( panel
, -1, wxString(), wxDefaultPosition
, wxSize(100,200), wxTE_MULTILINE
);
919 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
920 wxLogMessage(_("This is the log window"));
922 bottom_sizer
->Add( m_log
, 1, wxGROW
);
925 // wxDataViewTreeStore
927 wxDataViewCtrl
*treectrl
= new wxDataViewCtrl( panel
, -1,
928 wxDefaultPosition
, wxSize(100,200), wxDV_NO_HEADER
);
930 wxDataViewTreeStore
*store
= new wxDataViewTreeStore
;
931 wxDataViewItem parent
= store
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm
) );
932 wxDataViewItem child
= store
->AppendItem( parent
,wxT("Child 1"), wxIcon(small1_xpm
) );
933 child
= store
->AppendItem( parent
,wxT("Child 2"), wxIcon(small1_xpm
) );
934 child
= store
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), wxIcon(small1_xpm
) );
935 treectrl
->AssociateModel( store
);
938 treectrl
->AppendIconTextColumn( wxT("no label"), 0, wxDATAVIEW_CELL_INERT
, -1, (wxAlignment
) 0,
939 wxDATAVIEW_COL_RESIZABLE
);
941 bottom_sizer
->Add( treectrl
, 1 );
943 // wxDataViewTreeCtrl
945 wxDataViewTreeCtrl
*treectrl2
= new wxDataViewTreeCtrl( panel
, -1, wxDefaultPosition
, wxSize(100,200) );
947 wxImageList
*ilist
= new wxImageList( 16, 16 );
948 ilist
->Add( wxIcon(small1_xpm
) );
949 treectrl2
->SetImageList( ilist
);
951 parent
= treectrl2
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
952 child
= treectrl2
->AppendItem( parent
,wxT("Child 1"), 0 );
953 child
= treectrl2
->AppendItem( parent
,wxT("Child 2"), 0 );
954 child
= treectrl2
->AppendItem( parent
,wxT("Child 3, very long, long, long, long"), 0 );
956 bottom_sizer
->Add( treectrl2
, 1 );
961 main_sizer
->Add( bottom_sizer
, 0, wxGROW
);
963 panel
->SetSizer( main_sizer
);
968 delete wxLog::SetActiveTarget(m_logOld
);
971 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
976 void MyFrame::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
978 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
981 void MyFrame::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
983 wxDataViewItemArray items
;
984 int len
= m_musicCtrl
->GetSelections( items
);
985 for( int i
= 0; i
< len
; i
++ )
987 m_music_model
->Delete( items
[i
] );
990 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
992 m_musicCtrl
->DeleteColumn( m_musicCtrl
->GetColumn( 2 ) );
993 FindWindow( ID_DELETE_YEAR
)->Disable();
996 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
998 m_musicCtrl
->Select( m_music_model
->GetNinthItem() );
1001 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1003 m_list_model
->Prepend(wxT("Test"));
1006 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1008 wxDataViewItemArray items
;
1009 int len
= m_listCtrl
->GetSelections( items
);
1011 m_list_model
->DeleteItems( items
);
1014 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
1019 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event
.GetItem().GetID(), event
.GetColumn() );
1022 void MyFrame::OnActivated( wxDataViewEvent
&event
)
1027 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1028 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
);
1031 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
1036 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1038 title
= wxT("None");
1040 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
);
1043 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
1048 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1049 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
);
1053 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
1058 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1059 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
);
1062 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
1067 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1068 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
);
1071 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
1076 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1077 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
);
1080 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1085 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1086 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
);
1089 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1094 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1095 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
);
1098 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1103 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1104 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
);
1107 menu
.Append( 1, wxT("entry 1") );
1108 menu
.Append( 2, wxT("entry 2") );
1109 menu
.Append( 3, wxT("entry 3") );
1111 m_musicCtrl
->PopupMenu(&menu
);
1113 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1116 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1118 // we need to skip the event to let the default behaviour of sorting by
1119 // this column when it is clicked to take place
1125 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1127 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
1130 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1135 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1137 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
1140 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1145 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1147 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
1150 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1155 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event
.GetX(), event
.GetY());
1158 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1160 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1161 m_listCtrl
->EnsureVisible(item
,m_col
);
1164 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1166 m_list_model
->AddMany();
1170 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1172 wxAboutDialogInfo info
;
1173 info
.SetName(_("DataView sample"));
1174 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
1175 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));