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 licence
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/colordlg.h"
32 #include "wx/choicdlg.h"
33 #include "wx/numdlg.h"
34 #include "wx/spinctrl.h"
35 #include "wx/imaglist.h"
36 #include "wx/notebook.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
45 #include "../sample.xpm"
48 #include "wx_small.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class MyApp
: public wxApp
57 virtual bool OnInit();
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 class MyFrame
: public wxFrame
67 MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
);
70 void BuildDataViewCtrl(wxPanel
* parent
,
72 unsigned long style
= 0);
76 void OnStyleChange(wxCommandEvent
& event
);
77 void OnSetBackgroundColour(wxCommandEvent
& event
);
78 void OnSetForegroundColour(wxCommandEvent
& event
);
79 void OnQuit(wxCommandEvent
& event
);
80 void OnAbout(wxCommandEvent
& event
);
82 void OnClearLog(wxCommandEvent
& event
);
83 void OnPageChanged(wxBookCtrlEvent
& event
);
85 void OnAddMozart(wxCommandEvent
& event
);
86 void OnDeleteSelected(wxCommandEvent
& event
);
87 void OnDeleteYear(wxCommandEvent
& event
);
88 void OnSelectNinth(wxCommandEvent
& event
);
89 void OnCollapse(wxCommandEvent
& event
);
90 void OnExpand(wxCommandEvent
& event
);
91 void OnShowCurrent(wxCommandEvent
& event
);
92 void OnSetNinthCurrent(wxCommandEvent
& event
);
94 void OnPrependList(wxCommandEvent
& event
);
95 void OnDeleteList(wxCommandEvent
& event
);
97 void OnDeleteTreeItem(wxCommandEvent
& event
);
98 void OnDeleteAllTreeItems(wxCommandEvent
& event
);
99 void OnAddTreeItem(wxCommandEvent
& event
);
100 void OnAddTreeContainerItem(wxCommandEvent
& event
);
102 void OnValueChanged( wxDataViewEvent
&event
);
104 void OnActivated( wxDataViewEvent
&event
);
105 void OnExpanding( wxDataViewEvent
&event
);
106 void OnExpanded( wxDataViewEvent
&event
);
107 void OnCollapsing( wxDataViewEvent
&event
);
108 void OnCollapsed( wxDataViewEvent
&event
);
109 void OnSelectionChanged( wxDataViewEvent
&event
);
111 void OnStartEditing( wxDataViewEvent
&event
);
112 void OnEditingStarted( wxDataViewEvent
&event
);
113 void OnEditingDone( wxDataViewEvent
&event
);
115 void OnHeaderClick( wxDataViewEvent
&event
);
116 void OnAttrHeaderClick( wxDataViewEvent
&event
);
117 void OnHeaderRightClick( wxDataViewEvent
&event
);
118 void OnSorted( wxDataViewEvent
&event
);
120 void OnContextMenu( wxDataViewEvent
&event
);
122 void OnRightClick( wxMouseEvent
&event
);
123 void OnGoto( wxCommandEvent
&event
);
124 void OnAddMany( wxCommandEvent
&event
);
125 void OnHideAttributes( wxCommandEvent
&event
);
126 void OnShowAttributes( wxCommandEvent
&event
);
128 #if wxUSE_DRAG_AND_DROP
129 void OnBeginDrag( wxDataViewEvent
&event
);
130 void OnDropPossible( wxDataViewEvent
&event
);
131 void OnDrop( wxDataViewEvent
&event
);
132 #endif // wxUSE_DRAG_AND_DROP
134 void OnDataViewChar(wxKeyEvent
& event
);
136 // helper used by both OnDeleteSelected() and OnDataViewChar()
137 void DeleteSelectedItems();
140 wxNotebook
* m_notebook
;
142 // the controls stored in the various tabs of the main notebook:
144 wxDataViewCtrl
* m_ctrl
[4];
146 // the models associated with the first two DVC:
148 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
149 wxObjectDataPtr
<MyListModel
> m_list_model
;
153 wxDataViewColumn
* m_col
;
154 wxDataViewColumn
* m_attributes
;
160 DECLARE_EVENT_TABLE()
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 class MyCustomRenderer
: public wxDataViewCustomRenderer
172 : wxDataViewCustomRenderer("string",
173 wxDATAVIEW_CELL_ACTIVATABLE
,
177 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
)
179 dc
->SetBrush( *wxLIGHT_GREY_BRUSH
);
180 dc
->SetPen( *wxTRANSPARENT_PEN
);
183 dc
->DrawRoundedRectangle( rect
, 5 );
187 wxRect(dc
->GetTextExtent(m_value
)).CentreIn(rect
),
193 virtual bool Activate( wxRect
WXUNUSED(cell
),
194 wxDataViewModel
*WXUNUSED(model
),
195 const wxDataViewItem
&WXUNUSED(item
),
196 unsigned int WXUNUSED(col
) )
198 wxLogMessage( "MyCustomRenderer Activate()" );
202 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
203 wxDataViewModel
*WXUNUSED(model
),
204 const wxDataViewItem
&WXUNUSED(item
),
205 unsigned int WXUNUSED(col
) )
207 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
211 virtual wxSize
GetSize() const
213 return wxSize(60,20);
216 virtual bool SetValue( const wxVariant
&value
)
218 m_value
= value
.GetString();
222 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
229 // ============================================================================
231 // ============================================================================
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
241 if ( !wxApp::OnInit() )
245 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
259 ID_CLEARLOG
= wxID_HIGHEST
+1,
260 ID_BACKGROUND_COLOUR
,
261 ID_FOREGROUND_COLOUR
,
265 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
274 ID_ABOUT
= wxID_ABOUT
,
284 ID_DELETE_YEAR
= 102,
285 ID_SELECT_NINTH
= 103,
289 ID_SET_NINTH_CURRENT
,
291 ID_PREPEND_LIST
= 200,
292 ID_DELETE_LIST
= 201,
295 ID_HIDE_ATTRIBUTES
= 204,
296 ID_SHOW_ATTRIBUTES
= 205,
299 ID_DELETE_TREE_ITEM
= 400,
300 ID_DELETE_ALL_TREE_ITEMS
= 401,
301 ID_ADD_TREE_ITEM
= 402,
302 ID_ADD_TREE_CONTAINER_ITEM
= 403
305 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
306 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
307 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
308 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
309 EVT_MENU( ID_CLEARLOG
, MyFrame::OnClearLog
)
311 EVT_MENU( ID_FOREGROUND_COLOUR
, MyFrame::OnSetForegroundColour
)
312 EVT_MENU( ID_BACKGROUND_COLOUR
, MyFrame::OnSetBackgroundColour
)
314 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
316 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
317 EVT_BUTTON( ID_DELETE_SEL
, MyFrame::OnDeleteSelected
)
318 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
319 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
320 EVT_BUTTON( ID_COLLAPSE
, MyFrame::OnCollapse
)
321 EVT_BUTTON( ID_EXPAND
, MyFrame::OnExpand
)
322 EVT_BUTTON( ID_SHOW_CURRENT
, MyFrame::OnShowCurrent
)
323 EVT_BUTTON( ID_SET_NINTH_CURRENT
, MyFrame::OnSetNinthCurrent
)
325 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
326 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
327 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
328 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
329 EVT_BUTTON( ID_HIDE_ATTRIBUTES
, MyFrame::OnHideAttributes
)
330 EVT_BUTTON( ID_SHOW_ATTRIBUTES
, MyFrame::OnShowAttributes
)
332 EVT_BUTTON( ID_DELETE_TREE_ITEM
, MyFrame::OnDeleteTreeItem
)
333 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS
, MyFrame::OnDeleteAllTreeItems
)
334 EVT_BUTTON( ID_ADD_TREE_ITEM
, MyFrame::OnAddTreeItem
)
335 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM
, MyFrame::OnAddTreeContainerItem
)
337 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
339 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
340 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
341 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
342 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
343 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
344 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
346 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL
, MyFrame::OnStartEditing
)
347 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
348 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
350 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
351 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
352 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
354 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
356 #if wxUSE_DRAG_AND_DROP
357 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
358 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
359 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
360 #endif // wxUSE_DRAG_AND_DROP
362 EVT_RIGHT_UP(MyFrame::OnRightClick
)
364 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL
, MyFrame::OnAttrHeaderClick
)
368 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
369 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
379 SetIcon(wxICON(sample
));
385 wxMenu
*style_menu
= new wxMenu
;
386 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
387 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
388 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
389 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
390 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
392 wxMenu
*file_menu
= new wxMenu
;
393 file_menu
->Append(ID_CLEARLOG
, "&Clear log\tCtrl-L");
394 file_menu
->Append(ID_FOREGROUND_COLOUR
, "Set &foreground colour...\tCtrl-S");
395 file_menu
->Append(ID_BACKGROUND_COLOUR
, "Set &background colour...\tCtrl-B");
396 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
397 file_menu
->AppendSeparator();
398 file_menu
->Append(ID_EXIT
, "E&xit");
400 wxMenu
*about_menu
= new wxMenu
;
401 about_menu
->Append(ID_ABOUT
, "&About");
403 wxMenuBar
*menu_bar
= new wxMenuBar
;
404 menu_bar
->Append(file_menu
, "&File");
405 menu_bar
->Append(about_menu
, "&About");
407 SetMenuBar(menu_bar
);
411 // redirect logs from our event handlers to text control
412 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
413 wxDefaultSize
, wxTE_MULTILINE
);
414 m_log
->SetMinSize(wxSize(-1, 100));
415 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
416 wxLogMessage( "This is the log window" );
419 // first page of the notebook
420 // --------------------------
422 m_notebook
= new wxNotebook( this, wxID_ANY
);
424 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
426 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
428 const wxSizerFlags border
= wxSizerFlags().DoubleBorder();
430 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
431 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), border
);
432 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_SEL
, "Delete selected"), border
);
433 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), border
);
434 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), border
);
435 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), border
);
436 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), border
);
438 wxBoxSizer
*sizerCurrent
= new wxBoxSizer(wxHORIZONTAL
);
439 sizerCurrent
->Add(new wxButton(firstPanel
, ID_SHOW_CURRENT
,
440 "&Show current"), border
);
441 sizerCurrent
->Add(new wxButton(firstPanel
, ID_SET_NINTH_CURRENT
,
442 "Make &ninth symphony current"), border
);
444 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
445 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
446 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
448 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
450 firstPanelSz
->Add(button_sizer
);
451 firstPanelSz
->Add(sizerCurrent
);
452 firstPanel
->SetSizerAndFit(firstPanelSz
);
455 // second page of the notebook
456 // ---------------------------
458 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
460 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
462 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
463 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
464 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
465 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
466 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
467 button_sizer2
->Add( new wxButton( secondPanel
, ID_HIDE_ATTRIBUTES
, "Hide attributes"), 0, wxALL
, 10 );
468 button_sizer2
->Add( new wxButton( secondPanel
, ID_SHOW_ATTRIBUTES
, "Show attributes"), 0, wxALL
, 10 );
470 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
471 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
472 secondPanelSz
->Add(button_sizer2
);
473 secondPanel
->SetSizerAndFit(secondPanelSz
);
476 // third page of the notebook
477 // ---------------------------
479 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
481 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
483 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
484 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
485 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
488 // fourth page of the notebook
489 // ---------------------------
491 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
493 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
495 wxBoxSizer
*button_sizer4
= new wxBoxSizer( wxHORIZONTAL
);
496 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_TREE_ITEM
, "Delete Selected"), 0, wxALL
, 10 );
497 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_ALL_TREE_ITEMS
, "Delete All"), 0, wxALL
, 10 );
498 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_ITEM
, "Add Item"), 0, wxALL
, 10 );
499 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_CONTAINER_ITEM
, "Add Container"), 0, wxALL
, 10 );
501 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
502 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
503 fourthPanelSz
->Add(button_sizer4
);
504 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
511 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
512 m_notebook
->AddPage(secondPanel
, "MyListModel");
513 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
514 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
516 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
518 mainSizer
->Add( m_notebook
, 1, wxGROW
);
519 mainSizer
->Add( m_log
, 0, wxGROW
);
521 SetSizerAndFit(mainSizer
);
526 delete wxLog::SetActiveTarget(m_logOld
);
529 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
535 wxASSERT(!m_ctrl
[0] && !m_music_model
);
537 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
538 wxDefaultSize
, style
);
539 m_ctrl
[0]->Connect(wxEVT_CHAR
,
540 wxKeyEventHandler(MyFrame::OnDataViewChar
),
543 m_music_model
= new MyMusicTreeModel
;
544 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
546 #if wxUSE_DRAG_AND_DROP
547 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
548 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
549 #endif // wxUSE_DRAG_AND_DROP
551 // column 0 of the view control:
553 wxDataViewTextRenderer
*tr
=
554 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
555 wxDataViewColumn
*column0
=
556 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
557 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
558 m_ctrl
[0]->AppendColumn( column0
);
560 // Call this and sorting is enabled
561 // immediatly upon start up.
562 column0
->SetAsSortKey();
565 // column 1 of the view control:
567 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
568 wxDataViewColumn
*column1
=
569 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
570 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
571 wxDATAVIEW_COL_RESIZABLE
);
572 column1
->SetMinWidth(150); // this column can't be resized to be smaller
573 m_ctrl
[0]->AppendColumn( column1
);
575 // column 2 of the view control:
577 wxDataViewSpinRenderer
*sr
=
578 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
579 wxDataViewColumn
*column2
=
580 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
581 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
582 m_ctrl
[0]->AppendColumn( column2
);
584 // column 3 of the view control:
586 wxArrayString choices
;
587 choices
.Add( "good" );
588 choices
.Add( "bad" );
589 choices
.Add( "lousy" );
590 wxDataViewChoiceRenderer
*c
=
591 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
592 wxDataViewColumn
*column3
=
593 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
594 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
595 m_ctrl
[0]->AppendColumn( column3
);
597 // column 4 of the view control:
599 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
601 // column 5 of the view control:
603 MyCustomRenderer
*cr
= new MyCustomRenderer
;
604 wxDataViewColumn
*column5
=
605 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
606 wxDATAVIEW_COL_RESIZABLE
);
607 m_ctrl
[0]->AppendColumn( column5
);
610 // select initially the ninth symphony:
611 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
617 wxASSERT(!m_ctrl
[1] && !m_list_model
);
618 m_ctrl
[1] = new wxDataViewCtrl( parent
, ID_ATTR_CTRL
, wxDefaultPosition
,
619 wxDefaultSize
, style
);
621 m_list_model
= new MyListModel
;
622 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
624 // the various columns
625 m_ctrl
[1]->AppendTextColumn("editable string",
626 MyListModel::Col_EditableText
,
627 wxDATAVIEW_CELL_EDITABLE
,
628 wxCOL_WIDTH_AUTOSIZE
);
629 m_ctrl
[1]->AppendIconTextColumn("icon",
630 MyListModel::Col_IconText
,
631 wxDATAVIEW_CELL_EDITABLE
,
632 wxCOL_WIDTH_AUTOSIZE
);
635 new wxDataViewColumn("attributes",
636 new wxDataViewTextRenderer
,
637 MyListModel::Col_TextWithAttr
,
638 wxCOL_WIDTH_AUTOSIZE
,
640 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
641 m_ctrl
[1]->AppendColumn( m_attributes
);
643 m_ctrl
[1]->AppendColumn(
644 new wxDataViewColumn("custom renderer",
645 new MyCustomRenderer
,
646 MyListModel::Col_Custom
)
653 wxASSERT(!m_ctrl
[2]);
654 wxDataViewListCtrl
* lc
=
655 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
656 wxDefaultSize
, style
);
659 lc
->AppendToggleColumn( "Toggle" );
660 lc
->AppendTextColumn( "Text" );
661 lc
->AppendProgressColumn( "Progress" );
663 wxVector
<wxVariant
> data
;
664 for (unsigned int i
=0; i
<10; i
++)
667 data
.push_back( (i%3
) == 0 );
668 data
.push_back( wxString::Format("row %d", i
) );
669 data
.push_back( long(5*i
) );
671 lc
->AppendItem( data
);
678 wxASSERT(!m_ctrl
[3]);
679 wxDataViewTreeCtrl
* tc
=
680 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
681 wxDefaultSize
, style
| wxDV_NO_HEADER
);
684 wxImageList
*ilist
= new wxImageList( 16, 16 );
685 ilist
->Add( wxIcon(wx_small_xpm
) );
686 tc
->SetImageList( ilist
);
688 wxDataViewItem parent
=
689 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
690 tc
->AppendItem( parent
, "Child 1", 0 );
691 tc
->AppendItem( parent
, "Child 2", 0 );
692 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
694 wxDataViewItem cont
=
695 tc
->AppendContainer( parent
, "Container child", 0 );
696 tc
->AppendItem( cont
, "Child 4", 0 );
697 tc
->AppendItem( cont
, "Child 5", 0 );
706 // ----------------------------------------------------------------------------
707 // MyFrame - generic event handlers
708 // ----------------------------------------------------------------------------
710 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
715 void MyFrame::OnSetForegroundColour(wxCommandEvent
& WXUNUSED(event
))
717 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
718 wxColour col
= wxGetColourFromUser(this, dvc
->GetForegroundColour());
721 dvc
->SetForegroundColour(col
);
726 void MyFrame::OnSetBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
728 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
729 wxColour col
= wxGetColourFromUser(this, dvc
->GetBackgroundColour());
732 dvc
->SetBackgroundColour(col
);
737 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
739 unsigned int nPanel
= m_notebook
->GetSelection();
741 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
742 wxString::Format("Style of panel #%d", nPanel
+1));
744 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
746 unsigned long style
= 0;
753 style
= wxDV_MULTIPLE
;
756 style
= wxDV_ROW_LINES
;
759 style
= wxDV_HORIZ_RULES
;
762 style
= wxDV_VERT_RULES
;
768 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
772 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
774 unsigned int nPanel
= m_notebook
->GetSelection();
777 unsigned long style
= 0;
778 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
779 style |= wxDV_SINGLE;*/
780 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
781 style
|= wxDV_MULTIPLE
;
782 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
783 style
|= wxDV_ROW_LINES
;
784 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
785 style
|= wxDV_HORIZ_RULES
;
786 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
787 style
|= wxDV_VERT_RULES
;
789 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
792 sz
->Detach(m_ctrl
[nPanel
]);
793 wxDELETE(m_ctrl
[nPanel
]);
794 m_ctrl
[nPanel
] = NULL
;
797 m_music_model
.reset(NULL
);
798 else if (nPanel
== 1)
799 m_list_model
.reset(NULL
);
801 // rebuild the DVC for the selected panel:
802 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
804 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
808 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
813 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
815 wxAboutDialogInfo info
;
816 info
.SetName(_("DataView sample"));
817 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
818 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
819 info
.AddDeveloper("Robert Roebling");
820 info
.AddDeveloper("Francesco Montorsi");
826 // ----------------------------------------------------------------------------
827 // MyFrame - event handlers for the first page
828 // ----------------------------------------------------------------------------
830 #if wxUSE_DRAG_AND_DROP
832 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
834 wxDataViewItem
item( event
.GetItem() );
836 // only allow drags for item, not containers
837 if (m_music_model
->IsContainer( item
) )
843 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
844 wxTextDataObject
*obj
= new wxTextDataObject
;
845 obj
->SetText( node
->m_title
);
846 event
.SetDataObject( obj
);
849 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
851 wxDataViewItem
item( event
.GetItem() );
853 // only allow drags for item, not containers
854 if (m_music_model
->IsContainer( item
) )
857 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
861 void MyFrame::OnDrop( wxDataViewEvent
&event
)
863 wxDataViewItem
item( event
.GetItem() );
865 // only allow drops for item, not containers
866 if (m_music_model
->IsContainer( item
) )
872 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
878 wxTextDataObject obj
;
879 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
881 wxLogMessage( "Text dropped: %s", obj
.GetText() );
884 #endif // wxUSE_DRAG_AND_DROP
886 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
888 m_music_model
->AddToClassical( "Eine kleine Nachtmusik", "Wolfgang Mozart", 1787 );
891 void MyFrame::DeleteSelectedItems()
893 wxDataViewItemArray items
;
894 int len
= m_ctrl
[0]->GetSelections( items
);
895 for( int i
= 0; i
< len
; i
++ )
897 m_music_model
->Delete( items
[i
] );
900 void MyFrame::OnDeleteSelected( wxCommandEvent
& WXUNUSED(event
) )
902 DeleteSelectedItems();
905 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
907 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
908 FindWindow( ID_DELETE_YEAR
)->Disable();
911 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
913 if (!m_music_model
->GetNinthItem().IsOk())
915 wxLogError( "Cannot select the ninth symphony: it was removed!" );
919 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
922 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
924 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
926 m_ctrl
[0]->Collapse( item
);
929 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
931 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
933 m_ctrl
[0]->Expand( item
);
936 void MyFrame::OnShowCurrent(wxCommandEvent
& WXUNUSED(event
))
938 wxDataViewItem item
= m_ctrl
[0]->GetCurrentItem();
941 wxLogMessage("Current item: \"%s\" by %s",
942 m_music_model
->GetTitle(item
),
943 m_music_model
->GetArtist(item
));
947 wxLogMessage("There is no current item.");
951 void MyFrame::OnSetNinthCurrent(wxCommandEvent
& WXUNUSED(event
))
953 wxDataViewItem
item(m_music_model
->GetNinthItem());
956 wxLogError( "Cannot make the ninth symphony current: it was removed!" );
960 m_ctrl
[0]->SetCurrentItem(item
);
963 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
965 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
966 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
967 title
, event
.GetColumn() );
970 void MyFrame::OnActivated( wxDataViewEvent
&event
)
972 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
973 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
975 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
977 wxLogMessage( "Item: %s is expanded", title
);
981 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
983 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
987 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
990 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
992 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
993 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
997 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
999 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
1000 if (artist
== "Ludwig van Beethoven")
1004 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
1007 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
1011 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
1013 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1014 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
1017 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
1019 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1020 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
1023 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
1025 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1026 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
1029 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1031 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1032 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
1035 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1037 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1038 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
1041 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1043 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1044 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
1047 menu
.Append( 1, "menuitem 1" );
1048 menu
.Append( 2, "menuitem 2" );
1049 menu
.Append( 3, "menuitem 3" );
1051 m_ctrl
[0]->PopupMenu(&menu
);
1054 void MyFrame::OnAttrHeaderClick( wxDataViewEvent
&event
)
1056 // we need to skip the event to let the default behaviour of sorting by
1057 // this column when it is clicked to take place
1060 int pos
= m_ctrl
[1]->GetColumnPosition( event
.GetDataViewColumn() );
1062 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1063 wxLogMessage( "Column title: %s Column width: %d", event
.GetDataViewColumn()->GetTitle(), event
.GetDataViewColumn()->GetWidth() );
1066 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1068 // we need to skip the event to let the default behaviour of sorting by
1069 // this column when it is clicked to take place
1072 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1074 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1075 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
1078 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1080 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1082 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
1085 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1087 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1089 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
1092 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1094 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
1095 event
.GetX(), event
.GetY() );
1098 void MyFrame::OnDataViewChar(wxKeyEvent
& event
)
1100 if ( event
.GetKeyCode() == WXK_DELETE
)
1101 DeleteSelectedItems();
1106 // ----------------------------------------------------------------------------
1107 // MyFrame - event handlers for the second page
1108 // ----------------------------------------------------------------------------
1110 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1112 m_list_model
->Prepend("Test");
1115 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1117 wxDataViewItemArray items
;
1118 int len
= m_ctrl
[1]->GetSelections( items
);
1120 m_list_model
->DeleteItems( items
);
1123 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1125 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1126 m_ctrl
[1]->EnsureVisible(item
,m_col
);
1129 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1131 m_list_model
->AddMany();
1134 void MyFrame::OnHideAttributes(wxCommandEvent
& WXUNUSED(event
))
1136 m_attributes
->SetHidden(true);
1139 void MyFrame::OnShowAttributes(wxCommandEvent
& WXUNUSED(event
))
1141 m_attributes
->SetHidden(false);
1144 // ----------------------------------------------------------------------------
1145 // MyFrame - event handlers for the fourth page
1146 // ----------------------------------------------------------------------------
1148 void MyFrame::OnDeleteTreeItem(wxCommandEvent
& WXUNUSED(event
))
1150 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1151 wxDataViewItem selected
= ctrl
->GetSelection();
1152 if (!selected
.IsOk())
1155 ctrl
->DeleteItem(selected
);
1158 void MyFrame::OnDeleteAllTreeItems(wxCommandEvent
& WXUNUSED(event
))
1160 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1161 ctrl
->DeleteAllItems();
1164 void MyFrame::OnAddTreeItem(wxCommandEvent
& WXUNUSED(event
))
1166 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1167 wxDataViewItem selected
= ctrl
->GetSelection();
1168 if (ctrl
->IsContainer(selected
))
1169 ctrl
->AppendItem( selected
, "Item", 0 );
1172 void MyFrame::OnAddTreeContainerItem(wxCommandEvent
& WXUNUSED(event
))
1174 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1175 wxDataViewItem selected
= ctrl
->GetSelection();
1176 if (ctrl
->IsContainer(selected
))
1177 ctrl
->AppendContainer(selected
, "Container", 0 );