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