]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
457c81f1d9ed493552dbc2c114c3babaf1fd0402
[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 int ID = parent.GetID();
195 switch (ID)
196 {
197 case 1:
198 {
199 if (n == 0) return wxDataViewItem( 2 );
200 else if (n == 1) return wxDataViewItem( 5 );
201 }
202 break;
203 case 2:
204 {
205 if (n == 0) return wxDataViewItem( 3 );
206 else if (n == 1) return wxDataViewItem( 4 );
207 }
208 break;
209 case 5:
210 {
211 if (n == 0) return wxDataViewItem( 6 );
212 else if (n == 1) return wxDataViewItem( 7 );
213 }
214 break;
215 }
216
217 return wxDataViewItem(0);
218 }
219 };
220
221 // -------------------------------------
222 // MyApp
223 // -------------------------------------
224
225 class MyApp: public wxApp
226 {
227 public:
228 bool OnInit(void);
229 int OnExit();
230 };
231
232 // -------------------------------------
233 // MyFrame
234 // -------------------------------------
235
236 class MyFrame : public wxFrame
237 {
238 public:
239 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
240
241 public:
242 void OnQuit(wxCommandEvent& event);
243 void OnAbout(wxCommandEvent& event);
244
245 private:
246 wxDataViewCtrl* m_dataview;
247
248 private:
249 DECLARE_EVENT_TABLE()
250 };
251
252 // -------------------------------------
253 // MyApp
254 // -------------------------------------
255
256 IMPLEMENT_APP(MyApp)
257
258 bool MyApp::OnInit(void)
259 {
260 if ( !wxApp::OnInit() )
261 return false;
262
263 // build the first frame
264 MyFrame *frame =
265 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
266 frame->Show(true);
267
268 SetTopWindow(frame);
269 return true;
270 }
271
272 int MyApp::OnExit()
273 {
274 return 0;
275 }
276
277
278 // -------------------------------------
279 // MyFrame
280 // -------------------------------------
281
282 enum
283 {
284 // file menu
285 ID_ABOUT = wxID_ABOUT,
286 ID_EXIT = wxID_EXIT,
287 };
288
289 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
290 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
291 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
292 END_EVENT_TABLE()
293
294 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
295 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
296 {
297 SetIcon(wxICON(sample));
298
299 // build the menus:
300
301 wxMenu *file_menu = new wxMenu;
302 file_menu->Append(ID_ABOUT, "&About");
303 file_menu->AppendSeparator();
304 file_menu->Append(ID_EXIT, "E&xit");
305
306 wxMenuBar *menu_bar = new wxMenuBar;
307 menu_bar->Append(file_menu, "&File");
308
309 SetMenuBar(menu_bar);
310 CreateStatusBar();
311
312 m_dataview = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
313 wxDefaultSize );
314
315 wxObjectDataPtr<MyMusicModel> model(new MyMusicModel);
316 m_dataview->AssociateModel( model.get() );
317
318 m_dataview->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, -1,
319 DEFAULT_ALIGN );
320 m_dataview->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT, -1,
321 DEFAULT_ALIGN );
322 m_dataview->AppendTextColumn( "Year", 1, wxDATAVIEW_CELL_INERT, -1,
323 DEFAULT_ALIGN );
324 }
325
326 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
327 {
328 Close(true);
329 }
330
331 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
332 {
333 wxAboutDialogInfo info;
334 info.SetName(_("DataView sample"));
335 info.SetDescription(_("This sample demonstrates the dataview control handling"));
336 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
337
338 wxAboutBox(info);
339 }
340