2008-09-24 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-check-button.c
index fa1c3d7..414d29e 100644 (file)
  * SECTION:hildon-check-button
  * @short_description: Button with a check box inside
  *
- * This is a standard GtkButton which contains a check box and a
- * label. Functions are provided to get and set the values of both the
- * check box and the label. Standard GtkButton methods must not be
- * used for that.
+ * This is a standard #GtkButton which contains a check box and a
+ * label. Functions are provided to get and set the value of the check
+ * box. Note that this button does NOT support an image, so don't use
+ * gtk_button_set_image()
  *
  * <example>
+ * <title>Using a Hildon check button</title>
  * <programlisting>
  * void
  * button_clicked (GtkButton *button, gpointer user_data)
@@ -43,7 +44,7 @@
  *     GtkWidget *button;
  * <!-- -->
  *     button = hildon_check_button_new (HILDON_SIZE_AUTO);
- *     hildon_check_button_set_label (GTK_BUTTON (button), "Click me");
+ *     gtk_button_set_label (GTK_BUTTON (button), "Click me");
  * <!-- -->
  *     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
  * <!-- -->
@@ -65,50 +66,6 @@ check_button_clicked                            (GtkButton             *button,
 }
 
 /**
- * hildon_check_button_set_label:
- * @button: A #GtkButton created with hildon_check_button_new()
- * @label: New text for the label.
- *
- * Sets the text of the button label to @label.
- **/
-void
-hildon_check_button_set_label                   (GtkButton   *button,
-                                                 const gchar *label)
-{
-    GtkCellRendererText *text_renderer;
-
-    g_return_if_fail (GTK_IS_BUTTON (button));
-    text_renderer = GTK_CELL_RENDERER_TEXT (g_object_get_data (G_OBJECT (button), "text-renderer"));
-    g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (text_renderer));
-
-    g_object_set (text_renderer, "text", label, NULL);
-}
-
-/**
- * hildon_check_button_get_label:
- * @button: A #GtkButton created with hildon_check_button_new()
- *
- * Gets the text of the label inside the button.
- *
- * Return value: the text of the label. This string is owned by the
- * button and must not be modified or freed.
- **/
-const gchar *
-hildon_check_button_get_label                   (GtkButton   *button)
-{
-    GtkCellRendererText *text_renderer;
-    const gchar *text;
-
-    g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
-    text_renderer = GTK_CELL_RENDERER_TEXT (g_object_get_data (G_OBJECT (button), "text-renderer"));
-    g_return_val_if_fail (GTK_IS_CELL_RENDERER_TEXT (text_renderer), NULL);
-
-    g_object_get (text_renderer, "text", &text, NULL);
-
-    return text;
-}
-
-/**
  * hildon_check_button_set_active:
  * @button: A #GtkButton created with hildon_check_button_new()
  * @is_active: new state for the check box
@@ -120,12 +77,18 @@ hildon_check_button_set_active                  (GtkButton *button,
                                                  gboolean   is_active)
 {
     GtkCellRendererToggle *toggle_renderer;
+    gboolean prev_is_active;
 
     g_return_if_fail (GTK_IS_BUTTON (button));
     toggle_renderer = GTK_CELL_RENDERER_TOGGLE (g_object_get_data (G_OBJECT (button), "toggle-renderer"));
     g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle_renderer));
 
-    gtk_cell_renderer_toggle_set_active (toggle_renderer, is_active);
+    prev_is_active = gtk_cell_renderer_toggle_get_active (toggle_renderer);
+
+    if (prev_is_active != is_active) {
+      gtk_cell_renderer_toggle_set_active (toggle_renderer, is_active);
+      gtk_widget_queue_draw (GTK_WIDGET (button));
+    }
 }
 
 /**
@@ -155,13 +118,7 @@ hildon_check_button_get_active                  (GtkButton *button)
  * This function creates a #GtkButton containing a label and a check
  * box.
  *
- * This button has specific functions to get and set the values of the
- * label and the check box. You must not use the standard #GtkButton
- * methods for that.
- *
- * To set the alignment you can use gtk_button_set_alignment()
- *
- * Return value: A newly created #GtkButton widget.
+ * Return value: A newly created #GtkButton widget with a check box.
  **/
 GtkWidget *
 hildon_check_button_new                         (HildonSizeType size)
@@ -169,25 +126,22 @@ hildon_check_button_new                         (HildonSizeType size)
     GtkWidget *button = gtk_button_new ();
     GtkWidget *cell_view = gtk_cell_view_new ();
     GtkCellRenderer *toggle_renderer = gtk_cell_renderer_toggle_new ();
-    GtkCellRenderer *text_renderer = gtk_cell_renderer_text_new ();
-    GtkWidget *alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
 
     /* Set the size of the button */
     hildon_gtk_widget_set_theme_size (button, size);
 
-    /* Pack everything */
-    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view), toggle_renderer, FALSE);
-    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view), text_renderer, FALSE);
-    gtk_container_add (GTK_CONTAINER (alignment), cell_view);
-    gtk_container_add (GTK_CONTAINER (button), alignment);
-    gtk_widget_show_all (GTK_WIDGET (alignment));
-
     /* Toggle the check box when the button is clicked */
     g_signal_connect (button, "clicked", G_CALLBACK (check_button_clicked), toggle_renderer);
 
-    /* Set the data */
+    /* Make sure that the check box is always shown, no matter the value of gtk-button-images */
+    g_signal_connect (cell_view, "notify::visible", G_CALLBACK (gtk_widget_show), NULL);
+
+    /* Store the renderer for later use */
     g_object_set_data (G_OBJECT (button), "toggle-renderer", toggle_renderer);
-    g_object_set_data (G_OBJECT (button), "text-renderer", text_renderer);
+
+    /* Pack everything */
+    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view), toggle_renderer, FALSE);
+    gtk_button_set_image (GTK_BUTTON (button), cell_view);
 
     return button;
 }