From a6be5bdae3a05992bb6195eead5e7594ce64c871 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= <vaclav@slavik.io>
Date: Fri, 21 Oct 2016 17:50:34 +0200
Subject: [PATCH] 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.
---
 include/wx/dvrenderers.h     |  4 ++++
 include/wx/gtk/dvrenderer.h  |  2 +-
 include/wx/gtk/dvrenderers.h |  6 ++++--
 src/common/datavcmn.cpp      | 17 +++++++++++++---
 src/gtk/dataview.cpp         | 39 +++++++++++++++++++++---------------
 5 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/include/wx/dvrenderers.h b/include/wx/dvrenderers.h
index f65adedc9b..8d4fe361b5 100644
--- a/include/wx/dvrenderers.h
+++ b/include/wx/dvrenderers.h
@@ -180,6 +180,10 @@ public:
     // wxDVR_DEFAULT_ALIGNMENT.
     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.
     void NotifyEditingStarted(const wxDataViewItem& item);
 
diff --git a/include/wx/gtk/dvrenderer.h b/include/wx/gtk/dvrenderer.h
index c5d3d74a4e..a99be60bcf 100644
--- a/include/wx/gtk/dvrenderer.h
+++ b/include/wx/gtk/dvrenderer.h
@@ -53,7 +53,7 @@ public:
 
     GtkCellRenderer* GetGtkHandle() { return m_renderer; }
     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
     // specific attributes: can return NULL if this renderer doesn't render any
diff --git a/include/wx/gtk/dvrenderers.h b/include/wx/gtk/dvrenderers.h
index 3305203057..9169d09761 100644
--- a/include/wx/gtk/dvrenderers.h
+++ b/include/wx/gtk/dvrenderers.h
@@ -51,7 +51,7 @@ public:
         return true;
     }
 
-    virtual void SetAlignment( int align ) wxOVERRIDE;
+    virtual void GtkUpdateAlignment() wxOVERRIDE;
 
     virtual GtkCellRendererText *GtkGetTextRenderer() const wxOVERRIDE;
 
@@ -151,6 +151,8 @@ public:
     virtual GtkCellRendererText *GtkGetTextRenderer() const wxOVERRIDE;
     virtual GtkWidget* GtkGetEditorWidget() const wxOVERRIDE;
 
+    virtual void GtkUpdateAlignment() wxOVERRIDE;
+
 private:
     bool Init(wxDataViewCellMode mode, int align);
 
@@ -254,7 +256,7 @@ public:
     virtual bool SetValue( const wxVariant &value ) 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]; }
     const wxArrayString& GetChoices() const { return m_choices; }
diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp
index ff22ecc3d7..ebf02dc967 100644
--- a/src/common/datavcmn.cpp
+++ b/src/common/datavcmn.cpp
@@ -872,14 +872,25 @@ wxDataViewRendererBase::PrepareForItem(const wxDataViewModel *model,
 
 
 int wxDataViewRendererBase::GetEffectiveAlignment() const
+{
+    int alignment = GetEffectiveAlignmentIfKnown();
+    wxASSERT( alignment != wxDVR_DEFAULT_ALIGNMENT );
+    return alignment;
+}
+
+
+int wxDataViewRendererBase::GetEffectiveAlignmentIfKnown() const
 {
     int alignment = GetAlignment();
 
     if ( alignment == wxDVR_DEFAULT_ALIGNMENT )
     {
-        // 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;
+        if ( GetOwner() != NULL )
+        {
+            // 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;
diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp
index 2646fb7d7d..624fb7a286 100644
--- a/src/gtk/dataview.cpp
+++ b/src/gtk/dataview.cpp
@@ -2056,18 +2056,9 @@ wxDataViewCellMode wxDataViewRenderer::GetMode() const
 
 void wxDataViewRenderer::GtkApplyAlignment(GtkCellRenderer *renderer)
 {
-    int align = m_alignment;
-
-    // query alignment from column ?
-    if (align == -1)
-    {
-        // None there yet
-        if (GetOwner() == NULL)
-            return;
-
-        align = GetOwner()->GetAlignment();
-        align |= wxALIGN_CENTRE_VERTICAL;
-    }
+    int align = GetEffectiveAlignmentIfKnown();
+    if ( align == wxDVR_DEFAULT_ALIGNMENT )
+        return; // none set yet
 
     // horizontal alignment:
 
@@ -2341,15 +2332,19 @@ bool wxDataViewTextRenderer::GetTextValue(wxString& str) const
     return true;
 }
 
-void wxDataViewTextRenderer::SetAlignment( int align )
+void wxDataViewTextRenderer::GtkUpdateAlignment()
 {
-    wxDataViewRenderer::SetAlignment(align);
+    wxDataViewRenderer::GtkUpdateAlignment();
 
 #ifndef __WXGTK3__
     if (gtk_check_version(2,10,0))
         return;
 #endif
 
+    int align = GetEffectiveAlignmentIfKnown();
+    if ( align == wxDVR_DEFAULT_ALIGNMENT )
+        return; // none set yet
+
     // horizontal alignment:
     PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
     if (align & wxALIGN_RIGHT)
@@ -2587,6 +2582,14 @@ wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
         Init(mode, align);
 }
 
+void wxDataViewCustomRenderer::GtkUpdateAlignment()
+{
+    wxDataViewCustomRendererBase::GtkUpdateAlignment();
+
+    if ( m_text_renderer )
+        GtkApplyAlignment(GTK_CELL_RENDERER(m_text_renderer));
+}
+
 void wxDataViewCustomRenderer::GtkInitTextRenderer()
 {
     m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
@@ -2868,15 +2871,19 @@ bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
     return true;
 }
 
-void wxDataViewChoiceRenderer::SetAlignment( int align )
+void wxDataViewChoiceRenderer::GtkUpdateAlignment()
 {
-    wxDataViewCustomRenderer::SetAlignment(align);
+    wxDataViewCustomRenderer::GtkUpdateAlignment();
 
 #ifndef __WXGTK3__
     if (gtk_check_version(2,10,0))
         return;
 #endif
 
+    int align = GetEffectiveAlignmentIfKnown();
+    if ( align == wxDVR_DEFAULT_ALIGNMENT )
+        return; // none set yet
+
     // horizontal alignment:
     PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
     if (align & wxALIGN_RIGHT)
