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