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/dataview.h"
24 #include "wx/datetime.h"
25 #include "wx/splitter.h"
26 #include "wx/aboutdlg.h"
27 #include "wx/choicdlg.h"
28 #include "wx/numdlg.h"
29 #include "wx/spinctrl.h"
30 #include "wx/imaglist.h"
33 #include "../sample.xpm"
39 static const char *small1_xpm
[] = {
40 /* columns rows colors chars-per-pixel */
68 // -------------------------------------
70 // -------------------------------------
73 Implement this data model
74 Title Artist Year Judgement
75 --------------------------------------------------------------------------
78 3: You are not alone Michael Jackson 1995 good
79 4: Take a bow Madonna 1994 good
81 6: Ninth Symphony Ludwig v. Beethoven 1824 good
82 7: German Requiem Johannes Brahms 1868 good
87 class MyMusicModelNode
;
88 WX_DEFINE_ARRAY_PTR( MyMusicModelNode
*, MyMusicModelNodes
);
90 class MyMusicModelNode
93 MyMusicModelNode( MyMusicModelNode
* parent
,
94 const wxString
&title
, const wxString
&artist
, int year
)
101 m_isContainer
= false;
104 MyMusicModelNode( MyMusicModelNode
* parent
,
105 const wxString
&branch
)
110 m_isContainer
= true;
115 size_t count
= m_children
.GetCount();
117 for (i
= 0; i
< count
; i
++)
119 MyMusicModelNode
*child
= m_children
[i
];
124 bool IsContainer() { return m_isContainer
; }
126 MyMusicModelNode
* GetParent() { return m_parent
; }
127 MyMusicModelNodes
&GetChildren() { return m_children
; }
128 MyMusicModelNode
* GetNthChild( unsigned int n
) { return m_children
.Item( n
); }
129 void Insert( MyMusicModelNode
* child
, unsigned int n
) { m_children
.Insert( child
, n
); }
130 void Append( MyMusicModelNode
* child
) { m_children
.Add( child
); }
131 unsigned int GetChildCount() { return m_children
.GetCount(); }
140 MyMusicModelNode
*m_parent
;
141 MyMusicModelNodes m_children
;
146 class MyMusicModel
: public wxDataViewModel
154 m_root
= new MyMusicModelNode( NULL
, wxT("My Music" ));
155 m_pop
= new MyMusicModelNode( m_root
, wxT("Pop music") );
156 m_root
->Append( m_pop
);
157 m_pop
->Append( new MyMusicModelNode( m_pop
,
158 wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
159 m_pop
->Append( new MyMusicModelNode( m_pop
,
160 wxT("Take a bow"), wxT("Madonna"), 1994 ) );
161 m_classical
= new MyMusicModelNode( m_root
, wxT("Classical music") );
162 m_root
->Append( m_classical
);
163 m_ninth
= new MyMusicModelNode( m_classical
,
164 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 );
165 m_classical
->Append( m_ninth
);
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;
277 case 3: variant
= node
->m_quality
; break;
279 // wxMac doesn't conceal the popularity progress renderer, return 0 for containers
280 if (IsContainer(item
))
283 variant
= (long) 80; // all music is very 80% popular
286 // Make size of red square depend on year
287 if (GetYear(item
) < 1900)
294 wxLogError( wxT("MyMusicModel::GetValue: wrong column %d"), col
);
296 // provoke a crash when mouse button down
297 wxMouseState state
= wxGetMouseState();
298 if (state
.ShiftDown())
307 virtual bool SetValue( const wxVariant
&variant
,
308 const wxDataViewItem
&item
, unsigned int col
)
310 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
313 case 0: node
->m_title
= variant
.GetString(); return true;
314 case 1: node
->m_artist
= variant
.GetString(); return true;
315 case 2: node
->m_year
= variant
.GetLong(); return true;
316 case 3: node
->m_quality
= variant
.GetString(); return true;
317 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
322 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
324 // the invisble root node has no parent
326 return wxDataViewItem(0);
328 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
330 // "MyMusic" also has no parent
332 return wxDataViewItem(0);
334 return wxDataViewItem( (void*) node
->GetParent() );
337 virtual bool IsContainer( const wxDataViewItem
&item
) const
339 // the invisble root node can have children (in
340 // our model always "MyMusic")
344 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
345 return node
->IsContainer();
348 virtual unsigned int GetChildren( const wxDataViewItem
&parent
, wxDataViewItemArray
&array
) const
350 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
353 array
.Add( wxDataViewItem( (void*) m_root
) );
357 if (node
== m_classical
)
359 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
360 model
->m_classicalMusicIsKnownToControl
= true;
363 if (node
->GetChildCount() == 0)
368 unsigned int count
= node
->GetChildren().GetCount();
370 for (pos
= 0; pos
< count
; pos
++)
372 MyMusicModelNode
*child
= node
->GetChildren().Item( pos
);
373 array
.Add( wxDataViewItem( (void*) child
) );
378 wxDataViewItem
GetNinthItem()
380 return wxDataViewItem( m_ninth
);
384 MyMusicModelNode
* m_root
;
385 MyMusicModelNode
* m_pop
;
386 MyMusicModelNode
* m_classical
;
387 MyMusicModelNode
* m_ninth
;
388 bool m_classicalMusicIsKnownToControl
;
392 static int my_sort_reverse( int *v1
, int *v2
)
397 static int my_sort( int *v1
, int *v2
)
402 class MyListModel
: public wxDataViewVirtualListModel
407 wxDataViewVirtualListModel( 1000 + 100 )
409 wxDataViewVirtualListModel( 100000 + 100 )
413 m_virtualItems
= 1000;
415 m_virtualItems
= 100000;
419 for (i
= 0; i
< 100; i
++)
422 str
.Printf( wxT("row number %d"), i
);
426 m_icon
= wxIcon( null_xpm
);
429 // helper methods to change the model
431 void Prepend( const wxString
&text
)
433 m_array
.Insert( text
, 0 );
437 void DeleteItem( const wxDataViewItem
&item
)
439 unsigned int row
= GetRow( item
);
440 if (row
>= m_array
.GetCount())
443 m_array
.RemoveAt( row
);
447 void DeleteItems( const wxDataViewItemArray
&items
)
451 for (i
= 0; i
< items
.GetCount(); i
++)
453 unsigned int row
= GetRow( items
[i
] );
454 if (row
< m_array
.GetCount())
458 // Sort in descending order so that the last
459 // row will be deleted first. Otherwise the
460 // remaining indeces would all be wrong.
461 rows
.Sort( my_sort_reverse
);
462 for (i
= 0; i
< rows
.GetCount(); i
++)
463 m_array
.RemoveAt( rows
[i
] );
465 // This is just to test if wxDataViewCtrl can
466 // cope with removing rows not sorted in
468 rows
.Sort( my_sort
);
474 m_virtualItems
+= 1000;
475 Reset( m_array
.GetCount() + m_virtualItems
);
478 // implementation of base class virtuals to define model
480 virtual unsigned int GetColumnCount() const
485 virtual wxString
GetColumnType( unsigned int col
) const
488 return wxT("wxDataViewIconText");
490 return wxT("string");
493 virtual unsigned int GetRowCount()
495 return m_array
.GetCount();
498 virtual void GetValueByRow( wxVariant
&variant
,
499 unsigned int row
, unsigned int col
) const
503 if (row
>= m_array
.GetCount())
506 str
.Printf(wxT("row %d"), row
- m_array
.GetCount() );
511 variant
= m_array
[ row
];
516 wxDataViewIconText
data( wxT("test"), m_icon
);
521 if (row
>= m_array
.GetCount())
522 variant
= wxT("plain");
524 variant
= wxT("blue");
528 virtual bool GetAttrByRow( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
)
533 if (row
< m_array
.GetCount())
535 attr
.SetColour( *wxBLUE
);
536 attr
.SetItalic( true );
542 virtual bool SetValueByRow( const wxVariant
&variant
,
543 unsigned int row
, unsigned int col
)
547 if (row
>= m_array
.GetCount())
550 m_array
[row
] = variant
.GetString();
557 wxArrayString m_array
;
562 // -------------------------------------
564 // -------------------------------------
566 class MyCustomRenderer
: public wxDataViewCustomRenderer
569 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
570 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,m_height);
599 return wxSize(60,20);
602 virtual bool SetValue( const wxVariant
&value
)
608 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
614 // -------------------------------------
616 // -------------------------------------
618 class MyApp
: public wxApp
625 // -------------------------------------
627 // -------------------------------------
629 class MyFrame
: public wxFrame
632 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
636 void OnQuit(wxCommandEvent
& event
);
637 void OnAbout(wxCommandEvent
& event
);
639 void OnAddMozart(wxCommandEvent
& event
);
640 void OnDeleteMusic(wxCommandEvent
& event
);
641 void OnDeleteYear(wxCommandEvent
& event
);
642 void OnSelectNinth(wxCommandEvent
& event
);
644 void OnPrependList(wxCommandEvent
& event
);
645 void OnDeleteList(wxCommandEvent
& event
);
647 void OnValueChanged( wxDataViewEvent
&event
);
649 void OnActivated( wxDataViewEvent
&event
);
650 void OnExpanding( wxDataViewEvent
&event
);
651 void OnExpanded( wxDataViewEvent
&event
);
652 void OnCollapsing( wxDataViewEvent
&event
);
653 void OnCollapsed( wxDataViewEvent
&event
);
654 void OnSelectionChanged( wxDataViewEvent
&event
);
656 void OnEditingStarted( wxDataViewEvent
&event
);
657 void OnEditingDone( wxDataViewEvent
&event
);
659 void OnHeaderClick( wxDataViewEvent
&event
);
660 void OnHeaderRightClick( wxDataViewEvent
&event
);
661 void OnSorted( wxDataViewEvent
&event
);
663 void OnContextMenu( wxDataViewEvent
&event
);
665 void OnRightClick( wxMouseEvent
&event
);
666 void OnGoto( wxCommandEvent
&event
);
667 void OnAddMany( wxCommandEvent
&event
);
669 void OnBeginDrag( wxDataViewEvent
&event
);
670 void OnDropPossible( wxDataViewEvent
&event
);
671 void OnDrop( wxDataViewEvent
&event
);
674 wxDataViewCtrl
* m_musicCtrl
;
675 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
677 wxDataViewCtrl
* m_listCtrl
;
678 wxObjectDataPtr
<MyListModel
> m_list_model
;
680 wxDataViewColumn
* m_col
;
686 DECLARE_EVENT_TABLE()
689 // -------------------------------------
691 // -------------------------------------
695 bool MyApp::OnInit(void)
697 if ( !wxApp::OnInit() )
700 // build the first frame
702 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
715 // -------------------------------------
717 // -------------------------------------
722 ID_ABOUT
= wxID_ABOUT
,
728 ID_DELETE_MUSIC
= 101,
729 ID_DELETE_YEAR
= 102,
730 ID_SELECT_NINTH
= 103,
732 ID_PREPEND_LIST
= 200,
733 ID_DELETE_LIST
= 201,
738 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
739 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
740 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
741 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
742 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
743 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
744 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
745 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
746 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
747 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
748 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
750 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
752 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
753 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
754 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
755 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
756 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
757 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
759 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
760 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
762 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
763 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
764 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
766 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
768 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
769 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
770 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
772 EVT_RIGHT_UP(MyFrame::OnRightClick
)
775 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
776 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
781 SetIcon(wxICON(sample
));
785 wxMenu
*file_menu
= new wxMenu
;
786 file_menu
->Append(ID_ABOUT
, wxT("&About"));
787 file_menu
->AppendSeparator();
788 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
790 wxMenuBar
*menu_bar
= new wxMenuBar
;
791 menu_bar
->Append(file_menu
, wxT("&File"));
793 SetMenuBar(menu_bar
);
796 wxPanel
*panel
= new wxPanel( this, -1 );
798 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
800 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
804 m_musicCtrl
= new wxDataViewCtrl( panel
, ID_MUSIC_CTRL
, wxDefaultPosition
,
805 wxSize(400,200), wxDV_MULTIPLE
|wxDV_VARIABLE_LINE_HEIGHT
);
807 m_music_model
= new MyMusicModel
;
808 m_musicCtrl
->AssociateModel( m_music_model
.get() );
810 m_musicCtrl
->EnableDragSource( wxDF_UNICODETEXT
);
811 m_musicCtrl
->EnableDropTarget( wxDF_UNICODETEXT
);
813 wxDataViewTextRenderer
*tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
814 wxDataViewColumn
*column0
= new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
815 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
816 m_musicCtrl
->AppendColumn( column0
);
818 // Call this and sorting is enabled
819 // immediatly upon start up.
820 column0
->SetAsSortKey();
823 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
824 wxDataViewColumn
*column1
= new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_LEFT
,
825 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
826 column1
->SetMinWidth(150); // this column can't be resized to be smaller
827 m_musicCtrl
->AppendColumn( column1
);
829 wxDataViewSpinRenderer
*sr
= new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
830 wxDataViewColumn
*column2
= new wxDataViewColumn( wxT("year"), sr
, 2, 60, wxALIGN_LEFT
,
831 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
832 m_musicCtrl
->AppendColumn( column2
);
834 wxArrayString choices
;
835 choices
.Add( "good" );
836 choices
.Add( "bad" );
837 choices
.Add( "lousy" );
838 wxDataViewChoiceRenderer
*c
= new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
839 wxDataViewColumn
*column3
= new wxDataViewColumn( wxT("rating"), c
, 3, 100, wxALIGN_LEFT
,
840 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
841 m_musicCtrl
->AppendColumn( column3
);
843 m_musicCtrl
->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT
, 80 );
845 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
846 wxDataViewColumn
*column4
= new wxDataViewColumn( wxT("custom"), cr
, 5, -1, wxALIGN_LEFT
,
847 wxDATAVIEW_COL_RESIZABLE
);
848 m_musicCtrl
->AppendColumn( column4
);
850 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
854 m_listCtrl
= new wxDataViewCtrl( panel
, wxID_ANY
, wxDefaultPosition
,
855 wxSize(400,200), wxDV_MULTIPLE
| wxDV_ROW_LINES
);
857 m_list_model
= new MyListModel
;
858 m_listCtrl
->AssociateModel( m_list_model
.get() );
861 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
862 m_listCtrl
->AppendIconTextColumn(wxIcon(small1_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
864 m_listCtrl
->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
865 m_listCtrl
->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
868 wxDataViewTextRendererAttr
*ra
= new wxDataViewTextRendererAttr
;
869 wxDataViewColumn
*column5
= new wxDataViewColumn(wxT("attributes"), ra
, 2 );
870 m_listCtrl
->AppendColumn( column5
);
872 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
874 main_sizer
->Add( data_sizer
, 2, wxGROW
);
876 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
878 button_sizer
->Add( new wxButton( panel
, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
879 button_sizer
->Add( new wxButton( panel
, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
880 button_sizer
->Add( new wxButton( panel
, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
881 button_sizer
->Add( new wxButton( panel
, ID_SELECT_NINTH
, _("Select Ninth")), 0, wxALL
, 10 );
882 button_sizer
->Add( 10, 10, 1 );
883 wxFlexGridSizer
*grid_sizer
= new wxFlexGridSizer( 2, 2 );
884 grid_sizer
->Add( new wxButton( panel
, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 2 );
885 grid_sizer
->Add( new wxButton( panel
, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 2 );
886 grid_sizer
->Add( new wxButton( panel
, ID_GOTO
, _("Goto 50")), 0, wxALL
, 2 );
887 grid_sizer
->Add( new wxButton( panel
, 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( panel
, -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 // wxDataViewListCtrl
902 wxDataViewListCtrl
*listctrl
= new wxDataViewListCtrl( panel
, -1,
903 wxDefaultPosition
, wxSize(100,200) );
905 listctrl
->AppendToggleColumn( wxT("Toggle") );
906 listctrl
->AppendTextColumn( wxT("Text") );
908 wxVector
<wxVariant
> data
;
909 data
.push_back( true );
910 data
.push_back( "row 1" );
911 listctrl
->AppendItem( data
);
914 data
.push_back( false );
915 data
.push_back( "row 3" );
916 listctrl
->AppendItem( data
);
918 bottom_sizer
->Add( listctrl
, 1 );
921 // wxDataViewTreeCtrl
923 wxDataViewTreeCtrl
*treectrl2
= new wxDataViewTreeCtrl( panel
, -1, wxDefaultPosition
, wxSize(100,200) );
925 wxImageList
*ilist
= new wxImageList( 16, 16 );
926 ilist
->Add( wxIcon(small1_xpm
) );
927 treectrl2
->SetImageList( ilist
);
929 wxDataViewItem parent2
= treectrl2
->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
930 treectrl2
->AppendItem( parent2
, wxT("Child 1"), 0 );
931 treectrl2
->AppendItem( parent2
, wxT("Child 2"), 0 );
932 treectrl2
->AppendItem( parent2
, wxT("Child 3, very long, long, long, long"), 0 );
934 bottom_sizer
->Add( treectrl2
, 1 );
938 main_sizer
->Add( bottom_sizer
, 0, wxGROW
);
940 panel
->SetSizer( main_sizer
);
945 delete wxLog::SetActiveTarget(m_logOld
);
948 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
953 void MyFrame::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
955 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
958 void MyFrame::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
960 wxDataViewItemArray items
;
961 int len
= m_musicCtrl
->GetSelections( items
);
962 for( int i
= 0; i
< len
; i
++ )
964 m_music_model
->Delete( items
[i
] );
967 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
969 m_musicCtrl
->DeleteColumn( m_musicCtrl
->GetColumn( 2 ) );
970 FindWindow( ID_DELETE_YEAR
)->Disable();
973 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
975 m_musicCtrl
->Select( m_music_model
->GetNinthItem() );
978 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
980 m_list_model
->Prepend(wxT("Test"));
983 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
985 wxDataViewItemArray items
;
986 int len
= m_listCtrl
->GetSelections( items
);
988 m_list_model
->DeleteItems( items
);
991 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
996 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event
.GetItem().GetID(), event
.GetColumn() );
999 void MyFrame::OnActivated( wxDataViewEvent
&event
)
1004 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1005 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
);
1007 if (m_musicCtrl
->IsExpanded( event
.GetItem() ))
1008 wxLogMessage(wxT("Item: %s is expanded"), title
);
1011 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
1016 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1018 title
= wxT("None");
1020 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
);
1023 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
1028 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1029 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
);
1033 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
1038 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1039 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
);
1042 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
1047 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1048 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
);
1051 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
1056 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1057 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
);
1060 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1065 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1066 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
);
1069 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1074 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1075 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
);
1078 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1083 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1084 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
);
1087 menu
.Append( 1, wxT("entry 1") );
1088 menu
.Append( 2, wxT("entry 2") );
1089 menu
.Append( 3, wxT("entry 3") );
1091 m_musicCtrl
->PopupMenu(&menu
);
1093 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1096 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1098 // we need to skip the event to let the default behaviour of sorting by
1099 // this column when it is clicked to take place
1105 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1107 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
1110 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1115 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1117 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
1120 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1125 int pos
= m_musicCtrl
->GetColumnPosition( event
.GetDataViewColumn() );
1127 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
1130 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1135 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event
.GetX(), event
.GetY());
1138 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1140 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1141 m_listCtrl
->EnsureVisible(item
,m_col
);
1144 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1146 m_list_model
->AddMany();
1150 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1152 wxAboutDialogInfo info
;
1153 info
.SetName(_("DataView sample"));
1154 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
1155 info
.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
1160 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
1162 wxDataViewItem
item( event
.GetItem() );
1164 // only allow drags for item, not containers
1165 if (m_music_model
->IsContainer( item
) )
1171 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
1172 wxTextDataObject
*obj
= new wxTextDataObject
;
1173 obj
->SetText( node
->m_title
);
1174 event
.SetDataObject( obj
);
1177 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
1179 wxDataViewItem
item( event
.GetItem() );
1181 // only allow drags for item, not containers
1182 if (m_music_model
->IsContainer( item
) )
1185 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
1189 void MyFrame::OnDrop( wxDataViewEvent
&event
)
1191 wxDataViewItem
item( event
.GetItem() );
1193 // only allow drops for item, not containers
1194 if (m_music_model
->IsContainer( item
) )
1200 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
1206 wxTextDataObject obj
;
1207 obj
.SetData( wxDF_TEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
1209 wxLogMessage(wxT("Text dropped: %s"), obj
.GetText() );