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