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