]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/mymodels.h
Make storing non-trivial data in wxThreadSpecificInfo possible.
[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 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 // ----------------------------------------------------------------------------
13 // MyMusicTreeModelNode: a node inside MyMusicTreeModel
14 // ----------------------------------------------------------------------------
15
16 class MyMusicTreeModelNode;
17 WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray );
18
19 class MyMusicTreeModelNode
20 {
21 public:
22 MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
23 const wxString &title, const wxString &artist,
24 unsigned int year )
25 {
26 m_parent = parent;
27
28 m_title = title;
29 m_artist = artist;
30 m_year = year;
31 m_quality = "good";
32
33 m_container = false;
34 }
35
36 MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
37 const wxString &branch )
38 {
39 m_parent = parent;
40
41 m_title = branch;
42 m_year = -1;
43
44 m_container = true;
45 }
46
47 ~MyMusicTreeModelNode()
48 {
49 // free all our children nodes
50 size_t count = m_children.GetCount();
51 for (size_t i = 0; i < count; i++)
52 {
53 MyMusicTreeModelNode *child = m_children[i];
54 delete child;
55 }
56 }
57
58 bool IsContainer() const
59 { return m_container; }
60
61 MyMusicTreeModelNode* GetParent()
62 { return m_parent; }
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(); }
73
74 public: // public to avoid getters/setters
75 wxString m_title;
76 wxString m_artist;
77 int m_year;
78 wxString m_quality;
79
80 // TODO/FIXME:
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.
83 // Thus implementing:
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)
89 bool m_container;
90
91 private:
92 MyMusicTreeModelNode *m_parent;
93 MyMusicTreeModelNodePtrArray m_children;
94 };
95
96
97 // ----------------------------------------------------------------------------
98 // MyMusicTreeModel
99 // ----------------------------------------------------------------------------
100
101 /*
102 Implement this data model
103 Title Artist Year Judgement
104 --------------------------------------------------------------------------
105 1: My Music:
106 2: Pop music
107 3: You are not alone Michael Jackson 1995 good
108 4: Take a bow Madonna 1994 good
109 5: Classical music
110 6: Ninth Symphony Ludwig v. Beethoven 1824 good
111 7: German Requiem Johannes Brahms 1868 good
112 */
113
114 class MyMusicTreeModel: public wxDataViewModel
115 {
116 public:
117 MyMusicTreeModel();
118 ~MyMusicTreeModel()
119 {
120 delete m_root;
121 }
122
123 // helper method for wxLog
124
125 wxString GetTitle( const wxDataViewItem &item ) const;
126 wxString GetArtist( const wxDataViewItem &item ) const;
127 int GetYear( const wxDataViewItem &item ) const;
128
129 // helper methods to change the model
130
131 void AddToClassical( const wxString &title, const wxString &artist,
132 unsigned int year );
133 void Delete( const wxDataViewItem &item );
134
135 wxDataViewItem GetNinthItem() const
136 {
137 return wxDataViewItem( m_ninth );
138 }
139
140 // override sorting to always sort branches ascendingly
141
142 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
143 unsigned int column, bool ascending ) const;
144
145 // implementation of base class virtuals to define model
146
147 virtual unsigned int GetColumnCount() const
148 {
149 return 6;
150 }
151
152 virtual wxString GetColumnType( unsigned int col ) const
153 {
154 if (col == 2)
155 return wxT("long");
156
157 return wxT("string");
158 }
159
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 );
164
165 virtual bool IsEnabled( const wxDataViewItem &item,
166 unsigned int col ) const;
167
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;
172
173 private:
174 MyMusicTreeModelNode* m_root;
175
176 // pointers to some "special" nodes of the tree:
177 MyMusicTreeModelNode* m_pop;
178 MyMusicTreeModelNode* m_classical;
179 MyMusicTreeModelNode* m_ninth;
180
181 // ??
182 bool m_classicalMusicIsKnownToControl;
183 };
184
185
186 // ----------------------------------------------------------------------------
187 // MyListModel
188 // ----------------------------------------------------------------------------
189
190 class MyListModel: public wxDataViewVirtualListModel
191 {
192 public:
193 enum
194 {
195 Col_EditableText,
196 Col_IconText,
197 Col_TextWithAttr,
198 Col_Custom,
199 Col_Max
200 };
201
202 MyListModel();
203
204 // helper methods to change the model
205
206 void Prepend( const wxString &text );
207 void DeleteItem( const wxDataViewItem &item );
208 void DeleteItems( const wxDataViewItemArray &items );
209 void AddMany();
210
211
212 // implementation of base class virtuals to define model
213
214 virtual unsigned int GetColumnCount() const
215 {
216 return Col_Max;
217 }
218
219 virtual wxString GetColumnType( unsigned int col ) const
220 {
221 if (col == Col_IconText)
222 return wxT("wxDataViewIconText");
223
224 return wxT("string");
225 }
226
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 );
233
234 private:
235 wxArrayString m_textColValues;
236 wxArrayString m_iconColValues;
237 wxIcon m_icon[2];
238 };
239
240 // ----------------------------------------------------------------------------
241 // MyListStoreDerivedModel
242 // ----------------------------------------------------------------------------
243
244 class MyListStoreDerivedModel : public wxDataViewListStore
245 {
246 public:
247 virtual bool IsEnabledByRow(unsigned int row, unsigned int col) const;
248 };