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);
74 public: // event handlers
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 OnDeleteMusic(wxCommandEvent
& event
);
87 void OnDeleteYear(wxCommandEvent
& event
);
88 void OnSelectNinth(wxCommandEvent
& event
);
89 void OnCollapse(wxCommandEvent
& event
);
90 void OnExpand(wxCommandEvent
& event
);
92 void OnPrependList(wxCommandEvent
& event
);
93 void OnDeleteList(wxCommandEvent
& event
);
95 void OnDeleteTreeItem(wxCommandEvent
& event
);
96 void OnDeleteAllTreeItems(wxCommandEvent
& event
);
97 void OnAddTreeItem(wxCommandEvent
& event
);
98 void OnAddTreeContainerItem(wxCommandEvent
& event
);
100 void OnValueChanged( wxDataViewEvent
&event
);
102 void OnActivated( wxDataViewEvent
&event
);
103 void OnExpanding( wxDataViewEvent
&event
);
104 void OnExpanded( wxDataViewEvent
&event
);
105 void OnCollapsing( wxDataViewEvent
&event
);
106 void OnCollapsed( wxDataViewEvent
&event
);
107 void OnSelectionChanged( wxDataViewEvent
&event
);
109 void OnStartEditing( wxDataViewEvent
&event
);
110 void OnEditingStarted( wxDataViewEvent
&event
);
111 void OnEditingDone( wxDataViewEvent
&event
);
113 void OnHeaderClick( wxDataViewEvent
&event
);
114 void OnAttrHeaderClick( wxDataViewEvent
&event
);
115 void OnHeaderRightClick( wxDataViewEvent
&event
);
116 void OnSorted( wxDataViewEvent
&event
);
118 void OnContextMenu( wxDataViewEvent
&event
);
120 void OnRightClick( wxMouseEvent
&event
);
121 void OnGoto( wxCommandEvent
&event
);
122 void OnAddMany( wxCommandEvent
&event
);
123 void OnHideAttributes( wxCommandEvent
&event
);
124 void OnShowAttributes( wxCommandEvent
&event
);
126 void OnBeginDrag( wxDataViewEvent
&event
);
127 void OnDropPossible( wxDataViewEvent
&event
);
128 void OnDrop( wxDataViewEvent
&event
);
131 wxNotebook
* m_notebook
;
133 // the controls stored in the various tabs of the main notebook:
135 wxDataViewCtrl
* m_ctrl
[4];
137 // the models associated with the first two DVC:
139 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
140 wxObjectDataPtr
<MyListModel
> m_list_model
;
144 wxDataViewColumn
* m_col
;
145 wxDataViewColumn
* m_attributes
;
151 DECLARE_EVENT_TABLE()
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 class MyCustomRenderer
: public wxDataViewCustomRenderer
163 : wxDataViewCustomRenderer("string",
164 wxDATAVIEW_CELL_ACTIVATABLE
,
168 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
)
170 dc
->SetBrush( *wxLIGHT_GREY_BRUSH
);
171 dc
->SetPen( *wxTRANSPARENT_PEN
);
174 dc
->DrawRoundedRectangle( rect
, 5 );
178 wxRect(dc
->GetTextExtent(m_value
)).CentreIn(rect
),
184 virtual bool Activate( wxRect
WXUNUSED(cell
),
185 wxDataViewModel
*WXUNUSED(model
),
186 const wxDataViewItem
&WXUNUSED(item
),
187 unsigned int WXUNUSED(col
) )
189 wxLogMessage( "MyCustomRenderer Activate()" );
193 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
194 wxDataViewModel
*WXUNUSED(model
),
195 const wxDataViewItem
&WXUNUSED(item
),
196 unsigned int WXUNUSED(col
) )
198 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
202 virtual wxSize
GetSize() const
204 return wxSize(60,20);
207 virtual bool SetValue( const wxVariant
&value
)
209 m_value
= value
.GetString();
213 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
220 // ============================================================================
222 // ============================================================================
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
232 if ( !wxApp::OnInit() )
236 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
250 ID_CLEARLOG
= wxID_HIGHEST
+1,
251 ID_BACKGROUND_COLOUR
,
252 ID_FOREGROUND_COLOUR
,
256 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
265 ID_ABOUT
= wxID_ABOUT
,
274 ID_DELETE_MUSIC
= 101,
275 ID_DELETE_YEAR
= 102,
276 ID_SELECT_NINTH
= 103,
280 ID_PREPEND_LIST
= 200,
281 ID_DELETE_LIST
= 201,
284 ID_HIDE_ATTRIBUTES
= 204,
285 ID_SHOW_ATTRIBUTES
= 205,
288 ID_DELETE_TREE_ITEM
= 400,
289 ID_DELETE_ALL_TREE_ITEMS
= 401,
290 ID_ADD_TREE_ITEM
= 402,
291 ID_ADD_TREE_CONTAINER_ITEM
= 403
294 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
295 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
296 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
297 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
298 EVT_MENU( ID_CLEARLOG
, MyFrame::OnClearLog
)
300 EVT_MENU( ID_FOREGROUND_COLOUR
, MyFrame::OnSetForegroundColour
)
301 EVT_MENU( ID_BACKGROUND_COLOUR
, MyFrame::OnSetBackgroundColour
)
303 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
305 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
306 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
307 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
308 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
309 EVT_BUTTON( ID_COLLAPSE
, MyFrame::OnCollapse
)
310 EVT_BUTTON( ID_EXPAND
, MyFrame::OnExpand
)
312 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
313 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
314 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
315 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
316 EVT_BUTTON( ID_HIDE_ATTRIBUTES
, MyFrame::OnHideAttributes
)
317 EVT_BUTTON( ID_SHOW_ATTRIBUTES
, MyFrame::OnShowAttributes
)
319 EVT_BUTTON( ID_DELETE_TREE_ITEM
, MyFrame::OnDeleteTreeItem
)
320 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS
, MyFrame::OnDeleteAllTreeItems
)
321 EVT_BUTTON( ID_ADD_TREE_ITEM
, MyFrame::OnAddTreeItem
)
322 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM
, MyFrame::OnAddTreeContainerItem
)
324 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
326 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
327 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
328 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
329 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
330 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
331 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
333 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL
, MyFrame::OnStartEditing
)
334 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
335 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
337 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
338 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
339 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
341 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
343 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
344 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
345 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
347 EVT_RIGHT_UP(MyFrame::OnRightClick
)
349 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL
, MyFrame::OnAttrHeaderClick
)
353 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
354 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
364 SetIcon(wxICON(sample
));
370 wxMenu
*style_menu
= new wxMenu
;
371 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
372 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
373 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
374 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
375 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
377 wxMenu
*file_menu
= new wxMenu
;
378 file_menu
->Append(ID_CLEARLOG
, "&Clear log\tCtrl-L");
379 file_menu
->Append(ID_FOREGROUND_COLOUR
, "Set &foreground colour...\tCtrl-S");
380 file_menu
->Append(ID_BACKGROUND_COLOUR
, "Set &background colour...\tCtrl-B");
381 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
382 file_menu
->AppendSeparator();
383 file_menu
->Append(ID_EXIT
, "E&xit");
385 wxMenu
*about_menu
= new wxMenu
;
386 about_menu
->Append(ID_ABOUT
, "&About");
388 wxMenuBar
*menu_bar
= new wxMenuBar
;
389 menu_bar
->Append(file_menu
, "&File");
390 menu_bar
->Append(about_menu
, "&About");
392 SetMenuBar(menu_bar
);
396 // first page of the notebook
397 // --------------------------
399 m_notebook
= new wxNotebook( this, wxID_ANY
);
401 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
403 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
405 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
406 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
407 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_MUSIC
,"Delete selected"), 0, wxALL
, 10 );
408 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), 0, wxALL
, 10 );
409 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), 0, wxALL
, 10 );
410 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), 0, wxALL
, 10 );
411 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), 0, wxALL
, 10 );
413 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
414 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
415 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
417 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
419 firstPanelSz
->Add(button_sizer
);
420 firstPanel
->SetSizerAndFit(firstPanelSz
);
423 // second page of the notebook
424 // ---------------------------
426 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
428 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
430 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
431 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
432 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
433 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
434 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
435 button_sizer2
->Add( new wxButton( secondPanel
, ID_HIDE_ATTRIBUTES
, "Hide attributes"), 0, wxALL
, 10 );
436 button_sizer2
->Add( new wxButton( secondPanel
, ID_SHOW_ATTRIBUTES
, "Show attributes"), 0, wxALL
, 10 );
438 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
439 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
440 secondPanelSz
->Add(button_sizer2
);
441 secondPanel
->SetSizerAndFit(secondPanelSz
);
444 // third page of the notebook
445 // ---------------------------
447 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
449 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
451 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
452 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
453 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
456 // fourth page of the notebook
457 // ---------------------------
459 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
461 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
463 wxBoxSizer
*button_sizer4
= new wxBoxSizer( wxHORIZONTAL
);
464 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_TREE_ITEM
, "Delete Selected"), 0, wxALL
, 10 );
465 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_ALL_TREE_ITEMS
, "Delete All"), 0, wxALL
, 10 );
466 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_ITEM
, "Add Item"), 0, wxALL
, 10 );
467 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_CONTAINER_ITEM
, "Add Container"), 0, wxALL
, 10 );
469 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
470 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
471 fourthPanelSz
->Add(button_sizer4
);
472 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
479 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
480 m_notebook
->AddPage(secondPanel
, "MyListModel");
481 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
482 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
484 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
486 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
487 wxDefaultSize
, wxTE_MULTILINE
);
488 m_log
->SetMinSize(wxSize(-1, 100));
489 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
490 wxLogMessage( "This is the log window" );
492 mainSizer
->Add( m_notebook
, 1, wxGROW
);
493 mainSizer
->Add( m_log
, 0, wxGROW
);
495 SetSizerAndFit(mainSizer
);
500 delete wxLog::SetActiveTarget(m_logOld
);
503 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
509 wxASSERT(!m_ctrl
[0] && !m_music_model
);
511 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
512 wxDefaultSize
, style
);
514 m_music_model
= new MyMusicTreeModel
;
515 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
517 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
518 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
520 // column 0 of the view control:
522 wxDataViewTextRenderer
*tr
=
523 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
524 wxDataViewColumn
*column0
=
525 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
526 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
527 m_ctrl
[0]->AppendColumn( column0
);
529 // Call this and sorting is enabled
530 // immediatly upon start up.
531 column0
->SetAsSortKey();
534 // column 1 of the view control:
536 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
537 wxDataViewColumn
*column1
=
538 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
539 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
540 wxDATAVIEW_COL_RESIZABLE
);
541 column1
->SetMinWidth(150); // this column can't be resized to be smaller
542 m_ctrl
[0]->AppendColumn( column1
);
544 // column 2 of the view control:
546 wxDataViewSpinRenderer
*sr
=
547 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
548 wxDataViewColumn
*column2
=
549 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
550 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
551 m_ctrl
[0]->AppendColumn( column2
);
553 // column 3 of the view control:
555 wxArrayString choices
;
556 choices
.Add( "good" );
557 choices
.Add( "bad" );
558 choices
.Add( "lousy" );
559 wxDataViewChoiceRenderer
*c
=
560 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
561 wxDataViewColumn
*column3
=
562 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
563 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
564 m_ctrl
[0]->AppendColumn( column3
);
566 // column 4 of the view control:
568 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
570 // column 5 of the view control:
572 MyCustomRenderer
*cr
= new MyCustomRenderer
;
573 wxDataViewColumn
*column5
=
574 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
575 wxDATAVIEW_COL_RESIZABLE
);
576 m_ctrl
[0]->AppendColumn( column5
);
579 // select initially the ninth symphony:
580 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
586 wxASSERT(!m_ctrl
[1] && !m_list_model
);
587 m_ctrl
[1] = new wxDataViewCtrl( parent
, ID_ATTR_CTRL
, wxDefaultPosition
,
588 wxDefaultSize
, style
);
590 m_list_model
= new MyListModel
;
591 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
593 // the various columns
594 m_ctrl
[1]->AppendTextColumn("editable string",
595 MyListModel::Col_EditableText
,
596 wxDATAVIEW_CELL_EDITABLE
);
597 m_ctrl
[1]->AppendIconTextColumn("icon",
598 MyListModel::Col_IconText
,
599 wxDATAVIEW_CELL_EDITABLE
);
602 new wxDataViewColumn("attributes",
603 new wxDataViewTextRenderer
,
604 MyListModel::Col_TextWithAttr
,
607 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
608 m_ctrl
[1]->AppendColumn( m_attributes
);
610 m_ctrl
[1]->AppendColumn(
611 new wxDataViewColumn("custom renderer",
612 new MyCustomRenderer
,
613 MyListModel::Col_Custom
)
620 wxASSERT(!m_ctrl
[2]);
621 wxDataViewListCtrl
* lc
=
622 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
623 wxDefaultSize
, style
);
626 lc
->AppendToggleColumn( "Toggle" );
627 lc
->AppendTextColumn( "Text" );
628 lc
->AppendProgressColumn( "Progress" );
630 wxVector
<wxVariant
> data
;
631 for (unsigned int i
=0; i
<10; i
++)
634 data
.push_back( (i%3
) == 0 );
635 data
.push_back( wxString::Format("row %d", i
) );
636 data
.push_back( long(5*i
) );
638 lc
->AppendItem( data
);
645 wxASSERT(!m_ctrl
[3]);
646 wxDataViewTreeCtrl
* tc
=
647 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
648 wxDefaultSize
, style
| wxDV_NO_HEADER
);
651 wxImageList
*ilist
= new wxImageList( 16, 16 );
652 ilist
->Add( wxIcon(wx_small_xpm
) );
653 tc
->SetImageList( ilist
);
655 wxDataViewItem parent
=
656 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
657 tc
->AppendItem( parent
, "Child 1", 0 );
658 tc
->AppendItem( parent
, "Child 2", 0 );
659 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
661 wxDataViewItem cont
=
662 tc
->AppendContainer( parent
, "Container child", 0 );
663 tc
->AppendItem( cont
, "Child 4", 0 );
664 tc
->AppendItem( cont
, "Child 5", 0 );
673 // ----------------------------------------------------------------------------
674 // MyFrame - generic event handlers
675 // ----------------------------------------------------------------------------
677 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
682 void MyFrame::OnSetForegroundColour(wxCommandEvent
& WXUNUSED(event
))
684 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
685 wxColour col
= wxGetColourFromUser(this, dvc
->GetForegroundColour());
688 dvc
->SetForegroundColour(col
);
693 void MyFrame::OnSetBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
695 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
696 wxColour col
= wxGetColourFromUser(this, dvc
->GetBackgroundColour());
699 dvc
->SetBackgroundColour(col
);
704 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
706 unsigned int nPanel
= m_notebook
->GetSelection();
708 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
709 wxString::Format("Style of panel #%d", nPanel
+1));
711 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
713 unsigned long style
= 0;
720 style
= wxDV_MULTIPLE
;
723 style
= wxDV_ROW_LINES
;
726 style
= wxDV_HORIZ_RULES
;
729 style
= wxDV_VERT_RULES
;
735 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
739 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
741 unsigned int nPanel
= m_notebook
->GetSelection();
744 unsigned long style
= 0;
745 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
746 style |= wxDV_SINGLE;*/
747 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
748 style
|= wxDV_MULTIPLE
;
749 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
750 style
|= wxDV_ROW_LINES
;
751 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
752 style
|= wxDV_HORIZ_RULES
;
753 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
754 style
|= wxDV_VERT_RULES
;
756 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
759 sz
->Detach(m_ctrl
[nPanel
]);
760 wxDELETE(m_ctrl
[nPanel
]);
761 m_ctrl
[nPanel
] = NULL
;
764 m_music_model
.reset(NULL
);
765 else if (nPanel
== 1)
766 m_list_model
.reset(NULL
);
768 // rebuild the DVC for the selected panel:
769 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
771 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
775 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
780 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
782 wxAboutDialogInfo info
;
783 info
.SetName(_("DataView sample"));
784 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
785 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
786 info
.AddDeveloper("Robert Roebling");
787 info
.AddDeveloper("Francesco Montorsi");
793 // ----------------------------------------------------------------------------
794 // MyFrame - event handlers for the first page
795 // ----------------------------------------------------------------------------
797 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
799 wxDataViewItem
item( event
.GetItem() );
801 // only allow drags for item, not containers
802 if (m_music_model
->IsContainer( item
) )
808 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
809 wxTextDataObject
*obj
= new wxTextDataObject
;
810 obj
->SetText( node
->m_title
);
811 event
.SetDataObject( obj
);
814 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
816 wxDataViewItem
item( event
.GetItem() );
818 // only allow drags for item, not containers
819 if (m_music_model
->IsContainer( item
) )
822 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
826 void MyFrame::OnDrop( wxDataViewEvent
&event
)
828 wxDataViewItem
item( event
.GetItem() );
830 // only allow drops for item, not containers
831 if (m_music_model
->IsContainer( item
) )
837 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
843 wxTextDataObject obj
;
844 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
846 wxLogMessage( "Text dropped: %s", obj
.GetText() );
849 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
851 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
854 void MyFrame::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
856 wxDataViewItemArray items
;
857 int len
= m_ctrl
[0]->GetSelections( items
);
858 for( int i
= 0; i
< len
; i
++ )
860 m_music_model
->Delete( items
[i
] );
863 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
865 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
866 FindWindow( ID_DELETE_YEAR
)->Disable();
869 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
871 if (!m_music_model
->GetNinthItem().IsOk())
873 wxLogError( "Cannot select the ninth symphony: it was removed!" );
877 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
880 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
882 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
884 m_ctrl
[0]->Collapse( item
);
887 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
889 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
891 m_ctrl
[0]->Expand( item
);
894 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
899 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
900 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
901 title
, event
.GetColumn() );
904 void MyFrame::OnActivated( wxDataViewEvent
&event
)
909 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
910 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
912 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
914 wxLogMessage( "Item: %s is expanded", title
);
918 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
923 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
927 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
930 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
935 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
936 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
940 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
942 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
943 if (artist
== "Ludwig van Beethoven")
950 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
953 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
957 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
962 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
963 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
966 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
971 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
972 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
975 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
980 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
981 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
984 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
989 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
990 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
993 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
998 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
999 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
1002 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1007 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1008 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
1011 menu
.Append( 1, "menuitem 1" );
1012 menu
.Append( 2, "menuitem 2" );
1013 menu
.Append( 3, "menuitem 3" );
1015 m_ctrl
[0]->PopupMenu(&menu
);
1018 void MyFrame::OnAttrHeaderClick( wxDataViewEvent
&event
)
1020 // we need to skip the event to let the default behaviour of sorting by
1021 // this column when it is clicked to take place
1027 int pos
= m_ctrl
[1]->GetColumnPosition( event
.GetDataViewColumn() );
1029 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1030 wxLogMessage( "Column title: %s Column width: %d", event
.GetDataViewColumn()->GetTitle(), event
.GetDataViewColumn()->GetWidth() );
1033 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1035 // we need to skip the event to let the default behaviour of sorting by
1036 // this column when it is clicked to take place
1042 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1044 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1045 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
1048 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1053 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1055 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
1058 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1063 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1065 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
1068 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1073 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
1074 event
.GetX(), event
.GetY() );
1078 // ----------------------------------------------------------------------------
1079 // MyFrame - event handlers for the second page
1080 // ----------------------------------------------------------------------------
1082 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1084 m_list_model
->Prepend("Test");
1087 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1089 wxDataViewItemArray items
;
1090 int len
= m_ctrl
[1]->GetSelections( items
);
1092 m_list_model
->DeleteItems( items
);
1095 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1097 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1098 m_ctrl
[1]->EnsureVisible(item
,m_col
);
1101 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1103 m_list_model
->AddMany();
1106 void MyFrame::OnHideAttributes(wxCommandEvent
& WXUNUSED(event
))
1108 m_attributes
->SetHidden(true);
1111 void MyFrame::OnShowAttributes(wxCommandEvent
& WXUNUSED(event
))
1113 m_attributes
->SetHidden(false);
1116 // ----------------------------------------------------------------------------
1117 // MyFrame - event handlers for the fourth page
1118 // ----------------------------------------------------------------------------
1120 void MyFrame::OnDeleteTreeItem(wxCommandEvent
& WXUNUSED(event
))
1122 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1123 wxDataViewItem selected
= ctrl
->GetSelection();
1124 if (!selected
.IsOk())
1127 ctrl
->DeleteItem(selected
);
1130 void MyFrame::OnDeleteAllTreeItems(wxCommandEvent
& WXUNUSED(event
))
1132 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1133 ctrl
->DeleteAllItems();
1136 void MyFrame::OnAddTreeItem(wxCommandEvent
& WXUNUSED(event
))
1138 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1139 wxDataViewItem selected
= ctrl
->GetSelection();
1140 if (ctrl
->IsContainer(selected
))
1141 ctrl
->AppendItem( selected
, "Item", 0 );
1144 void MyFrame::OnAddTreeContainerItem(wxCommandEvent
& WXUNUSED(event
))
1146 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1147 wxDataViewItem selected
= ctrl
->GetSelection();
1148 if (ctrl
->IsContainer(selected
))
1149 ctrl
->AppendContainer(selected
, "Container", 0 );