Fix wxGTK wxDataViewRenderers' alignment handling

Don't apply alignment to native controls in SetAlignment() method, where
it may not be known yet due to column-to-renderer inheritance if
wxDVR_DEFAULT_ALIGNMENT is used (the default). Move such code to
GtkUpdateAlignment() (which was made virtual) in all renderers.

This fixes unintended right-aligning of columns with GTK+ 2 when default
alignment was used.
This commit is contained in:
Václav Slavík
2016-10-21 17:50:34 +02:00
parent 552940b547
commit a6be5bdae3
5 changed files with 46 additions and 22 deletions

View File

@@ -180,6 +180,10 @@ public:
// wxDVR_DEFAULT_ALIGNMENT. // wxDVR_DEFAULT_ALIGNMENT.
int GetEffectiveAlignment() const; int GetEffectiveAlignment() const;
// Like GetEffectiveAlignment(), but returns wxDVR_DEFAULT_ALIGNMENT if
// the owner isn't set and GetAlignment() is default.
int GetEffectiveAlignmentIfKnown() const;
// Send wxEVT_DATAVIEW_ITEM_EDITING_STARTED event. // Send wxEVT_DATAVIEW_ITEM_EDITING_STARTED event.
void NotifyEditingStarted(const wxDataViewItem& item); void NotifyEditingStarted(const wxDataViewItem& item);

View File

@@ -53,7 +53,7 @@ public:
GtkCellRenderer* GetGtkHandle() { return m_renderer; } GtkCellRenderer* GetGtkHandle() { return m_renderer; }
void GtkInitHandlers(); void GtkInitHandlers();
void GtkUpdateAlignment() { GtkApplyAlignment(m_renderer); } virtual void GtkUpdateAlignment() { GtkApplyAlignment(m_renderer); }
// return the text renderer used by this renderer for setting text cell // return the text renderer used by this renderer for setting text cell
// specific attributes: can return NULL if this renderer doesn't render any // specific attributes: can return NULL if this renderer doesn't render any

View File

@@ -51,7 +51,7 @@ public:
return true; return true;
} }
virtual void SetAlignment( int align ) wxOVERRIDE; virtual void GtkUpdateAlignment() wxOVERRIDE;
virtual GtkCellRendererText *GtkGetTextRenderer() const wxOVERRIDE; virtual GtkCellRendererText *GtkGetTextRenderer() const wxOVERRIDE;
@@ -151,6 +151,8 @@ public:
virtual GtkCellRendererText *GtkGetTextRenderer() const wxOVERRIDE; virtual GtkCellRendererText *GtkGetTextRenderer() const wxOVERRIDE;
virtual GtkWidget* GtkGetEditorWidget() const wxOVERRIDE; virtual GtkWidget* GtkGetEditorWidget() const wxOVERRIDE;
virtual void GtkUpdateAlignment() wxOVERRIDE;
private: private:
bool Init(wxDataViewCellMode mode, int align); bool Init(wxDataViewCellMode mode, int align);
@@ -254,7 +256,7 @@ public:
virtual bool SetValue( const wxVariant &value ) wxOVERRIDE; virtual bool SetValue( const wxVariant &value ) wxOVERRIDE;
virtual bool GetValue( wxVariant &value ) const wxOVERRIDE; virtual bool GetValue( wxVariant &value ) const wxOVERRIDE;
void SetAlignment( int align ) wxOVERRIDE; virtual void GtkUpdateAlignment() wxOVERRIDE;
wxString GetChoice(size_t index) const { return m_choices[index]; } wxString GetChoice(size_t index) const { return m_choices[index]; }
const wxArrayString& GetChoices() const { return m_choices; } const wxArrayString& GetChoices() const { return m_choices; }

View File

@@ -872,14 +872,25 @@ wxDataViewRendererBase::PrepareForItem(const wxDataViewModel *model,
int wxDataViewRendererBase::GetEffectiveAlignment() const int wxDataViewRendererBase::GetEffectiveAlignment() const
{
int alignment = GetEffectiveAlignmentIfKnown();
wxASSERT( alignment != wxDVR_DEFAULT_ALIGNMENT );
return alignment;
}
int wxDataViewRendererBase::GetEffectiveAlignmentIfKnown() const
{ {
int alignment = GetAlignment(); int alignment = GetAlignment();
if ( alignment == wxDVR_DEFAULT_ALIGNMENT ) if ( alignment == wxDVR_DEFAULT_ALIGNMENT )
{ {
// if we don't have an explicit alignment ourselves, use that of the if ( GetOwner() != NULL )
// column in horizontal direction and default vertical alignment {
alignment = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL; // if we don't have an explicit alignment ourselves, use that of the
// column in horizontal direction and default vertical alignment
alignment = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL;
}
} }
return alignment; return alignment;

View File

@@ -2056,18 +2056,9 @@ wxDataViewCellMode wxDataViewRenderer::GetMode() const
void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer *renderer) void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer *renderer)
{ {
int align = m_alignment; int align = GetEffectiveAlignmentIfKnown();
if ( align == wxDVR_DEFAULT_ALIGNMENT )
// query alignment from column ? return; // none set yet
if (align == -1)
{
// None there yet
if (GetOwner() == NULL)
return;
align = GetOwner()->GetAlignment();
align |= wxALIGN_CENTRE_VERTICAL;
}
// horizontal alignment: // horizontal alignment:
@@ -2341,15 +2332,19 @@ bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
return true; return true;
} }
void wxDataViewTextRenderer::SetAlignment( int align ) void wxDataViewTextRenderer::GtkUpdateAlignment()
{ {
wxDataViewRenderer::SetAlignment(align); wxDataViewRenderer::GtkUpdateAlignment();
#ifndef __WXGTK3__ #ifndef __WXGTK3__
if (gtk_check_version(2,10,0)) if (gtk_check_version(2,10,0))
return; return;
#endif #endif
int align = GetEffectiveAlignmentIfKnown();
if ( align == wxDVR_DEFAULT_ALIGNMENT )
return; // none set yet
// horizontal alignment: // horizontal alignment:
PangoAlignment pangoAlign = PANGO_ALIGN_LEFT; PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
if (align & wxALIGN_RIGHT) if (align & wxALIGN_RIGHT)
@@ -2587,6 +2582,14 @@ wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
Init(mode, align); Init(mode, align);
} }
void wxDataViewCustomRenderer::GtkUpdateAlignment()
{
wxDataViewCustomRendererBase::GtkUpdateAlignment();
if ( m_text_renderer )
GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer));
}
void wxDataViewCustomRenderer::GtkInitTextRenderer() void wxDataViewCustomRenderer::GtkInitTextRenderer()
{ {
m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new()); m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
@@ -2868,15 +2871,19 @@ bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
return true; return true;
} }
void wxDataViewChoiceRenderer::SetAlignment( int align ) void wxDataViewChoiceRenderer::GtkUpdateAlignment()
{ {
wxDataViewCustomRenderer::SetAlignment(align); wxDataViewCustomRenderer::GtkUpdateAlignment();
#ifndef __WXGTK3__ #ifndef __WXGTK3__
if (gtk_check_version(2,10,0)) if (gtk_check_version(2,10,0))
return; return;
#endif #endif
int align = GetEffectiveAlignmentIfKnown();
if ( align == wxDVR_DEFAULT_ALIGNMENT )
return; // none set yet
// horizontal alignment: // horizontal alignment:
PangoAlignment pangoAlign = PANGO_ALIGN_LEFT; PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
if (align & wxALIGN_RIGHT) if (align & wxALIGN_RIGHT)