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