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 OnValueChanged( wxDataViewEvent
&event
);
97 void OnActivated( wxDataViewEvent
&event
);
98 void OnExpanding( wxDataViewEvent
&event
);
99 void OnExpanded( wxDataViewEvent
&event
);
100 void OnCollapsing( wxDataViewEvent
&event
);
101 void OnCollapsed( wxDataViewEvent
&event
);
102 void OnSelectionChanged( wxDataViewEvent
&event
);
104 void OnStartEditing( wxDataViewEvent
&event
);
105 void OnEditingStarted( wxDataViewEvent
&event
);
106 void OnEditingDone( wxDataViewEvent
&event
);
108 void OnHeaderClick( wxDataViewEvent
&event
);
109 void OnHeaderRightClick( wxDataViewEvent
&event
);
110 void OnSorted( wxDataViewEvent
&event
);
112 void OnContextMenu( wxDataViewEvent
&event
);
114 void OnRightClick( wxMouseEvent
&event
);
115 void OnGoto( wxCommandEvent
&event
);
116 void OnAddMany( wxCommandEvent
&event
);
118 void OnBeginDrag( wxDataViewEvent
&event
);
119 void OnDropPossible( wxDataViewEvent
&event
);
120 void OnDrop( wxDataViewEvent
&event
);
123 wxNotebook
* m_notebook
;
125 // the controls stored in the various tabs of the main notebook:
127 wxDataViewCtrl
* m_ctrl
[4];
129 // the models associated with the first two DVC:
131 wxObjectDataPtr
<MyMusicTreeModel
> m_music_model
;
132 wxObjectDataPtr
<MyListModel
> m_list_model
;
136 wxDataViewColumn
* m_col
;
142 DECLARE_EVENT_TABLE()
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 class MyCustomRenderer
: public wxDataViewCustomRenderer
153 MyCustomRenderer( wxDataViewCellMode mode
, int alignment
) :
154 wxDataViewCustomRenderer( wxString("long"), mode
, alignment
)
157 virtual bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
159 dc
->SetBrush( *wxRED_BRUSH
);
160 dc
->SetPen( *wxTRANSPARENT_PEN
);
161 dc
->DrawRectangle( rect
.Deflate(2) );
165 virtual bool Activate( wxRect
WXUNUSED(cell
),
166 wxDataViewModel
*WXUNUSED(model
),
167 const wxDataViewItem
&WXUNUSED(item
),
168 unsigned int WXUNUSED(col
) )
170 wxLogMessage( "MyCustomRenderer Activate()" );
174 virtual bool LeftClick( wxPoint cursor
, wxRect
WXUNUSED(cell
),
175 wxDataViewModel
*WXUNUSED(model
),
176 const wxDataViewItem
&WXUNUSED(item
),
177 unsigned int WXUNUSED(col
) )
179 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor
.x
, cursor
.y
);
183 virtual wxSize
GetSize() const
185 //return wxSize(60,m_height);
186 return wxSize(60,20);
189 virtual bool SetValue( const wxVariant
&value
)
195 virtual bool GetValue( wxVariant
&WXUNUSED(value
) ) const { return true; }
202 // ============================================================================
204 // ============================================================================
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
214 if ( !wxApp::OnInit() )
218 new MyFrame(NULL
, "wxDataViewCtrl sample", 40, 40, 1000, 540);
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
232 ID_CLEARLOG
= wxID_HIGHEST
+1,
233 ID_BACKGROUND_COLOUR
,
234 ID_FOREGROUND_COLOUR
,
238 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
247 ID_ABOUT
= wxID_ABOUT
,
255 ID_DELETE_MUSIC
= 101,
256 ID_DELETE_YEAR
= 102,
257 ID_SELECT_NINTH
= 103,
261 ID_PREPEND_LIST
= 200,
262 ID_DELETE_LIST
= 201,
267 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
268 EVT_MENU_RANGE( ID_MULTIPLE
, ID_VERT_RULES
, MyFrame::OnStyleChange
)
269 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
270 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
271 EVT_MENU( ID_CLEARLOG
, MyFrame::OnClearLog
)
273 EVT_MENU( ID_FOREGROUND_COLOUR
, MyFrame::OnSetForegroundColour
)
274 EVT_MENU( ID_BACKGROUND_COLOUR
, MyFrame::OnSetBackgroundColour
)
276 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY
, MyFrame::OnPageChanged
)
278 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
279 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
280 EVT_BUTTON( ID_DELETE_YEAR
, MyFrame::OnDeleteYear
)
281 EVT_BUTTON( ID_SELECT_NINTH
, MyFrame::OnSelectNinth
)
282 EVT_BUTTON( ID_COLLAPSE
, MyFrame::OnCollapse
)
283 EVT_BUTTON( ID_EXPAND
, MyFrame::OnExpand
)
285 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
286 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
287 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
288 EVT_BUTTON( ID_ADD_MANY
, MyFrame::OnAddMany
)
290 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
292 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
293 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL
, MyFrame::OnExpanding
)
294 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL
, MyFrame::OnExpanded
)
295 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL
, MyFrame::OnCollapsing
)
296 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL
, MyFrame::OnCollapsed
)
297 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL
, MyFrame::OnSelectionChanged
)
299 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL
, MyFrame::OnStartEditing
)
300 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL
, MyFrame::OnEditingStarted
)
301 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL
, MyFrame::OnEditingDone
)
303 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
304 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
305 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
307 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL
, MyFrame::OnContextMenu
)
309 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL
, MyFrame::OnBeginDrag
)
310 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL
, MyFrame::OnDropPossible
)
311 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL
, MyFrame::OnDrop
)
313 EVT_RIGHT_UP(MyFrame::OnRightClick
)
316 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
):
317 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
327 SetIcon(wxICON(sample
));
333 wxMenu
*style_menu
= new wxMenu
;
334 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
335 style_menu
->AppendCheckItem(ID_MULTIPLE
, "Multiple selection");
336 style_menu
->AppendCheckItem(ID_ROW_LINES
, "Alternating colours");
337 style_menu
->AppendCheckItem(ID_HORIZ_RULES
, "Display horizontal rules");
338 style_menu
->AppendCheckItem(ID_VERT_RULES
, "Display vertical rules");
340 wxMenu
*file_menu
= new wxMenu
;
341 file_menu
->Append(ID_CLEARLOG
, "&Clear log\tCtrl-L");
342 file_menu
->Append(ID_FOREGROUND_COLOUR
, "Set &foreground colour...\tCtrl-F");
343 file_menu
->Append(ID_BACKGROUND_COLOUR
, "Set &background colour...\tCtrl-B");
344 file_menu
->Append(ID_STYLE_MENU
, "&Style", style_menu
);
345 file_menu
->AppendSeparator();
346 file_menu
->Append(ID_EXIT
, "E&xit");
348 wxMenu
*about_menu
= new wxMenu
;
349 about_menu
->Append(ID_ABOUT
, "&About");
351 wxMenuBar
*menu_bar
= new wxMenuBar
;
352 menu_bar
->Append(file_menu
, "&File");
353 menu_bar
->Append(about_menu
, "&About");
355 SetMenuBar(menu_bar
);
359 // first page of the notebook
360 // --------------------------
362 m_notebook
= new wxNotebook( this, wxID_ANY
);
364 wxPanel
*firstPanel
= new wxPanel( m_notebook
, wxID_ANY
);
366 BuildDataViewCtrl(firstPanel
, 0); // sets m_ctrl[0]
368 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
369 button_sizer
->Add( new wxButton( firstPanel
, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
370 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_MUSIC
,"Delete selected"), 0, wxALL
, 10 );
371 button_sizer
->Add( new wxButton( firstPanel
, ID_DELETE_YEAR
, "Delete \"Year\" column"), 0, wxALL
, 10 );
372 button_sizer
->Add( new wxButton( firstPanel
, ID_SELECT_NINTH
,"Select ninth symphony"), 0, wxALL
, 10 );
373 button_sizer
->Add( new wxButton( firstPanel
, ID_COLLAPSE
, "Collapse"), 0, wxALL
, 10 );
374 button_sizer
->Add( new wxButton( firstPanel
, ID_EXPAND
, "Expand"), 0, wxALL
, 10 );
376 wxSizer
*firstPanelSz
= new wxBoxSizer( wxVERTICAL
);
377 m_ctrl
[0]->SetMinSize(wxSize(-1, 200));
378 firstPanelSz
->Add(m_ctrl
[0], 1, wxGROW
|wxALL
, 5);
380 new wxStaticText(firstPanel
, wxID_ANY
, "Most of the cells above are editable!"),
382 firstPanelSz
->Add(button_sizer
);
383 firstPanel
->SetSizerAndFit(firstPanelSz
);
386 // second page of the notebook
387 // ---------------------------
389 wxPanel
*secondPanel
= new wxPanel( m_notebook
, wxID_ANY
);
391 BuildDataViewCtrl(secondPanel
, 1); // sets m_ctrl[1]
393 wxBoxSizer
*button_sizer2
= new wxBoxSizer( wxHORIZONTAL
);
394 button_sizer2
->Add( new wxButton( secondPanel
, ID_PREPEND_LIST
,"Prepend"), 0, wxALL
, 10 );
395 button_sizer2
->Add( new wxButton( secondPanel
, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
396 button_sizer2
->Add( new wxButton( secondPanel
, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
397 button_sizer2
->Add( new wxButton( secondPanel
, ID_ADD_MANY
, "Add 1000"), 0, wxALL
, 10 );
399 wxSizer
*secondPanelSz
= new wxBoxSizer( wxVERTICAL
);
400 secondPanelSz
->Add(m_ctrl
[1], 1, wxGROW
|wxALL
, 5);
401 secondPanelSz
->Add(button_sizer2
);
402 secondPanel
->SetSizerAndFit(secondPanelSz
);
405 // third page of the notebook
406 // ---------------------------
408 wxPanel
*thirdPanel
= new wxPanel( m_notebook
, wxID_ANY
);
410 BuildDataViewCtrl(thirdPanel
, 2); // sets m_ctrl[2]
412 wxSizer
*thirdPanelSz
= new wxBoxSizer( wxVERTICAL
);
413 thirdPanelSz
->Add(m_ctrl
[2], 1, wxGROW
|wxALL
, 5);
414 thirdPanel
->SetSizerAndFit(thirdPanelSz
);
417 // fourth page of the notebook
418 // ---------------------------
420 wxPanel
*fourthPanel
= new wxPanel( m_notebook
, wxID_ANY
);
422 BuildDataViewCtrl(fourthPanel
, 3); // sets m_ctrl[3]
424 wxSizer
*fourthPanelSz
= new wxBoxSizer( wxVERTICAL
);
425 fourthPanelSz
->Add(m_ctrl
[3], 1, wxGROW
|wxALL
, 5);
426 fourthPanel
->SetSizerAndFit(fourthPanelSz
);
433 m_notebook
->AddPage(firstPanel
, "MyMusicTreeModel");
434 m_notebook
->AddPage(secondPanel
, "MyListModel");
435 m_notebook
->AddPage(thirdPanel
, "wxDataViewListCtrl");
436 m_notebook
->AddPage(fourthPanel
, "wxDataViewTreeCtrl");
438 wxSizer
* mainSizer
= new wxBoxSizer(wxVERTICAL
);
440 m_log
= new wxTextCtrl( this, wxID_ANY
, wxString(), wxDefaultPosition
,
441 wxDefaultSize
, wxTE_MULTILINE
);
442 m_log
->SetMinSize(wxSize(-1, 100));
443 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
444 wxLogMessage( "This is the log window" );
446 mainSizer
->Add( m_notebook
, 1, wxGROW
);
447 mainSizer
->Add( m_log
, 0, wxGROW
);
449 SetSizerAndFit(mainSizer
);
454 delete wxLog::SetActiveTarget(m_logOld
);
457 void MyFrame::BuildDataViewCtrl(wxPanel
* parent
, unsigned int nPanel
, unsigned long style
)
463 wxASSERT(!m_ctrl
[0] && !m_music_model
);
465 new wxDataViewCtrl( parent
, ID_MUSIC_CTRL
, wxDefaultPosition
,
466 wxDefaultSize
, style
);
468 m_music_model
= new MyMusicTreeModel
;
469 m_ctrl
[0]->AssociateModel( m_music_model
.get() );
471 m_ctrl
[0]->EnableDragSource( wxDF_UNICODETEXT
);
472 m_ctrl
[0]->EnableDropTarget( wxDF_UNICODETEXT
);
474 // column 0 of the view control:
476 wxDataViewTextRenderer
*tr
=
477 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT
);
478 wxDataViewColumn
*column0
=
479 new wxDataViewColumn( "title", tr
, 0, 200, wxALIGN_LEFT
,
480 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_RESIZABLE
);
481 m_ctrl
[0]->AppendColumn( column0
);
483 // Call this and sorting is enabled
484 // immediatly upon start up.
485 column0
->SetAsSortKey();
488 // column 1 of the view control:
490 tr
= new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE
);
491 wxDataViewColumn
*column1
=
492 new wxDataViewColumn( "artist", tr
, 1, 150, wxALIGN_LEFT
,
493 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
|
494 wxDATAVIEW_COL_RESIZABLE
);
495 column1
->SetMinWidth(150); // this column can't be resized to be smaller
496 m_ctrl
[0]->AppendColumn( column1
);
498 // column 2 of the view control:
500 wxDataViewSpinRenderer
*sr
=
501 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
502 wxDataViewColumn
*column2
=
503 new wxDataViewColumn( "year", sr
, 2, 60, wxALIGN_LEFT
,
504 wxDATAVIEW_COL_SORTABLE
| wxDATAVIEW_COL_REORDERABLE
);
505 m_ctrl
[0]->AppendColumn( column2
);
507 // column 3 of the view control:
509 wxArrayString choices
;
510 choices
.Add( "good" );
511 choices
.Add( "bad" );
512 choices
.Add( "lousy" );
513 wxDataViewChoiceRenderer
*c
=
514 new wxDataViewChoiceRenderer( choices
, wxDATAVIEW_CELL_EDITABLE
, wxALIGN_RIGHT
);
515 wxDataViewColumn
*column3
=
516 new wxDataViewColumn( "rating", c
, 3, 100, wxALIGN_LEFT
,
517 wxDATAVIEW_COL_REORDERABLE
| wxDATAVIEW_COL_RESIZABLE
);
518 m_ctrl
[0]->AppendColumn( column3
);
520 // column 4 of the view control:
522 m_ctrl
[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT
, 80 );
524 // column 5 of the view control:
526 MyCustomRenderer
*cr
= new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE
, wxALIGN_RIGHT
);
527 wxDataViewColumn
*column5
=
528 new wxDataViewColumn( "custom", cr
, 5, -1, wxALIGN_LEFT
,
529 wxDATAVIEW_COL_RESIZABLE
);
530 m_ctrl
[0]->AppendColumn( column5
);
533 // select initially the ninth symphony:
534 m_ctrl
[0]->Select(m_music_model
->GetNinthItem());
540 wxASSERT(!m_ctrl
[1] && !m_list_model
);
541 m_ctrl
[1] = new wxDataViewCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
542 wxDefaultSize
, style
);
544 m_list_model
= new MyListModel
;
545 m_ctrl
[1]->AssociateModel( m_list_model
.get() );
547 // the various columns
548 m_ctrl
[1]->AppendTextColumn("editable string", 0, wxDATAVIEW_CELL_EDITABLE
);
549 m_ctrl
[1]->AppendIconTextColumn("icon", 1, wxDATAVIEW_CELL_EDITABLE
);
550 m_ctrl
[1]->AppendColumn(
551 new wxDataViewColumn("attributes", new wxDataViewTextRenderer
, 2 ));
557 wxASSERT(!m_ctrl
[2]);
558 wxDataViewListCtrl
* lc
=
559 new wxDataViewListCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
560 wxDefaultSize
, style
);
563 lc
->AppendToggleColumn( "Toggle" );
564 lc
->AppendTextColumn( "Text" );
565 lc
->AppendProgressColumn( "Progress" );
567 wxVector
<wxVariant
> data
;
568 for (unsigned int i
=0; i
<10; i
++)
571 data
.push_back( (i%3
) == 0 );
572 data
.push_back( wxString::Format("row %d", i
) );
573 data
.push_back( long(5*i
) );
575 lc
->AppendItem( data
);
582 wxASSERT(!m_ctrl
[3]);
583 wxDataViewTreeCtrl
* tc
=
584 new wxDataViewTreeCtrl( parent
, wxID_ANY
, wxDefaultPosition
,
585 wxDefaultSize
, style
);
588 wxImageList
*ilist
= new wxImageList( 16, 16 );
589 ilist
->Add( wxIcon(wx_small_xpm
) );
590 tc
->SetImageList( ilist
);
592 wxDataViewItem parent
=
593 tc
->AppendContainer( wxDataViewItem(0), "The Root", 0 );
594 tc
->AppendItem( parent
, "Child 1", 0 );
595 tc
->AppendItem( parent
, "Child 2", 0 );
596 tc
->AppendItem( parent
, "Child 3, very long, long, long, long", 0 );
598 wxDataViewItem cont
=
599 tc
->AppendContainer( parent
, "Container child", 0 );
600 tc
->AppendItem( cont
, "Child 4", 0 );
601 tc
->AppendItem( cont
, "Child 5", 0 );
610 // ----------------------------------------------------------------------------
611 // MyFrame - generic event handlers
612 // ----------------------------------------------------------------------------
614 void MyFrame::OnClearLog( wxCommandEvent
& WXUNUSED(event
) )
619 void MyFrame::OnSetForegroundColour(wxCommandEvent
& WXUNUSED(event
))
621 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
622 wxColour col
= wxGetColourFromUser(this, dvc
->GetForegroundColour());
625 dvc
->SetForegroundColour(col
);
630 void MyFrame::OnSetBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
632 wxDataViewCtrl
* const dvc
= m_ctrl
[m_notebook
->GetSelection()];
633 wxColour col
= wxGetColourFromUser(this, dvc
->GetBackgroundColour());
636 dvc
->SetBackgroundColour(col
);
641 void MyFrame::OnPageChanged( wxBookCtrlEvent
& WXUNUSED(event
) )
643 unsigned int nPanel
= m_notebook
->GetSelection();
645 GetMenuBar()->FindItem(ID_STYLE_MENU
)->SetItemLabel(
646 wxString::Format("Style of panel #%d", nPanel
+1));
648 for (unsigned int id
= ID_MULTIPLE
; id
<= ID_VERT_RULES
; id
++)
650 unsigned long style
= 0;
657 style
= wxDV_MULTIPLE
;
660 style
= wxDV_ROW_LINES
;
663 style
= wxDV_HORIZ_RULES
;
666 style
= wxDV_VERT_RULES
;
672 GetMenuBar()->FindItem(id
)->Check( m_ctrl
[nPanel
]->HasFlag(style
) );
676 void MyFrame::OnStyleChange( wxCommandEvent
& WXUNUSED(event
) )
678 unsigned int nPanel
= m_notebook
->GetSelection();
681 unsigned long style
= 0;
682 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
683 style |= wxDV_SINGLE;*/
684 if (GetMenuBar()->FindItem(ID_MULTIPLE
)->IsChecked())
685 style
|= wxDV_MULTIPLE
;
686 if (GetMenuBar()->FindItem(ID_ROW_LINES
)->IsChecked())
687 style
|= wxDV_ROW_LINES
;
688 if (GetMenuBar()->FindItem(ID_HORIZ_RULES
)->IsChecked())
689 style
|= wxDV_HORIZ_RULES
;
690 if (GetMenuBar()->FindItem(ID_VERT_RULES
)->IsChecked())
691 style
|= wxDV_VERT_RULES
;
693 wxSizer
* sz
= m_ctrl
[nPanel
]->GetContainingSizer();
696 sz
->Detach(m_ctrl
[nPanel
]);
697 wxDELETE(m_ctrl
[nPanel
]);
698 m_ctrl
[nPanel
] = NULL
;
701 m_music_model
.reset(NULL
);
702 else if (nPanel
== 1)
703 m_list_model
.reset(NULL
);
705 // rebuild the DVC for the selected panel:
706 BuildDataViewCtrl((wxPanel
*)m_notebook
->GetPage(nPanel
), nPanel
, style
);
708 sz
->Prepend(m_ctrl
[nPanel
], 1, wxGROW
|wxALL
, 5);
712 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
717 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
719 wxAboutDialogInfo info
;
720 info
.SetName(_("DataView sample"));
721 info
.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
722 info
.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
723 info
.AddDeveloper("Robert Roebling");
724 info
.AddDeveloper("Francesco Montorsi");
730 // ----------------------------------------------------------------------------
731 // MyFrame - event handlers for the first page
732 // ----------------------------------------------------------------------------
734 void MyFrame::OnBeginDrag( wxDataViewEvent
&event
)
736 wxDataViewItem
item( event
.GetItem() );
738 // only allow drags for item, not containers
739 if (m_music_model
->IsContainer( item
) )
745 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
746 wxTextDataObject
*obj
= new wxTextDataObject
;
747 obj
->SetText( node
->m_title
);
748 event
.SetDataObject( obj
);
751 void MyFrame::OnDropPossible( wxDataViewEvent
&event
)
753 wxDataViewItem
item( event
.GetItem() );
755 // only allow drags for item, not containers
756 if (m_music_model
->IsContainer( item
) )
759 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
763 void MyFrame::OnDrop( wxDataViewEvent
&event
)
765 wxDataViewItem
item( event
.GetItem() );
767 // only allow drops for item, not containers
768 if (m_music_model
->IsContainer( item
) )
774 if (event
.GetDataFormat() != wxDF_UNICODETEXT
)
780 wxTextDataObject obj
;
781 obj
.SetData( wxDF_UNICODETEXT
, event
.GetDataSize(), event
.GetDataBuffer() );
783 wxLogMessage( "Text dropped: %s", obj
.GetText() );
786 void MyFrame::OnAddMozart( wxCommandEvent
& WXUNUSED(event
) )
788 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
791 void MyFrame::OnDeleteMusic( wxCommandEvent
& WXUNUSED(event
) )
793 wxDataViewItemArray items
;
794 int len
= m_ctrl
[0]->GetSelections( items
);
795 for( int i
= 0; i
< len
; i
++ )
797 m_music_model
->Delete( items
[i
] );
800 void MyFrame::OnDeleteYear( wxCommandEvent
& WXUNUSED(event
) )
802 m_ctrl
[0]->DeleteColumn( m_ctrl
[0]->GetColumn( 2 ) );
803 FindWindow( ID_DELETE_YEAR
)->Disable();
806 void MyFrame::OnSelectNinth( wxCommandEvent
& WXUNUSED(event
) )
808 if (!m_music_model
->GetNinthItem().IsOk())
810 wxLogError( "Cannot select the ninth symphony: it was removed!" );
814 m_ctrl
[0]->Select( m_music_model
->GetNinthItem() );
817 void MyFrame::OnCollapse( wxCommandEvent
& WXUNUSED(event
) )
819 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
821 m_ctrl
[0]->Collapse( item
);
824 void MyFrame::OnExpand( wxCommandEvent
& WXUNUSED(event
) )
826 wxDataViewItem item
= m_ctrl
[0]->GetSelection();
828 m_ctrl
[0]->Expand( item
);
831 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
836 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d",
837 event
.GetItem().GetID(), event
.GetColumn() );
840 void MyFrame::OnActivated( wxDataViewEvent
&event
)
845 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
846 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title
);
848 if (m_ctrl
[0]->IsExpanded( event
.GetItem() ))
850 wxLogMessage( "Item: %s is expanded", title
);
854 void MyFrame::OnSelectionChanged( wxDataViewEvent
&event
)
859 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
863 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title
);
866 void MyFrame::OnExpanding( wxDataViewEvent
&event
)
871 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
872 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title
);
876 void MyFrame::OnStartEditing( wxDataViewEvent
&event
)
878 wxString artist
= m_music_model
->GetArtist( event
.GetItem() );
879 if (artist
== "Ludwig van Beethoven")
886 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist
);
889 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist
);
893 void MyFrame::OnEditingStarted( wxDataViewEvent
&event
)
898 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
899 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title
);
902 void MyFrame::OnEditingDone( wxDataViewEvent
&event
)
907 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
908 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title
);
911 void MyFrame::OnExpanded( wxDataViewEvent
&event
)
916 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
917 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title
);
920 void MyFrame::OnCollapsing( wxDataViewEvent
&event
)
925 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
926 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title
);
929 void MyFrame::OnCollapsed( wxDataViewEvent
&event
)
934 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
935 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title
);
938 void MyFrame::OnContextMenu( wxDataViewEvent
&event
)
943 wxString title
= m_music_model
->GetTitle( event
.GetItem() );
944 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title
);
947 menu
.Append( 1, "menuitem 1" );
948 menu
.Append( 2, "menuitem 2" );
949 menu
.Append( 3, "menuitem 3" );
951 m_ctrl
[0]->PopupMenu(&menu
);
954 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
956 // we need to skip the event to let the default behaviour of sorting by
957 // this column when it is clicked to take place
963 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
965 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos
);
966 wxLogMessage( "Column width: %d", event
.GetDataViewColumn()->GetWidth() );
969 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
974 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
976 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos
);
979 void MyFrame::OnSorted( wxDataViewEvent
&event
)
984 int pos
= m_ctrl
[0]->GetColumnPosition( event
.GetDataViewColumn() );
986 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos
);
989 void MyFrame::OnRightClick( wxMouseEvent
&event
)
994 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
995 event
.GetX(), event
.GetY() );
999 // ----------------------------------------------------------------------------
1000 // MyFrame - event handlers for the second page
1001 // ----------------------------------------------------------------------------
1003 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
1005 m_list_model
->Prepend("Test");
1008 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
1010 wxDataViewItemArray items
;
1011 int len
= m_ctrl
[1]->GetSelections( items
);
1013 m_list_model
->DeleteItems( items
);
1016 void MyFrame::OnGoto(wxCommandEvent
& WXUNUSED(event
))
1018 wxDataViewItem item
= m_list_model
->GetItem( 50 );
1019 m_ctrl
[1]->EnsureVisible(item
,m_col
);
1022 void MyFrame::OnAddMany(wxCommandEvent
& WXUNUSED(event
))
1024 m_list_model
->AddMany();