]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
don't output -DwxUSE_GUI=0 in cflags by default, do output it if 'base' is specified...
[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 /*****************************************************************
138 If wxDataViewItem is not valid in the two methods I quote above
139 then it means "return the child item from the invisible root".
140 ******************************************************************/
141
142 virtual bool HasChildren( const wxDataViewItem &item ) const
143 {
144 int ID = item.GetID();
145 return ((ID == 1) || (ID == 2) || (ID == 5) || (ID == 0));
146 }
147
148 virtual int GetChildCount( const wxDataViewItem &item ) const
149 {
150 int ID = item.GetID();
151 switch (ID)
152 {
153 case 0: return 1;
154 case 1: return 2;
155 case 2: return 2;
156 case 5: return 2;
157 }
158 return 0;
159 }
160 virtual wxDataViewItem GetParent( const wxDataViewItem &child ) const
161 {
162 int ID = child.GetID();
163 switch (ID)
164 {
165 case 2:
166 case 5: return wxDataViewItem( 1 );
167 case 3:
168 case 4: return wxDataViewItem( 2 );
169 case 6:
170 case 7: return wxDataViewItem( 5 );
171 }
172
173 return wxDataViewItem(0);
174 }
175 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
176 {
177 int ID = parent.GetID();
178 switch (ID)
179 {
180 case 0: return wxDataViewItem( 1 );
181 case 1: return wxDataViewItem( 2 );
182 case 2: return wxDataViewItem( 3 );
183 case 5: return wxDataViewItem( 6 );
184 }
185
186 return wxDataViewItem(0);
187 }
188 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
189 {
190 int ID = item.GetID();
191 switch (ID)
192 {
193 case 2: return wxDataViewItem( 5 );
194 case 3: return wxDataViewItem( 4 );
195 case 6: return wxDataViewItem( 7 );
196 }
197
198 return wxDataViewItem(0);
199 }
200 virtual wxDataViewItem GetNthChild( const wxDataViewItem &parent, unsigned int n ) const
201 {
202 if (!parent.IsOk())
203 {
204 // root node
205 if (n == 0)
206 return wxDataViewItem( 1 );
207
208 return wxDataViewItem( 0 );
209 }
210
211 int ID = parent.GetID();
212 switch (ID)
213 {
214 case 1:
215 {
216 if (n == 0) return wxDataViewItem( 2 );
217 else if (n == 1) return wxDataViewItem( 5 );
218 }
219 break;
220 case 2:
221 {
222 if (n == 0) return wxDataViewItem( 3 );
223 else if (n == 1) return wxDataViewItem( 4 );
224 }
225 break;
226 case 5:
227 {
228 if (n == 0) return wxDataViewItem( 6 );
229 else if (n == 1) return wxDataViewItem( 7 );
230 }
231 break;
232 }
233
234 return wxDataViewItem(0);
235 }
236 };
237
238 // -------------------------------------
239 // MyApp
240 // -------------------------------------
241
242 class MyApp: public wxApp
243 {
244 public:
245 bool OnInit(void);
246 int OnExit();
247 };
248
249 // -------------------------------------
250 // MyFrame
251 // -------------------------------------
252
253 class MyFrame : public wxFrame
254 {
255 public:
256 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
257
258 public:
259 void OnQuit(wxCommandEvent& event);
260 void OnAbout(wxCommandEvent& event);
261
262 private:
263 wxDataViewCtrl* m_dataview;
264
265 private:
266 DECLARE_EVENT_TABLE()
267 };
268
269 // -------------------------------------
270 // MyApp
271 // -------------------------------------
272
273 IMPLEMENT_APP(MyApp)
274
275 bool MyApp::OnInit(void)
276 {
277 if ( !wxApp::OnInit() )
278 return false;
279
280 // build the first frame
281 MyFrame *frame =
282 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
283 frame->Show(true);
284
285 SetTopWindow(frame);
286 return true;
287 }
288
289 int MyApp::OnExit()
290 {
291 return 0;
292 }
293
294
295 // -------------------------------------
296 // MyFrame
297 // -------------------------------------
298
299 enum
300 {
301 // file menu
302 ID_ABOUT = wxID_ABOUT,
303 ID_EXIT = wxID_EXIT,
304 };
305
306 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
307 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
308 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
309 END_EVENT_TABLE()
310
311 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
312 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
313 {
314 SetIcon(wxICON(sample));
315
316 // build the menus:
317
318 wxMenu *file_menu = new wxMenu;
319 file_menu->Append(ID_ABOUT, "&About");
320 file_menu->AppendSeparator();
321 file_menu->Append(ID_EXIT, "E&xit");
322
323 wxMenuBar *menu_bar = new wxMenuBar;
324 menu_bar->Append(file_menu, "&File");
325
326 SetMenuBar(menu_bar);
327 CreateStatusBar();
328
329 m_dataview = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
330 wxDefaultSize );
331
332 wxObjectDataPtr<MyMusicModel> model(new MyMusicModel);
333 m_dataview->AssociateModel( model.get() );
334
335 m_dataview->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
336 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
337 m_dataview->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT, 200,
338 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
339 m_dataview->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
340 DEFAULT_ALIGN );
341 }
342
343 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
344 {
345 Close(true);
346 }
347
348 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
349 {
350 wxAboutDialogInfo info;
351 info.SetName(_("DataView sample"));
352 info.SetDescription(_("This sample demonstrates the dataview control handling"));
353 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
354
355 wxAboutBox(info);
356 }
357