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