reorganize the sample splitting it in two source files and an additional header;...
[wxWidgets.git] / samples / dataview / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
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
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/dataview.h"
28 #include "wx/datetime.h"
29 #include "wx/splitter.h"
30 #include "wx/aboutdlg.h"
31 #include "wx/choicdlg.h"
32 #include "wx/numdlg.h"
33 #include "wx/spinctrl.h"
34 #include "wx/imaglist.h"
35 #include "wx/notebook.h"
36
37 #include "mymodels.h"
38
39 // ----------------------------------------------------------------------------
40 // resources
41 // ----------------------------------------------------------------------------
42
43 #ifndef __WXMSW__
44 #include "../sample.xpm"
45 #endif
46
47 #include "wx_small.xpm"
48
49 // ----------------------------------------------------------------------------
50 // MyApp
51 // ----------------------------------------------------------------------------
52
53 class MyApp: public wxApp
54 {
55 public:
56 virtual bool OnInit();
57 };
58
59 // ----------------------------------------------------------------------------
60 // MyFrame
61 // ----------------------------------------------------------------------------
62
63 class MyFrame : public wxFrame
64 {
65 public:
66 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
67 ~MyFrame();
68
69 public:
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72
73 void OnAddMozart(wxCommandEvent& event);
74 void OnDeleteMusic(wxCommandEvent& event);
75 void OnDeleteYear(wxCommandEvent& event);
76 void OnSelectNinth(wxCommandEvent& event);
77
78 void OnPrependList(wxCommandEvent& event);
79 void OnDeleteList(wxCommandEvent& event);
80
81 void OnValueChanged( wxDataViewEvent &event );
82
83 void OnActivated( wxDataViewEvent &event );
84 void OnExpanding( wxDataViewEvent &event );
85 void OnExpanded( wxDataViewEvent &event );
86 void OnCollapsing( wxDataViewEvent &event );
87 void OnCollapsed( wxDataViewEvent &event );
88 void OnSelectionChanged( wxDataViewEvent &event );
89
90 void OnEditingStarted( wxDataViewEvent &event );
91 void OnEditingDone( wxDataViewEvent &event );
92
93 void OnHeaderClick( wxDataViewEvent &event );
94 void OnHeaderRightClick( wxDataViewEvent &event );
95 void OnSorted( wxDataViewEvent &event );
96
97 void OnContextMenu( wxDataViewEvent &event );
98
99 void OnRightClick( wxMouseEvent &event );
100 void OnGoto( wxCommandEvent &event);
101 void OnAddMany( wxCommandEvent &event);
102
103 void OnBeginDrag( wxDataViewEvent &event );
104 void OnDropPossible( wxDataViewEvent &event );
105 void OnDrop( wxDataViewEvent &event );
106
107 private:
108 wxNotebook* m_notebook;
109
110 // the controls stored in the various tabs of the main notebook:
111
112 wxDataViewCtrl* m_myMusicModelViewCtrl;
113 wxObjectDataPtr<MyMusicTreeModel> m_music_model;
114
115 wxDataViewCtrl* m_myListModelViewCtrl;
116 wxObjectDataPtr<MyListModel> m_list_model;
117
118 wxDataViewListCtrl* m_listctrl;
119 wxDataViewTreeCtrl* m_treectrl;
120
121 // other data:
122
123 wxDataViewColumn* m_col;
124
125 wxTextCtrl* m_log;
126 wxLog *m_logOld;
127
128 private:
129 DECLARE_EVENT_TABLE()
130 };
131
132
133 // ----------------------------------------------------------------------------
134 // MyCustomRenderer
135 // ----------------------------------------------------------------------------
136
137 class MyCustomRenderer: public wxDataViewCustomRenderer
138 {
139 public:
140 MyCustomRenderer( wxDataViewCellMode mode, int alignment ) :
141 wxDataViewCustomRenderer( wxString("long"), mode, alignment )
142 { m_height = 25; }
143
144 virtual bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) )
145 {
146 dc->SetBrush( *wxRED_BRUSH );
147 dc->SetPen( *wxTRANSPARENT_PEN );
148 dc->DrawRectangle( rect );
149 return true;
150 }
151
152
153 virtual bool Activate( wxRect WXUNUSED(cell),
154 wxDataViewModel *WXUNUSED(model),
155 const wxDataViewItem &WXUNUSED(item),
156 unsigned int WXUNUSED(col) )
157 {
158 wxLogMessage( wxT("MyCustomRenderer Activate()") );
159 return false;
160 }
161
162 virtual bool LeftClick( wxPoint cursor, wxRect WXUNUSED(cell),
163 wxDataViewModel *WXUNUSED(model),
164 const wxDataViewItem &WXUNUSED(item),
165 unsigned int WXUNUSED(col) )
166 {
167 wxLogMessage( wxT("MyCustomRenderer LeftClick( %d, %d )"), cursor.x, cursor.y );
168 return false;
169 }
170
171 virtual wxSize GetSize() const
172 {
173 //return wxSize(60,m_height);
174 return wxSize(60,20);
175 }
176
177 virtual bool SetValue( const wxVariant &value )
178 {
179 m_height = value;
180 return true;
181 }
182
183 virtual bool GetValue( wxVariant &WXUNUSED(value) ) const { return true; }
184
185 private:
186 long m_height;
187 };
188
189
190 // ============================================================================
191 // implementation
192 // ============================================================================
193
194 // ----------------------------------------------------------------------------
195 // MyApp
196 // ----------------------------------------------------------------------------
197
198 IMPLEMENT_APP(MyApp)
199
200 bool MyApp::OnInit()
201 {
202 if ( !wxApp::OnInit() )
203 return false;
204
205 MyFrame *frame =
206 new MyFrame(NULL, wxT("wxDataViewCtrl sample"), 40, 40, 1000, 540);
207 SetTopWindow(frame);
208
209 frame->Show(true);
210 return true;
211 }
212
213
214 // ----------------------------------------------------------------------------
215 // MyFrame
216 // ----------------------------------------------------------------------------
217
218 enum
219 {
220 // file menu
221 ID_ABOUT = wxID_ABOUT,
222 ID_EXIT = wxID_EXIT,
223
224 ID_MUSIC_CTRL = 50,
225
226 ID_ADD_MOZART = 100,
227 ID_DELETE_MUSIC = 101,
228 ID_DELETE_YEAR = 102,
229 ID_SELECT_NINTH = 103,
230
231 ID_PREPEND_LIST = 200,
232 ID_DELETE_LIST = 201,
233 ID_GOTO = 202,
234 ID_ADD_MANY = 203
235 };
236
237 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
238 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
239 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
240 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
241 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
242 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
243 EVT_BUTTON( ID_SELECT_NINTH, MyFrame::OnSelectNinth )
244 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
245 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
246 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
247 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
248
249 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
250
251 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
252 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
253 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
254 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
255 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
256 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
257
258 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
259 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
260
261 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
262 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
263 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
264
265 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
266
267 EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
268 EVT_DATAVIEW_ITEM_DROP_POSSIBLE( ID_MUSIC_CTRL, MyFrame::OnDropPossible )
269 EVT_DATAVIEW_ITEM_DROP( ID_MUSIC_CTRL, MyFrame::OnDrop )
270
271 EVT_RIGHT_UP(MyFrame::OnRightClick)
272 END_EVENT_TABLE()
273
274 MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
275 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
276 {
277 m_log = NULL;
278 m_col = NULL;
279
280 SetIcon(wxICON(sample));
281
282
283 // build the menus
284 // ----------------
285
286 wxMenu *file_menu = new wxMenu;
287 file_menu->Append(ID_ABOUT, wxT("&About"));
288 file_menu->AppendSeparator();
289 file_menu->Append(ID_EXIT, wxT("E&xit"));
290
291 wxMenuBar *menu_bar = new wxMenuBar;
292 menu_bar->Append(file_menu, wxT("&File"));
293
294 SetMenuBar(menu_bar);
295 CreateStatusBar();
296
297
298 // first page of the notebook
299 // --------------------------
300
301 m_notebook = new wxNotebook( this, wxID_ANY );
302
303 wxPanel *firstPanel = new wxPanel( m_notebook, wxID_ANY );
304 m_myMusicModelViewCtrl =
305 new wxDataViewCtrl( firstPanel, ID_MUSIC_CTRL, wxDefaultPosition,
306 wxDefaultSize, wxDV_MULTIPLE|wxDV_VARIABLE_LINE_HEIGHT );
307
308 m_music_model = new MyMusicTreeModel;
309 m_myMusicModelViewCtrl->AssociateModel( m_music_model.get() );
310
311 m_myMusicModelViewCtrl->EnableDragSource( wxDF_UNICODETEXT );
312 m_myMusicModelViewCtrl->EnableDropTarget( wxDF_UNICODETEXT );
313
314 // column 0 of the view control:
315
316 wxDataViewTextRenderer *tr =
317 new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT );
318 wxDataViewColumn *column0 =
319 new wxDataViewColumn( wxT("title"), tr, 0, 200, wxALIGN_LEFT,
320 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE );
321 m_myMusicModelViewCtrl->AppendColumn( column0 );
322 #if 0
323 // Call this and sorting is enabled
324 // immediatly upon start up.
325 column0->SetAsSortKey();
326 #endif
327
328 // column 1 of the view control:
329
330 tr = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
331 wxDataViewColumn *column1 =
332 new wxDataViewColumn( wxT("artist"), tr, 1, 150, wxALIGN_LEFT,
333 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE |
334 wxDATAVIEW_COL_RESIZABLE );
335 column1->SetMinWidth(150); // this column can't be resized to be smaller
336 m_myMusicModelViewCtrl->AppendColumn( column1 );
337
338 // column 2 of the view control:
339
340 wxDataViewSpinRenderer *sr =
341 new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
342 wxDataViewColumn *column2 =
343 new wxDataViewColumn( wxT("year"), sr, 2, 60, wxALIGN_LEFT,
344 wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE );
345 m_myMusicModelViewCtrl->AppendColumn( column2 );
346
347 // column 3 of the view control:
348
349 wxArrayString choices;
350 choices.Add( "good" );
351 choices.Add( "bad" );
352 choices.Add( "lousy" );
353 wxDataViewChoiceRenderer *c =
354 new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT );
355 wxDataViewColumn *column3 =
356 new wxDataViewColumn( wxT("rating"), c, 3, 100, wxALIGN_LEFT,
357 wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE );
358 m_myMusicModelViewCtrl->AppendColumn( column3 );
359
360 // column 4 of the view control:
361
362 m_myMusicModelViewCtrl->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT, 80 );
363
364 // column 5 of the view control:
365
366 MyCustomRenderer *cr = new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE, wxALIGN_RIGHT );
367 wxDataViewColumn *column5 =
368 new wxDataViewColumn( wxT("custom"), cr, 5, -1, wxALIGN_LEFT,
369 wxDATAVIEW_COL_RESIZABLE );
370 m_myMusicModelViewCtrl->AppendColumn( column5 );
371
372 // complete this page:
373
374 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
375 button_sizer->Add( new wxButton( firstPanel, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 );
376 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 );
377 button_sizer->Add( new wxButton( firstPanel, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 );
378 button_sizer->Add( new wxButton( firstPanel, ID_SELECT_NINTH,_("Select Ninth")), 0, wxALL, 10 );
379
380 wxSizer *firstPanelSz = new wxBoxSizer( wxVERTICAL );
381 m_myMusicModelViewCtrl->SetMinSize(wxSize(-1, 200));
382 firstPanelSz->Add(m_myMusicModelViewCtrl, 1, wxGROW|wxALL, 5);
383 firstPanelSz->Add(button_sizer);
384 firstPanel->SetSizerAndFit(firstPanelSz);
385
386
387 // second page of the notebook
388 // ---------------------------
389
390 wxPanel *secondPanel = new wxPanel( m_notebook, wxID_ANY );
391
392 m_myListModelViewCtrl = new wxDataViewCtrl( secondPanel, wxID_ANY, wxDefaultPosition,
393 wxDefaultSize, wxDV_MULTIPLE | wxDV_ROW_LINES);
394
395 m_list_model = new MyListModel;
396 m_myListModelViewCtrl->AssociateModel( m_list_model.get() );
397
398 // the various columns
399 #if 1
400 m_myListModelViewCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE, 120 );
401 m_myListModelViewCtrl->AppendIconTextColumn(wxIcon(wx_small_xpm), 1, wxDATAVIEW_CELL_INERT )->SetTitle( wxT("icon") );
402 #else
403 m_myListModelViewCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE );
404 m_myListModelViewCtrl->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT );
405 #endif
406 m_myListModelViewCtrl->AppendColumn(
407 new wxDataViewColumn(wxT("attributes"), new wxDataViewTextRendererAttr, 2 ));
408
409 // complete this page:
410
411 wxBoxSizer *button_sizer2 = new wxBoxSizer( wxHORIZONTAL );
412 button_sizer2->Add( new wxButton( secondPanel, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 10 );
413 button_sizer2->Add( new wxButton( secondPanel, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 10 );
414 button_sizer2->Add( new wxButton( secondPanel, ID_GOTO, _("Goto 50")), 0, wxALL, 10 );
415 button_sizer2->Add( new wxButton( secondPanel, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 10 );
416
417 wxSizer *secondPanelSz = new wxBoxSizer( wxVERTICAL );
418 secondPanelSz->Add(m_myListModelViewCtrl, 1, wxGROW|wxALL, 5);
419 secondPanelSz->Add(button_sizer2);
420 secondPanel->SetSizerAndFit(secondPanelSz);
421
422
423
424 // third page of the notebook
425 // ---------------------------
426
427 wxPanel *thirdPanel = new wxPanel( m_notebook, wxID_ANY );
428
429 m_listctrl = new wxDataViewListCtrl( thirdPanel, wxID_ANY );
430 m_listctrl->AppendToggleColumn( wxT("Toggle") );
431 m_listctrl->AppendTextColumn( wxT("Text") );
432
433 wxVector<wxVariant> data;
434 data.push_back( true );
435 data.push_back( "row 1" );
436 m_listctrl->AppendItem( data );
437
438 data.clear();
439 data.push_back( false );
440 data.push_back( "row 3" );
441 m_listctrl->AppendItem( data );
442
443 // complete this page:
444
445 wxSizer *thirdPanelSz = new wxBoxSizer( wxVERTICAL );
446 thirdPanelSz->Add(m_listctrl, 1, wxGROW|wxALL, 5);
447 thirdPanel->SetSizerAndFit(thirdPanelSz);
448
449
450 // fourth page of the notebook
451 // ---------------------------
452
453 wxPanel *fourthPanel = new wxPanel( m_notebook, wxID_ANY );
454
455 m_treectrl = new wxDataViewTreeCtrl( fourthPanel, wxID_ANY );
456
457 wxImageList *ilist = new wxImageList( 16, 16 );
458 ilist->Add( wxIcon(wx_small_xpm) );
459 m_treectrl->SetImageList( ilist );
460
461 wxDataViewItem parent2 = m_treectrl->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
462 m_treectrl->AppendItem( parent2, wxT("Child 1"), 0 );
463 m_treectrl->AppendItem( parent2, wxT("Child 2"), 0 );
464 m_treectrl->AppendItem( parent2, wxT("Child 3, very long, long, long, long"), 0 );
465
466 // complete this page:
467
468 wxSizer *fourthPanelSz = new wxBoxSizer( wxVERTICAL );
469 fourthPanelSz->Add(m_treectrl, 1, wxGROW|wxALL, 5);
470 fourthPanel->SetSizerAndFit(fourthPanelSz);
471
472
473
474 // complete GUI
475 // ------------
476
477 m_notebook->AddPage(firstPanel, "MyMusicTreeModel");
478 m_notebook->AddPage(secondPanel, "MyListModel");
479 m_notebook->AddPage(thirdPanel, "wxDataViewListCtrl");
480 m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl");
481
482 wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
483
484 m_log = new wxTextCtrl( this, wxID_ANY, wxString(), wxDefaultPosition,
485 wxDefaultSize, wxTE_MULTILINE );
486 m_log->SetMinSize(wxSize(-1, 100));
487 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
488 wxLogMessage(_("This is the log window"));
489
490 mainSizer->Add( m_notebook, 1, wxGROW );
491 mainSizer->Add( m_log, 0, wxGROW );
492
493 SetSizerAndFit(mainSizer);
494 }
495
496 MyFrame::~MyFrame()
497 {
498 delete wxLog::SetActiveTarget(m_logOld);
499 }
500
501 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
502 {
503 Close(true);
504 }
505
506 void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
507 {
508 m_music_model->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
509 }
510
511 void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
512 {
513 wxDataViewItemArray items;
514 int len = m_myMusicModelViewCtrl->GetSelections( items );
515 for( int i = 0; i < len; i ++ )
516 if (items[i].IsOk())
517 m_music_model->Delete( items[i] );
518 }
519
520 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
521 {
522 m_myMusicModelViewCtrl->DeleteColumn( m_myMusicModelViewCtrl->GetColumn( 2 ) );
523 FindWindow( ID_DELETE_YEAR )->Disable();
524 }
525
526 void MyFrame::OnSelectNinth( wxCommandEvent& WXUNUSED(event) )
527 {
528 m_myMusicModelViewCtrl->Select( m_music_model->GetNinthItem() );
529 }
530
531 void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
532 {
533 m_list_model->Prepend(wxT("Test"));
534 }
535
536 void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
537 {
538 wxDataViewItemArray items;
539 int len = m_myListModelViewCtrl->GetSelections( items );
540 if (len > 0)
541 m_list_model->DeleteItems( items );
542 }
543
544 void MyFrame::OnValueChanged( wxDataViewEvent &event )
545 {
546 if (!m_log)
547 return;
548
549 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"),
550 event.GetItem().GetID(), event.GetColumn() );
551 }
552
553 void MyFrame::OnActivated( wxDataViewEvent &event )
554 {
555 if(!m_log)
556 return;
557
558 wxString title = m_music_model->GetTitle( event.GetItem() );
559 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title );
560
561 if (m_myMusicModelViewCtrl->IsExpanded( event.GetItem() ))
562 wxLogMessage(wxT("Item: %s is expanded"), title );
563 }
564
565 void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
566 {
567 if(!m_log)
568 return;
569
570 wxString title = m_music_model->GetTitle( event.GetItem() );
571 if (title.empty())
572 title = wxT("None");
573
574 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title );
575 }
576
577 void MyFrame::OnExpanding( wxDataViewEvent &event )
578 {
579 if (!m_log)
580 return;
581
582 wxString title = m_music_model->GetTitle( event.GetItem() );
583 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title );
584 }
585
586
587 void MyFrame::OnEditingStarted( wxDataViewEvent &event )
588 {
589 if (!m_log)
590 return;
591
592 wxString title = m_music_model->GetTitle( event.GetItem() );
593 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title );
594 }
595
596 void MyFrame::OnEditingDone( wxDataViewEvent &event )
597 {
598 if (!m_log)
599 return;
600
601 wxString title = m_music_model->GetTitle( event.GetItem() );
602 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title );
603 }
604
605 void MyFrame::OnExpanded( wxDataViewEvent &event )
606 {
607 if (!m_log)
608 return;
609
610 wxString title = m_music_model->GetTitle( event.GetItem() );
611 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title );
612 }
613
614 void MyFrame::OnCollapsing( wxDataViewEvent &event )
615 {
616 if (!m_log)
617 return;
618
619 wxString title = m_music_model->GetTitle( event.GetItem() );
620 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title );
621 }
622
623 void MyFrame::OnCollapsed( wxDataViewEvent &event )
624 {
625 if (!m_log)
626 return;
627
628 wxString title = m_music_model->GetTitle( event.GetItem() );
629 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title);
630 }
631
632 void MyFrame::OnContextMenu( wxDataViewEvent &event )
633 {
634 if (!m_log)
635 return;
636
637 wxString title = m_music_model->GetTitle( event.GetItem() );
638 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title );
639
640 wxMenu menu;
641 menu.Append( 1, wxT("entry 1") );
642 menu.Append( 2, wxT("entry 2") );
643 menu.Append( 3, wxT("entry 3") );
644
645 m_myMusicModelViewCtrl->PopupMenu(&menu);
646
647 // wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s Value: %s"),
648 // title.GetData(), event.GetValue().GetString());
649 }
650
651 void MyFrame::OnHeaderClick( wxDataViewEvent &event )
652 {
653 // we need to skip the event to let the default behaviour of sorting by
654 // this column when it is clicked to take place
655 event.Skip();
656
657 if(!m_log)
658 return;
659
660 int pos = m_myMusicModelViewCtrl->GetColumnPosition( event.GetDataViewColumn() );
661
662 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos );
663 }
664
665 void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
666 {
667 if(!m_log)
668 return;
669
670 int pos = m_myMusicModelViewCtrl->GetColumnPosition( event.GetDataViewColumn() );
671
672 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos );
673 }
674
675 void MyFrame::OnSorted( wxDataViewEvent &event )
676 {
677 if(!m_log)
678 return;
679
680 int pos = m_myMusicModelViewCtrl->GetColumnPosition( event.GetDataViewColumn() );
681
682 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos );
683 }
684
685 void MyFrame::OnRightClick( wxMouseEvent &event )
686 {
687 if(!m_log)
688 return;
689
690 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"),
691 event.GetX(), event.GetY());
692 }
693
694 void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
695 {
696 wxDataViewItem item = m_list_model->GetItem( 50 );
697 m_myListModelViewCtrl->EnsureVisible(item,m_col);
698 }
699
700 void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
701 {
702 m_list_model->AddMany();
703 }
704
705
706 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
707 {
708 wxAboutDialogInfo info;
709 info.SetName(_("DataView sample"));
710 info.SetDescription(_("This sample demonstrates wxDataViewCtrl"));
711 info.SetCopyright(_T("(C) 2007-2009 Robert Roebling"));
712 info.AddDeveloper("Robert Roebling");
713 info.AddDeveloper("Francesco Montorsi");
714
715 wxAboutBox(info);
716 }
717
718 void MyFrame::OnBeginDrag( wxDataViewEvent &event )
719 {
720 wxDataViewItem item( event.GetItem() );
721
722 // only allow drags for item, not containers
723 if (m_music_model->IsContainer( item ) )
724 {
725 event.Veto();
726 return;
727 }
728
729 MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
730 wxTextDataObject *obj = new wxTextDataObject;
731 obj->SetText( node->m_title );
732 event.SetDataObject( obj );
733 }
734
735 void MyFrame::OnDropPossible( wxDataViewEvent &event )
736 {
737 wxDataViewItem item( event.GetItem() );
738
739 // only allow drags for item, not containers
740 if (m_music_model->IsContainer( item ) )
741 event.Veto();
742
743 if (event.GetDataFormat() != wxDF_UNICODETEXT)
744 event.Veto();
745 }
746
747 void MyFrame::OnDrop( wxDataViewEvent &event )
748 {
749 wxDataViewItem item( event.GetItem() );
750
751 // only allow drops for item, not containers
752 if (m_music_model->IsContainer( item ) )
753 {
754 event.Veto();
755 return;
756 }
757
758 if (event.GetDataFormat() != wxDF_UNICODETEXT)
759 {
760 event.Veto();
761 return;
762 }
763
764 wxTextDataObject obj;
765 obj.SetData( wxDF_TEXT, event.GetDataSize(), event.GetDataBuffer() );
766
767 wxLogMessage(wxT("Text dropped: %s"), obj.GetText() );
768 }
769