]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
fixed WX_CLEAR_HASH_SET to compile
[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
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
0bcd4039 20 #include "wx/wx.h"
bd6169a6
RR
21#endif
22
b910a8ad 23#include "wx/datetime.h"
87f0efe2
RR
24#include "wx/splitter.h"
25#include "wx/aboutdlg.h"
9861f022
RR
26#include "wx/choicdlg.h"
27#include "wx/numdlg.h"
28#include "wx/dataview.h"
1e510b1e 29#include "wx/spinctrl.h"
b910a8ad 30
bd6169a6 31#ifndef __WXMSW__
0bcd4039 32 #include "../sample.xpm"
bd6169a6
RR
33#endif
34
cbc9145c
RR
35#include "null.xpm"
36
9861f022
RR
37
38#define DEFAULT_ALIGN wxALIGN_LEFT
39#define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
40
41
405a351f
RR
42// -------------------------------------
43// MySpinCtrlInPlaceRenderer
44// -------------------------------------
45
46class MySpinCtrlInPlaceRenderer: public wxDataViewCustomRenderer
47{
48public:
49 MySpinCtrlInPlaceRenderer() :
50 wxDataViewCustomRenderer( wxT("long"), wxDATAVIEW_CELL_EDITABLE ) { }
51
52
53 virtual bool HasEditorCtrl()
54 {
55 return true;
56 }
57 virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
58 {
59 long l = value;
60 return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString,
61 labelRect.GetTopLeft(), labelRect.GetSize(), -0, -1, 2010, l );
62 }
63 virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
64 {
65 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
66 long l = sc->GetValue();
67 value = l;
68 return true;
69 }
70
71 bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) )
72 {
73 wxString str;
74 str.Printf( wxT("%d"), (int) m_data );
75 dc->SetTextForeground( *wxBLACK );
76 dc->DrawText( str, rect.x, rect.y );
77 return true;
78 }
79 wxSize GetSize() const
80 {
81 return wxSize(80,16);
82 }
83 bool SetValue( const wxVariant &value )
84 {
85 m_data = value.GetLong();
86 return true;
87 }
88 bool GetValue( wxVariant &value ) const
89 {
90 value = m_data;
91 return true;
92 }
93
94private:
95 long m_data;
96};
97
98
bd6169a6 99
362d7fb9 100// -------------------------------------
5debbdcf 101// MyMusicModel
362d7fb9 102// -------------------------------------
bd6169a6 103
5debbdcf
RR
104/*
105Implement this data model
106 Title Artist Year
107-------------------------------------------------------------
1081: My Music:
109 2: Pop music
110 3: You are not alone Michael Jackson 1995
111 4: Take a bow Madonna 1994
112 5: Classical music
113 6: Ninth Symphony Ludwig v. Beethoven 1824
114 7: German Requiem Johannes Brahms 1868
115*/
4d496ecb 116
1e08ad10
RR
117
118
119class MyMusicModelNode;
120WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
121
122class MyMusicModelNode
123{
124public:
b5fce9e2 125 MyMusicModelNode( MyMusicModelNode* parent,
405a351f 126 const wxString &title, const wxString &artist, int year )
1e08ad10
RR
127 {
128 m_parent = parent;
1e08ad10
RR
129 m_title = title;
130 m_artist = artist;
131 m_year = year;
132 m_isContainer = false;
133 }
134
b5fce9e2 135 MyMusicModelNode( MyMusicModelNode* parent,
1e08ad10
RR
136 const wxString &branch )
137 {
138 m_parent = parent;
1e08ad10 139 m_title = branch;
405a351f 140 m_year = -1;
1e08ad10
RR
141 m_isContainer = true;
142 }
143
144 ~MyMusicModelNode()
145 {
146 size_t count = m_children.GetCount();
147 size_t i;
148 for (i = 0; i < count; i++)
149 {
150 MyMusicModelNode *child = m_children[i];
151 delete child;
152 }
153 }
154
1e08ad10
RR
155 bool IsContainer() { return m_isContainer; }
156
157 MyMusicModelNode* GetParent() { return m_parent; }
158 MyMusicModelNodes &GetChildren() { return m_children; }
159 MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
160 void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
161 void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
162 unsigned int GetChildCount() { return m_children.GetCount(); }
163
164public:
165 wxString m_title;
166 wxString m_artist;
405a351f 167 int m_year;
1e08ad10
RR
168
169private:
170 MyMusicModelNode *m_parent;
171 MyMusicModelNodes m_children;
1e08ad10
RR
172 bool m_isContainer;
173};
174
aba9bfd0 175
5debbdcf 176class MyMusicModel: public wxDataViewModel
bd6169a6
RR
177{
178public:
e63807a8
RR
179
180 // constructor
181
1e08ad10
RR
182 MyMusicModel()
183 {
b5fce9e2
RR
184 m_root = new MyMusicModelNode( NULL, "My Music" );
185 m_pop = new MyMusicModelNode( m_root, "Pop music" );
1e08ad10 186 m_root->Append( m_pop );
b5fce9e2 187 m_pop->Append( new MyMusicModelNode( m_pop,
405a351f 188 "You are not alone", "Michael Jackson", 1995 ) );
b5fce9e2 189 m_pop->Append( new MyMusicModelNode( m_pop,
405a351f 190 "Take a bow", "Madonna", 1994 ) );
b5fce9e2 191 m_classical = new MyMusicModelNode( m_root, "Classical music" );
1e08ad10 192 m_root->Append( m_classical );
b5fce9e2 193 m_classical->Append( new MyMusicModelNode( m_classical,
405a351f 194 "Ninth symphony", "Ludwig van Beethoven", 1824 ) );
b5fce9e2 195 m_classical->Append( new MyMusicModelNode( m_classical,
405a351f 196 "German Requiem", "Johannes Brahms", 1868 ) );
befa9b61 197 m_classicalMusicIsKnownToControl = false;
1e08ad10
RR
198 }
199
d32332aa
RR
200 // helper method for wxLog
201
202 wxString GetTitle( const wxDataViewItem &item )
203 {
204 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
205 if (!node)
206 return wxEmptyString;
207
208 return node->m_title;
209 }
210
e63807a8
RR
211 // helper methods to change the model
212
405a351f 213 void AddToClassical( const wxString &title, const wxString &artist, int year )
1e08ad10
RR
214 {
215 // add to data
216 MyMusicModelNode *child_node =
b5fce9e2 217 new MyMusicModelNode( m_classical, title, artist, year );
b7e9f8b1 218
1e08ad10
RR
219 m_classical->Append( child_node );
220
befa9b61
RR
221 if (m_classicalMusicIsKnownToControl)
222 {
223 // notify control
b5fce9e2
RR
224 wxDataViewItem child( (void*) child_node );
225 wxDataViewItem parent( (void*) m_classical );
befa9b61
RR
226 ItemAdded( parent, child );
227 }
1e08ad10 228 }
e63807a8
RR
229
230 void Delete( const wxDataViewItem &item )
231 {
b5fce9e2 232 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
9d8fe14a 233 wxDataViewItem parent( node->GetParent() );
e63807a8 234
d3f00f59
VZ
235 node->GetParent()->GetChildren().Remove( node );
236 delete node;
405a351f
RR
237
238 // notify control
239 ItemDeleted( parent, item );
e63807a8
RR
240 }
241
cf283a47
RR
242 // override sorting to always sort branches ascendingly
243
7ee7191c
RR
244 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
245 unsigned int column, bool ascending )
cf283a47 246 {
ed903e42 247 if (IsContainer(item1) && IsContainer(item2))
cf283a47
RR
248 {
249 wxVariant value1,value2;
250 GetValue( value1, item1, 0 );
251 GetValue( value2, item2, 0 );
252
253 wxString str1 = value1.GetString();
254 wxString str2 = value2.GetString();
255 int res = str1.Cmp( str2 );
256 if (res) return res;
257
258 // items must be different
259 unsigned long litem1 = (unsigned long) item1.GetID();
260 unsigned long litem2 = (unsigned long) item2.GetID();
261
262 return litem1-litem2;
263 }
264
7ee7191c 265 return wxDataViewModel::Compare( item1, item2, column, ascending );
cf283a47
RR
266 }
267
e63807a8 268 // implementation of base class virtuals to define model
5debbdcf
RR
269
270 virtual unsigned int GetColumnCount() const
a7f61f76 271 {
fc5eacdb 272 return 3;
a7f61f76 273 }
b910a8ad 274
9861f022 275 virtual wxString GetColumnType( unsigned int col ) const
5debbdcf 276 {
405a351f
RR
277 if (col == 2)
278 return "long";
279
5debbdcf
RR
280 return "string";
281 }
b910a8ad 282
5debbdcf
RR
283 virtual void GetValue( wxVariant &variant,
284 const wxDataViewItem &item, unsigned int col ) const
285 {
b5fce9e2 286 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 287 switch (col)
605c2c4a 288 {
1e08ad10
RR
289 case 0: variant = node->m_title; break;
290 case 1: variant = node->m_artist; break;
405a351f 291 case 2: variant = (long) node->m_year; break;
0bd26819
RR
292 default:
293 {
294 wxLogError( "MyMusicModel::GetValue: wrong column" );
405a351f
RR
295
296 // provoke a crash when mouse button down
0bd26819
RR
297 wxMouseState state = wxGetMouseState();
298 if (state.ShiftDown())
299 {
300 char *crash = 0;
301 *crash = 0;
302 }
303 }
a7f61f76 304 }
e152afc3 305 }
9861f022 306
5debbdcf
RR
307 virtual bool SetValue( const wxVariant &variant,
308 const wxDataViewItem &item, unsigned int col )
9861f022 309 {
b5fce9e2 310 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10
RR
311 switch (col)
312 {
c2a92c80
RR
313 case 0: node->m_title = variant.GetString(); return true;
314 case 1: node->m_artist = variant.GetString(); return true;
315 case 2: node->m_year = variant.GetLong(); return true;
1e08ad10
RR
316 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
317 }
c2a92c80 318 return false;
9861f022
RR
319 }
320
ed903e42 321 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
b910a8ad 322 {
ed903e42
RR
323 // the invisble root node has no parent
324 if (!item.IsOk())
325 return wxDataViewItem(0);
326
b5fce9e2 327 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
ed903e42
RR
328
329 // "MyMusic" also has no parent
330 if (node == m_root)
331 return wxDataViewItem(0);
332
333 return wxDataViewItem( (void*) node->GetParent() );
334 }
335
336 virtual bool IsContainer( const wxDataViewItem &item ) const
337 {
338 // the invisble root node can have children (in
339 // our model always "MyMusic")
340 if (!item.IsOk())
1e08ad10
RR
341 return true;
342
ed903e42 343 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 344 return node->IsContainer();
e152afc3 345 }
5debbdcf 346
74fe973b 347 virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const
1e510b1e 348 {
b5fce9e2
RR
349 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
350 if (!node)
74fe973b
RR
351 {
352 array.Add( wxDataViewItem( (void*) m_root ) );
353 return 1;
354 }
1e08ad10 355
befa9b61
RR
356 if (node == m_classical)
357 {
358 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
359 model->m_classicalMusicIsKnownToControl = true;
360 }
1e510b1e 361
74fe973b
RR
362 if (node->GetChildCount() == 0)
363 {
364 return 0;
365 }
366
367 unsigned int count = node->GetChildren().GetCount();
368 unsigned int pos;
369 for (pos = 0; pos < count; pos++)
370 {
371 MyMusicModelNode *child = node->GetChildren().Item( pos );
372 array.Add( wxDataViewItem( (void*) child ) );
373 }
374 return count;
9861f022 375 }
1e08ad10 376
1e08ad10 377private:
1e08ad10
RR
378 MyMusicModelNode* m_root;
379 MyMusicModelNode* m_pop;
380 MyMusicModelNode* m_classical;
befa9b61 381 bool m_classicalMusicIsKnownToControl;
241cb04b
RR
382};
383
c534e696
RR
384class MyListModel: public wxDataViewIndexListModel
385{
386public:
387 MyListModel() :
388 wxDataViewIndexListModel( 100 )
389 {
390 unsigned int i;
391 for (i = 0; i < 100; i++)
392 {
393 wxString str;
394 str.Printf( "row number %d", i );
395 m_array.Add( str );
396 }
c9c13e70
RR
397
398 m_icon = wxIcon( null_xpm );
c534e696
RR
399 }
400
401 // helper methods to change the model
402
403 void Prepend( const wxString &text )
404 {
405 m_array.Insert( text, 0 );
406 RowPrepended();
407 }
408
409 void DeleteItem( const wxDataViewItem &item )
410 {
411 unsigned int row = GetRow( item );
412 m_array.RemoveAt( row );
413 RowDeleted( row );
414 }
415
416 // implementation of base class virtuals to define model
417
418 virtual unsigned int GetColumnCount() const
419 {
c9c13e70 420 return 3;
c534e696
RR
421 }
422
423 virtual wxString GetColumnType( unsigned int col ) const
424 {
c9c13e70
RR
425 if (col == 1)
426 return "wxDataViewIconText";
427
c534e696
RR
428 return "string";
429 }
430
431 virtual unsigned int GetRowCount()
432 {
433 return m_array.GetCount();
434 }
435
436 virtual void GetValue( wxVariant &variant,
437 unsigned int row, unsigned int col ) const
438 {
439 if (col==0)
440 {
441 variant = m_array[ row ];
c9c13e70
RR
442 } else
443 if (col==1)
444 {
445 wxDataViewIconText data( "test", m_icon );
446 variant << data;
c534e696
RR
447 }
448 else
449 {
450 wxString str;
451 str.Printf( "row %d col %d", row, col );
452 variant = str;
453 }
454 }
455
456 virtual bool SetValue( const wxVariant &variant,
457 unsigned int row, unsigned int col )
458 {
459 if (col == 0)
460 {
461 m_array[row] = variant.GetString();
462 return true;
463 }
464
465 return false;
466 }
467
468 wxArrayString m_array;
c9c13e70 469 wxIcon m_icon;
c534e696
RR
470};
471
362d7fb9
RR
472// -------------------------------------
473// MyApp
474// -------------------------------------
bd6169a6
RR
475
476class MyApp: public wxApp
477{
478public:
479 bool OnInit(void);
87f0efe2 480 int OnExit();
bd6169a6
RR
481};
482
362d7fb9
RR
483// -------------------------------------
484// MyFrame
485// -------------------------------------
486
87f0efe2 487class MyFrame : public wxFrame
bd6169a6
RR
488{
489public:
490 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
491
492public:
493 void OnQuit(wxCommandEvent& event);
494 void OnAbout(wxCommandEvent& event);
c534e696
RR
495
496 void OnAddMozart(wxCommandEvent& event);
497 void OnDeleteMusic(wxCommandEvent& event);
498
499 void OnPrependList(wxCommandEvent& event);
500 void OnDeleteList(wxCommandEvent& event);
b910a8ad 501
d8331a01
RR
502 void OnValueChanged( wxDataViewEvent &event );
503 void OnItemAdded( wxDataViewEvent &event );
504 void OnItemDeleted( wxDataViewEvent &event );
718fd180 505
b7e9f8b1 506 void OnActivated( wxDataViewEvent &event );
718fd180
RR
507 void OnExpanding( wxDataViewEvent &event );
508 void OnExpanded( wxDataViewEvent &event );
509 void OnCollapsing( wxDataViewEvent &event );
510 void OnCollapsed( wxDataViewEvent &event );
aed836f3 511 void OnSelectionChanged( wxDataViewEvent &event );
718fd180 512
d14e1c3a
RR
513 void OnEditingStarted( wxDataViewEvent &event );
514 void OnEditingDone( wxDataViewEvent &event );
515
b7e9f8b1
RR
516 void OnHeaderClick( wxDataViewEvent &event );
517 void OnHeaderRightClick( wxDataViewEvent &event );
518 void OnSorted( wxDataViewEvent &event );
519
520 void OnRightClick( wxMouseEvent &event );
521 void OnGoto( wxCommandEvent &event);
d8331a01 522
bd6169a6 523private:
c534e696
RR
524 wxDataViewCtrl* m_musicCtrl;
525 wxObjectDataPtr<MyMusicModel> m_music_model;
526
527 wxDataViewCtrl* m_listCtrl;
528 wxObjectDataPtr<MyListModel> m_list_model;
fbda518c
RR
529
530 wxDataViewColumn * m_col;
c534e696 531
1e08ad10 532 wxTextCtrl * m_log;
b7e9f8b1 533 wxLog *m_logOld;
31fb32e1 534
241cb04b 535private:
abd6692c 536 DECLARE_EVENT_TABLE()
241cb04b
RR
537};
538
362d7fb9
RR
539// -------------------------------------
540// MyApp
541// -------------------------------------
542
87f0efe2 543IMPLEMENT_APP(MyApp)
bd6169a6 544
bd6169a6
RR
545bool MyApp::OnInit(void)
546{
45e6e6f8
VZ
547 if ( !wxApp::OnInit() )
548 return false;
549
87f0efe2
RR
550 // build the first frame
551 MyFrame *frame =
405a351f 552 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 440);
bd6169a6
RR
553 frame->Show(true);
554
555 SetTopWindow(frame);
bd6169a6
RR
556 return true;
557}
558
87f0efe2
RR
559int MyApp::OnExit()
560{
561 return 0;
562}
563
564
bd6169a6
RR
565// -------------------------------------
566// MyFrame
567// -------------------------------------
568
87f0efe2
RR
569enum
570{
571 // file menu
572 ID_ABOUT = wxID_ABOUT,
87f0efe2 573 ID_EXIT = wxID_EXIT,
1e08ad10 574
d8331a01
RR
575 ID_MUSIC_CTRL = 50,
576
c534e696
RR
577 ID_ADD_MOZART = 100,
578 ID_DELETE_MUSIC = 101,
579
580 ID_PREPEND_LIST = 200,
b7e9f8b1 581 ID_DELETE_LIST = 201,
405a351f 582 ID_GOTO = 202
87f0efe2
RR
583};
584
585BEGIN_EVENT_TABLE(MyFrame, wxFrame)
87f0efe2 586 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
87f0efe2 587 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
c534e696
RR
588 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
589 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
590 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
591 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
b7e9f8b1 592 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
718fd180 593
d8331a01
RR
594 EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL, MyFrame::OnItemAdded )
595 EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL, MyFrame::OnItemDeleted )
596 EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
b7e9f8b1 597 EVT_DATAVIEW_MODEL_ITEM_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
718fd180 598
b7e9f8b1 599 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
718fd180
RR
600 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
601 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
602 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
603 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
aed836f3 604 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
718fd180 605
d14e1c3a
RR
606 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
607 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
608
b7e9f8b1
RR
609 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
610 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
611 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
718fd180 612
b7e9f8b1 613 EVT_RIGHT_UP(MyFrame::OnRightClick)
87f0efe2
RR
614END_EVENT_TABLE()
615
bd6169a6
RR
616MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
617 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
618{
d8331a01 619 m_log = NULL;
fbda518c 620 m_col = NULL;
d8331a01 621
0bcd4039 622 SetIcon(wxICON(sample));
bd6169a6 623
87f0efe2
RR
624 // build the menus:
625
bd6169a6 626 wxMenu *file_menu = new wxMenu;
5debbdcf 627 file_menu->Append(ID_ABOUT, "&About");
87f0efe2 628 file_menu->AppendSeparator();
5debbdcf 629 file_menu->Append(ID_EXIT, "E&xit");
bd6169a6 630
bd6169a6 631 wxMenuBar *menu_bar = new wxMenuBar;
5debbdcf 632 menu_bar->Append(file_menu, "&File");
87f0efe2 633
bd6169a6 634 SetMenuBar(menu_bar);
87f0efe2 635 CreateStatusBar();
bd6169a6 636
1e08ad10
RR
637 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
638
c534e696 639 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
bd6169a6 640
c534e696 641 // MyMusic
b910a8ad 642
d8331a01 643 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
b7e9f8b1 644 wxDefaultSize, wxDV_MULTIPLE );
c534e696
RR
645
646 m_music_model = new MyMusicModel;
647 m_musicCtrl->AssociateModel( m_music_model.get() );
648
0bd26819 649 wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
94b1f7bc 650 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
0bd26819
RR
651#if 0
652 // Call this and sorting is enabled
653 // immediatly upon start up.
654 col->SetSortOrder( true );
655#endif
656
405a351f 657 m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 150,
94b1f7bc 658 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
405a351f
RR
659
660 MySpinCtrlInPlaceRenderer *sr = new MySpinCtrlInPlaceRenderer;
661 wxDataViewColumn *column = new wxDataViewColumn( "year", sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE );
662 m_musicCtrl->AppendColumn( column );
1e08ad10 663
c534e696
RR
664 data_sizer->Add( m_musicCtrl, 3, wxGROW );
665
666#if 1
667
668 // MyList
669
670 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
b7e9f8b1 671 wxDefaultSize, wxDV_MULTIPLE );
c534e696
RR
672
673 m_list_model = new MyListModel;
674 m_listCtrl->AssociateModel( m_list_model.get() );
675
676 m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
b04fcede
RR
677 m_listCtrl->AppendIconTextColumn( "icon", 1, wxDATAVIEW_CELL_INERT, 60 );
678 m_listCtrl->AppendTextColumn( "index", 2, wxDATAVIEW_CELL_INERT, 120 );
c534e696
RR
679
680 data_sizer->Add( m_listCtrl, 2, wxGROW );
681
682#endif
683
684 main_sizer->Add( data_sizer, 2, wxGROW );
1e08ad10
RR
685
686 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
687
c534e696
RR
688 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 );
689 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 );
690 button_sizer->Add( 10, 10, 1 );
691 button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 );
692 button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
b7e9f8b1 693 button_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
1e08ad10 694
c534e696 695 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
1e08ad10
RR
696
697 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
b7e9f8b1
RR
698 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
699 wxLogMessage("This is the log window");
700
1e08ad10
RR
701 main_sizer->Add( m_log, 1, wxGROW );
702
703 SetSizer( main_sizer );
bd6169a6
RR
704}
705
706void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
707{
708 Close(true);
709}
710
c534e696
RR
711void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
712{
405a351f 713 m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
c534e696
RR
714}
715
716void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
1e08ad10 717{
b7e9f8b1
RR
718 wxDataViewItemArray items;
719 int len = m_musicCtrl->GetSelections( items );
720 for( int i = 0; i < len; i ++ )
721 if (items[i].IsOk())
722 m_music_model->Delete( items[i] );
e63807a8
RR
723}
724
c534e696 725void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
e63807a8 726{
c534e696
RR
727 m_list_model->Prepend( "Test" );
728}
729
730void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
731{
b7e9f8b1
RR
732 wxDataViewItemArray items;
733 int len = m_listCtrl->GetSelections( items );
734 for( int i = 0; i < len; i ++ )
735 if (items[i].IsOk())
736 m_list_model->DeleteItem( items[i] );
1e08ad10
RR
737}
738
d8331a01
RR
739void MyFrame::OnItemAdded( wxDataViewEvent &event )
740{
741 if (!m_log)
742 return;
743
b7e9f8b1 744 wxLogMessage("wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, Item Id: %d",event.GetItem().GetID());
d8331a01
RR
745}
746
747void MyFrame::OnItemDeleted( wxDataViewEvent &event )
748{
749 if (!m_log)
750 return;
751
b7e9f8b1 752 wxLogMessage( "EVT_DATAVIEW_MODEL_ITEM_DELETED, Item Id: %d", event.GetItem().GetID() );
d8331a01
RR
753}
754
755void MyFrame::OnValueChanged( wxDataViewEvent &event )
756{
757 if (!m_log)
758 return;
759
b7e9f8b1
RR
760 wxLogMessage( "EVT_DATAVIEW_MODEL_VALUE_CHANGED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn() );
761}
762
763void MyFrame::OnActivated( wxDataViewEvent &event )
764{
765 if(!m_log)
766 return;
767
d32332aa
RR
768 wxString title = m_music_model->GetTitle( event.GetItem() );
769 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s", title );
b7e9f8b1
RR
770}
771
aed836f3 772void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
6848478c
RR
773{
774 if(!m_log)
775 return;
776
d32332aa
RR
777 wxString title = m_music_model->GetTitle( event.GetItem() );
778 if (title.empty())
779 title = "None";
780
781 wxLogMessage("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title );
6848478c
RR
782}
783
718fd180
RR
784void MyFrame::OnExpanding( wxDataViewEvent &event )
785{
786 if (!m_log)
787 return;
788
d32332aa
RR
789 wxString title = m_music_model->GetTitle( event.GetItem() );
790 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title );
718fd180
RR
791}
792
d14e1c3a
RR
793
794void MyFrame::OnEditingStarted( wxDataViewEvent &event )
795{
796 if (!m_log)
797 return;
798
d32332aa
RR
799 wxString title = m_music_model->GetTitle( event.GetItem() );
800 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title );
d14e1c3a
RR
801}
802
803void MyFrame::OnEditingDone( wxDataViewEvent &event )
804{
805 if (!m_log)
806 return;
807
d32332aa
RR
808 wxString title = m_music_model->GetTitle( event.GetItem() );
809 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title );
d14e1c3a
RR
810}
811
718fd180
RR
812void MyFrame::OnExpanded( wxDataViewEvent &event )
813{
814 if (!m_log)
815 return;
816
d32332aa
RR
817 wxString title = m_music_model->GetTitle( event.GetItem() );
818 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title );
718fd180
RR
819}
820
821void MyFrame::OnCollapsing( wxDataViewEvent &event )
822{
823 if (!m_log)
824 return;
825
d32332aa
RR
826 wxString title = m_music_model->GetTitle( event.GetItem() );
827 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title );
718fd180
RR
828}
829
830void MyFrame::OnCollapsed( wxDataViewEvent &event )
831{
832 if (!m_log)
833 return;
d32332aa
RR
834
835 wxString title = m_music_model->GetTitle( event.GetItem() );
836 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title );
718fd180
RR
837}
838
b7e9f8b1
RR
839void MyFrame::OnHeaderClick( wxDataViewEvent &event )
840{
841 if(!m_log)
842 return;
aed836f3
RR
843
844 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
b7e9f8b1 845
aed836f3 846 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
b7e9f8b1
RR
847}
848
849void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
850{
851 if(!m_log)
852 return;
853
dadc879e
RR
854 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
855
856 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos );
b7e9f8b1
RR
857}
858
859void MyFrame::OnSorted( wxDataViewEvent &event )
860{
861 if(!m_log)
862 return;
863
d32332aa
RR
864 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
865
866 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos );
b7e9f8b1
RR
867}
868
869void MyFrame::OnRightClick( wxMouseEvent &event )
870{
871 if(!m_log)
872 return;
873
874 wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event.GetX(), event.GetY());
875}
876
877void MyFrame::OnGoto( wxCommandEvent &event)
878{
718fd180 879 wxDataViewItem item = m_list_model->GetItem( 50 );
fbda518c 880 m_listCtrl->EnsureVisible(item,m_col);
d8331a01
RR
881}
882
bd6169a6
RR
883void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
884{
87f0efe2
RR
885 wxAboutDialogInfo info;
886 info.SetName(_("DataView sample"));
887 info.SetDescription(_("This sample demonstrates the dataview control handling"));
888 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
bd6169a6 889
87f0efe2 890 wxAboutBox(info);
bd6169a6
RR
891}
892