From: Alberto Garcia Date: Tue, 29 Jul 2008 18:09:33 +0000 (+0000) Subject: * src/hildon-button.h * src/hildon-button.c (hildon_button_new) (hildon_button_new_wi... X-Git-Tag: 2.1.66-1~644 X-Git-Url: https://vcs.maemo.org/git/?a=commitdiff_plain;h=bf7ae9bb771427698b3d4c877308ea58d1491982;p=hildon * src/hildon-button.h * src/hildon-button.c (hildon_button_new) (hildon_button_new_with_text): New constructor to create a button with no text at all. (hildon_button_init, hildon_button_set_arrangement) (hildon_button_set_title, hildon_button_set_value) (hildon_button_set_text) (hildon_button_construct_child): Hide the value when it's not used so the rest of the text is properly aligned. Allow creating buttons with arbitrary widgets inside (not just title/value labels). * examples/hildon-button-example.c (vertical_buttons_window) (horizontal_buttons_window): Update example to use new API. --- diff --git a/ChangeLog b/ChangeLog index f66bb02..17c9523 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2008-07-29 Alberto Garcia + + * src/hildon-button.h + * src/hildon-button.c + (hildon_button_new) + (hildon_button_new_with_text): New constructor to create a button + with no text at all. + (hildon_button_init, hildon_button_set_arrangement) + (hildon_button_set_title, hildon_button_set_value) + (hildon_button_set_text) + (hildon_button_construct_child): Hide the value when it's not used + so the rest of the text is properly aligned. + Allow creating buttons with arbitrary widgets inside (not just + title/value labels). + + * examples/hildon-button-example.c (vertical_buttons_window) + (horizontal_buttons_window): Update example to use new API. + 2008-07-29 Claudio Saavedra * src/Makefile.am: Added HildonTimeButton. diff --git a/examples/hildon-button-example.c b/examples/hildon-button-example.c index 04b6d40..beeda0c 100644 --- a/examples/hildon-button-example.c +++ b/examples/hildon-button-example.c @@ -60,8 +60,8 @@ vertical_buttons_window (void) /* Finger buttons */ for (i = 0; i < 4; i++) { char *title = g_strdup_printf ("Finger %d", i); - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE | - HILDON_BUTTON_FINGER_HEIGHT, title, "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE | + HILDON_BUTTON_FINGER_HEIGHT, title, "Some value"); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); gtk_box_pack_start (vbox1, button, FALSE, FALSE, 0); g_free (title); @@ -70,8 +70,8 @@ vertical_buttons_window (void) /* Thumb buttons */ for (i = 0; i < 3; i++) { char *title = g_strdup_printf ("Thumb %d", i); - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE | - HILDON_BUTTON_THUMB_HEIGHT, title, "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE | + HILDON_BUTTON_THUMB_HEIGHT, title, "Some value"); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); gtk_box_pack_start (vbox2, button, FALSE, FALSE, 0); g_free (title); @@ -80,7 +80,7 @@ vertical_buttons_window (void) /* Auto buttons */ for (i = 0; i < 4; i++) { char *title = g_strdup_printf ("Auto %d", i); - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE, title, "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE, title, "Some value"); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); gtk_box_pack_start (vbox3, button, FALSE, FALSE, 0); g_free (title); @@ -118,28 +118,28 @@ horizontal_buttons_window (void) gtk_box_pack_start (vbox, GTK_WIDGET (hbox3), TRUE, TRUE, 0); /* Full screen width button */ - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE | - HILDON_BUTTON_FULLSCREEN_WIDTH, "Full width", "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE | + HILDON_BUTTON_FULLSCREEN_WIDTH, "Full width", "Some value"); gtk_box_pack_start (hbox1, button, TRUE, TRUE, 0); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); /* Half screen width buttons */ - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE | - HILDON_BUTTON_HALFSCREEN_WIDTH, "Half width 1", "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE | + HILDON_BUTTON_HALFSCREEN_WIDTH, "Half width 1", "Some value"); gtk_box_pack_start (hbox2, button, TRUE, TRUE, 0); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE | - HILDON_BUTTON_HALFSCREEN_WIDTH, "Half width 2 with longer text", "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE | + HILDON_BUTTON_HALFSCREEN_WIDTH, "Half width 2 with longer text", "Some value"); gtk_box_pack_start (hbox2, button, TRUE, TRUE, 0); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); /* Auto width button */ - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE, "Auto 1", "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE, "Auto 1", "Some value"); gtk_box_pack_start (hbox3, button, TRUE, TRUE, 0); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); - button = hildon_button_new (HILDON_BUTTON_WITH_VERTICAL_VALUE, "Auto 2 with longer text", "Some value"); + button = hildon_button_new_with_text (HILDON_BUTTON_WITH_VERTICAL_VALUE, "Auto 2 with longer text", "Some value"); gtk_box_pack_start (hbox3, button, TRUE, TRUE, 0); g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL); diff --git a/src/hildon-button.c b/src/hildon-button.c index e269b5c..1c6eb9a 100644 --- a/src/hildon-button.c +++ b/src/hildon-button.c @@ -44,6 +44,7 @@ struct _HildonButtonPrivate { GtkLabel *title; GtkLabel *value; + GtkWidget *alignment; }; enum { @@ -52,7 +53,12 @@ enum { PROP_ARRANGEMENT_FLAGS }; -static void hildon_button_set_arrangement (HildonButton *button, HildonButtonFlags flags); +static void +hildon_button_set_arrangement (HildonButton *button, + HildonButtonFlags flags); + +static void +hildon_button_construct_child (HildonButton *button); static void hildon_button_set_property (GObject *object, @@ -132,14 +138,15 @@ hildon_button_class_init (HildonButtonClass *klass) G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); g_object_class_install_property ( - gobject_class, - PROP_ARRANGEMENT_FLAGS, - g_param_spec_int ("arrangement-flags", - "Arrangement flags", - "How the button contents must be arranged", - 0, 64, - HILDON_BUTTON_WITH_HORIZONTAL_VALUE, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + gobject_class, + PROP_ARRANGEMENT_FLAGS, + g_param_spec_int ( + "arrangement-flags", + "Arrangement flags", + "How the button contents must be arranged", + 0, 64, + HILDON_BUTTON_WITH_HORIZONTAL_VALUE, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); gtk_widget_class_install_style_property ( widget_class, @@ -169,12 +176,16 @@ 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); gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title"); gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value"); 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_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE); } void @@ -196,7 +207,13 @@ hildon_button_set_size_groups (HildonButton *button, } GtkWidget * -hildon_button_new (HildonButtonFlags flags, +hildon_button_new (HildonButtonFlags flags) +{ + return hildon_button_new_full (flags, NULL, NULL, NULL, NULL); +} + +GtkWidget * +hildon_button_new_with_text (HildonButtonFlags flags, const char *title, const char *value) { @@ -231,7 +248,6 @@ hildon_button_set_arrangement (HildonButton *button, HildonButtonFlags flags) { GtkWidget *box; - GtkWidget *alignment; HildonButtonPrivate *priv; guint horizontal_spacing; guint vertical_spacing; @@ -278,15 +294,10 @@ hildon_button_set_arrangement (HildonButton *button, box = gtk_hbox_new (FALSE, horizontal_spacing); } - alignment = gtk_alignment_new (0.5, 0.5, 0, 0); - gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->title), TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->value), TRUE, TRUE, 0); - gtk_container_add (GTK_CONTAINER (alignment), box); - gtk_container_add (GTK_CONTAINER (button), alignment); - - gtk_widget_show_all (alignment); + gtk_container_add (GTK_CONTAINER (priv->alignment), box); } void @@ -300,6 +311,9 @@ hildon_button_set_title (HildonButton *button, priv = HILDON_BUTTON_GET_PRIVATE (button); gtk_label_set_text (priv->title, title); + if (title) + hildon_button_construct_child (button); + g_object_notify (G_OBJECT (button), "title"); } @@ -314,6 +328,13 @@ hildon_button_set_value (HildonButton *button, priv = HILDON_BUTTON_GET_PRIVATE (button); gtk_label_set_text (priv->value, value); + /* If the button has no value, hide the label so the title is + * properly aligned */ + if (value) + gtk_widget_show (GTK_WIDGET (priv->value)); + else + gtk_widget_hide (GTK_WIDGET (priv->value)); + g_object_notify (G_OBJECT (button), "value"); } @@ -342,10 +363,28 @@ hildon_button_get_value (HildonButton *button) } void -hildon_button_set_title_and_value (HildonButton *button, +hildon_button_set_text (HildonButton *button, const char *title, const char *value) { hildon_button_set_title (button, title); hildon_button_set_value (button, value); } + +static void +hildon_button_construct_child (HildonButton *button) +{ + HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button); + GtkBin *bin = GTK_BIN (button); + + /* Return if there's nothing to do */ + if (bin->child == priv->alignment) + return; + + if (bin->child) { + gtk_container_remove (GTK_CONTAINER (button), bin->child); + } + + gtk_container_add (GTK_CONTAINER (button), priv->alignment); + gtk_widget_show_all (priv->alignment); +} diff --git a/src/hildon-button.h b/src/hildon-button.h index ce3b581..8832ad0 100644 --- a/src/hildon-button.h +++ b/src/hildon-button.h @@ -73,7 +73,10 @@ GType hildon_button_get_type (void) G_GNUC_CONST; GtkWidget * -hildon_button_new (HildonButtonFlags flags, +hildon_button_new (HildonButtonFlags flags); + +GtkWidget * +hildon_button_new_with_text (HildonButtonFlags flags, const char *title, const char *value); @@ -99,9 +102,9 @@ const char * hildon_button_get_value (HildonButton *button); void -hildon_button_set_title_and_value (HildonButton *button, - const char *title, - const char *value); +hildon_button_set_text (HildonButton *button, + const char *title, + const char *value); void hildon_button_set_size_groups (HildonButton *button,