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