]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
Redundant call to GetItemByRow
[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:
e94d0c1e 490 MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h);
bd6169a6
RR
491
492public:
493 void OnQuit(wxCommandEvent& event);
494 void OnAbout(wxCommandEvent& event);
c534e696 495
e94d0c1e
RR
496 void OnTestTreeCtrl(wxCommandEvent& event);
497
c534e696
RR
498 void OnAddMozart(wxCommandEvent& event);
499 void OnDeleteMusic(wxCommandEvent& event);
736fe67c 500 void OnDeleteYear(wxCommandEvent& event);
c534e696
RR
501
502 void OnPrependList(wxCommandEvent& event);
503 void OnDeleteList(wxCommandEvent& event);
b910a8ad 504
d8331a01 505 void OnValueChanged( wxDataViewEvent &event );
718fd180 506
b7e9f8b1 507 void OnActivated( wxDataViewEvent &event );
718fd180
RR
508 void OnExpanding( wxDataViewEvent &event );
509 void OnExpanded( wxDataViewEvent &event );
510 void OnCollapsing( wxDataViewEvent &event );
511 void OnCollapsed( wxDataViewEvent &event );
aed836f3 512 void OnSelectionChanged( wxDataViewEvent &event );
718fd180 513
d14e1c3a
RR
514 void OnEditingStarted( wxDataViewEvent &event );
515 void OnEditingDone( wxDataViewEvent &event );
516
b7e9f8b1
RR
517 void OnHeaderClick( wxDataViewEvent &event );
518 void OnHeaderRightClick( wxDataViewEvent &event );
519 void OnSorted( wxDataViewEvent &event );
520
521 void OnRightClick( wxMouseEvent &event );
522 void OnGoto( wxCommandEvent &event);
d8331a01 523
bd6169a6 524private:
c534e696
RR
525 wxDataViewCtrl* m_musicCtrl;
526 wxObjectDataPtr<MyMusicModel> m_music_model;
527
528 wxDataViewCtrl* m_listCtrl;
529 wxObjectDataPtr<MyListModel> m_list_model;
fbda518c
RR
530
531 wxDataViewColumn * m_col;
c534e696 532
1e08ad10 533 wxTextCtrl * m_log;
b7e9f8b1 534 wxLog *m_logOld;
31fb32e1 535
241cb04b 536private:
abd6692c 537 DECLARE_EVENT_TABLE()
241cb04b
RR
538};
539
362d7fb9
RR
540// -------------------------------------
541// MyApp
542// -------------------------------------
543
87f0efe2 544IMPLEMENT_APP(MyApp)
bd6169a6 545
bd6169a6
RR
546bool MyApp::OnInit(void)
547{
45e6e6f8
VZ
548 if ( !wxApp::OnInit() )
549 return false;
550
87f0efe2
RR
551 // build the first frame
552 MyFrame *frame =
405a351f 553 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 440);
bd6169a6
RR
554 frame->Show(true);
555
556 SetTopWindow(frame);
bd6169a6
RR
557 return true;
558}
559
87f0efe2
RR
560int MyApp::OnExit()
561{
562 return 0;
563}
564
565
bd6169a6
RR
566// -------------------------------------
567// MyFrame
568// -------------------------------------
569
87f0efe2
RR
570enum
571{
572 // file menu
573 ID_ABOUT = wxID_ABOUT,
87f0efe2 574 ID_EXIT = wxID_EXIT,
1e08ad10 575
e94d0c1e
RR
576 ID_TEST_TREECTRL = 40,
577
d8331a01
RR
578 ID_MUSIC_CTRL = 50,
579
c534e696
RR
580 ID_ADD_MOZART = 100,
581 ID_DELETE_MUSIC = 101,
736fe67c 582 ID_DELETE_YEAR = 102,
c534e696
RR
583
584 ID_PREPEND_LIST = 200,
b7e9f8b1 585 ID_DELETE_LIST = 201,
405a351f 586 ID_GOTO = 202
87f0efe2
RR
587};
588
589BEGIN_EVENT_TABLE(MyFrame, wxFrame)
87f0efe2 590 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
e94d0c1e 591 EVT_MENU( ID_TEST_TREECTRL, MyFrame::OnTestTreeCtrl )
87f0efe2 592 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
c534e696
RR
593 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
594 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
736fe67c 595 EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear )
c534e696
RR
596 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
597 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
b7e9f8b1 598 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
718fd180 599
0376cc52 600 EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
718fd180 601
b7e9f8b1 602 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
718fd180
RR
603 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
604 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
605 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
606 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
aed836f3 607 EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged)
718fd180 608
d14e1c3a
RR
609 EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted)
610 EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone)
611
b7e9f8b1
RR
612 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
613 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
614 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
718fd180 615
b7e9f8b1 616 EVT_RIGHT_UP(MyFrame::OnRightClick)
87f0efe2
RR
617END_EVENT_TABLE()
618
e94d0c1e 619MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h):
bd6169a6
RR
620 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
621{
d8331a01 622 m_log = NULL;
fbda518c 623 m_col = NULL;
d8331a01 624
0bcd4039 625 SetIcon(wxICON(sample));
bd6169a6 626
87f0efe2
RR
627 // build the menus:
628
bd6169a6 629 wxMenu *file_menu = new wxMenu;
5debbdcf 630 file_menu->Append(ID_ABOUT, "&About");
87f0efe2 631 file_menu->AppendSeparator();
e94d0c1e
RR
632 file_menu->Append(ID_TEST_TREECTRL, "Test &Treectrl");
633 file_menu->AppendSeparator();
5debbdcf 634 file_menu->Append(ID_EXIT, "E&xit");
bd6169a6 635
bd6169a6 636 wxMenuBar *menu_bar = new wxMenuBar;
5debbdcf 637 menu_bar->Append(file_menu, "&File");
87f0efe2 638
bd6169a6 639 SetMenuBar(menu_bar);
87f0efe2 640 CreateStatusBar();
bd6169a6 641
1e08ad10
RR
642 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
643
c534e696 644 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
bd6169a6 645
c534e696 646 // MyMusic
b910a8ad 647
d8331a01 648 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
b7e9f8b1 649 wxDefaultSize, wxDV_MULTIPLE );
c534e696
RR
650
651 m_music_model = new MyMusicModel;
652 m_musicCtrl->AssociateModel( m_music_model.get() );
653
0bd26819 654 wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
94b1f7bc 655 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
0bd26819
RR
656#if 0
657 // Call this and sorting is enabled
658 // immediatly upon start up.
659 col->SetSortOrder( true );
660#endif
661
405a351f 662 m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 150,
94b1f7bc 663 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
405a351f
RR
664
665 MySpinCtrlInPlaceRenderer *sr = new MySpinCtrlInPlaceRenderer;
666 wxDataViewColumn *column = new wxDataViewColumn( "year", sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE );
667 m_musicCtrl->AppendColumn( column );
1e08ad10 668
c534e696
RR
669 data_sizer->Add( m_musicCtrl, 3, wxGROW );
670
671#if 1
672
673 // MyList
674
675 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
b7e9f8b1 676 wxDefaultSize, wxDV_MULTIPLE );
c534e696
RR
677
678 m_list_model = new MyListModel;
679 m_listCtrl->AssociateModel( m_list_model.get() );
680
681 m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
b04fcede
RR
682 m_listCtrl->AppendIconTextColumn( "icon", 1, wxDATAVIEW_CELL_INERT, 60 );
683 m_listCtrl->AppendTextColumn( "index", 2, wxDATAVIEW_CELL_INERT, 120 );
c534e696
RR
684
685 data_sizer->Add( m_listCtrl, 2, wxGROW );
686
687#endif
688
689 main_sizer->Add( data_sizer, 2, wxGROW );
1e08ad10
RR
690
691 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
692
c534e696
RR
693 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 );
694 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 );
736fe67c 695 button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, "Delete \"Year\" column"), 0, wxALL, 10 );
c534e696
RR
696 button_sizer->Add( 10, 10, 1 );
697 button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 );
698 button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
b7e9f8b1 699 button_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
1e08ad10 700
c534e696 701 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
1e08ad10
RR
702
703 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
b7e9f8b1
RR
704 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
705 wxLogMessage("This is the log window");
706
1e08ad10
RR
707 main_sizer->Add( m_log, 1, wxGROW );
708
709 SetSizer( main_sizer );
bd6169a6
RR
710}
711
712void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
713{
714 Close(true);
715}
716
e94d0c1e
RR
717
718/* XPM */
719static char *small1_xpm[] = {
720/* columns rows colors chars-per-pixel */
721"16 16 6 1",
722". c Black",
723"o c #FFFFFF",
724"X c #000080",
725"O c #FFFF00",
726" c None",
727"+ c #FF0000",
728/* pixels */
729" ",
730" ",
731" ",
732" ....... ",
733" .XXXXX. ",
734" .oXXXX. ",
735" .oXXX.......",
736".....oXXX.OOOOO.",
737".+++.XXXX.oOOOO.",
738".o++......oOOOO.",
739".o++++. .oOOOO.",
740".o++++. .OOOOO.",
741".+++++. .......",
742"....... ",
743" ",
744" "
745};
746
747void MyFrame::OnTestTreeCtrl(wxCommandEvent& WXUNUSED(event) )
748{
b89cac3f 749 wxDialog dialog( this, -1, "Test wxDataViewTreeStore" );
e94d0c1e
RR
750
751 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
752
753 wxDataViewCtrl *treectrl = new wxDataViewCtrl( &dialog, -1,
754 wxDefaultPosition, wxSize(300,200), wxDV_NO_HEADER );
0a13ee1b 755 treectrl->AppendIconTextColumn( "no label", 0, wxDATAVIEW_CELL_INERT, 200 );
e94d0c1e 756
0a13ee1b 757 main_sizer->Add( treectrl, 1, wxGROW );
e94d0c1e
RR
758
759 wxDataViewTreeStore *store = new wxDataViewTreeStore;
760 wxDataViewItem parent = store->AppendContainer( wxDataViewItem(0), "Root 1", wxIcon(small1_xpm) );
0a13ee1b
RR
761 wxDataViewItem child = store->AppendItem( parent, "Child 1", wxIcon(small1_xpm) );
762 child = store->AppendItem( parent, "Child 2", wxIcon(small1_xpm) );
763 child = store->AppendItem( parent, "Child 3", wxIcon(small1_xpm) );
e94d0c1e
RR
764
765 treectrl->AssociateModel( store );
766
767 store->DecRef();
b89cac3f 768
e94d0c1e
RR
769 wxSizer *button_sizer = dialog.CreateButtonSizer( wxOK );
770 if (button_sizer)
b89cac3f 771 main_sizer->Add( button_sizer, 0, wxGROW|wxALL, 10 );
e94d0c1e
RR
772
773 dialog.SetSizer( main_sizer );
774 main_sizer->Fit( &dialog );
775
776 dialog.ShowModal();
777}
778
c534e696
RR
779void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
780{
405a351f 781 m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
c534e696
RR
782}
783
784void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
1e08ad10 785{
b7e9f8b1
RR
786 wxDataViewItemArray items;
787 int len = m_musicCtrl->GetSelections( items );
788 for( int i = 0; i < len; i ++ )
789 if (items[i].IsOk())
790 m_music_model->Delete( items[i] );
e63807a8
RR
791}
792
736fe67c
RR
793void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
794{
795 m_musicCtrl->DeleteColumn( m_musicCtrl->GetColumn( 2 ) );
796 FindWindow( ID_DELETE_YEAR )->Disable();
797}
798
c534e696 799void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
e63807a8 800{
c534e696
RR
801 m_list_model->Prepend( "Test" );
802}
803
804void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
805{
b7e9f8b1
RR
806 wxDataViewItemArray items;
807 int len = m_listCtrl->GetSelections( items );
808 for( int i = 0; i < len; i ++ )
809 if (items[i].IsOk())
810 m_list_model->DeleteItem( items[i] );
1e08ad10
RR
811}
812
d8331a01
RR
813void MyFrame::OnValueChanged( wxDataViewEvent &event )
814{
815 if (!m_log)
816 return;
817
0376cc52 818 wxLogMessage( "EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn() );
b7e9f8b1
RR
819}
820
821void MyFrame::OnActivated( 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_ACTIVATED, Item: %s", title );
b7e9f8b1
RR
828}
829
aed836f3 830void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
6848478c
RR
831{
832 if(!m_log)
833 return;
834
d32332aa
RR
835 wxString title = m_music_model->GetTitle( event.GetItem() );
836 if (title.empty())
837 title = "None";
838
839 wxLogMessage("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s", title );
6848478c
RR
840}
841
718fd180
RR
842void MyFrame::OnExpanding( wxDataViewEvent &event )
843{
844 if (!m_log)
845 return;
846
d32332aa
RR
847 wxString title = m_music_model->GetTitle( event.GetItem() );
848 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s", title );
718fd180
RR
849}
850
d14e1c3a
RR
851
852void MyFrame::OnEditingStarted( wxDataViewEvent &event )
853{
854 if (!m_log)
855 return;
856
d32332aa
RR
857 wxString title = m_music_model->GetTitle( event.GetItem() );
858 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s", title );
d14e1c3a
RR
859}
860
861void MyFrame::OnEditingDone( wxDataViewEvent &event )
862{
863 if (!m_log)
864 return;
865
d32332aa
RR
866 wxString title = m_music_model->GetTitle( event.GetItem() );
867 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s", title );
d14e1c3a
RR
868}
869
718fd180
RR
870void MyFrame::OnExpanded( wxDataViewEvent &event )
871{
872 if (!m_log)
873 return;
874
d32332aa
RR
875 wxString title = m_music_model->GetTitle( event.GetItem() );
876 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s", title );
718fd180
RR
877}
878
879void MyFrame::OnCollapsing( wxDataViewEvent &event )
880{
881 if (!m_log)
882 return;
883
d32332aa
RR
884 wxString title = m_music_model->GetTitle( event.GetItem() );
885 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s", title );
718fd180
RR
886}
887
888void MyFrame::OnCollapsed( wxDataViewEvent &event )
889{
890 if (!m_log)
891 return;
d32332aa
RR
892
893 wxString title = m_music_model->GetTitle( event.GetItem() );
894 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s", title );
718fd180
RR
895}
896
b7e9f8b1
RR
897void MyFrame::OnHeaderClick( wxDataViewEvent &event )
898{
899 if(!m_log)
900 return;
aed836f3
RR
901
902 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
b7e9f8b1 903
aed836f3 904 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d", pos );
b7e9f8b1
RR
905}
906
907void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
908{
909 if(!m_log)
910 return;
911
dadc879e
RR
912 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
913
914 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d", pos );
b7e9f8b1
RR
915}
916
917void MyFrame::OnSorted( wxDataViewEvent &event )
918{
919 if(!m_log)
920 return;
921
d32332aa
RR
922 int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() );
923
924 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d", pos );
b7e9f8b1
RR
925}
926
927void MyFrame::OnRightClick( wxMouseEvent &event )
928{
929 if(!m_log)
930 return;
931
932 wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event.GetX(), event.GetY());
933}
934
935void MyFrame::OnGoto( wxCommandEvent &event)
936{
718fd180 937 wxDataViewItem item = m_list_model->GetItem( 50 );
fbda518c 938 m_listCtrl->EnsureVisible(item,m_col);
d8331a01
RR
939}
940
bd6169a6
RR
941void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
942{
87f0efe2
RR
943 wxAboutDialogInfo info;
944 info.SetName(_("DataView sample"));
945 info.SetDescription(_("This sample demonstrates the dataview control handling"));
946 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
bd6169a6 947
87f0efe2 948 wxAboutBox(info);
bd6169a6
RR
949}
950