1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/dataview.h"
28 #include "wx/datetime.h"
29 #include "wx/splitter.h"
30 #include "wx/aboutdlg.h"
31 #include "wx/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 OnHeaderRightClick( wxDataViewEvent
&event
);
115 void OnSorted( wxDataViewEvent
&event
);
117 void OnContextMenu( wxDataViewEvent
&event
);
119 void OnRightClick( wxMouseEvent
&event
);
120 void OnGoto( wxCommandEvent
&event
);
121 void OnAddMany( wxCommandEvent
&event
);
123 void OnBeginDrag( wxDataViewEvent
&event
);
124 void OnDropPossible( wxDataViewEvent
&event
);
125 void OnDrop( wxDataViewEvent
&event
);
128 wxNotebook
* m_notebook
;
130 // the controls stored in the various tabs of the main notebook:
132 wxDataViewCtrl
* m_ctrl
[4];
134 // the models associated with the first two DVC:
136 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
137 wxObjectDataPtr
<MyListModel
> m_list_model
;
141 wxDataViewColumn
* m_col
;
147 DECLARE_EVENT_TABLE()
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 class MyCustomRenderer
: public wxDataViewCustomRenderer
159 : wxDataViewCustomRenderer("string",
160 wxDATAVIEW_CELL_ACTIVATABLE
,
164 virtual bool Render( wxRect rect
, wxDC
*dc
, int state
)
166 dc
->SetBrush( *wxLIGHT_GREY_BRUSH
);
167 dc
->SetPen( *wxTRANSPARENT_PEN
);
170 dc
->DrawRoundedRectangle( rect
, 5 );
174 wxRect(dc
->GetTextExtent(m_value
)).CentreIn(rect
),
180 virtual bool Activate( wxRect
WXUNUSED(cell
),
181 wxDataViewModel
*WXUNUSED(model
),
182 const wxDataViewItem
&WXUNUSED(item
),
183 unsigned int WXUNUSED(col
) )
185 wxLogMessage( "MyCustomRenderer Activate()" );
189 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
190 wxDataViewModel
*WXUNUSED(model
),
191 const wxDataViewItem
&WXUNUSED(item
),
192 unsigned int WXUNUSED(col
) )
194 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
198 virtual wxSize
GetSize() const
200 return wxSize(60,20);
203 virtual bool SetValue( const wxVariant
&value
)
205 m_value
= value
.GetString();
209 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
216 // ============================================================================
218 // ============================================================================
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
228 if ( !wxApp::OnInit() )
232 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
246 ID_CLEARLOG
= wxID_HIGHEST
+1,
247 ID_BACKGROUND_COLOUR
,
248 ID_FOREGROUND_COLOUR
,
252 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
261 ID_ABOUT
= wxID_ABOUT
,
269 ID_DELETE_MUSIC
= 101,
270 ID_DELETE_YEAR
= 102,
271 ID_SELECT_NINTH
= 103,
275 ID_PREPEND_LIST
= 200,
276 ID_DELETE_LIST
= 201,
280 ID_DELETE_TREE_ITEM
= 400,
281 ID_DELETE_ALL_TREE_ITEMS
= 401,
282 ID_ADD_TREE_ITEM
= 402,
283 ID_ADD_TREE_CONTAINER_ITEM
= 403
286 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
287 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
288 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
289 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
290 EVT_MENU( ID_CLEARLOG
, MyFrame::OnClearLog
)
292 EVT_MENU( ID_FOREGROUND_COLOUR
, MyFrame::OnSetForegroundColour
)
293 EVT_MENU( ID_BACKGROUND_COLOUR
, MyFrame::OnSetBackgroundColour
)
295 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
297 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
298 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
299 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
300 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
301 EVT_BUTTON( ID_COLLAPSE
, MyFrame::OnCollapse
)
302 EVT_BUTTON( ID_EXPAND
, MyFrame::OnExpand
)
304 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
305 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
306 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
307 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
309 EVT_BUTTON( ID_DELETE_TREE_ITEM
, MyFrame::OnDeleteTreeItem
)
310 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS
, MyFrame::OnDeleteAllTreeItems
)
311 EVT_BUTTON( ID_ADD_TREE_ITEM
, MyFrame::OnAddTreeItem
)
312 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM
, MyFrame::OnAddTreeContainerItem
)
314 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
316 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
317 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
318 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
319 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
320 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
321 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
323 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL
, MyFrame::OnStartEditing
)
324 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
325 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
327 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
328 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
329 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
331 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
333 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
334 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
335 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
337 EVT_RIGHT_UP(MyFrame::OnRightClick
)
340 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
341 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
351 SetIcon(wxICON(sample
));
357 wxMenu
*style_menu
= new wxMenu
;
358 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
359 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
360 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
361 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
362 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
364 wxMenu
*file_menu
= new wxMenu
;
365 file_menu
->Append(ID_CLEARLOG
, "&Clear log\tCtrl-L");
366 file_menu
->Append(ID_FOREGROUND_COLOUR
, "Set &foreground colour...\tCtrl-S");
367 file_menu
->Append(ID_BACKGROUND_COLOUR
, "Set &background colour...\tCtrl-B");
368 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
369 file_menu
->AppendSeparator();
370 file_menu
->Append(ID_EXIT
, "E&xit");
372 wxMenu
*about_menu
= new wxMenu
;
373 about_menu
->Append(ID_ABOUT
, "&About");
375 wxMenuBar
*menu_bar
= new wxMenuBar
;
376 menu_bar
->Append(file_menu
, "&File");
377 menu_bar
->Append(about_menu
, "&About");
379 SetMenuBar(menu_bar
);
383 // first page of the notebook
384 // --------------------------
386 m_notebook
= new wxNotebook( this, wxID_ANY
);
388 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
390 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
392 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
393 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
394 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_MUSIC
,"Delete selected"), 0, wxALL
, 10 );
395 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), 0, wxALL
, 10 );
396 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), 0, wxALL
, 10 );
397 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), 0, wxALL
, 10 );
398 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), 0, wxALL
, 10 );
400 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
401 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
402 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
404 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
406 firstPanelSz
->Add(button_sizer
);
407 firstPanel
->SetSizerAndFit(firstPanelSz
);
410 // second page of the notebook
411 // ---------------------------
413 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
415 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
417 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
418 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
419 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
420 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
421 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
423 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
424 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
425 secondPanelSz
->Add(button_sizer2
);
426 secondPanel
->SetSizerAndFit(secondPanelSz
);
429 // third page of the notebook
430 // ---------------------------
432 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
434 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
436 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
437 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
438 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
441 // fourth page of the notebook
442 // ---------------------------
444 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
446 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
448 wxBoxSizer
*button_sizer4
= new wxBoxSizer( wxHORIZONTAL
);
449 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_TREE_ITEM
, "Delete Selected"), 0, wxALL
, 10 );
450 button_sizer4
->Add( new wxButton( fourthPanel
, ID_DELETE_ALL_TREE_ITEMS
, "Delete All"), 0, wxALL
, 10 );
451 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_ITEM
, "Add Item"), 0, wxALL
, 10 );
452 button_sizer4
->Add( new wxButton( fourthPanel
, ID_ADD_TREE_CONTAINER_ITEM
, "Add Container"), 0, wxALL
, 10 );
454 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
455 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
456 fourthPanelSz
->Add(button_sizer4
);
457 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
464 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
465 m_notebook
->AddPage(secondPanel
, "MyListModel");
466 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
467 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
469 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
471 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
472 wxDefaultSize
, wxTE_MULTILINE
);
473 m_log
->SetMinSize(wxSize(-1, 100));
474 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
475 wxLogMessage( "This is the log window" );
477 mainSizer
->Add( m_notebook
, 1, wxGROW
);
478 mainSizer
->Add( m_log
, 0, wxGROW
);
480 SetSizerAndFit(mainSizer
);
485 delete wxLog::SetActiveTarget(m_logOld
);
488 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
494 wxASSERT(!m_ctrl
[0] && !m_music_model
);
496 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
497 wxDefaultSize
, style
);
499 m_music_model
= new MyMusicTreeModel
;
500 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
502 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
503 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
505 // column 0 of the view control:
507 wxDataViewTextRenderer
*tr
=
508 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
509 wxDataViewColumn
*column0
=
510 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
511 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
512 m_ctrl
[0]->AppendColumn( column0
);
514 // Call this and sorting is enabled
515 // immediatly upon start up.
516 column0
->SetAsSortKey();
519 // column 1 of the view control:
521 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
522 wxDataViewColumn
*column1
=
523 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
524 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
525 wxDATAVIEW_COL_RESIZABLE
);
526 column1
->SetMinWidth(150); // this column can't be resized to be smaller
527 m_ctrl
[0]->AppendColumn( column1
);
529 // column 2 of the view control:
531 wxDataViewSpinRenderer
*sr
=
532 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
533 wxDataViewColumn
*column2
=
534 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
535 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
536 m_ctrl
[0]->AppendColumn( column2
);
538 // column 3 of the view control:
540 wxArrayString choices
;
541 choices
.Add( "good" );
542 choices
.Add( "bad" );
543 choices
.Add( "lousy" );
544 wxDataViewChoiceRenderer
*c
=
545 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
546 wxDataViewColumn
*column3
=
547 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
548 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
549 m_ctrl
[0]->AppendColumn( column3
);
551 // column 4 of the view control:
553 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
555 // column 5 of the view control:
557 MyCustomRenderer
*cr
= new MyCustomRenderer
;
558 wxDataViewColumn
*column5
=
559 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
560 wxDATAVIEW_COL_RESIZABLE
);
561 m_ctrl
[0]->AppendColumn( column5
);
564 // select initially the ninth symphony:
565 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
571 wxASSERT(!m_ctrl
[1] && !m_list_model
);
572 m_ctrl
[1] = new wxDataViewCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
573 wxDefaultSize
, style
);
575 m_list_model
= new MyListModel
;
576 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
578 // the various columns
579 m_ctrl
[1]->AppendTextColumn("editable string",
580 MyListModel::Col_EditableText
,
581 wxDATAVIEW_CELL_EDITABLE
);
582 m_ctrl
[1]->AppendIconTextColumn("icon",
583 MyListModel::Col_IconText
,
584 wxDATAVIEW_CELL_EDITABLE
);
585 m_ctrl
[1]->AppendColumn(
586 new wxDataViewColumn("attributes",
587 new wxDataViewTextRenderer
,
588 MyListModel::Col_TextWithAttr
)
591 m_ctrl
[1]->AppendColumn(
592 new wxDataViewColumn("custom renderer",
593 new MyCustomRenderer
,
594 MyListModel::Col_Custom
)
601 wxASSERT(!m_ctrl
[2]);
602 wxDataViewListCtrl
* lc
=
603 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
604 wxDefaultSize
, style
);
607 lc
->AppendToggleColumn( "Toggle" );
608 lc
->AppendTextColumn( "Text" );
609 lc
->AppendProgressColumn( "Progress" );
611 wxVector
<wxVariant
> data
;
612 for (unsigned int i
=0; i
<10; i
++)
615 data
.push_back( (i%3
) == 0 );
616 data
.push_back( wxString::Format("row %d", i
) );
617 data
.push_back( long(5*i
) );
619 lc
->AppendItem( data
);
626 wxASSERT(!m_ctrl
[3]);
627 wxDataViewTreeCtrl
* tc
=
628 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
629 wxDefaultSize
, style
| wxDV_NO_HEADER
);
632 wxImageList
*ilist
= new wxImageList( 16, 16 );
633 ilist
->Add( wxIcon(wx_small_xpm
) );
634 tc
->SetImageList( ilist
);
636 wxDataViewItem parent
=
637 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
638 tc
->AppendItem( parent
, "Child 1", 0 );
639 tc
->AppendItem( parent
, "Child 2", 0 );
640 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
642 wxDataViewItem cont
=
643 tc
->AppendContainer( parent
, "Container child", 0 );
644 tc
->AppendItem( cont
, "Child 4", 0 );
645 tc
->AppendItem( cont
, "Child 5", 0 );
654 // ----------------------------------------------------------------------------
655 // MyFrame - generic event handlers
656 // ----------------------------------------------------------------------------
658 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
663 void MyFrame::OnSetForegroundColour(wxCommandEvent
& WXUNUSED(event
))
665 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
666 wxColour col
= wxGetColourFromUser(this, dvc
->GetForegroundColour());
669 dvc
->SetForegroundColour(col
);
674 void MyFrame::OnSetBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
676 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
677 wxColour col
= wxGetColourFromUser(this, dvc
->GetBackgroundColour());
680 dvc
->SetBackgroundColour(col
);
685 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
687 unsigned int nPanel
= m_notebook
->GetSelection();
689 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
690 wxString::Format("Style of panel #%d", nPanel
+1));
692 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
694 unsigned long style
= 0;
701 style
= wxDV_MULTIPLE
;
704 style
= wxDV_ROW_LINES
;
707 style
= wxDV_HORIZ_RULES
;
710 style
= wxDV_VERT_RULES
;
716 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
720 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
722 unsigned int nPanel
= m_notebook
->GetSelection();
725 unsigned long style
= 0;
726 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
727 style |= wxDV_SINGLE;*/
728 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
729 style
|= wxDV_MULTIPLE
;
730 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
731 style
|= wxDV_ROW_LINES
;
732 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
733 style
|= wxDV_HORIZ_RULES
;
734 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
735 style
|= wxDV_VERT_RULES
;
737 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
740 sz
->Detach(m_ctrl
[nPanel
]);
741 wxDELETE(m_ctrl
[nPanel
]);
742 m_ctrl
[nPanel
] = NULL
;
745 m_music_model
.reset(NULL
);
746 else if (nPanel
== 1)
747 m_list_model
.reset(NULL
);
749 // rebuild the DVC for the selected panel:
750 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
752 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
756 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
761 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
763 wxAboutDialogInfo info
;
764 info
.SetName(_("DataView sample"));
765 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
766 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
767 info
.AddDeveloper("Robert Roebling");
768 info
.AddDeveloper("Francesco Montorsi");
774 // ----------------------------------------------------------------------------
775 // MyFrame - event handlers for the first page
776 // ----------------------------------------------------------------------------
778 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
780 wxDataViewItem
item( event
.GetItem() );
782 // only allow drags for item, not containers
783 if (m_music_model
->IsContainer( item
) )
789 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
790 wxTextDataObject
*obj
= new wxTextDataObject
;
791 obj
->SetText( node
->m_title
);
792 event
.SetDataObject( obj
);
795 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
797 wxDataViewItem
item( event
.GetItem() );
799 // only allow drags for item, not containers
800 if (m_music_model
->IsContainer( item
) )
803 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
807 void MyFrame::OnDrop( wxDataViewEvent
&event
)
809 wxDataViewItem
item( event
.GetItem() );
811 // only allow drops for item, not containers
812 if (m_music_model
->IsContainer( item
) )
818 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
824 wxTextDataObject obj
;
825 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
827 wxLogMessage( "Text dropped: %s", obj
.GetText() );
830 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
832 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
835 void MyFrame::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
837 wxDataViewItemArray items
;
838 int len
= m_ctrl
[0]->GetSelections( items
);
839 for( int i
= 0; i
< len
; i
++ )
841 m_music_model
->Delete( items
[i
] );
844 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
846 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
847 FindWindow( ID_DELETE_YEAR
)->Disable();
850 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
852 if (!m_music_model
->GetNinthItem().IsOk())
854 wxLogError( "Cannot select the ninth symphony: it was removed!" );
858 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
861 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
863 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
865 m_ctrl
[0]->Collapse( item
);
868 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
870 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
872 m_ctrl
[0]->Expand( item
);
875 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
880 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d",
881 event
.GetItem().GetID(), event
.GetColumn() );
884 void MyFrame::OnActivated( wxDataViewEvent
&event
)
889 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
890 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
892 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
894 wxLogMessage( "Item: %s is expanded", title
);
898 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
903 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
907 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
910 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
915 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
916 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
920 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
922 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
923 if (artist
== "Ludwig van Beethoven")
930 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
933 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
937 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
942 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
943 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
946 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
951 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
952 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
955 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
960 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
961 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
964 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
969 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
970 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
973 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
978 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
979 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
982 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
987 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
988 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
991 menu
.Append( 1, "menuitem 1" );
992 menu
.Append( 2, "menuitem 2" );
993 menu
.Append( 3, "menuitem 3" );
995 m_ctrl
[0]->PopupMenu(&menu
);
998 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
1000 // we need to skip the event to let the default behaviour of sorting by
1001 // this column when it is clicked to take place
1007 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1009 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
1010 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
1013 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
1018 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1020 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
1023 void MyFrame::OnSorted( wxDataViewEvent
&event
)
1028 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
1030 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
1033 void MyFrame::OnRightClick( wxMouseEvent
&event
)
1038 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
1039 event
.GetX(), event
.GetY() );
1043 // ----------------------------------------------------------------------------
1044 // MyFrame - event handlers for the second page
1045 // ----------------------------------------------------------------------------
1047 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1049 m_list_model
->Prepend("Test");
1052 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1054 wxDataViewItemArray items
;
1055 int len
= m_ctrl
[1]->GetSelections( items
);
1057 m_list_model
->DeleteItems( items
);
1060 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1062 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1063 m_ctrl
[1]->EnsureVisible(item
,m_col
);
1066 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1068 m_list_model
->AddMany();
1071 // ----------------------------------------------------------------------------
1072 // MyFrame - event handlers for the fourth page
1073 // ----------------------------------------------------------------------------
1075 void MyFrame::OnDeleteTreeItem(wxCommandEvent
& WXUNUSED(event
))
1077 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1078 wxDataViewItem selected
= ctrl
->GetSelection();
1079 if (!selected
.IsOk())
1082 ctrl
->DeleteItem(selected
);
1085 void MyFrame::OnDeleteAllTreeItems(wxCommandEvent
& WXUNUSED(event
))
1087 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1088 ctrl
->DeleteAllItems();
1091 void MyFrame::OnAddTreeItem(wxCommandEvent
& WXUNUSED(event
))
1093 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1094 wxDataViewItem selected
= ctrl
->GetSelection();
1095 if (ctrl
->IsContainer(selected
))
1096 ctrl
->AppendItem( selected
, "Item", 0 );
1099 void MyFrame::OnAddTreeContainerItem(wxCommandEvent
& WXUNUSED(event
))
1101 wxDataViewTreeCtrl
* ctrl
= (wxDataViewTreeCtrl
*) m_ctrl
[3];
1102 wxDataViewItem selected
= ctrl
->GetSelection();
1103 if (ctrl
->IsContainer(selected
))
1104 ctrl
->AppendContainer(selected
, "Container", 0 );