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