]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
show wxUSE_STL in the summary at the end too
[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
62 class MyMusicModelNode;
63 WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
64
65 class MyMusicModelNode
66 {
67 public:
68 MyMusicModelNode( MyMusicModelNode* parent,
69 const wxString &title, const wxString &artist, const wxString &year )
70 {
71 m_parent = parent;
72 m_title = title;
73 m_artist = artist;
74 m_year = year;
75 m_isContainer = false;
76 }
77
78 MyMusicModelNode( MyMusicModelNode* parent,
79 const wxString &branch )
80 {
81 m_parent = parent;
82 m_title = branch;
83 m_isContainer = true;
84 }
85
86 ~MyMusicModelNode()
87 {
88 size_t count = m_children.GetCount();
89 size_t i;
90 for (i = 0; i < count; i++)
91 {
92 MyMusicModelNode *child = m_children[i];
93 delete child;
94 }
95 }
96
97 bool IsContainer() { return m_isContainer; }
98
99 MyMusicModelNode* GetParent() { return m_parent; }
100 MyMusicModelNodes &GetChildren() { return m_children; }
101 MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
102 void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
103 void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
104 unsigned int GetChildCount() { return m_children.GetCount(); }
105
106 public:
107 wxString m_title;
108 wxString m_artist;
109 wxString m_year;
110
111 private:
112 MyMusicModelNode *m_parent;
113 MyMusicModelNodes m_children;
114 bool m_isContainer;
115 };
116
117
118 class MyMusicModel: public wxDataViewModel
119 {
120 public:
121
122 // constructor
123
124 MyMusicModel()
125 {
126 m_root = new MyMusicModelNode( NULL, "My Music" );
127 m_pop = new MyMusicModelNode( m_root, "Pop music" );
128 m_root->Append( m_pop );
129 m_pop->Append( new MyMusicModelNode( m_pop,
130 "You are not alone", "Michael Jackson", "1995" ) );
131 m_pop->Append( new MyMusicModelNode( m_pop,
132 "Take a bow", "Madonna", "1994" ) );
133 m_classical = new MyMusicModelNode( m_root, "Classical music" );
134 m_root->Append( m_classical );
135 m_classical->Append( new MyMusicModelNode( m_classical,
136 "Ninth symphony", "Ludwig van Beethoven", "1824" ) );
137 m_classical->Append( new MyMusicModelNode( m_classical,
138 "German Requiem", "Johannes Brahms", "1868" ) );
139 m_classicalMusicIsKnownToControl = false;
140 }
141
142 // helper methods to change the model
143
144 void AddToClassical( const wxString &title, const wxString &artist, const wxString &year )
145 {
146 // add to data
147 MyMusicModelNode *child_node =
148 new MyMusicModelNode( m_classical, title, artist, year );
149 m_classical->Append( child_node );
150
151 if (m_classicalMusicIsKnownToControl)
152 {
153 // notify control
154 wxDataViewItem child( (void*) child_node );
155 wxDataViewItem parent( (void*) m_classical );
156 ItemAdded( parent, child );
157 }
158 }
159
160 void Delete( const wxDataViewItem &item )
161 {
162 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
163 wxDataViewItem parent( node->GetParent() );
164 node->GetParent()->GetChildren().Remove( node );
165 delete node;
166
167 // notify control
168 ItemDeleted( parent, item );
169 }
170
171 // override sorting to always sort branches ascendingly
172
173 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
174 {
175 if (IsContainer(item1) && IsContainer(item2))
176 {
177 wxVariant value1,value2;
178 GetValue( value1, item1, 0 );
179 GetValue( value2, item2, 0 );
180
181 wxString str1 = value1.GetString();
182 wxString str2 = value2.GetString();
183 int res = str1.Cmp( str2 );
184 if (res) return res;
185
186 // items must be different
187 unsigned long litem1 = (unsigned long) item1.GetID();
188 unsigned long litem2 = (unsigned long) item2.GetID();
189
190 return litem1-litem2;
191 }
192
193 return wxDataViewModel::Compare( item1, item2 );
194 }
195
196 // implementation of base class virtuals to define model
197
198 virtual unsigned int GetColumnCount() const
199 {
200 return 3;
201 }
202
203 virtual wxString GetColumnType( unsigned int col ) const
204 {
205 return "string";
206 }
207
208 virtual void GetValue( wxVariant &variant,
209 const wxDataViewItem &item, unsigned int col ) const
210 {
211 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
212 switch (col)
213 {
214 case 0: variant = node->m_title; break;
215 case 1: variant = node->m_artist; break;
216 case 2: variant = node->m_year; break;
217 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
218 }
219 }
220
221 virtual bool SetValue( const wxVariant &variant,
222 const wxDataViewItem &item, unsigned int col )
223 {
224 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
225 switch (col)
226 {
227 case 0: node->m_title = variant.GetString(); break;
228 case 1: node->m_artist = variant.GetString(); break;
229 case 2: node->m_year = variant.GetString(); break;
230 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
231 }
232 }
233
234 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
235 {
236 // the invisble root node has no parent
237 if (!item.IsOk())
238 return wxDataViewItem(0);
239
240 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
241
242 // "MyMusic" also has no parent
243 if (node == m_root)
244 return wxDataViewItem(0);
245
246 return wxDataViewItem( (void*) node->GetParent() );
247 }
248
249 virtual bool IsContainer( const wxDataViewItem &item ) const
250 {
251 // the invisble root node can have children (in
252 // our model always "MyMusic")
253 if (!item.IsOk())
254 return true;
255
256 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
257 return node->IsContainer();
258 }
259
260 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
261 {
262 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
263 if (!node)
264 return wxDataViewItem( (void*) m_root );
265
266 if (node->GetChildCount() == 0)
267 return wxDataViewItem( 0 );
268
269 if (node == m_classical)
270 {
271 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
272 model->m_classicalMusicIsKnownToControl = true;
273 }
274
275 MyMusicModelNode *first_child = node->GetChildren().Item( 0 );
276 return wxDataViewItem( (void*) first_child );
277 }
278
279 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
280 {
281 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
282
283 // "MyMusic" has no siblings in our model
284 if (node == m_root)
285 return wxDataViewItem(0);
286
287 MyMusicModelNode *parent = node->GetParent();
288 int pos = parent->GetChildren().Index( node );
289
290 // Something went wrong
291 if (pos == wxNOT_FOUND)
292 return wxDataViewItem(0);
293
294 // No more children
295 if (pos == parent->GetChildCount()-1)
296 return wxDataViewItem(0);
297
298 node = parent->GetChildren().Item( pos+1 );
299 return wxDataViewItem( (void*) node );
300 }
301
302 private:
303 MyMusicModelNode* m_root;
304 MyMusicModelNode* m_pop;
305 MyMusicModelNode* m_classical;
306 bool m_classicalMusicIsKnownToControl;
307 };
308
309 class MyListModel: public wxDataViewIndexListModel
310 {
311 public:
312 MyListModel() :
313 wxDataViewIndexListModel( 100 )
314 {
315 unsigned int i;
316 for (i = 0; i < 100; i++)
317 {
318 wxString str;
319 str.Printf( "row number %d", i );
320 m_array.Add( str );
321 }
322 }
323
324 // helper methods to change the model
325
326 void Prepend( const wxString &text )
327 {
328 m_array.Insert( text, 0 );
329 RowPrepended();
330 }
331
332 void DeleteItem( const wxDataViewItem &item )
333 {
334 unsigned int row = GetRow( item );
335 m_array.RemoveAt( row );
336 RowDeleted( row );
337 }
338
339 // implementation of base class virtuals to define model
340
341 virtual unsigned int GetColumnCount() const
342 {
343 return 2;
344 }
345
346 virtual wxString GetColumnType( unsigned int col ) const
347 {
348 return "string";
349 }
350
351 virtual unsigned int GetRowCount()
352 {
353 return m_array.GetCount();
354 }
355
356 virtual void GetValue( wxVariant &variant,
357 unsigned int row, unsigned int col ) const
358 {
359 if (col==0)
360 {
361 variant = m_array[ row ];
362 }
363 else
364 {
365 wxString str;
366 str.Printf( "row %d col %d", row, col );
367 variant = str;
368 }
369 }
370
371 virtual bool SetValue( const wxVariant &variant,
372 unsigned int row, unsigned int col )
373 {
374 if (col == 0)
375 {
376 m_array[row] = variant.GetString();
377 return true;
378 }
379
380 return false;
381 }
382
383 wxArrayString m_array;
384 };
385
386 // -------------------------------------
387 // MyApp
388 // -------------------------------------
389
390 class MyApp: public wxApp
391 {
392 public:
393 bool OnInit(void);
394 int OnExit();
395 };
396
397 // -------------------------------------
398 // MyFrame
399 // -------------------------------------
400
401 class MyFrame : public wxFrame
402 {
403 public:
404 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
405
406 public:
407 void OnQuit(wxCommandEvent& event);
408 void OnAbout(wxCommandEvent& event);
409
410 void OnAddMozart(wxCommandEvent& event);
411 void OnDeleteMusic(wxCommandEvent& event);
412
413 void OnPrependList(wxCommandEvent& event);
414 void OnDeleteList(wxCommandEvent& event);
415
416 void OnValueChanged( wxDataViewEvent &event );
417 void OnItemAdded( wxDataViewEvent &event );
418 void OnItemDeleted( wxDataViewEvent &event );
419
420 private:
421 wxDataViewCtrl* m_musicCtrl;
422 wxObjectDataPtr<MyMusicModel> m_music_model;
423
424 wxDataViewCtrl* m_listCtrl;
425 wxObjectDataPtr<MyListModel> m_list_model;
426
427 wxTextCtrl * m_log;
428
429 private:
430 DECLARE_EVENT_TABLE()
431 };
432
433 // -------------------------------------
434 // MyApp
435 // -------------------------------------
436
437 IMPLEMENT_APP(MyApp)
438
439 bool MyApp::OnInit(void)
440 {
441 if ( !wxApp::OnInit() )
442 return false;
443
444 // build the first frame
445 MyFrame *frame =
446 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
447 frame->Show(true);
448
449 SetTopWindow(frame);
450 return true;
451 }
452
453 int MyApp::OnExit()
454 {
455 return 0;
456 }
457
458
459 // -------------------------------------
460 // MyFrame
461 // -------------------------------------
462
463 enum
464 {
465 // file menu
466 ID_ABOUT = wxID_ABOUT,
467 ID_EXIT = wxID_EXIT,
468
469 ID_MUSIC_CTRL = 50,
470
471 ID_ADD_MOZART = 100,
472 ID_DELETE_MUSIC = 101,
473
474 ID_PREPEND_LIST = 200,
475 ID_DELETE_LIST = 201
476 };
477
478 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
479 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
480 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
481 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
482 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
483 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
484 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
485 EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL, MyFrame::OnItemAdded )
486 EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL, MyFrame::OnItemDeleted )
487 EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
488 END_EVENT_TABLE()
489
490 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
491 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
492 {
493 m_log = NULL;
494
495 SetIcon(wxICON(sample));
496
497 // build the menus:
498
499 wxMenu *file_menu = new wxMenu;
500 file_menu->Append(ID_ABOUT, "&About");
501 file_menu->AppendSeparator();
502 file_menu->Append(ID_EXIT, "E&xit");
503
504 wxMenuBar *menu_bar = new wxMenuBar;
505 menu_bar->Append(file_menu, "&File");
506
507 SetMenuBar(menu_bar);
508 CreateStatusBar();
509
510 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
511
512 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
513
514 // MyMusic
515
516 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
517 wxDefaultSize );
518
519 m_music_model = new MyMusicModel;
520 m_musicCtrl->AssociateModel( m_music_model.get() );
521
522 m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
523 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
524 m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 200,
525 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
526 m_musicCtrl->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
527 DEFAULT_ALIGN );
528
529 data_sizer->Add( m_musicCtrl, 3, wxGROW );
530
531 #if 1
532
533 // MyList
534
535 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
536 wxDefaultSize );
537
538 m_list_model = new MyListModel;
539 m_listCtrl->AssociateModel( m_list_model.get() );
540
541 m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
542 m_listCtrl->AppendTextColumn( "index", 1, wxDATAVIEW_CELL_INERT, 120 );
543
544 data_sizer->Add( m_listCtrl, 2, wxGROW );
545
546 #endif
547
548 main_sizer->Add( data_sizer, 2, wxGROW );
549
550 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
551
552 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 );
553 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 );
554 button_sizer->Add( 10, 10, 1 );
555 button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 );
556 button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
557
558 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
559
560 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
561
562 main_sizer->Add( m_log, 1, wxGROW );
563
564 SetSizer( main_sizer );
565 }
566
567 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
568 {
569 Close(true);
570 }
571
572 void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
573 {
574 m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
575 }
576
577 void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
578 {
579 wxDataViewItem item = m_musicCtrl->GetSelection();
580 if (item.IsOk())
581 m_music_model->Delete( item );
582 }
583
584 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
585 {
586 m_list_model->Prepend( "Test" );
587 }
588
589 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
590 {
591 wxDataViewItem item = m_listCtrl->GetSelection();
592 if (item.IsOk())
593 m_list_model->DeleteItem( item );
594 }
595
596 void MyFrame::OnItemAdded( wxDataViewEvent &event )
597 {
598 if (!m_log)
599 return;
600
601 m_log->AppendText( "EVT_DATAVIEW_MODEL_ITEM_ADDED\n" );
602 }
603
604 void MyFrame::OnItemDeleted( wxDataViewEvent &event )
605 {
606 if (!m_log)
607 return;
608
609 m_log->AppendText( "EVT_DATAVIEW_MODEL_ITEM_DELETED\n" );
610 }
611
612 void MyFrame::OnValueChanged( wxDataViewEvent &event )
613 {
614 if (!m_log)
615 return;
616
617 m_log->AppendText( "EVT_DATAVIEW_MODEL_VALUE_CHANGED\n" );
618 }
619
620 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
621 {
622 wxAboutDialogInfo info;
623 info.SetName(_("DataView sample"));
624 info.SetDescription(_("This sample demonstrates the dataview control handling"));
625 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
626
627 wxAboutBox(info);
628 }
629