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