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