]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
don't recompute nonbreakable space character every time a text fragment is added...
[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
bd6169a6 42
362d7fb9 43// -------------------------------------
5debbdcf 44// MyMusicModel
362d7fb9 45// -------------------------------------
bd6169a6 46
5debbdcf
RR
47/*
48Implement this data model
49 Title Artist Year
50-------------------------------------------------------------
511: My Music:
52 2: Pop music
53 3: You are not alone Michael Jackson 1995
54 4: Take a bow Madonna 1994
55 5: Classical music
56 6: Ninth Symphony Ludwig v. Beethoven 1824
57 7: German Requiem Johannes Brahms 1868
58*/
4d496ecb 59
1e08ad10
RR
60
61
62class MyMusicModelNode;
63WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
64
65class MyMusicModelNode
66{
67public:
b5fce9e2 68 MyMusicModelNode( MyMusicModelNode* parent,
1e08ad10
RR
69 const wxString &title, const wxString &artist, const wxString &year )
70 {
71 m_parent = parent;
1e08ad10
RR
72 m_title = title;
73 m_artist = artist;
74 m_year = year;
75 m_isContainer = false;
76 }
77
b5fce9e2 78 MyMusicModelNode( MyMusicModelNode* parent,
1e08ad10
RR
79 const wxString &branch )
80 {
81 m_parent = parent;
1e08ad10
RR
82 m_title = branch;
83 m_isContainer = true;
84 }
85
86 ~MyMusicModelNode()
87 {
88 size_t count = m_children.GetCount();
89 size_t i;
90 for (i = 0; i < count; i++)
91 {
92 MyMusicModelNode *child = m_children[i];
93 delete child;
94 }
95 }
96
1e08ad10
RR
97 bool IsContainer() { return m_isContainer; }
98
99 MyMusicModelNode* GetParent() { return m_parent; }
100 MyMusicModelNodes &GetChildren() { return m_children; }
101 MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
102 void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
103 void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
104 unsigned int GetChildCount() { return m_children.GetCount(); }
105
106public:
107 wxString m_title;
108 wxString m_artist;
109 wxString m_year;
110
111private:
112 MyMusicModelNode *m_parent;
113 MyMusicModelNodes m_children;
1e08ad10
RR
114 bool m_isContainer;
115};
116
aba9bfd0 117
5debbdcf 118class MyMusicModel: public wxDataViewModel
bd6169a6
RR
119{
120public:
e63807a8
RR
121
122 // constructor
123
1e08ad10
RR
124 MyMusicModel()
125 {
b5fce9e2
RR
126 m_root = new MyMusicModelNode( NULL, "My Music" );
127 m_pop = new MyMusicModelNode( m_root, "Pop music" );
1e08ad10 128 m_root->Append( m_pop );
b5fce9e2 129 m_pop->Append( new MyMusicModelNode( m_pop,
1e08ad10 130 "You are not alone", "Michael Jackson", "1995" ) );
b5fce9e2 131 m_pop->Append( new MyMusicModelNode( m_pop,
1e08ad10 132 "Take a bow", "Madonna", "1994" ) );
b5fce9e2 133 m_classical = new MyMusicModelNode( m_root, "Classical music" );
1e08ad10 134 m_root->Append( m_classical );
b5fce9e2 135 m_classical->Append( new MyMusicModelNode( m_classical,
1e08ad10 136 "Ninth symphony", "Ludwig van Beethoven", "1824" ) );
b5fce9e2 137 m_classical->Append( new MyMusicModelNode( m_classical,
1e08ad10 138 "German Requiem", "Johannes Brahms", "1868" ) );
befa9b61 139 m_classicalMusicIsKnownToControl = false;
1e08ad10
RR
140 }
141
e63807a8
RR
142 // helper methods to change the model
143
1e08ad10
RR
144 void AddToClassical( const wxString &title, const wxString &artist, const wxString &year )
145 {
146 // add to data
147 MyMusicModelNode *child_node =
b5fce9e2 148 new MyMusicModelNode( m_classical, title, artist, year );
b7e9f8b1 149
1e08ad10
RR
150 m_classical->Append( child_node );
151
befa9b61
RR
152 if (m_classicalMusicIsKnownToControl)
153 {
154 // notify control
b5fce9e2
RR
155 wxDataViewItem child( (void*) child_node );
156 wxDataViewItem parent( (void*) m_classical );
befa9b61
RR
157 ItemAdded( parent, child );
158 }
1e08ad10 159 }
e63807a8
RR
160
161 void Delete( const wxDataViewItem &item )
162 {
b5fce9e2 163 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
9d8fe14a 164 wxDataViewItem parent( node->GetParent() );
e63807a8
RR
165
166 // notify control
9d8fe14a 167 ItemDeleted( parent, item );
b7e9f8b1
RR
168 //We must delete the node after we call ItemDeleted
169 //The reason is that:
170 //When we use wxSortedArray, the array find a node through binary search for speed.
171 //And when the array is searching for some node, it call the model's compare function.
172 //The compare function need the node to be compared. So we should delete the node later, here.
d3f00f59
VZ
173 node->GetParent()->GetChildren().Remove( node );
174 delete node;
e63807a8
RR
175 }
176
cf283a47
RR
177 // override sorting to always sort branches ascendingly
178
7ee7191c
RR
179 int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
180 unsigned int column, bool ascending )
cf283a47 181 {
ed903e42 182 if (IsContainer(item1) && IsContainer(item2))
cf283a47
RR
183 {
184 wxVariant value1,value2;
185 GetValue( value1, item1, 0 );
186 GetValue( value2, item2, 0 );
187
188 wxString str1 = value1.GetString();
189 wxString str2 = value2.GetString();
190 int res = str1.Cmp( str2 );
191 if (res) return res;
192
193 // items must be different
194 unsigned long litem1 = (unsigned long) item1.GetID();
195 unsigned long litem2 = (unsigned long) item2.GetID();
196
197 return litem1-litem2;
198 }
199
7ee7191c 200 return wxDataViewModel::Compare( item1, item2, column, ascending );
cf283a47
RR
201 }
202
e63807a8 203 // implementation of base class virtuals to define model
5debbdcf
RR
204
205 virtual unsigned int GetColumnCount() const
a7f61f76 206 {
fc5eacdb 207 return 3;
a7f61f76 208 }
b910a8ad 209
9861f022 210 virtual wxString GetColumnType( unsigned int col ) const
5debbdcf
RR
211 {
212 return "string";
213 }
b910a8ad 214
5debbdcf
RR
215 virtual void GetValue( wxVariant &variant,
216 const wxDataViewItem &item, unsigned int col ) const
217 {
b5fce9e2 218 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 219 switch (col)
605c2c4a 220 {
1e08ad10
RR
221 case 0: variant = node->m_title; break;
222 case 1: variant = node->m_artist; break;
223 case 2: variant = node->m_year; break;
224 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
a7f61f76 225 }
e152afc3 226 }
9861f022 227
5debbdcf
RR
228 virtual bool SetValue( const wxVariant &variant,
229 const wxDataViewItem &item, unsigned int col )
9861f022 230 {
b5fce9e2 231 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10
RR
232 switch (col)
233 {
234 case 0: node->m_title = variant.GetString(); break;
235 case 1: node->m_artist = variant.GetString(); break;
236 case 2: node->m_year = variant.GetString(); break;
237 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
238 }
9861f022
RR
239 }
240
ed903e42 241 virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
b910a8ad 242 {
ed903e42
RR
243 // the invisble root node has no parent
244 if (!item.IsOk())
245 return wxDataViewItem(0);
246
b5fce9e2 247 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
ed903e42
RR
248
249 // "MyMusic" also has no parent
250 if (node == m_root)
251 return wxDataViewItem(0);
252
253 return wxDataViewItem( (void*) node->GetParent() );
254 }
255
256 virtual bool IsContainer( const wxDataViewItem &item ) const
257 {
258 // the invisble root node can have children (in
259 // our model always "MyMusic")
260 if (!item.IsOk())
1e08ad10
RR
261 return true;
262
ed903e42 263 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 264 return node->IsContainer();
e152afc3 265 }
5debbdcf 266
5debbdcf 267 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
1e510b1e 268 {
b5fce9e2
RR
269 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
270 if (!node)
271 return wxDataViewItem( (void*) m_root );
1e08ad10 272
1e08ad10
RR
273 if (node->GetChildCount() == 0)
274 return wxDataViewItem( 0 );
befa9b61
RR
275
276 if (node == m_classical)
277 {
278 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
279 model->m_classicalMusicIsKnownToControl = true;
280 }
1e510b1e 281
1e08ad10 282 MyMusicModelNode *first_child = node->GetChildren().Item( 0 );
b5fce9e2 283 return wxDataViewItem( (void*) first_child );
9861f022 284 }
1e08ad10 285
5debbdcf 286 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
241cb04b 287 {
b5fce9e2 288 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
ed903e42
RR
289
290 // "MyMusic" has no siblings in our model
291 if (node == m_root)
1e08ad10
RR
292 return wxDataViewItem(0);
293
ed903e42 294 MyMusicModelNode *parent = node->GetParent();
1e08ad10 295 int pos = parent->GetChildren().Index( node );
ed903e42
RR
296
297 // Something went wrong
1e08ad10
RR
298 if (pos == wxNOT_FOUND)
299 return wxDataViewItem(0);
300
ed903e42 301 // No more children
1e08ad10
RR
302 if (pos == parent->GetChildCount()-1)
303 return wxDataViewItem(0);
304
305 node = parent->GetChildren().Item( pos+1 );
b5fce9e2 306 return wxDataViewItem( (void*) node );
1e08ad10
RR
307 }
308
309private:
1e08ad10
RR
310 MyMusicModelNode* m_root;
311 MyMusicModelNode* m_pop;
312 MyMusicModelNode* m_classical;
befa9b61 313 bool m_classicalMusicIsKnownToControl;
241cb04b
RR
314};
315
c534e696
RR
316class MyListModel: public wxDataViewIndexListModel
317{
318public:
319 MyListModel() :
320 wxDataViewIndexListModel( 100 )
321 {
322 unsigned int i;
323 for (i = 0; i < 100; i++)
324 {
325 wxString str;
326 str.Printf( "row number %d", i );
327 m_array.Add( str );
328 }
329 }
330
331 // helper methods to change the model
332
333 void Prepend( const wxString &text )
334 {
335 m_array.Insert( text, 0 );
336 RowPrepended();
337 }
338
339 void DeleteItem( const wxDataViewItem &item )
340 {
341 unsigned int row = GetRow( item );
342 m_array.RemoveAt( row );
343 RowDeleted( row );
344 }
345
346 // implementation of base class virtuals to define model
347
348 virtual unsigned int GetColumnCount() const
349 {
350 return 2;
351 }
352
353 virtual wxString GetColumnType( unsigned int col ) const
354 {
355 return "string";
356 }
357
358 virtual unsigned int GetRowCount()
359 {
360 return m_array.GetCount();
361 }
362
363 virtual void GetValue( wxVariant &variant,
364 unsigned int row, unsigned int col ) const
365 {
366 if (col==0)
367 {
368 variant = m_array[ row ];
369 }
370 else
371 {
372 wxString str;
373 str.Printf( "row %d col %d", row, col );
374 variant = str;
375 }
376 }
377
378 virtual bool SetValue( const wxVariant &variant,
379 unsigned int row, unsigned int col )
380 {
381 if (col == 0)
382 {
383 m_array[row] = variant.GetString();
384 return true;
385 }
386
387 return false;
388 }
389
390 wxArrayString m_array;
391};
392
362d7fb9
RR
393// -------------------------------------
394// MyApp
395// -------------------------------------
bd6169a6
RR
396
397class MyApp: public wxApp
398{
399public:
400 bool OnInit(void);
87f0efe2 401 int OnExit();
bd6169a6
RR
402};
403
362d7fb9
RR
404// -------------------------------------
405// MyFrame
406// -------------------------------------
407
87f0efe2 408class MyFrame : public wxFrame
bd6169a6
RR
409{
410public:
411 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
412
413public:
414 void OnQuit(wxCommandEvent& event);
415 void OnAbout(wxCommandEvent& event);
c534e696
RR
416
417 void OnAddMozart(wxCommandEvent& event);
418 void OnDeleteMusic(wxCommandEvent& event);
419
420 void OnPrependList(wxCommandEvent& event);
421 void OnDeleteList(wxCommandEvent& event);
b910a8ad 422
d8331a01
RR
423 void OnValueChanged( wxDataViewEvent &event );
424 void OnItemAdded( wxDataViewEvent &event );
425 void OnItemDeleted( wxDataViewEvent &event );
718fd180 426
b7e9f8b1 427 void OnActivated( wxDataViewEvent &event );
718fd180
RR
428 void OnExpanding( wxDataViewEvent &event );
429 void OnExpanded( wxDataViewEvent &event );
430 void OnCollapsing( wxDataViewEvent &event );
431 void OnCollapsed( wxDataViewEvent &event );
6848478c 432 void OnSelected( wxDataViewEvent &event );
718fd180 433
b7e9f8b1
RR
434 void OnHeaderClick( wxDataViewEvent &event );
435 void OnHeaderRightClick( wxDataViewEvent &event );
436 void OnSorted( wxDataViewEvent &event );
437
438 void OnRightClick( wxMouseEvent &event );
439 void OnGoto( wxCommandEvent &event);
d8331a01 440
bd6169a6 441private:
c534e696
RR
442 wxDataViewCtrl* m_musicCtrl;
443 wxObjectDataPtr<MyMusicModel> m_music_model;
444
445 wxDataViewCtrl* m_listCtrl;
446 wxObjectDataPtr<MyListModel> m_list_model;
447
1e08ad10 448 wxTextCtrl * m_log;
b7e9f8b1 449 wxLog *m_logOld;
31fb32e1 450
241cb04b 451private:
abd6692c 452 DECLARE_EVENT_TABLE()
241cb04b
RR
453};
454
362d7fb9
RR
455// -------------------------------------
456// MyApp
457// -------------------------------------
458
87f0efe2 459IMPLEMENT_APP(MyApp)
bd6169a6 460
bd6169a6
RR
461bool MyApp::OnInit(void)
462{
45e6e6f8
VZ
463 if ( !wxApp::OnInit() )
464 return false;
465
87f0efe2
RR
466 // build the first frame
467 MyFrame *frame =
6848478c 468 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 700, 440);
bd6169a6
RR
469 frame->Show(true);
470
471 SetTopWindow(frame);
bd6169a6
RR
472 return true;
473}
474
87f0efe2
RR
475int MyApp::OnExit()
476{
477 return 0;
478}
479
480
bd6169a6
RR
481// -------------------------------------
482// MyFrame
483// -------------------------------------
484
87f0efe2
RR
485enum
486{
487 // file menu
488 ID_ABOUT = wxID_ABOUT,
87f0efe2 489 ID_EXIT = wxID_EXIT,
1e08ad10 490
d8331a01
RR
491 ID_MUSIC_CTRL = 50,
492
c534e696
RR
493 ID_ADD_MOZART = 100,
494 ID_DELETE_MUSIC = 101,
495
496 ID_PREPEND_LIST = 200,
b7e9f8b1
RR
497 ID_DELETE_LIST = 201,
498 ID_GOTO = 202
87f0efe2
RR
499};
500
501BEGIN_EVENT_TABLE(MyFrame, wxFrame)
87f0efe2 502 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
87f0efe2 503 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
c534e696
RR
504 EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart )
505 EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic )
506 EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList )
507 EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList )
b7e9f8b1 508 EVT_BUTTON( ID_GOTO, MyFrame::OnGoto)
718fd180 509
d8331a01
RR
510 EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL, MyFrame::OnItemAdded )
511 EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL, MyFrame::OnItemDeleted )
512 EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
b7e9f8b1 513 EVT_DATAVIEW_MODEL_ITEM_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged )
718fd180 514
b7e9f8b1 515 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated )
718fd180
RR
516 EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding)
517 EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded)
518 EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing)
519 EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed)
6848478c 520 EVT_DATAVIEW_ITEM_SELECTED(ID_MUSIC_CTRL, MyFrame::OnSelected)
718fd180 521
b7e9f8b1
RR
522 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick)
523 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick)
524 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted)
718fd180 525
b7e9f8b1 526 EVT_RIGHT_UP(MyFrame::OnRightClick)
87f0efe2
RR
527END_EVENT_TABLE()
528
bd6169a6
RR
529MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
530 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
531{
d8331a01
RR
532 m_log = NULL;
533
0bcd4039 534 SetIcon(wxICON(sample));
bd6169a6 535
87f0efe2
RR
536 // build the menus:
537
bd6169a6 538 wxMenu *file_menu = new wxMenu;
5debbdcf 539 file_menu->Append(ID_ABOUT, "&About");
87f0efe2 540 file_menu->AppendSeparator();
5debbdcf 541 file_menu->Append(ID_EXIT, "E&xit");
bd6169a6 542
bd6169a6 543 wxMenuBar *menu_bar = new wxMenuBar;
5debbdcf 544 menu_bar->Append(file_menu, "&File");
87f0efe2 545
bd6169a6 546 SetMenuBar(menu_bar);
87f0efe2 547 CreateStatusBar();
bd6169a6 548
1e08ad10
RR
549 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
550
c534e696 551 wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL );
bd6169a6 552
c534e696 553 // MyMusic
b910a8ad 554
d8331a01 555 m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition,
b7e9f8b1 556 wxDefaultSize, wxDV_MULTIPLE );
c534e696
RR
557
558 m_music_model = new MyMusicModel;
559 m_musicCtrl->AssociateModel( m_music_model.get() );
560
561 m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
94b1f7bc 562 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
c534e696 563 m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 200,
94b1f7bc 564 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
c534e696 565 m_musicCtrl->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
9861f022 566 DEFAULT_ALIGN );
1e08ad10 567
c534e696
RR
568 data_sizer->Add( m_musicCtrl, 3, wxGROW );
569
570#if 1
571
572 // MyList
573
574 m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
b7e9f8b1 575 wxDefaultSize, wxDV_MULTIPLE );
c534e696
RR
576
577 m_list_model = new MyListModel;
578 m_listCtrl->AssociateModel( m_list_model.get() );
579
580 m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
581 m_listCtrl->AppendTextColumn( "index", 1, wxDATAVIEW_CELL_INERT, 120 );
582
583 data_sizer->Add( m_listCtrl, 2, wxGROW );
584
585#endif
586
587 main_sizer->Add( data_sizer, 2, wxGROW );
1e08ad10
RR
588
589 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
590
c534e696
RR
591 button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 );
592 button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 );
593 button_sizer->Add( 10, 10, 1 );
594 button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 );
595 button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 );
b7e9f8b1 596 button_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 10 );
1e08ad10 597
c534e696 598 main_sizer->Add( button_sizer, 0, wxGROW, 0 );
1e08ad10
RR
599
600 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
b7e9f8b1
RR
601 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log));
602 wxLogMessage("This is the log window");
603
1e08ad10
RR
604 main_sizer->Add( m_log, 1, wxGROW );
605
606 SetSizer( main_sizer );
bd6169a6
RR
607}
608
609void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
610{
611 Close(true);
612}
613
c534e696
RR
614void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) )
615{
616 m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
617}
618
619void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) )
1e08ad10 620{
b7e9f8b1
RR
621 wxDataViewItemArray items;
622 int len = m_musicCtrl->GetSelections( items );
623 for( int i = 0; i < len; i ++ )
624 if (items[i].IsOk())
625 m_music_model->Delete( items[i] );
e63807a8
RR
626}
627
c534e696 628void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) )
e63807a8 629{
c534e696
RR
630 m_list_model->Prepend( "Test" );
631}
632
633void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) )
634{
b7e9f8b1
RR
635 wxDataViewItemArray items;
636 int len = m_listCtrl->GetSelections( items );
637 for( int i = 0; i < len; i ++ )
638 if (items[i].IsOk())
639 m_list_model->DeleteItem( items[i] );
1e08ad10
RR
640}
641
d8331a01
RR
642void MyFrame::OnItemAdded( wxDataViewEvent &event )
643{
644 if (!m_log)
645 return;
646
b7e9f8b1 647 wxLogMessage("wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, Item Id: %d",event.GetItem().GetID());
d8331a01
RR
648}
649
650void MyFrame::OnItemDeleted( wxDataViewEvent &event )
651{
652 if (!m_log)
653 return;
654
b7e9f8b1 655 wxLogMessage( "EVT_DATAVIEW_MODEL_ITEM_DELETED, Item Id: %d", event.GetItem().GetID() );
d8331a01
RR
656}
657
658void MyFrame::OnValueChanged( wxDataViewEvent &event )
659{
660 if (!m_log)
661 return;
662
b7e9f8b1
RR
663 wxLogMessage( "EVT_DATAVIEW_MODEL_VALUE_CHANGED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn() );
664}
665
666void MyFrame::OnActivated( wxDataViewEvent &event )
667{
668 if(!m_log)
669 return;
670
671 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn());
672}
673
6848478c
RR
674void MyFrame::OnSelected( wxDataViewEvent &event )
675{
676 if(!m_log)
677 return;
678
679 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED, Item Id: %d", event.GetItem().GetID() );
680}
681
718fd180
RR
682void MyFrame::OnExpanding( wxDataViewEvent &event )
683{
684 if (!m_log)
685 return;
686
687 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item Id: %d", event.GetItem().GetID() );
688}
689
690void MyFrame::OnExpanded( wxDataViewEvent &event )
691{
692 if (!m_log)
693 return;
694
695 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item Id: %d", event.GetItem().GetID() );
696}
697
698void MyFrame::OnCollapsing( wxDataViewEvent &event )
699{
700 if (!m_log)
701 return;
702
703 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item Id: %d", event.GetItem().GetID() );
704}
705
706void MyFrame::OnCollapsed( wxDataViewEvent &event )
707{
708 if (!m_log)
709 return;
710
711 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item Id: %d", event.GetItem().GetID() );
712}
713
b7e9f8b1
RR
714void MyFrame::OnHeaderClick( wxDataViewEvent &event )
715{
716 if(!m_log)
717 return;
718
719 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column: %d", event.GetColumn());
720}
721
722void MyFrame::OnHeaderRightClick( wxDataViewEvent &event )
723{
724 if(!m_log)
725 return;
726
727 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column: %d", event.GetColumn());
728}
729
730void MyFrame::OnSorted( wxDataViewEvent &event )
731{
732 if(!m_log)
733 return;
734
735 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column: %d", event.GetColumn());
736}
737
738void MyFrame::OnRightClick( wxMouseEvent &event )
739{
740 if(!m_log)
741 return;
742
743 wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event.GetX(), event.GetY());
744}
745
746void MyFrame::OnGoto( wxCommandEvent &event)
747{
718fd180
RR
748 wxDataViewItem item = m_list_model->GetItem( 50 );
749 m_listCtrl->EnsureVisible(item);
d8331a01
RR
750}
751
bd6169a6
RR
752void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
753{
87f0efe2
RR
754 wxAboutDialogInfo info;
755 info.SetName(_("DataView sample"));
756 info.SetDescription(_("This sample demonstrates the dataview control handling"));
757 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
bd6169a6 758
87f0efe2 759 wxAboutBox(info);
bd6169a6
RR
760}
761