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 node
->GetParent()->GetChildren().Remove( node
);
170 // override sorting to always sort branches ascendingly
172 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
)
174 if (HasChildren(item1
) && HasChildren(item2
))
176 wxVariant value1
,value2
;
177 GetValue( value1
, item1
, 0 );
178 GetValue( value2
, item2
, 0 );
180 wxString str1
= value1
.GetString();
181 wxString str2
= value2
.GetString();
182 int res
= str1
.Cmp( str2
);
185 // items must be different
186 unsigned long litem1
= (unsigned long) item1
.GetID();
187 unsigned long litem2
= (unsigned long) item2
.GetID();
189 return litem1
-litem2
;
192 return wxDataViewModel::Compare( item1
, item2
);
195 // implementation of base class virtuals to define model
197 virtual unsigned int GetColumnCount() const
202 virtual wxString
GetColumnType( unsigned int col
) const
207 virtual void GetValue( wxVariant
&variant
,
208 const wxDataViewItem
&item
, unsigned int col
) const
210 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
213 case 0: variant
= node
->m_title
; break;
214 case 1: variant
= node
->m_artist
; break;
215 case 2: variant
= node
->m_year
; break;
216 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
220 virtual bool SetValue( const wxVariant
&variant
,
221 const wxDataViewItem
&item
, unsigned int col
)
223 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
226 case 0: node
->m_title
= variant
.GetString(); break;
227 case 1: node
->m_artist
= variant
.GetString(); break;
228 case 2: node
->m_year
= variant
.GetString(); break;
229 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
233 virtual bool HasChildren( const wxDataViewItem
&item
) const
235 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
239 return node
->IsContainer();
242 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
244 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
246 return wxDataViewItem( (void*) m_root
);
248 if (node
->GetChildCount() == 0)
249 return wxDataViewItem( 0 );
251 if (node
== m_classical
)
253 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
254 model
->m_classicalMusicIsKnownToControl
= true;
257 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
258 return wxDataViewItem( (void*) first_child
);
261 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
263 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
264 MyMusicModelNode
*parent
= node
->GetParent();
266 return wxDataViewItem(0);
268 int pos
= parent
->GetChildren().Index( node
);
269 if (pos
== wxNOT_FOUND
)
270 return wxDataViewItem(0);
272 if (pos
== parent
->GetChildCount()-1)
273 return wxDataViewItem(0);
275 node
= parent
->GetChildren().Item( pos
+1 );
276 return wxDataViewItem( (void*) node
);
280 MyMusicModelNode
* m_root
;
281 MyMusicModelNode
* m_pop
;
282 MyMusicModelNode
* m_classical
;
283 bool m_classicalMusicIsKnownToControl
;
286 // -------------------------------------
288 // -------------------------------------
290 class MyApp
: public wxApp
297 // -------------------------------------
299 // -------------------------------------
301 class MyFrame
: public wxFrame
304 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
307 void OnQuit(wxCommandEvent
& event
);
308 void OnAbout(wxCommandEvent
& event
);
309 void OnAdd(wxCommandEvent
& event
);
310 void OnDelete(wxCommandEvent
& event
);
313 wxDataViewCtrl
* m_dataview
;
315 wxObjectDataPtr
<MyMusicModel
> m_model
;
318 DECLARE_EVENT_TABLE()
321 // -------------------------------------
323 // -------------------------------------
327 bool MyApp::OnInit(void)
329 if ( !wxApp::OnInit() )
332 // build the first frame
334 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
347 // -------------------------------------
349 // -------------------------------------
354 ID_ABOUT
= wxID_ABOUT
,
361 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
362 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
363 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
364 EVT_BUTTON( ID_ADD
, MyFrame::OnAdd
)
365 EVT_BUTTON( ID_DELETE
, MyFrame::OnDelete
)
368 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
369 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
371 SetIcon(wxICON(sample
));
375 wxMenu
*file_menu
= new wxMenu
;
376 file_menu
->Append(ID_ABOUT
, "&About");
377 file_menu
->AppendSeparator();
378 file_menu
->Append(ID_EXIT
, "E&xit");
380 wxMenuBar
*menu_bar
= new wxMenuBar
;
381 menu_bar
->Append(file_menu
, "&File");
383 SetMenuBar(menu_bar
);
386 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
388 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
391 m_model
= new MyMusicModel
;
392 m_dataview
->AssociateModel( m_model
.get() );
394 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
395 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
396 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE
, 200,
397 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
398 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
401 main_sizer
->Add( m_dataview
, 2, wxGROW
);
403 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
405 button_sizer
->Add( new wxButton( this, ID_ADD
, "Add Mozart"), 0, wxALL
, 10 );
406 button_sizer
->Add( new wxButton( this, ID_DELETE
, "Delete selected"), 0, wxALL
, 10 );
408 main_sizer
->Add( button_sizer
, 0, 0, 0 );
410 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
412 main_sizer
->Add( m_log
, 1, wxGROW
);
414 SetSizer( main_sizer
);
417 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
422 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
) )
424 wxDataViewItem item
= m_dataview
->GetSelection();
426 m_model
->Delete( item
);
429 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
) )
431 m_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
434 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
436 wxAboutDialogInfo info
;
437 info
.SetName(_("DataView sample"));
438 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
439 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));