2008-08-07 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-button.c
index 0135ccf..a7e7f9c 100644 (file)
  * "auto" so it behaves like a standard GtkButton.
  *
  * The #HildonButton can hold any valid child widget, but it usually
- * contains two labels: title and value (the latter being
- * optional). To change the alignment of the labels, use
- * gtk_button_set_alignment()
+ * contains two labels, named title and value. To change the alignment
+ * of the labels, use gtk_button_set_alignment()
  */
 
 #include                                        "hildon-button.h"
 #include                                        "hildon-enum-types.h"
 
-#define FINGER_BUTTON_HEIGHT                    70
-#define THUMB_BUTTON_HEIGHT                     105
-#define HALFSCREEN_BUTTON_WIDTH                 400
-#define FULLSCREEN_BUTTON_WIDTH                 800
-
 G_DEFINE_TYPE                                   (HildonButton, hildon_button, GTK_TYPE_BUTTON);
 
 #define                                         HILDON_BUTTON_GET_PRIVATE(obj) \
@@ -55,18 +49,22 @@ struct                                          _HildonButtonPrivate
 {
     GtkLabel *title;
     GtkLabel *value;
+    GtkBox *hbox;
     GtkWidget *alignment;
+    GtkWidget *image;
+    GtkPositionType image_position;
 };
 
 enum {
-  PROP_TITLE = 1,
-  PROP_VALUE,
-  PROP_ARRANGEMENT_FLAGS
+    PROP_TITLE = 1,
+    PROP_VALUE,
+    PROP_SIZE,
+    PROP_ARRANGEMENT
 };
 
 static void
-hildon_button_set_arrangement                   (HildonButton      *button,
-                                                 HildonButtonFlags  flags);
+hildon_button_set_arrangement                   (HildonButton            *button,
+                                                 HildonButtonArrangement  arrangement);
 
 static void
 hildon_button_construct_child                   (HildonButton *button);
@@ -87,8 +85,11 @@ hildon_button_set_property                      (GObject      *object,
     case PROP_VALUE:
         hildon_button_set_value (button, g_value_get_string (value));
         break;
-    case PROP_ARRANGEMENT_FLAGS:
-        hildon_button_set_arrangement (button, g_value_get_flags (value));
+    case PROP_SIZE:
+        hildon_helper_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
+        break;
+    case PROP_ARRANGEMENT:
+        hildon_button_set_arrangement (button, g_value_get_enum (value));
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -150,13 +151,24 @@ hildon_button_class_init                        (HildonButtonClass *klass)
 
     g_object_class_install_property (
         gobject_class,
-        PROP_ARRANGEMENT_FLAGS,
+        PROP_SIZE,
         g_param_spec_flags (
-            "arrangement-flags",
-            "Arrangement flags",
+            "size",
+            "Size",
+            "Size request for the button",
+            HILDON_TYPE_SIZE_TYPE,
+            HILDON_SIZE_AUTO,
+            G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
+
+    g_object_class_install_property (
+        gobject_class,
+        PROP_ARRANGEMENT,
+        g_param_spec_enum (
+            "arrangement",
+            "Arrangement",
             "How the button contents must be arranged",
-            HILDON_TYPE_BUTTON_FLAGS,
-            HILDON_BUTTON_WITH_HORIZONTAL_VALUE,
+            HILDON_TYPE_BUTTON_ARRANGEMENT,
+            HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
     gtk_widget_class_install_style_property (
@@ -188,6 +200,9 @@ hildon_button_init                              (HildonButton *self)
     priv->title = GTK_LABEL (gtk_label_new (NULL));
     priv->value = GTK_LABEL (gtk_label_new (NULL));
     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
+    priv->image = NULL;
+    priv->image_position = GTK_POS_LEFT;
+    priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, 0));
 
     gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
     gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
@@ -195,7 +210,10 @@ hildon_button_init                              (HildonButton *self)
     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
 
-    /* The value label is not shown automatically, see hildon_button_set_value() */
+    gtk_box_pack_start (priv->hbox, priv->alignment, TRUE, TRUE, 0);
+
+    /* The labels are not shown automatically, see hildon_button_set_(title|value) */
+    gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
 }
 
@@ -230,42 +248,47 @@ hildon_button_set_size_groups                   (HildonButton *button,
 
 /**
  * hildon_button_new:
- * @flags: flags to set the size and layout of the button
+ * @size: Flags to set the size of the button.
+ * @arrangement: How the labels must be arranged.
  *
  * Creates a new #HildonButton. To add a child widget use gtk_container_add().
  *
  * Returns: a new #HildonButton
  **/
 GtkWidget *
-hildon_button_new                               (HildonButtonFlags  flags)
+hildon_button_new                               (HildonSizeType          size,
+                                                 HildonButtonArrangement arrangement)
 {
-    return hildon_button_new_full (flags, NULL, NULL, NULL, NULL);
+    return hildon_button_new_full (size, arrangement, NULL, NULL, NULL, NULL);
 }
 
 /**
  * hildon_button_new_with_text:
- * @flags: flags to set the size and layout of the button
- * @title: Title of the button (main label)
+ * @size: Flags to set the size of the button.
+ * @arrangement: How the labels must be arranged.
+ * @title: Title of the button (main label), or %NULL
  * @value: Value of the button (secondary label), or %NULL
  *
  * Creates a new #HildonButton with two labels, @title and @value.
  *
- * If you just want to use the main label, set @value to %NULL. You
- * can set it to a non-%NULL value at any time later.
+ * If you just don't want to use one of the labels, set it to
+ * %NULL. You can set it to a non-%NULL value at any time later.
  *
  * Returns: a new #HildonButton
  **/
 GtkWidget *
-hildon_button_new_with_text                     (HildonButtonFlags  flags,
-                                                 const char        *title,
-                                                 const char        *value)
+hildon_button_new_with_text                     (HildonSizeType           size,
+                                                 HildonButtonArrangement  arrangement,
+                                                 const gchar             *title,
+                                                 const gchar             *value)
 {
-    return hildon_button_new_full (flags, title, value, NULL, NULL);
+    return hildon_button_new_full (size, arrangement, title, value, NULL, NULL);
 }
 
 /**
  * hildon_button_new_full:
- * @flags: flags to set the size and layout of the button
+ * @size: Flags to set the size of the button.
+ * @arrangement: How the labels must be arranged.
  * @title: Title of the button (main label)
  * @value: Value of the button (secondary label), or %NULL
  * @title_size_group: a #GtkSizeGroup for the @title label, or %NULL
@@ -274,8 +297,8 @@ hildon_button_new_with_text                     (HildonButtonFlags  flags,
  * Creates a new #HildonButton with two labels, @title and @value, and
  * their respective size groups.
  *
- * If you just want to use the main label, set @value to %NULL. You
- * can set it to a non-%NULL value at any time later.
+ * If you just don't want to use one of the labels, set it to
+ * %NULL. You can set it to a non-%NULL value at any time later.
  *
  * @title and @value will be added to @title_size_group and
  * @value_size_group, respectively, if present.
@@ -283,17 +306,19 @@ hildon_button_new_with_text                     (HildonButtonFlags  flags,
  * Returns: a new #HildonButton
  **/
 GtkWidget *
-hildon_button_new_full                          (HildonButtonFlags  flags,
-                                                 const char        *title,
-                                                 const char        *value,
-                                                 GtkSizeGroup      *title_size_group,
-                                                 GtkSizeGroup      *value_size_group)
+hildon_button_new_full                          (HildonSizeType           size,
+                                                 HildonButtonArrangement  arrangement,
+                                                 const gchar             *title,
+                                                 const gchar             *value,
+                                                 GtkSizeGroup            *title_size_group,
+                                                 GtkSizeGroup            *value_size_group)
 {
     GtkWidget *button;
 
     /* Create widget */
     button = g_object_new (HILDON_TYPE_BUTTON,
-                           "arrangement-flags", flags,
+                           "size", size,
+                           "arrangement", arrangement,
                            "title", title,
                            "value", value,
                            "name", "hildon-button",
@@ -306,51 +331,23 @@ hildon_button_new_full                          (HildonButtonFlags  flags,
 }
 
 static void
-hildon_button_set_arrangement (HildonButton *button,
-                               HildonButtonFlags flags)
+hildon_button_set_arrangement                   (HildonButton            *button,
+                                                 HildonButtonArrangement  arrangement)
 {
     GtkWidget *box;
     HildonButtonPrivate *priv;
     guint horizontal_spacing;
     guint vertical_spacing;
-    gint width = -1;
-    gint height = -1;
-    const char *widget_name = NULL;
 
     priv = HILDON_BUTTON_GET_PRIVATE (button);
 
-    /* Requested height */
-    if (flags & HILDON_BUTTON_FINGER_HEIGHT) {
-        height = FINGER_BUTTON_HEIGHT;
-        widget_name = "hildon-finger-button";
-    } else if (flags & HILDON_BUTTON_THUMB_HEIGHT) {
-        height = THUMB_BUTTON_HEIGHT;
-        widget_name = "hildon-thumb-button";
-    }
-
-    if (widget_name) {
-        gtk_widget_set_name (GTK_WIDGET (button), widget_name);
-    }
-
-    /* Requested width */
-    if (flags & HILDON_BUTTON_HALFSCREEN_WIDTH) {
-        width = HALFSCREEN_BUTTON_WIDTH;
-    } else if (flags & HILDON_BUTTON_FULLSCREEN_WIDTH) {
-        width = FULLSCREEN_BUTTON_WIDTH;
-    }
-
-    g_object_set (button,
-                  "width-request", width,
-                  "height-request", height,
-                  NULL);
-
     /* Pack everything */
     gtk_widget_style_get (GTK_WIDGET (button),
                           "horizontal-spacing", &horizontal_spacing,
                           "vertical-spacing", &vertical_spacing,
                           NULL);
 
-    if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) {
+    if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
         box = gtk_vbox_new (FALSE, vertical_spacing);
     } else {
         box = gtk_hbox_new (FALSE, horizontal_spacing);
@@ -365,15 +362,18 @@ hildon_button_set_arrangement (HildonButton *button,
 /**
  * hildon_button_set_title:
  * @button: a #HildonButton
- * @title: a new title (main label) for the button.
+ * @title: a new title (main label) for the button, or %NULL
  *
  * Sets the title (main label) of @button to @title.
  *
  * This will clear the previously set title.
+ *
+ * If @title is set to %NULL, the title label will be hidden and the
+ * value label will be realigned.
  **/
 void
 hildon_button_set_title                         (HildonButton *button,
-                                                 const char   *title)
+                                                 const gchar  *title)
 {
     HildonButtonPrivate *priv;
 
@@ -382,8 +382,14 @@ hildon_button_set_title                         (HildonButton *button,
     priv = HILDON_BUTTON_GET_PRIVATE (button);
     gtk_label_set_text (priv->title, title);
 
-    if (title)
+    /* If the button has no title, hide the label so the value is
+     * properly aligned */
+    if (title) {
         hildon_button_construct_child (button);
+        gtk_widget_show (GTK_WIDGET (priv->title));
+    } else {
+        gtk_widget_hide (GTK_WIDGET (priv->title));
+    }
 
     g_object_notify (G_OBJECT (button), "title");
 }
@@ -403,7 +409,7 @@ hildon_button_set_title                         (HildonButton *button,
  **/
 void
 hildon_button_set_value                         (HildonButton *button,
-                                                 const char   *value)
+                                                 const gchar  *value)
 {
     HildonButtonPrivate *priv;
 
@@ -414,10 +420,12 @@ hildon_button_set_value                         (HildonButton *button,
 
     /* If the button has no value, hide the label so the title is
      * properly aligned */
-    if (value)
+    if (value) {
+        hildon_button_construct_child (button);
         gtk_widget_show (GTK_WIDGET (priv->value));
-    else
+    } else {
         gtk_widget_hide (GTK_WIDGET (priv->value));
+    }
 
     g_object_notify (G_OBJECT (button), "value");
 }
@@ -432,7 +440,7 @@ hildon_button_set_value                         (HildonButton *button,
  * Returns: The text of the title label. This string is owned by the
  * widget and must not be modified or freed.
  **/
-const char *
+const gchar *
 hildon_button_get_title                         (HildonButton *button)
 {
     HildonButtonPrivate *priv;
@@ -454,7 +462,7 @@ hildon_button_get_title                         (HildonButton *button)
  * Returns: The text of the value label. This string is owned by the
  * widget and must not be modified or freed.
  **/
-const char *
+const gchar *
 hildon_button_get_value                         (HildonButton *button)
 {
     HildonButtonPrivate *priv;
@@ -476,27 +484,108 @@ hildon_button_get_value                         (HildonButton *button)
  **/
 void
 hildon_button_set_text                          (HildonButton *button,
-                                                 const char   *title,
-                                                 const char   *value)
+                                                 const gchar  *title,
+                                                 const gchar  *value)
 {
     hildon_button_set_title (button, title);
     hildon_button_set_value (button, value);
 }
 
+/**
+ * hildon_button_set_image:
+ * @button: a #HildonButton
+ * @image: a widget to set as the button image
+ *
+ * Sets the image of @button to the given widget. The previous image
+ * (if any) will be removed.
+ **/
+void
+hildon_button_set_image                         (HildonButton *button,
+                                                 GtkWidget    *image)
+{
+    HildonButtonPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_BUTTON (button));
+    g_return_if_fail (!image || GTK_IS_WIDGET (image));
+
+    priv = HILDON_BUTTON_GET_PRIVATE (button);
+
+    /* Return if there's nothing to do */
+    if (image == priv->image)
+        return;
+
+    if (priv->image && priv->image->parent)
+        gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
+
+    priv->image = image;
+
+    hildon_button_construct_child (button);
+}
+
+/**
+ * hildon_button_set_image_position:
+ * @button: a #HildonButton
+ * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
+ *
+ * Sets the position of the image inside @button. Only left and right
+ * are supported.
+ **/
+void
+hildon_button_set_image_position                (HildonButton    *button,
+                                                 GtkPositionType  position)
+{
+    HildonButtonPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_BUTTON (button));
+    g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
+
+    priv = HILDON_BUTTON_GET_PRIVATE (button);
+
+    /* Return if there's nothing to do */
+    if (priv->image_position == position)
+        return;
+
+    priv->image_position = position;
+
+    hildon_button_construct_child (button);
+}
+
 static void
 hildon_button_construct_child                   (HildonButton *button)
 {
     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
-    GtkBin *bin = GTK_BIN (button);
+    GtkWidget *child = gtk_bin_get_child (GTK_BIN (button));
 
-    /* Return if there's nothing to do */
-    if (bin->child == priv->alignment)
-        return;
+    /* Save a ref to the alignment if necessary */
+    if (priv->alignment->parent != NULL) {
+        g_object_ref (priv->alignment);
+        gtk_container_remove (GTK_CONTAINER (priv->alignment->parent), priv->alignment);
+    }
+
+    /* Save a ref to the image if necessary */
+    if (priv->image && priv->image->parent != NULL) {
+        g_object_ref (priv->image);
+        gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
+    }
 
-    if (bin->child) {
-        gtk_container_remove (GTK_CONTAINER (button), bin->child);
+    /* Remove the child from the container */
+    if (child != NULL) {
+        gtk_container_remove (GTK_CONTAINER (button), child);
     }
 
-    gtk_container_add (GTK_CONTAINER (button), priv->alignment);
-    gtk_widget_show_all (priv->alignment);
+    /* Pack the image and the alignment in a new hbox */
+    priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, 0));
+
+    if (priv->image && priv->image_position == GTK_POS_LEFT)
+        gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
+
+    gtk_box_pack_start (priv->hbox, priv->alignment, TRUE, TRUE, 0);
+
+    if (priv->image && priv->image_position == GTK_POS_RIGHT)
+        gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
+
+    /* Add the hbox to the button */
+    gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->hbox));
+
+    gtk_widget_show_all (GTK_WIDGET (priv->hbox));
 }