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
);
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
);
130 void OnDataViewChar(wxKeyEvent
& event
);
132 // helper used by both OnDeleteSelected() and OnDataViewChar()
133 void DeleteSelectedItems();
136 wxNotebook
* m_notebook
;
138 // the controls stored in the various tabs of the main notebook:
140 wxDataViewCtrl
* m_ctrl
[4];
142 // the models associated with the first two DVC:
144 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
145 wxObjectDataPtr
<MyListModel
> m_list_model
;
149 wxDataViewColumn
* m_col
;
150 wxDataViewColumn
* m_attributes
;
156 DECLARE_EVENT_TABLE()
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 class MyCustomRenderer
: public wxDataViewCustomRenderer
168 : wxDataViewCustomRenderer("string",
169 wxDATAVIEW_CELL_ACTIVATABLE
,
173 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
)
175 dc
->SetBrush( *wxLIGHT_GREY_BRUSH
);
176 dc
->SetPen( *wxTRANSPARENT_PEN
);
179 dc
->DrawRoundedRectangle( rect
, 5 );
183 wxRect(dc
->GetTextExtent(m_value
)).CentreIn(rect
),
189 virtual bool Activate( wxRect
WXUNUSED(cell
),
190 wxDataViewModel
*WXUNUSED(model
),
191 const wxDataViewItem
&WXUNUSED(item
),
192 unsigned int WXUNUSED(col
) )
194 wxLogMessage( "MyCustomRenderer Activate()" );
198 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
199 wxDataViewModel
*WXUNUSED(model
),
200 const wxDataViewItem
&WXUNUSED(item
),
201 unsigned int WXUNUSED(col
) )
203 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
207 virtual wxSize
GetSize() const
209 return wxSize(60,20);
212 virtual bool SetValue( const wxVariant
&value
)
214 m_value
= value
.GetString();
218 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
225 // ============================================================================
227 // ============================================================================
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
237 if ( !wxApp::OnInit() )
241 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
255 ID_CLEARLOG
= wxID_HIGHEST
+1,
256 ID_BACKGROUND_COLOUR
,
257 ID_FOREGROUND_COLOUR
,
261 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
270 ID_ABOUT
= wxID_ABOUT
,
280 ID_DELETE_YEAR
= 102,
281 ID_SELECT_NINTH
= 103,
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
)
317 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
318 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
319 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
320 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
321 EVT_BUTTON( ID_HIDE_ATTRIBUTES
, MyFrame::OnHideAttributes
)
322 EVT_BUTTON( ID_SHOW_ATTRIBUTES
, MyFrame::OnShowAttributes
)
324 EVT_BUTTON( ID_DELETE_TREE_ITEM
, MyFrame::OnDeleteTreeItem
)
325 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS
, MyFrame::OnDeleteAllTreeItems
)
326 EVT_BUTTON( ID_ADD_TREE_ITEM
, MyFrame::OnAddTreeItem
)
327 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM
, MyFrame::OnAddTreeContainerItem
)
329 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
331 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
332 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
333 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
334 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
335 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
336 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
338 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL
, MyFrame::OnStartEditing
)
339 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
340 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
342 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
343 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
344 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
346 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
348 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
349 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
350 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
352 EVT_RIGHT_UP(MyFrame::OnRightClick
)
354 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL
, MyFrame::OnAttrHeaderClick
)
358 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
359 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
369 SetIcon(wxICON(sample
));
375 wxMenu
*style_menu
= new wxMenu
;
376 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
377 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
378 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
379 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
380 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
382 wxMenu
*file_menu
= new wxMenu
;
383 file_menu
->Append(ID_CLEARLOG
, "&Clear log\tCtrl-L");
384 file_menu
->Append(ID_FOREGROUND_COLOUR
, "Set &foreground colour...\tCtrl-S");
385 file_menu
->Append(ID_BACKGROUND_COLOUR
, "Set &background colour...\tCtrl-B");
386 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
387 file_menu
->AppendSeparator();
388 file_menu
->Append(ID_EXIT
, "E&xit");
390 wxMenu
*about_menu
= new wxMenu
;
391 about_menu
->Append(ID_ABOUT
, "&About");
393 wxMenuBar
*menu_bar
= new wxMenuBar
;
394 menu_bar
->Append(file_menu
, "&File");
395 menu_bar
->Append(about_menu
, "&About");
397 SetMenuBar(menu_bar
);
401 // first page of the notebook
402 // --------------------------
404 m_notebook
= new wxNotebook( this, wxID_ANY
);
406 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
408 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
410 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
411 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
412 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_SEL
, "Delete selected"), 0, wxALL
, 10 );
413 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), 0, wxALL
, 10 );
414 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), 0, wxALL
, 10 );
415 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), 0, wxALL
, 10 );
416 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), 0, wxALL
, 10 );
418 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
419 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
420 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
422 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
424 firstPanelSz
->Add(button_sizer
);
425 firstPanel
->SetSizerAndFit(firstPanelSz
);
428 // second page of the notebook
429 // ---------------------------
431 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
433 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
435 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
436 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
437 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
438 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
439 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
440 button_sizer2
->Add( new wxButton( secondPanel
, ID_HIDE_ATTRIBUTES
, "Hide attributes"), 0, wxALL
, 10 );
441 button_sizer2
->Add( new wxButton( secondPanel
, ID_SHOW_ATTRIBUTES
, "Show attributes"), 0, wxALL
, 10 );
443 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
444 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
445 secondPanelSz
->Add(button_sizer2
);
446 secondPanel
->SetSizerAndFit(secondPanelSz
);
449 // third page of the notebook
450 // ---------------------------
452 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
454 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
456 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
457 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
458 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
461 // fourth page of the notebook
462 // ---------------------------
464 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
466 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
468 wxBoxSizer
*button_sizer4
= new wxBoxSizer( wxHORIZONTAL
);
469 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_TREE_ITEM
, "Delete Selected"), 0, wxALL
, 10 );
470 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_ALL_TREE_ITEMS
, "Delete All"), 0, wxALL
, 10 );
471 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_ITEM
, "Add Item"), 0, wxALL
, 10 );
472 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_CONTAINER_ITEM
, "Add Container"), 0, wxALL
, 10 );
474 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
475 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
476 fourthPanelSz
->Add(button_sizer4
);
477 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
484 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
485 m_notebook
->AddPage(secondPanel
, "MyListModel");
486 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
487 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
489 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
491 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
492 wxDefaultSize
, wxTE_MULTILINE
);
493 m_log
->SetMinSize(wxSize(-1, 100));
494 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
495 wxLogMessage( "This is the log window" );
497 mainSizer
->Add( m_notebook
, 1, wxGROW
);
498 mainSizer
->Add( m_log
, 0, wxGROW
);
500 SetSizerAndFit(mainSizer
);
505 delete wxLog::SetActiveTarget(m_logOld
);
508 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
514 wxASSERT(!m_ctrl
[0] && !m_music_model
);
516 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
517 wxDefaultSize
, style
);
518 m_ctrl
[0]->Connect(wxEVT_CHAR
,
519 wxKeyEventHandler(MyFrame::OnDataViewChar
),
522 m_music_model
= new MyMusicTreeModel
;
523 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
525 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
526 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
528 // column 0 of the view control:
530 wxDataViewTextRenderer
*tr
=
531 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
532 wxDataViewColumn
*column0
=
533 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
534 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
535 m_ctrl
[0]->AppendColumn( column0
);
537 // Call this and sorting is enabled
538 // immediatly upon start up.
539 column0
->SetAsSortKey();
542 // column 1 of the view control:
544 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
545 wxDataViewColumn
*column1
=
546 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
547 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
548 wxDATAVIEW_COL_RESIZABLE
);
549 column1
->SetMinWidth(150); // this column can't be resized to be smaller
550 m_ctrl
[0]->AppendColumn( column1
);
552 // column 2 of the view control:
554 wxDataViewSpinRenderer
*sr
=
555 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
556 wxDataViewColumn
*column2
=
557 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
558 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
559 m_ctrl
[0]->AppendColumn( column2
);
561 // column 3 of the view control:
563 wxArrayString choices
;
564 choices
.Add( "good" );
565 choices
.Add( "bad" );
566 choices
.Add( "lousy" );
567 wxDataViewChoiceRenderer
*c
=
568 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
569 wxDataViewColumn
*column3
=
570 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
571 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
572 m_ctrl
[0]->AppendColumn( column3
);
574 // column 4 of the view control:
576 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
578 // column 5 of the view control:
580 MyCustomRenderer
*cr
= new MyCustomRenderer
;
581 wxDataViewColumn
*column5
=
582 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
583 wxDATAVIEW_COL_RESIZABLE
);
584 m_ctrl
[0]->AppendColumn( column5
);
587 // select initially the ninth symphony:
588 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
594 wxASSERT(!m_ctrl
[1] && !m_list_model
);
595 m_ctrl
[1] = new wxDataViewCtrl( parent
, ID_ATTR_CTRL
, wxDefaultPosition
,
596 wxDefaultSize
, style
);
598 m_list_model
= new MyListModel
;
599 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
601 // the various columns
602 m_ctrl
[1]->AppendTextColumn("editable string",
603 MyListModel::Col_EditableText
,
604 wxDATAVIEW_CELL_EDITABLE
);
605 m_ctrl
[1]->AppendIconTextColumn("icon",
606 MyListModel::Col_IconText
,
607 wxDATAVIEW_CELL_EDITABLE
);
610 new wxDataViewColumn("attributes",
611 new wxDataViewTextRenderer
,
612 MyListModel::Col_TextWithAttr
,
615 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
616 m_ctrl
[1]->AppendColumn( m_attributes
);
618 m_ctrl
[1]->AppendColumn(
619 new wxDataViewColumn("custom renderer",
620 new MyCustomRenderer
,
621 MyListModel::Col_Custom
)
628 wxASSERT(!m_ctrl
[2]);
629 wxDataViewListCtrl
* lc
=
630 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
631 wxDefaultSize
, style
);
634 lc
->AppendToggleColumn( "Toggle" );
635 lc
->AppendTextColumn( "Text" );
636 lc
->AppendProgressColumn( "Progress" );
638 wxVector
<wxVariant
> data
;
639 for (unsigned int i
=0; i
<10; i
++)
642 data
.push_back( (i%3
) == 0 );
643 data
.push_back( wxString::Format("row %d", i
) );
644 data
.push_back( long(5*i
) );
646 lc
->AppendItem( data
);
653 wxASSERT(!m_ctrl
[3]);
654 wxDataViewTreeCtrl
* tc
=
655 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
656 wxDefaultSize
, style
| wxDV_NO_HEADER
);
659 wxImageList
*ilist
= new wxImageList( 16, 16 );
660 ilist
->Add( wxIcon(wx_small_xpm
) );
661 tc
->SetImageList( ilist
);
663 wxDataViewItem parent
=
664 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
665 tc
->AppendItem( parent
, "Child 1", 0 );
666 tc
->AppendItem( parent
, "Child 2", 0 );
667 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
669 wxDataViewItem cont
=
670 tc
->AppendContainer( parent
, "Container child", 0 );
671 tc
->AppendItem( cont
, "Child 4", 0 );
672 tc
->AppendItem( cont
, "Child 5", 0 );
681 // ----------------------------------------------------------------------------
682 // MyFrame - generic event handlers
683 // ----------------------------------------------------------------------------
685 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
690 void MyFrame::OnSetForegroundColour(wxCommandEvent
& WXUNUSED(event
))
692 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
693 wxColour col
= wxGetColourFromUser(this, dvc
->GetForegroundColour());
696 dvc
->SetForegroundColour(col
);
701 void MyFrame::OnSetBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
703 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
704 wxColour col
= wxGetColourFromUser(this, dvc
->GetBackgroundColour());
707 dvc
->SetBackgroundColour(col
);
712 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
714 unsigned int nPanel
= m_notebook
->GetSelection();
716 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
717 wxString::Format("Style of panel #%d", nPanel
+1));
719 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
721 unsigned long style
= 0;
728 style
= wxDV_MULTIPLE
;
731 style
= wxDV_ROW_LINES
;
734 style
= wxDV_HORIZ_RULES
;
737 style
= wxDV_VERT_RULES
;
743 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
747 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
749 unsigned int nPanel
= m_notebook
->GetSelection();
752 unsigned long style
= 0;
753 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
754 style |= wxDV_SINGLE;*/
755 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
756 style
|= wxDV_MULTIPLE
;
757 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
758 style
|= wxDV_ROW_LINES
;
759 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
760 style
|= wxDV_HORIZ_RULES
;
761 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
762 style
|= wxDV_VERT_RULES
;
764 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
767 sz
->Detach(m_ctrl
[nPanel
]);
768 wxDELETE(m_ctrl
[nPanel
]);
769 m_ctrl
[nPanel
] = NULL
;
772 m_music_model
.reset(NULL
);
773 else if (nPanel
== 1)
774 m_list_model
.reset(NULL
);
776 // rebuild the DVC for the selected panel:
777 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
779 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
783 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
788 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
790 wxAboutDialogInfo info
;
791 info
.SetName(_("DataView sample"));
792 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
793 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
794 info
.AddDeveloper("Robert Roebling");
795 info
.AddDeveloper("Francesco Montorsi");
801 // ----------------------------------------------------------------------------
802 // MyFrame - event handlers for the first page
803 // ----------------------------------------------------------------------------
805 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
807 wxDataViewItem
item( event
.GetItem() );
809 // only allow drags for item, not containers
810 if (m_music_model
->IsContainer( item
) )
816 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
817 wxTextDataObject
*obj
= new wxTextDataObject
;
818 obj
->SetText( node
->m_title
);
819 event
.SetDataObject( obj
);
822 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
824 wxDataViewItem
item( event
.GetItem() );
826 // only allow drags for item, not containers
827 if (m_music_model
->IsContainer( item
) )
830 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
834 void MyFrame::OnDrop( wxDataViewEvent
&event
)
836 wxDataViewItem
item( event
.GetItem() );
838 // only allow drops for item, not containers
839 if (m_music_model
->IsContainer( item
) )
845 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
851 wxTextDataObject obj
;
852 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
854 wxLogMessage( "Text dropped: %s", obj
.GetText() );
857 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
859 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
862 void MyFrame::DeleteSelectedItems()
864 wxDataViewItemArray items
;
865 int len
= m_ctrl
[0]->GetSelections( items
);
866 for( int i
= 0; i
< len
; i
++ )
868 m_music_model
->Delete( items
[i
] );
871 void MyFrame::OnDeleteSelected( wxCommandEvent
& WXUNUSED(event
) )
873 DeleteSelectedItems();
876 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
878 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
879 FindWindow( ID_DELETE_YEAR
)->Disable();
882 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
884 if (!m_music_model
->GetNinthItem().IsOk())
886 wxLogError( "Cannot select the ninth symphony: it was removed!" );
890 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
893 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
895 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
897 m_ctrl
[0]->Collapse( item
);
900 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
902 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
904 m_ctrl
[0]->Expand( item
);
907 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
912 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
913 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
914 title
, event
.GetColumn() );
917 void MyFrame::OnActivated( wxDataViewEvent
&event
)
922 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
923 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
925 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
927 wxLogMessage( "Item: %s is expanded", title
);
931 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
936 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
940 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
943 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
948 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
949 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
953 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
955 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
956 if (artist
== "Ludwig van Beethoven")
963 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
966 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
970 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
975 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
976 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
979 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
984 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
985 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
988 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
993 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
994 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
997 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
1002 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1003 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
1006 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
1011 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1012 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
1015 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
1020 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
1021 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
1024 menu
.Append( 1, "menuitem 1" );
1025 menu
.Append( 2, "menuitem 2" );
1026 menu
.Append( 3, "menuitem 3" );
1028 m_ctrl
[0]->PopupMenu(&menu
);
1031 void MyFrame::OnAttrHeaderClick( wxDataViewEvent
&event
)
1033 // we need to skip the event to let the default behaviour of sorting by
1034 // this column when it is clicked to take place
1040 int pos
= m_ctrl
[1]->GetColumnPosition( event
.GetDataViewColumn() );
1042 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1043 wxLogMessage( "Column title: %s Column width: %d", event
.GetDataViewColumn()->GetTitle(), event
.GetDataViewColumn()->GetWidth() );
1046 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1048 // we need to skip the event to let the default behaviour of sorting by
1049 // this column when it is clicked to take place
1055 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1057 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1058 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
1061 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1066 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1068 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
1071 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1076 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1078 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
1081 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1086 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
1087 event
.GetX(), event
.GetY() );
1090 void MyFrame::OnDataViewChar(wxKeyEvent
& event
)
1092 if ( event
.GetKeyCode() == WXK_DELETE
)
1093 DeleteSelectedItems();
1098 // ----------------------------------------------------------------------------
1099 // MyFrame - event handlers for the second page
1100 // ----------------------------------------------------------------------------
1102 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1104 m_list_model
->Prepend("Test");
1107 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1109 wxDataViewItemArray items
;
1110 int len
= m_ctrl
[1]->GetSelections( items
);
1112 m_list_model
->DeleteItems( items
);
1115 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1117 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1118 m_ctrl
[1]->EnsureVisible(item
,m_col
);
1121 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1123 m_list_model
->AddMany();
1126 void MyFrame::OnHideAttributes(wxCommandEvent
& WXUNUSED(event
))
1128 m_attributes
->SetHidden(true);
1131 void MyFrame::OnShowAttributes(wxCommandEvent
& WXUNUSED(event
))
1133 m_attributes
->SetHidden(false);
1136 // ----------------------------------------------------------------------------
1137 // MyFrame - event handlers for the fourth page
1138 // ----------------------------------------------------------------------------
1140 void MyFrame::OnDeleteTreeItem(wxCommandEvent
& WXUNUSED(event
))
1142 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1143 wxDataViewItem selected
= ctrl
->GetSelection();
1144 if (!selected
.IsOk())
1147 ctrl
->DeleteItem(selected
);
1150 void MyFrame::OnDeleteAllTreeItems(wxCommandEvent
& WXUNUSED(event
))
1152 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1153 ctrl
->DeleteAllItems();
1156 void MyFrame::OnAddTreeItem(wxCommandEvent
& WXUNUSED(event
))
1158 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1159 wxDataViewItem selected
= ctrl
->GetSelection();
1160 if (ctrl
->IsContainer(selected
))
1161 ctrl
->AppendItem( selected
, "Item", 0 );
1164 void MyFrame::OnAddTreeContainerItem(wxCommandEvent
& WXUNUSED(event
))
1166 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1167 wxDataViewItem selected
= ctrl
->GetSelection();
1168 if (ctrl
->IsContainer(selected
))
1169 ctrl
->AppendContainer(selected
, "Container", 0 );