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