[gtk+] accel label: Fix RTL handling



commit 9e2a1685c5d9c519212eee3c6bc4cde1025ee377
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Jan 17 14:08:22 2016 -0500

    accel label: Fix RTL handling
    
    The 'mad hack' that GtkAccelLabel used to affect the GtkLabel
    draw function broke with the introduction of gadgets, since
    the positioning is no longer relative to the widgets' allocation
    at the time of the call, but rather to the gadgets allocation.
    
    Instead of coming up with an even madder hack to keep this
    working, give the GtkLabel draw function knowledge about accel
    labels.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760663

 gtk/gtkaccellabel.c |   52 +++++++++++---------------------------------------
 gtk/gtklabel.c      |    8 +++++++
 2 files changed, 20 insertions(+), 40 deletions(-)
---
diff --git a/gtk/gtkaccellabel.c b/gtk/gtkaccellabel.c
index 3593829..e9d88cb 100644
--- a/gtk/gtkaccellabel.c
+++ b/gtk/gtkaccellabel.c
@@ -50,13 +50,13 @@
  * accelerator key on the right of the label text, e.g. “Ctl+S”.
  * It is commonly used in menus to show the keyboard short-cuts for commands.
  *
- * The accelerator key to display is not set explicitly.
- * Instead, the #GtkAccelLabel displays the accelerators which have been added to
- * a particular widget. This widget is set by calling
- * gtk_accel_label_set_accel_widget().
+ * The accelerator key to display is typically not set explicitly (although it
+ * can be, with gtk_accel_label_set_accel()). Instead, the #GtkAccelLabel displays
+ * the accelerators which have been added to a particular widget. This widget is
+ * set by calling gtk_accel_label_set_accel_widget().
  *
- * For example, a #GtkMenuItem widget may have an accelerator added to emit the
- * “activate” signal when the “Ctl+S” key combination is pressed.
+ * For example, a #GtkMenuItem widget may have an accelerator added to emit
+ * the “activate” signal when the “Ctl+S” key combination is pressed.
  * A #GtkAccelLabel is created and added to the #GtkMenuItem, and
  * gtk_accel_label_set_accel_widget() is called with the #GtkMenuItem as the
  * second argument. The #GtkAccelLabel will now display “Ctl+S” after its label.
@@ -449,12 +449,12 @@ gtk_accel_label_draw (GtkWidget *widget,
                       cairo_t   *cr)
 {
   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
-  GtkTextDirection direction;
   guint ac_width;
   GtkAllocation allocation;
   GtkRequisition requisition;
 
-  direction = gtk_widget_get_direction (widget);
+  GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
+
   ac_width = gtk_accel_label_get_accel_width (accel_label);
   gtk_widget_get_allocation (widget, &allocation);
   gtk_widget_get_preferred_size (widget, NULL, &requisition);
@@ -465,45 +465,21 @@ gtk_accel_label_draw (GtkWidget *widget,
       PangoLayout *label_layout;
       PangoLayout *accel_layout;
       GtkLabel *label = GTK_LABEL (widget);
-
       gint x;
       gint y;
 
       context = gtk_widget_get_style_context (widget);
-      label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
 
-      cairo_save (cr);
+      label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
+      accel_layout = gtk_accel_label_get_accel_layout (accel_label);
 
-      /* XXX: Mad hack: We modify the label's width so it renders
-       * properly in its draw function that we chain to.
-       */
-      if (direction == GTK_TEXT_DIR_RTL)
-        cairo_translate (cr, ac_width, 0);
-      if (gtk_label_get_ellipsize (label))
-        pango_layout_set_width (label_layout,
-                                pango_layout_get_width (label_layout) 
-                                - ac_width * PANGO_SCALE);
-      
-      allocation.width -= ac_width;
-      gtk_widget_set_allocation (widget, &allocation);
-      GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
-      allocation.width += ac_width;
-      gtk_widget_set_allocation (widget, &allocation);
-      if (gtk_label_get_ellipsize (label))
-        pango_layout_set_width (label_layout,
-                                pango_layout_get_width (label_layout)
-                                + ac_width * PANGO_SCALE);
-
-      cairo_restore (cr);
-
-      if (direction == GTK_TEXT_DIR_RTL)
+      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
         x = 0;
       else
         x = gtk_widget_get_allocated_width (widget) - ac_width;
 
       gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
 
-      accel_layout = gtk_accel_label_get_accel_layout (accel_label);
       y += get_first_baseline (label_layout) - get_first_baseline (accel_layout) - allocation.y;
 
       gtk_style_context_save_to_node (context, accel_label->priv->accel_node);
@@ -512,11 +488,7 @@ gtk_accel_label_draw (GtkWidget *widget,
 
       g_object_unref (accel_layout);
     }
-  else
-    {
-      GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
-    }
-  
+
   return FALSE;
 }
 
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 9d13def..a1bbb05 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -4299,6 +4299,14 @@ gtk_label_render (GtkCssGadget *gadget,
 
   context = gtk_widget_get_style_context (widget);
 
+  if (GTK_IS_ACCEL_LABEL (widget))
+    {
+      guint ac_width = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
+      width -= ac_width;
+      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+        x += ac_width;
+    }
+
   if (priv->text && (*priv->text != '\0'))
     {
       lx = ly = 0;


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