Distribute properly extra SGML files in the documentation
[hildon] / src / hildon-caption.c
index 4eac71e..09ab04a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
- * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
+ * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
 #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
@@ -106,10 +98,6 @@ hildon_caption_get_property                     (GObject *object,
                                                  GValue *value, 
                                                  GParamSpec *pspec);
 
-static gboolean 
-hildon_caption_expose                           (GtkWidget *widget,
-                                                 GdkEventExpose *event);
-
 static void 
 hildon_caption_destroy                          (GtkObject *self);
 
@@ -158,7 +146,7 @@ enum
  *
  * Initializes and returns the type of a hildon caption.
  *
- * @Returns: GType of #HildonCaption
+ * Returns: GType of #HildonCaption
  */
 GType G_GNUC_CONST 
 hildon_caption_get_type                         (void)
@@ -208,11 +196,11 @@ hildon_caption_class_init                       (HildonCaptionClass *caption_cla
     container_class->set_child_property         = hildon_caption_set_child_property;
     container_class->get_child_property         = hildon_caption_get_child_property;
 
-    widget_class->expose_event                  = hildon_caption_expose;
     widget_class->hierarchy_changed             = hildon_caption_hierarchy_changed;
     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 +210,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);
 
     /**
@@ -346,55 +334,6 @@ hildon_caption_destroy                          (GtkObject *self)
         GTK_OBJECT_CLASS (parent_class)->destroy (self);
 }
 
-/* Parent, eventbox will run allocate also for the child which may be
- * owning a window too. This window is then moved and resized
- * and we do not want to do that (it causes flickering).
- */
-static gboolean 
-hildon_caption_expose                           (GtkWidget *widget,
-                                                 GdkEventExpose *event)
-{
-    HildonCaptionPrivate *priv = NULL;
-    GtkRequisition req;
-    GtkAllocation alloc;
-    gfloat align;
-
-    g_assert (HILDON_IS_CAPTION (widget));
-    priv = HILDON_CAPTION_GET_PRIVATE (widget);
-    g_assert (priv);
-
-    if (! GTK_WIDGET_DRAWABLE (widget))
-        return FALSE;
-
-    GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
-
-    /* If our child control is focused, we draw nice looking focus
-       graphics for the caption */  
-    if (priv->is_focused && priv->text)
-    {
-        /* Determine focus box dimensions */
-        gtk_widget_get_child_requisition (priv->caption_area, &req);
-        align = hildon_caption_get_label_alignment (HILDON_CAPTION(widget));
-
-        alloc.width = priv->caption_area->allocation.width + HILDON_CAPTION_SPACING;
-        alloc.height = MIN (req.height + (2 * widget->style->ythickness), priv->caption_area->allocation.height);
-
-        alloc.x = priv->caption_area->allocation.x - HILDON_CAPTION_SPACING; /* - left margin */
-        alloc.y = priv->caption_area->allocation.y + 
-            MAX (((priv->caption_area->allocation.height - alloc.height) * align), 0);
-
-        /* Paint the focus box */
-        gtk_paint_box (widget->style, widget->window, GTK_STATE_ACTIVE,
-                GTK_SHADOW_OUT, NULL, widget, "selection",
-                alloc.x, alloc.y, alloc.width, alloc.height);
-
-        /* Paint caption contents on top of the focus box */
-        GTK_WIDGET_GET_CLASS (priv->caption_area)->expose_event (priv->caption_area, event);
-    }
-
-    return FALSE;
-}
-
 static void 
 hildon_caption_set_property                     (GObject *object, 
                                                  guint param_id,
@@ -402,7 +341,6 @@ hildon_caption_set_property                     (GObject *object,
                                                  GParamSpec *pspec)
 {
     HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (object);
-    g_assert (priv);
 
     switch (param_id) 
     {
@@ -572,30 +510,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;
 }
@@ -607,7 +525,6 @@ hildon_caption_init                             (HildonCaption *caption)
 
     /* Initialize startup state */
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     priv->status = HILDON_CAPTION_OPTIONAL;
     priv->icon = NULL;
@@ -647,7 +564,6 @@ hildon_caption_set_focus                        (GtkWindow *window,
                                                  GtkWidget *caption)
 {
     HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     /* check if ancestor gone */
     if (! widget)
@@ -679,10 +595,6 @@ hildon_caption_hierarchy_changed                (GtkWidget *widget,
                                                  GtkWidget *previous_toplevel)
 {
     GtkWidget *current_ancestor;
-    HildonCaptionPrivate *priv;
-
-    priv = HILDON_CAPTION_GET_PRIVATE (widget);
-    g_assert (priv);
 
     if (GTK_WIDGET_CLASS (parent_class)->hierarchy_changed)
         GTK_WIDGET_CLASS (parent_class)->hierarchy_changed (widget, previous_toplevel);
@@ -715,7 +627,6 @@ hildon_caption_size_request                     (GtkWidget *widget,
     g_return_if_fail (HILDON_IS_CAPTION(widget));
 
     priv = HILDON_CAPTION_GET_PRIVATE (widget);
-    g_assert (priv);
 
     /* Use the same size requisition for the main box of the caption */
     gtk_widget_size_request (priv->caption_area, &req);
@@ -736,13 +647,16 @@ hildon_caption_size_allocate                    (GtkWidget *widget,
 {
     GtkAllocation child_alloc;
     GtkAllocation caption_alloc;
-    GtkRequisition req;
+    GtkRequisition req, child_req = {0};
     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))
@@ -753,6 +667,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 +678,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 +719,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);
 
@@ -815,7 +743,6 @@ hildon_caption_forall                           (GtkContainer *container,
     g_assert (callback != NULL);
 
     priv = HILDON_CAPTION_GET_PRIVATE (container);
-    g_assert (priv);
 
     /* Execute callback for the child widgets */
     if (GTK_CONTAINER_CLASS (parent_class)->forall)
@@ -847,7 +774,7 @@ hildon_caption_set_size_group                   (const HildonCaption *self,
  *
  * Query given captioned control for the #GtkSizeGroup assigned to it.
  *
- * @Returns : a #GtkSizeGroup
+ * Returns: a #GtkSizeGroup
  * 
  */
 GtkSizeGroup*
@@ -857,7 +784,6 @@ hildon_caption_get_size_group                   (const HildonCaption *self)
     g_return_val_if_fail (HILDON_IS_CAPTION (self), NULL);
 
     priv = HILDON_CAPTION_GET_PRIVATE(self);
-    g_assert (priv);
 
     return priv->group;
 }
@@ -880,7 +806,7 @@ hildon_caption_get_size_group                   (const HildonCaption *self)
  * The default behaviour for the caption's activate signal is to call   
  * gtk_widget_activate on it's control.
  * 
- * @Returns : a #GtkWidget pointer of Caption
+ * Returns: a #GtkWidget pointer of Caption
  */
 GtkWidget*
 hildon_caption_new                              (GtkSizeGroup *group, 
@@ -913,7 +839,7 @@ hildon_caption_new                              (GtkSizeGroup *group,
  * 
  * Query #HildonCaption whether this captioned control is a mandatory one.
  *
- * @Returns : is this captioned control a mandatory one?
+ * Returns: is this captioned control a mandatory one?
  */
 gboolean 
 hildon_caption_is_mandatory                     (const HildonCaption *caption)
@@ -923,7 +849,6 @@ hildon_caption_is_mandatory                     (const HildonCaption *caption)
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), FALSE);
 
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     return (priv->status == HILDON_CAPTION_MANDATORY);
 }
@@ -942,7 +867,6 @@ hildon_caption_set_icon_position                (HildonCaption *caption,
 {
     g_return_if_fail (HILDON_IS_CAPTION (caption));
     HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     g_return_if_fail (priv->caption_area != NULL);
     
@@ -958,7 +882,7 @@ hildon_caption_set_icon_position                (HildonCaption *caption,
  *
  * Gets #HildonCaption icon position.
  *
- * @Returns : one of the values from #HildonCaptionIconPosition.
+ * Returns: one of the values from #HildonCaptionIconPosition.
  *
  */
 
@@ -968,7 +892,6 @@ hildon_caption_get_icon_position                (const HildonCaption *caption)
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), 0);
 
     HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     return priv->icon_position;
 }
@@ -991,11 +914,11 @@ hildon_caption_set_status                       (HildonCaption *caption,
 
 /**
  * hildon_caption_get_status:
- * @caption : a #HildonCaption
+ * @caption: a #HildonCaption
  *
  * Gets #HildonCaption status.
  *
- * @Returns : one of the values from #HildonCaptionStatus
+ * Returns: one of the values from #HildonCaptionStatus
  */
 HildonCaptionStatus 
 hildon_caption_get_status                       (const HildonCaption *caption)
@@ -1004,7 +927,6 @@ hildon_caption_get_status                       (const HildonCaption *caption)
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), HILDON_CAPTION_OPTIONAL);
 
     priv = HILDON_CAPTION_GET_PRIVATE(caption);
-    g_assert (priv);
 
     return priv->status;
 }
@@ -1032,7 +954,7 @@ hildon_caption_set_icon_image                   (HildonCaption *caption,
  *
  * Gets icon of #HildonCaption
  *
- * @Returns : the #GtkImage widget that is being used as the icon by the
+ * Returns: the #GtkImage widget that is being used as the icon by the
  *            hildon_caption, or NULL if no icon image is in use.
  */
 GtkWidget*
@@ -1043,7 +965,6 @@ hildon_caption_get_icon_image                   (const HildonCaption *caption)
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), NULL);
 
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     return priv->icon;
 }
@@ -1090,7 +1011,7 @@ hildon_caption_set_label_markup                 (HildonCaption *caption,
  *
  * Gets label of #HildonCaption
  * 
- * @Returns : the text currently being used as the label of the caption
+ * Returns: the text currently being used as the label of the caption
  *  control. The string is owned by the label and the caller should never 
  *  free or modify this value.
  */
@@ -1100,7 +1021,6 @@ hildon_caption_get_label                        (const HildonCaption *caption)
     HildonCaptionPrivate *priv;
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), "");
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     return (gchar*) gtk_label_get_text (GTK_LABEL (priv->label));
 }
@@ -1129,7 +1049,7 @@ hildon_caption_set_separator                    (HildonCaption *caption,
  *
  * Gets separator string of #HildonCaption
  *
- * @Returns : the text currently being used as the separator of the caption
+ * Returns: the text currently being used as the separator of the caption
  *  control. The string is owned by the caption control and the caller should
  *  never free or modify this value.   
  */
@@ -1140,7 +1060,6 @@ hildon_caption_get_separator                    (const HildonCaption *caption)
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), "");
 
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     return priv->separator;
 }
@@ -1159,6 +1078,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
@@ -1175,7 +1100,6 @@ hildon_caption_set_child_expand                 (HildonCaption *caption,
     g_return_if_fail (HILDON_IS_CAPTION (caption));
 
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     /* Did the setting really change? */
     if (priv->expand == expand)
@@ -1200,7 +1124,7 @@ hildon_caption_set_child_expand                 (HildonCaption *caption,
  *
  * Gets childs expandability.
  *
- * @Returns : wheter the child is expandable or not.
+ * Returns: wheter the child is expandable or not.
  */
 gboolean 
 hildon_caption_get_child_expand                 (const HildonCaption *caption)
@@ -1209,7 +1133,6 @@ hildon_caption_get_child_expand                 (const HildonCaption *caption)
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), FALSE);
 
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     return priv->expand;
 }
@@ -1283,7 +1206,6 @@ hildon_caption_set_label_alignment              (HildonCaption *caption,
     g_return_if_fail (HILDON_IS_CAPTION (caption));
 
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     g_object_set (priv->label, "yalign", alignment, NULL);
     g_object_set (priv->icon_align, "yalign", alignment, NULL);
@@ -1307,7 +1229,6 @@ hildon_caption_get_label_alignment              (HildonCaption *caption)
 
     g_return_val_if_fail (HILDON_IS_CAPTION (caption), 0);
     priv = HILDON_CAPTION_GET_PRIVATE (caption);
-    g_assert (priv);
 
     g_object_get (priv->label, "yalign", &result, NULL);