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 // implementation of base class virtuals to define model
172 virtual unsigned int GetColumnCount() const
177 virtual wxString
GetColumnType( unsigned int col
) const
182 virtual void GetValue( wxVariant
&variant
,
183 const wxDataViewItem
&item
, unsigned int col
) const
185 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
188 case 0: variant
= node
->m_title
; break;
189 case 1: variant
= node
->m_artist
; break;
190 case 2: variant
= node
->m_year
; break;
191 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
195 virtual bool SetValue( const wxVariant
&variant
,
196 const wxDataViewItem
&item
, unsigned int col
)
198 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
201 case 0: node
->m_title
= variant
.GetString(); break;
202 case 1: node
->m_artist
= variant
.GetString(); break;
203 case 2: node
->m_year
= variant
.GetString(); break;
204 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
208 virtual bool HasChildren( const wxDataViewItem
&item
) const
210 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
214 return node
->IsContainer();
217 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
219 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
221 return wxDataViewItem( (void*) m_root
);
223 if (node
->GetChildCount() == 0)
224 return wxDataViewItem( 0 );
226 if (node
== m_classical
)
228 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
229 model
->m_classicalMusicIsKnownToControl
= true;
232 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
233 return wxDataViewItem( (void*) first_child
);
236 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
238 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
239 MyMusicModelNode
*parent
= node
->GetParent();
241 return wxDataViewItem(0);
243 int pos
= parent
->GetChildren().Index( node
);
244 if (pos
== wxNOT_FOUND
)
245 return wxDataViewItem(0);
247 if (pos
== parent
->GetChildCount()-1)
248 return wxDataViewItem(0);
250 node
= parent
->GetChildren().Item( pos
+1 );
251 return wxDataViewItem( (void*) node
);
255 MyMusicModelNode
* m_root
;
256 MyMusicModelNode
* m_pop
;
257 MyMusicModelNode
* m_classical
;
258 bool m_classicalMusicIsKnownToControl
;
261 // -------------------------------------
263 // -------------------------------------
265 class MyApp
: public wxApp
272 // -------------------------------------
274 // -------------------------------------
276 class MyFrame
: public wxFrame
279 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
282 void OnQuit(wxCommandEvent
& event
);
283 void OnAbout(wxCommandEvent
& event
);
284 void OnAdd(wxCommandEvent
& event
);
285 void OnDelete(wxCommandEvent
& event
);
288 wxDataViewCtrl
* m_dataview
;
290 wxObjectDataPtr
<MyMusicModel
> m_model
;
293 DECLARE_EVENT_TABLE()
296 // -------------------------------------
298 // -------------------------------------
302 bool MyApp::OnInit(void)
304 if ( !wxApp::OnInit() )
307 // build the first frame
309 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
322 // -------------------------------------
324 // -------------------------------------
329 ID_ABOUT
= wxID_ABOUT
,
336 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
337 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
338 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
339 EVT_BUTTON( ID_ADD
, MyFrame::OnAdd
)
340 EVT_BUTTON( ID_DELETE
, MyFrame::OnDelete
)
343 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
344 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
346 SetIcon(wxICON(sample
));
350 wxMenu
*file_menu
= new wxMenu
;
351 file_menu
->Append(ID_ABOUT
, "&About");
352 file_menu
->AppendSeparator();
353 file_menu
->Append(ID_EXIT
, "E&xit");
355 wxMenuBar
*menu_bar
= new wxMenuBar
;
356 menu_bar
->Append(file_menu
, "&File");
358 SetMenuBar(menu_bar
);
361 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
363 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
366 m_model
= new MyMusicModel
;
367 m_dataview
->AssociateModel( m_model
.get() );
369 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
370 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
371 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT
, 200,
372 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
373 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
376 main_sizer
->Add( m_dataview
, 2, wxGROW
);
378 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
380 button_sizer
->Add( new wxButton( this, ID_ADD
, "Add Mozart"), 0, wxALL
, 10 );
381 button_sizer
->Add( new wxButton( this, ID_DELETE
, "Delete selected"), 0, wxALL
, 10 );
383 main_sizer
->Add( button_sizer
, 0, 0, 0 );
385 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
387 main_sizer
->Add( m_log
, 1, wxGROW
);
389 SetSizer( main_sizer
);
392 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
397 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
) )
399 wxDataViewItem item
= m_dataview
->GetSelection();
401 m_model
->Delete( item
);
404 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
) )
406 m_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
409 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
411 wxAboutDialogInfo info
;
412 info
.SetName(_("DataView sample"));
413 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
414 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));