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
, const wxUint32 id
,
69 const wxString
&title
, const wxString
&artist
, const wxString
&year
)
76 m_isContainer
= false;
79 MyMusicModelNode( MyMusicModelNode
* parent
, const wxUint32 id
,
80 const wxString
&branch
)
90 size_t count
= m_children
.GetCount();
92 for (i
= 0; i
< count
; i
++)
94 MyMusicModelNode
*child
= m_children
[i
];
99 wxUint32
GetID() { return m_id
; }
100 bool IsContainer() { return m_isContainer
; }
102 MyMusicModelNode
* GetParent() { return m_parent
; }
103 MyMusicModelNodes
&GetChildren() { return m_children
; }
104 MyMusicModelNode
* GetNthChild( unsigned int n
) { return m_children
.Item( n
); }
105 void Insert( MyMusicModelNode
* child
, unsigned int n
) { m_children
.Insert( child
, n
); }
106 void Append( MyMusicModelNode
* child
) { m_children
.Add( child
); }
107 unsigned int GetChildCount() { return m_children
.GetCount(); }
115 MyMusicModelNode
*m_parent
;
116 MyMusicModelNodes m_children
;
122 class MyMusicModel
: public wxDataViewModel
128 m_root
= new MyMusicModelNode( NULL
, GetNewId(), "My Music" );
129 m_pop
= new MyMusicModelNode( m_root
, GetNewId(), "Pop music" );
130 m_root
->Append( m_pop
);
131 m_pop
->Append( new MyMusicModelNode( m_pop
, GetNewId(),
132 "You are not alone", "Michael Jackson", "1995" ) );
133 m_pop
->Append( new MyMusicModelNode( m_pop
, GetNewId(),
134 "Take a bow", "Madonna", "1994" ) );
135 m_classical
= new MyMusicModelNode( m_root
, GetNewId(), "Classical music" );
136 m_root
->Append( m_classical
);
137 m_classical
->Append( new MyMusicModelNode( m_classical
, GetNewId(),
138 "Ninth symphony", "Ludwig van Beethoven", "1824" ) );
139 m_classical
->Append( new MyMusicModelNode( m_classical
, GetNewId(),
140 "German Requiem", "Johannes Brahms", "1868" ) );
141 m_classicalMusicIsKnownToControl
= false;
144 void AddToClassical( const wxString
&title
, const wxString
&artist
, const wxString
&year
)
147 MyMusicModelNode
*child_node
=
148 new MyMusicModelNode( m_classical
, GetNewId(), title
, artist
, year
);
149 m_classical
->Append( child_node
);
151 if (m_classicalMusicIsKnownToControl
)
154 wxDataViewItem
child( child_node
->GetID() );
155 wxDataViewItem
parent( m_classical
->GetID() );
156 ItemAdded( parent
, child
);
160 virtual unsigned int GetColumnCount() const
165 virtual wxString
GetColumnType( unsigned int col
) const
170 virtual void GetValue( wxVariant
&variant
,
171 const wxDataViewItem
&item
, unsigned int col
) const
173 MyMusicModelNode
*node
= FindNode( item
);
176 case 0: variant
= node
->m_title
; break;
177 case 1: variant
= node
->m_artist
; break;
178 case 2: variant
= node
->m_year
; break;
179 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
183 virtual bool SetValue( const wxVariant
&variant
,
184 const wxDataViewItem
&item
, unsigned int col
)
186 MyMusicModelNode
*node
= FindNode( item
);
189 case 0: node
->m_title
= variant
.GetString(); break;
190 case 1: node
->m_artist
= variant
.GetString(); break;
191 case 2: node
->m_year
= variant
.GetString(); break;
192 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
196 virtual bool HasChildren( const wxDataViewItem
&item
) const
198 if (item
.GetID() == 0)
201 MyMusicModelNode
*node
= FindNode( item
);
202 return node
->IsContainer();
205 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
207 if (parent
.GetID() == 0)
208 return wxDataViewItem( m_root
->GetID() );
210 MyMusicModelNode
*node
= FindNode( parent
);
212 if (node
->GetChildCount() == 0)
213 return wxDataViewItem( 0 );
215 if (node
== m_classical
)
217 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
218 model
->m_classicalMusicIsKnownToControl
= true;
221 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
222 return wxDataViewItem( first_child
->GetID() );
225 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
227 MyMusicModelNode
*node
= FindNode( item
);
228 MyMusicModelNode
*parent
= node
->GetParent();
230 return wxDataViewItem(0);
232 int pos
= parent
->GetChildren().Index( node
);
233 if (pos
== wxNOT_FOUND
)
234 return wxDataViewItem(0);
236 if (pos
== parent
->GetChildCount()-1)
237 return wxDataViewItem(0);
239 node
= parent
->GetChildren().Item( pos
+1 );
240 return wxDataViewItem( node
->GetID() );
244 wxUint32
GetNewId() { m_idCounter
++; return m_idCounter
; }
246 MyMusicModelNode
*FindNodeRec( MyMusicModelNode
*node
, const wxDataViewItem
&item
) const
248 if (node
->GetID() == item
.GetID())
251 size_t count
= node
->GetChildCount();
253 for (i
= 0; i
< count
; i
++)
255 MyMusicModelNode
*child
= node
->GetChildren().Item( i
);
256 MyMusicModelNode
*node2
= FindNodeRec( child
, item
);
263 MyMusicModelNode
*FindNode( const wxDataViewItem
&item
) const
265 if (item
.GetID() == 0)
271 return FindNodeRec( m_root
, item
);
274 MyMusicModelNode
* m_root
;
275 MyMusicModelNode
* m_pop
;
276 MyMusicModelNode
* m_classical
;
277 bool m_classicalMusicIsKnownToControl
;
278 wxUint32 m_idCounter
;
281 // -------------------------------------
283 // -------------------------------------
285 class MyApp
: public wxApp
292 // -------------------------------------
294 // -------------------------------------
296 class MyFrame
: public wxFrame
299 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
302 void OnQuit(wxCommandEvent
& event
);
303 void OnAbout(wxCommandEvent
& event
);
304 void OnAdd(wxCommandEvent
& event
);
307 wxDataViewCtrl
* m_dataview
;
309 wxObjectDataPtr
<MyMusicModel
> m_model
;
312 DECLARE_EVENT_TABLE()
315 // -------------------------------------
317 // -------------------------------------
321 bool MyApp::OnInit(void)
323 if ( !wxApp::OnInit() )
326 // build the first frame
328 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
341 // -------------------------------------
343 // -------------------------------------
348 ID_ABOUT
= wxID_ABOUT
,
354 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
355 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
356 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
357 EVT_BUTTON( ID_ADD
, MyFrame::OnAdd
)
360 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
361 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
363 SetIcon(wxICON(sample
));
367 wxMenu
*file_menu
= new wxMenu
;
368 file_menu
->Append(ID_ABOUT
, "&About");
369 file_menu
->AppendSeparator();
370 file_menu
->Append(ID_EXIT
, "E&xit");
372 wxMenuBar
*menu_bar
= new wxMenuBar
;
373 menu_bar
->Append(file_menu
, "&File");
375 SetMenuBar(menu_bar
);
378 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
380 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
383 m_model
= new MyMusicModel
;
384 m_dataview
->AssociateModel( m_model
.get() );
386 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
387 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
388 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT
, 200,
389 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
390 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
393 main_sizer
->Add( m_dataview
, 2, wxGROW
);
395 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
397 button_sizer
->Add( new wxButton( this, ID_ADD
, "Add Mozart"), 0, wxALL
, 10 );
399 main_sizer
->Add( button_sizer
, 0, 0, 0 );
401 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
403 main_sizer
->Add( m_log
, 1, wxGROW
);
405 SetSizer( main_sizer
);
408 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
413 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
) )
416 // ignore selection, do something better later
417 wxDataViewItem item
= m_dataview
->GetSelection();
420 if (m_model
->HasChildren(item
))
429 m_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
432 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
434 wxAboutDialogInfo info
;
435 info
.SetName(_("DataView sample"));
436 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
437 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));