]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
wxDataViewCtrl can show tree/list music sample.
[wxWidgets.git] / samples / dataview / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/datetime.h"
24 #include "wx/splitter.h"
25 #include "wx/aboutdlg.h"
26 #include "wx/choicdlg.h"
27 #include "wx/numdlg.h"
28 #include "wx/dataview.h"
29 #include "wx/spinctrl.h"
30
31 #ifndef __WXMSW__
32 #include "../sample.xpm"
33 #endif
34
35 #include "null.xpm"
36
37
38 #define DEFAULT_ALIGN wxALIGN_LEFT
39 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
40
41
42
43 // -------------------------------------
44 // MyMusicModel
45 // -------------------------------------
46
47 /*
48 Implement this data model
49 Title Artist Year
50 -------------------------------------------------------------
51 1: My Music:
52 2: Pop music
53 3: You are not alone Michael Jackson 1995
54 4: Take a bow Madonna 1994
55 5: Classical music
56 6: Ninth Symphony Ludwig v. Beethoven 1824
57 7: German Requiem Johannes Brahms 1868
58 */
59
60
61 class MyMusicModel: public wxDataViewModel
62 {
63 public:
64 MyMusicModel() {}
65
66 virtual unsigned int GetColumnCount() const
67 {
68 return 3;
69 }
70
71 virtual wxString GetColumnType( unsigned int col ) const
72 {
73 return "string";
74 }
75
76 virtual void GetValue( wxVariant &variant,
77 const wxDataViewItem &item, unsigned int col ) const
78 {
79 variant = wxString("");
80 int ID = item.GetID();
81 switch (ID)
82 {
83 case 1: if (col == 0) variant = wxString("My Music"); break;
84 case 2: if (col == 0) variant = wxString("Pop music"); break;
85 case 5: if (col == 0) variant = wxString("Classical music"); break;
86 case 3:
87 {
88 switch (col)
89 {
90 case 0: variant = wxString("You are not alone"); break;
91 case 1: variant = wxString("Michael Jackson"); break;
92 case 2: variant = wxString("1995");
93 }
94 }
95 break;
96 case 4:
97 {
98 switch (col)
99 {
100 case 0: variant = wxString("Take a bow"); break;
101 case 1: variant = wxString("Madonna"); break;
102 case 2: variant = wxString("1994");
103 }
104 }
105 break;
106 case 6:
107 {
108 switch (col)
109 {
110 case 0: variant = wxString("Ninth symphony"); break;
111 case 1: variant = wxString("Ludwig v. Beethoven"); break;
112 case 2: variant = wxString("1824");
113 }
114 }
115 break;
116 case 7:
117 {
118 switch (col)
119 {
120 case 0: variant = wxString("German requiem"); break;
121 case 1: variant = wxString("Johannes Brahms"); break;
122 case 2: variant = wxString("1868");
123 }
124 }
125 break;
126 }
127
128 }
129
130 virtual bool SetValue( const wxVariant &variant,
131 const wxDataViewItem &item, unsigned int col )
132 {
133 // readonly
134 return true;
135 }
136
137 virtual bool HasChildren( const wxDataViewItem &item ) const
138 {
139 int ID = item.GetID();
140 return ((ID == 1) || (ID == 2) || (ID == 5));
141 }
142
143 virtual int GetChildCount( const wxDataViewItem &item ) const
144 {
145 int ID = item.GetID();
146 switch (ID)
147 {
148 case 1: return 2;
149 case 2: return 2;
150 case 5: return 2;
151 }
152 return 0;
153 }
154 virtual wxDataViewItem GetParent( const wxDataViewItem &child ) const
155 {
156 int ID = child.GetID();
157 switch (ID)
158 {
159 case 2:
160 case 5: return wxDataViewItem( 1 );
161 case 3:
162 case 4: return wxDataViewItem( 2 );
163 case 6:
164 case 7: return wxDataViewItem( 5 );
165 }
166
167 return wxDataViewItem(0);
168 }
169 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
170 {
171 int ID = parent.GetID();
172 switch (ID)
173 {
174 case 1: return wxDataViewItem( 2 );
175 case 2: return wxDataViewItem( 3 );
176 case 5: return wxDataViewItem( 6 );
177 }
178
179 return wxDataViewItem(0);
180 }
181 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
182 {
183 int ID = item.GetID();
184 switch (ID)
185 {
186 case 2: return wxDataViewItem( 5 );
187 case 3: return wxDataViewItem( 4 );
188 case 6: return wxDataViewItem( 7 );
189 }
190
191 return wxDataViewItem(0);
192 }
193 virtual wxDataViewItem GetNthChild( const wxDataViewItem &parent, unsigned int n ) const
194 {
195 if (!parent.IsOk())
196 {
197 // root node
198 if (n == 0)
199 return wxDataViewItem( 1 );
200
201 return wxDataViewItem( 0 );
202 }
203
204 int ID = parent.GetID();
205 switch (ID)
206 {
207 case 1:
208 {
209 if (n == 0) return wxDataViewItem( 2 );
210 else if (n == 1) return wxDataViewItem( 5 );
211 }
212 break;
213 case 2:
214 {
215 if (n == 0) return wxDataViewItem( 3 );
216 else if (n == 1) return wxDataViewItem( 4 );
217 }
218 break;
219 case 5:
220 {
221 if (n == 0) return wxDataViewItem( 6 );
222 else if (n == 1) return wxDataViewItem( 7 );
223 }
224 break;
225 }
226
227 return wxDataViewItem(0);
228 }
229 };
230
231 // -------------------------------------
232 // MyApp
233 // -------------------------------------
234
235 class MyApp: public wxApp
236 {
237 public:
238 bool OnInit(void);
239 int OnExit();
240 };
241
242 // -------------------------------------
243 // MyFrame
244 // -------------------------------------
245
246 class MyFrame : public wxFrame
247 {
248 public:
249 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
250
251 public:
252 void OnQuit(wxCommandEvent& event);
253 void OnAbout(wxCommandEvent& event);
254
255 private:
256 wxDataViewCtrl* m_dataview;
257
258 private:
259 DECLARE_EVENT_TABLE()
260 };
261
262 // -------------------------------------
263 // MyApp
264 // -------------------------------------
265
266 IMPLEMENT_APP(MyApp)
267
268 bool MyApp::OnInit(void)
269 {
270 if ( !wxApp::OnInit() )
271 return false;
272
273 // build the first frame
274 MyFrame *frame =
275 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
276 frame->Show(true);
277
278 SetTopWindow(frame);
279 return true;
280 }
281
282 int MyApp::OnExit()
283 {
284 return 0;
285 }
286
287
288 // -------------------------------------
289 // MyFrame
290 // -------------------------------------
291
292 enum
293 {
294 // file menu
295 ID_ABOUT = wxID_ABOUT,
296 ID_EXIT = wxID_EXIT,
297 };
298
299 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
300 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
301 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
302 END_EVENT_TABLE()
303
304 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
305 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
306 {
307 SetIcon(wxICON(sample));
308
309 // build the menus:
310
311 wxMenu *file_menu = new wxMenu;
312 file_menu->Append(ID_ABOUT, "&About");
313 file_menu->AppendSeparator();
314 file_menu->Append(ID_EXIT, "E&xit");
315
316 wxMenuBar *menu_bar = new wxMenuBar;
317 menu_bar->Append(file_menu, "&File");
318
319 SetMenuBar(menu_bar);
320 CreateStatusBar();
321
322 m_dataview = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
323 wxDefaultSize );
324
325 wxObjectDataPtr<MyMusicModel> model(new MyMusicModel);
326 m_dataview->AssociateModel( model.get() );
327
328 m_dataview->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
329 DEFAULT_ALIGN );
330 m_dataview->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT, 200,
331 DEFAULT_ALIGN );
332 m_dataview->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
333 DEFAULT_ALIGN );
334 }
335
336 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
337 {
338 Close(true);
339 }
340
341 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
342 {
343 wxAboutDialogInfo info;
344 info.SetName(_("DataView sample"));
345 info.SetDescription(_("This sample demonstrates the dataview control handling"));
346 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
347
348 wxAboutBox(info);
349 }
350