1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
13 // MyMusicTreeModelNode: a node inside MyMusicTreeModel
14 // ----------------------------------------------------------------------------
16 class MyMusicTreeModelNode
;
17 WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode
*, MyMusicTreeModelNodePtrArray
);
19 class MyMusicTreeModelNode
22 MyMusicTreeModelNode( MyMusicTreeModelNode
* parent
,
23 const wxString
&title
, const wxString
&artist
,
36 MyMusicTreeModelNode( MyMusicTreeModelNode
* parent
,
37 const wxString
&branch
)
47 ~MyMusicTreeModelNode()
49 // free all our children nodes
50 size_t count
= m_children
.GetCount();
51 for (size_t i
= 0; i
< count
; i
++)
53 MyMusicTreeModelNode
*child
= m_children
[i
];
58 bool IsContainer() const
59 { return m_container
; }
61 MyMusicTreeModelNode
* GetParent()
63 MyMusicTreeModelNodePtrArray
& GetChildren()
64 { return m_children
; }
65 MyMusicTreeModelNode
* GetNthChild( unsigned int n
)
66 { return m_children
.Item( n
); }
67 void Insert( MyMusicTreeModelNode
* child
, unsigned int n
)
68 { m_children
.Insert( child
, n
); }
69 void Append( MyMusicTreeModelNode
* child
)
70 { m_children
.Add( child
); }
71 unsigned int GetChildCount() const
72 { return m_children
.GetCount(); }
74 public: // public to avoid getters/setters
81 // the GTK version of wxDVC (in particular wxDataViewCtrlInternal::ItemAdded)
82 // needs to know in advance if a node is or _will be_ a container.
84 // bool IsContainer() const
85 // { return m_children.GetCount()>0; }
86 // doesn't work with wxGTK when MyMusicTreeModel::AddToClassical is called
87 // AND the classical node was removed (a new node temporary without children
88 // would be added to the control)
92 MyMusicTreeModelNode
*m_parent
;
93 MyMusicTreeModelNodePtrArray m_children
;
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
102 Implement this data model
103 Title Artist Year Judgement
104 --------------------------------------------------------------------------
107 3: You are not alone Michael Jackson 1995 good
108 4: Take a bow Madonna 1994 good
110 6: Ninth Symphony Ludwig v. Beethoven 1824 good
111 7: German Requiem Johannes Brahms 1868 good
114 class MyMusicTreeModel
: public wxDataViewModel
123 // helper method for wxLog
125 wxString
GetTitle( const wxDataViewItem
&item
) const;
126 wxString
GetArtist( const wxDataViewItem
&item
) const;
127 int GetYear( const wxDataViewItem
&item
) const;
129 // helper methods to change the model
131 void AddToClassical( const wxString
&title
, const wxString
&artist
,
133 void Delete( const wxDataViewItem
&item
);
135 wxDataViewItem
GetNinthItem() const
137 return wxDataViewItem( m_ninth
);
140 // override sorting to always sort branches ascendingly
142 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
143 unsigned int column
, bool ascending
) const;
145 // implementation of base class virtuals to define model
147 virtual unsigned int GetColumnCount() const
152 virtual wxString
GetColumnType( unsigned int col
) const
157 return wxT("string");
160 virtual void GetValue( wxVariant
&variant
,
161 const wxDataViewItem
&item
, unsigned int col
) const;
162 virtual bool SetValue( const wxVariant
&variant
,
163 const wxDataViewItem
&item
, unsigned int col
);
165 virtual bool IsEnabled( const wxDataViewItem
&item
,
166 unsigned int col
) const;
168 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const;
169 virtual bool IsContainer( const wxDataViewItem
&item
) const;
170 virtual unsigned int GetChildren( const wxDataViewItem
&parent
,
171 wxDataViewItemArray
&array
) const;
174 MyMusicTreeModelNode
* m_root
;
176 // pointers to some "special" nodes of the tree:
177 MyMusicTreeModelNode
* m_pop
;
178 MyMusicTreeModelNode
* m_classical
;
179 MyMusicTreeModelNode
* m_ninth
;
182 bool m_classicalMusicIsKnownToControl
;
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 class MyListModel
: public wxDataViewVirtualListModel
204 // helper methods to change the model
206 void Prepend( const wxString
&text
);
207 void DeleteItem( const wxDataViewItem
&item
);
208 void DeleteItems( const wxDataViewItemArray
&items
);
212 // implementation of base class virtuals to define model
214 virtual unsigned int GetColumnCount() const
219 virtual wxString
GetColumnType( unsigned int col
) const
221 if (col
== Col_IconText
)
222 return wxT("wxDataViewIconText");
224 return wxT("string");
227 virtual void GetValueByRow( wxVariant
&variant
,
228 unsigned int row
, unsigned int col
) const;
229 virtual bool GetAttrByRow( unsigned int row
, unsigned int col
,
230 wxDataViewItemAttr
&attr
) const;
231 virtual bool SetValueByRow( const wxVariant
&variant
,
232 unsigned int row
, unsigned int col
);
235 wxArrayString m_textColValues
;
236 wxArrayString m_iconColValues
;
240 // ----------------------------------------------------------------------------
241 // MyListStoreDerivedModel
242 // ----------------------------------------------------------------------------
244 class MyListStoreDerivedModel
: public wxDataViewListStore
247 virtual bool IsEnabledByRow(unsigned int row
, unsigned int col
) const;