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