Added wxDataViewListIndexModel::RowsDeleted() and various related corrections
[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, Bo Yang
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 /* XPM */
38 static const char *small1_xpm[] = {
39 /* columns rows colors chars-per-pixel */
40 "16 16 6 1",
41 ". c Black",
42 "o c #FFFFFF",
43 "X c #000080",
44 "O c #FFFF00",
45 " c None",
46 "+ c #FF0000",
47 /* pixels */
48 " ",
49 " ",
50 " ",
51 " ....... ",
52 " .XXXXX. ",
53 " .oXXXX. ",
54 " .oXXX.......",
55 ".....oXXX.OOOOO.",
56 ".+++.XXXX.oOOOO.",
57 ".o++......oOOOO.",
58 ".o++++. .oOOOO.",
59 ".o++++. .OOOOO.",
60 ".+++++. .......",
61 "....... ",
62 " ",
63 " "
64 };
65
66
67
68 #define DEFAULT_ALIGN wxALIGN_LEFT
69 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
70
71
72 // -------------------------------------
73 // MyMusicModel
74 // -------------------------------------
75
76 /*
77 Implement this data model
78 Title Artist Year
79 -------------------------------------------------------------
80 1: My Music:
81 2: Pop music
82 3: You are not alone Michael Jackson 1995
83 4: Take a bow Madonna 1994
84 5: Classical music
85 6: Ninth Symphony Ludwig v. Beethoven 1824
86 7: German Requiem Johannes Brahms 1868
87 */
88
89
90
91 class MyMusicModelNode;
92 WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
93
94 class MyMusicModelNode
95 {
96 public:
97 MyMusicModelNode( MyMusicModelNode* parent,
98 const wxString &title, const wxString &artist, int year )
99 {
100 m_parent = parent;
101 m_title = title;
102 m_artist = artist;
103 m_year = year;
104 m_isContainer = false;
105 }
106
107 MyMusicModelNode( MyMusicModelNode* parent,
108 const wxString &branch )
109 {
110 m_parent = parent;
111 m_title = branch;
112 m_year = -1;
113 m_isContainer = true;
114 }
115
116 ~MyMusicModelNode()
117 {
118 size_t count = m_children.GetCount();
119 size_t i;
120 for (i = 0; i < count; i++)
121 {
122 MyMusicModelNode *child = m_children[i];
123 delete child;
124 }
125 }
126
127 bool IsContainer() { return m_isContainer; }
128
129 MyMusicModelNode* GetParent() { return m_parent; }
130 MyMusicModelNodes &GetChildren() { return m_children; }
131 MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
132 void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
133 void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
134 unsigned int GetChildCount() { return m_children.GetCount(); }
135
136 public:
137 wxString m_title;
138 wxString m_artist;
139 int m_year;
140
141 private:
142 MyMusicModelNode *m_parent;
143 MyMusicModelNodes m_children;
144 bool m_isContainer;
145 };
146
147
148 class MyMusicModel: public wxDataViewModel
149 {
150 public:
151
152 // constructor
153
154 MyMusicModel()
155 {
156 m_root = new MyMusicModelNode( NULL, "My Music" );
157 m_pop = new MyMusicModelNode( m_root, "Pop music" );
158 m_root->Append( m_pop );
159 m_pop->Append( new MyMusicModelNode( m_pop,
160 "You are not alone", "Michael Jackson", 1995 ) );
161 m_pop->Append( new MyMusicModelNode( m_pop,
162 "Take a bow", "Madonna", 1994 ) );
163 m_classical = new MyMusicModelNode( m_root, "Classical music" );
164 m_root->Append( m_classical );
165 m_classical->Append( new MyMusicModelNode( m_classical,
166 "Ninth symphony", "Ludwig van Beethoven", 1824 ) );
167 m_classical->Append( new MyMusicModelNode( m_classical,
168 "German Requiem", "Johannes Brahms", 1868 ) );
169 m_classicalMusicIsKnownToControl = false;
170 }
171
172 // helper method for wxLog
173
174 wxString GetTitle( const wxDataViewItem &item )
175 {
176 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
177 if (!node)
178 return wxEmptyString;
179
180 return node->m_title;
181 }
182
183 // helper methods to change the model
184
185 void AddToClassical( const wxString &title, const wxString &artist, int year )
186 {
187 // add to data
188 MyMusicModelNode *child_node =
189 new MyMusicModelNode( m_classical, title, artist, year );
190
191 m_classical->Append( child_node );
192
193 if (m_classicalMusicIsKnownToControl)
194 {
195 // notify control
196 wxDataViewItem child( (void*) child_node );
197 wxDataViewItem parent( (void*) m_classical );
198 ItemAdded( parent, child );
199 }
200 }
201
202 void Delete( const wxDataViewItem &item )
203 {
204 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
205 wxDataViewItem parent( node->GetParent() );
206
207 node->GetParent()->GetChildren().Remove( node );
208 delete node;
209
210 // notify control
211 ItemDeleted( parent, item );
212 }
213
214 // override sorting to always sort branches ascendingly
215
216 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
217 unsigned int column, bool ascending )
218 {
219 if (IsContainer(item1) && IsContainer(item2))
220 {
221 wxVariant value1,value2;
222 GetValue( value1, item1, 0 );
223 GetValue( value2, item2, 0 );
224
225 wxString str1 = value1.GetString();
226 wxString str2 = value2.GetString();
227 int res = str1.Cmp( str2 );
228 if (res) return res;
229
230 // items must be different
231 unsigned long litem1 = (unsigned long) item1.GetID();
232 unsigned long litem2 = (unsigned long) item2.GetID();
233
234 return litem1-litem2;
235 }
236
237 return wxDataViewModel::Compare( item1, item2, column, ascending );
238 }
239
240 // implementation of base class virtuals to define model
241
242 virtual unsigned int GetColumnCount() const
243 {
244 return 3;
245 }
246
247 virtual wxString GetColumnType( unsigned int col ) const
248 {
249 if (col == 2)
250 return "long";
251
252 return "string";
253 }
254
255 virtual void GetValue( wxVariant &variant,
256 const wxDataViewItem &item, unsigned int col ) const
257 {
258 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
259 switch (col)
260 {
261 case 0: variant = node->m_title; break;
262 case 1: variant = node->m_artist; break;
263 case 2: variant = (long) node->m_year; break;
264 default:
265 {
266 wxLogError( "MyMusicModel::GetValue: wrong column" );
267
268 // provoke a crash when mouse button down
269 wxMouseState state = wxGetMouseState();
270 if (state.ShiftDown())
271 {
272 char *crash = 0;
273 *crash = 0;
274 }
275 }
276 }
277 }
278
279 virtual bool SetValue( const wxVariant &variant,
280 const wxDataViewItem &item, unsigned int col )
281 {
282 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
283 switch (col)
284 {
285 case 0: node->m_title = variant.GetString(); return true;
286 case 1: node->m_artist = variant.GetString(); return true;
287 case 2: node->m_year = variant.GetLong(); return true;
288 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
289 }
290 return false;
291 }
292
293 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
294 {
295 // the invisble root node has no parent
296 if (!item.IsOk())
297 return wxDataViewItem(0);
298
299 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
300
301 // "MyMusic" also has no parent
302 if (node == m_root)
303 return wxDataViewItem(0);
304
305 return wxDataViewItem( (void*) node->GetParent() );
306 }
307
308 virtual bool IsContainer( const wxDataViewItem &item ) const
309 {
310 // the invisble root node can have children (in
311 // our model always "MyMusic")
312 if (!item.IsOk())
313 return true;
314
315 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
316 return node->IsContainer();
317 }
318
319 virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const
320 {
321 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
322 if (!node)
323 {
324 array.Add( wxDataViewItem( (void*) m_root ) );
325 return 1;
326 }
327
328 if (node == m_classical)
329 {
330 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
331 model->m_classicalMusicIsKnownToControl = true;
332 }
333
334 if (node->GetChildCount() == 0)
335 {
336 return 0;
337 }
338
339 unsigned int count = node->GetChildren().GetCount();
340 unsigned int pos;
341 for (pos = 0; pos < count; pos++)
342 {
343 MyMusicModelNode *child = node->GetChildren().Item( pos );
344 array.Add( wxDataViewItem( (void*) child ) );
345 }
346 return count;
347 }
348
349 private:
350 MyMusicModelNode* m_root;
351 MyMusicModelNode* m_pop;
352 MyMusicModelNode* m_classical;
353 bool m_classicalMusicIsKnownToControl;
354 };
355
356
357 static int my_sort_reverse( int *v1, int *v2 )
358 {
359 return *v2-*v1;
360 }
361
362 static int my_sort( int *v1, int *v2 )
363 {
364 return *v1-*v2;
365 }
366
367 class MyListModel: public wxDataViewIndexListModel
368 {
369 public:
370 MyListModel() :
371 #ifdef __WXMAC__
372 wxDataViewIndexListModel( 1000 )
373 #else
374 wxDataViewIndexListModel( 100000 )
375 #endif
376 {
377 unsigned int i;
378 for (i = 0; i < 100; i++)
379 {
380 wxString str;
381 str.Printf( "row number %d", i );
382 m_array.Add( str );
383 }
384
385 m_icon = wxIcon( null_xpm );
386 }
387
388 // helper methods to change the model
389
390 void Prepend( const wxString &text )
391 {
392 m_array.Insert( text, 0 );
393 RowPrepended();
394 }
395
396 void DeleteItem( const wxDataViewItem &item )
397 {
398 unsigned int row = GetRow( item );
399 m_array.RemoveAt( row );
400 RowDeleted( row );
401 }
402
403 void DeleteItems( const wxDataViewItemArray &items )
404 {
405 wxArrayInt rows;
406 unsigned int i;
407 for (i = 0; i < items.GetCount(); i++)
408 {
409 unsigned int row = GetRow( items[i] );
410 rows.Add( row );
411 }
412
413 // Sort in descending order so that the last
414 // row will be deleted first. Otherwise the
415 // remaining indeces would all be wrong.
416 rows.Sort( my_sort_reverse );
417 for (i = 0; i < rows.GetCount(); i++)
418 m_array.RemoveAt( rows[i] );
419
420 // This is just to test if wxDataViewCtrl can
421 // cope with removing rows not sorted in
422 // descending order
423 rows.Sort( my_sort );
424 RowsDeleted( rows );
425 }
426
427 void AddMany()
428 {
429 }
430
431 // implementation of base class virtuals to define model
432
433 virtual unsigned int GetColumnCount() const
434 {
435 return 3;
436 }
437
438 virtual wxString GetColumnType( unsigned int col ) const
439 {
440 if (col == 1)
441 return "wxDataViewIconText";
442
443 return "string";
444 }
445
446 virtual unsigned int GetRowCount()
447 {
448 return m_array.GetCount();
449 }
450
451 virtual void GetValue( wxVariant &variant,
452 unsigned int row, unsigned int col ) const
453 {
454 if (col==0)
455 {
456 if (row >= m_array.GetCount())
457 {
458 wxString str;
459 str.Printf( "row %d", row );
460 variant = str;
461 }
462 else
463 {
464 variant = m_array[ row ];
465 }
466 } else
467 if (col==1)
468 {
469 wxDataViewIconText data( "test", m_icon );
470 variant << data;
471 } else
472 if (col==2)
473 {
474 if ((row % 2) == 1)
475 variant = "Blue";
476 else
477 variant = "Italic";
478 }
479 }
480
481 virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr )
482 {
483 if (col != 2)
484 return false;
485
486 if ((row % 2) == 1)
487 attr.SetColour( *wxBLUE );
488 else
489 attr.SetItalic( true );
490
491 return true;
492 }
493
494 virtual bool SetValue( const wxVariant &variant,
495 unsigned int row, unsigned int col )
496 {
497 if (col == 0)
498 {
499 if (row >= m_array.GetCount())
500 return false;
501
502 m_array[row] = variant.GetString();
503 return true;
504 }
505
506 return false;
507 }
508
509 wxArrayString m_array;
510 wxIcon m_icon;
511 };
512
513 // -------------------------------------
514 // MyApp
515 // -------------------------------------
516
517 class MyApp: public wxApp
518 {
519 public:
520 bool OnInit(void);
521 int OnExit();
522 };
523
524 // -------------------------------------
525 // MyFrame
526 // -------------------------------------
527
528 class MyFrame : public wxFrame
529 {
530 public:
531 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
532
533 public:
534 void OnQuit(wxCommandEvent& event);
535 void OnAbout(wxCommandEvent& event);
536
537 void OnAddMozart(wxCommandEvent& event);
538 void OnDeleteMusic(wxCommandEvent& event);
539 void OnDeleteYear(wxCommandEvent& event);
540
541 void OnPrependList(wxCommandEvent& event);
542 void OnDeleteList(wxCommandEvent& event);
543
544 void OnValueChanged( wxDataViewEvent &event );
545
546 void OnActivated( wxDataViewEvent &event );
547 void OnExpanding( wxDataViewEvent &event );
548 void OnExpanded( wxDataViewEvent &event );
549 void OnCollapsing( wxDataViewEvent &event );
550 void OnCollapsed( wxDataViewEvent &event );
551 void OnSelectionChanged( wxDataViewEvent &event );
552
553 void OnEditingStarted( wxDataViewEvent &event );
554 void OnEditingDone( wxDataViewEvent &event );
555
556 void OnHeaderClick( wxDataViewEvent &event );
557 void OnHeaderRightClick( wxDataViewEvent &event );
558 void OnSorted( wxDataViewEvent &event );
559
560 void OnContextMenu( wxDataViewEvent &event );
561
562 void OnRightClick( wxMouseEvent &event );
563 void OnGoto( wxCommandEvent &event);
564 void OnAddMany( wxCommandEvent &event);
565
566 private:
567 wxDataViewCtrl* m_musicCtrl;
568 wxObjectDataPtr<MyMusicModel> m_music_model;
569
570 wxDataViewCtrl* m_listCtrl;
571 wxObjectDataPtr<MyListModel> m_list_model;
572
573 wxDataViewColumn * m_col;
574
575 wxTextCtrl * m_log;
576 wxLog *m_logOld;
577
578 private:
579 DECLARE_EVENT_TABLE()
580 };
581
582 // -------------------------------------
583 // MyApp
584 // -------------------------------------
585
586 IMPLEMENT_APP(MyApp)
587
588 bool MyApp::OnInit(void)
589 {
590 if ( !wxApp::OnInit() )
591 return false;
592
593 // build the first frame
594 MyFrame *frame =
595 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 540);
596 frame->Show(true);
597
598 SetTopWindow(frame);
599 return true;
600 }
601
602 int MyApp::OnExit()
603 {
604 return 0;
605 }
606
607
608 // -------------------------------------
609 // MyFrame
610 // -------------------------------------
611
612 enum
613 {
614 // file menu
615 ID_ABOUT = wxID_ABOUT,
616 ID_EXIT = wxID_EXIT,
617
618 ID_MUSIC_CTRL = 50,
619
620 ID_ADD_MOZART = 100,
621 ID_DELETE_MUSIC = 101,
622 ID_DELETE_YEAR = 102,
623
624 ID_PREPEND_LIST = 200,
625 ID_DELETE_LIST = 201,
626 ID_GOTO = 202,
627 ID_ADD_MANY = 203
628 };
629
630 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
631 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
632 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
633 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
634 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
635 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
636 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
637 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
638 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
639 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
640
641 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
642
643 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
644 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
645 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
646 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
647 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
648 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
649
650 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
651 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
652
653 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
654 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
655 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
656
657 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
658
659 EVT_RIGHT_UP(MyFrame::OnRightClick)
660 END_EVENT_TABLE()
661
662 MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
663 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
664 {
665 m_log = NULL;
666 m_col = NULL;
667
668 SetIcon(wxICON(sample));
669
670 // build the menus:
671
672 wxMenu *file_menu = new wxMenu;
673 file_menu->Append(ID_ABOUT, "&About");
674 file_menu->AppendSeparator();
675 file_menu->Append(ID_EXIT, "E&xit");
676
677 wxMenuBar *menu_bar = new wxMenuBar;
678 menu_bar->Append(file_menu, "&File");
679
680 SetMenuBar(menu_bar);
681 CreateStatusBar();
682
683 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
684
685 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
686
687 // MyMusic
688
689 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
690 wxDefaultSize, wxDV_MULTIPLE );
691
692 m_music_model = new MyMusicModel;
693 m_musicCtrl->AssociateModel( m_music_model.get() );
694
695 /* wxDataViewColumn *col = */ m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
696 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
697 #if 0
698 // Call this and sorting is enabled
699 // immediatly upon start up.
700 col->SetSortOrder( true );
701 #endif
702
703 m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 150,
704 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
705
706 wxDataViewSpinRenderer *sr = new wxDataViewSpinRenderer( 0, 2010 );
707 wxDataViewColumn *column = new wxDataViewColumn( "year", sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE );
708 m_musicCtrl->AppendColumn( column );
709
710 data_sizer->Add( m_musicCtrl, 3, wxGROW );
711
712 #if 1
713
714 // MyList
715
716 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
717 wxDefaultSize, wxDV_MULTIPLE );
718
719 m_list_model = new MyListModel;
720 m_listCtrl->AssociateModel( m_list_model.get() );
721
722 m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
723 m_listCtrl->AppendIconTextColumn( "icon", 1, wxDATAVIEW_CELL_INERT, 60 );
724
725 wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr;
726 column = new wxDataViewColumn( "attributes", ra, 2 );
727 m_listCtrl->AppendColumn( column );
728
729 data_sizer->Add( m_listCtrl, 2, wxGROW );
730
731 #endif
732
733 main_sizer->Add( data_sizer, 2, wxGROW );
734
735 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
736
737 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 );
738 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 );
739 button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, "Delete \"Year\" column"), 0, wxALL, 10 );
740 button_sizer->Add( 10, 10, 1 );
741 wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 );
742 grid_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 2 );
743 grid_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 2 );
744 grid_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 2 );
745 grid_sizer->Add( new wxButton( this, ID_ADD_MANY, "Add 1000"), 0, wxALL, 2 );
746 button_sizer->Add( grid_sizer, 0, wxALL, 10 );
747
748 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
749
750 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
751
752 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
753 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
754 wxLogMessage("This is the log window");
755
756 bottom_sizer->Add( m_log, 1, wxGROW );
757
758 // wxDataViewTreeStore
759
760 wxDataViewCtrl *treectrl = new wxDataViewCtrl( this, -1,
761 wxDefaultPosition, wxSize(300,200), wxDV_NO_HEADER );
762
763 wxDataViewTreeStore *store = new wxDataViewTreeStore;
764 wxDataViewItem parent = store->AppendContainer( wxDataViewItem(0), "Root 1", wxIcon(small1_xpm) );
765 wxDataViewItem child = store->AppendItem( parent, "Child 1", wxIcon(small1_xpm) );
766 child = store->AppendItem( parent, "Child 2", wxIcon(small1_xpm) );
767 child = store->AppendItem( parent, "Child 3", wxIcon(small1_xpm) );
768 treectrl->AssociateModel( store );
769 store->DecRef();
770
771 treectrl->AppendIconTextColumn( "no label", 0, wxDATAVIEW_CELL_INERT, 200 );
772
773 bottom_sizer->Add( treectrl );
774
775 // wxDataViewTreeCtrl
776
777 wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( this, -1, wxDefaultPosition, wxSize(300,200) );
778
779 wxImageList *ilist = new wxImageList;
780 ilist->Add( wxIcon(small1_xpm) );
781 treectrl2->SetImageList( ilist );
782
783 parent = treectrl2->AppendContainer( wxDataViewItem(0), "Root 1", 0 );
784 child = treectrl2->AppendItem( parent, "Child 1", 0 );
785 child = treectrl2->AppendItem( parent, "Child 2", 0 );
786 child = treectrl2->AppendItem( parent, "Child 3", 0 );
787
788 bottom_sizer->Add( treectrl2 );
789
790 // main sizer
791
792 main_sizer->Add( bottom_sizer, 0, wxGROW );
793
794 SetSizer( main_sizer );
795 }
796
797 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
798 {
799 Close(true);
800 }
801
802 void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
803 {
804 m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
805 }
806
807 void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
808 {
809 wxDataViewItemArray items;
810 int len = m_musicCtrl->GetSelections( items );
811 for( int i = 0; i < len; i ++ )
812 if (items[i].IsOk())
813 m_music_model->Delete( items[i] );
814 }
815
816 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
817 {
818 m_musicCtrl->DeleteColumn( m_musicCtrl->GetColumn( 2 ) );
819 FindWindow( ID_DELETE_YEAR )->Disable();
820 }
821
822 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
823 {
824 m_list_model->Prepend( "Test" );
825 }
826
827 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
828 {
829 wxDataViewItemArray items;
830 int len = m_listCtrl->GetSelections( items );
831 if (len > 0)
832 m_list_model->DeleteItems( items );
833 }
834
835 void MyFrame::OnValueChanged( wxDataViewEvent &event )
836 {
837 if (!m_log)
838 return;
839
840 wxLogMessage( "EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn() );
841 }
842
843 void MyFrame::OnActivated( wxDataViewEvent &event )
844 {
845 if(!m_log)
846 return;
847
848 wxString title = m_music_model->GetTitle( event.GetItem() );
849 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title );
850 }
851
852 void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
853 {
854 if(!m_log)
855 return;
856
857 wxString title = m_music_model->GetTitle( event.GetItem() );
858 if (title.empty())
859 title = "None";
860
861 wxLogMessage("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title );
862 }
863
864 void MyFrame::OnExpanding( wxDataViewEvent &event )
865 {
866 if (!m_log)
867 return;
868
869 wxString title = m_music_model->GetTitle( event.GetItem() );
870 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title );
871 }
872
873
874 void MyFrame::OnEditingStarted( wxDataViewEvent &event )
875 {
876 if (!m_log)
877 return;
878
879 wxString title = m_music_model->GetTitle( event.GetItem() );
880 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title );
881 }
882
883 void MyFrame::OnEditingDone( wxDataViewEvent &event )
884 {
885 if (!m_log)
886 return;
887
888 wxString title = m_music_model->GetTitle( event.GetItem() );
889 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title );
890 }
891
892 void MyFrame::OnExpanded( wxDataViewEvent &event )
893 {
894 if (!m_log)
895 return;
896
897 wxString title = m_music_model->GetTitle( event.GetItem() );
898 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title );
899 }
900
901 void MyFrame::OnCollapsing( wxDataViewEvent &event )
902 {
903 if (!m_log)
904 return;
905
906 wxString title = m_music_model->GetTitle( event.GetItem() );
907 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title );
908 }
909
910 void MyFrame::OnCollapsed( wxDataViewEvent &event )
911 {
912 if (!m_log)
913 return;
914
915 wxString title = m_music_model->GetTitle( event.GetItem() );
916 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title );
917 }
918
919 void MyFrame::OnContextMenu( wxDataViewEvent &event )
920 {
921 if (!m_log)
922 return;
923
924 wxString title = m_music_model->GetTitle( event.GetItem() );
925 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title );
926 }
927
928 void MyFrame::OnHeaderClick( wxDataViewEvent &event )
929 {
930 if(!m_log)
931 return;
932
933 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
934
935 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
936 }
937
938 void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
939 {
940 if(!m_log)
941 return;
942
943 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
944
945 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos );
946 }
947
948 void MyFrame::OnSorted( wxDataViewEvent &event )
949 {
950 if(!m_log)
951 return;
952
953 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
954
955 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos );
956 }
957
958 void MyFrame::OnRightClick( wxMouseEvent &event )
959 {
960 if(!m_log)
961 return;
962
963 wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event.GetX(), event.GetY());
964 }
965
966 void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
967 {
968 wxDataViewItem item = m_list_model->GetItem( 50 );
969 m_listCtrl->EnsureVisible(item,m_col);
970 }
971
972 void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
973 {
974 m_list_model->AddMany();
975 }
976
977
978 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
979 {
980 wxAboutDialogInfo info;
981 info.SetName(_("DataView sample"));
982 info.SetDescription(_("This sample demonstrates the dataview control handling"));
983 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
984
985 wxAboutBox(info);
986 }
987