1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/dataview.h"
27 #include "wx/datetime.h"
28 #include "wx/splitter.h"
29 #include "wx/aboutdlg.h"
30 #include "wx/colordlg.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 // ----------------------------------------------------------------------------
43 #ifndef wxHAS_IMAGES_IN_RESOURCES
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);
75 void OnStyleChange(wxCommandEvent
& event
);
76 void OnSetBackgroundColour(wxCommandEvent
& event
);
77 void OnSetForegroundColour(wxCommandEvent
& event
);
78 void OnQuit(wxCommandEvent
& event
);
79 void OnAbout(wxCommandEvent
& event
);
81 void OnClearLog(wxCommandEvent
& event
);
82 void OnPageChanged(wxBookCtrlEvent
& event
);
84 void OnAddMozart(wxCommandEvent
& event
);
85 void OnDeleteSelected(wxCommandEvent
& event
);
86 void OnDeleteYear(wxCommandEvent
& event
);
87 void OnSelectNinth(wxCommandEvent
& event
);
88 void OnCollapse(wxCommandEvent
& event
);
89 void OnExpand(wxCommandEvent
& event
);
90 void OnShowCurrent(wxCommandEvent
& event
);
91 void OnSetNinthCurrent(wxCommandEvent
& event
);
93 void OnPrependList(wxCommandEvent
& event
);
94 void OnDeleteList(wxCommandEvent
& event
);
96 void OnDeleteTreeItem(wxCommandEvent
& event
);
97 void OnDeleteAllTreeItems(wxCommandEvent
& event
);
98 void OnAddTreeItem(wxCommandEvent
& event
);
99 void OnAddTreeContainerItem(wxCommandEvent
& event
);
101 void OnValueChanged( wxDataViewEvent
&event
);
103 void OnActivated( wxDataViewEvent
&event
);
104 void OnExpanding( wxDataViewEvent
&event
);
105 void OnExpanded( wxDataViewEvent
&event
);
106 void OnCollapsing( wxDataViewEvent
&event
);
107 void OnCollapsed( wxDataViewEvent
&event
);
108 void OnSelectionChanged( wxDataViewEvent
&event
);
110 void OnStartEditing( wxDataViewEvent
&event
);
111 void OnEditingStarted( wxDataViewEvent
&event
);
112 void OnEditingDone( wxDataViewEvent
&event
);
114 void OnHeaderClick( wxDataViewEvent
&event
);
115 void OnAttrHeaderClick( wxDataViewEvent
&event
);
116 void OnHeaderRightClick( wxDataViewEvent
&event
);
117 void OnSorted( wxDataViewEvent
&event
);
119 void OnContextMenu( wxDataViewEvent
&event
);
121 void OnGoto( wxCommandEvent
&event
);
122 void OnAddMany( wxCommandEvent
&event
);
123 void OnHideAttributes( wxCommandEvent
&event
);
124 void OnShowAttributes( wxCommandEvent
&event
);
126 #if wxUSE_DRAG_AND_DROP
127 void OnBeginDrag( wxDataViewEvent
&event
);
128 void OnDropPossible( wxDataViewEvent
&event
);
129 void OnDrop( wxDataViewEvent
&event
);
130 #endif // wxUSE_DRAG_AND_DROP
132 void OnDataViewChar(wxKeyEvent
& event
);
134 // helper used by both OnDeleteSelected() and OnDataViewChar()
135 void DeleteSelectedItems();
138 wxNotebook
* m_notebook
;
140 // the controls stored in the various tabs of the main notebook:
142 wxDataViewCtrl
* m_ctrl
[4];
144 // the models associated with the first two DVC:
146 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
147 wxObjectDataPtr
<MyListModel
> m_list_model
;
151 wxDataViewColumn
* m_col
;
152 wxDataViewColumn
* m_attributes
;
158 DECLARE_EVENT_TABLE()
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 class MyCustomRenderer
: public wxDataViewCustomRenderer
170 : wxDataViewCustomRenderer("string",
171 wxDATAVIEW_CELL_ACTIVATABLE
,
175 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
)
177 dc
->SetBrush( *wxLIGHT_GREY_BRUSH
);
178 dc
->SetPen( *wxTRANSPARENT_PEN
);
181 dc
->DrawRoundedRectangle( rect
, 5 );
185 wxRect(dc
->GetTextExtent(m_value
)).CentreIn(rect
),
191 virtual bool ActivateCell(const wxRect
& WXUNUSED(cell
),
192 wxDataViewModel
*WXUNUSED(model
),
193 const wxDataViewItem
&WXUNUSED(item
),
194 unsigned int WXUNUSED(col
),
195 const wxMouseEvent
*mouseEvent
)
199 position
= wxString::Format("via mouse at %d, %d", mouseEvent
->m_x
, mouseEvent
->m_y
);
201 position
= "from keyboard";
202 wxLogMessage("MyCustomRenderer ActivateCell() %s", position
);
206 virtual wxSize
GetSize() const
208 return wxSize(60,20);
211 virtual bool SetValue( const wxVariant
&value
)
213 m_value
= value
.GetString();
217 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
224 // ============================================================================
226 // ============================================================================
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
236 if ( !wxApp::OnInit() )
240 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
253 ID_CLEARLOG
= wxID_HIGHEST
+1,
254 ID_BACKGROUND_COLOUR
,
255 ID_FOREGROUND_COLOUR
,
259 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
268 ID_ABOUT
= wxID_ABOUT
,
278 ID_DELETE_YEAR
= 102,
279 ID_SELECT_NINTH
= 103,
283 ID_SET_NINTH_CURRENT
,
285 ID_PREPEND_LIST
= 200,
286 ID_DELETE_LIST
= 201,
289 ID_HIDE_ATTRIBUTES
= 204,
290 ID_SHOW_ATTRIBUTES
= 205,
293 ID_DELETE_TREE_ITEM
= 400,
294 ID_DELETE_ALL_TREE_ITEMS
= 401,
295 ID_ADD_TREE_ITEM
= 402,
296 ID_ADD_TREE_CONTAINER_ITEM
= 403
299 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
300 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
301 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
302 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
303 EVT_MENU( ID_CLEARLOG
, MyFrame::OnClearLog
)
305 EVT_MENU( ID_FOREGROUND_COLOUR
, MyFrame::OnSetForegroundColour
)
306 EVT_MENU( ID_BACKGROUND_COLOUR
, MyFrame::OnSetBackgroundColour
)
308 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
310 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
311 EVT_BUTTON( ID_DELETE_SEL
, MyFrame::OnDeleteSelected
)
312 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
313 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
314 EVT_BUTTON( ID_COLLAPSE
, MyFrame::OnCollapse
)
315 EVT_BUTTON( ID_EXPAND
, MyFrame::OnExpand
)
316 EVT_BUTTON( ID_SHOW_CURRENT
, MyFrame::OnShowCurrent
)
317 EVT_BUTTON( ID_SET_NINTH_CURRENT
, MyFrame::OnSetNinthCurrent
)
319 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
320 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
321 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
322 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
323 EVT_BUTTON( ID_HIDE_ATTRIBUTES
, MyFrame::OnHideAttributes
)
324 EVT_BUTTON( ID_SHOW_ATTRIBUTES
, MyFrame::OnShowAttributes
)
326 EVT_BUTTON( ID_DELETE_TREE_ITEM
, MyFrame::OnDeleteTreeItem
)
327 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS
, MyFrame::OnDeleteAllTreeItems
)
328 EVT_BUTTON( ID_ADD_TREE_ITEM
, MyFrame::OnAddTreeItem
)
329 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM
, MyFrame::OnAddTreeContainerItem
)
331 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
333 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
334 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
335 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
336 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
337 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
338 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
340 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL
, MyFrame::OnStartEditing
)
341 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
342 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
344 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
345 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
346 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
348 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
350 #if wxUSE_DRAG_AND_DROP
351 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
352 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
353 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
354 #endif // wxUSE_DRAG_AND_DROP
356 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL
, MyFrame::OnAttrHeaderClick
)
360 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
361 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
371 SetIcon(wxICON(sample
));
377 wxMenu
*style_menu
= new wxMenu
;
378 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
379 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
380 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
381 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
382 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
384 wxMenu
*file_menu
= new wxMenu
;
385 file_menu
->Append(ID_CLEARLOG
, "&Clear log\tCtrl-L");
386 file_menu
->Append(ID_FOREGROUND_COLOUR
, "Set &foreground colour...\tCtrl-S");
387 file_menu
->Append(ID_BACKGROUND_COLOUR
, "Set &background colour...\tCtrl-B");
388 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
389 file_menu
->AppendSeparator();
390 file_menu
->Append(ID_EXIT
, "E&xit");
392 wxMenu
*about_menu
= new wxMenu
;
393 about_menu
->Append(ID_ABOUT
, "&About");
395 wxMenuBar
*menu_bar
= new wxMenuBar
;
396 menu_bar
->Append(file_menu
, "&File");
397 menu_bar
->Append(about_menu
, "&About");
399 SetMenuBar(menu_bar
);
403 // redirect logs from our event handlers to text control
404 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
405 wxDefaultSize
, wxTE_MULTILINE
);
406 m_log
->SetMinSize(wxSize(-1, 100));
407 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
408 wxLogMessage( "This is the log window" );
411 // first page of the notebook
412 // --------------------------
414 m_notebook
= new wxNotebook( this, wxID_ANY
);
416 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
418 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
420 const wxSizerFlags border
= wxSizerFlags().DoubleBorder();
422 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
423 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), border
);
424 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_SEL
, "Delete selected"), border
);
425 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), border
);
426 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), border
);
427 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), border
);
428 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), border
);
430 wxBoxSizer
*sizerCurrent
= new wxBoxSizer(wxHORIZONTAL
);
431 sizerCurrent
->Add(new wxButton(firstPanel
, ID_SHOW_CURRENT
,
432 "&Show current"), border
);
433 sizerCurrent
->Add(new wxButton(firstPanel
, ID_SET_NINTH_CURRENT
,
434 "Make &ninth symphony current"), border
);
436 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
437 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
438 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
440 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
442 firstPanelSz
->Add(button_sizer
);
443 firstPanelSz
->Add(sizerCurrent
);
444 firstPanel
->SetSizerAndFit(firstPanelSz
);
447 // second page of the notebook
448 // ---------------------------
450 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
452 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
454 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
455 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
456 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
457 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
458 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
459 button_sizer2
->Add( new wxButton( secondPanel
, ID_HIDE_ATTRIBUTES
, "Hide attributes"), 0, wxALL
, 10 );
460 button_sizer2
->Add( new wxButton( secondPanel
, ID_SHOW_ATTRIBUTES
, "Show attributes"), 0, wxALL
, 10 );
462 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
463 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
464 secondPanelSz
->Add(button_sizer2
);
465 secondPanel
->SetSizerAndFit(secondPanelSz
);
468 // third page of the notebook
469 // ---------------------------
471 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
473 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
475 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
476 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
477 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
480 // fourth page of the notebook
481 // ---------------------------
483 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
485 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
487 wxBoxSizer
*button_sizer4
= new wxBoxSizer( wxHORIZONTAL
);
488 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_TREE_ITEM
, "Delete Selected"), 0, wxALL
, 10 );
489 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_ALL_TREE_ITEMS
, "Delete All"), 0, wxALL
, 10 );
490 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_ITEM
, "Add Item"), 0, wxALL
, 10 );
491 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_CONTAINER_ITEM
, "Add Container"), 0, wxALL
, 10 );
493 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
494 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
495 fourthPanelSz
->Add(button_sizer4
);
496 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
503 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
504 m_notebook
->AddPage(secondPanel
, "MyListModel");
505 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
506 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
508 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
510 mainSizer
->Add( m_notebook
, 1, wxGROW
);
511 mainSizer
->Add( m_log
, 0, wxGROW
);
513 SetSizerAndFit(mainSizer
);
518 delete wxLog::SetActiveTarget(m_logOld
);
521 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
527 wxASSERT(!m_ctrl
[0] && !m_music_model
);
529 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
530 wxDefaultSize
, style
);
531 m_ctrl
[0]->Connect(wxEVT_CHAR
,
532 wxKeyEventHandler(MyFrame::OnDataViewChar
),
535 m_music_model
= new MyMusicTreeModel
;
536 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
538 #if wxUSE_DRAG_AND_DROP && wxUSE_UNICODE
539 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
540 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
541 #endif // wxUSE_DRAG_AND_DROP && wxUSE_UNICODE
543 // column 0 of the view control:
545 wxDataViewTextRenderer
*tr
=
546 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
547 wxDataViewColumn
*column0
=
548 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
549 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
550 m_ctrl
[0]->AppendColumn( column0
);
552 // Call this and sorting is enabled
553 // immediatly upon start up.
554 column0
->SetAsSortKey();
557 // column 1 of the view control:
559 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
560 wxDataViewColumn
*column1
=
561 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
562 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
563 wxDATAVIEW_COL_RESIZABLE
);
564 column1
->SetMinWidth(150); // this column can't be resized to be smaller
565 m_ctrl
[0]->AppendColumn( column1
);
567 // column 2 of the view control:
569 wxDataViewSpinRenderer
*sr
=
570 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
571 wxDataViewColumn
*column2
=
572 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
573 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
574 m_ctrl
[0]->AppendColumn( column2
);
576 // column 3 of the view control:
578 wxArrayString choices
;
579 choices
.Add( "good" );
580 choices
.Add( "bad" );
581 choices
.Add( "lousy" );
582 wxDataViewChoiceRenderer
*c
=
583 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
584 wxDataViewColumn
*column3
=
585 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
586 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
587 m_ctrl
[0]->AppendColumn( column3
);
589 // column 4 of the view control:
591 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
593 // column 5 of the view control:
595 MyCustomRenderer
*cr
= new MyCustomRenderer
;
596 wxDataViewColumn
*column5
=
597 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
598 wxDATAVIEW_COL_RESIZABLE
);
599 m_ctrl
[0]->AppendColumn( column5
);
602 // select initially the ninth symphony:
603 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
609 wxASSERT(!m_ctrl
[1] && !m_list_model
);
610 m_ctrl
[1] = new wxDataViewCtrl( parent
, ID_ATTR_CTRL
, wxDefaultPosition
,
611 wxDefaultSize
, style
);
613 m_list_model
= new MyListModel
;
614 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
616 // the various columns
617 m_ctrl
[1]->AppendTextColumn("editable string",
618 MyListModel::Col_EditableText
,
619 wxDATAVIEW_CELL_EDITABLE
,
620 wxCOL_WIDTH_AUTOSIZE
);
621 m_ctrl
[1]->AppendIconTextColumn("icon",
622 MyListModel::Col_IconText
,
623 wxDATAVIEW_CELL_EDITABLE
,
624 wxCOL_WIDTH_AUTOSIZE
);
627 new wxDataViewColumn("attributes",
628 new wxDataViewTextRenderer
,
629 MyListModel::Col_TextWithAttr
,
630 wxCOL_WIDTH_AUTOSIZE
,
632 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
633 m_ctrl
[1]->AppendColumn( m_attributes
);
635 m_ctrl
[1]->AppendColumn(
636 new wxDataViewColumn("custom renderer",
637 new MyCustomRenderer
,
638 MyListModel::Col_Custom
)
645 wxASSERT(!m_ctrl
[2]);
646 wxDataViewListCtrl
* lc
=
647 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
648 wxDefaultSize
, style
);
651 MyListStoreDerivedModel
* page2_model
= new MyListStoreDerivedModel();
652 lc
->AssociateModel(page2_model
);
653 page2_model
->DecRef();
655 lc
->AppendToggleColumn( "Toggle" );
656 lc
->AppendTextColumn( "Text" );
657 lc
->AppendProgressColumn( "Progress" );
659 wxVector
<wxVariant
> data
;
660 for (unsigned int i
=0; i
<10; i
++)
663 data
.push_back( (i%3
) == 0 );
664 data
.push_back( wxString::Format("row %d", i
) );
665 data
.push_back( long(5*i
) );
667 lc
->AppendItem( data
);
674 wxASSERT(!m_ctrl
[3]);
675 wxDataViewTreeCtrl
* tc
=
676 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
677 wxDefaultSize
, style
| wxDV_NO_HEADER
);
680 wxImageList
*ilist
= new wxImageList( 16, 16 );
681 ilist
->Add( wxIcon(wx_small_xpm
) );
682 tc
->AssignImageList( ilist
);
684 wxDataViewItem parent
=
685 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
686 tc
->AppendItem( parent
, "Child 1", 0 );
687 tc
->AppendItem( parent
, "Child 2", 0 );
688 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
690 wxDataViewItem cont
=
691 tc
->AppendContainer( parent
, "Container child", 0 );
692 tc
->AppendItem( cont
, "Child 4", 0 );
693 tc
->AppendItem( cont
, "Child 5", 0 );
702 // ----------------------------------------------------------------------------
703 // MyFrame - generic event handlers
704 // ----------------------------------------------------------------------------
706 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
711 void MyFrame::OnSetForegroundColour(wxCommandEvent
& WXUNUSED(event
))
713 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
714 wxColour col
= wxGetColourFromUser(this, dvc
->GetForegroundColour());
717 dvc
->SetForegroundColour(col
);
722 void MyFrame::OnSetBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
724 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
725 wxColour col
= wxGetColourFromUser(this, dvc
->GetBackgroundColour());
728 dvc
->SetBackgroundColour(col
);
733 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
735 unsigned int nPanel
= m_notebook
->GetSelection();
737 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
738 wxString::Format("Style of panel #%d", nPanel
+1));
740 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
742 unsigned long style
= 0;
749 style
= wxDV_MULTIPLE
;
752 style
= wxDV_ROW_LINES
;
755 style
= wxDV_HORIZ_RULES
;
758 style
= wxDV_VERT_RULES
;
764 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
768 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
770 unsigned int nPanel
= m_notebook
->GetSelection();
773 unsigned long style
= 0;
774 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
775 style |= wxDV_SINGLE;*/
776 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
777 style
|= wxDV_MULTIPLE
;
778 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
779 style
|= wxDV_ROW_LINES
;
780 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
781 style
|= wxDV_HORIZ_RULES
;
782 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
783 style
|= wxDV_VERT_RULES
;
785 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
788 sz
->Detach(m_ctrl
[nPanel
]);
789 wxDELETE(m_ctrl
[nPanel
]);
790 m_ctrl
[nPanel
] = NULL
;
793 m_music_model
.reset(NULL
);
794 else if (nPanel
== 1)
795 m_list_model
.reset(NULL
);
797 // rebuild the DVC for the selected panel:
798 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
800 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
804 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
809 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
811 wxAboutDialogInfo info
;
812 info
.SetName(_("DataView sample"));
813 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
814 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
815 info
.AddDeveloper("Robert Roebling");
816 info
.AddDeveloper("Francesco Montorsi");
822 // ----------------------------------------------------------------------------
823 // MyFrame - event handlers for the first page
824 // ----------------------------------------------------------------------------
826 #if wxUSE_DRAG_AND_DROP
828 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
830 wxDataViewItem
item( event
.GetItem() );
832 // only allow drags for item, not containers
833 if (m_music_model
->IsContainer( item
) )
839 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
840 wxTextDataObject
*obj
= new wxTextDataObject
;
841 obj
->SetText( node
->m_title
);
842 event
.SetDataObject( obj
);
843 event
.SetDragFlags(wxDrag_AllowMove
); // allows both copy and move
846 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
848 wxDataViewItem
item( event
.GetItem() );
850 // only allow drags for item, not containers
851 if (m_music_model
->IsContainer( item
) )
854 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
858 void MyFrame::OnDrop( wxDataViewEvent
&event
)
860 wxDataViewItem
item( event
.GetItem() );
862 // only allow drops for item, not containers
863 if (m_music_model
->IsContainer( item
) )
869 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
875 wxTextDataObject obj
;
876 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
878 wxLogMessage( "Text dropped: %s", obj
.GetText() );
881 #endif // wxUSE_DRAG_AND_DROP
883 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
885 m_music_model
->AddToClassical( "Eine kleine Nachtmusik", "Wolfgang Mozart", 1787 );
888 void MyFrame::DeleteSelectedItems()
890 wxDataViewItemArray items
;
891 int len
= m_ctrl
[0]->GetSelections( items
);
892 for( int i
= 0; i
< len
; i
++ )
894 m_music_model
->Delete( items
[i
] );
897 void MyFrame::OnDeleteSelected( wxCommandEvent
& WXUNUSED(event
) )
899 DeleteSelectedItems();
902 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
904 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
905 FindWindow( ID_DELETE_YEAR
)->Disable();
908 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
910 if (!m_music_model
->GetNinthItem().IsOk())
912 wxLogError( "Cannot select the ninth symphony: it was removed!" );
916 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
919 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
921 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
923 m_ctrl
[0]->Collapse( item
);
926 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
928 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
930 m_ctrl
[0]->Expand( item
);
933 void MyFrame::OnShowCurrent(wxCommandEvent
& WXUNUSED(event
))
935 wxDataViewItem item
= m_ctrl
[0]->GetCurrentItem();
938 wxLogMessage("Current item: \"%s\" by %s",
939 m_music_model
->GetTitle(item
),
940 m_music_model
->GetArtist(item
));
944 wxLogMessage("There is no current item.");
947 wxDataViewColumn
*col
= m_ctrl
[0]->GetCurrentColumn();
950 wxLogMessage("Current column: %d",
951 m_ctrl
[0]->GetColumnPosition(col
));
955 wxLogMessage("There is no current column.");
959 void MyFrame::OnSetNinthCurrent(wxCommandEvent
& WXUNUSED(event
))
961 wxDataViewItem
item(m_music_model
->GetNinthItem());
964 wxLogError( "Cannot make the ninth symphony current: it was removed!" );
968 m_ctrl
[0]->SetCurrentItem(item
);
971 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
973 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
974 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
975 title
, event
.GetColumn() );
978 void MyFrame::OnActivated( wxDataViewEvent
&event
)
980 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
981 wxLogMessage( "wxEVT_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
983 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
985 wxLogMessage( "Item: %s is expanded", title
);
989 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
991 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
995 wxLogMessage( "wxEVT_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
998 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
1000 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1001 wxLogMessage( "wxEVT_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
1005 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
1007 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
1008 if (artist
== "Ludwig van Beethoven")
1012 wxLogMessage( "wxEVT_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
1015 wxLogMessage( "wxEVT_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
1019 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
1021 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1022 wxLogMessage( "wxEVT_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
1025 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
1027 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1028 wxLogMessage( "wxEVT_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
1031 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
1033 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1034 wxLogMessage( "wxEVT_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
1037 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1039 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1040 wxLogMessage( "wxEVT_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
1043 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1045 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1046 wxLogMessage( "wxEVT_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
1049 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1051 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1052 wxLogMessage( "wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
1055 menu
.Append( 1, "menuitem 1" );
1056 menu
.Append( 2, "menuitem 2" );
1057 menu
.Append( 3, "menuitem 3" );
1059 m_ctrl
[0]->PopupMenu(&menu
);
1062 void MyFrame::OnAttrHeaderClick( wxDataViewEvent
&event
)
1064 // we need to skip the event to let the default behaviour of sorting by
1065 // this column when it is clicked to take place
1068 int pos
= m_ctrl
[1]->GetColumnPosition( event
.GetDataViewColumn() );
1070 wxLogMessage( "wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1071 wxLogMessage( "Column title: %s Column width: %d", event
.GetDataViewColumn()->GetTitle(), event
.GetDataViewColumn()->GetWidth() );
1074 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1076 // we need to skip the event to let the default behaviour of sorting by
1077 // this column when it is clicked to take place
1080 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1082 wxLogMessage( "wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1083 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
1086 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1088 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1090 wxLogMessage( "wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
1093 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1095 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1097 wxLogMessage( "wxEVT_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
1100 void MyFrame::OnDataViewChar(wxKeyEvent
& event
)
1102 if ( event
.GetKeyCode() == WXK_DELETE
)
1103 DeleteSelectedItems();
1108 // ----------------------------------------------------------------------------
1109 // MyFrame - event handlers for the second page
1110 // ----------------------------------------------------------------------------
1112 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1114 m_list_model
->Prepend("Test");
1117 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1119 wxDataViewItemArray items
;
1120 int len
= m_ctrl
[1]->GetSelections( items
);
1122 m_list_model
->DeleteItems( items
);
1125 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1127 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1128 m_ctrl
[1]->EnsureVisible(item
,m_col
);
1131 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1133 m_list_model
->AddMany();
1136 void MyFrame::OnHideAttributes(wxCommandEvent
& WXUNUSED(event
))
1138 m_attributes
->SetHidden(true);
1141 void MyFrame::OnShowAttributes(wxCommandEvent
& WXUNUSED(event
))
1143 m_attributes
->SetHidden(false);
1146 // ----------------------------------------------------------------------------
1147 // MyFrame - event handlers for the fourth page
1148 // ----------------------------------------------------------------------------
1150 void MyFrame::OnDeleteTreeItem(wxCommandEvent
& WXUNUSED(event
))
1152 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1153 wxDataViewItem selected
= ctrl
->GetSelection();
1154 if (!selected
.IsOk())
1157 ctrl
->DeleteItem(selected
);
1160 void MyFrame::OnDeleteAllTreeItems(wxCommandEvent
& WXUNUSED(event
))
1162 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1163 ctrl
->DeleteAllItems();
1166 void MyFrame::OnAddTreeItem(wxCommandEvent
& WXUNUSED(event
))
1168 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1169 wxDataViewItem selected
= ctrl
->GetSelection();
1170 if (ctrl
->IsContainer(selected
)) {
1171 wxDataViewItem newitem
= ctrl
->AppendItem( selected
, "Item", 0 );
1172 ctrl
->Select(newitem
);
1173 ctrl
->EditItem(newitem
, ctrl
->GetColumn(0));
1177 void MyFrame::OnAddTreeContainerItem(wxCommandEvent
& WXUNUSED(event
))
1179 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1180 wxDataViewItem selected
= ctrl
->GetSelection();
1181 if (ctrl
->IsContainer(selected
))
1182 ctrl
->AppendContainer(selected
, "Container", 0 );