[gtk+/wip/baseline2] Fix indicator position for baseline aligned radio/check buttons



commit 3d92798c964ff8661f52aecc279b44cf35dd5cc1
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Mar 26 10:31:13 2013 +0100

    Fix indicator position for baseline aligned radio/check buttons
    
    Rather than just always centering the indicator we adjust it according
    to the metrics of the widget font.

 gtk/gtkbuttonprivate.h |    3 +++
 gtk/gtkcheckbutton.c   |   20 +++++++++++++++++++-
 gtk/gtkradiobutton.c   |    8 +++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkbuttonprivate.h b/gtk/gtkbuttonprivate.h
index ea44b71..54483de 100644
--- a/gtk/gtkbuttonprivate.h
+++ b/gtk/gtkbuttonprivate.h
@@ -39,6 +39,9 @@ struct _GtkButtonPrivate
   gfloat                 xalign;
   gfloat                 yalign;
 
+  /* This is only used by checkbox and subclasses */
+  gfloat                 baseline_align;
+
   guint                  activate_timeout;
   guint32                grab_time;
 
diff --git a/gtk/gtkcheckbutton.c b/gtk/gtkcheckbutton.c
index c6ba0cf..48f9a96 100644
--- a/gtk/gtkcheckbutton.c
+++ b/gtk/gtkcheckbutton.c
@@ -379,6 +379,8 @@ static void
 gtk_check_button_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
 {
+  PangoContext *pango_context;
+  PangoFontMetrics *metrics;
   GtkCheckButton *check_button;
   GtkToggleButton *toggle_button;
   GtkButton *button;
@@ -435,6 +437,16 @@ gtk_check_button_size_allocate (GtkWidget     *widget,
            baseline -= child_allocation.y - allocation->y;
          gtk_widget_size_allocate_with_baseline (child, &child_allocation, baseline);
        }
+
+      pango_context = gtk_widget_get_pango_context (widget);
+      metrics = pango_context_get_metrics (pango_context,
+                                          pango_context_get_font_description (pango_context),
+                                          pango_context_get_language (pango_context));
+      button->priv->baseline_align =
+       (double)pango_font_metrics_get_ascent (metrics) /
+       (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics));
+      pango_font_metrics_unref (metrics);
+
     }
   else
     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
@@ -492,6 +504,7 @@ gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
   gint indicator_spacing;
   gint focus_width;
   gint focus_pad;
+  gint baseline;
   guint border_width;
   gboolean interior_focus;
   GtkAllocation allocation;
@@ -502,6 +515,7 @@ gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
   toggle_button = GTK_TOGGLE_BUTTON (check_button);
 
   gtk_widget_get_allocation (widget, &allocation);
+  baseline = gtk_widget_get_allocated_baseline (widget);
   context = gtk_widget_get_style_context (widget);
   state = gtk_widget_get_state_flags (widget);
 
@@ -516,7 +530,11 @@ gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
 
   x = indicator_spacing + border_width;
-  y = (allocation.height - indicator_size) / 2;
+  if (baseline == -1)
+    y = (allocation.height - indicator_size) / 2;
+  else
+    y = CLAMP (baseline - indicator_size * button->priv->baseline_align,
+              0, allocation.height - indicator_size);
 
   child = gtk_bin_get_child (GTK_BIN (check_button));
   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
diff --git a/gtk/gtkradiobutton.c b/gtk/gtkradiobutton.c
index 82ab28c..56bb999 100644
--- a/gtk/gtkradiobutton.c
+++ b/gtk/gtkradiobutton.c
@@ -903,6 +903,7 @@ gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
   gint indicator_size, indicator_spacing;
   gint focus_width;
   gint focus_pad;
+  gint baseline;
   guint border_width;
   gboolean interior_focus;
 
@@ -923,9 +924,14 @@ gtk_radio_button_draw_indicator (GtkCheckButton *check_button,
   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
 
   gtk_widget_get_allocation (widget, &allocation);
+  baseline = gtk_widget_get_allocated_baseline (widget);
 
   x = indicator_spacing + border_width;
-  y = (allocation.height - indicator_size) / 2;
+  if (baseline == -1)
+    y = (allocation.height - indicator_size) / 2;
+  else
+    y = CLAMP (baseline - indicator_size * button->priv->baseline_align,
+              0, allocation.height - indicator_size);
 
   child = gtk_bin_get_child (GTK_BIN (check_button));
   if (!interior_focus || !(child && gtk_widget_get_visible (child)))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]