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 OnEditingStarted( wxDataViewEvent
&event
);
102 void OnEditingDone( wxDataViewEvent
&event
);
104 void OnHeaderClick( wxDataViewEvent
&event
);
105 void OnHeaderRightClick( wxDataViewEvent
&event
);
106 void OnSorted( wxDataViewEvent
&event
);
108 void OnContextMenu( wxDataViewEvent
&event
);
110 void OnRightClick( wxMouseEvent
&event
);
111 void OnGoto( wxCommandEvent
&event
);
112 void OnAddMany( wxCommandEvent
&event
);
114 void OnBeginDrag( wxDataViewEvent
&event
);
115 void OnDropPossible( wxDataViewEvent
&event
);
116 void OnDrop( wxDataViewEvent
&event
);
119 wxNotebook
* m_notebook
;
121 // the controls stored in the various tabs of the main notebook:
123 wxDataViewCtrl
* m_ctrl
[4];
125 // the models associated with the first two DVC:
127 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
128 wxObjectDataPtr
<MyListModel
> m_list_model
;
132 wxDataViewColumn
* m_col
;
138 DECLARE_EVENT_TABLE()
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 class MyCustomRenderer
: public wxDataViewCustomRenderer
149 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
150 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
153 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
155 dc
->SetBrush( *wxRED_BRUSH
);
156 dc
->SetPen( *wxTRANSPARENT_PEN
);
157 dc
->DrawRectangle( rect
.Deflate(2) );
161 virtual bool Activate( wxRect
WXUNUSED(cell
),
162 wxDataViewModel
*WXUNUSED(model
),
163 const wxDataViewItem
&WXUNUSED(item
),
164 unsigned int WXUNUSED(col
) )
166 wxLogMessage( "MyCustomRenderer Activate()" );
170 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
171 wxDataViewModel
*WXUNUSED(model
),
172 const wxDataViewItem
&WXUNUSED(item
),
173 unsigned int WXUNUSED(col
) )
175 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
179 virtual wxSize
GetSize() const
181 //return wxSize(60,m_height);
182 return wxSize(60,20);
185 virtual bool SetValue( const wxVariant
&value
)
191 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
198 // ============================================================================
200 // ============================================================================
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
210 if ( !wxApp::OnInit() )
214 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
228 ID_CLEARLOG
= wxID_HIGHEST
+1,
232 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
241 ID_ABOUT
= wxID_ABOUT
,
249 ID_DELETE_MUSIC
= 101,
250 ID_DELETE_YEAR
= 102,
251 ID_SELECT_NINTH
= 103,
255 ID_PREPEND_LIST
= 200,
256 ID_DELETE_LIST
= 201,
261 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
262 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
263 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
264 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
265 EVT_MENU( ID_CLEARLOG
, MyFrame::OnClearLog
)
267 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
269 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
270 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
271 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
272 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
273 EVT_BUTTON( ID_COLLAPSE
, MyFrame::OnCollapse
)
274 EVT_BUTTON( ID_EXPAND
, MyFrame::OnExpand
)
276 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
277 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
278 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
279 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
281 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
283 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
284 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
285 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
286 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
287 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
288 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
290 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
291 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
293 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
294 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
295 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
297 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
299 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
300 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
301 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
303 EVT_RIGHT_UP(MyFrame::OnRightClick
)
306 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
307 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
317 SetIcon(wxICON(sample
));
323 wxMenu
*style_menu
= new wxMenu
;
324 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
325 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
326 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
327 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
328 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
330 wxMenu
*file_menu
= new wxMenu
;
331 file_menu
->Append(ID_CLEARLOG
, "Clear log");
332 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
333 file_menu
->AppendSeparator();
334 file_menu
->Append(ID_EXIT
, "E&xit");
336 wxMenu
*about_menu
= new wxMenu
;
337 about_menu
->Append(ID_ABOUT
, "&About");
339 wxMenuBar
*menu_bar
= new wxMenuBar
;
340 menu_bar
->Append(file_menu
, "&File");
341 menu_bar
->Append(about_menu
, "&About");
343 SetMenuBar(menu_bar
);
347 // first page of the notebook
348 // --------------------------
350 m_notebook
= new wxNotebook( this, wxID_ANY
);
352 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
354 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
356 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
357 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
358 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_MUSIC
,"Delete selected"), 0, wxALL
, 10 );
359 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), 0, wxALL
, 10 );
360 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), 0, wxALL
, 10 );
361 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), 0, wxALL
, 10 );
362 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), 0, wxALL
, 10 );
364 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
365 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
366 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
368 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
370 firstPanelSz
->Add(button_sizer
);
371 firstPanel
->SetSizerAndFit(firstPanelSz
);
374 // second page of the notebook
375 // ---------------------------
377 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
379 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
381 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
382 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
383 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
384 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
385 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
387 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
388 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
389 secondPanelSz
->Add(button_sizer2
);
390 secondPanel
->SetSizerAndFit(secondPanelSz
);
393 // third page of the notebook
394 // ---------------------------
396 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
398 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
400 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
401 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
402 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
405 // fourth page of the notebook
406 // ---------------------------
408 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
410 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
412 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
413 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
414 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
421 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
422 m_notebook
->AddPage(secondPanel
, "MyListModel");
423 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
424 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
426 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
428 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
429 wxDefaultSize
, wxTE_MULTILINE
);
430 m_log
->SetMinSize(wxSize(-1, 100));
431 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
432 wxLogMessage( "This is the log window" );
434 mainSizer
->Add( m_notebook
, 1, wxGROW
);
435 mainSizer
->Add( m_log
, 0, wxGROW
);
437 SetSizerAndFit(mainSizer
);
442 delete wxLog::SetActiveTarget(m_logOld
);
445 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
451 wxASSERT(!m_ctrl
[0] && !m_music_model
);
453 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
454 wxDefaultSize
, style
);
456 m_music_model
= new MyMusicTreeModel
;
457 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
459 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
460 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
462 // column 0 of the view control:
464 wxDataViewTextRenderer
*tr
=
465 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
466 wxDataViewColumn
*column0
=
467 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
468 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
469 m_ctrl
[0]->AppendColumn( column0
);
471 // Call this and sorting is enabled
472 // immediatly upon start up.
473 column0
->SetAsSortKey();
476 // column 1 of the view control:
478 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
479 wxDataViewColumn
*column1
=
480 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
481 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
482 wxDATAVIEW_COL_RESIZABLE
);
483 column1
->SetMinWidth(150); // this column can't be resized to be smaller
484 m_ctrl
[0]->AppendColumn( column1
);
486 // column 2 of the view control:
488 wxDataViewSpinRenderer
*sr
=
489 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
490 wxDataViewColumn
*column2
=
491 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
492 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
493 m_ctrl
[0]->AppendColumn( column2
);
495 // column 3 of the view control:
497 wxArrayString choices
;
498 choices
.Add( "good" );
499 choices
.Add( "bad" );
500 choices
.Add( "lousy" );
501 wxDataViewChoiceRenderer
*c
=
502 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
503 wxDataViewColumn
*column3
=
504 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
505 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
506 m_ctrl
[0]->AppendColumn( column3
);
508 // column 4 of the view control:
510 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
512 // column 5 of the view control:
514 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
515 wxDataViewColumn
*column5
=
516 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
517 wxDATAVIEW_COL_RESIZABLE
);
518 m_ctrl
[0]->AppendColumn( column5
);
521 // select initially the ninth symphony:
522 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
528 wxASSERT(!m_ctrl
[1] && !m_list_model
);
529 m_ctrl
[1] = new wxDataViewCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
530 wxDefaultSize
, style
);
532 m_list_model
= new MyListModel
;
533 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
535 // the various columns
537 m_ctrl
[1]->AppendTextColumn("editable string", 0, wxDATAVIEW_CELL_EDITABLE
, 120);
538 m_ctrl
[1]->AppendIconTextColumn(wxIcon(wx_small_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( "icon");
540 m_ctrl
[1]->AppendTextColumn("editable string", 0, wxDATAVIEW_CELL_EDITABLE
);
541 m_ctrl
[1]->AppendIconTextColumn("icon", 1, wxDATAVIEW_CELL_INERT
);
543 m_ctrl
[1]->AppendColumn(
544 new wxDataViewColumn("attributes", new wxDataViewTextRendererAttr
, 2 ));
550 wxASSERT(!m_ctrl
[2]);
551 wxDataViewListCtrl
* lc
=
552 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
553 wxDefaultSize
, style
);
556 lc
->AppendToggleColumn( "Toggle" );
557 lc
->AppendTextColumn( "Text" );
558 lc
->AppendProgressColumn( "Progress" );
560 wxVector
<wxVariant
> data
;
561 for (unsigned int i
=0; i
<10; i
++)
564 data
.push_back( (i%3
) == 0 );
565 data
.push_back( wxString::Format("row %d", i
) );
566 data
.push_back( long(5*i
) );
568 lc
->AppendItem( data
);
575 wxASSERT(!m_ctrl
[3]);
576 wxDataViewTreeCtrl
* tc
=
577 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
578 wxDefaultSize
, style
);
581 wxImageList
*ilist
= new wxImageList( 16, 16 );
582 ilist
->Add( wxIcon(wx_small_xpm
) );
583 tc
->SetImageList( ilist
);
585 wxDataViewItem parent
=
586 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
587 tc
->AppendItem( parent
, "Child 1", 0 );
588 tc
->AppendItem( parent
, "Child 2", 0 );
589 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
591 wxDataViewItem cont
=
592 tc
->AppendContainer( parent
, "Container child", 0 );
593 tc
->AppendItem( cont
, "Child 4", 0 );
594 tc
->AppendItem( cont
, "Child 5", 0 );
603 // ----------------------------------------------------------------------------
604 // MyFrame - generic event handlers
605 // ----------------------------------------------------------------------------
607 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
612 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
614 unsigned int nPanel
= m_notebook
->GetSelection();
616 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
617 wxString::Format("Style of panel #%d", nPanel
+1));
619 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
621 unsigned long style
= 0;
628 style
= wxDV_MULTIPLE
;
631 style
= wxDV_ROW_LINES
;
634 style
= wxDV_HORIZ_RULES
;
637 style
= wxDV_VERT_RULES
;
643 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
647 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
649 unsigned int nPanel
= m_notebook
->GetSelection();
652 unsigned long style
= 0;
653 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
654 style |= wxDV_SINGLE;*/
655 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
656 style
|= wxDV_MULTIPLE
;
657 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
658 style
|= wxDV_ROW_LINES
;
659 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
660 style
|= wxDV_HORIZ_RULES
;
661 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
662 style
|= wxDV_VERT_RULES
;
664 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
667 sz
->Detach(m_ctrl
[nPanel
]);
668 wxDELETE(m_ctrl
[nPanel
]);
669 m_ctrl
[nPanel
] = NULL
;
672 m_music_model
.reset(NULL
);
673 else if (nPanel
== 1)
674 m_list_model
.reset(NULL
);
676 // rebuild the DVC for the selected panel:
677 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
679 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
683 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
688 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
690 wxAboutDialogInfo info
;
691 info
.SetName(_("DataView sample"));
692 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
693 info
.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
694 info
.AddDeveloper("Robert Roebling");
695 info
.AddDeveloper("Francesco Montorsi");
701 // ----------------------------------------------------------------------------
702 // MyFrame - event handlers for the first page
703 // ----------------------------------------------------------------------------
705 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
707 wxDataViewItem
item( event
.GetItem() );
709 // only allow drags for item, not containers
710 if (m_music_model
->IsContainer( item
) )
716 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
717 wxTextDataObject
*obj
= new wxTextDataObject
;
718 obj
->SetText( node
->m_title
);
719 event
.SetDataObject( obj
);
722 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
724 wxDataViewItem
item( event
.GetItem() );
726 // only allow drags for item, not containers
727 if (m_music_model
->IsContainer( item
) )
730 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
734 void MyFrame::OnDrop( wxDataViewEvent
&event
)
736 wxDataViewItem
item( event
.GetItem() );
738 // only allow drops for item, not containers
739 if (m_music_model
->IsContainer( item
) )
745 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
751 wxTextDataObject obj
;
752 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
754 wxLogMessage( "Text dropped: %s", obj
.GetText() );
757 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
759 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
762 void MyFrame::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
764 wxDataViewItemArray items
;
765 int len
= m_ctrl
[0]->GetSelections( items
);
766 for( int i
= 0; i
< len
; i
++ )
768 m_music_model
->Delete( items
[i
] );
771 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
773 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
774 FindWindow( ID_DELETE_YEAR
)->Disable();
777 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
779 if (!m_music_model
->GetNinthItem().IsOk())
781 wxLogError( "Cannot select the ninth symphony: it was removed!" );
785 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
788 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
790 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
792 m_ctrl
[0]->Collapse( item
);
795 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
797 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
799 m_ctrl
[0]->Expand( item
);
802 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
807 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d",
808 event
.GetItem().GetID(), event
.GetColumn() );
811 void MyFrame::OnActivated( wxDataViewEvent
&event
)
816 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
817 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
819 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
820 wxLogMessage( "Item: %s is expanded", title
);
823 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
828 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
832 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
835 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
840 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
841 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
845 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
850 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
851 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
854 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
859 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
860 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
863 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
868 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
869 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
872 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
877 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
878 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
881 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
886 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
887 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
890 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
895 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
896 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
899 menu
.Append( 1, "menuitem 1" );
900 menu
.Append( 2, "menuitem 2" );
901 menu
.Append( 3, "menuitem 3" );
903 m_ctrl
[0]->PopupMenu(&menu
);
906 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
908 // we need to skip the event to let the default behaviour of sorting by
909 // this column when it is clicked to take place
915 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
917 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
920 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
925 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
927 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
930 void MyFrame::OnSorted( wxDataViewEvent
&event
)
935 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
937 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
940 void MyFrame::OnRightClick( wxMouseEvent
&event
)
945 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
946 event
.GetX(), event
.GetY() );
950 // ----------------------------------------------------------------------------
951 // MyFrame - event handlers for the second page
952 // ----------------------------------------------------------------------------
954 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
956 m_list_model
->Prepend("Test");
959 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
961 wxDataViewItemArray items
;
962 int len
= m_ctrl
[1]->GetSelections( items
);
964 m_list_model
->DeleteItems( items
);
967 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
969 wxDataViewItem item
= m_list_model
->GetItem( 50 );
970 m_ctrl
[1]->EnsureVisible(item
,m_col
);
973 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
975 m_list_model
->AddMany();