1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
14 // MyMusicTreeModelNode: a node inside MyMusicTreeModel
15 // ----------------------------------------------------------------------------
17 class MyMusicTreeModelNode
;
18 WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode
*, MyMusicTreeModelNodePtrArray
);
20 class MyMusicTreeModelNode
23 MyMusicTreeModelNode( MyMusicTreeModelNode
* parent
,
24 const wxString
&title
, const wxString
&artist
,
35 MyMusicTreeModelNode( MyMusicTreeModelNode
* parent
,
36 const wxString
&branch
)
44 ~MyMusicTreeModelNode()
46 // free all our children nodes
47 size_t count
= m_children
.GetCount();
48 for (size_t i
= 0; i
< count
; i
++)
50 MyMusicTreeModelNode
*child
= m_children
[i
];
55 bool IsContainer() const
56 { return m_children
.GetCount()>0; }
58 MyMusicTreeModelNode
* GetParent()
60 MyMusicTreeModelNodePtrArray
& GetChildren()
61 { return m_children
; }
62 MyMusicTreeModelNode
* GetNthChild( unsigned int n
)
63 { return m_children
.Item( n
); }
64 void Insert( MyMusicTreeModelNode
* child
, unsigned int n
)
65 { m_children
.Insert( child
, n
); }
66 void Append( MyMusicTreeModelNode
* child
)
67 { m_children
.Add( child
); }
68 unsigned int GetChildCount() const
69 { return m_children
.GetCount(); }
71 public: // public to avoid getters/setters
78 MyMusicTreeModelNode
*m_parent
;
79 MyMusicTreeModelNodePtrArray m_children
;
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
88 Implement this data model
89 Title Artist Year Judgement
90 --------------------------------------------------------------------------
93 3: You are not alone Michael Jackson 1995 good
94 4: Take a bow Madonna 1994 good
96 6: Ninth Symphony Ludwig v. Beethoven 1824 good
97 7: German Requiem Johannes Brahms 1868 good
100 class MyMusicTreeModel
: public wxDataViewModel
109 // helper method for wxLog
111 wxString
GetTitle( const wxDataViewItem
&item
) const;
112 int GetYear( const wxDataViewItem
&item
) const;
114 // helper methods to change the model
116 void AddToClassical( const wxString
&title
, const wxString
&artist
,
118 void Delete( const wxDataViewItem
&item
);
120 wxDataViewItem
GetNinthItem() const
122 return wxDataViewItem( m_ninth
);
125 // override sorting to always sort branches ascendingly
127 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
128 unsigned int column
, bool ascending
);
130 // implementation of base class virtuals to define model
132 virtual unsigned int GetColumnCount() const
137 virtual wxString
GetColumnType( unsigned int col
) const
142 return wxT("string");
145 virtual void GetValue( wxVariant
&variant
,
146 const wxDataViewItem
&item
, unsigned int col
) const;
147 virtual bool SetValue( const wxVariant
&variant
,
148 const wxDataViewItem
&item
, unsigned int col
);
150 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const;
151 virtual bool IsContainer( const wxDataViewItem
&item
) const;
152 virtual unsigned int GetChildren( const wxDataViewItem
&parent
,
153 wxDataViewItemArray
&array
) const;
156 MyMusicTreeModelNode
* m_root
;
158 // pointers to some "special" nodes of the tree:
159 MyMusicTreeModelNode
* m_pop
;
160 MyMusicTreeModelNode
* m_classical
;
161 MyMusicTreeModelNode
* m_ninth
;
164 bool m_classicalMusicIsKnownToControl
;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 class MyListModel
: public wxDataViewVirtualListModel
177 // helper methods to change the model
179 void Prepend( const wxString
&text
);
180 void DeleteItem( const wxDataViewItem
&item
);
181 void DeleteItems( const wxDataViewItemArray
&items
);
185 // implementation of base class virtuals to define model
187 virtual unsigned int GetColumnCount() const
192 virtual wxString
GetColumnType( unsigned int col
) const
195 return wxT("wxDataViewIconText");
197 return wxT("string");
200 virtual unsigned int GetRowCount()
202 return m_array
.GetCount();
205 virtual void GetValueByRow( wxVariant
&variant
,
206 unsigned int row
, unsigned int col
) const;
207 virtual bool GetAttrByRow( unsigned int row
, unsigned int col
, wxDataViewItemAttr
&attr
);
208 virtual bool SetValueByRow( const wxVariant
&variant
,
209 unsigned int row
, unsigned int col
);
212 wxArrayString m_array
;