GtkButton patch for bug #58603



Ok, this patch implements the GtkButton behaviour described in bug #58603, 
and the posting by owen it references.

Ok?

/ Alex

Index: gtk/gtkbutton.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkbutton.c,v
retrieving revision 1.65
diff -u -p -r1.65 gtkbutton.c
--- gtk/gtkbutton.c	2001/07/18 23:39:21	1.65
+++ gtk/gtkbutton.c	2001/08/25 06:02:26
@@ -58,7 +58,9 @@ enum {
 enum {
   PROP_0,
   PROP_LABEL,
-  PROP_RELIEF
+  PROP_RELIEF,
+  PROP_USE_UNDERLINE,
+  PROP_USE_STOCK
 };
 
 static void gtk_button_class_init     (GtkButtonClass   *klass);
@@ -91,10 +93,6 @@ static gint gtk_button_enter_notify   (G
 				       GdkEventCrossing *event);
 static gint gtk_button_leave_notify   (GtkWidget        *widget,
 				       GdkEventCrossing *event);
-static void gtk_button_add            (GtkContainer     *container,
-				       GtkWidget        *widget);
-static void gtk_button_remove         (GtkContainer     *container,
-				       GtkWidget        *widget);
 static void gtk_real_button_pressed   (GtkButton        *button);
 static void gtk_real_button_released  (GtkButton        *button);
 static void gtk_real_button_enter     (GtkButton        *button);
@@ -162,8 +160,6 @@ gtk_button_class_init (GtkButtonClass *k
   widget_class->enter_notify_event = gtk_button_enter_notify;
   widget_class->leave_notify_event = gtk_button_leave_notify;
 
-  container_class->add = gtk_button_add;
-  container_class->remove = gtk_button_remove;
   container_class->child_type = gtk_button_child_type;
 
   klass->pressed = gtk_real_button_pressed;
@@ -182,6 +178,22 @@ gtk_button_class_init (GtkButtonClass *k
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
   
   g_object_class_install_property (G_OBJECT_CLASS(object_class),
+                                   PROP_USE_UNDERLINE,
+                                   g_param_spec_boolean ("use_underline",
+							 _("Use underline"),
+							 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
+                                                        FALSE,
+                                                        G_PARAM_READWRITE));
+  
+  g_object_class_install_property (G_OBJECT_CLASS(object_class),
+                                   PROP_USE_STOCK,
+                                   g_param_spec_boolean ("use_stock",
+							 _("Use stock"),
+							 _("If set, the label is used to pick a stock item instead of being displayed"),
+                                                        FALSE,
+                                                        G_PARAM_READWRITE));
+  
+  g_object_class_install_property (G_OBJECT_CLASS(object_class),
                                    PROP_RELIEF,
                                    g_param_spec_enum ("relief",
                                                       _("Border relief"),
@@ -255,10 +267,13 @@ gtk_button_init (GtkButton *button)
   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
   GTK_WIDGET_UNSET_FLAGS (button, GTK_NO_WINDOW);
 
-  button->child = NULL;
+  button->label_text = NULL;
+  
   button->in_button = FALSE;
   button->button_down = FALSE;
   button->relief = GTK_RELIEF_NORMAL;
+  button->use_stock = FALSE;
+  button->use_underline = FALSE;
 }
 
 static GtkType
@@ -282,24 +297,18 @@ gtk_button_set_property (GObject        
 
   switch (prop_id)
     {
-      GtkWidget *child;
-
     case PROP_LABEL:
-      child = GTK_BIN (button)->child;
-      if (!child)
-	child = gtk_widget_new (GTK_TYPE_LABEL,
-				"visible", TRUE,
-				"parent", button,
-				NULL);
-      if (GTK_IS_LABEL (child))
-        {
-          gtk_label_set_text (GTK_LABEL (child),
-                              g_value_get_string (value) ? g_value_get_string (value) : "");
-        }
+      gtk_button_set_label (button, g_value_get_string (value));
       break;
     case PROP_RELIEF:
       gtk_button_set_relief (button, g_value_get_enum (value));
       break;
+    case PROP_USE_UNDERLINE:
+      gtk_button_set_use_underline (button, g_value_get_boolean (value));
+      break;
+    case PROP_USE_STOCK:
+      gtk_button_set_use_stock (button, g_value_get_boolean (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -319,14 +328,17 @@ gtk_button_get_property (GObject        
   switch (prop_id)
     {
     case PROP_LABEL:
-      if (GTK_BIN (button)->child && GTK_IS_LABEL (GTK_BIN (button)->child))
-	 g_value_set_string (value, GTK_LABEL (GTK_BIN (button)->child)->label); 
-      else
-	 g_value_set_string (value, NULL);
+      g_value_set_string (value, button->label_text);
       break;
     case PROP_RELIEF:
       g_value_set_enum (value, gtk_button_get_relief (button));
       break;
+    case PROP_USE_UNDERLINE:
+      g_value_set_boolean (value, button->use_underline);
+      break;
+    case PROP_USE_STOCK:
+      g_value_set_boolean (value, button->use_stock);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -339,20 +351,70 @@ gtk_button_new (void)
   return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
 }
 
+static void
+gtk_button_construct_child (GtkButton *button)
+{
+  GtkStockItem item;
+  GtkWidget *label;
+  GtkWidget *image;
+  GtkWidget *hbox;
+
+  if (button->label_text == NULL)
+    return;
+
+  if (GTK_BIN (button)->child)
+    gtk_container_remove (GTK_CONTAINER (button),
+			  GTK_BIN (button)->child);
+
+  
+  if (button->use_stock &&
+      gtk_stock_lookup (button->label_text, &item))
+    {
+      label = gtk_label_new_with_mnemonic (item.label);
+
+      gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
+      
+      image = gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON);
+      hbox = gtk_hbox_new (FALSE, 1);
+
+      gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
+      gtk_box_pack_end (GTK_BOX (hbox), label, TRUE, TRUE, 0);
+      
+      gtk_container_add (GTK_CONTAINER (button), hbox);
+      gtk_widget_show_all (hbox);
+
+      return;
+    }
+
+  if (button->use_underline)
+    {
+      label = gtk_label_new_with_mnemonic (button->label_text);
+      gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
+    }
+  else
+    label = gtk_label_new (button->label_text);
+  
+  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
+
+  gtk_container_add (GTK_CONTAINER (button), label);
+  gtk_widget_show (label);
+}
+
+
 GtkWidget*
 gtk_button_new_with_label (const gchar *label)
 {
-  GtkWidget *button;
-  GtkWidget *label_widget;
+  GtkButton *button;
+
+  button = GTK_BUTTON (gtk_button_new ());
 
-  button = gtk_button_new ();
-  label_widget = gtk_label_new (label);
-  gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
+  button->label_text = g_strdup (label);
+  button->use_stock = FALSE;
+  button->use_underline = FALSE;
 
-  gtk_container_add (GTK_CONTAINER (button), label_widget);
-  gtk_widget_show (label_widget);
+  gtk_button_construct_child (button);
 
-  return button;
+  return GTK_WIDGET (button);
 }
 
 /**
@@ -367,36 +429,17 @@ gtk_button_new_with_label (const gchar *
 GtkWidget*
 gtk_button_new_from_stock (const gchar   *stock_id)
 {
-  GtkWidget *button;
-  GtkStockItem item;
-
-  if (gtk_stock_lookup (stock_id, &item))
-    {
-      GtkWidget *label;
-      GtkWidget *image;
-      GtkWidget *hbox;
-      
-      button = gtk_button_new ();
-
-      label = gtk_label_new_with_mnemonic (item.label);
-
-      gtk_label_set_mnemonic_widget (GTK_LABEL (label), button);
+  GtkButton *button;
+  
+  button = GTK_BUTTON (gtk_button_new ());
 
-      image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
-      hbox = gtk_hbox_new (FALSE, 1);
+  button->label_text = g_strdup (stock_id);
+  button->use_stock = TRUE;
+  button->use_underline = FALSE;
 
-      gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
-      gtk_box_pack_end (GTK_BOX (hbox), label, TRUE, TRUE, 0);
-      
-      gtk_container_add (GTK_CONTAINER (button), hbox);
-      gtk_widget_show_all (hbox);
-    }
-  else
-    {
-      button = gtk_button_new_with_mnemonic (stock_id);
-    }
+  gtk_button_construct_child (button);
   
-  return button;
+  return GTK_WIDGET (button);
 }
 
 /**
@@ -413,19 +456,17 @@ gtk_button_new_from_stock (const gchar  
 GtkWidget*
 gtk_button_new_with_mnemonic (const gchar *label)
 {
-  GtkWidget *button;
-  GtkWidget *label_widget;
+  GtkButton *button;
 
-  button = gtk_button_new ();
-  
-  label_widget = gtk_label_new_with_mnemonic (label);
+  button = GTK_BUTTON (gtk_button_new ());
 
-  gtk_label_set_mnemonic_widget (GTK_LABEL (label_widget), button);
-  
-  gtk_container_add (GTK_CONTAINER (button), label_widget);
-  gtk_widget_show (label_widget);
+  button->label_text = g_strdup (label);
+  button->use_stock = FALSE;
+  button->use_underline = TRUE;
 
-  return button;
+  gtk_button_construct_child (button);
+
+  return GTK_WIDGET (button);
 }
 
 void
@@ -909,32 +950,6 @@ gtk_button_leave_notify (GtkWidget      
 }
 
 static void
-gtk_button_add (GtkContainer *container,
-		GtkWidget    *widget)
-{
-  g_return_if_fail (container != NULL);
-  g_return_if_fail (widget != NULL);
-
-  if (GTK_CONTAINER_CLASS (parent_class)->add)
-    GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
-
-  GTK_BUTTON (container)->child = GTK_BIN (container)->child;
-}
-
-static void
-gtk_button_remove (GtkContainer *container,
-		   GtkWidget    *widget)
-{
-  g_return_if_fail (container != NULL);
-  g_return_if_fail (widget != NULL);
-
-  if (GTK_CONTAINER_CLASS (parent_class)->remove)
-    GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
-
-  GTK_BUTTON (container)->child = GTK_BIN (container)->child;
-}
-
-static void
 gtk_real_button_pressed (GtkButton *button)
 {
   GtkStateType new_state;
@@ -1074,3 +1089,125 @@ gtk_button_finish_activate (GtkButton *b
     gtk_button_clicked (button);
 }
 
+/**
+ * gtk_button_set_label:
+ * @button: a #GtkButton
+ * @label: a string
+ *
+ * Sets the text of the label of the button to @str. This text is
+ * also used to select the stock item if gtk_button_set_use_stock()
+ * is used.
+ *
+ * This will also clear any previously set labels.
+ **/
+void
+gtk_button_set_label (GtkButton   *button,
+		      const gchar *label)
+{
+  g_return_if_fail (GTK_IS_BUTTON (button));
+  
+  g_free (button->label_text);
+  button->label_text = g_strdup (label);
+  
+  gtk_button_construct_child (button);
+  
+  g_object_notify (G_OBJECT (button), "label");
+}
+
+/**
+ * gtk_button_get_label:
+ * @button: a #GtkButton
+ *
+ * Fetches the text from the label of the button, as set by
+ * gtk_button_set_label().
+ *
+ * Return value: the text of the label widget. This string is
+ *   owned by the widget and must not be modified or freed.
+ *   If the label text has not been set the return value
+ *   can be NULL. This can happen i.e. if you put a custom
+ *   child widget into the button.
+ **/
+G_CONST_RETURN gchar *
+gtk_button_get_label (GtkButton *button)
+{
+  g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
+  
+  return button->label_text;
+}
+
+/**
+ * gtk_button_set_use_underline:
+ * @button: a #GtkButton
+ * @value: %TRUE if underlines in the text indicate mnemonics
+ *
+ * If true, an underline in the text of the button label indicates
+ * the next character should be used for the mnemonic accelerator key.
+ */
+void
+gtk_button_set_use_underline (GtkButton *button,
+			      gboolean   value)
+{
+  g_return_if_fail (GTK_IS_BUTTON (button));
+  
+  button->use_underline = value;
+  
+  gtk_button_construct_child (button);
+  
+  g_object_notify (G_OBJECT (button), "use_underline");
+}
+
+/**
+ * gtk_button_get_use_underline:
+ * @label: a #GtkButton
+ *
+ * Returns whether an embedded underline in the button label indicates a
+ * mnemonic. See gtk_button_set_use_underline ().
+ *
+ * Return value: %TRUE if an embedded underline in the button label
+ *               indicates the mnemonic accelerator keys.
+ **/
+gboolean
+gtk_button_get_use_underline (GtkButton *button)
+{
+  g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
+  
+  return button->use_underline;
+}
+
+/**
+ * gtk_button_set_use_stock:
+ * @button: a #GtkButton
+ * @value: %TRUE if the button should use a stock item
+ *
+ * If true, the label set on the button is used as a
+ * stock id to select the stock item for the button.
+ */
+void
+gtk_button_set_use_stock (GtkButton *button,
+			  gboolean   value)
+{
+  g_return_if_fail (GTK_IS_BUTTON (button));
+  
+  button->use_stock = value;
+  
+  gtk_button_construct_child (button);
+  g_object_notify (G_OBJECT (button), "use_underline");
+}
+
+/**
+ * gtk_button_get_use_stock:
+ * @button: a #GtkButton
+ *
+ * Returns whether the button label is a stock item.
+ *
+ * Return value: %TRUE if the button label is used to
+ *               select a stock item instead of being
+ *               used directly as the label text.
+ */
+gboolean
+gtk_button_get_use_stock (GtkButton *button)
+{
+  g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
+  
+  return button->use_stock;
+}
Index: gtk/gtkbutton.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkbutton.h,v
retrieving revision 1.17
diff -u -p -r1.17 gtkbutton.h
--- gtk/gtkbutton.h	2001/03/23 23:39:23	1.17
+++ gtk/gtkbutton.h	2001/08/25 06:02:26
@@ -52,15 +52,16 @@ struct _GtkButton
 {
   GtkBin bin;
 
-  GtkWidget *child /* deprecapted field,
-		    * use GTK_BIN (button)->child instead
-		    */;
+  gchar *label_text;
 
   guint activate_timeout;
 
   guint in_button : 1;
   guint button_down : 1;
   guint relief : 2;
+  
+  guint use_underline : 1;
+  guint use_stock : 1;
 };
 
 struct _GtkButtonClass
@@ -89,6 +90,17 @@ void           gtk_button_leave         
 void           gtk_button_set_relief        (GtkButton      *button,
 					     GtkReliefStyle  newstyle);
 GtkReliefStyle gtk_button_get_relief        (GtkButton      *button);
+
+void           gtk_button_set_label         (GtkButton      *button,
+					     const gchar    *label);
+
+G_CONST_RETURN gchar *gtk_button_get_label         (GtkButton *button);
+void                  gtk_button_set_use_underline (GtkButton *button,
+						    gboolean   value);
+gboolean              gtk_button_get_use_underline (GtkButton *button);
+void                  gtk_button_set_use_stock     (GtkButton *button,
+						    gboolean   value);
+gboolean              gtk_button_get_use_stock     (GtkButton *button);
 
 
 #ifdef __cplusplus
Index: gtk/gtkclist.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkclist.c,v
retrieving revision 1.201
diff -u -p -r1.201 gtkclist.c
--- gtk/gtkclist.c	2001/08/11 20:27:35	1.201
+++ gtk/gtkclist.c	2001/08/25 06:02:27
@@ -1572,7 +1572,7 @@ gtk_clist_get_column_widget (GtkCList *c
     return NULL;
 
   if (clist->column[column].button)
-    return GTK_BUTTON (clist->column[column].button)->child;
+    return GTK_BIN (clist->column[column].button)->child;
 
   return NULL;
 }
Index: gtk/gtklabel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtklabel.c,v
retrieving revision 1.97
diff -u -p -r1.97 gtklabel.c
--- gtk/gtklabel.c	2001/07/18 23:39:22	1.97
+++ gtk/gtklabel.c	2001/08/25 06:02:28
@@ -2309,7 +2309,7 @@ gtk_label_set_use_underline (GtkLabel *l
  * gtk_label_get_use_underline:
  * @label: a #GtkLabel
  *
- * Returns whether an embedded underline in thef label indicates a
+ * Returns whether an embedded underline in the label indicates a
  * mnemonic. See gtk_label_set_use_underline ().
  *
  * Return value: %TRUE whether an embedded underline in the label indicates


_______________________________________________
gtk-devel-list mailing list
gtk-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-devel-list





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]