X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/03dfc8f515662f12c7a13f66bf9064d5c5985c8a..4d623b67ee74794fbeba5805a269433d030c67d5:/samples/dataview/mymodels.cpp diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index 5d5bb2884e..a3cb111198 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -6,7 +6,7 @@ // Created: 06/01/06 // RCS-ID: $Id$ // Copyright: (c) Robert Roebling -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -38,23 +38,23 @@ MyMusicTreeModel::MyMusicTreeModel() { - m_root = new MyMusicTreeModelNode( NULL, wxT("My Music" )); + m_root = new MyMusicTreeModelNode( NULL, "My Music" ); // setup pop music - m_pop = new MyMusicTreeModelNode( m_root, wxT("Pop music") ); + m_pop = new MyMusicTreeModelNode( m_root, "Pop music" ); m_pop->Append( - new MyMusicTreeModelNode( m_pop, wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) ); + new MyMusicTreeModelNode( m_pop, "You are not alone", "Michael Jackson", 1995 ) ); m_pop->Append( - new MyMusicTreeModelNode( m_pop, wxT("Take a bow"), wxT("Madonna"), 1994 ) ); + new MyMusicTreeModelNode( m_pop, "Take a bow", "Madonna", 1994 ) ); m_root->Append( m_pop ); // setup classical music - m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") ); - m_ninth = new MyMusicTreeModelNode( m_classical, wxT("Ninth symphony"), - wxT("Ludwig van Beethoven"), 1824 ); + m_classical = new MyMusicTreeModelNode( m_root, "Classical music" ); + m_ninth = new MyMusicTreeModelNode( m_classical, "Ninth symphony", + "Ludwig van Beethoven", 1824 ); m_classical->Append( m_ninth ); - m_classical->Append( new MyMusicTreeModelNode( m_classical, wxT("German Requiem"), - wxT("Johannes Brahms"), 1868 ) ); + m_classical->Append( new MyMusicTreeModelNode( m_classical, "German Requiem", + "Johannes Brahms", 1868 ) ); m_root->Append( m_classical ); m_classicalMusicIsKnownToControl = false; @@ -69,6 +69,15 @@ wxString MyMusicTreeModel::GetTitle( const wxDataViewItem &item ) const return node->m_title; } +wxString MyMusicTreeModel::GetArtist( const wxDataViewItem &item ) const +{ + MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); + if (!node) // happens if item.IsOk()==false + return wxEmptyString; + + return node->m_artist; +} + int MyMusicTreeModel::GetYear( const wxDataViewItem &item ) const { MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); @@ -86,8 +95,13 @@ void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &ar wxASSERT(m_root); // it was removed: restore it - m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") ); + m_classical = new MyMusicTreeModelNode( m_root, "Classical music" ); m_root->Append( m_classical ); + + // notify control + wxDataViewItem child( (void*) m_classical ); + wxDataViewItem parent( (void*) m_root ); + ItemAdded( parent, child ); } // add to the classical music node a new node: @@ -95,6 +109,7 @@ void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &ar new MyMusicTreeModelNode( m_classical, title, artist, year ); m_classical->Append( child_node ); + // FIXME: what's m_classicalMusicIsKnownToControl for? if (m_classicalMusicIsKnownToControl) { // notify control @@ -116,7 +131,7 @@ void MyMusicTreeModel::Delete( const wxDataViewItem &item ) wxASSERT(node == m_root); // don't make the control completely empty: - wxLogError("Cannot remove the root item!"); + wxLogError( "Cannot remove the root item!" ); return; } @@ -141,7 +156,7 @@ void MyMusicTreeModel::Delete( const wxDataViewItem &item ) } int MyMusicTreeModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, - unsigned int column, bool ascending ) + unsigned int column, bool ascending ) const { wxASSERT(item1.IsOk() && item2.IsOk()); // should never happen @@ -188,22 +203,17 @@ void MyMusicTreeModel::GetValue( wxVariant &variant, variant = node->m_quality; break; case 4: - // wxMac doesn't conceal the popularity progress renderer, return 0 for containers - if (IsContainer(item)) - variant = (long) 0; - else - variant = (long) 80; // all music is very 80% popular + variant = 80L; // all music is very 80% popular break; case 5: - // Make size of red square depend on year if (GetYear(item) < 1900) - variant = (long) 35; + variant = "old"; else - variant = (long) 25; + variant = "new"; break; default: - wxLogError( wxT("MyMusicTreeModel::GetValue: wrong column %d"), col ); + wxLogError( "MyMusicTreeModel::GetValue: wrong column %d", col ); } } @@ -229,11 +239,22 @@ bool MyMusicTreeModel::SetValue( const wxVariant &variant, return true; default: - wxLogError( wxT("MyMusicTreeModel::SetValue: wrong column") ); + wxLogError( "MyMusicTreeModel::SetValue: wrong column" ); } return false; } +bool MyMusicTreeModel::IsEnabled( const wxDataViewItem &item, + unsigned int col ) const +{ + wxASSERT(item.IsOk()); + + MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); + + // disable Beethoven's ratings, his pieces can only be good + return !(col == 3 && node->m_artist.EndsWith("Beethoven")); +} + wxDataViewItem MyMusicTreeModel::GetParent( const wxDataViewItem &item ) const { // the invisible root node has no parent @@ -307,43 +328,42 @@ static int my_sort( int *v1, int *v2 ) return *v1-*v2; } -#ifdef __WXMAC__ - #define INITIAL_NUMBER_OF_ITEMS 100 -#else - #define INITIAL_NUMBER_OF_ITEMS 100000 -#endif +#define INITIAL_NUMBER_OF_ITEMS 10000 MyListModel::MyListModel() : wxDataViewVirtualListModel( INITIAL_NUMBER_OF_ITEMS ) { - m_virtualItems = INITIAL_NUMBER_OF_ITEMS; - // the first 100 items are really stored in this model; - // all the others are synthetized on request - for (unsigned int i = 0; i < 100; i++) + // all the others are synthesized on request + static const unsigned NUMBER_REAL_ITEMS = 100; + + m_textColValues.reserve(NUMBER_REAL_ITEMS); + m_textColValues.push_back("first row with long label to test ellipsization"); + for (unsigned int i = 1; i < NUMBER_REAL_ITEMS; i++) { - wxString str; - str.Printf( wxT("real row %d"), i ); - m_array.Add( str ); + m_textColValues.push_back(wxString::Format("real row %d", i)); } + m_iconColValues.assign(NUMBER_REAL_ITEMS, "test"); + m_icon[0] = wxIcon( null_xpm ); m_icon[1] = wxIcon( wx_small_xpm ); } void MyListModel::Prepend( const wxString &text ) { - m_array.Insert( text, 0 ); + m_textColValues.Insert( text, 0 ); RowPrepended(); } void MyListModel::DeleteItem( const wxDataViewItem &item ) { unsigned int row = GetRow( item ); - if (row >= m_array.GetCount()) + + if (row >= m_textColValues.GetCount()) return; - m_array.RemoveAt( row ); + m_textColValues.RemoveAt( row ); RowDeleted( row ); } @@ -351,10 +371,10 @@ void MyListModel::DeleteItems( const wxDataViewItemArray &items ) { unsigned i; wxArrayInt rows; - for (int i = 0; i < items.GetCount(); i++) + for (i = 0; i < items.GetCount(); i++) { unsigned int row = GetRow( items[i] ); - if (row < m_array.GetCount()) + if (row < m_textColValues.GetCount()) rows.Add( row ); } @@ -362,7 +382,7 @@ void MyListModel::DeleteItems( const wxDataViewItemArray &items ) { // none of the selected items were in the range of the items // which we store... for simplicity, don't allow removing them - wxLogError("Cannot remove rows with an index greater than %d", m_array.GetCount()); + wxLogError( "Cannot remove rows with an index greater than %d", m_textColValues.GetCount() ); return; } @@ -371,7 +391,7 @@ void MyListModel::DeleteItems( const wxDataViewItemArray &items ) // remaining indeces would all be wrong. rows.Sort( my_sort_reverse ); for (i = 0; i < rows.GetCount(); i++) - m_array.RemoveAt( rows[i] ); + m_textColValues.RemoveAt( rows[i] ); // This is just to test if wxDataViewCtrl can // cope with removing rows not sorted in @@ -382,45 +402,96 @@ void MyListModel::DeleteItems( const wxDataViewItemArray &items ) void MyListModel::AddMany() { - m_virtualItems += 1000; - Reset( m_array.GetCount() + m_virtualItems ); + Reset( GetCount()+1000 ); } void MyListModel::GetValueByRow( wxVariant &variant, unsigned int row, unsigned int col ) const { - if (col==0) - { - if (row >= m_array.GetCount()) - variant = wxString::Format( wxT("virtual row %d"), row ); - else - variant = m_array[ row ]; - } - else if (col==1) + switch ( col ) { - wxDataViewIconText data( wxT("test"), m_icon[ row%2 ] ); - variant << data; - } - else if (col==2) - { - if (row >= m_array.GetCount()) - variant = wxT("plain"); - else - variant = wxT("blue/green/red"); + case Col_EditableText: + if (row >= m_textColValues.GetCount()) + variant = wxString::Format( "virtual row %d", row ); + else + variant = m_textColValues[ row ]; + break; + + case Col_IconText: + { + wxString text; + if ( row >= m_iconColValues.GetCount() ) + text = "virtual icon"; + else + text = m_iconColValues[row]; + + variant << wxDataViewIconText(text, m_icon[row % 2]); + } + break; + + case Col_TextWithAttr: + { + static const char *labels[5] = + { + "blue", "green", "red", "bold cyan", "default", + }; + + variant = labels[row % 5]; + } + break; + + case Col_Custom: + variant = wxString::Format("%d", row % 100); + break; + + case Col_Max: + wxFAIL_MSG( "invalid column" ); } } bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col, - wxDataViewItemAttr &attr ) + wxDataViewItemAttr &attr ) const { - if (col != 2) - return false; - - if (row < m_array.GetCount()) + switch ( col ) { - attr.SetColour( (row%2) == 0 ? *wxBLUE : - ((row%3) == 0 ? *wxGREEN : *wxRED) ); - attr.SetItalic( (row%2) == 5 ); + case Col_EditableText: + return false; + + case Col_IconText: + if ( !(row % 2) ) + return false; + attr.SetColour(*wxLIGHT_GREY); + break; + + case Col_TextWithAttr: + case Col_Custom: + // do what the labels defined in GetValueByRow() hint at + switch ( row % 5 ) + { + case 0: + attr.SetColour(*wxBLUE); + break; + + case 1: + attr.SetColour(*wxGREEN); + break; + + case 2: + attr.SetColour(*wxRED); + break; + + case 3: + attr.SetColour(*wxCYAN); + attr.SetBold(true); + break; + + case 4: + return false; + } + break; + + case Col_Max: + wxFAIL_MSG( "invalid column" ); } return true; @@ -429,19 +500,50 @@ bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col, bool MyListModel::SetValueByRow( const wxVariant &variant, unsigned int row, unsigned int col ) { - if (col == 0) + switch ( col ) { - if (row >= m_array.GetCount()) - { - // the item is not in the range of the items - // which we store... for simplicity, don't allow editing it - wxLogError("Cannot edit rows with an index greater than %d", m_array.GetCount()); - return false; - } + case Col_EditableText: + case Col_IconText: + if (row >= m_textColValues.GetCount()) + { + // the item is not in the range of the items + // which we store... for simplicity, don't allow editing it + wxLogError( "Cannot edit rows with an index greater than %d", + m_textColValues.GetCount() ); + return false; + } + + if ( col == Col_EditableText ) + { + m_textColValues[row] = variant.GetString(); + } + else // col == Col_IconText + { + wxDataViewIconText iconText; + iconText << variant; + m_iconColValues[row] = iconText.GetText(); + } + return true; - m_array[row] = variant.GetString(); - return true; + case Col_TextWithAttr: + case Col_Custom: + wxLogError("Cannot edit the column %d", col); + break; + + case Col_Max: + wxFAIL_MSG( "invalid column" ); } return false; } + + +// ---------------------------------------------------------------------------- +// MyListStoreDerivedModel +// ---------------------------------------------------------------------------- + +bool MyListStoreDerivedModel::IsEnabledByRow(unsigned int row, unsigned int col) const +{ + // disabled the last two checkboxes + return !(col == 0 && 8 <= row && row <= 9); +}