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
);
85 void OnCollapse(wxCommandEvent
& event
);
86 void OnExpand(wxCommandEvent
& event
);
88 void OnPrependList(wxCommandEvent
& event
);
89 void OnDeleteList(wxCommandEvent
& event
);
91 void OnValueChanged( wxDataViewEvent
&event
);
93 void OnActivated( wxDataViewEvent
&event
);
94 void OnExpanding( wxDataViewEvent
&event
);
95 void OnExpanded( wxDataViewEvent
&event
);
96 void OnCollapsing( wxDataViewEvent
&event
);
97 void OnCollapsed( wxDataViewEvent
&event
);
98 void OnSelectionChanged( wxDataViewEvent
&event
);
100 void OnEditingStarted( wxDataViewEvent
&event
);
101 void OnEditingDone( wxDataViewEvent
&event
);
103 void OnHeaderClick( wxDataViewEvent
&event
);
104 void OnHeaderRightClick( wxDataViewEvent
&event
);
105 void OnSorted( wxDataViewEvent
&event
);
107 void OnContextMenu( wxDataViewEvent
&event
);
109 void OnRightClick( wxMouseEvent
&event
);
110 void OnGoto( wxCommandEvent
&event
);
111 void OnAddMany( wxCommandEvent
&event
);
113 void OnBeginDrag( wxDataViewEvent
&event
);
114 void OnDropPossible( wxDataViewEvent
&event
);
115 void OnDrop( wxDataViewEvent
&event
);
118 wxNotebook
* m_notebook
;
120 // the controls stored in the various tabs of the main notebook:
122 wxDataViewCtrl
* m_ctrl
[4];
124 // the models associated with the first two DVC:
126 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
127 wxObjectDataPtr
<MyListModel
> m_list_model
;
131 wxDataViewColumn
* m_col
;
137 DECLARE_EVENT_TABLE()
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 class MyCustomRenderer
: public wxDataViewCustomRenderer
148 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
149 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
152 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
154 dc
->SetBrush( *wxRED_BRUSH
);
155 dc
->SetPen( *wxTRANSPARENT_PEN
);
156 dc
->DrawRectangle( rect
.Deflate(2) );
160 virtual bool Activate( wxRect
WXUNUSED(cell
),
161 wxDataViewModel
*WXUNUSED(model
),
162 const wxDataViewItem
&WXUNUSED(item
),
163 unsigned int WXUNUSED(col
) )
165 wxLogMessage( "MyCustomRenderer Activate()" );
169 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
170 wxDataViewModel
*WXUNUSED(model
),
171 const wxDataViewItem
&WXUNUSED(item
),
172 unsigned int WXUNUSED(col
) )
174 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
178 virtual wxSize
GetSize() const
180 //return wxSize(60,m_height);
181 return wxSize(60,20);
184 virtual bool SetValue( const wxVariant
&value
)
190 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
197 // ============================================================================
199 // ============================================================================
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
209 if ( !wxApp
::OnInit() )
213 new MyFrame(NULL
, wxT("wxDataViewCtrl sample"), 40, 40, 1000, 540);
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
227 ID_STYLE_MENU
= wxID_HIGHEST
+1,
230 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
239 ID_ABOUT
= wxID_ABOUT
,
247 ID_DELETE_MUSIC
= 101,
248 ID_DELETE_YEAR
= 102,
249 ID_SELECT_NINTH
= 103,
253 ID_PREPEND_LIST
= 200,
254 ID_DELETE_LIST
= 201,
259 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
260 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame
::OnStyleChange
)
261 EVT_MENU( ID_EXIT
, MyFrame
::OnQuit
)
262 EVT_MENU( ID_ABOUT
, MyFrame
::OnAbout
)
264 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame
::OnPageChanged
)
266 EVT_BUTTON( ID_ADD_MOZART
, MyFrame
::OnAddMozart
)
267 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame
::OnDeleteMusic
)
268 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame
::OnDeleteYear
)
269 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame
::OnSelectNinth
)
270 EVT_BUTTON( ID_COLLAPSE
, MyFrame
::OnCollapse
)
271 EVT_BUTTON( ID_EXPAND
, MyFrame
::OnExpand
)
273 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame
::OnPrependList
)
274 EVT_BUTTON( ID_DELETE_LIST
, MyFrame
::OnDeleteList
)
275 EVT_BUTTON( ID_GOTO
, MyFrame
::OnGoto
)
276 EVT_BUTTON( ID_ADD_MANY
, MyFrame
::OnAddMany
)
278 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame
::OnValueChanged
)
280 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame
::OnActivated
)
281 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame
::OnExpanding
)
282 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame
::OnExpanded
)
283 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame
::OnCollapsing
)
284 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame
::OnCollapsed
)
285 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame
::OnSelectionChanged
)
287 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame
::OnEditingStarted
)
288 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame
::OnEditingDone
)
290 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame
::OnHeaderClick
)
291 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame
::OnHeaderRightClick
)
292 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame
::OnSorted
)
294 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame
::OnContextMenu
)
296 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame
::OnBeginDrag
)
297 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame
::OnDropPossible
)
298 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame
::OnDrop
)
300 EVT_RIGHT_UP(MyFrame
::OnRightClick
)
303 MyFrame
::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
304 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
314 SetIcon(wxICON(sample
));
320 wxMenu
*style_menu
= new wxMenu
;
321 //style_menu->AppendCheckItem(ID_SINGLE, wxT("Single selection"));
322 style_menu
->AppendCheckItem(ID_MULTIPLE
, wxT("Multiple selection"));
323 style_menu
->AppendCheckItem(ID_ROW_LINES
, wxT("Alternating colours"));
324 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, wxT("Display horizontal rules"));
325 style_menu
->AppendCheckItem(ID_VERT_RULES
, wxT("Display vertical rules"));
327 wxMenu
*file_menu
= new wxMenu
;
328 file_menu
->Append(ID_STYLE_MENU
, wxT("&Style"), style_menu
);
329 file_menu
->AppendSeparator();
330 file_menu
->Append(ID_EXIT
, wxT("E&xit"));
332 wxMenu
*about_menu
= new wxMenu
;
333 about_menu
->Append(ID_ABOUT
, wxT("&About"));
335 wxMenuBar
*menu_bar
= new wxMenuBar
;
336 menu_bar
->Append(file_menu
, wxT("&File"));
337 menu_bar
->Append(about_menu
, wxT("&About"));
339 SetMenuBar(menu_bar
);
343 // first page of the notebook
344 // --------------------------
346 m_notebook
= new wxNotebook( this, wxID_ANY
);
348 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
350 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
352 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
353 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, _("Add Mozart")), 0, wxALL
, 10 );
354 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_MUSIC
,_("Delete selected")), 0, wxALL
, 10 );
355 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, _("Delete \"Year\" column")), 0, wxALL
, 10 );
356 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,_("Select Ninth")), 0, wxALL
, 10 );
357 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, _("Collapse")), 0, wxALL
, 10 );
358 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, _("Expand")), 0, wxALL
, 10 );
360 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
361 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
362 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
364 new wxStaticText(firstPanel
, wxID_ANY
, wxT("Most of the cells above are editable!")),
366 firstPanelSz
->Add(button_sizer
);
367 firstPanel
->SetSizerAndFit(firstPanelSz
);
370 // second page of the notebook
371 // ---------------------------
373 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
375 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
377 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
378 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,_("Prepend")), 0, wxALL
, 10 );
379 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, _("Delete selected")), 0, wxALL
, 10 );
380 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, _("Goto 50")), 0, wxALL
, 10 );
381 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, _("Add 1000")), 0, wxALL
, 10 );
383 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
384 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
385 secondPanelSz
->Add(button_sizer2
);
386 secondPanel
->SetSizerAndFit(secondPanelSz
);
389 // third page of the notebook
390 // ---------------------------
392 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
394 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
396 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
397 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
398 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
401 // fourth page of the notebook
402 // ---------------------------
404 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
406 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
408 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
409 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
410 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
417 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
418 m_notebook
->AddPage(secondPanel
, "MyListModel");
419 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
420 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
422 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
424 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
425 wxDefaultSize
, wxTE_MULTILINE
);
426 m_log
->SetMinSize(wxSize(-1, 100));
427 m_logOld
= wxLog
::SetActiveTarget(new wxLogTextCtrl(m_log
));
428 wxLogMessage(_("This is the log window"));
430 mainSizer
->Add( m_notebook
, 1, wxGROW
);
431 mainSizer
->Add( m_log
, 0, wxGROW
);
433 SetSizerAndFit(mainSizer
);
438 delete wxLog
::SetActiveTarget(m_logOld
);
441 void MyFrame
::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
447 wxASSERT(!m_ctrl
[0] && !m_music_model
);
449 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
450 wxDefaultSize
, style
);
452 m_music_model
= new MyMusicTreeModel
;
453 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
455 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
456 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
458 // column 0 of the view control:
460 wxDataViewTextRenderer
*tr
=
461 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT
);
462 wxDataViewColumn
*column0
=
463 new wxDataViewColumn( wxT("title"), tr
, 0, 200, wxALIGN_LEFT
,
464 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
465 m_ctrl
[0]->AppendColumn( column0
);
467 // Call this and sorting is enabled
468 // immediatly upon start up.
469 column0
->SetAsSortKey();
472 // column 1 of the view control:
474 tr
= new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
475 wxDataViewColumn
*column1
=
476 new wxDataViewColumn( wxT("artist"), tr
, 1, 150, wxALIGN_LEFT
,
477 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
478 wxDATAVIEW_COL_RESIZABLE
);
479 column1
->SetMinWidth(150); // this column can't be resized to be smaller
480 m_ctrl
[0]->AppendColumn( column1
);
482 // column 2 of the view control:
484 wxDataViewSpinRenderer
*sr
=
485 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
486 wxDataViewColumn
*column2
=
487 new wxDataViewColumn( wxT("year"), sr
, 2, 60, wxALIGN_LEFT
,
488 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
489 m_ctrl
[0]->AppendColumn( column2
);
491 // column 3 of the view control:
493 wxArrayString choices
;
494 choices
.Add( "good" );
495 choices
.Add( "bad" );
496 choices
.Add( "lousy" );
497 wxDataViewChoiceRenderer
*c
=
498 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
499 wxDataViewColumn
*column3
=
500 new wxDataViewColumn( wxT("rating"), c
, 3, 100, wxALIGN_LEFT
,
501 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
502 m_ctrl
[0]->AppendColumn( column3
);
504 // column 4 of the view control:
506 m_ctrl
[0]->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT
, 80 );
508 // column 5 of the view control:
510 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
511 wxDataViewColumn
*column5
=
512 new wxDataViewColumn( wxT("custom"), cr
, 5, -1, wxALIGN_LEFT
,
513 wxDATAVIEW_COL_RESIZABLE
);
514 m_ctrl
[0]->AppendColumn( column5
);
517 // select initially the ninth symphony:
518 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
524 wxASSERT(!m_ctrl
[1] && !m_list_model
);
525 m_ctrl
[1] = new wxDataViewCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
526 wxDefaultSize
, style
);
528 m_list_model
= new MyListModel
;
529 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
531 // the various columns
533 m_ctrl
[1]->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
534 m_ctrl
[1]->AppendIconTextColumn(wxIcon(wx_small_xpm
), 1, wxDATAVIEW_CELL_INERT
)->SetTitle( wxT("icon") );
536 m_ctrl
[1]->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE
);
537 m_ctrl
[1]->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT
);
539 m_ctrl
[1]->AppendColumn(
540 new wxDataViewColumn(wxT("attributes"), new wxDataViewTextRendererAttr
, 2 ));
546 wxASSERT(!m_ctrl
[2]);
547 wxDataViewListCtrl
* lc
=
548 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
549 wxDefaultSize
, style
);
552 lc
->AppendToggleColumn( wxT("Toggle") );
553 lc
->AppendTextColumn( wxT("Text") );
554 lc
->AppendProgressColumn( wxT("Progress") );
556 wxVector
<wxVariant
> data
;
557 for (unsigned int i
=0; i
<10; i
++)
560 data
.push_back( (i
%3) == 0 );
561 data
.push_back( wxString
::Format("row %d", i
) );
562 data
.push_back( long(5*i
) );
564 lc
->AppendItem( data
);
571 wxASSERT(!m_ctrl
[3]);
572 wxDataViewTreeCtrl
* tc
=
573 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
574 wxDefaultSize
, style
);
577 wxImageList
*ilist
= new wxImageList( 16, 16 );
578 ilist
->Add( wxIcon(wx_small_xpm
) );
579 tc
->SetImageList( ilist
);
581 wxDataViewItem parent
=
582 tc
->AppendContainer( wxDataViewItem(0), wxT("The Root"), 0 );
583 tc
->AppendItem( parent
, wxT("Child 1"), 0 );
584 tc
->AppendItem( parent
, wxT("Child 2"), 0 );
585 tc
->AppendItem( parent
, wxT("Child 3, very long, long, long, long"), 0 );
587 wxDataViewItem cont
=
588 tc
->AppendContainer( parent
, wxT("Container child"), 0 );
589 tc
->AppendItem( cont
, wxT("Child 4"), 0 );
590 tc
->AppendItem( cont
, wxT("Child 5"), 0 );
599 // ----------------------------------------------------------------------------
600 // MyFrame - generic event handlers
601 // ----------------------------------------------------------------------------
603 void MyFrame
::OnPageChanged(wxBookCtrlEvent
& WXUNUSED(event
) )
605 unsigned int nPanel
= m_notebook
->GetSelection();
607 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
608 wxString
::Format("Style of panel #%d", nPanel
+1));
610 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
612 unsigned long style
= 0;
619 style
= wxDV_MULTIPLE
;
622 style
= wxDV_ROW_LINES
;
625 style
= wxDV_HORIZ_RULES
;
628 style
= wxDV_VERT_RULES
;
634 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
638 void MyFrame
::OnStyleChange(wxCommandEvent
& WXUNUSED(event
) )
640 unsigned int nPanel
= m_notebook
->GetSelection();
643 unsigned long style
= 0;
644 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
645 style |= wxDV_SINGLE;*/
646 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
647 style
|= wxDV_MULTIPLE
;
648 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
649 style
|= wxDV_ROW_LINES
;
650 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
651 style
|= wxDV_HORIZ_RULES
;
652 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
653 style
|= wxDV_VERT_RULES
;
655 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
658 sz
->Detach(m_ctrl
[nPanel
]);
659 wxDELETE(m_ctrl
[nPanel
]);
660 m_ctrl
[nPanel
] = NULL
;
663 m_music_model
.reset(NULL
);
664 else if (nPanel
== 1)
665 m_list_model
.reset(NULL
);
667 // rebuild the DVC for the selected panel:
668 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
670 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
674 void MyFrame
::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
679 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
681 wxAboutDialogInfo info
;
682 info
.SetName(_("DataView sample"));
683 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
684 info
.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
685 info
.AddDeveloper("Robert Roebling");
686 info
.AddDeveloper("Francesco Montorsi");
692 // ----------------------------------------------------------------------------
693 // MyFrame - event handlers for the first page
694 // ----------------------------------------------------------------------------
696 void MyFrame
::OnBeginDrag( wxDataViewEvent
&event
)
698 wxDataViewItem
item( event
.GetItem() );
700 // only allow drags for item, not containers
701 if (m_music_model
->IsContainer( item
) )
707 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
708 wxTextDataObject
*obj
= new wxTextDataObject
;
709 obj
->SetText( node
->m_title
);
710 event
.SetDataObject( obj
);
713 void MyFrame
::OnDropPossible( wxDataViewEvent
&event
)
715 wxDataViewItem
item( event
.GetItem() );
717 // only allow drags for item, not containers
718 if (m_music_model
->IsContainer( item
) )
721 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
725 void MyFrame
::OnDrop( wxDataViewEvent
&event
)
727 wxDataViewItem
item( event
.GetItem() );
729 // only allow drops for item, not containers
730 if (m_music_model
->IsContainer( item
) )
736 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
742 wxTextDataObject obj
;
743 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
745 wxLogMessage(wxT("Text dropped: %s"), obj
.GetText() );
748 void MyFrame
::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
750 m_music_model
->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
753 void MyFrame
::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
755 wxDataViewItemArray items
;
756 int len
= m_ctrl
[0]->GetSelections( items
);
757 for( int i
= 0; i
< len
; i
++ )
759 m_music_model
->Delete( items
[i
] );
762 void MyFrame
::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
764 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
765 FindWindow( ID_DELETE_YEAR
)->Disable();
768 void MyFrame
::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
770 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
773 void MyFrame
::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
775 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
777 m_ctrl
[0]->Collapse( item
);
780 void MyFrame
::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
782 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
784 m_ctrl
[0]->Expand( item
);
787 void MyFrame
::OnValueChanged( wxDataViewEvent
&event
)
792 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"),
793 event
.GetItem().GetID(), event
.GetColumn() );
796 void MyFrame
::OnActivated( wxDataViewEvent
&event
)
801 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
802 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title
);
804 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
805 wxLogMessage(wxT("Item: %s is expanded"), title
);
808 void MyFrame
::OnSelectionChanged( wxDataViewEvent
&event
)
813 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
817 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title
);
820 void MyFrame
::OnExpanding( wxDataViewEvent
&event
)
825 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
826 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title
);
830 void MyFrame
::OnEditingStarted( wxDataViewEvent
&event
)
835 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
836 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title
);
839 void MyFrame
::OnEditingDone( wxDataViewEvent
&event
)
844 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
845 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title
);
848 void MyFrame
::OnExpanded( wxDataViewEvent
&event
)
853 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
854 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title
);
857 void MyFrame
::OnCollapsing( wxDataViewEvent
&event
)
862 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
863 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title
);
866 void MyFrame
::OnCollapsed( wxDataViewEvent
&event
)
871 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
872 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title
);
875 void MyFrame
::OnContextMenu( wxDataViewEvent
&event
)
880 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
881 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title
);
884 menu
.Append( 1, wxT("entry 1") );
885 menu
.Append( 2, wxT("entry 2") );
886 menu
.Append( 3, wxT("entry 3") );
888 m_ctrl
[0]->PopupMenu(&menu
);
890 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),
891 title
.GetData(), event
.GetValue().GetString());
894 void MyFrame
::OnHeaderClick( wxDataViewEvent
&event
)
896 // we need to skip the event to let the default behaviour of sorting by
897 // this column when it is clicked to take place
903 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
905 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos
);
908 void MyFrame
::OnHeaderRightClick( wxDataViewEvent
&event
)
913 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
915 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos
);
918 void MyFrame
::OnSorted( wxDataViewEvent
&event
)
923 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
925 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos
);
928 void MyFrame
::OnRightClick( wxMouseEvent
&event
)
933 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"),
934 event
.GetX(), event
.GetY());
938 // ----------------------------------------------------------------------------
939 // MyFrame - event handlers for the second page
940 // ----------------------------------------------------------------------------
942 void MyFrame
::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
944 m_list_model
->Prepend(wxT("Test"));
947 void MyFrame
::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
949 wxDataViewItemArray items
;
950 int len
= m_ctrl
[1]->GetSelections( items
);
952 m_list_model
->DeleteItems( items
);
955 void MyFrame
::OnGoto(wxCommandEvent
& WXUNUSED(event
))
957 wxDataViewItem item
= m_list_model
->GetItem( 50 );
958 m_ctrl
[1]->EnsureVisible(item
,m_col
);
961 void MyFrame
::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
963 m_list_model
->AddMany();