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