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 OnPageChanged(wxBookCtrlEvent
& event
);
81 void OnAddMozart(wxCommandEvent
& event
);
82 void OnDeleteMusic(wxCommandEvent
& event
);
83 void OnDeleteYear(wxCommandEvent
& event
);
84 void OnSelectNinth(wxCommandEvent
& event
);
86 void OnPrependList(wxCommandEvent
& event
);
87 void OnDeleteList(wxCommandEvent
& event
);
89 void OnValueChanged( wxDataViewEvent
&event
);
91 void OnActivated( wxDataViewEvent
&event
);
92 void OnExpanding( wxDataViewEvent
&event
);
93 void OnExpanded( wxDataViewEvent
&event
);
94 void OnCollapsing( wxDataViewEvent
&event
);
95 void OnCollapsed( wxDataViewEvent
&event
);
96 void OnSelectionChanged( wxDataViewEvent
&event
);
98 void OnEditingStarted( wxDataViewEvent
&event
);
99 void OnEditingDone( wxDataViewEvent
&event
);
101 void OnHeaderClick( wxDataViewEvent
&event
);
102 void OnHeaderRightClick( wxDataViewEvent
&event
);
103 void OnSorted( wxDataViewEvent
&event
);
105 void OnContextMenu( wxDataViewEvent
&event
);
107 void OnRightClick( wxMouseEvent
&event
);
108 void OnGoto( wxCommandEvent
&event
);
109 void OnAddMany( wxCommandEvent
&event
);
111 void OnBeginDrag( wxDataViewEvent
&event
);
112 void OnDropPossible( wxDataViewEvent
&event
);
113 void OnDrop( wxDataViewEvent
&event
);
116 wxNotebook
* m_notebook
;
118 // the controls stored in the various tabs of the main notebook:
120 wxDataViewCtrl
* m_ctrl
[4];
122 // the models associated with the first two DVC:
124 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
125 wxObjectDataPtr
<MyListModel
> m_list_model
;
129 wxDataViewColumn
* m_col
;
135 DECLARE_EVENT_TABLE()
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 class MyCustomRenderer
: public wxDataViewCustomRenderer
146 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
147 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
150 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
152 dc
->SetBrush( *wxRED_BRUSH
);
153 dc
->SetPen( *wxTRANSPARENT_PEN
);
154 dc
->DrawRectangle( rect
.Deflate(2) );
158 virtual bool Activate( wxRect
WXUNUSED(cell
),
159 wxDataViewModel
*WXUNUSED(model
),
160 const wxDataViewItem
&WXUNUSED(item
),
161 unsigned int WXUNUSED(col
) )
163 wxLogMessage( "MyCustomRenderer Activate()" );
167 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
168 wxDataViewModel
*WXUNUSED(model
),
169 const wxDataViewItem
&WXUNUSED(item
),
170 unsigned int WXUNUSED(col
) )
172 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
176 virtual wxSize
GetSize() const
178 //return wxSize(60,m_height);
179 return wxSize(60,20);
182 virtual bool SetValue( const wxVariant
&value
)
188 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
195 // ============================================================================
197 // ============================================================================
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
207 if ( !wxApp::OnInit() )
211 new MyFrame(NULL
, wxT("wxDataViewCtrl sample"), 40, 40, 1000, 540);
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
225 ID_STYLE_MENU
= wxID_HIGHEST
+1,
228 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
237 ID_ABOUT
= wxID_ABOUT
,
245 ID_DELETE_MUSIC
= 101,
246 ID_DELETE_YEAR
= 102,
247 ID_SELECT_NINTH
= 103,
249 ID_PREPEND_LIST
= 200,
250 ID_DELETE_LIST
= 201,
255 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
256 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
257 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
258 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
260 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
262 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
263 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
264 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
265 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
266 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
267 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
268 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
269 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
271 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
273 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
274 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
275 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
276 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
277 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
278 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
280 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
281 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
283 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
284 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
285 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
287 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
289 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
290 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
291 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
293 EVT_RIGHT_UP(MyFrame::OnRightClick
)
296 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
297 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
307 SetIcon(wxICON(sample
));
313 wxMenu
*style_menu
= new wxMenu
;
314 //style_menu->AppendCheckItem(ID_SINGLE, wxT("Single selection"));
315 style_menu
->AppendCheckItem(ID_MULTIPLE
, wxT("Multiple selection"));
316 style_menu
->AppendCheckItem(ID_ROW_LINES
, wxT("Alternating colours"));
317 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, wxT("Display horizontal rules"));
318 style_menu
->AppendCheckItem(ID_VERT_RULES
, wxT("Display vertical rules"));
320 wxMenu
*file_menu
= new wxMenu
;
321 file_menu
->Append(ID_STYLE_MENU
, wxT("&Style"), style_menu
);
322 file_menu
->AppendSeparator();
323 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
325 wxMenu
*about_menu
= new wxMenu
;
326 about_menu
->Append(ID_ABOUT
, wxT("&About"));
328 wxMenuBar
*menu_bar
= new wxMenuBar
;
329 menu_bar
->Append(file_menu
, wxT("&File"));
330 menu_bar
->Append(about_menu
, wxT("&About"));
332 SetMenuBar(menu_bar
);
336 // first page of the notebook
337 // --------------------------
339 m_notebook
= new wxNotebook( this, wxID_ANY
);
341 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
343 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
345 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
346 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
347 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
348 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
349 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,_("Select Ninth")), 0, wxALL
, 10 );
351 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
352 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
353 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
355 new wxStaticText(firstPanel
, wxID_ANY
, wxT("Most of the cells above are editable!")),
357 firstPanelSz
->Add(button_sizer
);
358 firstPanel
->SetSizerAndFit(firstPanelSz
);
361 // second page of the notebook
362 // ---------------------------
364 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
366 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
368 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
369 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 10 );
370 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 10 );
371 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, _("Goto 50")), 0, wxALL
, 10 );
372 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, _("Add 1000")), 0, wxALL
, 10 );
374 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
375 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
376 secondPanelSz
->Add(button_sizer2
);
377 secondPanel
->SetSizerAndFit(secondPanelSz
);
380 // third page of the notebook
381 // ---------------------------
383 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
385 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
387 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
388 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
389 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
392 // fourth page of the notebook
393 // ---------------------------
395 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
397 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
399 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
400 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
401 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
408 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
409 m_notebook
->AddPage(secondPanel
, "MyListModel");
410 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
411 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
413 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
415 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
416 wxDefaultSize
, wxTE_MULTILINE
);
417 m_log
->SetMinSize(wxSize(-1, 100));
418 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
419 wxLogMessage(_("This is the log window"));
421 mainSizer
->Add( m_notebook
, 1, wxGROW
);
422 mainSizer
->Add( m_log
, 0, wxGROW
);
424 SetSizerAndFit(mainSizer
);
429 delete wxLog::SetActiveTarget(m_logOld
);
432 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
438 wxASSERT(!m_ctrl
[0] && !m_music_model
);
440 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
441 wxDefaultSize
, style
);
443 m_music_model
= new MyMusicTreeModel
;
444 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
446 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
447 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
449 // column 0 of the view control:
451 wxDataViewTextRenderer
*tr
=
452 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
453 wxDataViewColumn
*column0
=
454 new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
455 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
456 m_ctrl
[0]->AppendColumn( column0
);
458 // Call this and sorting is enabled
459 // immediatly upon start up.
460 column0
->SetAsSortKey();
463 // column 1 of the view control:
465 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
466 wxDataViewColumn
*column1
=
467 new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_LEFT
,
468 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
469 wxDATAVIEW_COL_RESIZABLE
);
470 column1
->SetMinWidth(150); // this column can't be resized to be smaller
471 m_ctrl
[0]->AppendColumn( column1
);
473 // column 2 of the view control:
475 wxDataViewSpinRenderer
*sr
=
476 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
477 wxDataViewColumn
*column2
=
478 new wxDataViewColumn( wxT("year"), sr
, 2, 60, wxALIGN_LEFT
,
479 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
480 m_ctrl
[0]->AppendColumn( column2
);
482 // column 3 of the view control:
484 wxArrayString choices
;
485 choices
.Add( "good" );
486 choices
.Add( "bad" );
487 choices
.Add( "lousy" );
488 wxDataViewChoiceRenderer
*c
=
489 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
490 wxDataViewColumn
*column3
=
491 new wxDataViewColumn( wxT("rating"), c
, 3, 100, wxALIGN_LEFT
,
492 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
493 m_ctrl
[0]->AppendColumn( column3
);
495 // column 4 of the view control:
497 m_ctrl
[0]->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT
, 80 );
499 // column 5 of the view control:
501 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
502 wxDataViewColumn
*column5
=
503 new wxDataViewColumn( wxT("custom"), cr
, 5, -1, wxALIGN_LEFT
,
504 wxDATAVIEW_COL_RESIZABLE
);
505 m_ctrl
[0]->AppendColumn( column5
);
508 // select initially the ninth symphony:
509 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
515 wxASSERT(!m_ctrl
[1] && !m_list_model
);
516 m_ctrl
[1] = new wxDataViewCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
517 wxDefaultSize
, style
);
519 m_list_model
= new MyListModel
;
520 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
522 // the various columns
524 m_ctrl
[1]->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
525 m_ctrl
[1]->AppendIconTextColumn(wxIcon(wx_small_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
527 m_ctrl
[1]->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
528 m_ctrl
[1]->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
530 m_ctrl
[1]->AppendColumn(
531 new wxDataViewColumn(wxT("attributes"), new wxDataViewTextRendererAttr
, 2 ));
537 wxASSERT(!m_ctrl
[2]);
538 wxDataViewListCtrl
* lc
=
539 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
540 wxDefaultSize
, style
);
543 lc
->AppendToggleColumn( wxT("Toggle") );
544 lc
->AppendTextColumn( wxT("Text") );
545 lc
->AppendProgressColumn( wxT("Progress") );
547 wxVector
<wxVariant
> data
;
548 for (unsigned int i
=0; i
<10; i
++)
551 data
.push_back( (i%3
) == 0 );
552 data
.push_back( wxString::Format("row %d", i
) );
553 data
.push_back( long(5*i
) );
555 lc
->AppendItem( data
);
562 wxASSERT(!m_ctrl
[3]);
563 wxDataViewTreeCtrl
* tc
=
564 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
565 wxDefaultSize
, style
);
568 wxImageList
*ilist
= new wxImageList( 16, 16 );
569 ilist
->Add( wxIcon(wx_small_xpm
) );
570 tc
->SetImageList( ilist
);
572 wxDataViewItem parent
=
573 tc
->AppendContainer( wxDataViewItem(0), wxT("The Root"), 0 );
574 tc
->AppendItem( parent
, wxT("Child 1"), 0 );
575 tc
->AppendItem( parent
, wxT("Child 2"), 0 );
576 tc
->AppendItem( parent
, wxT("Child 3, very long, long, long, long"), 0 );
578 wxDataViewItem cont
=
579 tc
->AppendContainer( parent
, wxT("Container child"), 0 );
580 tc
->AppendItem( cont
, wxT("Child 4"), 0 );
581 tc
->AppendItem( cont
, wxT("Child 5"), 0 );
590 // ----------------------------------------------------------------------------
591 // MyFrame - generic event handlers
592 // ----------------------------------------------------------------------------
594 void MyFrame::OnPageChanged(wxBookCtrlEvent
& WXUNUSED(event
) )
596 unsigned int nPanel
= m_notebook
->GetSelection();
598 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
599 wxString::Format("Style of panel #%d", nPanel
+1));
601 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
603 unsigned long style
= 0;
610 style
= wxDV_MULTIPLE
;
613 style
= wxDV_ROW_LINES
;
616 style
= wxDV_HORIZ_RULES
;
619 style
= wxDV_VERT_RULES
;
625 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
629 void MyFrame::OnStyleChange(wxCommandEvent
& WXUNUSED(event
) )
631 unsigned int nPanel
= m_notebook
->GetSelection();
634 unsigned long style
= 0;
635 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
636 style |= wxDV_SINGLE;*/
637 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
638 style
|= wxDV_MULTIPLE
;
639 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
640 style
|= wxDV_ROW_LINES
;
641 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
642 style
|= wxDV_HORIZ_RULES
;
643 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
644 style
|= wxDV_VERT_RULES
;
646 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
649 sz
->Detach(m_ctrl
[nPanel
]);
650 wxDELETE(m_ctrl
[nPanel
]);
651 m_ctrl
[nPanel
] = NULL
;
654 m_music_model
.reset(NULL
);
655 else if (nPanel
== 1)
656 m_list_model
.reset(NULL
);
658 // rebuild the DVC for the selected panel:
659 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
661 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
665 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
670 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
672 wxAboutDialogInfo info
;
673 info
.SetName(_("DataView sample"));
674 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
675 info
.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
676 info
.AddDeveloper("Robert Roebling");
677 info
.AddDeveloper("Francesco Montorsi");
683 // ----------------------------------------------------------------------------
684 // MyFrame - event handlers for the first page
685 // ----------------------------------------------------------------------------
687 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
689 wxDataViewItem
item( event
.GetItem() );
691 // only allow drags for item, not containers
692 if (m_music_model
->IsContainer( item
) )
698 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
699 wxTextDataObject
*obj
= new wxTextDataObject
;
700 obj
->SetText( node
->m_title
);
701 event
.SetDataObject( obj
);
704 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
706 wxDataViewItem
item( event
.GetItem() );
708 // only allow drags for item, not containers
709 if (m_music_model
->IsContainer( item
) )
712 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
716 void MyFrame::OnDrop( wxDataViewEvent
&event
)
718 wxDataViewItem
item( event
.GetItem() );
720 // only allow drops for item, not containers
721 if (m_music_model
->IsContainer( item
) )
727 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
733 wxTextDataObject obj
;
734 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
736 wxLogMessage(wxT("Text dropped: %s"), obj
.GetText() );
739 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
741 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
744 void MyFrame::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
746 wxDataViewItemArray items
;
747 int len
= m_ctrl
[0]->GetSelections( items
);
748 for( int i
= 0; i
< len
; i
++ )
750 m_music_model
->Delete( items
[i
] );
753 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
755 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
756 FindWindow( ID_DELETE_YEAR
)->Disable();
759 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
761 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
764 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
769 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"),
770 event
.GetItem().GetID(), event
.GetColumn() );
773 void MyFrame::OnActivated( wxDataViewEvent
&event
)
778 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
779 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
);
781 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
782 wxLogMessage(wxT("Item: %s is expanded"), title
);
785 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
790 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
794 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
);
797 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
802 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
803 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
);
807 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
812 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
813 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
);
816 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
821 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
822 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
);
825 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
830 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
831 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
);
834 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
839 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
840 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
);
843 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
848 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
849 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
);
852 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
857 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
858 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
);
861 menu
.Append( 1, wxT("entry 1") );
862 menu
.Append( 2, wxT("entry 2") );
863 menu
.Append( 3, wxT("entry 3") );
865 m_ctrl
[0]->PopupMenu(&menu
);
867 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),
868 title
.GetData(), event
.GetValue().GetString());
871 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
873 // we need to skip the event to let the default behaviour of sorting by
874 // this column when it is clicked to take place
880 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
882 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
885 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
890 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
892 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
895 void MyFrame::OnSorted( wxDataViewEvent
&event
)
900 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
902 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
905 void MyFrame::OnRightClick( wxMouseEvent
&event
)
910 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"),
911 event
.GetX(), event
.GetY());
915 // ----------------------------------------------------------------------------
916 // MyFrame - event handlers for the second page
917 // ----------------------------------------------------------------------------
919 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
921 m_list_model
->Prepend(wxT("Test"));
924 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
926 wxDataViewItemArray items
;
927 int len
= m_ctrl
[1]->GetSelections( items
);
929 m_list_model
->DeleteItems( items
);
932 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
934 wxDataViewItem item
= m_list_model
->GetItem( 50 );
935 m_ctrl
[1]->EnsureVisible(item
,m_col
);
938 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
940 m_list_model
->AddMany();