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