all code revisited.
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/dataview.h"
28 #include "wx/datetime.h"
29 #include "wx/splitter.h"
30 #include "wx/aboutdlg.h"
31 #include "wx/choicdlg.h"
32 #include "wx/numdlg.h"
33 #include "wx/spinctrl.h"
34 #include "wx/imaglist.h"
35 #include "wx/notebook.h"
36
37 #include "mymodels.h"
38
39 // ----------------------------------------------------------------------------
40 // resources
41 // ----------------------------------------------------------------------------
42
43 #ifndef __WXMSW__
44 #include "../sample.xpm"
45 #endif
46
47 #include "wx_small.xpm"
48
49 // ----------------------------------------------------------------------------
50 // MyApp
51 // ----------------------------------------------------------------------------
52
53 class MyApp: public wxApp
54 {
55 public:
56 virtual bool OnInit();
57 };
58
59 // ----------------------------------------------------------------------------
60 // MyFrame
61 // ----------------------------------------------------------------------------
62
63 class MyFrame : public wxFrame
64 {
65 public:
66 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
67 ~MyFrame();
68
69 void BuildDataViewCtrl(wxPanel* parent,
70 unsigned int nPanel,
71 unsigned long style = 0);
72
73 public: // event handlers
74
75 void OnStyleChange(wxCommandEvent& event);
76 void OnQuit(wxCommandEvent& event);
77 void OnAbout(wxCommandEvent& event);
78
79 void OnPageChanged(wxBookCtrlEvent& event);
80
81 void OnAddMozart(wxCommandEvent& event);
82 void OnDeleteMusic(wxCommandEvent& event);
83 void OnDeleteYear(wxCommandEvent& event);
84 void OnSelectNinth(wxCommandEvent& event);
85
86 void OnPrependList(wxCommandEvent& event);
87 void OnDeleteList(wxCommandEvent& event);
88
89 void OnValueChanged( wxDataViewEvent &event );
90
91 void OnActivated( wxDataViewEvent &event );
92 void OnExpanding( wxDataViewEvent &event );
93 void OnExpanded( wxDataViewEvent &event );
94 void OnCollapsing( wxDataViewEvent &event );
95 void OnCollapsed( wxDataViewEvent &event );
96 void OnSelectionChanged( wxDataViewEvent &event );
97
98 void OnEditingStarted( wxDataViewEvent &event );
99 void OnEditingDone( wxDataViewEvent &event );
100
101 void OnHeaderClick( wxDataViewEvent &event );
102 void OnHeaderRightClick( wxDataViewEvent &event );
103 void OnSorted( wxDataViewEvent &event );
104
105 void OnContextMenu( wxDataViewEvent &event );
106
107 void OnRightClick( wxMouseEvent &event );
108 void OnGoto( wxCommandEvent &event);
109 void OnAddMany( wxCommandEvent &event);
110
111 void OnBeginDrag( wxDataViewEvent &event );
112 void OnDropPossible( wxDataViewEvent &event );
113 void OnDrop( wxDataViewEvent &event );
114
115 private:
116 wxNotebook* m_notebook;
117
118 // the controls stored in the various tabs of the main notebook:
119
120 wxDataViewCtrl* m_ctrl[4];
121
122 // the models associated with the first two DVC:
123
124 wxObjectDataPtr<MyMusicTreeModel> m_music_model;
125 wxObjectDataPtr<MyListModel> m_list_model;
126
127 // other data:
128
129 wxDataViewColumn* m_col;
130
131 wxTextCtrl* m_log;
132 wxLog *m_logOld;
133
134 private:
135 DECLARE_EVENT_TABLE()
136 };
137
138
139 // ----------------------------------------------------------------------------
140 // MyCustomRenderer
141 // ----------------------------------------------------------------------------
142
143 class MyCustomRenderer: public wxDataViewCustomRenderer
144 {
145 public:
146 MyCustomRenderer( wxDataViewCellMode mode, int alignment ) :
147 wxDataViewCustomRenderer( wxString("long"), mode, alignment )
148 { m_height = 25; }
149
150 virtual bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) )
151 {
152 dc->SetBrush( *wxRED_BRUSH );
153 dc->SetPen( *wxTRANSPARENT_PEN );
154 dc->DrawRectangle( rect.Deflate(2) );
155 return true;
156 }
157
158 virtual bool Activate( wxRect WXUNUSED(cell),
159 wxDataViewModel *WXUNUSED(model),
160 const wxDataViewItem &WXUNUSED(item),
161 unsigned int WXUNUSED(col) )
162 {
163 wxLogMessage( "MyCustomRenderer Activate()" );
164 return false;
165 }
166
167 virtual bool LeftClick( wxPoint cursor, wxRect WXUNUSED(cell),
168 wxDataViewModel *WXUNUSED(model),
169 const wxDataViewItem &WXUNUSED(item),
170 unsigned int WXUNUSED(col) )
171 {
172 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor.x, cursor.y );
173 return false;
174 }
175
176 virtual wxSize GetSize() const
177 {
178 //return wxSize(60,m_height);
179 return wxSize(60,20);
180 }
181
182 virtual bool SetValue( const wxVariant &value )
183 {
184 m_height = value;
185 return true;
186 }
187
188 virtual bool GetValue( wxVariant &WXUNUSED(value) ) const { return true; }
189
190 private:
191 long m_height;
192 };
193
194
195 // ============================================================================
196 // implementation
197 // ============================================================================
198
199 // ----------------------------------------------------------------------------
200 // MyApp
201 // ----------------------------------------------------------------------------
202
203 IMPLEMENT_APP(MyApp)
204
205 bool MyApp::OnInit()
206 {
207 if ( !wxApp::OnInit() )
208 return false;
209
210 MyFrame *frame =
211 new MyFrame(NULL, wxT("wxDataViewCtrl sample"), 40, 40, 1000, 540);
212 SetTopWindow(frame);
213
214 frame->Show(true);
215 return true;
216 }
217
218
219 // ----------------------------------------------------------------------------
220 // MyFrame
221 // ----------------------------------------------------------------------------
222
223 enum
224 {
225 ID_STYLE_MENU = wxID_HIGHEST+1,
226
227 // file menu
228 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
229 ID_MULTIPLE,
230 ID_ROW_LINES,
231 ID_HORIZ_RULES,
232 ID_VERT_RULES,
233
234 ID_EXIT = wxID_EXIT,
235
236 // about menu
237 ID_ABOUT = wxID_ABOUT,
238
239
240 // control IDs
241
242 ID_MUSIC_CTRL = 50,
243
244 ID_ADD_MOZART = 100,
245 ID_DELETE_MUSIC = 101,
246 ID_DELETE_YEAR = 102,
247 ID_SELECT_NINTH = 103,
248
249 ID_PREPEND_LIST = 200,
250 ID_DELETE_LIST = 201,
251 ID_GOTO = 202,
252 ID_ADD_MANY = 203
253 };
254
255 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
256 EVT_MENU_RANGE( ID_MULTIPLE, ID_VERT_RULES, MyFrame::OnStyleChange )
257 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
258 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
259
260 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY, MyFrame::OnPageChanged )
261
262 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
263 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
264 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
265 EVT_BUTTON( ID_SELECT_NINTH, MyFrame::OnSelectNinth )
266 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
267 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
268 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
269 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
270
271 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
272
273 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
274 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
275 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
276 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
277 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
278 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
279
280 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
281 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
282
283 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
284 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
285 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
286
287 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
288
289 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
290 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL, MyFrame::OnDropPossible )
291 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL, MyFrame::OnDrop )
292
293 EVT_RIGHT_UP(MyFrame::OnRightClick)
294 END_EVENT_TABLE()
295
296 MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
297 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
298 {
299 m_log = NULL;
300 m_col = NULL;
301
302 m_ctrl[0] = NULL;
303 m_ctrl[1] = NULL;
304 m_ctrl[2] = NULL;
305 m_ctrl[3] = NULL;
306
307 SetIcon(wxICON(sample));
308
309
310 // build the menus
311 // ----------------
312
313 wxMenu *style_menu = new wxMenu;
314 //style_menu->AppendCheckItem(ID_SINGLE, wxT("Single selection"));
315 style_menu->AppendCheckItem(ID_MULTIPLE, wxT("Multiple selection"));
316 style_menu->AppendCheckItem(ID_ROW_LINES, wxT("Alternating colours"));
317 style_menu->AppendCheckItem(ID_HORIZ_RULES, wxT("Display horizontal rules"));
318 style_menu->AppendCheckItem(ID_VERT_RULES, wxT("Display vertical rules"));
319
320 wxMenu *file_menu = new wxMenu;
321 file_menu->Append(ID_STYLE_MENU, wxT("&Style"), style_menu);
322 file_menu->AppendSeparator();
323 file_menu->Append(ID_EXIT, wxT("E&xit"));
324
325 wxMenu *about_menu = new wxMenu;
326 about_menu->Append(ID_ABOUT, wxT("&About"));
327
328 wxMenuBar *menu_bar = new wxMenuBar;
329 menu_bar->Append(file_menu, wxT("&File"));
330 menu_bar->Append(about_menu, wxT("&About"));
331
332 SetMenuBar(menu_bar);
333 CreateStatusBar();
334
335
336 // first page of the notebook
337 // --------------------------
338
339 m_notebook = new wxNotebook( this, wxID_ANY );
340
341 wxPanel *firstPanel = new wxPanel( m_notebook, wxID_ANY );
342
343 BuildDataViewCtrl(firstPanel, 0); // sets m_ctrl[0]
344
345 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
346 button_sizer->Add( new wxButton( firstPanel, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 );
347 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 );
348 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 );
349 button_sizer->Add( new wxButton( firstPanel, ID_SELECT_NINTH,_("Select Ninth")), 0, wxALL, 10 );
350
351 wxSizer *firstPanelSz = new wxBoxSizer( wxVERTICAL );
352 m_ctrl[0]->SetMinSize(wxSize(-1, 200));
353 firstPanelSz->Add(m_ctrl[0], 1, wxGROW|wxALL, 5);
354 firstPanelSz->Add(
355 new wxStaticText(firstPanel, wxID_ANY, wxT("Most of the cells above are editable!")),
356 0, wxGROW|wxALL, 5);
357 firstPanelSz->Add(button_sizer);
358 firstPanel->SetSizerAndFit(firstPanelSz);
359
360
361 // second page of the notebook
362 // ---------------------------
363
364 wxPanel *secondPanel = new wxPanel( m_notebook, wxID_ANY );
365
366 BuildDataViewCtrl(secondPanel, 1); // sets m_ctrl[1]
367
368 wxBoxSizer *button_sizer2 = new wxBoxSizer( wxHORIZONTAL );
369 button_sizer2->Add( new wxButton( secondPanel, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 10 );
370 button_sizer2->Add( new wxButton( secondPanel, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 10 );
371 button_sizer2->Add( new wxButton( secondPanel, ID_GOTO, _("Goto 50")), 0, wxALL, 10 );
372 button_sizer2->Add( new wxButton( secondPanel, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 10 );
373
374 wxSizer *secondPanelSz = new wxBoxSizer( wxVERTICAL );
375 secondPanelSz->Add(m_ctrl[1], 1, wxGROW|wxALL, 5);
376 secondPanelSz->Add(button_sizer2);
377 secondPanel->SetSizerAndFit(secondPanelSz);
378
379
380 // third page of the notebook
381 // ---------------------------
382
383 wxPanel *thirdPanel = new wxPanel( m_notebook, wxID_ANY );
384
385 BuildDataViewCtrl(thirdPanel, 2); // sets m_ctrl[2]
386
387 wxSizer *thirdPanelSz = new wxBoxSizer( wxVERTICAL );
388 thirdPanelSz->Add(m_ctrl[2], 1, wxGROW|wxALL, 5);
389 thirdPanel->SetSizerAndFit(thirdPanelSz);
390
391
392 // fourth page of the notebook
393 // ---------------------------
394
395 wxPanel *fourthPanel = new wxPanel( m_notebook, wxID_ANY );
396
397 BuildDataViewCtrl(fourthPanel, 3); // sets m_ctrl[3]
398
399 wxSizer *fourthPanelSz = new wxBoxSizer( wxVERTICAL );
400 fourthPanelSz->Add(m_ctrl[3], 1, wxGROW|wxALL, 5);
401 fourthPanel->SetSizerAndFit(fourthPanelSz);
402
403
404
405 // complete GUI
406 // ------------
407
408 m_notebook->AddPage(firstPanel, "MyMusicTreeModel");
409 m_notebook->AddPage(secondPanel, "MyListModel");
410 m_notebook->AddPage(thirdPanel, "wxDataViewListCtrl");
411 m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl");
412
413 wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
414
415 m_log = new wxTextCtrl( this, wxID_ANY, wxString(), wxDefaultPosition,
416 wxDefaultSize, wxTE_MULTILINE );
417 m_log->SetMinSize(wxSize(-1, 100));
418 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
419 wxLogMessage(_("This is the log window"));
420
421 mainSizer->Add( m_notebook, 1, wxGROW );
422 mainSizer->Add( m_log, 0, wxGROW );
423
424 SetSizerAndFit(mainSizer);
425 }
426
427 MyFrame::~MyFrame()
428 {
429 delete wxLog::SetActiveTarget(m_logOld);
430 }
431
432 void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned long style)
433 {
434 switch (nPanel)
435 {
436 case 0:
437 {
438 wxASSERT(!m_ctrl[0] && !m_music_model);
439 m_ctrl[0] =
440 new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
441 wxDefaultSize, style );
442
443 m_music_model = new MyMusicTreeModel;
444 m_ctrl[0]->AssociateModel( m_music_model.get() );
445
446 m_ctrl[0]->EnableDragSource( wxDF_UNICODETEXT );
447 m_ctrl[0]->EnableDropTarget( wxDF_UNICODETEXT );
448
449 // column 0 of the view control:
450
451 wxDataViewTextRenderer *tr =
452 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT );
453 wxDataViewColumn *column0 =
454 new wxDataViewColumn( wxT("title"), tr, 0, 200, wxALIGN_LEFT,
455 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE );
456 m_ctrl[0]->AppendColumn( column0 );
457 #if 0
458 // Call this and sorting is enabled
459 // immediatly upon start up.
460 column0->SetAsSortKey();
461 #endif
462
463 // column 1 of the view control:
464
465 tr = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
466 wxDataViewColumn *column1 =
467 new wxDataViewColumn( wxT("artist"), tr, 1, 150, wxALIGN_LEFT,
468 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE |
469 wxDATAVIEW_COL_RESIZABLE );
470 column1->SetMinWidth(150); // this column can't be resized to be smaller
471 m_ctrl[0]->AppendColumn( column1 );
472
473 // column 2 of the view control:
474
475 wxDataViewSpinRenderer *sr =
476 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
477 wxDataViewColumn *column2 =
478 new wxDataViewColumn( wxT("year"), sr, 2, 60, wxALIGN_LEFT,
479 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE );
480 m_ctrl[0]->AppendColumn( column2 );
481
482 // column 3 of the view control:
483
484 wxArrayString choices;
485 choices.Add( "good" );
486 choices.Add( "bad" );
487 choices.Add( "lousy" );
488 wxDataViewChoiceRenderer *c =
489 new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
490 wxDataViewColumn *column3 =
491 new wxDataViewColumn( wxT("rating"), c, 3, 100, wxALIGN_LEFT,
492 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
493 m_ctrl[0]->AppendColumn( column3 );
494
495 // column 4 of the view control:
496
497 m_ctrl[0]->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT, 80 );
498
499 // column 5 of the view control:
500
501 MyCustomRenderer *cr = new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE, wxALIGN_RIGHT );
502 wxDataViewColumn *column5 =
503 new wxDataViewColumn( wxT("custom"), cr, 5, -1, wxALIGN_LEFT,
504 wxDATAVIEW_COL_RESIZABLE );
505 m_ctrl[0]->AppendColumn( column5 );
506
507
508 // select initially the ninth symphony:
509 m_ctrl[0]->Select(m_music_model->GetNinthItem());
510 }
511 break;
512
513 case 1:
514 {
515 wxASSERT(!m_ctrl[1] && !m_list_model);
516 m_ctrl[1] = new wxDataViewCtrl( parent, wxID_ANY, wxDefaultPosition,
517 wxDefaultSize, style );
518
519 m_list_model = new MyListModel;
520 m_ctrl[1]->AssociateModel( m_list_model.get() );
521
522 // the various columns
523 #if 1
524 m_ctrl[1]->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE, 120 );
525 m_ctrl[1]->AppendIconTextColumn(wxIcon(wx_small_xpm), 1, wxDATAVIEW_CELL_INERT )->SetTitle( wxT("icon") );
526 #else
527 m_ctrl[1]->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE );
528 m_ctrl[1]->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT );
529 #endif
530 m_ctrl[1]->AppendColumn(
531 new wxDataViewColumn(wxT("attributes"), new wxDataViewTextRendererAttr, 2 ));
532 }
533 break;
534
535 case 2:
536 {
537 wxASSERT(!m_ctrl[2]);
538 wxDataViewListCtrl* lc =
539 new wxDataViewListCtrl( parent, wxID_ANY, wxDefaultPosition,
540 wxDefaultSize, style );
541 m_ctrl[2] = lc;
542
543 lc->AppendToggleColumn( wxT("Toggle") );
544 lc->AppendTextColumn( wxT("Text") );
545 lc->AppendProgressColumn( wxT("Progress") );
546
547 wxVector<wxVariant> data;
548 for (unsigned int i=0; i<10; i++)
549 {
550 data.clear();
551 data.push_back( (i%3) == 0 );
552 data.push_back( wxString::Format("row %d", i) );
553 data.push_back( long(5*i) );
554
555 lc->AppendItem( data );
556 }
557 }
558 break;
559
560 case 3:
561 {
562 wxASSERT(!m_ctrl[3]);
563 wxDataViewTreeCtrl* tc =
564 new wxDataViewTreeCtrl( parent, wxID_ANY, wxDefaultPosition,
565 wxDefaultSize, style );
566 m_ctrl[3] = tc;
567
568 wxImageList *ilist = new wxImageList( 16, 16 );
569 ilist->Add( wxIcon(wx_small_xpm) );
570 tc->SetImageList( ilist );
571
572 wxDataViewItem parent =
573 tc->AppendContainer( wxDataViewItem(0), wxT("The Root"), 0 );
574 tc->AppendItem( parent, wxT("Child 1"), 0 );
575 tc->AppendItem( parent, wxT("Child 2"), 0 );
576 tc->AppendItem( parent, wxT("Child 3, very long, long, long, long"), 0 );
577
578 wxDataViewItem cont =
579 tc->AppendContainer( parent, wxT("Container child"), 0 );
580 tc->AppendItem( cont, wxT("Child 4"), 0 );
581 tc->AppendItem( cont, wxT("Child 5"), 0 );
582
583 tc->Expand(cont);
584 }
585 break;
586 }
587 }
588
589
590 // ----------------------------------------------------------------------------
591 // MyFrame - generic event handlers
592 // ----------------------------------------------------------------------------
593
594 void MyFrame::OnPageChanged(wxBookCtrlEvent& WXUNUSED(event) )
595 {
596 unsigned int nPanel = m_notebook->GetSelection();
597
598 GetMenuBar()->FindItem(ID_STYLE_MENU)->SetItemLabel(
599 wxString::Format("Style of panel #%d", nPanel+1));
600
601 for (unsigned int id = ID_MULTIPLE; id <= ID_VERT_RULES; id++)
602 {
603 unsigned long style = 0;
604 switch (id)
605 {
606 /*case ID_SINGLE:
607 style = wxDV_SINGLE;
608 break;*/
609 case ID_MULTIPLE:
610 style = wxDV_MULTIPLE;
611 break;
612 case ID_ROW_LINES:
613 style = wxDV_ROW_LINES;
614 break;
615 case ID_HORIZ_RULES:
616 style = wxDV_HORIZ_RULES;
617 break;
618 case ID_VERT_RULES:
619 style = wxDV_VERT_RULES;
620 break;
621 default:
622 wxFAIL;
623 }
624
625 GetMenuBar()->FindItem(id)->Check( m_ctrl[nPanel]->HasFlag(style) );
626 }
627 }
628
629 void MyFrame::OnStyleChange(wxCommandEvent& WXUNUSED(event) )
630 {
631 unsigned int nPanel = m_notebook->GetSelection();
632
633 // build the style
634 unsigned long style = 0;
635 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
636 style |= wxDV_SINGLE;*/
637 if (GetMenuBar()->FindItem(ID_MULTIPLE)->IsChecked())
638 style |= wxDV_MULTIPLE;
639 if (GetMenuBar()->FindItem(ID_ROW_LINES)->IsChecked())
640 style |= wxDV_ROW_LINES;
641 if (GetMenuBar()->FindItem(ID_HORIZ_RULES)->IsChecked())
642 style |= wxDV_HORIZ_RULES;
643 if (GetMenuBar()->FindItem(ID_VERT_RULES)->IsChecked())
644 style |= wxDV_VERT_RULES;
645
646 wxSizer* sz = m_ctrl[nPanel]->GetContainingSizer();
647 wxASSERT(sz);
648
649 sz->Detach(m_ctrl[nPanel]);
650 wxDELETE(m_ctrl[nPanel]);
651 m_ctrl[nPanel] = NULL;
652
653 if (nPanel == 0)
654 m_music_model.reset(NULL);
655 else if (nPanel == 1)
656 m_list_model.reset(NULL);
657
658 // rebuild the DVC for the selected panel:
659 BuildDataViewCtrl((wxPanel*)m_notebook->GetPage(nPanel), nPanel, style);
660
661 sz->Prepend(m_ctrl[nPanel], 1, wxGROW|wxALL, 5);
662 sz->Layout();
663 }
664
665 void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
666 {
667 Close(true);
668 }
669
670 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
671 {
672 wxAboutDialogInfo info;
673 info.SetName(_("DataView sample"));
674 info.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
675 info.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
676 info.AddDeveloper("Robert Roebling");
677 info.AddDeveloper("Francesco Montorsi");
678
679 wxAboutBox(info);
680 }
681
682
683 // ----------------------------------------------------------------------------
684 // MyFrame - event handlers for the first page
685 // ----------------------------------------------------------------------------
686
687 void MyFrame::OnBeginDrag( wxDataViewEvent &event )
688 {
689 wxDataViewItem item( event.GetItem() );
690
691 // only allow drags for item, not containers
692 if (m_music_model->IsContainer( item ) )
693 {
694 event.Veto();
695 return;
696 }
697
698 MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
699 wxTextDataObject *obj = new wxTextDataObject;
700 obj->SetText( node->m_title );
701 event.SetDataObject( obj );
702 }
703
704 void MyFrame::OnDropPossible( wxDataViewEvent &event )
705 {
706 wxDataViewItem item( event.GetItem() );
707
708 // only allow drags for item, not containers
709 if (m_music_model->IsContainer( item ) )
710 event.Veto();
711
712 if (event.GetDataFormat() != wxDF_UNICODETEXT)
713 event.Veto();
714 }
715
716 void MyFrame::OnDrop( wxDataViewEvent &event )
717 {
718 wxDataViewItem item( event.GetItem() );
719
720 // only allow drops for item, not containers
721 if (m_music_model->IsContainer( item ) )
722 {
723 event.Veto();
724 return;
725 }
726
727 if (event.GetDataFormat() != wxDF_UNICODETEXT)
728 {
729 event.Veto();
730 return;
731 }
732
733 wxTextDataObject obj;
734 obj.SetData( wxDF_UNICODETEXT, event.GetDataSize(), event.GetDataBuffer() );
735
736 wxLogMessage(wxT("Text dropped: %s"), obj.GetText() );
737 }
738
739 void MyFrame::OnAddMozart( wxCommandEvent& WXUNUSED(event) )
740 {
741 m_music_model->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
742 }
743
744 void MyFrame::OnDeleteMusic( wxCommandEvent& WXUNUSED(event) )
745 {
746 wxDataViewItemArray items;
747 int len = m_ctrl[0]->GetSelections( items );
748 for( int i = 0; i < len; i ++ )
749 if (items[i].IsOk())
750 m_music_model->Delete( items[i] );
751 }
752
753 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
754 {
755 m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) );
756 FindWindow( ID_DELETE_YEAR )->Disable();
757 }
758
759 void MyFrame::OnSelectNinth( wxCommandEvent& WXUNUSED(event) )
760 {
761 m_ctrl[0]->Select( m_music_model->GetNinthItem() );
762 }
763
764 void MyFrame::OnValueChanged( wxDataViewEvent &event )
765 {
766 if (!m_log)
767 return;
768
769 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"),
770 event.GetItem().GetID(), event.GetColumn() );
771 }
772
773 void MyFrame::OnActivated( wxDataViewEvent &event )
774 {
775 if(!m_log)
776 return;
777
778 wxString title = m_music_model->GetTitle( event.GetItem() );
779 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title );
780
781 if (m_ctrl[0]->IsExpanded( event.GetItem() ))
782 wxLogMessage(wxT("Item: %s is expanded"), title );
783 }
784
785 void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
786 {
787 if(!m_log)
788 return;
789
790 wxString title = m_music_model->GetTitle( event.GetItem() );
791 if (title.empty())
792 title = wxT("None");
793
794 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title );
795 }
796
797 void MyFrame::OnExpanding( wxDataViewEvent &event )
798 {
799 if (!m_log)
800 return;
801
802 wxString title = m_music_model->GetTitle( event.GetItem() );
803 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title );
804 }
805
806
807 void MyFrame::OnEditingStarted( wxDataViewEvent &event )
808 {
809 if (!m_log)
810 return;
811
812 wxString title = m_music_model->GetTitle( event.GetItem() );
813 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title );
814 }
815
816 void MyFrame::OnEditingDone( wxDataViewEvent &event )
817 {
818 if (!m_log)
819 return;
820
821 wxString title = m_music_model->GetTitle( event.GetItem() );
822 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title );
823 }
824
825 void MyFrame::OnExpanded( wxDataViewEvent &event )
826 {
827 if (!m_log)
828 return;
829
830 wxString title = m_music_model->GetTitle( event.GetItem() );
831 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title );
832 }
833
834 void MyFrame::OnCollapsing( wxDataViewEvent &event )
835 {
836 if (!m_log)
837 return;
838
839 wxString title = m_music_model->GetTitle( event.GetItem() );
840 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title );
841 }
842
843 void MyFrame::OnCollapsed( wxDataViewEvent &event )
844 {
845 if (!m_log)
846 return;
847
848 wxString title = m_music_model->GetTitle( event.GetItem() );
849 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title);
850 }
851
852 void MyFrame::OnContextMenu( wxDataViewEvent &event )
853 {
854 if (!m_log)
855 return;
856
857 wxString title = m_music_model->GetTitle( event.GetItem() );
858 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title );
859
860 wxMenu menu;
861 menu.Append( 1, wxT("entry 1") );
862 menu.Append( 2, wxT("entry 2") );
863 menu.Append( 3, wxT("entry 3") );
864
865 m_ctrl[0]->PopupMenu(&menu);
866
867 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),
868 title.GetData(), event.GetValue().GetString());
869 }
870
871 void MyFrame::OnHeaderClick( wxDataViewEvent &event )
872 {
873 // we need to skip the event to let the default behaviour of sorting by
874 // this column when it is clicked to take place
875 event.Skip();
876
877 if (!m_log)
878 return;
879
880 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
881
882 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos );
883 }
884
885 void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
886 {
887 if(!m_log)
888 return;
889
890 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
891
892 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos );
893 }
894
895 void MyFrame::OnSorted( wxDataViewEvent &event )
896 {
897 if(!m_log)
898 return;
899
900 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
901
902 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos );
903 }
904
905 void MyFrame::OnRightClick( wxMouseEvent &event )
906 {
907 if(!m_log)
908 return;
909
910 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"),
911 event.GetX(), event.GetY());
912 }
913
914
915 // ----------------------------------------------------------------------------
916 // MyFrame - event handlers for the second page
917 // ----------------------------------------------------------------------------
918
919 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
920 {
921 m_list_model->Prepend(wxT("Test"));
922 }
923
924 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
925 {
926 wxDataViewItemArray items;
927 int len = m_ctrl[1]->GetSelections( items );
928 if (len > 0)
929 m_list_model->DeleteItems( items );
930 }
931
932 void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
933 {
934 wxDataViewItem item = m_list_model->GetItem( 50 );
935 m_ctrl[1]->EnsureVisible(item,m_col);
936 }
937
938 void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
939 {
940 m_list_model->AddMany();
941 }
942