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 licence
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 wxString
MyMusicTreeModel::GetArtist( const wxDataViewItem
&item
) const
74 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
75 if (!node
) // happens if item.IsOk()==false
78 return node
->m_artist
;
81 int MyMusicTreeModel::GetYear( const wxDataViewItem
&item
) const
83 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
84 if (!node
) // happens if item.IsOk()==false
90 void MyMusicTreeModel::AddToClassical( const wxString
&title
, const wxString
&artist
,
97 // it was removed: restore it
98 m_classical
= new MyMusicTreeModelNode( m_root
, "Classical music" );
99 m_root
->Append( m_classical
);
102 wxDataViewItem
child( (void*) m_classical
);
103 wxDataViewItem
parent( (void*) m_root
);
104 ItemAdded( parent
, child
);
107 // add to the classical music node a new node:
108 MyMusicTreeModelNode
*child_node
=
109 new MyMusicTreeModelNode( m_classical
, title
, artist
, year
);
110 m_classical
->Append( child_node
);
112 // FIXME: what's m_classicalMusicIsKnownToControl for?
113 if (m_classicalMusicIsKnownToControl
)
116 wxDataViewItem
child( (void*) child_node
);
117 wxDataViewItem
parent( (void*) m_classical
);
118 ItemAdded( parent
, child
);
122 void MyMusicTreeModel::Delete( const wxDataViewItem
&item
)
124 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
125 if (!node
) // happens if item.IsOk()==false
128 wxDataViewItem
parent( node
->GetParent() );
131 wxASSERT(node
== m_root
);
133 // don't make the control completely empty:
134 wxLogError( "Cannot remove the root item!" );
138 // is the node one of those we keep stored in special pointers?
141 else if (node
== m_classical
)
143 else if (node
== m_ninth
)
146 // first remove the node from the parent's array of children;
147 // NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_
148 // thus removing the node from it doesn't result in freeing it
149 node
->GetParent()->GetChildren().Remove( node
);
155 ItemDeleted( parent
, item
);
158 int MyMusicTreeModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
159 unsigned int column
, bool ascending
) const
161 wxASSERT(item1
.IsOk() && item2
.IsOk());
162 // should never happen
164 if (IsContainer(item1
) && IsContainer(item2
))
166 wxVariant value1
, value2
;
167 GetValue( value1
, item1
, 0 );
168 GetValue( value2
, item2
, 0 );
170 wxString str1
= value1
.GetString();
171 wxString str2
= value2
.GetString();
172 int res
= str1
.Cmp( str2
);
175 // items must be different
176 wxUIntPtr litem1
= (wxUIntPtr
) item1
.GetID();
177 wxUIntPtr litem2
= (wxUIntPtr
) item2
.GetID();
179 return litem1
-litem2
;
182 return wxDataViewModel::Compare( item1
, item2
, column
, ascending
);
185 void MyMusicTreeModel::GetValue( wxVariant
&variant
,
186 const wxDataViewItem
&item
, unsigned int col
) const
188 wxASSERT(item
.IsOk());
190 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
194 variant
= node
->m_title
;
197 variant
= node
->m_artist
;
200 variant
= (long) node
->m_year
;
203 variant
= node
->m_quality
;
206 variant
= 80L; // all music is very 80% popular
209 if (GetYear(item
) < 1900)
216 wxLogError( "MyMusicTreeModel::GetValue: wrong column %d", col
);
220 bool MyMusicTreeModel::SetValue( const wxVariant
&variant
,
221 const wxDataViewItem
&item
, unsigned int col
)
223 wxASSERT(item
.IsOk());
225 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
229 node
->m_title
= variant
.GetString();
232 node
->m_artist
= variant
.GetString();
235 node
->m_year
= variant
.GetLong();
238 node
->m_quality
= variant
.GetString();
242 wxLogError( "MyMusicTreeModel::SetValue: wrong column" );
247 bool MyMusicTreeModel::IsEnabled( const wxDataViewItem
&item
,
248 unsigned int col
) const
250 wxASSERT(item
.IsOk());
252 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
254 // disable Beethoven's ratings, his pieces can only be good
255 return !(col
== 3 && node
->m_artist
.EndsWith("Beethoven"));
258 wxDataViewItem
MyMusicTreeModel::GetParent( const wxDataViewItem
&item
) const
260 // the invisible root node has no parent
262 return wxDataViewItem(0);
264 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
266 // "MyMusic" also has no parent
268 return wxDataViewItem(0);
270 return wxDataViewItem( (void*) node
->GetParent() );
273 bool MyMusicTreeModel::IsContainer( const wxDataViewItem
&item
) const
275 // the invisble root node can have children
276 // (in our model always "MyMusic")
280 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) item
.GetID();
281 return node
->IsContainer();
284 unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem
&parent
,
285 wxDataViewItemArray
&array
) const
287 MyMusicTreeModelNode
*node
= (MyMusicTreeModelNode
*) parent
.GetID();
290 array
.Add( wxDataViewItem( (void*) m_root
) );
294 if (node
== m_classical
)
296 MyMusicTreeModel
*model
= (MyMusicTreeModel
*)(const MyMusicTreeModel
*) this;
297 model
->m_classicalMusicIsKnownToControl
= true;
300 if (node
->GetChildCount() == 0)
305 unsigned int count
= node
->GetChildren().GetCount();
306 for (unsigned int pos
= 0; pos
< count
; pos
++)
308 MyMusicTreeModelNode
*child
= node
->GetChildren().Item( pos
);
309 array
.Add( wxDataViewItem( (void*) child
) );
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 static int my_sort_reverse( int *v1
, int *v2
)
326 static int my_sort( int *v1
, int *v2
)
331 #define INITIAL_NUMBER_OF_ITEMS 10000
333 MyListModel::MyListModel() :
334 wxDataViewVirtualListModel( INITIAL_NUMBER_OF_ITEMS
)
336 // the first 100 items are really stored in this model;
337 // all the others are synthesized on request
338 static const unsigned NUMBER_REAL_ITEMS
= 100;
340 m_textColValues
.reserve(NUMBER_REAL_ITEMS
);
341 m_textColValues
.push_back("first row with long label to test ellipsization");
342 for (unsigned int i
= 1; i
< NUMBER_REAL_ITEMS
; i
++)
344 m_textColValues
.push_back(wxString::Format("real row %d", i
));
347 m_iconColValues
.assign(NUMBER_REAL_ITEMS
, "test");
349 m_icon
[0] = wxIcon( null_xpm
);
350 m_icon
[1] = wxIcon( wx_small_xpm
);
353 void MyListModel::Prepend( const wxString
&text
)
355 m_textColValues
.Insert( text
, 0 );
359 void MyListModel::DeleteItem( const wxDataViewItem
&item
)
361 unsigned int row
= GetRow( item
);
363 if (row
>= m_textColValues
.GetCount())
366 m_textColValues
.RemoveAt( row
);
370 void MyListModel::DeleteItems( const wxDataViewItemArray
&items
)
374 for (i
= 0; i
< items
.GetCount(); i
++)
376 unsigned int row
= GetRow( items
[i
] );
377 if (row
< m_textColValues
.GetCount())
381 if (rows
.GetCount() == 0)
383 // none of the selected items were in the range of the items
384 // which we store... for simplicity, don't allow removing them
385 wxLogError( "Cannot remove rows with an index greater than %d", m_textColValues
.GetCount() );
389 // Sort in descending order so that the last
390 // row will be deleted first. Otherwise the
391 // remaining indeces would all be wrong.
392 rows
.Sort( my_sort_reverse
);
393 for (i
= 0; i
< rows
.GetCount(); i
++)
394 m_textColValues
.RemoveAt( rows
[i
] );
396 // This is just to test if wxDataViewCtrl can
397 // cope with removing rows not sorted in
399 rows
.Sort( my_sort
);
403 void MyListModel::AddMany()
405 Reset( GetCount()+1000 );
408 void MyListModel::GetValueByRow( wxVariant
&variant
,
409 unsigned int row
, unsigned int col
) const
413 case Col_EditableText
:
414 if (row
>= m_textColValues
.GetCount())
415 variant
= wxString::Format( "virtual row %d", row
);
417 variant
= m_textColValues
[ row
];
423 if ( row
>= m_iconColValues
.GetCount() )
424 text
= "virtual icon";
426 text
= m_iconColValues
[row
];
428 variant
<< wxDataViewIconText(text
, m_icon
[row
% 2]);
432 case Col_TextWithAttr
:
434 static const char *labels
[5] =
436 "blue", "green", "red", "bold cyan", "default",
439 variant
= labels
[row
% 5];
444 variant
= wxString::Format("%d", row
% 100);
448 wxFAIL_MSG( "invalid column" );
452 bool MyListModel::GetAttrByRow( unsigned int row
, unsigned int col
,
453 wxDataViewItemAttr
&attr
) const
457 case Col_EditableText
:
463 attr
.SetColour(*wxLIGHT_GREY
);
466 case Col_TextWithAttr
:
468 // do what the labels defined in GetValueByRow() hint at
472 attr
.SetColour(*wxBLUE
);
476 attr
.SetColour(*wxGREEN
);
480 attr
.SetColour(*wxRED
);
484 attr
.SetColour(*wxCYAN
);
494 wxFAIL_MSG( "invalid column" );
500 bool MyListModel::SetValueByRow( const wxVariant
&variant
,
501 unsigned int row
, unsigned int col
)
505 case Col_EditableText
:
507 if (row
>= m_textColValues
.GetCount())
509 // the item is not in the range of the items
510 // which we store... for simplicity, don't allow editing it
511 wxLogError( "Cannot edit rows with an index greater than %d",
512 m_textColValues
.GetCount() );
516 if ( col
== Col_EditableText
)
518 m_textColValues
[row
] = variant
.GetString();
520 else // col == Col_IconText
522 wxDataViewIconText iconText
;
524 m_iconColValues
[row
] = iconText
.GetText();
528 case Col_TextWithAttr
:
530 wxLogError("Cannot edit the column %d", col
);
534 wxFAIL_MSG( "invalid column" );
541 // ----------------------------------------------------------------------------
542 // MyListStoreDerivedModel
543 // ----------------------------------------------------------------------------
545 bool MyListStoreDerivedModel::IsEnabledByRow(unsigned int row
, unsigned int col
) const
547 // disabled the last two checkboxes
548 return !(col
== 0 && 8 <= row
&& row
<= 9);