set svn properties correctly for the new files (please use the auto-props!)
[wxWidgets.git] / samples / dataview / mymodels.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mymodels.h
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 // ----------------------------------------------------------------------------
14 // MyMusicTreeModelNode: a node inside MyMusicTreeModel
15 // ----------------------------------------------------------------------------
16
17 class MyMusicTreeModelNode;
18 WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray );
19
20 class MyMusicTreeModelNode
21 {
22 public:
23 MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
24 const wxString &title, const wxString &artist,
25 unsigned int year )
26 {
27 m_parent = parent;
28
29 m_title = title;
30 m_artist = artist;
31 m_year = year;
32 m_quality = "good";
33 }
34
35 MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
36 const wxString &branch )
37 {
38 m_parent = parent;
39
40 m_title = branch;
41 m_year = -1;
42 }
43
44 ~MyMusicTreeModelNode()
45 {
46 // free all our children nodes
47 size_t count = m_children.GetCount();
48 for (size_t i = 0; i < count; i++)
49 {
50 MyMusicTreeModelNode *child = m_children[i];
51 delete child;
52 }
53 }
54
55 bool IsContainer() const
56 { return m_children.GetCount()>0; }
57
58 MyMusicTreeModelNode* GetParent()
59 { return m_parent; }
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(); }
70
71 public: // public to avoid getters/setters
72 wxString m_title;
73 wxString m_artist;
74 int m_year;
75 wxString m_quality;
76
77 private:
78 MyMusicTreeModelNode *m_parent;
79 MyMusicTreeModelNodePtrArray m_children;
80 };
81
82
83 // ----------------------------------------------------------------------------
84 // MyMusicTreeModel
85 // ----------------------------------------------------------------------------
86
87 /*
88 Implement this data model
89 Title Artist Year Judgement
90 --------------------------------------------------------------------------
91 1: My Music:
92 2: Pop music
93 3: You are not alone Michael Jackson 1995 good
94 4: Take a bow Madonna 1994 good
95 5: Classical music
96 6: Ninth Symphony Ludwig v. Beethoven 1824 good
97 7: German Requiem Johannes Brahms 1868 good
98 */
99
100 class MyMusicTreeModel: public wxDataViewModel
101 {
102 public:
103 MyMusicTreeModel();
104 ~MyMusicTreeModel()
105 {
106 delete m_root;
107 }
108
109 // helper method for wxLog
110
111 wxString GetTitle( const wxDataViewItem &item ) const;
112 int GetYear( const wxDataViewItem &item ) const;
113
114 // helper methods to change the model
115
116 void AddToClassical( const wxString &title, const wxString &artist,
117 unsigned int year );
118 void Delete( const wxDataViewItem &item );
119
120 wxDataViewItem GetNinthItem() const
121 {
122 return wxDataViewItem( m_ninth );
123 }
124
125 // override sorting to always sort branches ascendingly
126
127 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
128 unsigned int column, bool ascending );
129
130 // implementation of base class virtuals to define model
131
132 virtual unsigned int GetColumnCount() const
133 {
134 return 6;
135 }
136
137 virtual wxString GetColumnType( unsigned int col ) const
138 {
139 if (col == 2)
140 return wxT("long");
141
142 return wxT("string");
143 }
144
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 );
149
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;
154
155 private:
156 MyMusicTreeModelNode* m_root;
157
158 // pointers to some "special" nodes of the tree:
159 MyMusicTreeModelNode* m_pop;
160 MyMusicTreeModelNode* m_classical;
161 MyMusicTreeModelNode* m_ninth;
162
163 // ??
164 bool m_classicalMusicIsKnownToControl;
165 };
166
167
168 // ----------------------------------------------------------------------------
169 // MyListModel
170 // ----------------------------------------------------------------------------
171
172 class MyListModel: public wxDataViewVirtualListModel
173 {
174 public:
175 MyListModel();
176
177 // helper methods to change the model
178
179 void Prepend( const wxString &text );
180 void DeleteItem( const wxDataViewItem &item );
181 void DeleteItems( const wxDataViewItemArray &items );
182 void AddMany();
183
184
185 // implementation of base class virtuals to define model
186
187 virtual unsigned int GetColumnCount() const
188 {
189 return 3;
190 }
191
192 virtual wxString GetColumnType( unsigned int col ) const
193 {
194 if (col == 1)
195 return wxT("wxDataViewIconText");
196
197 return wxT("string");
198 }
199
200 virtual unsigned int GetRowCount()
201 {
202 return m_array.GetCount();
203 }
204
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 );
210
211 private:
212 wxArrayString m_array;
213 wxIcon m_icon[2];
214 int m_virtualItems;
215 };
216