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