diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 92d57e45db..70a543f6a9 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -173,10 +173,12 @@ private: class MyCustomRenderer: public wxDataViewCustomRenderer { public: - MyCustomRenderer() - : wxDataViewCustomRenderer("string", - wxDATAVIEW_CELL_ACTIVATABLE, - wxALIGN_CENTER) + // This renderer can be either activatable or editable, for demonstration + // purposes. In real programs, you should select whether the user should be + // able to activate or edit the cell and it doesn't make sense to switch + // between the two -- but this is just an example, so it doesn't stop us. + explicit MyCustomRenderer(wxDataViewCellMode mode) + : wxDataViewCustomRenderer("string", mode, wxALIGN_CENTER) { } virtual bool Render( wxRect rect, wxDC *dc, int state ) wxOVERRIDE @@ -223,6 +225,34 @@ public: virtual bool GetValue( wxVariant &WXUNUSED(value) ) const wxOVERRIDE { return true; } + virtual bool HasEditorCtrl() const wxOVERRIDE { return true; } + + virtual wxWindow* + CreateEditorCtrl(wxWindow* parent, + wxRect labelRect, + const wxVariant& value) wxOVERRIDE + { + wxTextCtrl* text = new wxTextCtrl(parent, wxID_ANY, value, + labelRect.GetPosition(), + labelRect.GetSize(), + wxTE_PROCESS_ENTER); + text->SetInsertionPointEnd(); + + return text; + } + + virtual bool + GetValueFromEditorCtrl(wxWindow* ctrl, wxVariant& value) wxOVERRIDE + { + wxTextCtrl* text = wxDynamicCast(ctrl, wxTextCtrl); + if ( !text ) + return false; + + value = text->GetValue(); + + return true; + } + private: wxString m_value; }; @@ -614,7 +644,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l // column 5 of the view control: - MyCustomRenderer *cr = new MyCustomRenderer; + MyCustomRenderer *cr = new MyCustomRenderer(wxDATAVIEW_CELL_ACTIVATABLE); wxDataViewColumn *column5 = new wxDataViewColumn( "custom", cr, 5, -1, wxALIGN_LEFT, wxDATAVIEW_COL_RESIZABLE ); @@ -662,7 +692,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l m_ctrl[1]->AppendColumn( new wxDataViewColumn("custom renderer", - new MyCustomRenderer, + new MyCustomRenderer(wxDATAVIEW_CELL_EDITABLE), MyListModel::Col_Custom) ); } diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index c18f9000b9..590d3f2e67 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -444,7 +444,13 @@ void MyListModel::GetValueByRow( wxVariant &variant, break; case Col_Custom: - variant = wxString::Format("%d", row % 100); + { + IntToStringMap::const_iterator it = m_customColValues.find(row); + if ( it != m_customColValues.end() ) + variant = it->second; + else + variant = wxString::Format("%d", row % 100); + } break; case Col_Max: @@ -531,10 +537,13 @@ bool MyListModel::SetValueByRow( const wxVariant &variant, case Col_Date: case Col_TextWithAttr: - case Col_Custom: wxLogError("Cannot edit the column %d", col); break; + case Col_Custom: + m_customColValues[row] = variant.GetString(); + break; + case Col_Max: wxFAIL_MSG( "invalid column" ); } diff --git a/samples/dataview/mymodels.h b/samples/dataview/mymodels.h index 96921c1724..4c7d1a3a10 100644 --- a/samples/dataview/mymodels.h +++ b/samples/dataview/mymodels.h @@ -8,6 +8,10 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +#include "wx/hashmap.h" + +WX_DECLARE_HASH_MAP(unsigned, wxString, wxIntegerHash, wxIntegerEqual, + IntToStringMap); // ---------------------------------------------------------------------------- // MyMusicTreeModelNode: a node inside MyMusicTreeModel @@ -235,6 +239,7 @@ public: private: wxArrayString m_textColValues; wxArrayString m_iconColValues; + IntToStringMap m_customColValues; wxIcon m_icon[2]; };