From b2f872112a2a8d8dd915650718c70889ef0d0c8b Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 30 Mar 2011 02:25:12 -0400 Subject: [PATCH] Make GtkWidget::halign RTL-save This commit makes GTK_ALIGN_START/_END pay attention to the text direction when used in horizontal context. This is how similar parameters in GtkMisc and GtkAlignment work, and is generally expected of GTK+ positioning parameters. And this is new GTK+ 3 api, so it is basically still unused at this point. If explicit right/left turn out to be needed at some point, we can expand the enumeration with new values. --- gtk/gtkenums.h | 3 +++ gtk/gtkwidget.c | 36 ++++++++++++++++++++++++++---------- tests/testadjustsize.c | 3 +++ tests/testexpand.c | 3 +++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/gtk/gtkenums.h b/gtk/gtkenums.h index 2233b00b7f..95cfb299ba 100644 --- a/gtk/gtkenums.h +++ b/gtk/gtkenums.h @@ -63,6 +63,9 @@ G_BEGIN_DECLS * you have for example a 16x16 icon inside a 32x32 space, the icon * could be scaled and stretched, it could be centered, or it could be * positioned to one side of the space. + * + * Note that in horizontal context @GTK_ALIGN_START and @GTK_ALIGN_END + * are interpreted relative to text direction. */ typedef enum { diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index f1de106b97..46c9526b6f 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -5115,11 +5115,27 @@ gtk_widget_real_size_allocate (GtkWidget *widget, } } +/* translate initial/final into start/end */ +static GtkAlign +effective_align (GtkAlign align, + GtkTextDirection direction) +{ + switch (align) + { + case GTK_ALIGN_START: + return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_END : GTK_ALIGN_START; + case GTK_ALIGN_END: + return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END; + default: + return align; + } +} + static void -adjust_for_align(GtkAlign align, - gint *natural_size, - gint *allocated_pos, - gint *allocated_size) +adjust_for_align (GtkAlign align, + gint *natural_size, + gint *allocated_pos, + gint *allocated_size) { switch (align) { @@ -5177,18 +5193,18 @@ gtk_widget_real_adjust_size_allocation (GtkWidget *widget, { adjust_for_margin (aux_info->margin.left, aux_info->margin.right, - minimum_size, natural_size, - allocated_pos, allocated_size); - adjust_for_align (aux_info->halign, + minimum_size, natural_size, + allocated_pos, allocated_size); + adjust_for_align (effective_align (aux_info->halign, gtk_widget_get_direction (widget)), natural_size, allocated_pos, allocated_size); } else { adjust_for_margin (aux_info->margin.top, aux_info->margin.bottom, - minimum_size, natural_size, - allocated_pos, allocated_size); - adjust_for_align (aux_info->valign, + minimum_size, natural_size, + allocated_pos, allocated_size); + adjust_for_align (effective_align (aux_info->valign, GTK_TEXT_DIR_NONE), natural_size, allocated_pos, allocated_size); } } diff --git a/tests/testadjustsize.c b/tests/testadjustsize.c index c45522ad33..38cc0553a6 100644 --- a/tests/testadjustsize.c +++ b/tests/testadjustsize.c @@ -436,6 +436,9 @@ main (int argc, char *argv[]) { gtk_init (&argc, &argv); + if (g_getenv ("RTL")) + gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL); + open_test_window (); open_control_window (); open_alignment_window (); diff --git a/tests/testexpand.c b/tests/testexpand.c index 801b60e7ce..6f34fb71e0 100644 --- a/tests/testexpand.c +++ b/tests/testexpand.c @@ -200,6 +200,9 @@ main (int argc, char *argv[]) { gtk_init (&argc, &argv); + if (g_getenv ("RTL")) + gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL); + create_box_window (); create_table_window ();