Reduce code duplication in wxGTK wxDataViewRenderer classes
The code was more complicated than necessary, with the base class providing both virtual GtkOnCellChanged() and GtkOnTextEdited() that were both overridden to achieve the same thing, namely customizing how the value entered by user is converted to wxVariant, in different derived classes. Make GtkOnTextEdited() non-virtual and remove GtkOnCellChanged() completely and add a new simple GtkGetValueFromString() which is called from GtkOnTextEdited() to do the conversion. This removes the existing code duplication and will make it simpler to modify this code in the future, without changing the behaviour.
This commit is contained in:
@@ -47,9 +47,10 @@ public:
|
|||||||
|
|
||||||
// called when the cell value was edited by user with the new value
|
// called when the cell value was edited by user with the new value
|
||||||
//
|
//
|
||||||
// it validates the new value and notifies the model about the change by
|
// it uses GtkGetValueFromString() to parse the new value, then validates
|
||||||
// calling GtkOnCellChanged() if it was accepted
|
// it by calling Validate() and notifies the model about the change if it
|
||||||
virtual void GtkOnTextEdited(const char *itempath, const wxString& value);
|
// passes validation
|
||||||
|
void GtkOnTextEdited(const char *itempath, const wxString& value);
|
||||||
|
|
||||||
GtkCellRenderer* GetGtkHandle() { return m_renderer; }
|
GtkCellRenderer* GetGtkHandle() { return m_renderer; }
|
||||||
void GtkInitHandlers();
|
void GtkInitHandlers();
|
||||||
@@ -78,14 +79,16 @@ protected:
|
|||||||
|
|
||||||
virtual bool IsHighlighted() const wxOVERRIDE;
|
virtual bool IsHighlighted() const wxOVERRIDE;
|
||||||
|
|
||||||
virtual void GtkOnCellChanged(const wxVariant& value,
|
|
||||||
const wxDataViewItem& item,
|
|
||||||
unsigned col);
|
|
||||||
|
|
||||||
// Apply our effective alignment (i.e. m_alignment if specified or the
|
// Apply our effective alignment (i.e. m_alignment if specified or the
|
||||||
// associated column alignment by default) to the given renderer.
|
// associated column alignment by default) to the given renderer.
|
||||||
void GtkApplyAlignment(GtkCellRenderer *renderer);
|
void GtkApplyAlignment(GtkCellRenderer *renderer);
|
||||||
|
|
||||||
|
// This method is used to interpret the string entered by user and by
|
||||||
|
// default just uses it as is, but can be overridden for classes requiring
|
||||||
|
// special treatment.
|
||||||
|
virtual wxVariant GtkGetValueFromString(const wxString& str) const;
|
||||||
|
|
||||||
|
|
||||||
GtkCellRenderer *m_renderer;
|
GtkCellRenderer *m_renderer;
|
||||||
int m_alignment;
|
int m_alignment;
|
||||||
|
|
||||||
|
@@ -228,9 +228,7 @@ public:
|
|||||||
virtual void GtkPackIntoColumn(GtkTreeViewColumn *column) wxOVERRIDE;
|
virtual void GtkPackIntoColumn(GtkTreeViewColumn *column) wxOVERRIDE;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void GtkOnCellChanged(const wxVariant& value,
|
virtual wxVariant GtkGetValueFromString(const wxString& str) const wxOVERRIDE;
|
||||||
const wxDataViewItem& item,
|
|
||||||
unsigned col) wxOVERRIDE;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxDataViewIconText m_value;
|
wxDataViewIconText m_value;
|
||||||
@@ -281,7 +279,7 @@ public:
|
|||||||
virtual bool GetValue( wxVariant &value ) const wxOVERRIDE;
|
virtual bool GetValue( wxVariant &value ) const wxOVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void GtkOnTextEdited(const char *itempath, const wxString& str) wxOVERRIDE;
|
virtual wxVariant GtkGetValueFromString(const wxString& str) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@@ -2155,26 +2155,24 @@ bool wxDataViewRenderer::IsHighlighted() const
|
|||||||
GetOwner()->GetOwner()->IsSelected(m_itemBeingRendered);
|
GetOwner()->GetOwner()->IsSelected(m_itemBeingRendered);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxVariant
|
||||||
|
wxDataViewRenderer::GtkGetValueFromString(const wxString& str) const
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
wxDataViewRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
|
wxDataViewRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
|
||||||
{
|
{
|
||||||
wxVariant value(str);
|
wxVariant value(GtkGetValueFromString(str));
|
||||||
if (!Validate( value ))
|
if (!Validate( value ))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
wxDataViewItem
|
wxDataViewItem
|
||||||
item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
|
item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
|
||||||
|
|
||||||
GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value,
|
|
||||||
const wxDataViewItem& item,
|
|
||||||
unsigned col)
|
|
||||||
{
|
|
||||||
wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
|
wxDataViewModel *model = GetOwner()->GetOwner()->GetModel();
|
||||||
model->ChangeValue( value, item, col );
|
model->ChangeValue( value, item, GetOwner()->GetModelColumn() );
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxDataViewRenderer::SetAttr(const wxDataViewItemAttr& WXUNUSED(attr))
|
void wxDataViewRenderer::SetAttr(const wxDataViewItemAttr& WXUNUSED(attr))
|
||||||
@@ -2941,17 +2939,10 @@ wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayS
|
|||||||
m_variantType = wxS("long");
|
m_variantType = wxS("long");
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxDataViewChoiceByIndexRenderer::GtkOnTextEdited(const char *itempath, const wxString& str)
|
wxVariant
|
||||||
|
wxDataViewChoiceByIndexRenderer::GtkGetValueFromString(const wxString& str) const
|
||||||
{
|
{
|
||||||
wxVariant value( (long) GetChoices().Index( str ) );
|
return static_cast<long>(GetChoices().Index(str));
|
||||||
|
|
||||||
if (!Validate( value ))
|
|
||||||
return;
|
|
||||||
|
|
||||||
wxDataViewItem
|
|
||||||
item(GetOwner()->GetOwner()->GTKPathToItem(wxGtkTreePath(itempath)));
|
|
||||||
|
|
||||||
GtkOnCellChanged(value, item, GetOwner()->GetModelColumn());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
|
bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
|
||||||
@@ -3024,17 +3015,15 @@ bool wxDataViewIconTextRenderer::GetValue(wxVariant& value) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
wxVariant
|
||||||
wxDataViewIconTextRenderer::GtkOnCellChanged(const wxVariant& value,
|
wxDataViewIconTextRenderer::GtkGetValueFromString(const wxString& str) const
|
||||||
const wxDataViewItem& item,
|
|
||||||
unsigned col)
|
|
||||||
{
|
{
|
||||||
// we receive just the text part of our value as it's the only one which
|
// we receive just the text part of our value as it's the only one which
|
||||||
// can be edited, but we need the full wxDataViewIconText value for the
|
// can be edited, but we need the full wxDataViewIconText value for the
|
||||||
// model
|
// model
|
||||||
wxVariant valueIconText;
|
wxVariant valueIconText;
|
||||||
valueIconText << wxDataViewIconText(value.GetString(), m_value.GetIcon());
|
valueIconText << wxDataViewIconText(str, m_value.GetIcon());
|
||||||
wxDataViewTextRenderer::GtkOnCellChanged(valueIconText, item, col);
|
return valueIconText;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user