Patch from Hartwig and me for implementing reorderable dataview columns. Works on...
[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, wxT("My Music" ));
157 m_pop = new MyMusicModelNode( m_root, wxT("Pop music") );
158 m_root->Append( m_pop );
159 m_pop->Append( new MyMusicModelNode( m_pop,
160 wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
161 m_pop->Append( new MyMusicModelNode( m_pop,
162 wxT("Take a bow"), wxT("Madonna"), 1994 ) );
163 m_classical = new MyMusicModelNode( m_root, wxT("Classical music") );
164 m_root->Append( m_classical );
165 m_classical->Append( new MyMusicModelNode( m_classical,
166 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 ) );
167 m_classical->Append( new MyMusicModelNode( m_classical,
168 wxT("German Requiem"), wxT("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 wxT("long");
251
252 return wxT("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( wxT("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( wxT("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 + 100 )
373 #else
374 wxDataViewIndexListModel( 100000 + 100 )
375 #endif
376 {
377 #ifdef __WXMAC__
378 m_virtualItems = 1000;
379 #else
380 m_virtualItems = 100000;
381 #endif
382
383 unsigned int i;
384 for (i = 0; i < 100; i++)
385 {
386 wxString str;
387 str.Printf( wxT("row number %d"), i );
388 m_array.Add( str );
389 }
390
391 m_icon = wxIcon( null_xpm );
392 }
393
394 // helper methods to change the model
395
396 void Prepend( const wxString &text )
397 {
398 m_array.Insert( text, 0 );
399 RowPrepended();
400 }
401
402 void DeleteItem( const wxDataViewItem &item )
403 {
404 unsigned int row = GetRow( item );
405 if (row >= m_array.GetCount())
406 return;
407
408 m_array.RemoveAt( row );
409 RowDeleted( row );
410 }
411
412 void DeleteItems( const wxDataViewItemArray &items )
413 {
414 wxArrayInt rows;
415 unsigned int i;
416 for (i = 0; i < items.GetCount(); i++)
417 {
418 unsigned int row = GetRow( items[i] );
419 if (row < m_array.GetCount())
420 rows.Add( row );
421 }
422
423 // Sort in descending order so that the last
424 // row will be deleted first. Otherwise the
425 // remaining indeces would all be wrong.
426 rows.Sort( my_sort_reverse );
427 for (i = 0; i < rows.GetCount(); i++)
428 m_array.RemoveAt( rows[i] );
429
430 // This is just to test if wxDataViewCtrl can
431 // cope with removing rows not sorted in
432 // descending order
433 rows.Sort( my_sort );
434 RowsDeleted( rows );
435 }
436
437 void AddMany()
438 {
439 m_virtualItems += 1000;
440 Reset( m_array.GetCount() + m_virtualItems );
441 }
442
443 // implementation of base class virtuals to define model
444
445 virtual unsigned int GetColumnCount() const
446 {
447 return 3;
448 }
449
450 virtual wxString GetColumnType( unsigned int col ) const
451 {
452 if (col == 1)
453 return wxT("wxDataViewIconText");
454
455 return wxT("string");
456 }
457
458 virtual unsigned int GetRowCount()
459 {
460 return m_array.GetCount();
461 }
462
463 virtual void GetValue( wxVariant &variant,
464 unsigned int row, unsigned int col ) const
465 {
466 if (col==0)
467 {
468 if (row >= m_array.GetCount())
469 {
470 wxString str;
471 str.Printf(wxT("row %d"), row - m_array.GetCount() );
472 variant = str;
473 }
474 else
475 {
476 variant = m_array[ row ];
477 }
478 } else
479 if (col==1)
480 {
481 wxDataViewIconText data( wxT("test"), m_icon );
482 variant << data;
483 } else
484 if (col==2)
485 {
486 if (row >= m_array.GetCount())
487 variant = wxT("plain");
488 else
489 variant = wxT("blue");
490 }
491 }
492
493 virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr )
494 {
495 if (col != 2)
496 return false;
497
498 if (row < m_array.GetCount())
499 {
500 attr.SetColour( *wxBLUE );
501 attr.SetItalic( true );
502 }
503
504 return true;
505 }
506
507 virtual bool SetValue( const wxVariant &variant,
508 unsigned int row, unsigned int col )
509 {
510 if (col == 0)
511 {
512 if (row >= m_array.GetCount())
513 return false;
514
515 m_array[row] = variant.GetString();
516 return true;
517 }
518
519 return false;
520 }
521
522 wxArrayString m_array;
523 wxIcon m_icon;
524 int m_virtualItems;
525 };
526
527 // -------------------------------------
528 // MyApp
529 // -------------------------------------
530
531 class MyApp: public wxApp
532 {
533 public:
534 bool OnInit(void);
535 int OnExit();
536 };
537
538 // -------------------------------------
539 // MyFrame
540 // -------------------------------------
541
542 class MyFrame : public wxFrame
543 {
544 public:
545 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
546
547 public:
548 void OnQuit(wxCommandEvent& event);
549 void OnAbout(wxCommandEvent& event);
550
551 void OnAddMozart(wxCommandEvent& event);
552 void OnDeleteMusic(wxCommandEvent& event);
553 void OnDeleteYear(wxCommandEvent& event);
554
555 void OnPrependList(wxCommandEvent& event);
556 void OnDeleteList(wxCommandEvent& event);
557
558 void OnValueChanged( wxDataViewEvent &event );
559
560 void OnActivated( wxDataViewEvent &event );
561 void OnExpanding( wxDataViewEvent &event );
562 void OnExpanded( wxDataViewEvent &event );
563 void OnCollapsing( wxDataViewEvent &event );
564 void OnCollapsed( wxDataViewEvent &event );
565 void OnSelectionChanged( wxDataViewEvent &event );
566
567 void OnEditingStarted( wxDataViewEvent &event );
568 void OnEditingDone( wxDataViewEvent &event );
569
570 void OnHeaderClick( wxDataViewEvent &event );
571 void OnHeaderRightClick( wxDataViewEvent &event );
572 void OnSorted( wxDataViewEvent &event );
573
574 void OnContextMenu( wxDataViewEvent &event );
575
576 void OnRightClick( wxMouseEvent &event );
577 void OnGoto( wxCommandEvent &event);
578 void OnAddMany( wxCommandEvent &event);
579
580 private:
581 wxDataViewCtrl* m_musicCtrl;
582 wxObjectDataPtr<MyMusicModel> m_music_model;
583
584 wxDataViewCtrl* m_listCtrl;
585 wxObjectDataPtr<MyListModel> m_list_model;
586
587 wxDataViewColumn * m_col;
588
589 wxTextCtrl * m_log;
590 wxLog *m_logOld;
591
592 private:
593 DECLARE_EVENT_TABLE()
594 };
595
596 // -------------------------------------
597 // MyApp
598 // -------------------------------------
599
600 IMPLEMENT_APP(MyApp)
601
602 bool MyApp::OnInit(void)
603 {
604 if ( !wxApp::OnInit() )
605 return false;
606
607 // build the first frame
608 MyFrame *frame =
609 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 540);
610 frame->Show(true);
611
612 SetTopWindow(frame);
613 return true;
614 }
615
616 int MyApp::OnExit()
617 {
618 return 0;
619 }
620
621
622 // -------------------------------------
623 // MyFrame
624 // -------------------------------------
625
626 enum
627 {
628 // file menu
629 ID_ABOUT = wxID_ABOUT,
630 ID_EXIT = wxID_EXIT,
631
632 ID_MUSIC_CTRL = 50,
633
634 ID_ADD_MOZART = 100,
635 ID_DELETE_MUSIC = 101,
636 ID_DELETE_YEAR = 102,
637
638 ID_PREPEND_LIST = 200,
639 ID_DELETE_LIST = 201,
640 ID_GOTO = 202,
641 ID_ADD_MANY = 203
642 };
643
644 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
645 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
646 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
647 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
648 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
649 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
650 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
651 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
652 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
653 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
654
655 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
656
657 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
658 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
659 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
660 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
661 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
662 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
663
664 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
665 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
666
667 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
668 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
669 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
670
671 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
672
673 EVT_RIGHT_UP(MyFrame::OnRightClick)
674 END_EVENT_TABLE()
675
676 MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
677 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
678 {
679 m_log = NULL;
680 m_col = NULL;
681
682 SetIcon(wxICON(sample));
683
684 // build the menus:
685
686 wxMenu *file_menu = new wxMenu;
687 file_menu->Append(ID_ABOUT, wxT("&About"));
688 file_menu->AppendSeparator();
689 file_menu->Append(ID_EXIT, wxT("E&xit"));
690
691 wxMenuBar *menu_bar = new wxMenuBar;
692 menu_bar->Append(file_menu, wxT("&File"));
693
694 SetMenuBar(menu_bar);
695 CreateStatusBar();
696
697 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
698
699 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
700
701 // MyMusic
702
703 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
704 wxDefaultSize, wxDV_MULTIPLE );
705
706 m_music_model = new MyMusicModel;
707 m_musicCtrl->AssociateModel( m_music_model.get() );
708
709 #if 0
710 // Call this and sorting is enabled
711 // immediatly upon start up.
712 wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( wxT("Title"), 0, wxDATAVIEW_CELL_INERT, 200,
713 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
714 col->SetSortOrder( true );
715 #else
716 m_musicCtrl->AppendTextColumn(wxT("Title"),0,wxDATAVIEW_CELL_INERT,200,DEFAULT_ALIGN,wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
717 #endif
718
719 m_musicCtrl->AppendTextColumn( wxT("Artist"), 1, wxDATAVIEW_CELL_EDITABLE, 150,
720 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
721
722 wxDataViewSpinRenderer *sr = new wxDataViewSpinRenderer( 0, 2010 );
723 wxDataViewColumn *column = new wxDataViewColumn( wxT("year"), sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
724 m_musicCtrl->AppendColumn( column );
725
726 data_sizer->Add( m_musicCtrl, 3, wxGROW );
727
728 #if 1
729
730 // MyList
731
732 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
733 wxDefaultSize, wxDV_MULTIPLE | wxDV_ROW_LINES);
734
735 m_list_model = new MyListModel;
736 m_listCtrl->AssociateModel( m_list_model.get() );
737
738 m_listCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE, 120 );
739 m_listCtrl->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT, 60 );
740
741 wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr;
742 column = new wxDataViewColumn(wxT("attributes"), ra, 2 );
743 m_listCtrl->AppendColumn( column );
744
745 data_sizer->Add( m_listCtrl, 2, wxGROW );
746
747 #endif
748
749 main_sizer->Add( data_sizer, 2, wxGROW );
750
751 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
752
753 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 );
754 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 );
755 button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 );
756 button_sizer->Add( 10, 10, 1 );
757 wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 );
758 grid_sizer->Add( new wxButton( this, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 2 );
759 grid_sizer->Add( new wxButton( this, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 2 );
760 grid_sizer->Add( new wxButton( this, ID_GOTO, _("Goto 50")), 0, wxALL, 2 );
761 grid_sizer->Add( new wxButton( this, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 2 );
762 button_sizer->Add( grid_sizer, 0, wxALL, 10 );
763
764 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
765
766 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
767
768 m_log = new wxTextCtrl( this, -1,wxString(), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
769 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
770 wxLogMessage(_("This is the log window"));
771
772 bottom_sizer->Add( m_log, 1, wxGROW );
773
774 // wxDataViewTreeStore
775
776 wxDataViewCtrl *treectrl = new wxDataViewCtrl( this, -1,
777 wxDefaultPosition, wxSize(300,200), wxDV_NO_HEADER );
778
779 wxDataViewTreeStore *store = new wxDataViewTreeStore;
780 wxDataViewItem parent = store->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm) );
781 wxDataViewItem child = store->AppendItem( parent,wxT("Child 1"), wxIcon(small1_xpm) );
782 child = store->AppendItem( parent,wxT("Child 2"), wxIcon(small1_xpm) );
783 child = store->AppendItem( parent,wxT("Child 3"), wxIcon(small1_xpm) );
784 treectrl->AssociateModel( store );
785 store->DecRef();
786
787 treectrl->AppendIconTextColumn(wxT("no label"), 0, wxDATAVIEW_CELL_INERT, 200 );
788
789 bottom_sizer->Add( treectrl );
790
791 // wxDataViewTreeCtrl
792
793 wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( this, -1, wxDefaultPosition, wxSize(300,200) );
794
795 wxImageList *ilist = new wxImageList( 16, 16 );
796 ilist->Add( wxIcon(small1_xpm) );
797 treectrl2->SetImageList( ilist );
798
799 parent = treectrl2->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
800 child = treectrl2->AppendItem( parent,wxT("Child 1"), 0 );
801 child = treectrl2->AppendItem( parent,wxT("Child 2"), 0 );
802 child = treectrl2->AppendItem( parent,wxT("Child 3"), 0 );
803
804 bottom_sizer->Add( treectrl2 );
805
806 // main sizer
807
808 main_sizer->Add( bottom_sizer, 0, wxGROW );
809
810 SetSizer( main_sizer );
811 }
812
813 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
814 {
815 Close(true);
816 }
817
818 void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
819 {
820 m_music_model->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
821 }
822
823 void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
824 {
825 wxDataViewItemArray items;
826 int len = m_musicCtrl->GetSelections( items );
827 for( int i = 0; i < len; i ++ )
828 if (items[i].IsOk())
829 m_music_model->Delete( items[i] );
830 }
831
832 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
833 {
834 m_musicCtrl->DeleteColumn( m_musicCtrl->GetColumn( 2 ) );
835 FindWindow( ID_DELETE_YEAR )->Disable();
836 }
837
838 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
839 {
840 m_list_model->Prepend(wxT("Test"));
841 }
842
843 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
844 {
845 wxDataViewItemArray items;
846 int len = m_listCtrl->GetSelections( items );
847 if (len > 0)
848 m_list_model->DeleteItems( items );
849 }
850
851 void MyFrame::OnValueChanged( wxDataViewEvent &event )
852 {
853 if (!m_log)
854 return;
855
856 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event.GetItem().GetID(), event.GetColumn() );
857 }
858
859 void MyFrame::OnActivated( wxDataViewEvent &event )
860 {
861 if(!m_log)
862 return;
863
864 wxString title = m_music_model->GetTitle( event.GetItem() );
865 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title.GetData());
866 }
867
868 void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
869 {
870 if(!m_log)
871 return;
872
873 wxString title = m_music_model->GetTitle( event.GetItem() );
874 if (title.empty())
875 title = wxT("None");
876
877 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title.GetData() );
878 }
879
880 void MyFrame::OnExpanding( wxDataViewEvent &event )
881 {
882 if (!m_log)
883 return;
884
885 wxString title = m_music_model->GetTitle( event.GetItem() );
886 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title.GetData() );
887 }
888
889
890 void MyFrame::OnEditingStarted( wxDataViewEvent &event )
891 {
892 if (!m_log)
893 return;
894
895 wxString title = m_music_model->GetTitle( event.GetItem() );
896 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title.GetData() );
897 }
898
899 void MyFrame::OnEditingDone( wxDataViewEvent &event )
900 {
901 if (!m_log)
902 return;
903
904 wxString title = m_music_model->GetTitle( event.GetItem() );
905 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title.GetData() );
906 }
907
908 void MyFrame::OnExpanded( wxDataViewEvent &event )
909 {
910 if (!m_log)
911 return;
912
913 wxString title = m_music_model->GetTitle( event.GetItem() );
914 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title.GetData() );
915 }
916
917 void MyFrame::OnCollapsing( wxDataViewEvent &event )
918 {
919 if (!m_log)
920 return;
921
922 wxString title = m_music_model->GetTitle( event.GetItem() );
923 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title.GetData() );
924 }
925
926 void MyFrame::OnCollapsed( wxDataViewEvent &event )
927 {
928 if (!m_log)
929 return;
930
931 wxString title = m_music_model->GetTitle( event.GetItem() );
932 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title.GetData());
933 }
934
935 void MyFrame::OnContextMenu( wxDataViewEvent &event )
936 {
937 if (!m_log)
938 return;
939
940 wxString title = m_music_model->GetTitle( event.GetItem() );
941 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title.GetData());
942 }
943
944 void MyFrame::OnHeaderClick( wxDataViewEvent &event )
945 {
946 if(!m_log)
947 return;
948
949 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
950
951 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos );
952 }
953
954 void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
955 {
956 if(!m_log)
957 return;
958
959 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
960
961 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos );
962 }
963
964 void MyFrame::OnSorted( wxDataViewEvent &event )
965 {
966 if(!m_log)
967 return;
968
969 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
970
971 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos );
972 }
973
974 void MyFrame::OnRightClick( wxMouseEvent &event )
975 {
976 if(!m_log)
977 return;
978
979 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event.GetX(), event.GetY());
980 }
981
982 void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
983 {
984 wxDataViewItem item = m_list_model->GetItem( 50 );
985 m_listCtrl->EnsureVisible(item,m_col);
986 }
987
988 void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
989 {
990 m_list_model->AddMany();
991 }
992
993
994 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
995 {
996 wxAboutDialogInfo info;
997 info.SetName(_("DataView sample"));
998 info.SetDescription(_("This sample demonstrates the dataview control handling"));
999 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
1000
1001 wxAboutBox(info);
1002 }
1003