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 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
24 #include "wx/dataview.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
32 #include "wx_small.xpm"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 MyMusicTreeModel::MyMusicTreeModel()
41 m_root
= new MyMusicTreeModelNode( NULL
, "My Music" );
44 m_pop
= new MyMusicTreeModelNode( m_root
, "Pop music" );
46 new MyMusicTreeModelNode( m_pop
, "You are not alone"), "Michael Jackson", 1995 ) );
48 new MyMusicTreeModelNode( m_pop
, "Take a bow", "Madonna", 1994 ) );
49 m_root
->Append( m_pop
);
51 // setup classical music
52 m_classical
= new MyMusicTreeModelNode( m_root
, "Classical music" );
53 m_ninth
= new MyMusicTreeModelNode( m_classical
, "Ninth symphony",
54 "Ludwig van Beethoven", 1824 );
55 m_classical
->Append( m_ninth
);
56 m_classical
->Append( new MyMusicTreeModelNode( m_classical
, "German Requiem",
57 "Johannes Brahms", 1868 ) );
58 m_root
->Append( m_classical
);
60 m_classicalMusicIsKnownToControl
= false;
63 wxString
MyMusicTreeModel::GetTitle( const wxDataViewItem
&item
) const
65 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
66 if (!node
) // happens if item.IsOk()==false
72 int MyMusicTreeModel::GetYear( const wxDataViewItem
&item
) const
74 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
75 if (!node
) // happens if item.IsOk()==false
81 void MyMusicTreeModel::AddToClassical( const wxString
&title
, const wxString
&artist
,
88 // it was removed: restore it
89 m_classical
= new MyMusicTreeModelNode( m_root
, "Classical music" );
90 m_root
->Append( m_classical
);
93 wxDataViewItem
child( (void*) m_classical
);
94 wxDataViewItem
parent( (void*) m_root
);
95 ItemAdded( parent
, child
);
98 // add to the classical music node a new node:
99 MyMusicTreeModelNode
*child_node
=
100 new MyMusicTreeModelNode( m_classical
, title
, artist
, year
);
101 m_classical
->Append( child_node
);
103 // FIXME: what's m_classicalMusicIsKnownToControl for?
104 if (m_classicalMusicIsKnownToControl
)
107 wxDataViewItem
child( (void*) child_node
);
108 wxDataViewItem
parent( (void*) m_classical
);
109 ItemAdded( parent
, child
);
113 void MyMusicTreeModel::Delete( const wxDataViewItem
&item
)
115 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
116 if (!node
) // happens if item.IsOk()==false
119 wxDataViewItem
parent( node
->GetParent() );
122 wxASSERT(node
== m_root
);
124 // don't make the control completely empty:
125 wxLogError( "Cannot remove the root item!" );
129 // is the node one of those we keep stored in special pointers?
132 else if (node
== m_classical
)
134 else if (node
== m_ninth
)
137 // first remove the node from the parent's array of children;
138 // NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_
139 // thus removing the node from it doesn't result in freeing it
140 node
->GetParent()->GetChildren().Remove( node
);
146 ItemDeleted( parent
, item
);
149 int MyMusicTreeModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
150 unsigned int column
, bool ascending
)
152 wxASSERT(item1
.IsOk() && item2
.IsOk());
153 // should never happen
155 if (IsContainer(item1
) && IsContainer(item2
))
157 wxVariant value1
, value2
;
158 GetValue( value1
, item1
, 0 );
159 GetValue( value2
, item2
, 0 );
161 wxString str1
= value1
.GetString();
162 wxString str2
= value2
.GetString();
163 int res
= str1
.Cmp( str2
);
166 // items must be different
167 wxUIntPtr litem1
= (wxUIntPtr
) item1
.GetID();
168 wxUIntPtr litem2
= (wxUIntPtr
) item2
.GetID();
170 return litem1
-litem2
;
173 return wxDataViewModel::Compare( item1
, item2
, column
, ascending
);
176 void MyMusicTreeModel::GetValue( wxVariant
&variant
,
177 const wxDataViewItem
&item
, unsigned int col
) const
179 wxASSERT(item
.IsOk());
181 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
185 variant
= node
->m_title
;
188 variant
= node
->m_artist
;
191 variant
= (long) node
->m_year
;
194 variant
= node
->m_quality
;
197 // wxMac doesn't conceal the popularity progress renderer, return 0 for containers
198 if (IsContainer(item
))
201 variant
= (long) 80; // all music is very 80% popular
204 // Make size of red square depend on year
205 if (GetYear(item
) < 1900)
212 wxLogError( "MyMusicTreeModel::GetValue: wrong column %d", col
);
216 bool MyMusicTreeModel::SetValue( const wxVariant
&variant
,
217 const wxDataViewItem
&item
, unsigned int col
)
219 wxASSERT(item
.IsOk());
221 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
225 node
->m_title
= variant
.GetString();
228 node
->m_artist
= variant
.GetString();
231 node
->m_year
= variant
.GetLong();
234 node
->m_quality
= variant
.GetString();
238 wxLogError( "MyMusicTreeModel::SetValue: wrong column" );
243 wxDataViewItem
MyMusicTreeModel::GetParent( const wxDataViewItem
&item
) const
245 // the invisible root node has no parent
247 return wxDataViewItem(0);
249 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
251 // "MyMusic" also has no parent
253 return wxDataViewItem(0);
255 return wxDataViewItem( (void*) node
->GetParent() );
258 bool MyMusicTreeModel::IsContainer( const wxDataViewItem
&item
) const
260 // the invisble root node can have children
261 // (in our model always "MyMusic")
265 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
266 return node
->IsContainer();
269 unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem
&parent
,
270 wxDataViewItemArray
&array
) const
272 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) parent
.GetID();
275 array
.Add( wxDataViewItem( (void*) m_root
) );
279 if (node
== m_classical
)
281 MyMusicTreeModel
*model
= (MyMusicTreeModel
*)(const MyMusicTreeModel
*) this;
282 model
->m_classicalMusicIsKnownToControl
= true;
285 if (node
->GetChildCount() == 0)
290 unsigned int count
= node
->GetChildren().GetCount();
291 for (unsigned int pos
= 0; pos
< count
; pos
++)
293 MyMusicTreeModelNode
*child
= node
->GetChildren().Item( pos
);
294 array
.Add( wxDataViewItem( (void*) child
) );
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 static int my_sort_reverse( int *v1
, int *v2
)
311 static int my_sort( int *v1
, int *v2
)
317 #define INITIAL_NUMBER_OF_ITEMS 100
319 #define INITIAL_NUMBER_OF_ITEMS 100000
322 MyListModel::MyListModel() :
323 wxDataViewVirtualListModel( INITIAL_NUMBER_OF_ITEMS
)
325 m_virtualItems
= INITIAL_NUMBER_OF_ITEMS
;
327 // the first 100 items are really stored in this model;
328 // all the others are synthetized on request
329 for (unsigned int i
= 0; i
< 100; i
++)
332 str
.Printf( "real row %d"), i
);
336 m_icon
[0] = wxIcon( null_xpm
);
337 m_icon
[1] = wxIcon( wx_small_xpm
);
340 void MyListModel::Prepend( const wxString
&text
)
342 m_array
.Insert( text
, 0 );
346 void MyListModel::DeleteItem( const wxDataViewItem
&item
)
348 unsigned int row
= GetRow( item
);
349 if (row
>= m_array
.GetCount())
352 m_array
.RemoveAt( row
);
356 void MyListModel::DeleteItems( const wxDataViewItemArray
&items
)
360 for (i
= 0; i
< items
.GetCount(); i
++)
362 unsigned int row
= GetRow( items
[i
] );
363 if (row
< m_array
.GetCount())
367 if (rows
.GetCount() == 0)
369 // none of the selected items were in the range of the items
370 // which we store... for simplicity, don't allow removing them
371 wxLogError( "Cannot remove rows with an index greater than %d", m_array
.GetCount() );
375 // Sort in descending order so that the last
376 // row will be deleted first. Otherwise the
377 // remaining indeces would all be wrong.
378 rows
.Sort( my_sort_reverse
);
379 for (i
= 0; i
< rows
.GetCount(); i
++)
380 m_array
.RemoveAt( rows
[i
] );
382 // This is just to test if wxDataViewCtrl can
383 // cope with removing rows not sorted in
385 rows
.Sort( my_sort
);
389 void MyListModel::AddMany()
391 m_virtualItems
+= 1000;
392 Reset( m_array
.GetCount() + m_virtualItems
);
395 void MyListModel::GetValueByRow( wxVariant
&variant
,
396 unsigned int row
, unsigned int col
) const
400 if (row
>= m_array
.GetCount())
401 variant
= wxString::Format( "virtual row %d", row
);
403 variant
= m_array
[ row
];
407 wxDataViewIconText
data( "test", m_icon
[ row%2
] );
412 if (row
>= m_array
.GetCount())
415 variant
= "blue/green/red";
419 bool MyListModel::GetAttrByRow( unsigned int row
, unsigned int col
,
420 wxDataViewItemAttr
&attr
)
425 if (row
< m_array
.GetCount())
427 attr
.SetColour( (row%2
) == 0 ? *wxBLUE
:
428 ((row%3
) == 0 ? *wxGREEN
: *wxRED
) );
429 attr
.SetItalic( (row%2
) == 5 );
435 bool MyListModel::SetValueByRow( const wxVariant
&variant
,
436 unsigned int row
, unsigned int col
)
440 if (row
>= m_array
.GetCount())
442 // the item is not in the range of the items
443 // which we store... for simplicity, don't allow editing it
444 wxLogError( "Cannot edit rows with an index greater than %d", m_array
.GetCount() );
448 m_array
[row
] = variant
.GetString();