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