1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
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"
32 #include "../sample.xpm"
38 #define DEFAULT_ALIGN wxALIGN_LEFT
39 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
43 // -------------------------------------
45 // -------------------------------------
48 Implement this data model
50 -------------------------------------------------------------
53 3: You are not alone Michael Jackson 1995
54 4: Take a bow Madonna 1994
56 6: Ninth Symphony Ludwig v. Beethoven 1824
57 7: German Requiem Johannes Brahms 1868
62 class MyMusicModelNode
;
63 WX_DEFINE_ARRAY_PTR( MyMusicModelNode
*, MyMusicModelNodes
);
65 class MyMusicModelNode
68 MyMusicModelNode( MyMusicModelNode
* parent
,
69 const wxString
&title
, const wxString
&artist
, const wxString
&year
)
75 m_isContainer
= false;
78 MyMusicModelNode( MyMusicModelNode
* parent
,
79 const wxString
&branch
)
88 size_t count
= m_children
.GetCount();
90 for (i
= 0; i
< count
; i
++)
92 MyMusicModelNode
*child
= m_children
[i
];
97 bool IsContainer() { return m_isContainer
; }
99 MyMusicModelNode
* GetParent() { return m_parent
; }
100 MyMusicModelNodes
&GetChildren() { return m_children
; }
101 MyMusicModelNode
* GetNthChild( unsigned int n
) { return m_children
.Item( n
); }
102 void Insert( MyMusicModelNode
* child
, unsigned int n
) { m_children
.Insert( child
, n
); }
103 void Append( MyMusicModelNode
* child
) { m_children
.Add( child
); }
104 unsigned int GetChildCount() { return m_children
.GetCount(); }
112 MyMusicModelNode
*m_parent
;
113 MyMusicModelNodes m_children
;
118 class MyMusicModel
: public wxDataViewModel
126 m_root
= new MyMusicModelNode( NULL
, "My Music" );
127 m_pop
= new MyMusicModelNode( m_root
, "Pop music" );
128 m_root
->Append( m_pop
);
129 m_pop
->Append( new MyMusicModelNode( m_pop
,
130 "You are not alone", "Michael Jackson", "1995" ) );
131 m_pop
->Append( new MyMusicModelNode( m_pop
,
132 "Take a bow", "Madonna", "1994" ) );
133 m_classical
= new MyMusicModelNode( m_root
, "Classical music" );
134 m_root
->Append( m_classical
);
135 m_classical
->Append( new MyMusicModelNode( m_classical
,
136 "Ninth symphony", "Ludwig van Beethoven", "1824" ) );
137 m_classical
->Append( new MyMusicModelNode( m_classical
,
138 "German Requiem", "Johannes Brahms", "1868" ) );
139 m_classicalMusicIsKnownToControl
= false;
142 // helper methods to change the model
144 void AddToClassical( const wxString
&title
, const wxString
&artist
, const wxString
&year
)
147 MyMusicModelNode
*child_node
=
148 new MyMusicModelNode( m_classical
, title
, artist
, year
);
149 m_classical
->Append( child_node
);
151 if (m_classicalMusicIsKnownToControl
)
154 wxDataViewItem
child( (void*) child_node
);
155 wxDataViewItem
parent( (void*) m_classical
);
156 ItemAdded( parent
, child
);
160 void Delete( const wxDataViewItem
&item
)
162 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
163 wxDataViewItem
parent( node
->GetParent() );
164 node
->GetParent()->GetChildren().Remove( node
);
168 ItemDeleted( parent
, item
);
171 // override sorting to always sort branches ascendingly
173 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
)
175 if (IsContainer(item1
) && IsContainer(item2
))
177 wxVariant value1
,value2
;
178 GetValue( value1
, item1
, 0 );
179 GetValue( value2
, item2
, 0 );
181 wxString str1
= value1
.GetString();
182 wxString str2
= value2
.GetString();
183 int res
= str1
.Cmp( str2
);
186 // items must be different
187 unsigned long litem1
= (unsigned long) item1
.GetID();
188 unsigned long litem2
= (unsigned long) item2
.GetID();
190 return litem1
-litem2
;
193 return wxDataViewModel::Compare( item1
, item2
);
196 // implementation of base class virtuals to define model
198 virtual unsigned int GetColumnCount() const
203 virtual wxString
GetColumnType( unsigned int col
) const
208 virtual void GetValue( wxVariant
&variant
,
209 const wxDataViewItem
&item
, unsigned int col
) const
211 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
214 case 0: variant
= node
->m_title
; break;
215 case 1: variant
= node
->m_artist
; break;
216 case 2: variant
= node
->m_year
; break;
217 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
221 virtual bool SetValue( const wxVariant
&variant
,
222 const wxDataViewItem
&item
, unsigned int col
)
224 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
227 case 0: node
->m_title
= variant
.GetString(); break;
228 case 1: node
->m_artist
= variant
.GetString(); break;
229 case 2: node
->m_year
= variant
.GetString(); break;
230 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
234 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
236 // the invisble root node has no parent
238 return wxDataViewItem(0);
240 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
242 // "MyMusic" also has no parent
244 return wxDataViewItem(0);
246 return wxDataViewItem( (void*) node
->GetParent() );
249 virtual bool IsContainer( const wxDataViewItem
&item
) const
251 // the invisble root node can have children (in
252 // our model always "MyMusic")
256 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
257 return node
->IsContainer();
260 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
262 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
264 return wxDataViewItem( (void*) m_root
);
266 if (node
->GetChildCount() == 0)
267 return wxDataViewItem( 0 );
269 if (node
== m_classical
)
271 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
272 model
->m_classicalMusicIsKnownToControl
= true;
275 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
276 return wxDataViewItem( (void*) first_child
);
279 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
281 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
283 // "MyMusic" has no siblings in our model
285 return wxDataViewItem(0);
287 MyMusicModelNode
*parent
= node
->GetParent();
288 int pos
= parent
->GetChildren().Index( node
);
290 // Something went wrong
291 if (pos
== wxNOT_FOUND
)
292 return wxDataViewItem(0);
295 if (pos
== parent
->GetChildCount()-1)
296 return wxDataViewItem(0);
298 node
= parent
->GetChildren().Item( pos
+1 );
299 return wxDataViewItem( (void*) node
);
303 MyMusicModelNode
* m_root
;
304 MyMusicModelNode
* m_pop
;
305 MyMusicModelNode
* m_classical
;
306 bool m_classicalMusicIsKnownToControl
;
309 class MyListModel
: public wxDataViewIndexListModel
313 wxDataViewIndexListModel( 100 )
316 for (i
= 0; i
< 100; i
++)
319 str
.Printf( "row number %d", i
);
324 // helper methods to change the model
326 void Prepend( const wxString
&text
)
328 m_array
.Insert( text
, 0 );
332 void DeleteItem( const wxDataViewItem
&item
)
334 unsigned int row
= GetRow( item
);
335 m_array
.RemoveAt( row
);
339 // implementation of base class virtuals to define model
341 virtual unsigned int GetColumnCount() const
346 virtual wxString
GetColumnType( unsigned int col
) const
351 virtual unsigned int GetRowCount()
353 return m_array
.GetCount();
356 virtual void GetValue( wxVariant
&variant
,
357 unsigned int row
, unsigned int col
) const
361 variant
= m_array
[ row
];
366 str
.Printf( "row %d col %d", row
, col
);
371 virtual bool SetValue( const wxVariant
&variant
,
372 unsigned int row
, unsigned int col
)
376 m_array
[row
] = variant
.GetString();
383 wxArrayString m_array
;
386 // -------------------------------------
388 // -------------------------------------
390 class MyApp
: public wxApp
397 // -------------------------------------
399 // -------------------------------------
401 class MyFrame
: public wxFrame
404 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
407 void OnQuit(wxCommandEvent
& event
);
408 void OnAbout(wxCommandEvent
& event
);
410 void OnAddMozart(wxCommandEvent
& event
);
411 void OnDeleteMusic(wxCommandEvent
& event
);
413 void OnPrependList(wxCommandEvent
& event
);
414 void OnDeleteList(wxCommandEvent
& event
);
416 void OnValueChanged( wxDataViewEvent
&event
);
417 void OnItemAdded( wxDataViewEvent
&event
);
418 void OnItemDeleted( wxDataViewEvent
&event
);
421 wxDataViewCtrl
* m_musicCtrl
;
422 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
424 wxDataViewCtrl
* m_listCtrl
;
425 wxObjectDataPtr
<MyListModel
> m_list_model
;
430 DECLARE_EVENT_TABLE()
433 // -------------------------------------
435 // -------------------------------------
439 bool MyApp::OnInit(void)
441 if ( !wxApp::OnInit() )
444 // build the first frame
446 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
459 // -------------------------------------
461 // -------------------------------------
466 ID_ABOUT
= wxID_ABOUT
,
472 ID_DELETE_MUSIC
= 101,
474 ID_PREPEND_LIST
= 200,
478 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
479 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
480 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
481 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
482 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
483 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
484 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
485 EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL
, MyFrame::OnItemAdded
)
486 EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL
, MyFrame::OnItemDeleted
)
487 EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
490 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
491 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
495 SetIcon(wxICON(sample
));
499 wxMenu
*file_menu
= new wxMenu
;
500 file_menu
->Append(ID_ABOUT
, "&About");
501 file_menu
->AppendSeparator();
502 file_menu
->Append(ID_EXIT
, "E&xit");
504 wxMenuBar
*menu_bar
= new wxMenuBar
;
505 menu_bar
->Append(file_menu
, "&File");
507 SetMenuBar(menu_bar
);
510 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
512 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
516 m_musicCtrl
= new wxDataViewCtrl( this, ID_MUSIC_CTRL
, wxDefaultPosition
,
519 m_music_model
= new MyMusicModel
;
520 m_musicCtrl
->AssociateModel( m_music_model
.get() );
522 m_musicCtrl
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
523 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
524 m_musicCtrl
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE
, 200,
525 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
526 m_musicCtrl
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
529 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
535 m_listCtrl
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
538 m_list_model
= new MyListModel
;
539 m_listCtrl
->AssociateModel( m_list_model
.get() );
541 m_listCtrl
->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
542 m_listCtrl
->AppendTextColumn( "index", 1, wxDATAVIEW_CELL_INERT
, 120 );
544 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
548 main_sizer
->Add( data_sizer
, 2, wxGROW
);
550 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
552 button_sizer
->Add( new wxButton( this, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
553 button_sizer
->Add( new wxButton( this, ID_DELETE_MUSIC
, "Delete selected"), 0, wxALL
, 10 );
554 button_sizer
->Add( 10, 10, 1 );
555 button_sizer
->Add( new wxButton( this, ID_PREPEND_LIST
, "Prepend"), 0, wxALL
, 10 );
556 button_sizer
->Add( new wxButton( this, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
558 main_sizer
->Add( button_sizer
, 0, wxGROW
, 0 );
560 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
562 main_sizer
->Add( m_log
, 1, wxGROW
);
564 SetSizer( main_sizer
);
567 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
572 void MyFrame::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
574 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
577 void MyFrame::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
579 wxDataViewItem item
= m_musicCtrl
->GetSelection();
581 m_music_model
->Delete( item
);
584 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
586 m_list_model
->Prepend( "Test" );
589 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
591 wxDataViewItem item
= m_listCtrl
->GetSelection();
593 m_list_model
->DeleteItem( item
);
596 void MyFrame::OnItemAdded( wxDataViewEvent
&event
)
601 m_log
->AppendText( "EVT_DATAVIEW_MODEL_ITEM_ADDED\n" );
604 void MyFrame::OnItemDeleted( wxDataViewEvent
&event
)
609 m_log
->AppendText( "EVT_DATAVIEW_MODEL_ITEM_DELETED\n" );
612 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
617 m_log
->AppendText( "EVT_DATAVIEW_MODEL_VALUE_CHANGED\n" );
620 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
622 wxAboutDialogInfo info
;
623 info
.SetName(_("DataView sample"));
624 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
625 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));