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 licence
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
,
37 MyMusicTreeModelNode( MyMusicTreeModelNode
* parent
,
38 const wxString
&branch
)
48 ~MyMusicTreeModelNode()
50 // free all our children nodes
51 size_t count
= m_children
.GetCount();
52 for (size_t i
= 0; i
< count
; i
++)
54 MyMusicTreeModelNode
*child
= m_children
[i
];
59 bool IsContainer() const
60 { return m_container
; }
62 MyMusicTreeModelNode
* GetParent()
64 MyMusicTreeModelNodePtrArray
& GetChildren()
65 { return m_children
; }
66 MyMusicTreeModelNode
* GetNthChild( unsigned int n
)
67 { return m_children
.Item( n
); }
68 void Insert( MyMusicTreeModelNode
* child
, unsigned int n
)
69 { m_children
.Insert( child
, n
); }
70 void Append( MyMusicTreeModelNode
* child
)
71 { m_children
.Add( child
); }
72 unsigned int GetChildCount() const
73 { return m_children
.GetCount(); }
75 public: // public to avoid getters/setters
82 // the GTK version of wxDVC (in particular wxDataViewCtrlInternal::ItemAdded)
83 // needs to know in advance if a node is or _will be_ a container.
85 // bool IsContainer() const
86 // { return m_children.GetCount()>0; }
87 // doesn't work with wxGTK when MyMusicTreeModel::AddToClassical is called
88 // AND the classical node was removed (a new node temporary without children
89 // would be added to the control)
93 MyMusicTreeModelNode
*m_parent
;
94 MyMusicTreeModelNodePtrArray m_children
;
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
103 Implement this data model
104 Title Artist Year Judgement
105 --------------------------------------------------------------------------
108 3: You are not alone Michael Jackson 1995 good
109 4: Take a bow Madonna 1994 good
111 6: Ninth Symphony Ludwig v. Beethoven 1824 good
112 7: German Requiem Johannes Brahms 1868 good
115 class MyMusicTreeModel
: public wxDataViewModel
124 // helper method for wxLog
126 wxString
GetTitle( const wxDataViewItem
&item
) const;
127 wxString
GetArtist( const wxDataViewItem
&item
) const;
128 int GetYear( const wxDataViewItem
&item
) const;
130 // helper methods to change the model
132 void AddToClassical( const wxString
&title
, const wxString
&artist
,
134 void Delete( const wxDataViewItem
&item
);
136 wxDataViewItem
GetNinthItem() const
138 return wxDataViewItem( m_ninth
);
141 // override sorting to always sort branches ascendingly
143 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
144 unsigned int column
, bool ascending
) const;
146 // implementation of base class virtuals to define model
148 virtual unsigned int GetColumnCount() const
153 virtual wxString
GetColumnType( unsigned int col
) const
158 return wxT("string");
161 virtual void GetValue( wxVariant
&variant
,
162 const wxDataViewItem
&item
, unsigned int col
) const;
163 virtual bool SetValue( const wxVariant
&variant
,
164 const wxDataViewItem
&item
, unsigned int col
);
166 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const;
167 virtual bool IsContainer( const wxDataViewItem
&item
) const;
168 virtual unsigned int GetChildren( const wxDataViewItem
&parent
,
169 wxDataViewItemArray
&array
) const;
172 MyMusicTreeModelNode
* m_root
;
174 // pointers to some "special" nodes of the tree:
175 MyMusicTreeModelNode
* m_pop
;
176 MyMusicTreeModelNode
* m_classical
;
177 MyMusicTreeModelNode
* m_ninth
;
180 bool m_classicalMusicIsKnownToControl
;
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 class MyListModel
: public wxDataViewVirtualListModel
202 // helper methods to change the model
204 void Prepend( const wxString
&text
);
205 void DeleteItem( const wxDataViewItem
&item
);
206 void DeleteItems( const wxDataViewItemArray
&items
);
210 // implementation of base class virtuals to define model
212 virtual unsigned int GetColumnCount() const
217 virtual wxString
GetColumnType( unsigned int col
) const
219 if (col
== Col_IconText
)
220 return wxT("wxDataViewIconText");
222 return wxT("string");
225 virtual void GetValueByRow( wxVariant
&variant
,
226 unsigned int row
, unsigned int col
) const;
227 virtual bool GetAttrByRow( unsigned int row
, unsigned int col
,
228 wxDataViewItemAttr
&attr
) const;
229 virtual bool SetValueByRow( const wxVariant
&variant
,
230 unsigned int row
, unsigned int col
);
233 wxArrayString m_textColValues
;
234 wxArrayString m_iconColValues
;