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