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" ) );
143 void AddToClassical( const wxString
&title
, const wxString
&artist
, const wxString
&year
)
146 MyMusicModelNode
*child_node
=
147 new MyMusicModelNode( m_classical
, GetNewId(), title
, artist
, year
);
148 m_classical
->Append( child_node
);
151 wxDataViewItem
child( child_node
->GetID() );
152 wxDataViewItem
parent( m_classical
->GetID() );
153 wxPrintf( "parent id %d\n", m_classical
->GetID() );
154 ItemAdded( parent
, child
);
157 virtual unsigned int GetColumnCount() const
162 virtual wxString
GetColumnType( unsigned int col
) const
167 virtual void GetValue( wxVariant
&variant
,
168 const wxDataViewItem
&item
, unsigned int col
) const
170 MyMusicModelNode
*node
= FindNode( item
);
173 case 0: variant
= node
->m_title
; break;
174 case 1: variant
= node
->m_artist
; break;
175 case 2: variant
= node
->m_year
; break;
176 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
180 virtual bool SetValue( const wxVariant
&variant
,
181 const wxDataViewItem
&item
, unsigned int col
)
183 MyMusicModelNode
*node
= FindNode( item
);
186 case 0: node
->m_title
= variant
.GetString(); break;
187 case 1: node
->m_artist
= variant
.GetString(); break;
188 case 2: node
->m_year
= variant
.GetString(); break;
189 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
193 virtual bool HasChildren( const wxDataViewItem
&item
) const
195 if (item
.GetID() == 0)
198 MyMusicModelNode
*node
= FindNode( item
);
199 return node
->IsContainer();
202 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
204 if (parent
.GetID() == 0)
205 return wxDataViewItem( m_root
->GetID() );
207 MyMusicModelNode
*node
= FindNode( parent
);
209 if (node
->GetChildCount() == 0)
210 return wxDataViewItem( 0 );
212 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
213 return wxDataViewItem( first_child
->GetID() );
216 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
218 MyMusicModelNode
*node
= FindNode( item
);
219 MyMusicModelNode
*parent
= node
->GetParent();
221 return wxDataViewItem(0);
223 int pos
= parent
->GetChildren().Index( node
);
224 if (pos
== wxNOT_FOUND
)
225 return wxDataViewItem(0);
227 if (pos
== parent
->GetChildCount()-1)
228 return wxDataViewItem(0);
230 node
= parent
->GetChildren().Item( pos
+1 );
231 return wxDataViewItem( node
->GetID() );
235 wxUint32
GetNewId() { m_idCounter
++; return m_idCounter
; }
237 MyMusicModelNode
*FindNodeRec( MyMusicModelNode
*node
, const wxDataViewItem
&item
) const
239 if (node
->GetID() == item
.GetID())
242 size_t count
= node
->GetChildCount();
244 for (i
= 0; i
< count
; i
++)
246 MyMusicModelNode
*child
= node
->GetChildren().Item( i
);
247 MyMusicModelNode
*node2
= FindNodeRec( child
, item
);
254 MyMusicModelNode
*FindNode( const wxDataViewItem
&item
) const
256 if (item
.GetID() == 0)
262 return FindNodeRec( m_root
, item
);
265 MyMusicModelNode
* m_root
;
266 MyMusicModelNode
* m_pop
;
267 MyMusicModelNode
* m_classical
;
268 wxUint32 m_idCounter
;
271 // -------------------------------------
273 // -------------------------------------
275 class MyApp
: public wxApp
282 // -------------------------------------
284 // -------------------------------------
286 class MyFrame
: public wxFrame
289 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
292 void OnQuit(wxCommandEvent
& event
);
293 void OnAbout(wxCommandEvent
& event
);
294 void OnAdd(wxCommandEvent
& event
);
297 wxDataViewCtrl
* m_dataview
;
299 wxObjectDataPtr
<MyMusicModel
> m_model
;
302 DECLARE_EVENT_TABLE()
305 // -------------------------------------
307 // -------------------------------------
311 bool MyApp::OnInit(void)
313 if ( !wxApp::OnInit() )
316 // build the first frame
318 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
331 // -------------------------------------
333 // -------------------------------------
338 ID_ABOUT
= wxID_ABOUT
,
344 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
345 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
346 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
347 EVT_BUTTON( ID_ADD
, MyFrame::OnAdd
)
350 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
351 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
353 SetIcon(wxICON(sample
));
357 wxMenu
*file_menu
= new wxMenu
;
358 file_menu
->Append(ID_ABOUT
, "&About");
359 file_menu
->AppendSeparator();
360 file_menu
->Append(ID_EXIT
, "E&xit");
362 wxMenuBar
*menu_bar
= new wxMenuBar
;
363 menu_bar
->Append(file_menu
, "&File");
365 SetMenuBar(menu_bar
);
368 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
370 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
373 m_model
= new MyMusicModel
;
374 m_dataview
->AssociateModel( m_model
.get() );
376 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
377 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
378 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT
, 200,
379 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
380 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
383 main_sizer
->Add( m_dataview
, 2, wxGROW
);
385 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
387 button_sizer
->Add( new wxButton( this, ID_ADD
, "Add Mozart"), 0, wxALL
, 10 );
389 main_sizer
->Add( button_sizer
, 0, 0, 0 );
391 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
393 main_sizer
->Add( m_log
, 1, wxGROW
);
395 SetSizer( main_sizer
);
398 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
403 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
) )
406 // ignore selection, do something better later
407 wxDataViewItem item
= m_dataview
->GetSelection();
410 if (m_model
->HasChildren(item
))
419 m_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
422 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
424 wxAboutDialogInfo info
;
425 info
.SetName(_("DataView sample"));
426 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
427 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));