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 (IsContainer(item1
) && IsContainer(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 wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
235 // the invisble root node has no parent
237 return wxDataViewItem(0);
239 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
241 // "MyMusic" also has no parent
243 return wxDataViewItem(0);
245 return wxDataViewItem( (void*) node
->GetParent() );
248 virtual bool IsContainer( const wxDataViewItem
&item
) const
250 // the invisble root node can have children (in
251 // our model always "MyMusic")
255 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
256 return node
->IsContainer();
259 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
261 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
263 return wxDataViewItem( (void*) m_root
);
265 if (node
->GetChildCount() == 0)
266 return wxDataViewItem( 0 );
268 if (node
== m_classical
)
270 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
271 model
->m_classicalMusicIsKnownToControl
= true;
274 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
275 return wxDataViewItem( (void*) first_child
);
278 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
280 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
282 // "MyMusic" has no siblings in our model
284 return wxDataViewItem(0);
286 MyMusicModelNode
*parent
= node
->GetParent();
287 int pos
= parent
->GetChildren().Index( node
);
289 // Something went wrong
290 if (pos
== wxNOT_FOUND
)
291 return wxDataViewItem(0);
294 if (pos
== parent
->GetChildCount()-1)
295 return wxDataViewItem(0);
297 node
= parent
->GetChildren().Item( pos
+1 );
298 return wxDataViewItem( (void*) node
);
302 MyMusicModelNode
* m_root
;
303 MyMusicModelNode
* m_pop
;
304 MyMusicModelNode
* m_classical
;
305 bool m_classicalMusicIsKnownToControl
;
308 // -------------------------------------
310 // -------------------------------------
312 class MyApp
: public wxApp
319 // -------------------------------------
321 // -------------------------------------
323 class MyFrame
: public wxFrame
326 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
329 void OnQuit(wxCommandEvent
& event
);
330 void OnAbout(wxCommandEvent
& event
);
331 void OnAdd(wxCommandEvent
& event
);
332 void OnDelete(wxCommandEvent
& event
);
335 wxDataViewCtrl
* m_dataview
;
337 wxObjectDataPtr
<MyMusicModel
> m_model
;
340 DECLARE_EVENT_TABLE()
343 // -------------------------------------
345 // -------------------------------------
349 bool MyApp::OnInit(void)
351 if ( !wxApp::OnInit() )
354 // build the first frame
356 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
369 // -------------------------------------
371 // -------------------------------------
376 ID_ABOUT
= wxID_ABOUT
,
383 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
384 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
385 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
386 EVT_BUTTON( ID_ADD
, MyFrame::OnAdd
)
387 EVT_BUTTON( ID_DELETE
, MyFrame::OnDelete
)
390 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
391 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
393 SetIcon(wxICON(sample
));
397 wxMenu
*file_menu
= new wxMenu
;
398 file_menu
->Append(ID_ABOUT
, "&About");
399 file_menu
->AppendSeparator();
400 file_menu
->Append(ID_EXIT
, "E&xit");
402 wxMenuBar
*menu_bar
= new wxMenuBar
;
403 menu_bar
->Append(file_menu
, "&File");
405 SetMenuBar(menu_bar
);
408 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
410 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
413 m_model
= new MyMusicModel
;
414 m_dataview
->AssociateModel( m_model
.get() );
416 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
417 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
418 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE
, 200,
419 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
420 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
423 main_sizer
->Add( m_dataview
, 2, wxGROW
);
425 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
427 button_sizer
->Add( new wxButton( this, ID_ADD
, "Add Mozart"), 0, wxALL
, 10 );
428 button_sizer
->Add( new wxButton( this, ID_DELETE
, "Delete selected"), 0, wxALL
, 10 );
430 main_sizer
->Add( button_sizer
, 0, 0, 0 );
432 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
434 main_sizer
->Add( m_log
, 1, wxGROW
);
436 SetSizer( main_sizer
);
439 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
444 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
) )
446 wxDataViewItem item
= m_dataview
->GetSelection();
448 m_model
->Delete( item
);
451 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
) )
453 m_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
456 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
458 wxAboutDialogInfo info
;
459 info
.SetName(_("DataView sample"));
460 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
461 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));