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