Add optional columns autosizing to wxDataViewCtrl.
[wxWidgets.git] / samples / dataview / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
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/colordlg.h"
32 #include "wx/choicdlg.h"
33 #include "wx/numdlg.h"
34 #include "wx/spinctrl.h"
35 #include "wx/imaglist.h"
36 #include "wx/notebook.h"
37
38 #include "mymodels.h"
39
40 // ----------------------------------------------------------------------------
41 // resources
42 // ----------------------------------------------------------------------------
43
44 #ifndef __WXMSW__
45 #include "../sample.xpm"
46 #endif
47
48 #include "wx_small.xpm"
49
50 // ----------------------------------------------------------------------------
51 // MyApp
52 // ----------------------------------------------------------------------------
53
54 class MyApp: public wxApp
55 {
56 public:
57 virtual bool OnInit();
58 };
59
60 // ----------------------------------------------------------------------------
61 // MyFrame
62 // ----------------------------------------------------------------------------
63
64 class MyFrame : public wxFrame
65 {
66 public:
67 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
68 ~MyFrame();
69
70 void BuildDataViewCtrl(wxPanel* parent,
71 unsigned int nPanel,
72 unsigned long style = 0);
73
74 private:
75 // event handlers
76 void OnStyleChange(wxCommandEvent& event);
77 void OnSetBackgroundColour(wxCommandEvent& event);
78 void OnSetForegroundColour(wxCommandEvent& event);
79 void OnQuit(wxCommandEvent& event);
80 void OnAbout(wxCommandEvent& event);
81
82 void OnClearLog(wxCommandEvent& event);
83 void OnPageChanged(wxBookCtrlEvent& event);
84
85 void OnAddMozart(wxCommandEvent& event);
86 void OnDeleteSelected(wxCommandEvent& event);
87 void OnDeleteYear(wxCommandEvent& event);
88 void OnSelectNinth(wxCommandEvent& event);
89 void OnCollapse(wxCommandEvent& event);
90 void OnExpand(wxCommandEvent& event);
91 void OnShowCurrent(wxCommandEvent& event);
92 void OnSetNinthCurrent(wxCommandEvent& event);
93
94 void OnPrependList(wxCommandEvent& event);
95 void OnDeleteList(wxCommandEvent& event);
96 // Fourth page.
97 void OnDeleteTreeItem(wxCommandEvent& event);
98 void OnDeleteAllTreeItems(wxCommandEvent& event);
99 void OnAddTreeItem(wxCommandEvent& event);
100 void OnAddTreeContainerItem(wxCommandEvent& event);
101
102 void OnValueChanged( wxDataViewEvent &event );
103
104 void OnActivated( wxDataViewEvent &event );
105 void OnExpanding( wxDataViewEvent &event );
106 void OnExpanded( wxDataViewEvent &event );
107 void OnCollapsing( wxDataViewEvent &event );
108 void OnCollapsed( wxDataViewEvent &event );
109 void OnSelectionChanged( wxDataViewEvent &event );
110
111 void OnStartEditing( wxDataViewEvent &event );
112 void OnEditingStarted( wxDataViewEvent &event );
113 void OnEditingDone( wxDataViewEvent &event );
114
115 void OnHeaderClick( wxDataViewEvent &event );
116 void OnAttrHeaderClick( wxDataViewEvent &event );
117 void OnHeaderRightClick( wxDataViewEvent &event );
118 void OnSorted( wxDataViewEvent &event );
119
120 void OnContextMenu( wxDataViewEvent &event );
121
122 void OnRightClick( wxMouseEvent &event );
123 void OnGoto( wxCommandEvent &event);
124 void OnAddMany( wxCommandEvent &event);
125 void OnHideAttributes( wxCommandEvent &event);
126 void OnShowAttributes( wxCommandEvent &event);
127
128 #if wxUSE_DRAG_AND_DROP
129 void OnBeginDrag( wxDataViewEvent &event );
130 void OnDropPossible( wxDataViewEvent &event );
131 void OnDrop( wxDataViewEvent &event );
132 #endif // wxUSE_DRAG_AND_DROP
133
134 void OnDataViewChar(wxKeyEvent& event);
135
136 // helper used by both OnDeleteSelected() and OnDataViewChar()
137 void DeleteSelectedItems();
138
139
140 wxNotebook* m_notebook;
141
142 // the controls stored in the various tabs of the main notebook:
143
144 wxDataViewCtrl* m_ctrl[4];
145
146 // the models associated with the first two DVC:
147
148 wxObjectDataPtr<MyMusicTreeModel> m_music_model;
149 wxObjectDataPtr<MyListModel> m_list_model;
150
151 // other data:
152
153 wxDataViewColumn* m_col;
154 wxDataViewColumn* m_attributes;
155
156 wxTextCtrl* m_log;
157 wxLog *m_logOld;
158
159 private:
160 DECLARE_EVENT_TABLE()
161 };
162
163
164 // ----------------------------------------------------------------------------
165 // MyCustomRenderer
166 // ----------------------------------------------------------------------------
167
168 class MyCustomRenderer: public wxDataViewCustomRenderer
169 {
170 public:
171 MyCustomRenderer()
172 : wxDataViewCustomRenderer("string",
173 wxDATAVIEW_CELL_ACTIVATABLE,
174 wxALIGN_CENTER)
175 { }
176
177 virtual bool Render( wxRect rect, wxDC *dc, int state )
178 {
179 dc->SetBrush( *wxLIGHT_GREY_BRUSH );
180 dc->SetPen( *wxTRANSPARENT_PEN );
181
182 rect.Deflate(2);
183 dc->DrawRoundedRectangle( rect, 5 );
184
185 RenderText(m_value,
186 0, // no offset
187 wxRect(dc->GetTextExtent(m_value)).CentreIn(rect),
188 dc,
189 state);
190 return true;
191 }
192
193 virtual bool Activate( wxRect WXUNUSED(cell),
194 wxDataViewModel *WXUNUSED(model),
195 const wxDataViewItem &WXUNUSED(item),
196 unsigned int WXUNUSED(col) )
197 {
198 wxLogMessage( "MyCustomRenderer Activate()" );
199 return false;
200 }
201
202 virtual bool LeftClick( wxPoint cursor, wxRect WXUNUSED(cell),
203 wxDataViewModel *WXUNUSED(model),
204 const wxDataViewItem &WXUNUSED(item),
205 unsigned int WXUNUSED(col) )
206 {
207 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor.x, cursor.y );
208 return false;
209 }
210
211 virtual wxSize GetSize() const
212 {
213 return wxSize(60,20);
214 }
215
216 virtual bool SetValue( const wxVariant &value )
217 {
218 m_value = value.GetString();
219 return true;
220 }
221
222 virtual bool GetValue( wxVariant &WXUNUSED(value) ) const { return true; }
223
224 private:
225 wxString m_value;
226 };
227
228
229 // ============================================================================
230 // implementation
231 // ============================================================================
232
233 // ----------------------------------------------------------------------------
234 // MyApp
235 // ----------------------------------------------------------------------------
236
237 IMPLEMENT_APP(MyApp)
238
239 bool MyApp::OnInit()
240 {
241 if ( !wxApp::OnInit() )
242 return false;
243
244 MyFrame *frame =
245 new MyFrame(NULL, "wxDataViewCtrl sample", 40, 40, 1000, 540);
246 SetTopWindow(frame);
247
248 frame->Show(true);
249 return true;
250 }
251
252
253 // ----------------------------------------------------------------------------
254 // MyFrame
255 // ----------------------------------------------------------------------------
256
257 enum
258 {
259 ID_CLEARLOG = wxID_HIGHEST+1,
260 ID_BACKGROUND_COLOUR,
261 ID_FOREGROUND_COLOUR,
262 ID_STYLE_MENU,
263
264 // file menu
265 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
266 ID_MULTIPLE,
267 ID_ROW_LINES,
268 ID_HORIZ_RULES,
269 ID_VERT_RULES,
270
271 ID_EXIT = wxID_EXIT,
272
273 // about menu
274 ID_ABOUT = wxID_ABOUT,
275
276
277 // control IDs
278
279 ID_MUSIC_CTRL = 50,
280 ID_ATTR_CTRL = 51,
281
282 ID_ADD_MOZART = 100,
283 ID_DELETE_SEL = 101,
284 ID_DELETE_YEAR = 102,
285 ID_SELECT_NINTH = 103,
286 ID_COLLAPSE = 104,
287 ID_EXPAND = 105,
288 ID_SHOW_CURRENT,
289 ID_SET_NINTH_CURRENT,
290
291 ID_PREPEND_LIST = 200,
292 ID_DELETE_LIST = 201,
293 ID_GOTO = 202,
294 ID_ADD_MANY = 203,
295 ID_HIDE_ATTRIBUTES = 204,
296 ID_SHOW_ATTRIBUTES = 205,
297
298 // Fourth page.
299 ID_DELETE_TREE_ITEM = 400,
300 ID_DELETE_ALL_TREE_ITEMS = 401,
301 ID_ADD_TREE_ITEM = 402,
302 ID_ADD_TREE_CONTAINER_ITEM = 403
303 };
304
305 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
306 EVT_MENU_RANGE( ID_MULTIPLE, ID_VERT_RULES, MyFrame::OnStyleChange )
307 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
308 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
309 EVT_MENU( ID_CLEARLOG, MyFrame::OnClearLog )
310
311 EVT_MENU( ID_FOREGROUND_COLOUR, MyFrame::OnSetForegroundColour )
312 EVT_MENU( ID_BACKGROUND_COLOUR, MyFrame::OnSetBackgroundColour )
313
314 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY, MyFrame::OnPageChanged )
315
316 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
317 EVT_BUTTON( ID_DELETE_SEL, MyFrame::OnDeleteSelected )
318 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
319 EVT_BUTTON( ID_SELECT_NINTH, MyFrame::OnSelectNinth )
320 EVT_BUTTON( ID_COLLAPSE, MyFrame::OnCollapse )
321 EVT_BUTTON( ID_EXPAND, MyFrame::OnExpand )
322 EVT_BUTTON( ID_SHOW_CURRENT, MyFrame::OnShowCurrent )
323 EVT_BUTTON( ID_SET_NINTH_CURRENT, MyFrame::OnSetNinthCurrent )
324
325 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
326 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
327 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
328 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
329 EVT_BUTTON( ID_HIDE_ATTRIBUTES, MyFrame::OnHideAttributes)
330 EVT_BUTTON( ID_SHOW_ATTRIBUTES, MyFrame::OnShowAttributes)
331 // Fourth page.
332 EVT_BUTTON( ID_DELETE_TREE_ITEM, MyFrame::OnDeleteTreeItem )
333 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS, MyFrame::OnDeleteAllTreeItems )
334 EVT_BUTTON( ID_ADD_TREE_ITEM, MyFrame::OnAddTreeItem )
335 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM, MyFrame::OnAddTreeContainerItem )
336
337 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
338
339 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
340 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
341 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
342 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
343 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
344 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
345
346 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL, MyFrame::OnStartEditing)
347 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
348 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
349
350 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
351 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
352 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
353
354 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
355
356 #if wxUSE_DRAG_AND_DROP
357 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
358 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL, MyFrame::OnDropPossible )
359 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL, MyFrame::OnDrop )
360 #endif // wxUSE_DRAG_AND_DROP
361
362 EVT_RIGHT_UP(MyFrame::OnRightClick)
363
364 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL, MyFrame::OnAttrHeaderClick)
365
366 END_EVENT_TABLE()
367
368 MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
369 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
370 {
371 m_log = NULL;
372 m_col = NULL;
373
374 m_ctrl[0] = NULL;
375 m_ctrl[1] = NULL;
376 m_ctrl[2] = NULL;
377 m_ctrl[3] = NULL;
378
379 SetIcon(wxICON(sample));
380
381
382 // build the menus
383 // ----------------
384
385 wxMenu *style_menu = new wxMenu;
386 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
387 style_menu->AppendCheckItem(ID_MULTIPLE, "Multiple selection");
388 style_menu->AppendCheckItem(ID_ROW_LINES, "Alternating colours");
389 style_menu->AppendCheckItem(ID_HORIZ_RULES, "Display horizontal rules");
390 style_menu->AppendCheckItem(ID_VERT_RULES, "Display vertical rules");
391
392 wxMenu *file_menu = new wxMenu;
393 file_menu->Append(ID_CLEARLOG, "&Clear log\tCtrl-L");
394 file_menu->Append(ID_FOREGROUND_COLOUR, "Set &foreground colour...\tCtrl-S");
395 file_menu->Append(ID_BACKGROUND_COLOUR, "Set &background colour...\tCtrl-B");
396 file_menu->Append(ID_STYLE_MENU, "&Style", style_menu);
397 file_menu->AppendSeparator();
398 file_menu->Append(ID_EXIT, "E&xit");
399
400 wxMenu *about_menu = new wxMenu;
401 about_menu->Append(ID_ABOUT, "&About");
402
403 wxMenuBar *menu_bar = new wxMenuBar;
404 menu_bar->Append(file_menu, "&File");
405 menu_bar->Append(about_menu, "&About");
406
407 SetMenuBar(menu_bar);
408 CreateStatusBar();
409
410
411 // first page of the notebook
412 // --------------------------
413
414 m_notebook = new wxNotebook( this, wxID_ANY );
415
416 wxPanel *firstPanel = new wxPanel( m_notebook, wxID_ANY );
417
418 BuildDataViewCtrl(firstPanel, 0); // sets m_ctrl[0]
419
420 const wxSizerFlags border = wxSizerFlags().DoubleBorder();
421
422 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
423 button_sizer->Add( new wxButton( firstPanel, ID_ADD_MOZART, "Add Mozart"), border );
424 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_SEL, "Delete selected"), border );
425 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_YEAR, "Delete \"Year\" column"), border );
426 button_sizer->Add( new wxButton( firstPanel, ID_SELECT_NINTH,"Select ninth symphony"), border );
427 button_sizer->Add( new wxButton( firstPanel, ID_COLLAPSE, "Collapse"), border );
428 button_sizer->Add( new wxButton( firstPanel, ID_EXPAND, "Expand"), border );
429
430 wxBoxSizer *sizerCurrent = new wxBoxSizer(wxHORIZONTAL);
431 sizerCurrent->Add(new wxButton(firstPanel, ID_SHOW_CURRENT,
432 "&Show current"), border);
433 sizerCurrent->Add(new wxButton(firstPanel, ID_SET_NINTH_CURRENT,
434 "Make &ninth symphony current"), border);
435
436 wxSizer *firstPanelSz = new wxBoxSizer( wxVERTICAL );
437 m_ctrl[0]->SetMinSize(wxSize(-1, 200));
438 firstPanelSz->Add(m_ctrl[0], 1, wxGROW|wxALL, 5);
439 firstPanelSz->Add(
440 new wxStaticText(firstPanel, wxID_ANY, "Most of the cells above are editable!"),
441 0, wxGROW|wxALL, 5);
442 firstPanelSz->Add(button_sizer);
443 firstPanelSz->Add(sizerCurrent);
444 firstPanel->SetSizerAndFit(firstPanelSz);
445
446
447 // second page of the notebook
448 // ---------------------------
449
450 wxPanel *secondPanel = new wxPanel( m_notebook, wxID_ANY );
451
452 BuildDataViewCtrl(secondPanel, 1); // sets m_ctrl[1]
453
454 wxBoxSizer *button_sizer2 = new wxBoxSizer( wxHORIZONTAL );
455 button_sizer2->Add( new wxButton( secondPanel, ID_PREPEND_LIST,"Prepend"), 0, wxALL, 10 );
456 button_sizer2->Add( new wxButton( secondPanel, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
457 button_sizer2->Add( new wxButton( secondPanel, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
458 button_sizer2->Add( new wxButton( secondPanel, ID_ADD_MANY, "Add 1000"), 0, wxALL, 10 );
459 button_sizer2->Add( new wxButton( secondPanel, ID_HIDE_ATTRIBUTES, "Hide attributes"), 0, wxALL, 10 );
460 button_sizer2->Add( new wxButton( secondPanel, ID_SHOW_ATTRIBUTES, "Show attributes"), 0, wxALL, 10 );
461
462 wxSizer *secondPanelSz = new wxBoxSizer( wxVERTICAL );
463 secondPanelSz->Add(m_ctrl[1], 1, wxGROW|wxALL, 5);
464 secondPanelSz->Add(button_sizer2);
465 secondPanel->SetSizerAndFit(secondPanelSz);
466
467
468 // third page of the notebook
469 // ---------------------------
470
471 wxPanel *thirdPanel = new wxPanel( m_notebook, wxID_ANY );
472
473 BuildDataViewCtrl(thirdPanel, 2); // sets m_ctrl[2]
474
475 wxSizer *thirdPanelSz = new wxBoxSizer( wxVERTICAL );
476 thirdPanelSz->Add(m_ctrl[2], 1, wxGROW|wxALL, 5);
477 thirdPanel->SetSizerAndFit(thirdPanelSz);
478
479
480 // fourth page of the notebook
481 // ---------------------------
482
483 wxPanel *fourthPanel = new wxPanel( m_notebook, wxID_ANY );
484
485 BuildDataViewCtrl(fourthPanel, 3); // sets m_ctrl[3]
486 // Buttons
487 wxBoxSizer *button_sizer4 = new wxBoxSizer( wxHORIZONTAL );
488 button_sizer4->Add( new wxButton( fourthPanel, ID_DELETE_TREE_ITEM, "Delete Selected"), 0, wxALL, 10 );
489 button_sizer4->Add( new wxButton( fourthPanel, ID_DELETE_ALL_TREE_ITEMS, "Delete All"), 0, wxALL, 10 );
490 button_sizer4->Add( new wxButton( fourthPanel, ID_ADD_TREE_ITEM, "Add Item"), 0, wxALL, 10 );
491 button_sizer4->Add( new wxButton( fourthPanel, ID_ADD_TREE_CONTAINER_ITEM, "Add Container"), 0, wxALL, 10 );
492
493 wxSizer *fourthPanelSz = new wxBoxSizer( wxVERTICAL );
494 fourthPanelSz->Add(m_ctrl[3], 1, wxGROW|wxALL, 5);
495 fourthPanelSz->Add(button_sizer4);
496 fourthPanel->SetSizerAndFit(fourthPanelSz);
497
498
499
500 // complete GUI
501 // ------------
502
503 m_notebook->AddPage(firstPanel, "MyMusicTreeModel");
504 m_notebook->AddPage(secondPanel, "MyListModel");
505 m_notebook->AddPage(thirdPanel, "wxDataViewListCtrl");
506 m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl");
507
508 wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
509
510 m_log = new wxTextCtrl( this, wxID_ANY, wxString(), wxDefaultPosition,
511 wxDefaultSize, wxTE_MULTILINE );
512 m_log->SetMinSize(wxSize(-1, 100));
513 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
514 wxLogMessage( "This is the log window" );
515
516 mainSizer->Add( m_notebook, 1, wxGROW );
517 mainSizer->Add( m_log, 0, wxGROW );
518
519 SetSizerAndFit(mainSizer);
520 }
521
522 MyFrame::~MyFrame()
523 {
524 delete wxLog::SetActiveTarget(m_logOld);
525 }
526
527 void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned long style)
528 {
529 switch (nPanel)
530 {
531 case 0:
532 {
533 wxASSERT(!m_ctrl[0] && !m_music_model);
534 m_ctrl[0] =
535 new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
536 wxDefaultSize, style );
537 m_ctrl[0]->Connect(wxEVT_CHAR,
538 wxKeyEventHandler(MyFrame::OnDataViewChar),
539 NULL, this);
540
541 m_music_model = new MyMusicTreeModel;
542 m_ctrl[0]->AssociateModel( m_music_model.get() );
543
544 #if wxUSE_DRAG_AND_DROP
545 m_ctrl[0]->EnableDragSource( wxDF_UNICODETEXT );
546 m_ctrl[0]->EnableDropTarget( wxDF_UNICODETEXT );
547 #endif // wxUSE_DRAG_AND_DROP
548
549 // column 0 of the view control:
550
551 wxDataViewTextRenderer *tr =
552 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT );
553 wxDataViewColumn *column0 =
554 new wxDataViewColumn( "title", tr, 0, 200, wxALIGN_LEFT,
555 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE );
556 m_ctrl[0]->AppendColumn( column0 );
557 #if 0
558 // Call this and sorting is enabled
559 // immediatly upon start up.
560 column0->SetAsSortKey();
561 #endif
562
563 // column 1 of the view control:
564
565 tr = new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE );
566 wxDataViewColumn *column1 =
567 new wxDataViewColumn( "artist", tr, 1, 150, wxALIGN_LEFT,
568 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE |
569 wxDATAVIEW_COL_RESIZABLE );
570 column1->SetMinWidth(150); // this column can't be resized to be smaller
571 m_ctrl[0]->AppendColumn( column1 );
572
573 // column 2 of the view control:
574
575 wxDataViewSpinRenderer *sr =
576 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
577 wxDataViewColumn *column2 =
578 new wxDataViewColumn( "year", sr, 2, 60, wxALIGN_LEFT,
579 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE );
580 m_ctrl[0]->AppendColumn( column2 );
581
582 // column 3 of the view control:
583
584 wxArrayString choices;
585 choices.Add( "good" );
586 choices.Add( "bad" );
587 choices.Add( "lousy" );
588 wxDataViewChoiceRenderer *c =
589 new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
590 wxDataViewColumn *column3 =
591 new wxDataViewColumn( "rating", c, 3, 100, wxALIGN_LEFT,
592 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
593 m_ctrl[0]->AppendColumn( column3 );
594
595 // column 4 of the view control:
596
597 m_ctrl[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT, 80 );
598
599 // column 5 of the view control:
600
601 MyCustomRenderer *cr = new MyCustomRenderer;
602 wxDataViewColumn *column5 =
603 new wxDataViewColumn( "custom", cr, 5, -1, wxALIGN_LEFT,
604 wxDATAVIEW_COL_RESIZABLE );
605 m_ctrl[0]->AppendColumn( column5 );
606
607
608 // select initially the ninth symphony:
609 m_ctrl[0]->Select(m_music_model->GetNinthItem());
610 }
611 break;
612
613 case 1:
614 {
615 wxASSERT(!m_ctrl[1] && !m_list_model);
616 m_ctrl[1] = new wxDataViewCtrl( parent, ID_ATTR_CTRL, wxDefaultPosition,
617 wxDefaultSize, style );
618
619 m_list_model = new MyListModel;
620 m_ctrl[1]->AssociateModel( m_list_model.get() );
621
622 // the various columns
623 m_ctrl[1]->AppendTextColumn("editable string",
624 MyListModel::Col_EditableText,
625 wxDATAVIEW_CELL_EDITABLE,
626 wxCOL_WIDTH_AUTOSIZE);
627 m_ctrl[1]->AppendIconTextColumn("icon",
628 MyListModel::Col_IconText,
629 wxDATAVIEW_CELL_EDITABLE,
630 wxCOL_WIDTH_AUTOSIZE);
631
632 m_attributes =
633 new wxDataViewColumn("attributes",
634 new wxDataViewTextRenderer,
635 MyListModel::Col_TextWithAttr,
636 wxCOL_WIDTH_AUTOSIZE,
637 wxALIGN_RIGHT,
638 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
639 m_ctrl[1]->AppendColumn( m_attributes );
640
641 m_ctrl[1]->AppendColumn(
642 new wxDataViewColumn("custom renderer",
643 new MyCustomRenderer,
644 MyListModel::Col_Custom)
645 );
646 }
647 break;
648
649 case 2:
650 {
651 wxASSERT(!m_ctrl[2]);
652 wxDataViewListCtrl* lc =
653 new wxDataViewListCtrl( parent, wxID_ANY, wxDefaultPosition,
654 wxDefaultSize, style );
655 m_ctrl[2] = lc;
656
657 lc->AppendToggleColumn( "Toggle" );
658 lc->AppendTextColumn( "Text" );
659 lc->AppendProgressColumn( "Progress" );
660
661 wxVector<wxVariant> data;
662 for (unsigned int i=0; i<10; i++)
663 {
664 data.clear();
665 data.push_back( (i%3) == 0 );
666 data.push_back( wxString::Format("row %d", i) );
667 data.push_back( long(5*i) );
668
669 lc->AppendItem( data );
670 }
671 }
672 break;
673
674 case 3:
675 {
676 wxASSERT(!m_ctrl[3]);
677 wxDataViewTreeCtrl* tc =
678 new wxDataViewTreeCtrl( parent, wxID_ANY, wxDefaultPosition,
679 wxDefaultSize, style | wxDV_NO_HEADER );
680 m_ctrl[3] = tc;
681
682 wxImageList *ilist = new wxImageList( 16, 16 );
683 ilist->Add( wxIcon(wx_small_xpm) );
684 tc->SetImageList( ilist );
685
686 wxDataViewItem parent =
687 tc->AppendContainer( wxDataViewItem(0), "The Root", 0 );
688 tc->AppendItem( parent, "Child 1", 0 );
689 tc->AppendItem( parent, "Child 2", 0 );
690 tc->AppendItem( parent, "Child 3, very long, long, long, long", 0 );
691
692 wxDataViewItem cont =
693 tc->AppendContainer( parent, "Container child", 0 );
694 tc->AppendItem( cont, "Child 4", 0 );
695 tc->AppendItem( cont, "Child 5", 0 );
696
697 tc->Expand(cont);
698 }
699 break;
700 }
701 }
702
703
704 // ----------------------------------------------------------------------------
705 // MyFrame - generic event handlers
706 // ----------------------------------------------------------------------------
707
708 void MyFrame::OnClearLog( wxCommandEvent& WXUNUSED(event) )
709 {
710 m_log->Clear();
711 }
712
713 void MyFrame::OnSetForegroundColour(wxCommandEvent& WXUNUSED(event))
714 {
715 wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()];
716 wxColour col = wxGetColourFromUser(this, dvc->GetForegroundColour());
717 if ( col.IsOk() )
718 {
719 dvc->SetForegroundColour(col);
720 Refresh();
721 }
722 }
723
724 void MyFrame::OnSetBackgroundColour(wxCommandEvent& WXUNUSED(event))
725 {
726 wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()];
727 wxColour col = wxGetColourFromUser(this, dvc->GetBackgroundColour());
728 if ( col.IsOk() )
729 {
730 dvc->SetBackgroundColour(col);
731 Refresh();
732 }
733 }
734
735 void MyFrame::OnPageChanged( wxBookCtrlEvent& WXUNUSED(event) )
736 {
737 unsigned int nPanel = m_notebook->GetSelection();
738
739 GetMenuBar()->FindItem(ID_STYLE_MENU)->SetItemLabel(
740 wxString::Format("Style of panel #%d", nPanel+1));
741
742 for (unsigned int id = ID_MULTIPLE; id <= ID_VERT_RULES; id++)
743 {
744 unsigned long style = 0;
745 switch (id)
746 {
747 /*case ID_SINGLE:
748 style = wxDV_SINGLE;
749 break;*/
750 case ID_MULTIPLE:
751 style = wxDV_MULTIPLE;
752 break;
753 case ID_ROW_LINES:
754 style = wxDV_ROW_LINES;
755 break;
756 case ID_HORIZ_RULES:
757 style = wxDV_HORIZ_RULES;
758 break;
759 case ID_VERT_RULES:
760 style = wxDV_VERT_RULES;
761 break;
762 default:
763 wxFAIL;
764 }
765
766 GetMenuBar()->FindItem(id)->Check( m_ctrl[nPanel]->HasFlag(style) );
767 }
768 }
769
770 void MyFrame::OnStyleChange( wxCommandEvent& WXUNUSED(event) )
771 {
772 unsigned int nPanel = m_notebook->GetSelection();
773
774 // build the style
775 unsigned long style = 0;
776 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
777 style |= wxDV_SINGLE;*/
778 if (GetMenuBar()->FindItem(ID_MULTIPLE)->IsChecked())
779 style |= wxDV_MULTIPLE;
780 if (GetMenuBar()->FindItem(ID_ROW_LINES)->IsChecked())
781 style |= wxDV_ROW_LINES;
782 if (GetMenuBar()->FindItem(ID_HORIZ_RULES)->IsChecked())
783 style |= wxDV_HORIZ_RULES;
784 if (GetMenuBar()->FindItem(ID_VERT_RULES)->IsChecked())
785 style |= wxDV_VERT_RULES;
786
787 wxSizer* sz = m_ctrl[nPanel]->GetContainingSizer();
788 wxASSERT(sz);
789
790 sz->Detach(m_ctrl[nPanel]);
791 wxDELETE(m_ctrl[nPanel]);
792 m_ctrl[nPanel] = NULL;
793
794 if (nPanel == 0)
795 m_music_model.reset(NULL);
796 else if (nPanel == 1)
797 m_list_model.reset(NULL);
798
799 // rebuild the DVC for the selected panel:
800 BuildDataViewCtrl((wxPanel*)m_notebook->GetPage(nPanel), nPanel, style);
801
802 sz->Prepend(m_ctrl[nPanel], 1, wxGROW|wxALL, 5);
803 sz->Layout();
804 }
805
806 void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
807 {
808 Close(true);
809 }
810
811 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
812 {
813 wxAboutDialogInfo info;
814 info.SetName(_("DataView sample"));
815 info.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
816 info.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
817 info.AddDeveloper("Robert Roebling");
818 info.AddDeveloper("Francesco Montorsi");
819
820 wxAboutBox(info);
821 }
822
823
824 // ----------------------------------------------------------------------------
825 // MyFrame - event handlers for the first page
826 // ----------------------------------------------------------------------------
827
828 #if wxUSE_DRAG_AND_DROP
829
830 void MyFrame::OnBeginDrag( wxDataViewEvent &event )
831 {
832 wxDataViewItem item( event.GetItem() );
833
834 // only allow drags for item, not containers
835 if (m_music_model->IsContainer( item ) )
836 {
837 event.Veto();
838 return;
839 }
840
841 MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
842 wxTextDataObject *obj = new wxTextDataObject;
843 obj->SetText( node->m_title );
844 event.SetDataObject( obj );
845 }
846
847 void MyFrame::OnDropPossible( wxDataViewEvent &event )
848 {
849 wxDataViewItem item( event.GetItem() );
850
851 // only allow drags for item, not containers
852 if (m_music_model->IsContainer( item ) )
853 event.Veto();
854
855 if (event.GetDataFormat() != wxDF_UNICODETEXT)
856 event.Veto();
857 }
858
859 void MyFrame::OnDrop( wxDataViewEvent &event )
860 {
861 wxDataViewItem item( event.GetItem() );
862
863 // only allow drops for item, not containers
864 if (m_music_model->IsContainer( item ) )
865 {
866 event.Veto();
867 return;
868 }
869
870 if (event.GetDataFormat() != wxDF_UNICODETEXT)
871 {
872 event.Veto();
873 return;
874 }
875
876 wxTextDataObject obj;
877 obj.SetData( wxDF_UNICODETEXT, event.GetDataSize(), event.GetDataBuffer() );
878
879 wxLogMessage( "Text dropped: %s", obj.GetText() );
880 }
881
882 #endif // wxUSE_DRAG_AND_DROP
883
884 void MyFrame::OnAddMozart( wxCommandEvent& WXUNUSED(event) )
885 {
886 m_music_model->AddToClassical( "Eine kleine Nachtmusik", "Wolfgang Mozart", 1787 );
887 }
888
889 void MyFrame::DeleteSelectedItems()
890 {
891 wxDataViewItemArray items;
892 int len = m_ctrl[0]->GetSelections( items );
893 for( int i = 0; i < len; i ++ )
894 if (items[i].IsOk())
895 m_music_model->Delete( items[i] );
896 }
897
898 void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
899 {
900 DeleteSelectedItems();
901 }
902
903 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
904 {
905 m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) );
906 FindWindow( ID_DELETE_YEAR )->Disable();
907 }
908
909 void MyFrame::OnSelectNinth( wxCommandEvent& WXUNUSED(event) )
910 {
911 if (!m_music_model->GetNinthItem().IsOk())
912 {
913 wxLogError( "Cannot select the ninth symphony: it was removed!" );
914 return;
915 }
916
917 m_ctrl[0]->Select( m_music_model->GetNinthItem() );
918 }
919
920 void MyFrame::OnCollapse( wxCommandEvent& WXUNUSED(event) )
921 {
922 wxDataViewItem item = m_ctrl[0]->GetSelection();
923 if (item.IsOk())
924 m_ctrl[0]->Collapse( item );
925 }
926
927 void MyFrame::OnExpand( wxCommandEvent& WXUNUSED(event) )
928 {
929 wxDataViewItem item = m_ctrl[0]->GetSelection();
930 if (item.IsOk())
931 m_ctrl[0]->Expand( item );
932 }
933
934 void MyFrame::OnShowCurrent(wxCommandEvent& WXUNUSED(event))
935 {
936 wxDataViewItem item = m_ctrl[0]->GetCurrentItem();
937 if ( item.IsOk() )
938 {
939 wxLogMessage("Current item: \"%s\" by %s",
940 m_music_model->GetTitle(item),
941 m_music_model->GetArtist(item));
942 }
943 else
944 {
945 wxLogMessage("There is no current item.");
946 }
947 }
948
949 void MyFrame::OnSetNinthCurrent(wxCommandEvent& WXUNUSED(event))
950 {
951 wxDataViewItem item(m_music_model->GetNinthItem());
952 if ( !item.IsOk() )
953 {
954 wxLogError( "Cannot make the ninth symphony current: it was removed!" );
955 return;
956 }
957
958 m_ctrl[0]->SetCurrentItem(item);
959 }
960
961 void MyFrame::OnValueChanged( wxDataViewEvent &event )
962 {
963 if (!m_log)
964 return;
965
966 wxString title = m_music_model->GetTitle( event.GetItem() );
967 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
968 title, event.GetColumn() );
969 }
970
971 void MyFrame::OnActivated( wxDataViewEvent &event )
972 {
973 if(!m_log)
974 return;
975
976 wxString title = m_music_model->GetTitle( event.GetItem() );
977 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title );
978
979 if (m_ctrl[0]->IsExpanded( event.GetItem() ))
980 {
981 wxLogMessage( "Item: %s is expanded", title );
982 }
983 }
984
985 void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
986 {
987 if(!m_log)
988 return;
989
990 wxString title = m_music_model->GetTitle( event.GetItem() );
991 if (title.empty())
992 title = "None";
993
994 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title );
995 }
996
997 void MyFrame::OnExpanding( wxDataViewEvent &event )
998 {
999 if (!m_log)
1000 return;
1001
1002 wxString title = m_music_model->GetTitle( event.GetItem() );
1003 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title );
1004 }
1005
1006
1007 void MyFrame::OnStartEditing( wxDataViewEvent &event )
1008 {
1009 wxString artist = m_music_model->GetArtist( event.GetItem() );
1010 if (artist == "Ludwig van Beethoven")
1011 {
1012 event.Veto();
1013
1014 if (!m_log)
1015 return;
1016
1017 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist );
1018 }
1019 else
1020 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist );
1021
1022 }
1023
1024 void MyFrame::OnEditingStarted( wxDataViewEvent &event )
1025 {
1026 if (!m_log)
1027 return;
1028
1029 wxString title = m_music_model->GetTitle( event.GetItem() );
1030 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title );
1031 }
1032
1033 void MyFrame::OnEditingDone( wxDataViewEvent &event )
1034 {
1035 if (!m_log)
1036 return;
1037
1038 wxString title = m_music_model->GetTitle( event.GetItem() );
1039 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title );
1040 }
1041
1042 void MyFrame::OnExpanded( wxDataViewEvent &event )
1043 {
1044 if (!m_log)
1045 return;
1046
1047 wxString title = m_music_model->GetTitle( event.GetItem() );
1048 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title );
1049 }
1050
1051 void MyFrame::OnCollapsing( wxDataViewEvent &event )
1052 {
1053 if (!m_log)
1054 return;
1055
1056 wxString title = m_music_model->GetTitle( event.GetItem() );
1057 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title );
1058 }
1059
1060 void MyFrame::OnCollapsed( wxDataViewEvent &event )
1061 {
1062 if (!m_log)
1063 return;
1064
1065 wxString title = m_music_model->GetTitle( event.GetItem() );
1066 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title );
1067 }
1068
1069 void MyFrame::OnContextMenu( wxDataViewEvent &event )
1070 {
1071 if (!m_log)
1072 return;
1073
1074 wxString title = m_music_model->GetTitle( event.GetItem() );
1075 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title );
1076
1077 wxMenu menu;
1078 menu.Append( 1, "menuitem 1" );
1079 menu.Append( 2, "menuitem 2" );
1080 menu.Append( 3, "menuitem 3" );
1081
1082 m_ctrl[0]->PopupMenu(&menu);
1083 }
1084
1085 void MyFrame::OnAttrHeaderClick( wxDataViewEvent &event )
1086 {
1087 // we need to skip the event to let the default behaviour of sorting by
1088 // this column when it is clicked to take place
1089 event.Skip();
1090
1091 if (!m_log)
1092 return;
1093
1094 int pos = m_ctrl[1]->GetColumnPosition( event.GetDataViewColumn() );
1095
1096 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
1097 wxLogMessage( "Column title: %s Column width: %d", event.GetDataViewColumn()->GetTitle(), event.GetDataViewColumn()->GetWidth() );
1098 }
1099
1100 void MyFrame::OnHeaderClick( wxDataViewEvent &event )
1101 {
1102 // we need to skip the event to let the default behaviour of sorting by
1103 // this column when it is clicked to take place
1104 event.Skip();
1105
1106 if (!m_log)
1107 return;
1108
1109 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
1110
1111 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
1112 wxLogMessage( "Column width: %d", event.GetDataViewColumn()->GetWidth() );
1113 }
1114
1115 void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
1116 {
1117 if(!m_log)
1118 return;
1119
1120 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
1121
1122 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos );
1123 }
1124
1125 void MyFrame::OnSorted( wxDataViewEvent &event )
1126 {
1127 if(!m_log)
1128 return;
1129
1130 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
1131
1132 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos );
1133 }
1134
1135 void MyFrame::OnRightClick( wxMouseEvent &event )
1136 {
1137 if(!m_log)
1138 return;
1139
1140 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
1141 event.GetX(), event.GetY() );
1142 }
1143
1144 void MyFrame::OnDataViewChar(wxKeyEvent& event)
1145 {
1146 if ( event.GetKeyCode() == WXK_DELETE )
1147 DeleteSelectedItems();
1148 else
1149 event.Skip();
1150 }
1151
1152 // ----------------------------------------------------------------------------
1153 // MyFrame - event handlers for the second page
1154 // ----------------------------------------------------------------------------
1155
1156 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
1157 {
1158 m_list_model->Prepend("Test");
1159 }
1160
1161 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
1162 {
1163 wxDataViewItemArray items;
1164 int len = m_ctrl[1]->GetSelections( items );
1165 if (len > 0)
1166 m_list_model->DeleteItems( items );
1167 }
1168
1169 void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
1170 {
1171 wxDataViewItem item = m_list_model->GetItem( 50 );
1172 m_ctrl[1]->EnsureVisible(item,m_col);
1173 }
1174
1175 void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
1176 {
1177 m_list_model->AddMany();
1178 }
1179
1180 void MyFrame::OnHideAttributes(wxCommandEvent& WXUNUSED(event))
1181 {
1182 m_attributes->SetHidden(true);
1183 }
1184
1185 void MyFrame::OnShowAttributes(wxCommandEvent& WXUNUSED(event))
1186 {
1187 m_attributes->SetHidden(false);
1188 }
1189
1190 // ----------------------------------------------------------------------------
1191 // MyFrame - event handlers for the fourth page
1192 // ----------------------------------------------------------------------------
1193
1194 void MyFrame::OnDeleteTreeItem(wxCommandEvent& WXUNUSED(event))
1195 {
1196 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1197 wxDataViewItem selected = ctrl->GetSelection();
1198 if (!selected.IsOk())
1199 return;
1200
1201 ctrl->DeleteItem(selected);
1202 }
1203
1204 void MyFrame::OnDeleteAllTreeItems(wxCommandEvent& WXUNUSED(event))
1205 {
1206 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1207 ctrl->DeleteAllItems();
1208 }
1209
1210 void MyFrame::OnAddTreeItem(wxCommandEvent& WXUNUSED(event))
1211 {
1212 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1213 wxDataViewItem selected = ctrl->GetSelection();
1214 if (ctrl->IsContainer(selected))
1215 ctrl->AppendItem( selected, "Item", 0 );
1216 }
1217
1218 void MyFrame::OnAddTreeContainerItem(wxCommandEvent& WXUNUSED(event))
1219 {
1220 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1221 wxDataViewItem selected = ctrl->GetSelection();
1222 if (ctrl->IsContainer(selected))
1223 ctrl->AppendContainer(selected, "Container", 0 );
1224 }
1225