]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
added wxDataViewIndexListModel and 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
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 node->GetParent()->GetChildren().Remove( node );
164 delete node;
165
166 // notify control
167 ItemDeleted( item );
168 }
169
170 // override sorting to always sort branches ascendingly
171
172 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
173 {
174 if (IsContainer(item1) && IsContainer(item2))
175 {
176 wxVariant value1,value2;
177 GetValue( value1, item1, 0 );
178 GetValue( value2, item2, 0 );
179
180 wxString str1 = value1.GetString();
181 wxString str2 = value2.GetString();
182 int res = str1.Cmp( str2 );
183 if (res) return res;
184
185 // items must be different
186 unsigned long litem1 = (unsigned long) item1.GetID();
187 unsigned long litem2 = (unsigned long) item2.GetID();
188
189 return litem1-litem2;
190 }
191
192 return wxDataViewModel::Compare( item1, item2 );
193 }
194
195 // implementation of base class virtuals to define model
196
197 virtual unsigned int GetColumnCount() const
198 {
199 return 3;
200 }
201
202 virtual wxString GetColumnType( unsigned int col ) const
203 {
204 return "string";
205 }
206
207 virtual void GetValue( wxVariant &variant,
208 const wxDataViewItem &item, unsigned int col ) const
209 {
210 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
211 switch (col)
212 {
213 case 0: variant = node->m_title; break;
214 case 1: variant = node->m_artist; break;
215 case 2: variant = node->m_year; break;
216 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
217 }
218 }
219
220 virtual bool SetValue( const wxVariant &variant,
221 const wxDataViewItem &item, unsigned int col )
222 {
223 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
224 switch (col)
225 {
226 case 0: node->m_title = variant.GetString(); break;
227 case 1: node->m_artist = variant.GetString(); break;
228 case 2: node->m_year = variant.GetString(); break;
229 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
230 }
231 }
232
233 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
234 {
235 // the invisble root node has no parent
236 if (!item.IsOk())
237 return wxDataViewItem(0);
238
239 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
240
241 // "MyMusic" also has no parent
242 if (node == m_root)
243 return wxDataViewItem(0);
244
245 return wxDataViewItem( (void*) node->GetParent() );
246 }
247
248 virtual bool IsContainer( const wxDataViewItem &item ) const
249 {
250 // the invisble root node can have children (in
251 // our model always "MyMusic")
252 if (!item.IsOk())
253 return true;
254
255 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
256 return node->IsContainer();
257 }
258
259 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
260 {
261 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
262 if (!node)
263 return wxDataViewItem( (void*) m_root );
264
265 if (node->GetChildCount() == 0)
266 return wxDataViewItem( 0 );
267
268 if (node == m_classical)
269 {
270 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
271 model->m_classicalMusicIsKnownToControl = true;
272 }
273
274 MyMusicModelNode *first_child = node->GetChildren().Item( 0 );
275 return wxDataViewItem( (void*) first_child );
276 }
277
278 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
279 {
280 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
281
282 // "MyMusic" has no siblings in our model
283 if (node == m_root)
284 return wxDataViewItem(0);
285
286 MyMusicModelNode *parent = node->GetParent();
287 int pos = parent->GetChildren().Index( node );
288
289 // Something went wrong
290 if (pos == wxNOT_FOUND)
291 return wxDataViewItem(0);
292
293 // No more children
294 if (pos == parent->GetChildCount()-1)
295 return wxDataViewItem(0);
296
297 node = parent->GetChildren().Item( pos+1 );
298 return wxDataViewItem( (void*) node );
299 }
300
301 private:
302 MyMusicModelNode* m_root;
303 MyMusicModelNode* m_pop;
304 MyMusicModelNode* m_classical;
305 bool m_classicalMusicIsKnownToControl;
306 };
307
308 class MyListModel: public wxDataViewIndexListModel
309 {
310 public:
311 MyListModel() :
312 wxDataViewIndexListModel( 100 )
313 {
314 unsigned int i;
315 for (i = 0; i < 100; i++)
316 {
317 wxString str;
318 str.Printf( "row number %d", i );
319 m_array.Add( str );
320 }
321 }
322
323 // helper methods to change the model
324
325 void Prepend( const wxString &text )
326 {
327 m_array.Insert( text, 0 );
328 RowPrepended();
329 }
330
331 void DeleteItem( const wxDataViewItem &item )
332 {
333 unsigned int row = GetRow( item );
334 m_array.RemoveAt( row );
335 RowDeleted( row );
336 }
337
338 // implementation of base class virtuals to define model
339
340 virtual unsigned int GetColumnCount() const
341 {
342 return 2;
343 }
344
345 virtual wxString GetColumnType( unsigned int col ) const
346 {
347 return "string";
348 }
349
350 virtual unsigned int GetRowCount()
351 {
352 return m_array.GetCount();
353 }
354
355 virtual void GetValue( wxVariant &variant,
356 unsigned int row, unsigned int col ) const
357 {
358 if (col==0)
359 {
360 variant = m_array[ row ];
361 }
362 else
363 {
364 wxString str;
365 str.Printf( "row %d col %d", row, col );
366 variant = str;
367 }
368 }
369
370 virtual bool SetValue( const wxVariant &variant,
371 unsigned int row, unsigned int col )
372 {
373 if (col == 0)
374 {
375 m_array[row] = variant.GetString();
376 return true;
377 }
378
379 return false;
380 }
381
382 wxArrayString m_array;
383 };
384
385 // -------------------------------------
386 // MyApp
387 // -------------------------------------
388
389 class MyApp: public wxApp
390 {
391 public:
392 bool OnInit(void);
393 int OnExit();
394 };
395
396 // -------------------------------------
397 // MyFrame
398 // -------------------------------------
399
400 class MyFrame : public wxFrame
401 {
402 public:
403 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
404
405 public:
406 void OnQuit(wxCommandEvent& event);
407 void OnAbout(wxCommandEvent& event);
408
409 void OnAddMozart(wxCommandEvent& event);
410 void OnDeleteMusic(wxCommandEvent& event);
411
412 void OnPrependList(wxCommandEvent& event);
413 void OnDeleteList(wxCommandEvent& event);
414
415 private:
416 wxDataViewCtrl* m_musicCtrl;
417 wxObjectDataPtr<MyMusicModel> m_music_model;
418
419 wxDataViewCtrl* m_listCtrl;
420 wxObjectDataPtr<MyListModel> m_list_model;
421
422 wxTextCtrl * m_log;
423
424 private:
425 DECLARE_EVENT_TABLE()
426 };
427
428 // -------------------------------------
429 // MyApp
430 // -------------------------------------
431
432 IMPLEMENT_APP(MyApp)
433
434 bool MyApp::OnInit(void)
435 {
436 if ( !wxApp::OnInit() )
437 return false;
438
439 // build the first frame
440 MyFrame *frame =
441 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
442 frame->Show(true);
443
444 SetTopWindow(frame);
445 return true;
446 }
447
448 int MyApp::OnExit()
449 {
450 return 0;
451 }
452
453
454 // -------------------------------------
455 // MyFrame
456 // -------------------------------------
457
458 enum
459 {
460 // file menu
461 ID_ABOUT = wxID_ABOUT,
462 ID_EXIT = wxID_EXIT,
463
464 ID_ADD_MOZART = 100,
465 ID_DELETE_MUSIC = 101,
466
467 ID_PREPEND_LIST = 200,
468 ID_DELETE_LIST = 201
469 };
470
471 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
472 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
473 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
474 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
475 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
476 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
477 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
478 END_EVENT_TABLE()
479
480 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
481 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
482 {
483 SetIcon(wxICON(sample));
484
485 // build the menus:
486
487 wxMenu *file_menu = new wxMenu;
488 file_menu->Append(ID_ABOUT, "&About");
489 file_menu->AppendSeparator();
490 file_menu->Append(ID_EXIT, "E&xit");
491
492 wxMenuBar *menu_bar = new wxMenuBar;
493 menu_bar->Append(file_menu, "&File");
494
495 SetMenuBar(menu_bar);
496 CreateStatusBar();
497
498 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
499
500 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
501
502 // MyMusic
503
504 m_musicCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
505 wxDefaultSize );
506
507 m_music_model = new MyMusicModel;
508 m_musicCtrl->AssociateModel( m_music_model.get() );
509
510 m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
511 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
512 m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 200,
513 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
514 m_musicCtrl->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
515 DEFAULT_ALIGN );
516
517 data_sizer->Add( m_musicCtrl, 3, wxGROW );
518
519 #if 1
520
521 // MyList
522
523 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
524 wxDefaultSize );
525
526 m_list_model = new MyListModel;
527 m_listCtrl->AssociateModel( m_list_model.get() );
528
529 m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
530 m_listCtrl->AppendTextColumn( "index", 1, wxDATAVIEW_CELL_INERT, 120 );
531
532 data_sizer->Add( m_listCtrl, 2, wxGROW );
533
534 #endif
535
536 main_sizer->Add( data_sizer, 2, wxGROW );
537
538 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
539
540 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 );
541 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 );
542 button_sizer->Add( 10, 10, 1 );
543 button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 );
544 button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
545
546 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
547
548 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
549
550 main_sizer->Add( m_log, 1, wxGROW );
551
552 SetSizer( main_sizer );
553 }
554
555 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
556 {
557 Close(true);
558 }
559
560 void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
561 {
562 m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
563 }
564
565 void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
566 {
567 wxDataViewItem item = m_musicCtrl->GetSelection();
568 if (item.IsOk())
569 m_music_model->Delete( item );
570 }
571
572 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
573 {
574 m_list_model->Prepend( "Test" );
575 }
576
577 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
578 {
579 wxDataViewItem item = m_listCtrl->GetSelection();
580 if (item.IsOk())
581 m_list_model->DeleteItem( item );
582 }
583
584 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
585 {
586 wxAboutDialogInfo info;
587 info.SetName(_("DataView sample"));
588 info.SetDescription(_("This sample demonstrates the dataview control handling"));
589 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
590
591 wxAboutBox(info);
592 }
593