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