+ virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
+ {
+ wxSpinCtrl *sc = (wxSpinCtrl*) editor;
+ long l = sc->GetValue();
+ value = l;
+ return true;
+ }
+
+ bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) )
+ {
+ wxString str;
+ str.Printf( wxT("%d"), (int) m_data );
+ dc->SetTextForeground( *wxBLACK );
+ dc->DrawText( str, rect.x, rect.y );
+ return true;
+ }
+ wxSize GetSize() const
+ {
+ return wxSize(80,16);
+ }
+ bool SetValue( const wxVariant &value )
+ {
+ m_data = value.GetLong();
+ return true;
+ }
+ bool GetValue( wxVariant &value ) const
+ {
+ value = m_data;
+ return true;
+ }
+
+private:
+ long m_data;
+};
+
+
+
+// -------------------------------------
+// MyMusicModel
+// -------------------------------------
+
+/*
+Implement this data model
+ Title Artist Year
+-------------------------------------------------------------
+1: My Music:
+ 2: Pop music
+ 3: You are not alone Michael Jackson 1995
+ 4: Take a bow Madonna 1994
+ 5: Classical music
+ 6: Ninth Symphony Ludwig v. Beethoven 1824
+ 7: German Requiem Johannes Brahms 1868
+*/
+
+
+
+class MyMusicModelNode;
+WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
+
+class MyMusicModelNode
+{
+public:
+ MyMusicModelNode( MyMusicModelNode* parent,
+ const wxString &title, const wxString &artist, int year )
+ {
+ m_parent = parent;
+ m_title = title;
+ m_artist = artist;
+ m_year = year;
+ m_isContainer = false;
+ }
+
+ MyMusicModelNode( MyMusicModelNode* parent,
+ const wxString &branch )
+ {
+ m_parent = parent;
+ m_title = branch;
+ m_year = -1;
+ m_isContainer = true;
+ }
+
+ ~MyMusicModelNode()
+ {
+ size_t count = m_children.GetCount();
+ size_t i;
+ for (i = 0; i < count; i++)
+ {
+ MyMusicModelNode *child = m_children[i];
+ delete child;
+ }
+ }
+
+ bool IsContainer() { return m_isContainer; }
+
+ MyMusicModelNode* GetParent() { return m_parent; }
+ MyMusicModelNodes &GetChildren() { return m_children; }
+ MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); }
+ void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); }
+ void Append( MyMusicModelNode* child ) { m_children.Add( child ); }
+ unsigned int GetChildCount() { return m_children.GetCount(); }
+
+public:
+ wxString m_title;
+ wxString m_artist;
+ int m_year;
+
+private:
+ MyMusicModelNode *m_parent;
+ MyMusicModelNodes m_children;
+ bool m_isContainer;
+};
+
+
+class MyMusicModel: public wxDataViewModel
+{
+public:
+
+ // constructor
+
+ MyMusicModel()
+ {
+ m_root = new MyMusicModelNode( NULL, "My Music" );
+ m_pop = new MyMusicModelNode( m_root, "Pop music" );
+ m_root->Append( m_pop );
+ m_pop->Append( new MyMusicModelNode( m_pop,
+ "You are not alone", "Michael Jackson", 1995 ) );
+ m_pop->Append( new MyMusicModelNode( m_pop,
+ "Take a bow", "Madonna", 1994 ) );
+ m_classical = new MyMusicModelNode( m_root, "Classical music" );
+ m_root->Append( m_classical );
+ m_classical->Append( new MyMusicModelNode( m_classical,
+ "Ninth symphony", "Ludwig van Beethoven", 1824 ) );
+ m_classical->Append( new MyMusicModelNode( m_classical,
+ "German Requiem", "Johannes Brahms", 1868 ) );
+ m_classicalMusicIsKnownToControl = false;
+ }
+
+ // helper method for wxLog
+
+ wxString GetTitle( const wxDataViewItem &item )
+ {
+ MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
+ if (!node)
+ return wxEmptyString;
+
+ return node->m_title;
+ }
+
+ // 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
+ unsigned long litem1 = (unsigned long) item1.GetID();
+ unsigned long litem2 = (unsigned long) item2.GetID();
+
+ return litem1-litem2;
+ }
+
+ return wxDataViewModel::Compare( item1, item2, column, ascending );
+ }
+
+ // implementation of base class virtuals to define model
+
+ virtual unsigned int GetColumnCount() const
+ {
+ return 3;
+ }
+
+ virtual wxString GetColumnType( unsigned int col ) const
+ {
+ if (col == 2)
+ return "long";
+
+ return "string";
+ }
+
+ virtual void GetValue( wxVariant &variant,
+ const wxDataViewItem &item, unsigned int col ) const
+ {
+ MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
+ switch (col)
+ {
+ case 0: variant = node->m_title; break;
+ case 1: variant = node->m_artist; break;
+ case 2: variant = (long) node->m_year; break;
+ default:
+ {
+ wxLogError( "MyMusicModel::GetValue: wrong column" );
+
+ // provoke a crash when mouse button down
+ wxMouseState state = wxGetMouseState();
+ if (state.ShiftDown())
+ {
+ char *crash = 0;
+ *crash = 0;
+ }
+ }
+ }
+ }
+
+ virtual bool SetValue( const wxVariant &variant,
+ const wxDataViewItem &item, unsigned int col )
+ {
+ MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
+ switch (col)
+ {
+ case 0: node->m_title = variant.GetString(); return true;
+ case 1: node->m_artist = variant.GetString(); return true;
+ case 2: node->m_year = variant.GetLong(); return true;
+ default: wxLogError( "MyMusicModel::SetValue: wrong column" );
+ }
+ return false;
+ }
+
+ virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const
+ {
+ // the invisble root node has no parent
+ if (!item.IsOk())
+ return wxDataViewItem(0);
+
+ MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
+
+ // "MyMusic" also has no parent
+ if (node == m_root)
+ return wxDataViewItem(0);
+
+ return wxDataViewItem( (void*) node->GetParent() );
+ }
+
+ virtual bool IsContainer( const wxDataViewItem &item ) const
+ {
+ // the invisble root node can have children (in
+ // our model always "MyMusic")
+ if (!item.IsOk())
+ return true;
+
+ MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
+ return node->IsContainer();
+ }
+
+ virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const
+ {
+ MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
+ if (!node)
+ {
+ array.Add( wxDataViewItem( (void*) m_root ) );
+ return 1;
+ }
+
+ if (node == m_classical)
+ {
+ MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
+ model->m_classicalMusicIsKnownToControl = true;
+ }
+
+ if (node->GetChildCount() == 0)
+ {
+ return 0;
+ }
+
+ unsigned int count = node->GetChildren().GetCount();
+ unsigned int pos;
+ for (pos = 0; pos < count; pos++)
+ {
+ MyMusicModelNode *child = node->GetChildren().Item( pos );
+ array.Add( wxDataViewItem( (void*) child ) );
+ }
+ return count;
+ }
+
+private:
+ MyMusicModelNode* m_root;
+ MyMusicModelNode* m_pop;
+ MyMusicModelNode* m_classical;
+ bool m_classicalMusicIsKnownToControl;
+};
+
+class MyListModel: public wxDataViewIndexListModel
+{
+public:
+ MyListModel() :
+ wxDataViewIndexListModel( 100 )
+ {
+ unsigned int i;
+ for (i = 0; i < 100; i++)
+ {
+ wxString str;
+ str.Printf( "row number %d", i );
+ m_array.Add( str );
+ }
+
+ m_icon = wxIcon( null_xpm );
+ }
+
+ // helper methods to change the model
+
+ void Prepend( const wxString &text )
+ {
+ m_array.Insert( text, 0 );
+ RowPrepended();
+ }
+
+ void DeleteItem( const wxDataViewItem &item )
+ {
+ unsigned int row = GetRow( item );
+ m_array.RemoveAt( row );
+ RowDeleted( row );
+ }
+
+ // implementation of base class virtuals to define model
+
+ virtual unsigned int GetColumnCount() const
+ {
+ return 3;
+ }
+
+ virtual wxString GetColumnType( unsigned int col ) const
+ {
+ if (col == 1)
+ return "wxDataViewIconText";
+
+ return "string";
+ }
+
+ virtual unsigned int GetRowCount()
+ {
+ return m_array.GetCount();
+ }
+
+ virtual void GetValue( wxVariant &variant,
+ unsigned int row, unsigned int col ) const
+ {
+ if (col==0)
+ {
+ variant = m_array[ row ];
+ } else
+ if (col==1)
+ {
+ wxDataViewIconText data( "test", m_icon );
+ variant << data;
+ }
+ else
+ {
+ wxString str;
+ str.Printf( "row %d col %d", row, col );
+ variant = str;
+ }
+ }
+
+ virtual bool SetValue( const wxVariant &variant,
+ unsigned int row, unsigned int col )
+ {
+ if (col == 0)
+ {
+ m_array[row] = variant.GetString();
+ return true;
+ }
+
+ return false;
+ }
+
+ wxArrayString m_array;
+ wxIcon m_icon;