]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
d9c9acb851b3417f23c0c03ac98a1ae5fce003d3
[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 2;
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 int ID = item.GetID();
80 switch (ID)
81 {
82 case 1: variant = "My Music"; break;
83 case 2: variant = "Pop music"; break;
84 case 5: variant = "Classical music"; break;
85 case 3:
86 {
87 switch (col)
88 {
89 case 0: variant = "You are not alone"; break;
90 case 1: variant = "Michael Jackson"; break;
91 case 2: variant = "1995";
92 }
93 }
94 break;
95 case 4:
96 {
97 switch (col)
98 {
99 case 0: variant = "Take a bow"; break;
100 case 1: variant = "Madonna"; break;
101 case 2: variant = "1994";
102 }
103 }
104 break;
105 case 6:
106 {
107 switch (col)
108 {
109 case 0: variant = "Ninth symphony"; break;
110 case 1: variant = "Ludwig v. Beethoven"; break;
111 case 2: variant = "1824";
112 }
113 }
114 break;
115 case 7:
116 {
117 switch (col)
118 {
119 case 0: variant = "German requiem"; break;
120 case 1: variant = "Johannes Brahms"; break;
121 case 2: variant = "1868";
122 }
123 }
124 break;
125 }
126
127 }
128
129 virtual bool SetValue( const wxVariant &variant,
130 const wxDataViewItem &item, unsigned int col )
131 {
132 // readonly
133 return true;
134 }
135
136 virtual bool HasChildren( const wxDataViewItem &item ) const
137 {
138 int ID = item.GetID();
139 return ((ID == 1) || (ID == 2) || (ID == 5));
140 }
141
142 virtual int GetChildCount( const wxDataViewItem &item ) const
143 {
144 int ID = item.GetID();
145 switch (ID)
146 {
147 case 1: return 2;
148 case 2: return 2;
149 case 5: return 2;
150 }
151 return 0;
152 }
153 virtual wxDataViewItem GetParent( const wxDataViewItem &child ) const
154 {
155 int ID = child.GetID();
156 switch (ID)
157 {
158 case 2:
159 case 5: return wxDataViewItem( 1 );
160 case 3:
161 case 4: return wxDataViewItem( 2 );
162 case 6:
163 case 7: return wxDataViewItem( 5 );
164 }
165
166 return wxDataViewItem(0);
167 }
168 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
169 {
170 int ID = parent.GetID();
171 switch (ID)
172 {
173 case 1: return wxDataViewItem( 2 );
174 case 2: return wxDataViewItem( 3 );
175 case 5: return wxDataViewItem( 6 );
176 }
177
178 return wxDataViewItem(0);
179 }
180 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
181 {
182 int ID = item.GetID();
183 switch (ID)
184 {
185 case 2: return wxDataViewItem( 5 );
186 case 3: return wxDataViewItem( 4 );
187 case 6: return wxDataViewItem( 7 );
188 }
189
190 return wxDataViewItem(0);
191 }
192 virtual wxDataViewItem GetNthChild( const wxDataViewItem &parent, unsigned int n ) const
193 {
194 if (!parent.IsOk())
195 {
196 // root node
197 if (n == 0)
198 return wxDataViewItem( 1 );
199
200 return wxDataViewItem( 0 );
201 }
202
203 int ID = parent.GetID();
204 switch (ID)
205 {
206 case 1:
207 {
208 if (n == 0) return wxDataViewItem( 2 );
209 else if (n == 1) return wxDataViewItem( 5 );
210 }
211 break;
212 case 2:
213 {
214 if (n == 0) return wxDataViewItem( 3 );
215 else if (n == 1) return wxDataViewItem( 4 );
216 }
217 break;
218 case 5:
219 {
220 if (n == 0) return wxDataViewItem( 6 );
221 else if (n == 1) return wxDataViewItem( 7 );
222 }
223 break;
224 }
225
226 return wxDataViewItem(0);
227 }
228 };
229
230 // -------------------------------------
231 // MyApp
232 // -------------------------------------
233
234 class MyApp: public wxApp
235 {
236 public:
237 bool OnInit(void);
238 int OnExit();
239 };
240
241 // -------------------------------------
242 // MyFrame
243 // -------------------------------------
244
245 class MyFrame : public wxFrame
246 {
247 public:
248 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
249
250 public:
251 void OnQuit(wxCommandEvent& event);
252 void OnAbout(wxCommandEvent& event);
253
254 private:
255 wxDataViewCtrl* m_dataview;
256
257 private:
258 DECLARE_EVENT_TABLE()
259 };
260
261 // -------------------------------------
262 // MyApp
263 // -------------------------------------
264
265 IMPLEMENT_APP(MyApp)
266
267 bool MyApp::OnInit(void)
268 {
269 if ( !wxApp::OnInit() )
270 return false;
271
272 // build the first frame
273 MyFrame *frame =
274 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
275 frame->Show(true);
276
277 SetTopWindow(frame);
278 return true;
279 }
280
281 int MyApp::OnExit()
282 {
283 return 0;
284 }
285
286
287 // -------------------------------------
288 // MyFrame
289 // -------------------------------------
290
291 enum
292 {
293 // file menu
294 ID_ABOUT = wxID_ABOUT,
295 ID_EXIT = wxID_EXIT,
296 };
297
298 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
299 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
300 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
301 END_EVENT_TABLE()
302
303 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
304 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
305 {
306 SetIcon(wxICON(sample));
307
308 // build the menus:
309
310 wxMenu *file_menu = new wxMenu;
311 file_menu->Append(ID_ABOUT, "&About");
312 file_menu->AppendSeparator();
313 file_menu->Append(ID_EXIT, "E&xit");
314
315 wxMenuBar *menu_bar = new wxMenuBar;
316 menu_bar->Append(file_menu, "&File");
317
318 SetMenuBar(menu_bar);
319 CreateStatusBar();
320
321 m_dataview = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
322 wxDefaultSize );
323
324 wxObjectDataPtr<MyMusicModel> model(new MyMusicModel);
325 m_dataview->AssociateModel( model.get() );
326
327 m_dataview->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, -1,
328 DEFAULT_ALIGN );
329 m_dataview->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT, -1,
330 DEFAULT_ALIGN );
331 m_dataview->AppendTextColumn( "Year", 1, wxDATAVIEW_CELL_INERT, -1,
332 DEFAULT_ALIGN );
333 }
334
335 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
336 {
337 Close(true);
338 }
339
340 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
341 {
342 wxAboutDialogInfo info;
343 info.SetName(_("DataView sample"));
344 info.SetDescription(_("This sample demonstrates the dataview control handling"));
345 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
346
347 wxAboutBox(info);
348 }
349