1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
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"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
44 #include "../sample.xpm"
47 #include "wx_small.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class MyApp
: public wxApp
56 virtual bool OnInit();
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class MyFrame
: public wxFrame
66 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
69 void BuildDataViewCtrl(wxPanel
* parent
,
71 unsigned long style
= 0);
73 public: // event handlers
75 void OnStyleChange(wxCommandEvent
& event
);
76 void OnQuit(wxCommandEvent
& event
);
77 void OnAbout(wxCommandEvent
& event
);
79 void OnClearLog(wxCommandEvent
& event
);
80 void OnPageChanged(wxBookCtrlEvent
& event
);
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
);
89 void OnPrependList(wxCommandEvent
& event
);
90 void OnDeleteList(wxCommandEvent
& event
);
92 void OnValueChanged( wxDataViewEvent
&event
);
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
);
101 void OnStartEditing( wxDataViewEvent
&event
);
102 void OnEditingStarted( wxDataViewEvent
&event
);
103 void OnEditingDone( wxDataViewEvent
&event
);
105 void OnHeaderClick( wxDataViewEvent
&event
);
106 void OnHeaderRightClick( wxDataViewEvent
&event
);
107 void OnSorted( wxDataViewEvent
&event
);
109 void OnContextMenu( wxDataViewEvent
&event
);
111 void OnRightClick( wxMouseEvent
&event
);
112 void OnGoto( wxCommandEvent
&event
);
113 void OnAddMany( wxCommandEvent
&event
);
115 void OnBeginDrag( wxDataViewEvent
&event
);
116 void OnDropPossible( wxDataViewEvent
&event
);
117 void OnDrop( wxDataViewEvent
&event
);
120 wxNotebook
* m_notebook
;
122 // the controls stored in the various tabs of the main notebook:
124 wxDataViewCtrl
* m_ctrl
[4];
126 // the models associated with the first two DVC:
128 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
129 wxObjectDataPtr
<MyListModel
> m_list_model
;
133 wxDataViewColumn
* m_col
;
139 DECLARE_EVENT_TABLE()
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 class MyCustomRenderer
: public wxDataViewCustomRenderer
150 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
151 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
154 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
156 dc
->SetBrush( *wxRED_BRUSH
);
157 dc
->SetPen( *wxTRANSPARENT_PEN
);
158 dc
->DrawRectangle( rect
.Deflate(2) );
162 virtual bool Activate( wxRect
WXUNUSED(cell
),
163 wxDataViewModel
*WXUNUSED(model
),
164 const wxDataViewItem
&WXUNUSED(item
),
165 unsigned int WXUNUSED(col
) )
167 wxLogMessage( "MyCustomRenderer Activate()" );
171 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
172 wxDataViewModel
*WXUNUSED(model
),
173 const wxDataViewItem
&WXUNUSED(item
),
174 unsigned int WXUNUSED(col
) )
176 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
180 virtual wxSize
GetSize() const
182 //return wxSize(60,m_height);
183 return wxSize(60,20);
186 virtual bool SetValue( const wxVariant
&value
)
192 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
199 // ============================================================================
201 // ============================================================================
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
211 if ( !wxApp::OnInit() )
215 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
229 ID_CLEARLOG
= wxID_HIGHEST
+1,
233 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
242 ID_ABOUT
= wxID_ABOUT
,
250 ID_DELETE_MUSIC
= 101,
251 ID_DELETE_YEAR
= 102,
252 ID_SELECT_NINTH
= 103,
256 ID_PREPEND_LIST
= 200,
257 ID_DELETE_LIST
= 201,
262 BEGIN_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
)
268 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
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
)
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
)
282 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
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
)
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
)
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
)
299 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
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
)
305 EVT_RIGHT_UP(MyFrame::OnRightClick
)
308 MyFrame::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
))
319 SetIcon(wxICON(sample
));
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");
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");
338 wxMenu
*about_menu
= new wxMenu
;
339 about_menu
->Append(ID_ABOUT
, "&About");
341 wxMenuBar
*menu_bar
= new wxMenuBar
;
342 menu_bar
->Append(file_menu
, "&File");
343 menu_bar
->Append(about_menu
, "&About");
345 SetMenuBar(menu_bar
);
349 // first page of the notebook
350 // --------------------------
352 m_notebook
= new wxNotebook( this, wxID_ANY
);
354 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
356 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
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 );
366 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
367 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
368 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
370 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
372 firstPanelSz
->Add(button_sizer
);
373 firstPanel
->SetSizerAndFit(firstPanelSz
);
376 // second page of the notebook
377 // ---------------------------
379 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
381 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
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 );
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
);
395 // third page of the notebook
396 // ---------------------------
398 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
400 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
402 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
403 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
404 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
407 // fourth page of the notebook
408 // ---------------------------
410 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
412 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
414 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
415 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
416 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
423 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
424 m_notebook
->AddPage(secondPanel
, "MyListModel");
425 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
426 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
428 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
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" );
436 mainSizer
->Add( m_notebook
, 1, wxGROW
);
437 mainSizer
->Add( m_log
, 0, wxGROW
);
439 SetSizerAndFit(mainSizer
);
444 delete wxLog::SetActiveTarget(m_logOld
);
447 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
453 wxASSERT(!m_ctrl
[0] && !m_music_model
);
455 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
456 wxDefaultSize
, style
);
458 m_music_model
= new MyMusicTreeModel
;
459 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
461 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
462 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
464 // column 0 of the view control:
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
);
473 // Call this and sorting is enabled
474 // immediatly upon start up.
475 column0
->SetAsSortKey();
478 // column 1 of the view control:
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
);
488 // column 2 of the view control:
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
);
497 // column 3 of the view control:
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
);
510 // column 4 of the view control:
512 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
514 // column 5 of the view control:
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
);
523 // select initially the ninth symphony:
524 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
530 wxASSERT(!m_ctrl
[1] && !m_list_model
);
531 m_ctrl
[1] = new wxDataViewCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
532 wxDefaultSize
, style
);
534 m_list_model
= new MyListModel
;
535 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
537 // the various columns
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");
542 m_ctrl
[1]->AppendTextColumn("editable string", 0, wxDATAVIEW_CELL_EDITABLE
);
543 m_ctrl
[1]->AppendIconTextColumn("icon", 1, wxDATAVIEW_CELL_INERT
);
545 m_ctrl
[1]->AppendColumn(
546 new wxDataViewColumn("attributes", new wxDataViewTextRendererAttr
, 2 ));
552 wxASSERT(!m_ctrl
[2]);
553 wxDataViewListCtrl
* lc
=
554 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
555 wxDefaultSize
, style
);
558 lc
->AppendToggleColumn( "Toggle" );
559 lc
->AppendTextColumn( "Text" );
560 lc
->AppendProgressColumn( "Progress" );
562 wxVector
<wxVariant
> data
;
563 for (unsigned int i
=0; i
<10; i
++)
566 data
.push_back( (i%3
) == 0 );
567 data
.push_back( wxString::Format("row %d", i
) );
568 data
.push_back( long(5*i
) );
570 lc
->AppendItem( data
);
577 wxASSERT(!m_ctrl
[3]);
578 wxDataViewTreeCtrl
* tc
=
579 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
580 wxDefaultSize
, style
);
583 wxImageList
*ilist
= new wxImageList( 16, 16 );
584 ilist
->Add( wxIcon(wx_small_xpm
) );
585 tc
->SetImageList( ilist
);
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 );
593 wxDataViewItem cont
=
594 tc
->AppendContainer( parent
, "Container child", 0 );
595 tc
->AppendItem( cont
, "Child 4", 0 );
596 tc
->AppendItem( cont
, "Child 5", 0 );
605 // ----------------------------------------------------------------------------
606 // MyFrame - generic event handlers
607 // ----------------------------------------------------------------------------
609 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
614 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
616 unsigned int nPanel
= m_notebook
->GetSelection();
618 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
619 wxString::Format("Style of panel #%d", nPanel
+1));
621 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
623 unsigned long style
= 0;
630 style
= wxDV_MULTIPLE
;
633 style
= wxDV_ROW_LINES
;
636 style
= wxDV_HORIZ_RULES
;
639 style
= wxDV_VERT_RULES
;
645 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
649 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
651 unsigned int nPanel
= m_notebook
->GetSelection();
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
;
666 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
669 sz
->Detach(m_ctrl
[nPanel
]);
670 wxDELETE(m_ctrl
[nPanel
]);
671 m_ctrl
[nPanel
] = NULL
;
674 m_music_model
.reset(NULL
);
675 else if (nPanel
== 1)
676 m_list_model
.reset(NULL
);
678 // rebuild the DVC for the selected panel:
679 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
681 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
685 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
690 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
692 wxAboutDialogInfo info
;
693 info
.SetName(_("DataView sample"));
694 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
695 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
696 info
.AddDeveloper("Robert Roebling");
697 info
.AddDeveloper("Francesco Montorsi");
703 // ----------------------------------------------------------------------------
704 // MyFrame - event handlers for the first page
705 // ----------------------------------------------------------------------------
707 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
709 wxDataViewItem
item( event
.GetItem() );
711 // only allow drags for item, not containers
712 if (m_music_model
->IsContainer( item
) )
718 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
719 wxTextDataObject
*obj
= new wxTextDataObject
;
720 obj
->SetText( node
->m_title
);
721 event
.SetDataObject( obj
);
724 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
726 wxDataViewItem
item( event
.GetItem() );
728 // only allow drags for item, not containers
729 if (m_music_model
->IsContainer( item
) )
732 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
736 void MyFrame::OnDrop( wxDataViewEvent
&event
)
738 wxDataViewItem
item( event
.GetItem() );
740 // only allow drops for item, not containers
741 if (m_music_model
->IsContainer( item
) )
747 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
753 wxTextDataObject obj
;
754 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
756 wxLogMessage( "Text dropped: %s", obj
.GetText() );
759 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
761 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
764 void MyFrame::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
766 wxDataViewItemArray items
;
767 int len
= m_ctrl
[0]->GetSelections( items
);
768 for( int i
= 0; i
< len
; i
++ )
770 m_music_model
->Delete( items
[i
] );
773 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
775 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
776 FindWindow( ID_DELETE_YEAR
)->Disable();
779 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
781 if (!m_music_model
->GetNinthItem().IsOk())
783 wxLogError( "Cannot select the ninth symphony: it was removed!" );
787 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
790 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
792 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
794 m_ctrl
[0]->Collapse( item
);
797 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
799 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
801 m_ctrl
[0]->Expand( item
);
804 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
809 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d",
810 event
.GetItem().GetID(), event
.GetColumn() );
813 void MyFrame::OnActivated( wxDataViewEvent
&event
)
818 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
819 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
821 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
823 wxLogMessage( "Item: %s is expanded", title
);
827 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
832 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
836 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
839 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
844 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
845 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
849 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
851 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
852 if (artist
== "Ludwig van Beethoven")
859 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
862 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
866 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
871 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
872 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
875 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
880 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
881 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
884 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
889 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
890 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
893 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
898 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
899 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
902 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
907 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
908 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
911 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
916 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
917 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
920 menu
.Append( 1, "menuitem 1" );
921 menu
.Append( 2, "menuitem 2" );
922 menu
.Append( 3, "menuitem 3" );
924 m_ctrl
[0]->PopupMenu(&menu
);
927 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
929 // we need to skip the event to let the default behaviour of sorting by
930 // this column when it is clicked to take place
936 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
938 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
939 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
942 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
947 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
949 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
952 void MyFrame::OnSorted( wxDataViewEvent
&event
)
957 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
959 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
962 void MyFrame::OnRightClick( wxMouseEvent
&event
)
967 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
968 event
.GetX(), event
.GetY() );
972 // ----------------------------------------------------------------------------
973 // MyFrame - event handlers for the second page
974 // ----------------------------------------------------------------------------
976 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
978 m_list_model
->Prepend("Test");
981 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
983 wxDataViewItemArray items
;
984 int len
= m_ctrl
[1]->GetSelections( items
);
986 m_list_model
->DeleteItems( items
);
989 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
991 wxDataViewItem item
= m_list_model
->GetItem( 50 );
992 m_ctrl
[1]->EnsureVisible(item
,m_col
);
995 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
997 m_list_model
->AddMany();