1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Bo Yang
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/datetime.h"
24 #include "wx/splitter.h"
25 #include "wx/aboutdlg.h"
26 #include "wx/choicdlg.h"
27 #include "wx/numdlg.h"
28 #include "wx/dataview.h"
29 #include "wx/spinctrl.h"
32 #include "../sample.xpm"
38 #define DEFAULT_ALIGN wxALIGN_LEFT
39 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
43 // -------------------------------------
45 // -------------------------------------
48 Implement this data model
50 -------------------------------------------------------------
53 3: You are not alone Michael Jackson 1995
54 4: Take a bow Madonna 1994
56 6: Ninth Symphony Ludwig v. Beethoven 1824
57 7: German Requiem Johannes Brahms 1868
62 class MyMusicModelNode
;
63 WX_DEFINE_ARRAY_PTR( MyMusicModelNode
*, MyMusicModelNodes
);
65 class MyMusicModelNode
68 MyMusicModelNode( MyMusicModelNode
* parent
,
69 const wxString
&title
, const wxString
&artist
, const wxString
&year
)
75 m_isContainer
= false;
78 MyMusicModelNode( MyMusicModelNode
* parent
,
79 const wxString
&branch
)
88 size_t count
= m_children
.GetCount();
90 for (i
= 0; i
< count
; i
++)
92 MyMusicModelNode
*child
= m_children
[i
];
97 bool IsContainer() { return m_isContainer
; }
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(); }
112 MyMusicModelNode
*m_parent
;
113 MyMusicModelNodes m_children
;
118 class MyMusicModel
: public wxDataViewModel
126 m_root
= new MyMusicModelNode( NULL
, "My Music" );
127 m_pop
= new MyMusicModelNode( m_root
, "Pop music" );
128 m_root
->Append( m_pop
);
129 m_pop
->Append( new MyMusicModelNode( m_pop
,
130 "You are not alone", "Michael Jackson", "1995" ) );
131 m_pop
->Append( new MyMusicModelNode( m_pop
,
132 "Take a bow", "Madonna", "1994" ) );
133 m_classical
= new MyMusicModelNode( m_root
, "Classical music" );
134 m_root
->Append( m_classical
);
135 m_classical
->Append( new MyMusicModelNode( m_classical
,
136 "Ninth symphony", "Ludwig van Beethoven", "1824" ) );
137 m_classical
->Append( new MyMusicModelNode( m_classical
,
138 "German Requiem", "Johannes Brahms", "1868" ) );
139 m_classicalMusicIsKnownToControl
= false;
142 // helper methods to change the model
144 void AddToClassical( const wxString
&title
, const wxString
&artist
, const wxString
&year
)
147 MyMusicModelNode
*child_node
=
148 new MyMusicModelNode( m_classical
, title
, artist
, year
);
150 m_classical
->Append( child_node
);
152 if (m_classicalMusicIsKnownToControl
)
155 wxDataViewItem
child( (void*) child_node
);
156 wxDataViewItem
parent( (void*) m_classical
);
157 ItemAdded( parent
, child
);
161 void Delete( const wxDataViewItem
&item
)
163 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
164 wxDataViewItem
parent( node
->GetParent() );
167 ItemDeleted( parent
, item
);
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.
173 node
->GetParent()->GetChildren().Remove( node
);
177 // override sorting to always sort branches ascendingly
179 int Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
180 unsigned int column
, bool ascending
)
182 if (IsContainer(item1
) && IsContainer(item2
))
184 wxVariant value1
,value2
;
185 GetValue( value1
, item1
, 0 );
186 GetValue( value2
, item2
, 0 );
188 wxString str1
= value1
.GetString();
189 wxString str2
= value2
.GetString();
190 int res
= str1
.Cmp( str2
);
193 // items must be different
194 unsigned long litem1
= (unsigned long) item1
.GetID();
195 unsigned long litem2
= (unsigned long) item2
.GetID();
197 return litem1
-litem2
;
200 return wxDataViewModel::Compare( item1
, item2
, column
, ascending
);
203 // implementation of base class virtuals to define model
205 virtual unsigned int GetColumnCount() const
210 virtual wxString
GetColumnType( unsigned int col
) const
215 virtual void GetValue( wxVariant
&variant
,
216 const wxDataViewItem
&item
, unsigned int col
) const
218 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
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" );
228 virtual bool SetValue( const wxVariant
&variant
,
229 const wxDataViewItem
&item
, unsigned int col
)
231 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
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" );
241 virtual wxDataViewItem
GetParent( const wxDataViewItem
&item
) const
243 // the invisble root node has no parent
245 return wxDataViewItem(0);
247 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
249 // "MyMusic" also has no parent
251 return wxDataViewItem(0);
253 return wxDataViewItem( (void*) node
->GetParent() );
256 virtual bool IsContainer( const wxDataViewItem
&item
) const
258 // the invisble root node can have children (in
259 // our model always "MyMusic")
263 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
264 return node
->IsContainer();
267 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
269 MyMusicModelNode
*node
= (MyMusicModelNode
*) parent
.GetID();
271 return wxDataViewItem( (void*) m_root
);
273 if (node
->GetChildCount() == 0)
274 return wxDataViewItem( 0 );
276 if (node
== m_classical
)
278 MyMusicModel
*model
= (MyMusicModel
*)(const MyMusicModel
*) this;
279 model
->m_classicalMusicIsKnownToControl
= true;
282 MyMusicModelNode
*first_child
= node
->GetChildren().Item( 0 );
283 return wxDataViewItem( (void*) first_child
);
286 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
288 MyMusicModelNode
*node
= (MyMusicModelNode
*) item
.GetID();
290 // "MyMusic" has no siblings in our model
292 return wxDataViewItem(0);
294 MyMusicModelNode
*parent
= node
->GetParent();
295 int pos
= parent
->GetChildren().Index( node
);
297 // Something went wrong
298 if (pos
== wxNOT_FOUND
)
299 return wxDataViewItem(0);
302 if (pos
== parent
->GetChildCount()-1)
303 return wxDataViewItem(0);
305 node
= parent
->GetChildren().Item( pos
+1 );
306 return wxDataViewItem( (void*) node
);
310 MyMusicModelNode
* m_root
;
311 MyMusicModelNode
* m_pop
;
312 MyMusicModelNode
* m_classical
;
313 bool m_classicalMusicIsKnownToControl
;
316 class MyListModel
: public wxDataViewIndexListModel
320 wxDataViewIndexListModel( 100 )
323 for (i
= 0; i
< 100; i
++)
326 str
.Printf( "row number %d", i
);
331 // helper methods to change the model
333 void Prepend( const wxString
&text
)
335 m_array
.Insert( text
, 0 );
339 void DeleteItem( const wxDataViewItem
&item
)
341 unsigned int row
= GetRow( item
);
342 m_array
.RemoveAt( row
);
346 // implementation of base class virtuals to define model
348 virtual unsigned int GetColumnCount() const
353 virtual wxString
GetColumnType( unsigned int col
) const
358 virtual unsigned int GetRowCount()
360 return m_array
.GetCount();
363 virtual void GetValue( wxVariant
&variant
,
364 unsigned int row
, unsigned int col
) const
368 variant
= m_array
[ row
];
373 str
.Printf( "row %d col %d", row
, col
);
378 virtual bool SetValue( const wxVariant
&variant
,
379 unsigned int row
, unsigned int col
)
383 m_array
[row
] = variant
.GetString();
390 wxArrayString m_array
;
393 // -------------------------------------
395 // -------------------------------------
397 class MyApp
: public wxApp
404 // -------------------------------------
406 // -------------------------------------
408 class MyFrame
: public wxFrame
411 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
414 void OnQuit(wxCommandEvent
& event
);
415 void OnAbout(wxCommandEvent
& event
);
417 void OnAddMozart(wxCommandEvent
& event
);
418 void OnDeleteMusic(wxCommandEvent
& event
);
420 void OnPrependList(wxCommandEvent
& event
);
421 void OnDeleteList(wxCommandEvent
& event
);
423 void OnValueChanged( wxDataViewEvent
&event
);
424 void OnItemAdded( wxDataViewEvent
&event
);
425 void OnItemDeleted( wxDataViewEvent
&event
);
426 void OnActivated( wxDataViewEvent
&event
);
427 void OnHeaderClick( wxDataViewEvent
&event
);
428 void OnHeaderRightClick( wxDataViewEvent
&event
);
429 void OnSorted( wxDataViewEvent
&event
);
431 void OnRightClick( wxMouseEvent
&event
);
432 void OnGoto( wxCommandEvent
&event
);
435 wxDataViewCtrl
* m_musicCtrl
;
436 wxObjectDataPtr
<MyMusicModel
> m_music_model
;
438 wxDataViewCtrl
* m_listCtrl
;
439 wxObjectDataPtr
<MyListModel
> m_list_model
;
445 DECLARE_EVENT_TABLE()
448 // -------------------------------------
450 // -------------------------------------
454 bool MyApp::OnInit(void)
456 if ( !wxApp::OnInit() )
459 // build the first frame
461 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
474 // -------------------------------------
476 // -------------------------------------
481 ID_ABOUT
= wxID_ABOUT
,
487 ID_DELETE_MUSIC
= 101,
489 ID_PREPEND_LIST
= 200,
490 ID_DELETE_LIST
= 201,
494 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
495 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
496 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
497 EVT_BUTTON( ID_ADD_MOZART
, MyFrame::OnAddMozart
)
498 EVT_BUTTON( ID_DELETE_MUSIC
, MyFrame::OnDeleteMusic
)
499 EVT_BUTTON( ID_PREPEND_LIST
, MyFrame::OnPrependList
)
500 EVT_BUTTON( ID_DELETE_LIST
, MyFrame::OnDeleteList
)
501 EVT_BUTTON( ID_GOTO
, MyFrame::OnGoto
)
502 EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL
, MyFrame::OnItemAdded
)
503 EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL
, MyFrame::OnItemDeleted
)
504 EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
505 EVT_DATAVIEW_MODEL_ITEM_CHANGED( ID_MUSIC_CTRL
, MyFrame::OnValueChanged
)
506 EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL
, MyFrame::OnActivated
)
507 EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL
, MyFrame::OnHeaderClick
)
508 EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL
, MyFrame::OnHeaderRightClick
)
509 EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL
, MyFrame::OnSorted
)
510 EVT_RIGHT_UP(MyFrame::OnRightClick
)
513 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
514 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
518 SetIcon(wxICON(sample
));
522 wxMenu
*file_menu
= new wxMenu
;
523 file_menu
->Append(ID_ABOUT
, "&About");
524 file_menu
->AppendSeparator();
525 file_menu
->Append(ID_EXIT
, "E&xit");
527 wxMenuBar
*menu_bar
= new wxMenuBar
;
528 menu_bar
->Append(file_menu
, "&File");
530 SetMenuBar(menu_bar
);
533 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
535 wxBoxSizer
*data_sizer
= new wxBoxSizer( wxHORIZONTAL
);
539 m_musicCtrl
= new wxDataViewCtrl( this, ID_MUSIC_CTRL
, wxDefaultPosition
,
540 wxDefaultSize
, wxDV_MULTIPLE
);
542 m_music_model
= new MyMusicModel
;
543 m_musicCtrl
->AssociateModel( m_music_model
.get() );
545 m_musicCtrl
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
546 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
547 m_musicCtrl
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE
, 200,
548 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
549 m_musicCtrl
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
552 data_sizer
->Add( m_musicCtrl
, 3, wxGROW
);
558 m_listCtrl
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
559 wxDefaultSize
, wxDV_MULTIPLE
);
561 m_list_model
= new MyListModel
;
562 m_listCtrl
->AssociateModel( m_list_model
.get() );
564 m_listCtrl
->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE
, 120 );
565 m_listCtrl
->AppendTextColumn( "index", 1, wxDATAVIEW_CELL_INERT
, 120 );
567 data_sizer
->Add( m_listCtrl
, 2, wxGROW
);
571 main_sizer
->Add( data_sizer
, 2, wxGROW
);
573 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
575 button_sizer
->Add( new wxButton( this, ID_ADD_MOZART
, "Add Mozart"), 0, wxALL
, 10 );
576 button_sizer
->Add( new wxButton( this, ID_DELETE_MUSIC
, "Delete selected"), 0, wxALL
, 10 );
577 button_sizer
->Add( 10, 10, 1 );
578 button_sizer
->Add( new wxButton( this, ID_PREPEND_LIST
, "Prepend"), 0, wxALL
, 10 );
579 button_sizer
->Add( new wxButton( this, ID_DELETE_LIST
, "Delete selected"), 0, wxALL
, 10 );
580 button_sizer
->Add( new wxButton( this, ID_GOTO
, "Goto 50"), 0, wxALL
, 10 );
582 main_sizer
->Add( button_sizer
, 0, wxGROW
, 0 );
584 m_log
= new wxTextCtrl( this, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
585 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_log
));
586 wxLogMessage("This is the log window");
588 main_sizer
->Add( m_log
, 1, wxGROW
);
590 SetSizer( main_sizer
);
593 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
598 void MyFrame::OnAddMozart(wxCommandEvent
& WXUNUSED(event
) )
600 m_music_model
->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
603 void MyFrame::OnDeleteMusic(wxCommandEvent
& WXUNUSED(event
) )
605 wxDataViewItemArray items
;
606 int len
= m_musicCtrl
->GetSelections( items
);
607 for( int i
= 0; i
< len
; i
++ )
609 m_music_model
->Delete( items
[i
] );
612 void MyFrame::OnPrependList( wxCommandEvent
& WXUNUSED(event
) )
614 m_list_model
->Prepend( "Test" );
617 void MyFrame::OnDeleteList( wxCommandEvent
& WXUNUSED(event
) )
619 wxDataViewItemArray items
;
620 int len
= m_listCtrl
->GetSelections( items
);
621 for( int i
= 0; i
< len
; i
++ )
623 m_list_model
->DeleteItem( items
[i
] );
626 void MyFrame::OnItemAdded( wxDataViewEvent
&event
)
631 wxLogMessage("wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, Item Id: %d",event
.GetItem().GetID());
634 void MyFrame::OnItemDeleted( wxDataViewEvent
&event
)
639 wxLogMessage( "EVT_DATAVIEW_MODEL_ITEM_DELETED, Item Id: %d", event
.GetItem().GetID() );
642 void MyFrame::OnValueChanged( wxDataViewEvent
&event
)
647 wxLogMessage( "EVT_DATAVIEW_MODEL_VALUE_CHANGED, Item Id: %d; Column: %d", event
.GetItem().GetID(), event
.GetColumn() );
650 void MyFrame::OnActivated( wxDataViewEvent
&event
)
655 wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item Id: %d; Column: %d", event
.GetItem().GetID(), event
.GetColumn());
658 void MyFrame::OnHeaderClick( wxDataViewEvent
&event
)
663 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column: %d", event
.GetColumn());
666 void MyFrame::OnHeaderRightClick( wxDataViewEvent
&event
)
671 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column: %d", event
.GetColumn());
674 void MyFrame::OnSorted( wxDataViewEvent
&event
)
679 wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column: %d", event
.GetColumn());
682 void MyFrame::OnRightClick( wxMouseEvent
&event
)
687 wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event
.GetX(), event
.GetY());
690 void MyFrame::OnGoto( wxCommandEvent
&event
)
692 m_listCtrl
->EnsureVisible(50);
695 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
697 wxAboutDialogInfo info
;
698 info
.SetName(_("DataView sample"));
699 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
700 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));