]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
Removed unneeded FixUpMouse() method, some more GTK prefixing
[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/dataview.h"
24 #include "wx/datetime.h"
25 #include "wx/splitter.h"
26 #include "wx/aboutdlg.h"
27 #include "wx/choicdlg.h"
28 #include "wx/numdlg.h"
29 #include "wx/spinctrl.h"
30 #include "wx/imaglist.h"
31
32 #ifndef __WXMSW__
33 #include "../sample.xpm"
34 #endif
35
36 #include "null.xpm"
37
38 /* XPM */
39 static const char *small1_xpm[] = {
40 /* columns rows colors chars-per-pixel */
41 "16 16 6 1",
42 ". c Black",
43 "o c #FFFFFF",
44 "X c #000080",
45 "O c #FFFF00",
46 " c None",
47 "+ c #FF0000",
48 /* pixels */
49 " ",
50 " ",
51 " ",
52 " ....... ",
53 " .XXXXX. ",
54 " .oXXXX. ",
55 " .oXXX.......",
56 ".....oXXX.OOOOO.",
57 ".+++.XXXX.oOOOO.",
58 ".o++......oOOOO.",
59 ".o++++. .oOOOO.",
60 ".o++++. .OOOOO.",
61 ".+++++. .......",
62 "....... ",
63 " ",
64 " "
65 };
66
67
68 // -------------------------------------
69 // MyMusicModel
70 // -------------------------------------
71
72 /*
73 Implement this data model
74 Title Artist Year Judgement
75 --------------------------------------------------------------------------
76 1: My Music:
77 2: Pop music
78 3: You are not alone Michael Jackson 1995 good
79 4: Take a bow Madonna 1994 good
80 5: Classical music
81 6: Ninth Symphony Ludwig v. Beethoven 1824 good
82 7: German Requiem Johannes Brahms 1868 good
83 */
84
85
86
87 class MyMusicModelNode;
88 WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
89
90 class MyMusicModelNode
91 {
92 public:
93 MyMusicModelNode( MyMusicModelNode* parent,
94 const wxString &title, const wxString &artist, int year )
95 {
96 m_parent = parent;
97 m_title = title;
98 m_artist = artist;
99 m_year = year;
100 m_quality = "good";
101 m_isContainer = false;
102 }
103
104 MyMusicModelNode( MyMusicModelNode* parent,
105 const wxString &branch )
106 {
107 m_parent = parent;
108 m_title = branch;
109 m_year = -1;
110 m_isContainer = true;
111 }
112
113 ~MyMusicModelNode()
114 {
115 size_t count = m_children.GetCount();
116 size_t i;
117 for (i = 0; i < count; i++)
118 {
119 MyMusicModelNode *child = m_children[i];
120 delete child;
121 }
122 }
123
124 bool IsContainer() { return m_isContainer; }
125
126 MyMusicModelNode* GetParent() { return m_parent; }
127 MyMusicModelNodes &GetChildren() { return m_children; }
128 MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
129 void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
130 void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
131 unsigned int GetChildCount() { return m_children.GetCount(); }
132
133 public:
134 wxString m_title;
135 wxString m_artist;
136 int m_year;
137 wxString m_quality;
138
139 private:
140 MyMusicModelNode *m_parent;
141 MyMusicModelNodes m_children;
142 bool m_isContainer;
143 };
144
145
146 class MyMusicModel: public wxDataViewModel
147 {
148 public:
149
150 // constructor
151
152 MyMusicModel()
153 {
154 m_root = new MyMusicModelNode( NULL, wxT("My Music" ));
155 m_pop = new MyMusicModelNode( m_root, wxT("Pop music") );
156 m_root->Append( m_pop );
157 m_pop->Append( new MyMusicModelNode( m_pop,
158 wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
159 m_pop->Append( new MyMusicModelNode( m_pop,
160 wxT("Take a bow"), wxT("Madonna"), 1994 ) );
161 m_classical = new MyMusicModelNode( m_root, wxT("Classical music") );
162 m_root->Append( m_classical );
163 m_ninth = new MyMusicModelNode( m_classical,
164 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 );
165 m_classical->Append( m_ninth );
166 m_classical->Append( new MyMusicModelNode( m_classical,
167 wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
168 m_classicalMusicIsKnownToControl = false;
169 }
170
171 ~MyMusicModel()
172 {
173 delete m_root;
174 }
175
176 // helper method for wxLog
177
178 wxString GetTitle( const wxDataViewItem &item ) const
179 {
180 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
181 if (!node)
182 return wxEmptyString;
183
184 return node->m_title;
185 }
186
187 int GetYear( const wxDataViewItem &item ) const
188 {
189 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
190 if (!node)
191 return 2000;
192
193 return node->m_year;
194 }
195
196 // helper methods to change the model
197
198 void AddToClassical( const wxString &title, const wxString &artist, int year )
199 {
200 // add to data
201 MyMusicModelNode *child_node =
202 new MyMusicModelNode( m_classical, title, artist, year );
203
204 m_classical->Append( child_node );
205
206 if (m_classicalMusicIsKnownToControl)
207 {
208 // notify control
209 wxDataViewItem child( (void*) child_node );
210 wxDataViewItem parent( (void*) m_classical );
211 ItemAdded( parent, child );
212 }
213 }
214
215 void Delete( const wxDataViewItem &item )
216 {
217 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
218 wxDataViewItem parent( node->GetParent() );
219
220 node->GetParent()->GetChildren().Remove( node );
221 delete node;
222
223 // notify control
224 ItemDeleted( parent, item );
225 }
226
227 // override sorting to always sort branches ascendingly
228
229 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
230 unsigned int column, bool ascending )
231 {
232 if (IsContainer(item1) && IsContainer(item2))
233 {
234 wxVariant value1,value2;
235 GetValue( value1, item1, 0 );
236 GetValue( value2, item2, 0 );
237
238 wxString str1 = value1.GetString();
239 wxString str2 = value2.GetString();
240 int res = str1.Cmp( str2 );
241 if (res) return res;
242
243 // items must be different
244 wxUIntPtr litem1 = (wxUIntPtr) item1.GetID();
245 wxUIntPtr litem2 = (wxUIntPtr) item2.GetID();
246
247 return litem1-litem2;
248 }
249
250 return wxDataViewModel::Compare( item1, item2, column, ascending );
251 }
252
253 // implementation of base class virtuals to define model
254
255 virtual unsigned int GetColumnCount() const
256 {
257 return 6;
258 }
259
260 virtual wxString GetColumnType( unsigned int col ) const
261 {
262 if (col == 2)
263 return wxT("long");
264
265 return wxT("string");
266 }
267
268 virtual void GetValue( wxVariant &variant,
269 const wxDataViewItem &item, unsigned int col ) const
270 {
271 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
272 switch (col)
273 {
274 case 0: variant = node->m_title; break;
275 case 1: variant = node->m_artist; break;
276 case 2: variant = (long) node->m_year; break;
277 case 3: variant = node->m_quality; break;
278 case 4:
279 // wxMac doesn't conceal the popularity progress renderer, return 0 for containers
280 if (IsContainer(item))
281 variant = (long) 0;
282 else
283 variant = (long) 80; // all music is very 80% popular
284 break;
285 case 5:
286 // Make size of red square depend on year
287 if (GetYear(item) < 1900)
288 variant = (long) 35;
289 else
290 variant = (long) 25;
291 break;
292 default:
293 {
294 wxLogError( wxT("MyMusicModel::GetValue: wrong column %d"), col );
295
296 // provoke a crash when mouse button down
297 wxMouseState state = wxGetMouseState();
298 if (state.ShiftDown())
299 {
300 char *crash = 0;
301 *crash = 0;
302 }
303 }
304 }
305 }
306
307 virtual bool SetValue( const wxVariant &variant,
308 const wxDataViewItem &item, unsigned int col )
309 {
310 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
311 switch (col)
312 {
313 case 0: node->m_title = variant.GetString(); return true;
314 case 1: node->m_artist = variant.GetString(); return true;
315 case 2: node->m_year = variant.GetLong(); return true;
316 case 3: node->m_quality = variant.GetString(); return true;
317 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
318 }
319 return false;
320 }
321
322 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
323 {
324 // the invisble root node has no parent
325 if (!item.IsOk())
326 return wxDataViewItem(0);
327
328 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
329
330 // "MyMusic" also has no parent
331 if (node == m_root)
332 return wxDataViewItem(0);
333
334 return wxDataViewItem( (void*) node->GetParent() );
335 }
336
337 virtual bool IsContainer( const wxDataViewItem &item ) const
338 {
339 // the invisble root node can have children (in
340 // our model always "MyMusic")
341 if (!item.IsOk())
342 return true;
343
344 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
345 return node->IsContainer();
346 }
347
348 virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const
349 {
350 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
351 if (!node)
352 {
353 array.Add( wxDataViewItem( (void*) m_root ) );
354 return 1;
355 }
356
357 if (node == m_classical)
358 {
359 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
360 model->m_classicalMusicIsKnownToControl = true;
361 }
362
363 if (node->GetChildCount() == 0)
364 {
365 return 0;
366 }
367
368 unsigned int count = node->GetChildren().GetCount();
369 unsigned int pos;
370 for (pos = 0; pos < count; pos++)
371 {
372 MyMusicModelNode *child = node->GetChildren().Item( pos );
373 array.Add( wxDataViewItem( (void*) child ) );
374 }
375 return count;
376 }
377
378 wxDataViewItem GetNinthItem()
379 {
380 return wxDataViewItem( m_ninth );
381 }
382
383 private:
384 MyMusicModelNode* m_root;
385 MyMusicModelNode* m_pop;
386 MyMusicModelNode* m_classical;
387 MyMusicModelNode* m_ninth;
388 bool m_classicalMusicIsKnownToControl;
389 };
390
391
392 static int my_sort_reverse( int *v1, int *v2 )
393 {
394 return *v2-*v1;
395 }
396
397 static int my_sort( int *v1, int *v2 )
398 {
399 return *v1-*v2;
400 }
401
402 class MyListModel: public wxDataViewVirtualListModel
403 {
404 public:
405 MyListModel() :
406 #ifdef __WXMAC__
407 wxDataViewVirtualListModel( 1000 + 100 )
408 #else
409 wxDataViewVirtualListModel( 100000 + 100 )
410 #endif
411 {
412 #ifdef __WXMAC__
413 m_virtualItems = 1000;
414 #else
415 m_virtualItems = 100000;
416 #endif
417
418 unsigned int i;
419 for (i = 0; i < 100; i++)
420 {
421 wxString str;
422 str.Printf( wxT("row number %d"), i );
423 m_array.Add( str );
424 }
425
426 m_icon = wxIcon( null_xpm );
427 }
428
429 // helper methods to change the model
430
431 void Prepend( const wxString &text )
432 {
433 m_array.Insert( text, 0 );
434 RowPrepended();
435 }
436
437 void DeleteItem( const wxDataViewItem &item )
438 {
439 unsigned int row = GetRow( item );
440 if (row >= m_array.GetCount())
441 return;
442
443 m_array.RemoveAt( row );
444 RowDeleted( row );
445 }
446
447 void DeleteItems( const wxDataViewItemArray &items )
448 {
449 wxArrayInt rows;
450 unsigned int i;
451 for (i = 0; i < items.GetCount(); i++)
452 {
453 unsigned int row = GetRow( items[i] );
454 if (row < m_array.GetCount())
455 rows.Add( row );
456 }
457
458 // Sort in descending order so that the last
459 // row will be deleted first. Otherwise the
460 // remaining indeces would all be wrong.
461 rows.Sort( my_sort_reverse );
462 for (i = 0; i < rows.GetCount(); i++)
463 m_array.RemoveAt( rows[i] );
464
465 // This is just to test if wxDataViewCtrl can
466 // cope with removing rows not sorted in
467 // descending order
468 rows.Sort( my_sort );
469 RowsDeleted( rows );
470 }
471
472 void AddMany()
473 {
474 m_virtualItems += 1000;
475 Reset( m_array.GetCount() + m_virtualItems );
476 }
477
478 // implementation of base class virtuals to define model
479
480 virtual unsigned int GetColumnCount() const
481 {
482 return 3;
483 }
484
485 virtual wxString GetColumnType( unsigned int col ) const
486 {
487 if (col == 1)
488 return wxT("wxDataViewIconText");
489
490 return wxT("string");
491 }
492
493 virtual unsigned int GetRowCount()
494 {
495 return m_array.GetCount();
496 }
497
498 virtual void GetValueByRow( wxVariant &variant,
499 unsigned int row, unsigned int col ) const
500 {
501 if (col==0)
502 {
503 if (row >= m_array.GetCount())
504 {
505 wxString str;
506 str.Printf(wxT("row %d"), row - m_array.GetCount() );
507 variant = str;
508 }
509 else
510 {
511 variant = m_array[ row ];
512 }
513 } else
514 if (col==1)
515 {
516 wxDataViewIconText data( wxT("test"), m_icon );
517 variant << data;
518 } else
519 if (col==2)
520 {
521 if (row >= m_array.GetCount())
522 variant = wxT("plain");
523 else
524 variant = wxT("blue");
525 }
526 }
527
528 virtual bool GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr )
529 {
530 if (col != 2)
531 return false;
532
533 if (row < m_array.GetCount())
534 {
535 attr.SetColour( *wxBLUE );
536 attr.SetItalic( true );
537 }
538
539 return true;
540 }
541
542 virtual bool SetValueByRow( const wxVariant &variant,
543 unsigned int row, unsigned int col )
544 {
545 if (col == 0)
546 {
547 if (row >= m_array.GetCount())
548 return false;
549
550 m_array[row] = variant.GetString();
551 return true;
552 }
553
554 return false;
555 }
556
557 wxArrayString m_array;
558 wxIcon m_icon;
559 int m_virtualItems;
560 };
561
562 // -------------------------------------
563 // MyCustomRenderer
564 // -------------------------------------
565
566 class MyCustomRenderer: public wxDataViewCustomRenderer
567 {
568 public:
569 MyCustomRenderer( wxDataViewCellMode mode, int alignment ) :
570 wxDataViewCustomRenderer( wxString("long"), mode, alignment )
571 { m_height = 25; }
572
573 virtual bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) )
574 {
575 dc->SetBrush( *wxRED_BRUSH );
576 dc->SetPen( *wxTRANSPARENT_PEN );
577 dc->DrawRectangle( rect );
578 return true;
579 }
580
581
582 virtual bool Activate( wxRect WXUNUSED(cell),
583 wxDataViewModel *WXUNUSED(model), const wxDataViewItem &WXUNUSED(item), unsigned int WXUNUSED(col) )
584 {
585 wxLogMessage( wxT("MyCustomRenderer Activate()") );
586 return false;
587 }
588
589 virtual bool LeftClick( wxPoint cursor, wxRect WXUNUSED(cell),
590 wxDataViewModel *WXUNUSED(model), const wxDataViewItem &WXUNUSED(item), unsigned int WXUNUSED(col) )
591 {
592 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor.x, cursor.y );
593 return false;
594 }
595
596 virtual wxSize GetSize() const
597 {
598 //return wxSize(60,m_height);
599 return wxSize(60,20);
600 }
601
602 virtual bool SetValue( const wxVariant &value )
603 {
604 m_height = value;
605 return true;
606 }
607
608 virtual bool GetValue( wxVariant &WXUNUSED(value) ) const { return true; }
609
610 private:
611 long m_height;
612 };
613
614 // -------------------------------------
615 // MyApp
616 // -------------------------------------
617
618 class MyApp: public wxApp
619 {
620 public:
621 bool OnInit(void);
622 int OnExit();
623 };
624
625 // -------------------------------------
626 // MyFrame
627 // -------------------------------------
628
629 class MyFrame : public wxFrame
630 {
631 public:
632 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
633 ~MyFrame();
634
635 public:
636 void OnQuit(wxCommandEvent& event);
637 void OnAbout(wxCommandEvent& event);
638
639 void OnAddMozart(wxCommandEvent& event);
640 void OnDeleteMusic(wxCommandEvent& event);
641 void OnDeleteYear(wxCommandEvent& event);
642 void OnSelectNinth(wxCommandEvent& event);
643
644 void OnPrependList(wxCommandEvent& event);
645 void OnDeleteList(wxCommandEvent& event);
646
647 void OnValueChanged( wxDataViewEvent &event );
648
649 void OnActivated( wxDataViewEvent &event );
650 void OnExpanding( wxDataViewEvent &event );
651 void OnExpanded( wxDataViewEvent &event );
652 void OnCollapsing( wxDataViewEvent &event );
653 void OnCollapsed( wxDataViewEvent &event );
654 void OnSelectionChanged( wxDataViewEvent &event );
655
656 void OnEditingStarted( wxDataViewEvent &event );
657 void OnEditingDone( wxDataViewEvent &event );
658
659 void OnHeaderClick( wxDataViewEvent &event );
660 void OnHeaderRightClick( wxDataViewEvent &event );
661 void OnSorted( wxDataViewEvent &event );
662
663 void OnContextMenu( wxDataViewEvent &event );
664
665 void OnRightClick( wxMouseEvent &event );
666 void OnGoto( wxCommandEvent &event);
667 void OnAddMany( wxCommandEvent &event);
668
669 void OnBeginDrag( wxDataViewEvent &event );
670 void OnDropPossible( wxDataViewEvent &event );
671 void OnDrop( wxDataViewEvent &event );
672
673 private:
674 wxDataViewCtrl* m_musicCtrl;
675 wxObjectDataPtr<MyMusicModel> m_music_model;
676
677 wxDataViewCtrl* m_listCtrl;
678 wxObjectDataPtr<MyListModel> m_list_model;
679
680 wxDataViewColumn * m_col;
681
682 wxTextCtrl * m_log;
683 wxLog *m_logOld;
684
685 private:
686 DECLARE_EVENT_TABLE()
687 };
688
689 // -------------------------------------
690 // MyApp
691 // -------------------------------------
692
693 IMPLEMENT_APP(MyApp)
694
695 bool MyApp::OnInit(void)
696 {
697 if ( !wxApp::OnInit() )
698 return false;
699
700 // build the first frame
701 MyFrame *frame =
702 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 1000, 540);
703 frame->Show(true);
704
705 SetTopWindow(frame);
706 return true;
707 }
708
709 int MyApp::OnExit()
710 {
711 return 0;
712 }
713
714
715 // -------------------------------------
716 // MyFrame
717 // -------------------------------------
718
719 enum
720 {
721 // file menu
722 ID_ABOUT = wxID_ABOUT,
723 ID_EXIT = wxID_EXIT,
724
725 ID_MUSIC_CTRL = 50,
726
727 ID_ADD_MOZART = 100,
728 ID_DELETE_MUSIC = 101,
729 ID_DELETE_YEAR = 102,
730 ID_SELECT_NINTH = 103,
731
732 ID_PREPEND_LIST = 200,
733 ID_DELETE_LIST = 201,
734 ID_GOTO = 202,
735 ID_ADD_MANY = 203
736 };
737
738 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
739 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
740 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
741 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
742 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
743 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
744 EVT_BUTTON( ID_SELECT_NINTH, MyFrame::OnSelectNinth )
745 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
746 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
747 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
748 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
749
750 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
751
752 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
753 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
754 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
755 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
756 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
757 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
758
759 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
760 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
761
762 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
763 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
764 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
765
766 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
767
768 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
769 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL, MyFrame::OnDropPossible )
770 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL, MyFrame::OnDrop )
771
772 EVT_RIGHT_UP(MyFrame::OnRightClick)
773 END_EVENT_TABLE()
774
775 MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
776 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
777 {
778 m_log = NULL;
779 m_col = NULL;
780
781 SetIcon(wxICON(sample));
782
783 // build the menus:
784
785 wxMenu *file_menu = new wxMenu;
786 file_menu->Append(ID_ABOUT, wxT("&About"));
787 file_menu->AppendSeparator();
788 file_menu->Append(ID_EXIT, wxT("E&xit"));
789
790 wxMenuBar *menu_bar = new wxMenuBar;
791 menu_bar->Append(file_menu, wxT("&File"));
792
793 SetMenuBar(menu_bar);
794 CreateStatusBar();
795
796 wxPanel *panel = new wxPanel( this, -1 );
797
798 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
799
800 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
801
802 // MyMusic
803
804 m_musicCtrl = new wxDataViewCtrl( panel, ID_MUSIC_CTRL, wxDefaultPosition,
805 wxSize(400,200), wxDV_MULTIPLE|wxDV_VARIABLE_LINE_HEIGHT );
806
807 m_music_model = new MyMusicModel;
808 m_musicCtrl->AssociateModel( m_music_model.get() );
809
810 m_musicCtrl->EnableDragSource( wxDF_UNICODETEXT );
811 m_musicCtrl->EnableDropTarget( wxDF_UNICODETEXT );
812
813 wxDataViewTextRenderer *tr = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT );
814 wxDataViewColumn *column0 = new wxDataViewColumn( wxT("title"), tr, 0, 200, wxALIGN_LEFT,
815 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE );
816 m_musicCtrl->AppendColumn( column0 );
817 #if 0
818 // Call this and sorting is enabled
819 // immediatly upon start up.
820 column0->SetAsSortKey();
821 #endif
822
823 tr = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
824 wxDataViewColumn *column1 = new wxDataViewColumn( wxT("artist"), tr, 1, 150, wxALIGN_LEFT,
825 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
826 column1->SetMinWidth(150); // this column can't be resized to be smaller
827 m_musicCtrl->AppendColumn( column1 );
828
829 wxDataViewSpinRenderer *sr = new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
830 wxDataViewColumn *column2 = new wxDataViewColumn( wxT("year"), sr, 2, 60, wxALIGN_LEFT,
831 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE );
832 m_musicCtrl->AppendColumn( column2 );
833
834 wxArrayString choices;
835 choices.Add( "good" );
836 choices.Add( "bad" );
837 choices.Add( "lousy" );
838 wxDataViewChoiceRenderer *c = new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
839 wxDataViewColumn *column3 = new wxDataViewColumn( wxT("rating"), c, 3, 100, wxALIGN_LEFT,
840 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
841 m_musicCtrl->AppendColumn( column3 );
842
843 m_musicCtrl->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT, 80 );
844
845 MyCustomRenderer *cr = new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE, wxALIGN_RIGHT );
846 wxDataViewColumn *column4 = new wxDataViewColumn( wxT("custom"), cr, 5, -1, wxALIGN_LEFT,
847 wxDATAVIEW_COL_RESIZABLE );
848 m_musicCtrl->AppendColumn( column4 );
849
850 data_sizer->Add( m_musicCtrl, 3, wxGROW );
851
852 // MyList
853
854 m_listCtrl = new wxDataViewCtrl( panel, wxID_ANY, wxDefaultPosition,
855 wxSize(400,200), wxDV_MULTIPLE | wxDV_ROW_LINES);
856
857 m_list_model = new MyListModel;
858 m_listCtrl->AssociateModel( m_list_model.get() );
859
860 #if 1
861 m_listCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE, 120 );
862 m_listCtrl->AppendIconTextColumn(wxIcon(small1_xpm), 1, wxDATAVIEW_CELL_INERT )->SetTitle( wxT("icon") );
863 #else
864 m_listCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE );
865 m_listCtrl->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT );
866 #endif
867
868 wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr;
869 wxDataViewColumn *column5 = new wxDataViewColumn(wxT("attributes"), ra, 2 );
870 m_listCtrl->AppendColumn( column5 );
871
872 data_sizer->Add( m_listCtrl, 2, wxGROW );
873
874 main_sizer->Add( data_sizer, 2, wxGROW );
875
876 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
877
878 button_sizer->Add( new wxButton( panel, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 );
879 button_sizer->Add( new wxButton( panel, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 );
880 button_sizer->Add( new wxButton( panel, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 );
881 button_sizer->Add( new wxButton( panel, ID_SELECT_NINTH, _("Select Ninth")), 0, wxALL, 10 );
882 button_sizer->Add( 10, 10, 1 );
883 wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 );
884 grid_sizer->Add( new wxButton( panel, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 2 );
885 grid_sizer->Add( new wxButton( panel, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 2 );
886 grid_sizer->Add( new wxButton( panel, ID_GOTO, _("Goto 50")), 0, wxALL, 2 );
887 grid_sizer->Add( new wxButton( panel, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 2 );
888 button_sizer->Add( grid_sizer, 0, wxALL, 10 );
889
890 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
891
892 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
893
894 m_log = new wxTextCtrl( panel, -1, wxString(), wxDefaultPosition, wxSize(100,200), wxTE_MULTILINE );
895 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
896 wxLogMessage(_("This is the log window"));
897
898 bottom_sizer->Add( m_log, 1, wxGROW );
899
900 // wxDataViewListCtrl
901
902 wxDataViewListCtrl *listctrl = new wxDataViewListCtrl( panel, -1,
903 wxDefaultPosition, wxSize(100,200) );
904
905 listctrl->AppendToggleColumn( wxT("Toggle") );
906 listctrl->AppendTextColumn( wxT("Text") );
907
908 wxVector<wxVariant> data;
909 data.push_back( true );
910 data.push_back( "row 1" );
911 listctrl->AppendItem( data );
912
913 data.clear();
914 data.push_back( false );
915 data.push_back( "row 3" );
916 listctrl->AppendItem( data );
917
918 bottom_sizer->Add( listctrl, 1 );
919
920
921 // wxDataViewTreeCtrl
922
923 wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( panel, -1, wxDefaultPosition, wxSize(100,200) );
924
925 wxImageList *ilist = new wxImageList( 16, 16 );
926 ilist->Add( wxIcon(small1_xpm) );
927 treectrl2->SetImageList( ilist );
928
929 wxDataViewItem parent2 = treectrl2->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
930 treectrl2->AppendItem( parent2, wxT("Child 1"), 0 );
931 treectrl2->AppendItem( parent2, wxT("Child 2"), 0 );
932 treectrl2->AppendItem( parent2, wxT("Child 3, very long, long, long, long"), 0 );
933
934 bottom_sizer->Add( treectrl2, 1 );
935
936 // main sizer
937
938 main_sizer->Add( bottom_sizer, 0, wxGROW );
939
940 panel->SetSizer( main_sizer );
941 }
942
943 MyFrame::~MyFrame()
944 {
945 delete wxLog::SetActiveTarget(m_logOld);
946 }
947
948 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
949 {
950 Close(true);
951 }
952
953 void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
954 {
955 m_music_model->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
956 }
957
958 void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
959 {
960 wxDataViewItemArray items;
961 int len = m_musicCtrl->GetSelections( items );
962 for( int i = 0; i < len; i ++ )
963 if (items[i].IsOk())
964 m_music_model->Delete( items[i] );
965 }
966
967 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
968 {
969 m_musicCtrl->DeleteColumn( m_musicCtrl->GetColumn( 2 ) );
970 FindWindow( ID_DELETE_YEAR )->Disable();
971 }
972
973 void MyFrame::OnSelectNinth( wxCommandEvent& WXUNUSED(event) )
974 {
975 m_musicCtrl->Select( m_music_model->GetNinthItem() );
976 }
977
978 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
979 {
980 m_list_model->Prepend(wxT("Test"));
981 }
982
983 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
984 {
985 wxDataViewItemArray items;
986 int len = m_listCtrl->GetSelections( items );
987 if (len > 0)
988 m_list_model->DeleteItems( items );
989 }
990
991 void MyFrame::OnValueChanged( wxDataViewEvent &event )
992 {
993 if (!m_log)
994 return;
995
996 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event.GetItem().GetID(), event.GetColumn() );
997 }
998
999 void MyFrame::OnActivated( wxDataViewEvent &event )
1000 {
1001 if(!m_log)
1002 return;
1003
1004 wxString title = m_music_model->GetTitle( event.GetItem() );
1005 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title );
1006
1007 if (m_musicCtrl->IsExpanded( event.GetItem() ))
1008 wxLogMessage(wxT("Item: %s is expanded"), title );
1009 }
1010
1011 void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
1012 {
1013 if(!m_log)
1014 return;
1015
1016 wxString title = m_music_model->GetTitle( event.GetItem() );
1017 if (title.empty())
1018 title = wxT("None");
1019
1020 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title );
1021 }
1022
1023 void MyFrame::OnExpanding( wxDataViewEvent &event )
1024 {
1025 if (!m_log)
1026 return;
1027
1028 wxString title = m_music_model->GetTitle( event.GetItem() );
1029 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title );
1030 }
1031
1032
1033 void MyFrame::OnEditingStarted( wxDataViewEvent &event )
1034 {
1035 if (!m_log)
1036 return;
1037
1038 wxString title = m_music_model->GetTitle( event.GetItem() );
1039 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title );
1040 }
1041
1042 void MyFrame::OnEditingDone( wxDataViewEvent &event )
1043 {
1044 if (!m_log)
1045 return;
1046
1047 wxString title = m_music_model->GetTitle( event.GetItem() );
1048 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title );
1049 }
1050
1051 void MyFrame::OnExpanded( wxDataViewEvent &event )
1052 {
1053 if (!m_log)
1054 return;
1055
1056 wxString title = m_music_model->GetTitle( event.GetItem() );
1057 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title );
1058 }
1059
1060 void MyFrame::OnCollapsing( wxDataViewEvent &event )
1061 {
1062 if (!m_log)
1063 return;
1064
1065 wxString title = m_music_model->GetTitle( event.GetItem() );
1066 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title );
1067 }
1068
1069 void MyFrame::OnCollapsed( wxDataViewEvent &event )
1070 {
1071 if (!m_log)
1072 return;
1073
1074 wxString title = m_music_model->GetTitle( event.GetItem() );
1075 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title);
1076 }
1077
1078 void MyFrame::OnContextMenu( wxDataViewEvent &event )
1079 {
1080 if (!m_log)
1081 return;
1082
1083 wxString title = m_music_model->GetTitle( event.GetItem() );
1084 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title );
1085
1086 wxMenu menu;
1087 menu.Append( 1, wxT("entry 1") );
1088 menu.Append( 2, wxT("entry 2") );
1089 menu.Append( 3, wxT("entry 3") );
1090
1091 m_musicCtrl->PopupMenu(&menu);
1092
1093 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),title.GetData(), event.GetValue().GetString());
1094 }
1095
1096 void MyFrame::OnHeaderClick( wxDataViewEvent &event )
1097 {
1098 // we need to skip the event to let the default behaviour of sorting by
1099 // this column when it is clicked to take place
1100 event.Skip();
1101
1102 if(!m_log)
1103 return;
1104
1105 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
1106
1107 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos );
1108 }
1109
1110 void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
1111 {
1112 if(!m_log)
1113 return;
1114
1115 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
1116
1117 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos );
1118 }
1119
1120 void MyFrame::OnSorted( wxDataViewEvent &event )
1121 {
1122 if(!m_log)
1123 return;
1124
1125 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
1126
1127 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos );
1128 }
1129
1130 void MyFrame::OnRightClick( wxMouseEvent &event )
1131 {
1132 if(!m_log)
1133 return;
1134
1135 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event.GetX(), event.GetY());
1136 }
1137
1138 void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
1139 {
1140 wxDataViewItem item = m_list_model->GetItem( 50 );
1141 m_listCtrl->EnsureVisible(item,m_col);
1142 }
1143
1144 void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
1145 {
1146 m_list_model->AddMany();
1147 }
1148
1149
1150 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1151 {
1152 wxAboutDialogInfo info;
1153 info.SetName(_("DataView sample"));
1154 info.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
1155 info.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
1156
1157 wxAboutBox(info);
1158 }
1159
1160 void MyFrame::OnBeginDrag( wxDataViewEvent &event )
1161 {
1162 wxDataViewItem item( event.GetItem() );
1163
1164 // only allow drags for item, not containers
1165 if (m_music_model->IsContainer( item ) )
1166 {
1167 event.Veto();
1168 return;
1169 }
1170
1171 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1172 wxTextDataObject *obj = new wxTextDataObject;
1173 obj->SetText( node->m_title );
1174 event.SetDataObject( obj );
1175 }
1176
1177 void MyFrame::OnDropPossible( wxDataViewEvent &event )
1178 {
1179 wxDataViewItem item( event.GetItem() );
1180
1181 // only allow drags for item, not containers
1182 if (m_music_model->IsContainer( item ) )
1183 event.Veto();
1184
1185 if (event.GetDataFormat() != wxDF_UNICODETEXT)
1186 event.Veto();
1187 }
1188
1189 void MyFrame::OnDrop( wxDataViewEvent &event )
1190 {
1191 wxDataViewItem item( event.GetItem() );
1192
1193 // only allow drops for item, not containers
1194 if (m_music_model->IsContainer( item ) )
1195 {
1196 event.Veto();
1197 return;
1198 }
1199
1200 if (event.GetDataFormat() != wxDF_UNICODETEXT)
1201 {
1202 event.Veto();
1203 return;
1204 }
1205
1206 wxTextDataObject obj;
1207 obj.SetData( wxDF_TEXT, event.GetDataSize(), event.GetDataBuffer() );
1208
1209 wxLogMessage(wxT("Text dropped: %s"), obj.GetText() );
1210 }
1211