Test custom editor in the dataview sample

Show how to create and use a custom editor in the custom renderer.
This commit is contained in:
Vadim Zeitlin
2015-11-27 15:43:21 +01:00
parent 29024e39ca
commit 7e37c6763f
3 changed files with 52 additions and 8 deletions

View File

@@ -173,10 +173,12 @@ private:
class MyCustomRenderer: public wxDataViewCustomRenderer class MyCustomRenderer: public wxDataViewCustomRenderer
{ {
public: public:
MyCustomRenderer() // This renderer can be either activatable or editable, for demonstration
: wxDataViewCustomRenderer("string", // purposes. In real programs, you should select whether the user should be
wxDATAVIEW_CELL_ACTIVATABLE, // able to activate or edit the cell and it doesn't make sense to switch
wxALIGN_CENTER) // 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 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 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: private:
wxString m_value; wxString m_value;
}; };
@@ -614,7 +644,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
// column 5 of the view control: // column 5 of the view control:
MyCustomRenderer *cr = new MyCustomRenderer; MyCustomRenderer *cr = new MyCustomRenderer(wxDATAVIEW_CELL_ACTIVATABLE);
wxDataViewColumn *column5 = wxDataViewColumn *column5 =
new wxDataViewColumn( "custom", cr, 5, -1, wxALIGN_LEFT, new wxDataViewColumn( "custom", cr, 5, -1, wxALIGN_LEFT,
wxDATAVIEW_COL_RESIZABLE ); wxDATAVIEW_COL_RESIZABLE );
@@ -662,7 +692,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
m_ctrl[1]->AppendColumn( m_ctrl[1]->AppendColumn(
new wxDataViewColumn("custom renderer", new wxDataViewColumn("custom renderer",
new MyCustomRenderer, new MyCustomRenderer(wxDATAVIEW_CELL_EDITABLE),
MyListModel::Col_Custom) MyListModel::Col_Custom)
); );
} }

View File

@@ -444,7 +444,13 @@ void MyListModel::GetValueByRow( wxVariant &variant,
break; break;
case Col_Custom: 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; break;
case Col_Max: case Col_Max:
@@ -531,10 +537,13 @@ bool MyListModel::SetValueByRow( const wxVariant &variant,
case Col_Date: case Col_Date:
case Col_TextWithAttr: case Col_TextWithAttr:
case Col_Custom:
wxLogError("Cannot edit the column %d", col); wxLogError("Cannot edit the column %d", col);
break; break;
case Col_Custom:
m_customColValues[row] = variant.GetString();
break;
case Col_Max: case Col_Max:
wxFAIL_MSG( "invalid column" ); wxFAIL_MSG( "invalid column" );
} }

View File

@@ -8,6 +8,10 @@
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#include "wx/hashmap.h"
WX_DECLARE_HASH_MAP(unsigned, wxString, wxIntegerHash, wxIntegerEqual,
IntToStringMap);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyMusicTreeModelNode: a node inside MyMusicTreeModel // MyMusicTreeModelNode: a node inside MyMusicTreeModel
@@ -235,6 +239,7 @@ public:
private: private:
wxArrayString m_textColValues; wxArrayString m_textColValues;
wxArrayString m_iconColValues; wxArrayString m_iconColValues;
IntToStringMap m_customColValues;
wxIcon m_icon[2]; wxIcon m_icon[2];
}; };