]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
Make it easier to define custom wxSizerXmlHandler subclasses.
[wxWidgets.git] / samples / dataview / dataview.cpp
CommitLineData
bd6169a6
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: dataview.cpp
9861f022 3// Purpose: wxDataViewCtrl wxWidgets sample
bd6169a6 4// Author: Robert Roebling
b7e9f8b1 5// Modified by: Francesco Montorsi, Bo Yang
bd6169a6
RR
6// Created: 06/01/06
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
526954c5 9// Licence: wxWindows licence
bd6169a6
RR
10/////////////////////////////////////////////////////////////////////////////
11
bf19b714
FM
12// ============================================================================
13// declarations
14// ============================================================================
15
bd6169a6
RR
16// For compilers that support precompilation, includes "wx/wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
0bcd4039 24 #include "wx/wx.h"
bd6169a6
RR
25#endif
26
8c2654ce 27#include "wx/dataview.h"
b910a8ad 28#include "wx/datetime.h"
87f0efe2
RR
29#include "wx/splitter.h"
30#include "wx/aboutdlg.h"
bb58fa37 31#include "wx/colordlg.h"
9861f022
RR
32#include "wx/choicdlg.h"
33#include "wx/numdlg.h"
1e510b1e 34#include "wx/spinctrl.h"
8c2654ce 35#include "wx/imaglist.h"
bf19b714
FM
36#include "wx/notebook.h"
37
38#include "mymodels.h"
39
40// ----------------------------------------------------------------------------
41// resources
42// ----------------------------------------------------------------------------
ad386793 43
bd6169a6 44#ifndef __WXMSW__
0bcd4039 45 #include "../sample.xpm"
bd6169a6
RR
46#endif
47
bf19b714 48#include "wx_small.xpm"
1e08ad10 49
bf19b714
FM
50// ----------------------------------------------------------------------------
51// MyApp
52// ----------------------------------------------------------------------------
1e08ad10 53
bf19b714 54class MyApp: public wxApp
1e08ad10
RR
55{
56public:
bf19b714 57 virtual bool OnInit();
1e08ad10
RR
58};
59
bf19b714
FM
60// ----------------------------------------------------------------------------
61// MyFrame
62// ----------------------------------------------------------------------------
1c3e52af 63
bf19b714 64class MyFrame : public wxFrame
bd6169a6
RR
65{
66public:
bf19b714
FM
67 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
68 ~MyFrame();
e63807a8 69
ecad59d8
FM
70 void BuildDataViewCtrl(wxPanel* parent,
71 unsigned int nPanel,
81c3f0fc
FM
72 unsigned long style = 0);
73
63ced01b
VZ
74private:
75 // event handlers
81c3f0fc 76 void OnStyleChange(wxCommandEvent& event);
bb58fa37
VZ
77 void OnSetBackgroundColour(wxCommandEvent& event);
78 void OnSetForegroundColour(wxCommandEvent& event);
bf19b714
FM
79 void OnQuit(wxCommandEvent& event);
80 void OnAbout(wxCommandEvent& event);
1c3e52af 81
ecad59d8 82 void OnClearLog(wxCommandEvent& event);
81c3f0fc
FM
83 void OnPageChanged(wxBookCtrlEvent& event);
84
bf19b714 85 void OnAddMozart(wxCommandEvent& event);
e0ff2295 86 void OnDeleteSelected(wxCommandEvent& event);
bf19b714
FM
87 void OnDeleteYear(wxCommandEvent& event);
88 void OnSelectNinth(wxCommandEvent& event);
d2ee27fe
RR
89 void OnCollapse(wxCommandEvent& event);
90 void OnExpand(wxCommandEvent& event);
80ce465c
VZ
91 void OnShowCurrent(wxCommandEvent& event);
92 void OnSetNinthCurrent(wxCommandEvent& event);
cf283a47 93
bf19b714
FM
94 void OnPrependList(wxCommandEvent& event);
95 void OnDeleteList(wxCommandEvent& event);
a46a47a3
RR
96 // Fourth page.
97 void OnDeleteTreeItem(wxCommandEvent& event);
98 void OnDeleteAllTreeItems(wxCommandEvent& event);
99 void OnAddTreeItem(wxCommandEvent& event);
100 void OnAddTreeContainerItem(wxCommandEvent& event);
1c3e52af 101
bf19b714 102 void OnValueChanged( wxDataViewEvent &event );
cf283a47 103
bf19b714
FM
104 void OnActivated( wxDataViewEvent &event );
105 void OnExpanding( wxDataViewEvent &event );
106 void OnExpanded( wxDataViewEvent &event );
107 void OnCollapsing( wxDataViewEvent &event );
108 void OnCollapsed( wxDataViewEvent &event );
109 void OnSelectionChanged( wxDataViewEvent &event );
1c3e52af 110
ecc32226 111 void OnStartEditing( wxDataViewEvent &event );
bf19b714
FM
112 void OnEditingStarted( wxDataViewEvent &event );
113 void OnEditingDone( wxDataViewEvent &event );
b910a8ad 114
bf19b714 115 void OnHeaderClick( wxDataViewEvent &event );
5d9e1605 116 void OnAttrHeaderClick( wxDataViewEvent &event );
bf19b714
FM
117 void OnHeaderRightClick( wxDataViewEvent &event );
118 void OnSorted( wxDataViewEvent &event );
1c3e52af 119
bf19b714 120 void OnContextMenu( wxDataViewEvent &event );
b910a8ad 121
bf19b714
FM
122 void OnRightClick( wxMouseEvent &event );
123 void OnGoto( wxCommandEvent &event);
124 void OnAddMany( wxCommandEvent &event);
5d9e1605
RR
125 void OnHideAttributes( wxCommandEvent &event);
126 void OnShowAttributes( wxCommandEvent &event);
9861f022 127
4e848be7 128#if wxUSE_DRAG_AND_DROP
bf19b714
FM
129 void OnBeginDrag( wxDataViewEvent &event );
130 void OnDropPossible( wxDataViewEvent &event );
131 void OnDrop( wxDataViewEvent &event );
4e848be7 132#endif // wxUSE_DRAG_AND_DROP
9861f022 133
63ced01b
VZ
134 void OnDataViewChar(wxKeyEvent& event);
135
136 // helper used by both OnDeleteSelected() and OnDataViewChar()
137 void DeleteSelectedItems();
138
139
bf19b714 140 wxNotebook* m_notebook;
1c3e52af 141
bf19b714 142 // the controls stored in the various tabs of the main notebook:
1c3e52af 143
81c3f0fc 144 wxDataViewCtrl* m_ctrl[4];
1c3e52af 145
81c3f0fc 146 // the models associated with the first two DVC:
ed903e42 147
81c3f0fc
FM
148 wxObjectDataPtr<MyMusicTreeModel> m_music_model;
149 wxObjectDataPtr<MyListModel> m_list_model;
1c3e52af 150
bf19b714 151 // other data:
1c3e52af 152
bf19b714 153 wxDataViewColumn* m_col;
5d9e1605 154 wxDataViewColumn* m_attributes;
1c3e52af 155
bf19b714
FM
156 wxTextCtrl* m_log;
157 wxLog *m_logOld;
1c3e52af 158
1e08ad10 159private:
bf19b714 160 DECLARE_EVENT_TABLE()
241cb04b
RR
161};
162
8b6cf9bf 163
bf19b714 164// ----------------------------------------------------------------------------
0bdfa388 165// MyCustomRenderer
bf19b714 166// ----------------------------------------------------------------------------
0bdfa388
RR
167
168class MyCustomRenderer: public wxDataViewCustomRenderer
169{
170public:
ef6833f9
VZ
171 MyCustomRenderer()
172 : wxDataViewCustomRenderer("string",
173 wxDATAVIEW_CELL_ACTIVATABLE,
174 wxALIGN_CENTER)
175 { }
1c3e52af 176
ef6833f9 177 virtual bool Render( wxRect rect, wxDC *dc, int state )
0bdfa388 178 {
ef6833f9 179 dc->SetBrush( *wxLIGHT_GREY_BRUSH );
0bdfa388 180 dc->SetPen( *wxTRANSPARENT_PEN );
ef6833f9
VZ
181
182 rect.Deflate(2);
183 dc->DrawRoundedRectangle( rect, 5 );
184
185 RenderText(m_value,
186 0, // no offset
187 wxRect(dc->GetTextExtent(m_value)).CentreIn(rect),
188 dc,
189 state);
0bdfa388
RR
190 return true;
191 }
192
0bdfa388 193 virtual bool Activate( wxRect WXUNUSED(cell),
ecad59d8
FM
194 wxDataViewModel *WXUNUSED(model),
195 const wxDataViewItem &WXUNUSED(item),
bf19b714 196 unsigned int WXUNUSED(col) )
1c3e52af 197 {
81c3f0fc 198 wxLogMessage( "MyCustomRenderer Activate()" );
0bdfa388
RR
199 return false;
200 }
201
1c3e52af 202 virtual bool LeftClick( wxPoint cursor, wxRect WXUNUSED(cell),
ecad59d8
FM
203 wxDataViewModel *WXUNUSED(model),
204 const wxDataViewItem &WXUNUSED(item),
bf19b714 205 unsigned int WXUNUSED(col) )
1c3e52af 206 {
81c3f0fc 207 wxLogMessage( "MyCustomRenderer LeftClick( %d, %d )", cursor.x, cursor.y );
0bdfa388
RR
208 return false;
209 }
1c3e52af 210
0bdfa388 211 virtual wxSize GetSize() const
1c3e52af 212 {
7448d67c 213 return wxSize(60,20);
a7a27e86 214 }
1c3e52af
VZ
215
216 virtual bool SetValue( const wxVariant &value )
217 {
ef6833f9 218 m_value = value.GetString();
a7a27e86 219 return true;
0bdfa388 220 }
1c3e52af 221
0bdfa388 222 virtual bool GetValue( wxVariant &WXUNUSED(value) ) const { return true; }
1c3e52af 223
a7a27e86 224private:
ef6833f9 225 wxString m_value;
0bdfa388
RR
226};
227
1c3e52af 228
bf19b714
FM
229// ============================================================================
230// implementation
231// ============================================================================
8c2654ce 232
bf19b714 233// ----------------------------------------------------------------------------
362d7fb9 234// MyApp
bf19b714 235// ----------------------------------------------------------------------------
362d7fb9 236
87f0efe2 237IMPLEMENT_APP(MyApp)
bd6169a6 238
bf19b714 239bool MyApp::OnInit()
bd6169a6 240{
45e6e6f8
VZ
241 if ( !wxApp::OnInit() )
242 return false;
243
1c3e52af 244 MyFrame *frame =
ecad59d8 245 new MyFrame(NULL, "wxDataViewCtrl sample", 40, 40, 1000, 540);
bd6169a6 246 SetTopWindow(frame);
bd6169a6 247
bf19b714
FM
248 frame->Show(true);
249 return true;
87f0efe2
RR
250}
251
252
bf19b714 253// ----------------------------------------------------------------------------
bd6169a6 254// MyFrame
bf19b714 255// ----------------------------------------------------------------------------
bd6169a6 256
87f0efe2
RR
257enum
258{
ecad59d8 259 ID_CLEARLOG = wxID_HIGHEST+1,
bb58fa37
VZ
260 ID_BACKGROUND_COLOUR,
261 ID_FOREGROUND_COLOUR,
ecad59d8 262 ID_STYLE_MENU,
81c3f0fc 263
87f0efe2 264 // file menu
81c3f0fc
FM
265 //ID_SINGLE, wxDV_SINGLE==0 so it's always present
266 ID_MULTIPLE,
267 ID_ROW_LINES,
268 ID_HORIZ_RULES,
269 ID_VERT_RULES,
270
87f0efe2 271 ID_EXIT = wxID_EXIT,
1c3e52af 272
81c3f0fc
FM
273 // about menu
274 ID_ABOUT = wxID_ABOUT,
275
276
277 // control IDs
278
d8331a01 279 ID_MUSIC_CTRL = 50,
5d9e1605 280 ID_ATTR_CTRL = 51,
1c3e52af 281
c534e696 282 ID_ADD_MOZART = 100,
e0ff2295 283 ID_DELETE_SEL = 101,
736fe67c 284 ID_DELETE_YEAR = 102,
a400d56b 285 ID_SELECT_NINTH = 103,
d2ee27fe
RR
286 ID_COLLAPSE = 104,
287 ID_EXPAND = 105,
80ce465c
VZ
288 ID_SHOW_CURRENT,
289 ID_SET_NINTH_CURRENT,
1c3e52af 290
c534e696 291 ID_PREPEND_LIST = 200,
b7e9f8b1 292 ID_DELETE_LIST = 201,
8b6cf9bf 293 ID_GOTO = 202,
a46a47a3 294 ID_ADD_MANY = 203,
5d9e1605
RR
295 ID_HIDE_ATTRIBUTES = 204,
296 ID_SHOW_ATTRIBUTES = 205,
ce00f59b 297
a46a47a3
RR
298 // Fourth page.
299 ID_DELETE_TREE_ITEM = 400,
300 ID_DELETE_ALL_TREE_ITEMS = 401,
301 ID_ADD_TREE_ITEM = 402,
302 ID_ADD_TREE_CONTAINER_ITEM = 403
87f0efe2
RR
303};
304
305BEGIN_EVENT_TABLE(MyFrame, wxFrame)
81c3f0fc 306 EVT_MENU_RANGE( ID_MULTIPLE, ID_VERT_RULES, MyFrame::OnStyleChange )
87f0efe2 307 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
81c3f0fc 308 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
ecad59d8 309 EVT_MENU( ID_CLEARLOG, MyFrame::OnClearLog )
81c3f0fc 310
bb58fa37
VZ
311 EVT_MENU( ID_FOREGROUND_COLOUR, MyFrame::OnSetForegroundColour )
312 EVT_MENU( ID_BACKGROUND_COLOUR, MyFrame::OnSetBackgroundColour )
313
81c3f0fc
FM
314 EVT_NOTEBOOK_PAGE_CHANGED( wxID_ANY, MyFrame::OnPageChanged )
315
c534e696 316 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
e0ff2295 317 EVT_BUTTON( ID_DELETE_SEL, MyFrame::OnDeleteSelected )
736fe67c 318 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
a400d56b 319 EVT_BUTTON( ID_SELECT_NINTH, MyFrame::OnSelectNinth )
d2ee27fe
RR
320 EVT_BUTTON( ID_COLLAPSE, MyFrame::OnCollapse )
321 EVT_BUTTON( ID_EXPAND, MyFrame::OnExpand )
80ce465c
VZ
322 EVT_BUTTON( ID_SHOW_CURRENT, MyFrame::OnShowCurrent )
323 EVT_BUTTON( ID_SET_NINTH_CURRENT, MyFrame::OnSetNinthCurrent )
ecad59d8 324
c534e696
RR
325 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
326 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
b7e9f8b1 327 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
8b6cf9bf 328 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
5d9e1605
RR
329 EVT_BUTTON( ID_HIDE_ATTRIBUTES, MyFrame::OnHideAttributes)
330 EVT_BUTTON( ID_SHOW_ATTRIBUTES, MyFrame::OnShowAttributes)
a46a47a3
RR
331 // Fourth page.
332 EVT_BUTTON( ID_DELETE_TREE_ITEM, MyFrame::OnDeleteTreeItem )
333 EVT_BUTTON( ID_DELETE_ALL_TREE_ITEMS, MyFrame::OnDeleteAllTreeItems )
334 EVT_BUTTON( ID_ADD_TREE_ITEM, MyFrame::OnAddTreeItem )
335 EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM, MyFrame::OnAddTreeContainerItem )
1c3e52af 336
0376cc52 337 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
1c3e52af 338
b7e9f8b1 339 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
718fd180
RR
340 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
341 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
342 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
343 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
aed836f3 344 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
1c3e52af 345
ecc32226 346 EVT_DATAVIEW_ITEM_START_EDITING(ID_MUSIC_CTRL, MyFrame::OnStartEditing)
d14e1c3a
RR
347 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
348 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
1c3e52af 349
b7e9f8b1
RR
350 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
351 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
352 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
74dea0de
RR
353
354 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
8c2654ce 355
4e848be7 356#if wxUSE_DRAG_AND_DROP
591cc82d 357 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
e4de825e
RR
358 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL, MyFrame::OnDropPossible )
359 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL, MyFrame::OnDrop )
4e848be7 360#endif // wxUSE_DRAG_AND_DROP
1c3e52af 361
b7e9f8b1 362 EVT_RIGHT_UP(MyFrame::OnRightClick)
ce00f59b 363
5d9e1605 364 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_ATTR_CTRL, MyFrame::OnAttrHeaderClick)
ce00f59b 365
87f0efe2
RR
366END_EVENT_TABLE()
367
e94d0c1e 368MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
bd6169a6
RR
369 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
370{
d8331a01 371 m_log = NULL;
fbda518c 372 m_col = NULL;
d8331a01 373
81c3f0fc
FM
374 m_ctrl[0] = NULL;
375 m_ctrl[1] = NULL;
376 m_ctrl[2] = NULL;
377 m_ctrl[3] = NULL;
378
0bcd4039 379 SetIcon(wxICON(sample));
bd6169a6 380
bf19b714
FM
381
382 // build the menus
383 // ----------------
87f0efe2 384
81c3f0fc 385 wxMenu *style_menu = new wxMenu;
ecad59d8
FM
386 //style_menu->AppendCheckItem(ID_SINGLE, "Single selection"));
387 style_menu->AppendCheckItem(ID_MULTIPLE, "Multiple selection");
388 style_menu->AppendCheckItem(ID_ROW_LINES, "Alternating colours");
389 style_menu->AppendCheckItem(ID_HORIZ_RULES, "Display horizontal rules");
390 style_menu->AppendCheckItem(ID_VERT_RULES, "Display vertical rules");
81c3f0fc 391
bd6169a6 392 wxMenu *file_menu = new wxMenu;
bb58fa37 393 file_menu->Append(ID_CLEARLOG, "&Clear log\tCtrl-L");
1e21d0ef 394 file_menu->Append(ID_FOREGROUND_COLOUR, "Set &foreground colour...\tCtrl-S");
bb58fa37 395 file_menu->Append(ID_BACKGROUND_COLOUR, "Set &background colour...\tCtrl-B");
ecad59d8 396 file_menu->Append(ID_STYLE_MENU, "&Style", style_menu);
87f0efe2 397 file_menu->AppendSeparator();
ecad59d8 398 file_menu->Append(ID_EXIT, "E&xit");
bd6169a6 399
81c3f0fc 400 wxMenu *about_menu = new wxMenu;
ecad59d8 401 about_menu->Append(ID_ABOUT, "&About");
81c3f0fc 402
bd6169a6 403 wxMenuBar *menu_bar = new wxMenuBar;
ecad59d8
FM
404 menu_bar->Append(file_menu, "&File");
405 menu_bar->Append(about_menu, "&About");
87f0efe2 406
bd6169a6 407 SetMenuBar(menu_bar);
87f0efe2 408 CreateStatusBar();
8c2654ce 409
1e08ad10 410
bf19b714
FM
411 // first page of the notebook
412 // --------------------------
ecad59d8 413
bf19b714 414 m_notebook = new wxNotebook( this, wxID_ANY );
bd6169a6 415
bf19b714 416 wxPanel *firstPanel = new wxPanel( m_notebook, wxID_ANY );
bf19b714 417
81c3f0fc 418 BuildDataViewCtrl(firstPanel, 0); // sets m_ctrl[0]
1c3e52af 419
80ce465c
VZ
420 const wxSizerFlags border = wxSizerFlags().DoubleBorder();
421
bf19b714 422 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
80ce465c
VZ
423 button_sizer->Add( new wxButton( firstPanel, ID_ADD_MOZART, "Add Mozart"), border );
424 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_SEL, "Delete selected"), border );
425 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_YEAR, "Delete \"Year\" column"), border );
426 button_sizer->Add( new wxButton( firstPanel, ID_SELECT_NINTH,"Select ninth symphony"), border );
427 button_sizer->Add( new wxButton( firstPanel, ID_COLLAPSE, "Collapse"), border );
428 button_sizer->Add( new wxButton( firstPanel, ID_EXPAND, "Expand"), border );
429
430 wxBoxSizer *sizerCurrent = new wxBoxSizer(wxHORIZONTAL);
431 sizerCurrent->Add(new wxButton(firstPanel, ID_SHOW_CURRENT,
432 "&Show current"), border);
433 sizerCurrent->Add(new wxButton(firstPanel, ID_SET_NINTH_CURRENT,
434 "Make &ninth symphony current"), border);
1c3e52af 435
bf19b714 436 wxSizer *firstPanelSz = new wxBoxSizer( wxVERTICAL );
81c3f0fc
FM
437 m_ctrl[0]->SetMinSize(wxSize(-1, 200));
438 firstPanelSz->Add(m_ctrl[0], 1, wxGROW|wxALL, 5);
439 firstPanelSz->Add(
ecad59d8 440 new wxStaticText(firstPanel, wxID_ANY, "Most of the cells above are editable!"),
81c3f0fc 441 0, wxGROW|wxALL, 5);
bf19b714 442 firstPanelSz->Add(button_sizer);
80ce465c 443 firstPanelSz->Add(sizerCurrent);
bf19b714 444 firstPanel->SetSizerAndFit(firstPanelSz);
1c3e52af 445
83087c60 446
bf19b714
FM
447 // second page of the notebook
448 // ---------------------------
83087c60 449
bf19b714 450 wxPanel *secondPanel = new wxPanel( m_notebook, wxID_ANY );
1c3e52af 451
81c3f0fc 452 BuildDataViewCtrl(secondPanel, 1); // sets m_ctrl[1]
1c3e52af 453
bf19b714 454 wxBoxSizer *button_sizer2 = new wxBoxSizer( wxHORIZONTAL );
5d9e1605
RR
455 button_sizer2->Add( new wxButton( secondPanel, ID_PREPEND_LIST,"Prepend"), 0, wxALL, 10 );
456 button_sizer2->Add( new wxButton( secondPanel, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
457 button_sizer2->Add( new wxButton( secondPanel, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
458 button_sizer2->Add( new wxButton( secondPanel, ID_ADD_MANY, "Add 1000"), 0, wxALL, 10 );
459 button_sizer2->Add( new wxButton( secondPanel, ID_HIDE_ATTRIBUTES, "Hide attributes"), 0, wxALL, 10 );
460 button_sizer2->Add( new wxButton( secondPanel, ID_SHOW_ATTRIBUTES, "Show attributes"), 0, wxALL, 10 );
a75124d0 461
bf19b714 462 wxSizer *secondPanelSz = new wxBoxSizer( wxVERTICAL );
81c3f0fc 463 secondPanelSz->Add(m_ctrl[1], 1, wxGROW|wxALL, 5);
bf19b714
FM
464 secondPanelSz->Add(button_sizer2);
465 secondPanel->SetSizerAndFit(secondPanelSz);
1c3e52af 466
b7e9f8b1 467
bf19b714
FM
468 // third page of the notebook
469 // ---------------------------
8c2654ce 470
bf19b714 471 wxPanel *thirdPanel = new wxPanel( m_notebook, wxID_ANY );
8c2654ce 472
81c3f0fc 473 BuildDataViewCtrl(thirdPanel, 2); // sets m_ctrl[2]
8c2654ce 474
bf19b714 475 wxSizer *thirdPanelSz = new wxBoxSizer( wxVERTICAL );
81c3f0fc 476 thirdPanelSz->Add(m_ctrl[2], 1, wxGROW|wxALL, 5);
bf19b714 477 thirdPanel->SetSizerAndFit(thirdPanelSz);
8c2654ce 478
672e58d9 479
bf19b714
FM
480 // fourth page of the notebook
481 // ---------------------------
ecad59d8 482
bf19b714 483 wxPanel *fourthPanel = new wxPanel( m_notebook, wxID_ANY );
a75124d0 484
81c3f0fc 485 BuildDataViewCtrl(fourthPanel, 3); // sets m_ctrl[3]
a46a47a3
RR
486 // Buttons
487 wxBoxSizer *button_sizer4 = new wxBoxSizer( wxHORIZONTAL );
488 button_sizer4->Add( new wxButton( fourthPanel, ID_DELETE_TREE_ITEM, "Delete Selected"), 0, wxALL, 10 );
489 button_sizer4->Add( new wxButton( fourthPanel, ID_DELETE_ALL_TREE_ITEMS, "Delete All"), 0, wxALL, 10 );
490 button_sizer4->Add( new wxButton( fourthPanel, ID_ADD_TREE_ITEM, "Add Item"), 0, wxALL, 10 );
491 button_sizer4->Add( new wxButton( fourthPanel, ID_ADD_TREE_CONTAINER_ITEM, "Add Container"), 0, wxALL, 10 );
1c3e52af 492
bf19b714 493 wxSizer *fourthPanelSz = new wxBoxSizer( wxVERTICAL );
81c3f0fc 494 fourthPanelSz->Add(m_ctrl[3], 1, wxGROW|wxALL, 5);
a46a47a3 495 fourthPanelSz->Add(button_sizer4);
bf19b714 496 fourthPanel->SetSizerAndFit(fourthPanelSz);
e39d30c0 497
1c3e52af 498
ecad59d8 499
bf19b714
FM
500 // complete GUI
501 // ------------
502
503 m_notebook->AddPage(firstPanel, "MyMusicTreeModel");
504 m_notebook->AddPage(secondPanel, "MyListModel");
505 m_notebook->AddPage(thirdPanel, "wxDataViewListCtrl");
506 m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl");
507
508 wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
ecad59d8
FM
509
510 m_log = new wxTextCtrl( this, wxID_ANY, wxString(), wxDefaultPosition,
bf19b714
FM
511 wxDefaultSize, wxTE_MULTILINE );
512 m_log->SetMinSize(wxSize(-1, 100));
513 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
ecad59d8 514 wxLogMessage( "This is the log window" );
1c3e52af 515
bf19b714
FM
516 mainSizer->Add( m_notebook, 1, wxGROW );
517 mainSizer->Add( m_log, 0, wxGROW );
43b2d5e7 518
bf19b714 519 SetSizerAndFit(mainSizer);
a75124d0
RR
520}
521
e39d30c0
RR
522MyFrame::~MyFrame()
523{
524 delete wxLog::SetActiveTarget(m_logOld);
525}
526
81c3f0fc
FM
527void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned long style)
528{
529 switch (nPanel)
530 {
531 case 0:
532 {
533 wxASSERT(!m_ctrl[0] && !m_music_model);
ecad59d8 534 m_ctrl[0] =
81c3f0fc
FM
535 new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
536 wxDefaultSize, style );
63ced01b
VZ
537 m_ctrl[0]->Connect(wxEVT_CHAR,
538 wxKeyEventHandler(MyFrame::OnDataViewChar),
539 NULL, this);
81c3f0fc
FM
540
541 m_music_model = new MyMusicTreeModel;
542 m_ctrl[0]->AssociateModel( m_music_model.get() );
543
4e848be7 544#if wxUSE_DRAG_AND_DROP
81c3f0fc
FM
545 m_ctrl[0]->EnableDragSource( wxDF_UNICODETEXT );
546 m_ctrl[0]->EnableDropTarget( wxDF_UNICODETEXT );
4e848be7 547#endif // wxUSE_DRAG_AND_DROP
81c3f0fc
FM
548
549 // column 0 of the view control:
550
ecad59d8
FM
551 wxDataViewTextRenderer *tr =
552 new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_INERT );
553 wxDataViewColumn *column0 =
554 new wxDataViewColumn( "title", tr, 0, 200, wxALIGN_LEFT,
81c3f0fc
FM
555 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE );
556 m_ctrl[0]->AppendColumn( column0 );
557#if 0
558 // Call this and sorting is enabled
559 // immediatly upon start up.
560 column0->SetAsSortKey();
561#endif
562
563 // column 1 of the view control:
564
ecad59d8
FM
565 tr = new wxDataViewTextRenderer( "string", wxDATAVIEW_CELL_EDITABLE );
566 wxDataViewColumn *column1 =
567 new wxDataViewColumn( "artist", tr, 1, 150, wxALIGN_LEFT,
568 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE |
81c3f0fc
FM
569 wxDATAVIEW_COL_RESIZABLE );
570 column1->SetMinWidth(150); // this column can't be resized to be smaller
571 m_ctrl[0]->AppendColumn( column1 );
572
573 // column 2 of the view control:
574
ecad59d8 575 wxDataViewSpinRenderer *sr =
81c3f0fc 576 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
ecad59d8
FM
577 wxDataViewColumn *column2 =
578 new wxDataViewColumn( "year", sr, 2, 60, wxALIGN_LEFT,
81c3f0fc
FM
579 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE );
580 m_ctrl[0]->AppendColumn( column2 );
581
582 // column 3 of the view control:
583
584 wxArrayString choices;
585 choices.Add( "good" );
586 choices.Add( "bad" );
587 choices.Add( "lousy" );
ecad59d8 588 wxDataViewChoiceRenderer *c =
81c3f0fc 589 new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
ecad59d8
FM
590 wxDataViewColumn *column3 =
591 new wxDataViewColumn( "rating", c, 3, 100, wxALIGN_LEFT,
81c3f0fc
FM
592 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
593 m_ctrl[0]->AppendColumn( column3 );
594
595 // column 4 of the view control:
596
ecad59d8 597 m_ctrl[0]->AppendProgressColumn( "popularity", 4, wxDATAVIEW_CELL_INERT, 80 );
81c3f0fc
FM
598
599 // column 5 of the view control:
600
ef6833f9 601 MyCustomRenderer *cr = new MyCustomRenderer;
ecad59d8
FM
602 wxDataViewColumn *column5 =
603 new wxDataViewColumn( "custom", cr, 5, -1, wxALIGN_LEFT,
81c3f0fc
FM
604 wxDATAVIEW_COL_RESIZABLE );
605 m_ctrl[0]->AppendColumn( column5 );
ecad59d8 606
81c3f0fc
FM
607
608 // select initially the ninth symphony:
609 m_ctrl[0]->Select(m_music_model->GetNinthItem());
610 }
611 break;
612
613 case 1:
614 {
615 wxASSERT(!m_ctrl[1] && !m_list_model);
5d9e1605 616 m_ctrl[1] = new wxDataViewCtrl( parent, ID_ATTR_CTRL, wxDefaultPosition,
81c3f0fc
FM
617 wxDefaultSize, style );
618
619 m_list_model = new MyListModel;
620 m_ctrl[1]->AssociateModel( m_list_model.get() );
621
622 // the various columns
2746bccf
VZ
623 m_ctrl[1]->AppendTextColumn("editable string",
624 MyListModel::Col_EditableText,
625 wxDATAVIEW_CELL_EDITABLE);
626 m_ctrl[1]->AppendIconTextColumn("icon",
627 MyListModel::Col_IconText,
628 wxDATAVIEW_CELL_EDITABLE);
ce00f59b
VZ
629
630 m_attributes =
2746bccf
VZ
631 new wxDataViewColumn("attributes",
632 new wxDataViewTextRenderer,
5d9e1605
RR
633 MyListModel::Col_TextWithAttr,
634 80,
635 wxALIGN_RIGHT,
636 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
637 m_ctrl[1]->AppendColumn( m_attributes );
ef6833f9
VZ
638
639 m_ctrl[1]->AppendColumn(
640 new wxDataViewColumn("custom renderer",
641 new MyCustomRenderer,
642 MyListModel::Col_Custom)
643 );
81c3f0fc
FM
644 }
645 break;
646
647 case 2:
648 {
649 wxASSERT(!m_ctrl[2]);
ecad59d8 650 wxDataViewListCtrl* lc =
81c3f0fc
FM
651 new wxDataViewListCtrl( parent, wxID_ANY, wxDefaultPosition,
652 wxDefaultSize, style );
653 m_ctrl[2] = lc;
654
ecad59d8
FM
655 lc->AppendToggleColumn( "Toggle" );
656 lc->AppendTextColumn( "Text" );
657 lc->AppendProgressColumn( "Progress" );
81c3f0fc
FM
658
659 wxVector<wxVariant> data;
660 for (unsigned int i=0; i<10; i++)
661 {
662 data.clear();
663 data.push_back( (i%3) == 0 );
664 data.push_back( wxString::Format("row %d", i) );
665 data.push_back( long(5*i) );
666
667 lc->AppendItem( data );
668 }
669 }
670 break;
671
672 case 3:
673 {
674 wxASSERT(!m_ctrl[3]);
ecad59d8 675 wxDataViewTreeCtrl* tc =
81c3f0fc 676 new wxDataViewTreeCtrl( parent, wxID_ANY, wxDefaultPosition,
a35169b6 677 wxDefaultSize, style | wxDV_NO_HEADER );
81c3f0fc
FM
678 m_ctrl[3] = tc;
679
680 wxImageList *ilist = new wxImageList( 16, 16 );
681 ilist->Add( wxIcon(wx_small_xpm) );
682 tc->SetImageList( ilist );
683
ecad59d8
FM
684 wxDataViewItem parent =
685 tc->AppendContainer( wxDataViewItem(0), "The Root", 0 );
686 tc->AppendItem( parent, "Child 1", 0 );
687 tc->AppendItem( parent, "Child 2", 0 );
688 tc->AppendItem( parent, "Child 3, very long, long, long, long", 0 );
81c3f0fc
FM
689
690 wxDataViewItem cont =
ecad59d8
FM
691 tc->AppendContainer( parent, "Container child", 0 );
692 tc->AppendItem( cont, "Child 4", 0 );
693 tc->AppendItem( cont, "Child 5", 0 );
81c3f0fc
FM
694
695 tc->Expand(cont);
ecad59d8 696 }
81c3f0fc
FM
697 break;
698 }
699}
700
701
702// ----------------------------------------------------------------------------
703// MyFrame - generic event handlers
704// ----------------------------------------------------------------------------
705
ecad59d8
FM
706void MyFrame::OnClearLog( wxCommandEvent& WXUNUSED(event) )
707{
708 m_log->Clear();
709}
710
bb58fa37
VZ
711void MyFrame::OnSetForegroundColour(wxCommandEvent& WXUNUSED(event))
712{
713 wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()];
714 wxColour col = wxGetColourFromUser(this, dvc->GetForegroundColour());
715 if ( col.IsOk() )
716 {
717 dvc->SetForegroundColour(col);
718 Refresh();
719 }
720}
721
722void MyFrame::OnSetBackgroundColour(wxCommandEvent& WXUNUSED(event))
723{
724 wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()];
725 wxColour col = wxGetColourFromUser(this, dvc->GetBackgroundColour());
726 if ( col.IsOk() )
727 {
728 dvc->SetBackgroundColour(col);
729 Refresh();
730 }
731}
732
ecad59d8 733void MyFrame::OnPageChanged( wxBookCtrlEvent& WXUNUSED(event) )
81c3f0fc
FM
734{
735 unsigned int nPanel = m_notebook->GetSelection();
736
737 GetMenuBar()->FindItem(ID_STYLE_MENU)->SetItemLabel(
738 wxString::Format("Style of panel #%d", nPanel+1));
739
740 for (unsigned int id = ID_MULTIPLE; id <= ID_VERT_RULES; id++)
741 {
742 unsigned long style = 0;
743 switch (id)
744 {
745 /*case ID_SINGLE:
746 style = wxDV_SINGLE;
747 break;*/
748 case ID_MULTIPLE:
749 style = wxDV_MULTIPLE;
750 break;
751 case ID_ROW_LINES:
752 style = wxDV_ROW_LINES;
753 break;
754 case ID_HORIZ_RULES:
755 style = wxDV_HORIZ_RULES;
756 break;
757 case ID_VERT_RULES:
758 style = wxDV_VERT_RULES;
759 break;
760 default:
761 wxFAIL;
762 }
763
764 GetMenuBar()->FindItem(id)->Check( m_ctrl[nPanel]->HasFlag(style) );
765 }
766}
767
ecad59d8 768void MyFrame::OnStyleChange( wxCommandEvent& WXUNUSED(event) )
81c3f0fc
FM
769{
770 unsigned int nPanel = m_notebook->GetSelection();
771
772 // build the style
773 unsigned long style = 0;
774 /*if (GetMenuBar()->FindItem(ID_SINGLE)->IsChecked())
775 style |= wxDV_SINGLE;*/
776 if (GetMenuBar()->FindItem(ID_MULTIPLE)->IsChecked())
777 style |= wxDV_MULTIPLE;
778 if (GetMenuBar()->FindItem(ID_ROW_LINES)->IsChecked())
779 style |= wxDV_ROW_LINES;
780 if (GetMenuBar()->FindItem(ID_HORIZ_RULES)->IsChecked())
781 style |= wxDV_HORIZ_RULES;
782 if (GetMenuBar()->FindItem(ID_VERT_RULES)->IsChecked())
783 style |= wxDV_VERT_RULES;
784
785 wxSizer* sz = m_ctrl[nPanel]->GetContainingSizer();
786 wxASSERT(sz);
787
788 sz->Detach(m_ctrl[nPanel]);
789 wxDELETE(m_ctrl[nPanel]);
790 m_ctrl[nPanel] = NULL;
791
792 if (nPanel == 0)
793 m_music_model.reset(NULL);
794 else if (nPanel == 1)
795 m_list_model.reset(NULL);
796
797 // rebuild the DVC for the selected panel:
798 BuildDataViewCtrl((wxPanel*)m_notebook->GetPage(nPanel), nPanel, style);
799
800 sz->Prepend(m_ctrl[nPanel], 1, wxGROW|wxALL, 5);
801 sz->Layout();
802}
803
804void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
a75124d0
RR
805{
806 Close(true);
e94d0c1e
RR
807}
808
ecad59d8 809void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
81c3f0fc
FM
810{
811 wxAboutDialogInfo info;
812 info.SetName(_("DataView sample"));
813 info.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
9a83f860 814 info.SetCopyright(wxT("(C) 2007-2009 Robert Roebling"));
81c3f0fc
FM
815 info.AddDeveloper("Robert Roebling");
816 info.AddDeveloper("Francesco Montorsi");
817
818 wxAboutBox(info);
819}
820
821
822// ----------------------------------------------------------------------------
823// MyFrame - event handlers for the first page
824// ----------------------------------------------------------------------------
825
4e848be7
VZ
826#if wxUSE_DRAG_AND_DROP
827
81c3f0fc
FM
828void MyFrame::OnBeginDrag( wxDataViewEvent &event )
829{
830 wxDataViewItem item( event.GetItem() );
831
832 // only allow drags for item, not containers
833 if (m_music_model->IsContainer( item ) )
834 {
835 event.Veto();
836 return;
837 }
838
839 MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
840 wxTextDataObject *obj = new wxTextDataObject;
841 obj->SetText( node->m_title );
842 event.SetDataObject( obj );
843}
844
845void MyFrame::OnDropPossible( wxDataViewEvent &event )
846{
847 wxDataViewItem item( event.GetItem() );
848
849 // only allow drags for item, not containers
850 if (m_music_model->IsContainer( item ) )
851 event.Veto();
852
853 if (event.GetDataFormat() != wxDF_UNICODETEXT)
854 event.Veto();
855}
856
857void MyFrame::OnDrop( wxDataViewEvent &event )
858{
859 wxDataViewItem item( event.GetItem() );
860
861 // only allow drops for item, not containers
862 if (m_music_model->IsContainer( item ) )
863 {
864 event.Veto();
865 return;
866 }
867
868 if (event.GetDataFormat() != wxDF_UNICODETEXT)
869 {
870 event.Veto();
871 return;
872 }
873
874 wxTextDataObject obj;
875 obj.SetData( wxDF_UNICODETEXT, event.GetDataSize(), event.GetDataBuffer() );
876
ecad59d8 877 wxLogMessage( "Text dropped: %s", obj.GetText() );
81c3f0fc
FM
878}
879
4e848be7
VZ
880#endif // wxUSE_DRAG_AND_DROP
881
81c3f0fc 882void MyFrame::OnAddMozart( wxCommandEvent& WXUNUSED(event) )
c534e696 883{
54f0a324 884 m_music_model->AddToClassical( "Eine kleine Nachtmusik", "Wolfgang Mozart", 1787 );
c534e696
RR
885}
886
63ced01b 887void MyFrame::DeleteSelectedItems()
1e08ad10 888{
b7e9f8b1 889 wxDataViewItemArray items;
81c3f0fc 890 int len = m_ctrl[0]->GetSelections( items );
b7e9f8b1
RR
891 for( int i = 0; i < len; i ++ )
892 if (items[i].IsOk())
893 m_music_model->Delete( items[i] );
e63807a8
RR
894}
895
63ced01b
VZ
896void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
897{
898 DeleteSelectedItems();
899}
900
736fe67c
RR
901void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
902{
81c3f0fc 903 m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) );
736fe67c
RR
904 FindWindow( ID_DELETE_YEAR )->Disable();
905}
906
a400d56b
RR
907void MyFrame::OnSelectNinth( wxCommandEvent& WXUNUSED(event) )
908{
ecad59d8
FM
909 if (!m_music_model->GetNinthItem().IsOk())
910 {
911 wxLogError( "Cannot select the ninth symphony: it was removed!" );
912 return;
913 }
914
81c3f0fc 915 m_ctrl[0]->Select( m_music_model->GetNinthItem() );
1e08ad10
RR
916}
917
d2ee27fe
RR
918void MyFrame::OnCollapse( wxCommandEvent& WXUNUSED(event) )
919{
920 wxDataViewItem item = m_ctrl[0]->GetSelection();
921 if (item.IsOk())
922 m_ctrl[0]->Collapse( item );
923}
924
925void MyFrame::OnExpand( wxCommandEvent& WXUNUSED(event) )
926{
927 wxDataViewItem item = m_ctrl[0]->GetSelection();
928 if (item.IsOk())
929 m_ctrl[0]->Expand( item );
930}
931
80ce465c
VZ
932void MyFrame::OnShowCurrent(wxCommandEvent& WXUNUSED(event))
933{
934 wxDataViewItem item = m_ctrl[0]->GetCurrentItem();
935 if ( item.IsOk() )
936 {
937 wxLogMessage("Current item: \"%s\" by %s",
938 m_music_model->GetTitle(item),
939 m_music_model->GetArtist(item));
940 }
941 else
942 {
943 wxLogMessage("There is no current item.");
944 }
945}
946
947void MyFrame::OnSetNinthCurrent(wxCommandEvent& WXUNUSED(event))
948{
949 wxDataViewItem item(m_music_model->GetNinthItem());
950 if ( !item.IsOk() )
951 {
952 wxLogError( "Cannot make the ninth symphony current: it was removed!" );
953 return;
954 }
955
956 m_ctrl[0]->SetCurrentItem(item);
957}
958
d8331a01
RR
959void MyFrame::OnValueChanged( wxDataViewEvent &event )
960{
961 if (!m_log)
962 return;
1c3e52af 963
a8fde0a8
RR
964 wxString title = m_music_model->GetTitle( event.GetItem() );
965 wxLogMessage( "wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %s; Column: %d",
966 title, event.GetColumn() );
b7e9f8b1
RR
967}
968
969void MyFrame::OnActivated( wxDataViewEvent &event )
970{
971 if(!m_log)
972 return;
973
d32332aa 974 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 975 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title );
8c2654ce 976
81c3f0fc 977 if (m_ctrl[0]->IsExpanded( event.GetItem() ))
43b2d5e7 978 {
ecad59d8 979 wxLogMessage( "Item: %s is expanded", title );
43b2d5e7 980 }
b7e9f8b1
RR
981}
982
aed836f3 983void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
6848478c
RR
984{
985 if(!m_log)
986 return;
987
d32332aa
RR
988 wxString title = m_music_model->GetTitle( event.GetItem() );
989 if (title.empty())
ecad59d8 990 title = "None";
1c3e52af 991
ecad59d8 992 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title );
6848478c
RR
993}
994
718fd180
RR
995void MyFrame::OnExpanding( wxDataViewEvent &event )
996{
997 if (!m_log)
998 return;
1c3e52af 999
d32332aa 1000 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1001 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title );
718fd180
RR
1002}
1003
d14e1c3a 1004
ecc32226
RR
1005void MyFrame::OnStartEditing( wxDataViewEvent &event )
1006{
1007 wxString artist = m_music_model->GetArtist( event.GetItem() );
1008 if (artist == "Ludwig van Beethoven")
1009 {
1010 event.Veto();
43b2d5e7 1011
ecc32226
RR
1012 if (!m_log)
1013 return;
43b2d5e7 1014
0a807957 1015 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING vetoed. Artist: %s", artist );
ecc32226 1016 }
0a807957
RR
1017 else
1018 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING not vetoed. Artist: %s", artist );
43b2d5e7 1019
ecc32226
RR
1020}
1021
d14e1c3a
RR
1022void MyFrame::OnEditingStarted( wxDataViewEvent &event )
1023{
1024 if (!m_log)
1025 return;
1c3e52af 1026
d32332aa 1027 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1028 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title );
d14e1c3a
RR
1029}
1030
1031void MyFrame::OnEditingDone( wxDataViewEvent &event )
1032{
1033 if (!m_log)
1034 return;
1c3e52af 1035
d32332aa 1036 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1037 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title );
d14e1c3a
RR
1038}
1039
718fd180
RR
1040void MyFrame::OnExpanded( wxDataViewEvent &event )
1041{
1042 if (!m_log)
1043 return;
1c3e52af 1044
d32332aa 1045 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1046 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title );
718fd180
RR
1047}
1048
1049void MyFrame::OnCollapsing( wxDataViewEvent &event )
1050{
1051 if (!m_log)
1052 return;
1c3e52af 1053
d32332aa 1054 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1055 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title );
718fd180
RR
1056}
1057
1058void MyFrame::OnCollapsed( wxDataViewEvent &event )
1059{
1060 if (!m_log)
1061 return;
1c3e52af 1062
d32332aa 1063 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1064 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title );
718fd180
RR
1065}
1066
74dea0de
RR
1067void MyFrame::OnContextMenu( wxDataViewEvent &event )
1068{
1069 if (!m_log)
1070 return;
1c3e52af 1071
74dea0de 1072 wxString title = m_music_model->GetTitle( event.GetItem() );
ecad59d8 1073 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s", title );
1c3e52af
VZ
1074
1075 wxMenu menu;
ecad59d8
FM
1076 menu.Append( 1, "menuitem 1" );
1077 menu.Append( 2, "menuitem 2" );
1078 menu.Append( 3, "menuitem 3" );
1c3e52af 1079
81c3f0fc 1080 m_ctrl[0]->PopupMenu(&menu);
74dea0de
RR
1081}
1082
5d9e1605
RR
1083void MyFrame::OnAttrHeaderClick( wxDataViewEvent &event )
1084{
1085 // we need to skip the event to let the default behaviour of sorting by
1086 // this column when it is clicked to take place
1087 event.Skip();
1088
1089 if (!m_log)
1090 return;
1091
1092 int pos = m_ctrl[1]->GetColumnPosition( event.GetDataViewColumn() );
1093
1094 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
1095 wxLogMessage( "Column title: %s Column width: %d", event.GetDataViewColumn()->GetTitle(), event.GetDataViewColumn()->GetWidth() );
1096}
1097
b7e9f8b1
RR
1098void MyFrame::OnHeaderClick( wxDataViewEvent &event )
1099{
fba41cf3
VZ
1100 // we need to skip the event to let the default behaviour of sorting by
1101 // this column when it is clicked to take place
1102 event.Skip();
1103
81c3f0fc 1104 if (!m_log)
b7e9f8b1 1105 return;
1c3e52af 1106
81c3f0fc 1107 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
b7e9f8b1 1108
ecad59d8 1109 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
1c958845 1110 wxLogMessage( "Column width: %d", event.GetDataViewColumn()->GetWidth() );
b7e9f8b1
RR
1111}
1112
1113void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
1114{
1115 if(!m_log)
1116 return;
1117
81c3f0fc 1118 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
dadc879e 1119
ecad59d8 1120 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos );
b7e9f8b1
RR
1121}
1122
1123void MyFrame::OnSorted( wxDataViewEvent &event )
1124{
1125 if(!m_log)
1126 return;
1127
81c3f0fc 1128 int pos = m_ctrl[0]->GetColumnPosition( event.GetDataViewColumn() );
d32332aa 1129
ecad59d8 1130 wxLogMessage( "wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos );
b7e9f8b1
RR
1131}
1132
1133void MyFrame::OnRightClick( wxMouseEvent &event )
1134{
1135 if(!m_log)
1136 return;
1137
ecad59d8
FM
1138 wxLogMessage( "wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d",
1139 event.GetX(), event.GetY() );
b7e9f8b1
RR
1140}
1141
63ced01b
VZ
1142void MyFrame::OnDataViewChar(wxKeyEvent& event)
1143{
1144 if ( event.GetKeyCode() == WXK_DELETE )
1145 DeleteSelectedItems();
1146 else
1147 event.Skip();
1148}
8b6cf9bf 1149
81c3f0fc
FM
1150// ----------------------------------------------------------------------------
1151// MyFrame - event handlers for the second page
1152// ----------------------------------------------------------------------------
8b6cf9bf 1153
81c3f0fc 1154void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
bd6169a6 1155{
ecad59d8 1156 m_list_model->Prepend("Test");
bd6169a6
RR
1157}
1158
81c3f0fc 1159void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
15cac64f 1160{
81c3f0fc
FM
1161 wxDataViewItemArray items;
1162 int len = m_ctrl[1]->GetSelections( items );
1163 if (len > 0)
1164 m_list_model->DeleteItems( items );
15cac64f 1165}
e4de825e 1166
81c3f0fc 1167void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
e4de825e 1168{
81c3f0fc
FM
1169 wxDataViewItem item = m_list_model->GetItem( 50 );
1170 m_ctrl[1]->EnsureVisible(item,m_col);
e4de825e
RR
1171}
1172
81c3f0fc 1173void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
e4de825e 1174{
81c3f0fc 1175 m_list_model->AddMany();
e4de825e
RR
1176}
1177
5d9e1605
RR
1178void MyFrame::OnHideAttributes(wxCommandEvent& WXUNUSED(event))
1179{
1180 m_attributes->SetHidden(true);
1181}
1182
1183void MyFrame::OnShowAttributes(wxCommandEvent& WXUNUSED(event))
1184{
1185 m_attributes->SetHidden(false);
1186}
1187
a46a47a3
RR
1188// ----------------------------------------------------------------------------
1189// MyFrame - event handlers for the fourth page
1190// ----------------------------------------------------------------------------
a53f722f
RR
1191
1192void MyFrame::OnDeleteTreeItem(wxCommandEvent& WXUNUSED(event))
1193{
a46a47a3
RR
1194 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1195 wxDataViewItem selected = ctrl->GetSelection();
a53f722f 1196 if (!selected.IsOk())
a46a47a3 1197 return;
ce00f59b 1198
a46a47a3
RR
1199 ctrl->DeleteItem(selected);
1200}
1201
a53f722f
RR
1202void MyFrame::OnDeleteAllTreeItems(wxCommandEvent& WXUNUSED(event))
1203{
a46a47a3
RR
1204 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1205 ctrl->DeleteAllItems();
1206}
1207
a53f722f
RR
1208void MyFrame::OnAddTreeItem(wxCommandEvent& WXUNUSED(event))
1209{
a46a47a3
RR
1210 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1211 wxDataViewItem selected = ctrl->GetSelection();
8ddda15b
RR
1212 if (ctrl->IsContainer(selected))
1213 ctrl->AppendItem( selected, "Item", 0 );
a46a47a3
RR
1214}
1215
a53f722f
RR
1216void MyFrame::OnAddTreeContainerItem(wxCommandEvent& WXUNUSED(event))
1217{
a46a47a3
RR
1218 wxDataViewTreeCtrl* ctrl = (wxDataViewTreeCtrl*) m_ctrl[3];
1219 wxDataViewItem selected = ctrl->GetSelection();
8ddda15b
RR
1220 if (ctrl->IsContainer(selected))
1221 ctrl->AppendContainer(selected, "Container", 0 );
a46a47a3
RR
1222}
1223