]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
Improved wxDataViewSpinCtrlRenderer under OS X
[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
a75124d0
RR
37/* XPM */
38static const char *small1_xpm[] = {
39/* columns rows colors chars-per-pixel */
40"16 16 6 1",
41". c Black",
42"o c #FFFFFF",
43"X c #000080",
44"O c #FFFF00",
45" c None",
46"+ c #FF0000",
47/* pixels */
48" ",
49" ",
50" ",
51" ....... ",
52" .XXXXX. ",
53" .oXXXX. ",
54" .oXXX.......",
55".....oXXX.OOOOO.",
56".+++.XXXX.oOOOO.",
57".o++......oOOOO.",
58".o++++. .oOOOO.",
59".o++++. .OOOOO.",
60".+++++. .......",
61"....... ",
62" ",
63" "
64};
65
66
9861f022
RR
67
68#define DEFAULT_ALIGN wxALIGN_LEFT
69#define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
70
71
362d7fb9 72// -------------------------------------
5debbdcf 73// MyMusicModel
362d7fb9 74// -------------------------------------
bd6169a6 75
5debbdcf
RR
76/*
77Implement this data model
78 Title Artist Year
79-------------------------------------------------------------
801: My Music:
81 2: Pop music
82 3: You are not alone Michael Jackson 1995
83 4: Take a bow Madonna 1994
84 5: Classical music
85 6: Ninth Symphony Ludwig v. Beethoven 1824
86 7: German Requiem Johannes Brahms 1868
87*/
4d496ecb 88
1e08ad10
RR
89
90
91class MyMusicModelNode;
92WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
93
94class MyMusicModelNode
95{
96public:
b5fce9e2 97 MyMusicModelNode( MyMusicModelNode* parent,
405a351f 98 const wxString &title, const wxString &artist, int year )
1e08ad10
RR
99 {
100 m_parent = parent;
1e08ad10
RR
101 m_title = title;
102 m_artist = artist;
103 m_year = year;
104 m_isContainer = false;
105 }
106
b5fce9e2 107 MyMusicModelNode( MyMusicModelNode* parent,
1e08ad10
RR
108 const wxString &branch )
109 {
110 m_parent = parent;
1e08ad10 111 m_title = branch;
405a351f 112 m_year = -1;
1e08ad10
RR
113 m_isContainer = true;
114 }
115
116 ~MyMusicModelNode()
117 {
118 size_t count = m_children.GetCount();
119 size_t i;
120 for (i = 0; i < count; i++)
121 {
122 MyMusicModelNode *child = m_children[i];
123 delete child;
124 }
125 }
126
1e08ad10
RR
127 bool IsContainer() { return m_isContainer; }
128
129 MyMusicModelNode* GetParent() { return m_parent; }
130 MyMusicModelNodes &GetChildren() { return m_children; }
131 MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
132 void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
133 void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
134 unsigned int GetChildCount() { return m_children.GetCount(); }
135
136public:
137 wxString m_title;
138 wxString m_artist;
405a351f 139 int m_year;
1e08ad10
RR
140
141private:
142 MyMusicModelNode *m_parent;
143 MyMusicModelNodes m_children;
1e08ad10
RR
144 bool m_isContainer;
145};
146
aba9bfd0 147
5debbdcf 148class MyMusicModel: public wxDataViewModel
bd6169a6
RR
149{
150public:
e63807a8
RR
151
152 // constructor
153
1e08ad10
RR
154 MyMusicModel()
155 {
99c75ebc
RR
156 m_root = new MyMusicModelNode( NULL, wxT("My Music" ));
157 m_pop = new MyMusicModelNode( m_root, wxT("Pop music") );
1e08ad10 158 m_root->Append( m_pop );
b5fce9e2 159 m_pop->Append( new MyMusicModelNode( m_pop,
99c75ebc 160 wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
b5fce9e2 161 m_pop->Append( new MyMusicModelNode( m_pop,
99c75ebc
RR
162 wxT("Take a bow"), wxT("Madonna"), 1994 ) );
163 m_classical = new MyMusicModelNode( m_root, wxT("Classical music") );
1e08ad10 164 m_root->Append( m_classical );
b5fce9e2 165 m_classical->Append( new MyMusicModelNode( m_classical,
99c75ebc 166 wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 ) );
b5fce9e2 167 m_classical->Append( new MyMusicModelNode( m_classical,
99c75ebc 168 wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
befa9b61 169 m_classicalMusicIsKnownToControl = false;
1e08ad10 170 }
e39d30c0
RR
171
172 ~MyMusicModel()
173 {
174 delete m_root;
175 }
1e08ad10 176
d32332aa
RR
177 // helper method for wxLog
178
179 wxString GetTitle( const wxDataViewItem &item )
180 {
181 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
182 if (!node)
183 return wxEmptyString;
184
185 return node->m_title;
186 }
187
e63807a8
RR
188 // helper methods to change the model
189
405a351f 190 void AddToClassical( const wxString &title, const wxString &artist, int year )
1e08ad10
RR
191 {
192 // add to data
193 MyMusicModelNode *child_node =
b5fce9e2 194 new MyMusicModelNode( m_classical, title, artist, year );
b7e9f8b1 195
1e08ad10
RR
196 m_classical->Append( child_node );
197
befa9b61
RR
198 if (m_classicalMusicIsKnownToControl)
199 {
200 // notify control
b5fce9e2
RR
201 wxDataViewItem child( (void*) child_node );
202 wxDataViewItem parent( (void*) m_classical );
befa9b61
RR
203 ItemAdded( parent, child );
204 }
1e08ad10 205 }
e63807a8
RR
206
207 void Delete( const wxDataViewItem &item )
208 {
b5fce9e2 209 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
9d8fe14a 210 wxDataViewItem parent( node->GetParent() );
e63807a8 211
d3f00f59
VZ
212 node->GetParent()->GetChildren().Remove( node );
213 delete node;
405a351f
RR
214
215 // notify control
216 ItemDeleted( parent, item );
e63807a8
RR
217 }
218
cf283a47
RR
219 // override sorting to always sort branches ascendingly
220
7ee7191c
RR
221 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
222 unsigned int column, bool ascending )
cf283a47 223 {
ed903e42 224 if (IsContainer(item1) && IsContainer(item2))
cf283a47
RR
225 {
226 wxVariant value1,value2;
227 GetValue( value1, item1, 0 );
228 GetValue( value2, item2, 0 );
229
230 wxString str1 = value1.GetString();
231 wxString str2 = value2.GetString();
232 int res = str1.Cmp( str2 );
233 if (res) return res;
234
235 // items must be different
236 unsigned long litem1 = (unsigned long) item1.GetID();
237 unsigned long litem2 = (unsigned long) item2.GetID();
238
239 return litem1-litem2;
240 }
241
7ee7191c 242 return wxDataViewModel::Compare( item1, item2, column, ascending );
cf283a47
RR
243 }
244
e63807a8 245 // implementation of base class virtuals to define model
5debbdcf
RR
246
247 virtual unsigned int GetColumnCount() const
a7f61f76 248 {
fc5eacdb 249 return 3;
a7f61f76 250 }
b910a8ad 251
9861f022 252 virtual wxString GetColumnType( unsigned int col ) const
5debbdcf 253 {
405a351f 254 if (col == 2)
99c75ebc 255 return wxT("long");
405a351f 256
99c75ebc 257 return wxT("string");
5debbdcf 258 }
b910a8ad 259
5debbdcf
RR
260 virtual void GetValue( wxVariant &variant,
261 const wxDataViewItem &item, unsigned int col ) const
262 {
b5fce9e2 263 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 264 switch (col)
605c2c4a 265 {
1e08ad10
RR
266 case 0: variant = node->m_title; break;
267 case 1: variant = node->m_artist; break;
405a351f 268 case 2: variant = (long) node->m_year; break;
0bd26819
RR
269 default:
270 {
99c75ebc 271 wxLogError( wxT("MyMusicModel::GetValue: wrong column" ));
405a351f
RR
272
273 // provoke a crash when mouse button down
0bd26819
RR
274 wxMouseState state = wxGetMouseState();
275 if (state.ShiftDown())
276 {
277 char *crash = 0;
278 *crash = 0;
279 }
280 }
a7f61f76 281 }
e152afc3 282 }
9861f022 283
5debbdcf
RR
284 virtual bool SetValue( const wxVariant &variant,
285 const wxDataViewItem &item, unsigned int col )
9861f022 286 {
b5fce9e2 287 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10
RR
288 switch (col)
289 {
c2a92c80
RR
290 case 0: node->m_title = variant.GetString(); return true;
291 case 1: node->m_artist = variant.GetString(); return true;
292 case 2: node->m_year = variant.GetLong(); return true;
99c75ebc 293 default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") );
1e08ad10 294 }
c2a92c80 295 return false;
9861f022
RR
296 }
297
ed903e42 298 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
b910a8ad 299 {
ed903e42
RR
300 // the invisble root node has no parent
301 if (!item.IsOk())
302 return wxDataViewItem(0);
303
b5fce9e2 304 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
ed903e42
RR
305
306 // "MyMusic" also has no parent
307 if (node == m_root)
308 return wxDataViewItem(0);
309
310 return wxDataViewItem( (void*) node->GetParent() );
311 }
312
313 virtual bool IsContainer( const wxDataViewItem &item ) const
314 {
315 // the invisble root node can have children (in
316 // our model always "MyMusic")
317 if (!item.IsOk())
1e08ad10
RR
318 return true;
319
ed903e42 320 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 321 return node->IsContainer();
e152afc3 322 }
5debbdcf 323
74fe973b 324 virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const
1e510b1e 325 {
b5fce9e2
RR
326 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
327 if (!node)
74fe973b
RR
328 {
329 array.Add( wxDataViewItem( (void*) m_root ) );
330 return 1;
331 }
1e08ad10 332
befa9b61
RR
333 if (node == m_classical)
334 {
335 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
336 model->m_classicalMusicIsKnownToControl = true;
337 }
1e510b1e 338
74fe973b
RR
339 if (node->GetChildCount() == 0)
340 {
341 return 0;
342 }
343
344 unsigned int count = node->GetChildren().GetCount();
345 unsigned int pos;
346 for (pos = 0; pos < count; pos++)
347 {
348 MyMusicModelNode *child = node->GetChildren().Item( pos );
349 array.Add( wxDataViewItem( (void*) child ) );
350 }
351 return count;
9861f022 352 }
1e08ad10 353
f6f0ef85
RR
354 // DnD
355
356 virtual bool IsDraggable( const wxDataViewItem &item )
357 {
358 // only drag items
359 return (!IsContainer(item));
360 }
361
362 virtual size_t GetDragDataSize( const wxDataViewItem &item, const wxDataFormat &WXUNUSED(format) )
363 {
364 wxPrintf( "GetDragDataSize\n" );
365
366 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
367 wxString data;
368 data += node->m_title; data += wxT(" ");
369 data += node->m_artist;
370 return strlen( data.utf8_str() ) + 1;
371 }
372 virtual bool GetDragData( const wxDataViewItem &item, const wxDataFormat &WXUNUSED(format),
373 void* dest, size_t WXUNUSED(size) )
374 {
375 wxPrintf( "GetDragData\n" );
376
377 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
378 wxString data;
379 data += node->m_title; data += wxT(" ");
380 data += node->m_artist;
381 wxCharBuffer buffer( data.utf8_str() );
382 memcpy( dest, buffer, strlen(buffer)+1 );
383 return true;
384 }
385
1e08ad10 386private:
1e08ad10
RR
387 MyMusicModelNode* m_root;
388 MyMusicModelNode* m_pop;
389 MyMusicModelNode* m_classical;
befa9b61 390 bool m_classicalMusicIsKnownToControl;
241cb04b
RR
391};
392
8b6cf9bf
RR
393
394static int my_sort_reverse( int *v1, int *v2 )
395{
396 return *v2-*v1;
397}
398
399static int my_sort( int *v1, int *v2 )
400{
401 return *v1-*v2;
402}
403
c534e696
RR
404class MyListModel: public wxDataViewIndexListModel
405{
406public:
407 MyListModel() :
a75124d0 408#ifdef __WXMAC__
33ba5a05 409 wxDataViewIndexListModel( 1000 + 100 )
a75124d0 410#else
33ba5a05 411 wxDataViewIndexListModel( 100000 + 100 )
a75124d0 412#endif
c534e696 413 {
33ba5a05
RR
414#ifdef __WXMAC__
415 m_virtualItems = 1000;
416#else
417 m_virtualItems = 100000;
418#endif
419
c534e696
RR
420 unsigned int i;
421 for (i = 0; i < 100; i++)
422 {
423 wxString str;
99c75ebc 424 str.Printf( wxT("row number %d"), i );
c534e696
RR
425 m_array.Add( str );
426 }
c9c13e70
RR
427
428 m_icon = wxIcon( null_xpm );
c534e696
RR
429 }
430
431 // helper methods to change the model
432
433 void Prepend( const wxString &text )
434 {
435 m_array.Insert( text, 0 );
436 RowPrepended();
437 }
438
439 void DeleteItem( const wxDataViewItem &item )
440 {
441 unsigned int row = GetRow( item );
33ba5a05
RR
442 if (row >= m_array.GetCount())
443 return;
444
c534e696
RR
445 m_array.RemoveAt( row );
446 RowDeleted( row );
447 }
8b6cf9bf
RR
448
449 void DeleteItems( const wxDataViewItemArray &items )
450 {
451 wxArrayInt rows;
452 unsigned int i;
453 for (i = 0; i < items.GetCount(); i++)
454 {
455 unsigned int row = GetRow( items[i] );
33ba5a05
RR
456 if (row < m_array.GetCount())
457 rows.Add( row );
8b6cf9bf
RR
458 }
459
460 // Sort in descending order so that the last
461 // row will be deleted first. Otherwise the
462 // remaining indeces would all be wrong.
463 rows.Sort( my_sort_reverse );
464 for (i = 0; i < rows.GetCount(); i++)
465 m_array.RemoveAt( rows[i] );
466
467 // This is just to test if wxDataViewCtrl can
468 // cope with removing rows not sorted in
469 // descending order
470 rows.Sort( my_sort );
471 RowsDeleted( rows );
472 }
473
474 void AddMany()
475 {
33ba5a05
RR
476 m_virtualItems += 1000;
477 Reset( m_array.GetCount() + m_virtualItems );
8b6cf9bf 478 }
c534e696
RR
479
480 // implementation of base class virtuals to define model
481
482 virtual unsigned int GetColumnCount() const
483 {
c9c13e70 484 return 3;
c534e696
RR
485 }
486
487 virtual wxString GetColumnType( unsigned int col ) const
488 {
c9c13e70 489 if (col == 1)
99c75ebc 490 return wxT("wxDataViewIconText");
c9c13e70 491
99c75ebc
RR
492 return wxT("string");
493 }
494
495 virtual unsigned int GetRowCount()
496 {
497 return m_array.GetCount();
c534e696
RR
498 }
499
c534e696
RR
500 virtual void GetValue( wxVariant &variant,
501 unsigned int row, unsigned int col ) const
502 {
503 if (col==0)
504 {
a75124d0
RR
505 if (row >= m_array.GetCount())
506 {
507 wxString str;
99c75ebc 508 str.Printf(wxT("row %d"), row - m_array.GetCount() );
a75124d0
RR
509 variant = str;
510 }
511 else
512 {
513 variant = m_array[ row ];
514 }
c9c13e70
RR
515 } else
516 if (col==1)
517 {
99c75ebc 518 wxDataViewIconText data( wxT("test"), m_icon );
c9c13e70 519 variant << data;
4264606e
RR
520 } else
521 if (col==2)
c534e696 522 {
33ba5a05 523 if (row >= m_array.GetCount())
99c75ebc 524 variant = wxT("plain");
4264606e 525 else
99c75ebc 526 variant = wxT("blue");
c534e696
RR
527 }
528 }
4264606e
RR
529
530 virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr )
531 {
532 if (col != 2)
533 return false;
534
33ba5a05
RR
535 if (row < m_array.GetCount())
536 {
4264606e 537 attr.SetColour( *wxBLUE );
4264606e 538 attr.SetItalic( true );
33ba5a05 539 }
4264606e
RR
540
541 return true;
542 }
c534e696
RR
543
544 virtual bool SetValue( const wxVariant &variant,
545 unsigned int row, unsigned int col )
546 {
547 if (col == 0)
548 {
a75124d0
RR
549 if (row >= m_array.GetCount())
550 return false;
551
c534e696
RR
552 m_array[row] = variant.GetString();
553 return true;
554 }
555
556 return false;
557 }
558
559 wxArrayString m_array;
c9c13e70 560 wxIcon m_icon;
33ba5a05 561 int m_virtualItems;
c534e696
RR
562};
563
362d7fb9
RR
564// -------------------------------------
565// MyApp
566// -------------------------------------
bd6169a6
RR
567
568class MyApp: public wxApp
569{
570public:
571 bool OnInit(void);
87f0efe2 572 int OnExit();
bd6169a6
RR
573};
574
362d7fb9
RR
575// -------------------------------------
576// MyFrame
577// -------------------------------------
578
87f0efe2 579class MyFrame : public wxFrame
bd6169a6
RR
580{
581public:
e94d0c1e 582 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
e39d30c0 583 ~MyFrame();
bd6169a6
RR
584
585public:
586 void OnQuit(wxCommandEvent& event);
587 void OnAbout(wxCommandEvent& event);
c534e696
RR
588
589 void OnAddMozart(wxCommandEvent& event);
590 void OnDeleteMusic(wxCommandEvent& event);
736fe67c 591 void OnDeleteYear(wxCommandEvent& event);
c534e696
RR
592
593 void OnPrependList(wxCommandEvent& event);
594 void OnDeleteList(wxCommandEvent& event);
b910a8ad 595
d8331a01 596 void OnValueChanged( wxDataViewEvent &event );
718fd180 597
b7e9f8b1 598 void OnActivated( wxDataViewEvent &event );
718fd180
RR
599 void OnExpanding( wxDataViewEvent &event );
600 void OnExpanded( wxDataViewEvent &event );
601 void OnCollapsing( wxDataViewEvent &event );
602 void OnCollapsed( wxDataViewEvent &event );
aed836f3 603 void OnSelectionChanged( wxDataViewEvent &event );
718fd180 604
d14e1c3a
RR
605 void OnEditingStarted( wxDataViewEvent &event );
606 void OnEditingDone( wxDataViewEvent &event );
607
b7e9f8b1
RR
608 void OnHeaderClick( wxDataViewEvent &event );
609 void OnHeaderRightClick( wxDataViewEvent &event );
610 void OnSorted( wxDataViewEvent &event );
611
74dea0de
RR
612 void OnContextMenu( wxDataViewEvent &event );
613
b7e9f8b1
RR
614 void OnRightClick( wxMouseEvent &event );
615 void OnGoto( wxCommandEvent &event);
8b6cf9bf 616 void OnAddMany( wxCommandEvent &event);
d8331a01 617
bd6169a6 618private:
c534e696
RR
619 wxDataViewCtrl* m_musicCtrl;
620 wxObjectDataPtr<MyMusicModel> m_music_model;
621
622 wxDataViewCtrl* m_listCtrl;
623 wxObjectDataPtr<MyListModel> m_list_model;
fbda518c
RR
624
625 wxDataViewColumn * m_col;
c534e696 626
1e08ad10 627 wxTextCtrl * m_log;
b7e9f8b1 628 wxLog *m_logOld;
31fb32e1 629
241cb04b 630private:
abd6692c 631 DECLARE_EVENT_TABLE()
241cb04b
RR
632};
633
362d7fb9
RR
634// -------------------------------------
635// MyApp
636// -------------------------------------
637
87f0efe2 638IMPLEMENT_APP(MyApp)
bd6169a6 639
bd6169a6
RR
640bool MyApp::OnInit(void)
641{
45e6e6f8
VZ
642 if ( !wxApp::OnInit() )
643 return false;
644
87f0efe2
RR
645 // build the first frame
646 MyFrame *frame =
8b6cf9bf 647 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 540);
bd6169a6
RR
648 frame->Show(true);
649
650 SetTopWindow(frame);
bd6169a6
RR
651 return true;
652}
653
87f0efe2
RR
654int MyApp::OnExit()
655{
656 return 0;
657}
658
659
bd6169a6
RR
660// -------------------------------------
661// MyFrame
662// -------------------------------------
663
87f0efe2
RR
664enum
665{
666 // file menu
667 ID_ABOUT = wxID_ABOUT,
87f0efe2 668 ID_EXIT = wxID_EXIT,
1e08ad10 669
d8331a01
RR
670 ID_MUSIC_CTRL = 50,
671
c534e696
RR
672 ID_ADD_MOZART = 100,
673 ID_DELETE_MUSIC = 101,
736fe67c 674 ID_DELETE_YEAR = 102,
c534e696
RR
675
676 ID_PREPEND_LIST = 200,
b7e9f8b1 677 ID_DELETE_LIST = 201,
8b6cf9bf
RR
678 ID_GOTO = 202,
679 ID_ADD_MANY = 203
87f0efe2
RR
680};
681
682BEGIN_EVENT_TABLE(MyFrame, wxFrame)
87f0efe2 683 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
87f0efe2 684 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
c534e696
RR
685 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
686 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
736fe67c 687 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
c534e696
RR
688 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
689 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
b7e9f8b1 690 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
8b6cf9bf 691 EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany)
718fd180 692
0376cc52 693 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
718fd180 694
b7e9f8b1 695 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
718fd180
RR
696 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
697 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
698 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
699 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
aed836f3 700 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
718fd180 701
d14e1c3a
RR
702 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
703 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
704
b7e9f8b1
RR
705 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
706 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
707 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
74dea0de
RR
708
709 EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
718fd180 710
b7e9f8b1 711 EVT_RIGHT_UP(MyFrame::OnRightClick)
87f0efe2
RR
712END_EVENT_TABLE()
713
e94d0c1e 714MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
bd6169a6
RR
715 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
716{
d8331a01 717 m_log = NULL;
fbda518c 718 m_col = NULL;
d8331a01 719
0bcd4039 720 SetIcon(wxICON(sample));
bd6169a6 721
87f0efe2
RR
722 // build the menus:
723
bd6169a6 724 wxMenu *file_menu = new wxMenu;
99c75ebc 725 file_menu->Append(ID_ABOUT, wxT("&About"));
87f0efe2 726 file_menu->AppendSeparator();
99c75ebc 727 file_menu->Append(ID_EXIT, wxT("E&xit"));
bd6169a6 728
bd6169a6 729 wxMenuBar *menu_bar = new wxMenuBar;
99c75ebc 730 menu_bar->Append(file_menu, wxT("&File"));
87f0efe2 731
bd6169a6 732 SetMenuBar(menu_bar);
87f0efe2 733 CreateStatusBar();
bd6169a6 734
1e08ad10
RR
735 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
736
c534e696 737 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
bd6169a6 738
c534e696 739 // MyMusic
b910a8ad 740
d8331a01 741 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
b7e9f8b1 742 wxDefaultSize, wxDV_MULTIPLE );
99c75ebc 743
c534e696
RR
744 m_music_model = new MyMusicModel;
745 m_musicCtrl->AssociateModel( m_music_model.get() );
746
0bd26819
RR
747#if 0
748 // Call this and sorting is enabled
749 // immediatly upon start up.
99c75ebc
RR
750 wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( wxT("Title"), 0, wxDATAVIEW_CELL_INERT, 200,
751 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
0bd26819 752 col->SetSortOrder( true );
99c75ebc
RR
753#else
754 m_musicCtrl->AppendTextColumn(wxT("Title"),0,wxDATAVIEW_CELL_INERT,200,DEFAULT_ALIGN,wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
0bd26819
RR
755#endif
756
99c75ebc
RR
757 m_musicCtrl->AppendTextColumn( wxT("Artist"), 1, wxDATAVIEW_CELL_EDITABLE, 150,
758 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
405a351f 759
ee17111a 760 wxDataViewSpinRenderer *sr = new wxDataViewSpinRenderer( 0, 2010 );
99c75ebc 761 wxDataViewColumn *column = new wxDataViewColumn( wxT("year"), sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE);
405a351f 762 m_musicCtrl->AppendColumn( column );
1e08ad10 763
c534e696
RR
764 data_sizer->Add( m_musicCtrl, 3, wxGROW );
765
766#if 1
767
768 // MyList
769
770 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
99c75ebc 771 wxDefaultSize, wxDV_MULTIPLE | wxDV_ROW_LINES);
c534e696
RR
772
773 m_list_model = new MyListModel;
774 m_listCtrl->AssociateModel( m_list_model.get() );
775
99c75ebc
RR
776 m_listCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE, 120 );
777 m_listCtrl->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT, 60 );
4264606e
RR
778
779 wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr;
99c75ebc 780 column = new wxDataViewColumn(wxT("attributes"), ra, 2 );
4264606e 781 m_listCtrl->AppendColumn( column );
c534e696
RR
782
783 data_sizer->Add( m_listCtrl, 2, wxGROW );
784
785#endif
786
787 main_sizer->Add( data_sizer, 2, wxGROW );
1e08ad10
RR
788
789 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
790
99c75ebc
RR
791 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 );
792 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 );
793 button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 );
c534e696 794 button_sizer->Add( 10, 10, 1 );
8b6cf9bf 795 wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 );
99c75ebc
RR
796 grid_sizer->Add( new wxButton( this, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 2 );
797 grid_sizer->Add( new wxButton( this, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 2 );
798 grid_sizer->Add( new wxButton( this, ID_GOTO, _("Goto 50")), 0, wxALL, 2 );
799 grid_sizer->Add( new wxButton( this, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 2 );
8b6cf9bf 800 button_sizer->Add( grid_sizer, 0, wxALL, 10 );
1e08ad10 801
c534e696 802 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
a75124d0
RR
803
804 wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
1e08ad10 805
99c75ebc 806 m_log = new wxTextCtrl( this, -1,wxString(), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
b7e9f8b1 807 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
99c75ebc 808 wxLogMessage(_("This is the log window"));
b7e9f8b1 809
a75124d0
RR
810 bottom_sizer->Add( m_log, 1, wxGROW );
811
812 // wxDataViewTreeStore
e94d0c1e 813
a75124d0 814 wxDataViewCtrl *treectrl = new wxDataViewCtrl( this, -1,
e94d0c1e 815 wxDefaultPosition, wxSize(300,200), wxDV_NO_HEADER );
e94d0c1e 816
e94d0c1e 817 wxDataViewTreeStore *store = new wxDataViewTreeStore;
99c75ebc
RR
818 wxDataViewItem parent = store->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm) );
819 wxDataViewItem child = store->AppendItem( parent,wxT("Child 1"), wxIcon(small1_xpm) );
820 child = store->AppendItem( parent,wxT("Child 2"), wxIcon(small1_xpm) );
821 child = store->AppendItem( parent,wxT("Child 3"), wxIcon(small1_xpm) );
e94d0c1e 822 treectrl->AssociateModel( store );
e94d0c1e 823 store->DecRef();
b89cac3f 824
99c75ebc 825 treectrl->AppendIconTextColumn(wxT("no label"), 0, wxDATAVIEW_CELL_INERT, 200 );
672e58d9 826
a75124d0 827 bottom_sizer->Add( treectrl );
672e58d9 828
a75124d0
RR
829 // wxDataViewTreeCtrl
830
831 wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( this, -1, wxDefaultPosition, wxSize(300,200) );
83aa1cf4 832
75a2e426 833 wxImageList *ilist = new wxImageList( 16, 16 );
a75124d0 834 ilist->Add( wxIcon(small1_xpm) );
83aa1cf4
RR
835 treectrl2->SetImageList( ilist );
836
99c75ebc
RR
837 parent = treectrl2->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 );
838 child = treectrl2->AppendItem( parent,wxT("Child 1"), 0 );
839 child = treectrl2->AppendItem( parent,wxT("Child 2"), 0 );
840 child = treectrl2->AppendItem( parent,wxT("Child 3"), 0 );
e39d30c0 841
a75124d0
RR
842 bottom_sizer->Add( treectrl2 );
843
844 // main sizer
845
846 main_sizer->Add( bottom_sizer, 0, wxGROW );
e94d0c1e 847
a75124d0
RR
848 SetSizer( main_sizer );
849}
850
e39d30c0
RR
851MyFrame::~MyFrame()
852{
853 delete wxLog::SetActiveTarget(m_logOld);
854}
855
a75124d0
RR
856void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
857{
858 Close(true);
e94d0c1e
RR
859}
860
c534e696
RR
861void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
862{
99c75ebc 863 m_music_model->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 );
c534e696
RR
864}
865
866void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
1e08ad10 867{
b7e9f8b1
RR
868 wxDataViewItemArray items;
869 int len = m_musicCtrl->GetSelections( items );
870 for( int i = 0; i < len; i ++ )
871 if (items[i].IsOk())
872 m_music_model->Delete( items[i] );
e63807a8
RR
873}
874
736fe67c
RR
875void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
876{
877 m_musicCtrl->DeleteColumn( m_musicCtrl->GetColumn( 2 ) );
878 FindWindow( ID_DELETE_YEAR )->Disable();
879}
880
c534e696 881void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
e63807a8 882{
99c75ebc 883 m_list_model->Prepend(wxT("Test"));
c534e696
RR
884}
885
886void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
887{
b7e9f8b1
RR
888 wxDataViewItemArray items;
889 int len = m_listCtrl->GetSelections( items );
8b6cf9bf
RR
890 if (len > 0)
891 m_list_model->DeleteItems( items );
1e08ad10
RR
892}
893
d8331a01
RR
894void MyFrame::OnValueChanged( wxDataViewEvent &event )
895{
896 if (!m_log)
897 return;
898
99c75ebc 899 wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event.GetItem().GetID(), event.GetColumn() );
b7e9f8b1
RR
900}
901
902void MyFrame::OnActivated( wxDataViewEvent &event )
903{
904 if(!m_log)
905 return;
906
d32332aa 907 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 908 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title.GetData());
b7e9f8b1
RR
909}
910
aed836f3 911void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
6848478c
RR
912{
913 if(!m_log)
914 return;
915
d32332aa
RR
916 wxString title = m_music_model->GetTitle( event.GetItem() );
917 if (title.empty())
99c75ebc 918 title = wxT("None");
d32332aa 919
99c75ebc 920 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title.GetData() );
6848478c
RR
921}
922
718fd180
RR
923void MyFrame::OnExpanding( wxDataViewEvent &event )
924{
925 if (!m_log)
926 return;
927
d32332aa 928 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 929 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title.GetData() );
718fd180
RR
930}
931
d14e1c3a
RR
932
933void MyFrame::OnEditingStarted( wxDataViewEvent &event )
934{
935 if (!m_log)
936 return;
937
d32332aa 938 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 939 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title.GetData() );
d14e1c3a
RR
940}
941
942void MyFrame::OnEditingDone( wxDataViewEvent &event )
943{
944 if (!m_log)
945 return;
946
d32332aa 947 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 948 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title.GetData() );
d14e1c3a
RR
949}
950
718fd180
RR
951void MyFrame::OnExpanded( wxDataViewEvent &event )
952{
953 if (!m_log)
954 return;
955
d32332aa 956 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 957 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title.GetData() );
718fd180
RR
958}
959
960void MyFrame::OnCollapsing( wxDataViewEvent &event )
961{
962 if (!m_log)
963 return;
964
d32332aa 965 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 966 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title.GetData() );
718fd180
RR
967}
968
969void MyFrame::OnCollapsed( wxDataViewEvent &event )
970{
971 if (!m_log)
972 return;
d32332aa
RR
973
974 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 975 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title.GetData());
718fd180
RR
976}
977
74dea0de
RR
978void MyFrame::OnContextMenu( wxDataViewEvent &event )
979{
980 if (!m_log)
981 return;
982
983 wxString title = m_music_model->GetTitle( event.GetItem() );
99c75ebc 984 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title.GetData());
74dea0de
RR
985}
986
b7e9f8b1
RR
987void MyFrame::OnHeaderClick( wxDataViewEvent &event )
988{
989 if(!m_log)
990 return;
aed836f3
RR
991
992 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
b7e9f8b1 993
99c75ebc 994 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos );
b7e9f8b1
RR
995}
996
997void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
998{
999 if(!m_log)
1000 return;
1001
dadc879e
RR
1002 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
1003
99c75ebc 1004 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos );
b7e9f8b1
RR
1005}
1006
1007void MyFrame::OnSorted( wxDataViewEvent &event )
1008{
1009 if(!m_log)
1010 return;
1011
d32332aa
RR
1012 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
1013
99c75ebc 1014 wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos );
b7e9f8b1
RR
1015}
1016
1017void MyFrame::OnRightClick( wxMouseEvent &event )
1018{
1019 if(!m_log)
1020 return;
1021
99c75ebc 1022 wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event.GetX(), event.GetY());
b7e9f8b1
RR
1023}
1024
d5ef211f 1025void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))
b7e9f8b1 1026{
718fd180 1027 wxDataViewItem item = m_list_model->GetItem( 50 );
fbda518c 1028 m_listCtrl->EnsureVisible(item,m_col);
d8331a01
RR
1029}
1030
8b6cf9bf
RR
1031void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event))
1032{
1033 m_list_model->AddMany();
1034}
1035
1036
bd6169a6
RR
1037void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
1038{
87f0efe2
RR
1039 wxAboutDialogInfo info;
1040 info.SetName(_("DataView sample"));
1041 info.SetDescription(_("This sample demonstrates the dataview control handling"));
1042 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
bd6169a6 1043
87f0efe2 1044 wxAboutBox(info);
bd6169a6
RR
1045}
1046