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