2008-12-05 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-caption.c
index a1bf348..4632ee7 100644 (file)
@@ -8,7 +8,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
- * the License.
+ * the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 #include                                        <config.h>
 #endif
 
-#include                                        "hildon-caption.h"
-#include                                        <gtk/gtkhbox.h>
-#include                                        <gtk/gtklabel.h>
-#include                                        <gtk/gtkimage.h>
-#include                                        <gtk/gtkentry.h>
-#include                                        <gtk/gtkcombo.h>
-#include                                        <gtk/gtkcombobox.h>
-#include                                        <gtk/gtkcomboboxentry.h>
-#include                                        <gtk/gtkoptionmenu.h>
-#include                                        <gtk/gtkmarshal.h>
-#include                                        <gtk/gtkalignment.h>
-#include                                        <gtk/gtkcheckbutton.h>
-#include                                        <stdio.h>
-#include                                        <string.h>
-#include                                        "hildon-defines.h"
 #include                                        <libintl.h>
+#include                                        <gtk/gtk.h>
+
+#include                                        "hildon-defines.h"
+#include                                        "hildon-caption.h"
 #include                                        "hildon-caption-private.h"
 
 #define                                         _(String)\
@@ -92,6 +81,9 @@ hildon_caption_set_focus                        (GtkWindow *window,
                                                  GtkWidget *caption);
 
 static void 
+hildon_caption_grab_focus                       (GtkWidget *widget);
+
+static void 
 hildon_caption_activate                         (GtkWidget *widget);
 
 static void
@@ -213,6 +205,7 @@ hildon_caption_class_init                       (HildonCaptionClass *caption_cla
     widget_class->size_request                  = hildon_caption_size_request;
     widget_class->size_allocate                 = hildon_caption_size_allocate;
     widget_class->button_press_event            = hildon_caption_button_press;
+    widget_class->grab_focus                    = hildon_caption_grab_focus;
 
     /* Create new signals and properties */
     widget_class->activate_signal = g_signal_new ("activate",
@@ -222,7 +215,7 @@ hildon_caption_class_init                       (HildonCaptionClass *caption_cla
             G_SIGNAL_ACTION,
             G_STRUCT_OFFSET (HildonCaptionClass,
                 activate), NULL, NULL,
-            gtk_marshal_VOID__VOID,
+            g_cclosure_marshal_VOID__VOID,
             G_TYPE_NONE, 0);
 
     /**
@@ -572,30 +565,10 @@ static gboolean
 hildon_caption_button_press                     (GtkWidget *widget, 
                                                  GdkEventButton *event)
 {
-    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (widget);
-    g_assert (priv);
-    GtkWidget *child = GTK_BIN (widget)->child;
-
-    /* nothing to do */
-    if (priv->is_focused == TRUE)
-        return FALSE;
-
-    /* If child can take focus, we simply grab focus to it */
-    if ((GTK_WIDGET_CAN_FOCUS (child) || GTK_IS_CONTAINER (child)) &&
-            GTK_WIDGET_IS_SENSITIVE (child))
-    {
-        /* Only if container can be focusable we must set is_focused to TRUE */ 
-        if (GTK_IS_CONTAINER (child))
-        {
-            if (gtk_widget_child_focus (child, GTK_DIR_TAB_FORWARD))
-                priv->is_focused = TRUE;
-        }
-        else
-        {
-            priv->is_focused = TRUE;
-            gtk_widget_grab_focus (GTK_BIN (widget)->child);
-        }
-    }
+    gtk_widget_grab_focus (GTK_BIN (widget)->child);
+    
+    /* we'll update our focused state in set-focus when/if the child receives
+     * focus */
 
     return FALSE;
 }
@@ -736,14 +709,18 @@ hildon_caption_size_allocate                    (GtkWidget *widget,
 {
     GtkAllocation child_alloc;
     GtkAllocation caption_alloc;
-    GtkRequisition req;
+    GtkRequisition req, child_req;
     GtkWidget *child = NULL;
     HildonCaptionPrivate *priv = NULL;
+    gboolean rtl;
 
     g_assert (HILDON_IS_CAPTION (widget));
     priv = HILDON_CAPTION_GET_PRIVATE (widget);
     g_assert (priv);
 
+    /* Get the rtl status */
+    rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
+
     /* Position the caption to its allocated location */
     if (GTK_WIDGET_REALIZED (widget))
         gdk_window_move_resize (widget->window,
@@ -753,6 +730,8 @@ hildon_caption_size_allocate                    (GtkWidget *widget,
                 MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0));
 
     child = GTK_BIN (widget)->child;
+    if (child)
+        gtk_widget_get_child_requisition (child, &child_req);
 
     widget->allocation = *allocation;
     gtk_widget_get_child_requisition (priv->caption_area, &req);
@@ -762,23 +741,32 @@ hildon_caption_size_allocate                    (GtkWidget *widget,
     child_alloc.x = caption_alloc.x = caption_alloc.y = child_alloc.y = 0;
 
     /* Center the captioned widget */
-    if (child_alloc.width > req.width + HILDON_CAPTION_SPACING)
+    if (rtl)
     {
-        child_alloc.x += req.width + HILDON_CAPTION_SPACING * 2;
-        caption_alloc.width = req.width;
+        if (caption_alloc.width > child_req.width + HILDON_CAPTION_SPACING)
+        {
+            caption_alloc.x = caption_alloc.width - req.width;
+            child_alloc.width = child_req.width;
+        }
+        caption_alloc.width -= child_req.width + HILDON_CAPTION_SPACING * 2;
     }
+    else
+    {
+        if (child_alloc.width > req.width + HILDON_CAPTION_SPACING)
+        {
+            child_alloc.x += req.width + HILDON_CAPTION_SPACING * 2;
+            caption_alloc.width = req.width;
+        }
+        /* Leave at least the space of the HILDON_CAPTION_SPACING in the left */
+        caption_alloc.x = HILDON_CAPTION_SPACING;
 
-    /* Leave at least the space of the HILDON_CAPTION_SPACING in the left */
-    caption_alloc.x = HILDON_CAPTION_SPACING;
-
-    /* Leave room for the other drawable parts of the caption control */
-    child_alloc.width -= req.width + HILDON_CAPTION_SPACING * 2;
+        /* Leave room for the other drawable parts of the caption control */
+        child_alloc.width -= req.width + HILDON_CAPTION_SPACING * 2;
+    }
 
     /* Give the child at least its minimum requisition, unless it is expandable */
     if (! priv->expand && child && GTK_WIDGET_VISIBLE(child))
     {
-        GtkRequisition child_req;
-        gtk_widget_get_child_requisition (child, &child_req);
         child_alloc.width  = MIN (child_alloc.width,  child_req.width);
         child_alloc.height = MIN (child_alloc.height, child_req.height);
        /* Center the child */
@@ -794,6 +782,9 @@ hildon_caption_size_allocate                    (GtkWidget *widget,
         caption_alloc.width = MAX (caption_alloc.width, 0);
     }
 
+    if (rtl)
+        child_alloc.x = caption_alloc.x - child_req.width - HILDON_CAPTION_SPACING * 2;
+
     child_alloc.height = MAX (child_alloc.height, 0);
     caption_alloc.height = MAX (caption_alloc.height, 0);
 
@@ -827,7 +818,7 @@ hildon_caption_forall                           (GtkContainer *container,
 }
 
 /**
- * hildon_caption_set_sizegroup:
+ * hildon_caption_set_size_group:
  * @caption : a #HildonCaption
  * @new_group : a #GtkSizeGroup
  *
@@ -842,7 +833,7 @@ hildon_caption_set_size_group                   (const HildonCaption *self,
 }
 
 /**
- * hildon_caption_get_sizegroup:
+ * hildon_caption_get_size_group:
  * @caption : a #HildonCaption
  *
  * Query given captioned control for the #GtkSizeGroup assigned to it.
@@ -1159,6 +1150,12 @@ hildon_caption_activate                         (GtkWidget *widget)
     gtk_widget_grab_focus (child);
 }
 
+static void
+hildon_caption_grab_focus                       (GtkWidget *widget)
+{
+    gtk_widget_grab_focus (GTK_BIN (widget)->child);
+}
+
 /**
  * hildon_caption_set_child_expand:
  * @caption : a #HildonCaption