reorganize the sample splitting it in two source files and an additional header;...
[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 // MyMusicTreeModel
15 // ----------------------------------------------------------------------------
16
17 /*
18 Implement this data model
19 Title Artist Year Judgement
20 --------------------------------------------------------------------------
21 1: My Music:
22 2: Pop music
23 3: You are not alone Michael Jackson 1995 good
24 4: Take a bow Madonna 1994 good
25 5: Classical music
26 6: Ninth Symphony Ludwig v. Beethoven 1824 good
27 7: German Requiem Johannes Brahms 1868 good
28 */
29
30 class MyMusicTreeModelNode;
31 WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodes );
32
33 class MyMusicTreeModelNode
34 {
35 public:
36 MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
37 const wxString &title, const wxString &artist, int year )
38 {
39 m_parent = parent;
40 m_title = title;
41 m_artist = artist;
42 m_year = year;
43 m_quality = "good";
44 m_isContainer = false;
45 }
46
47 MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
48 const wxString &branch )
49 {
50 m_parent = parent;
51 m_title = branch;
52 m_year = -1;
53 m_isContainer = true;
54 }
55
56 ~MyMusicTreeModelNode()
57 {
58 size_t count = m_children.GetCount();
59 size_t i;
60 for (i = 0; i < count; i++)
61 {
62 MyMusicTreeModelNode *child = m_children[i];
63 delete child;
64 }
65 }
66
67 bool IsContainer() { return m_isContainer; }
68
69 MyMusicTreeModelNode* GetParent() { return m_parent; }
70 MyMusicTreeModelNodes &GetChildren() { return m_children; }
71 MyMusicTreeModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
72 void Insert( MyMusicTreeModelNode* child, unsigned int n) { m_children.Insert( child, n); }
73 void Append( MyMusicTreeModelNode* child ) { m_children.Add( child ); }
74 unsigned int GetChildCount() { return m_children.GetCount(); }
75
76 public:
77 wxString m_title;
78 wxString m_artist;
79 int m_year;
80 wxString m_quality;
81
82 private:
83 MyMusicTreeModelNode *m_parent;
84 MyMusicTreeModelNodes m_children;
85 bool m_isContainer;
86 };
87
88 class MyMusicTreeModel: public wxDataViewModel
89 {
90 public:
91 MyMusicTreeModel();
92 ~MyMusicTreeModel()
93 {
94 delete m_root;
95 }
96
97 // helper method for wxLog
98
99 wxString GetTitle( const wxDataViewItem &item ) const;
100 int GetYear( const wxDataViewItem &item ) const;
101
102 // helper methods to change the model
103
104 void AddToClassical( const wxString &title, const wxString &artist, int year );
105 void Delete( const wxDataViewItem &item );
106
107 // override sorting to always sort branches ascendingly
108
109 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
110 unsigned int column, bool ascending );
111
112 // implementation of base class virtuals to define model
113
114 virtual unsigned int GetColumnCount() const
115 {
116 return 6;
117 }
118
119 virtual wxString GetColumnType( unsigned int col ) const
120 {
121 if (col == 2)
122 return wxT("long");
123
124 return wxT("string");
125 }
126
127 virtual void GetValue( wxVariant &variant,
128 const wxDataViewItem &item, unsigned int col ) const;
129 virtual bool SetValue( const wxVariant &variant,
130 const wxDataViewItem &item, unsigned int col );
131
132 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const;
133 virtual bool IsContainer( const wxDataViewItem &item ) const;
134 virtual unsigned int GetChildren( const wxDataViewItem &parent,
135 wxDataViewItemArray &array ) const;
136
137 wxDataViewItem GetNinthItem() const
138 {
139 return wxDataViewItem( m_ninth );
140 }
141
142 private:
143 MyMusicTreeModelNode* m_root;
144 MyMusicTreeModelNode* m_pop;
145 MyMusicTreeModelNode* m_classical;
146 MyMusicTreeModelNode* m_ninth;
147 bool m_classicalMusicIsKnownToControl;
148 };
149
150
151 // ----------------------------------------------------------------------------
152 // MyListModel
153 // ----------------------------------------------------------------------------
154
155 class MyListModel: public wxDataViewVirtualListModel
156 {
157 public:
158 MyListModel();
159
160 // helper methods to change the model
161
162 void Prepend( const wxString &text );
163 void DeleteItem( const wxDataViewItem &item );
164 void DeleteItems( const wxDataViewItemArray &items );
165 void AddMany();
166
167
168 // implementation of base class virtuals to define model
169
170 virtual unsigned int GetColumnCount() const
171 {
172 return 3;
173 }
174
175 virtual wxString GetColumnType( unsigned int col ) const
176 {
177 if (col == 1)
178 return wxT("wxDataViewIconText");
179
180 return wxT("string");
181 }
182
183 virtual unsigned int GetRowCount()
184 {
185 return m_array.GetCount();
186 }
187
188 virtual void GetValueByRow( wxVariant &variant,
189 unsigned int row, unsigned int col ) const;
190 virtual bool GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr );
191 virtual bool SetValueByRow( const wxVariant &variant,
192 unsigned int row, unsigned int col );
193
194 private:
195 wxArrayString m_array;
196 wxIcon m_icon;
197 int m_virtualItems;
198 };
199