- // constructor
-
- MyMusicModel()
- {
- m_root = new MyMusicModelNode( NULL, wxT("My Music" ));
- m_pop = new MyMusicModelNode( m_root, wxT("Pop music") );
- m_root->Append( m_pop );
- m_pop->Append( new MyMusicModelNode( m_pop,
- wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
- m_pop->Append( new MyMusicModelNode( m_pop,
- wxT("Take a bow"), wxT("Madonna"), 1994 ) );
- m_classical = new MyMusicModelNode( m_root, wxT("Classical music") );
- m_root->Append( m_classical );
- m_ninth = new MyMusicModelNode( m_classical,
- wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 );
- m_classical->Append( m_ninth );
- m_classical->Append( new MyMusicModelNode( m_classical,
- wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
- m_classicalMusicIsKnownToControl = false;
- }
-
- ~MyMusicModel()
- {
- delete m_root;
- }
-
- // helper method for wxLog
-
- wxString GetTitle( const wxDataViewItem &item ) const
- {
- MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
- if (!node)
- return wxEmptyString;
-
- return node->m_title;
- }
-
- int GetYear( const wxDataViewItem &item ) const
- {
- MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
- if (!node)
- return 2000;
-
- return node->m_year;
- }
-
- // helper methods to change the model
-
- void AddToClassical( const wxString &title, const wxString &artist, int year )
- {
- // add to data
- MyMusicModelNode *child_node =
- new MyMusicModelNode( m_classical, title, artist, year );
-
- m_classical->Append( child_node );
-
- if (m_classicalMusicIsKnownToControl)
- {
- // notify control
- wxDataViewItem child( (void*) child_node );
- wxDataViewItem parent( (void*) m_classical );
- ItemAdded( parent, child );
- }
- }
-
- void Delete( const wxDataViewItem &item )
- {
- MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
- wxDataViewItem parent( node->GetParent() );
-
- node->GetParent()->GetChildren().Remove( node );
- delete node;
-
- // notify control
- ItemDeleted( parent, item );
- }
-
- // override sorting to always sort branches ascendingly
-
- int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
- unsigned int column, bool ascending )
- {
- if (IsContainer(item1) && IsContainer(item2))
- {
- wxVariant value1,value2;
- GetValue( value1, item1, 0 );
- GetValue( value2, item2, 0 );
-
- wxString str1 = value1.GetString();
- wxString str2 = value2.GetString();
- int res = str1.Cmp( str2 );
- if (res) return res;
-
- // items must be different
- wxUIntPtr litem1 = (wxUIntPtr) item1.GetID();
- wxUIntPtr litem2 = (wxUIntPtr) item2.GetID();
-
- return litem1-litem2;
- }
-
- return wxDataViewModel::Compare( item1, item2, column, ascending );
- }