Commit f9e66624 authored by Alexander Larsson's avatar Alexander Larsson Committed by Alexander Larsson

Add properties for labels, mnemonics and stock items. Added C accessor

2001-08-26  Alexander Larsson  <alla@lysator.liu.se>

	* gtk/gtkbutton.[ch]:
	Add properties for labels, mnemonics and stock items.
	Added C accessor functions for the properties.
	Removed deprecated button->child.

	* gtk/gtkradiobutton.c:
	* gtk/gtktogglebutton.c:
	Update to use the functions in GtkButton.

	* gtk/gtkcheckbutton.c:
	Update to use the functions in GtkButton.
	Changed size allocation of child. Now only gets as much
	space as it requests.

	* gtk/gtkclist.c:
	Don't use GtkButton->child

	* gtk/gtklabel.c:
	Fixed typo in docs
parent 8d00215d
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* gtk/gtkbutton.[ch]:
Add properties for labels, mnemonics and stock items.
Added C accessor functions for the properties.
Removed deprecated button->child.
* gtk/gtkradiobutton.c:
* gtk/gtktogglebutton.c:
Update to use the functions in GtkButton.
* gtk/gtkcheckbutton.c:
Update to use the functions in GtkButton.
Changed size allocation of child. Now only gets as much
space as it requests.
* gtk/gtkclist.c:
Don't use GtkButton->child
* gtk/gtklabel.c:
Fixed typo in docs
2001-08-26 Alexander Larsson <alla@lysator.liu.se>
* tests/testgtk.c:
......
......@@ -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,19 +93,20 @@ static gint gtk_button_enter_notify (GtkWidget *widget,
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_activate (GtkButton *button);
static void gtk_button_update_state (GtkButton *button);
static GtkType gtk_button_child_type (GtkContainer *container);
static void gtk_button_finish_activate (GtkButton *button,
gboolean do_it);
static GObject* gtk_button_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params);
static void gtk_button_construct_child (GtkButton *button);
static GtkBinClass *parent_class = NULL;
static guint button_signals[LAST_SIGNAL] = { 0 };
......@@ -137,18 +140,21 @@ gtk_button_get_type (void)
static void
gtk_button_class_init (GtkButtonClass *klass)
{
GObjectClass *g_object_class;
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
g_object_class = G_OBJECT_CLASS (klass);
object_class = (GtkObjectClass*) klass;
widget_class = (GtkWidgetClass*) klass;
container_class = (GtkContainerClass*) klass;
parent_class = g_type_class_peek_parent (klass);
G_OBJECT_CLASS(object_class)->set_property = gtk_button_set_property;
G_OBJECT_CLASS(object_class)->get_property = gtk_button_get_property;
g_object_class->constructor = gtk_button_constructor;
g_object_class->set_property = gtk_button_set_property;
g_object_class->get_property = gtk_button_get_property;
widget_class->realize = gtk_button_realize;
widget_class->unrealize = gtk_button_unrealize;
......@@ -161,8 +167,6 @@ gtk_button_class_init (GtkButtonClass *klass)
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;
......@@ -178,7 +182,23 @@ gtk_button_class_init (GtkButtonClass *klass)
_("Label"),
_("Text of the label widget inside the button, if the button contains a label widget."),
NULL,
G_PARAM_READABLE | G_PARAM_WRITABLE));
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
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_PARAM_CONSTRUCT));
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_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS(object_class),
PROP_RELIEF,
......@@ -270,13 +290,39 @@ 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->constructed = FALSE;
button->in_button = FALSE;
button->button_down = FALSE;
button->relief = GTK_RELIEF_NORMAL;
button->use_stock = FALSE;
button->use_underline = FALSE;
button->depressed = FALSE;
}
static GObject*
gtk_button_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params)
{
GObject *object;
GtkButton *button;
object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
n_construct_properties,
construct_params);
button = GTK_BUTTON (object);
button->constructed = TRUE;
if (button->label_text != NULL)
gtk_button_construct_child (button);
return object;
}
static GtkType
gtk_button_child_type (GtkContainer *container)
{
......@@ -298,24 +344,18 @@ gtk_button_set_property (GObject *object,
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;
......@@ -335,14 +375,17 @@ gtk_button_get_property (GObject *object,
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;
......@@ -355,50 +398,33 @@ gtk_button_new (void)
return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
}
GtkWidget*
gtk_button_new_with_label (const gchar *label)
static void
gtk_button_construct_child (GtkButton *button)
{
GtkWidget *button;
GtkWidget *label_widget;
button = gtk_button_new ();
label_widget = gtk_label_new (label);
gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
gtk_container_add (GTK_CONTAINER (button), label_widget);
gtk_widget_show (label_widget);
GtkStockItem item;
GtkWidget *label;
GtkWidget *image;
GtkWidget *hbox;
return button;
}
if (!button->constructed)
return;
if (button->label_text == NULL)
return;
/**
* gtk_button_new_from_stock:
* @stock_id: the name of the stock item
* @returns: a new #GtkButton
*
* Creates a new #GtkButton containing the image and text from a stock item.
* Some stock ids have preprocessor macros like #GTK_STOCK_OK and
* #GTK_STOCK_APPLY.
**/
GtkWidget*
gtk_button_new_from_stock (const gchar *stock_id)
{
GtkWidget *button;
GtkStockItem item;
if (GTK_BIN (button)->child)
gtk_container_remove (GTK_CONTAINER (button),
GTK_BIN (button)->child);
if (gtk_stock_lookup (stock_id, &item))
if (button->use_stock &&
gtk_stock_lookup (button->label_text, &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);
image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
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);
......@@ -406,13 +432,44 @@ gtk_button_new_from_stock (const gchar *stock_id)
gtk_container_add (GTK_CONTAINER (button), hbox);
gtk_widget_show_all (hbox);
return;
}
else
if (button->use_underline)
{
button = gtk_button_new_with_mnemonic (stock_id);
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);
return button;
gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
gtk_widget_show (label);
gtk_container_add (GTK_CONTAINER (button), label);
}
GtkWidget*
gtk_button_new_with_label (const gchar *label)
{
return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
}
/**
* gtk_button_new_from_stock:
* @stock_id: the name of the stock item
* @returns: a new #GtkButton
*
* Creates a new #GtkButton containing the image and text from a stock item.
* Some stock ids have preprocessor macros like #GTK_STOCK_OK and
* #GTK_STOCK_APPLY.
**/
GtkWidget*
gtk_button_new_from_stock (const gchar *stock_id)
{
return g_object_new (GTK_TYPE_BUTTON, "label", stock_id, "use_stock", TRUE, NULL);
}
/**
......@@ -429,19 +486,7 @@ gtk_button_new_from_stock (const gchar *stock_id)
GtkWidget*
gtk_button_new_with_mnemonic (const gchar *label)
{
GtkWidget *button;
GtkWidget *label_widget;
button = gtk_button_new ();
label_widget = gtk_label_new_with_mnemonic (label);
gtk_label_set_mnemonic_widget (GTK_LABEL (label_widget), button);
gtk_container_add (GTK_CONTAINER (button), label_widget);
gtk_widget_show (label_widget);
return button;
return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE, NULL);
}
void
......@@ -934,32 +979,6 @@ gtk_button_leave_notify (GtkWidget *widget,
return FALSE;
}
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)
{
......@@ -1041,6 +1060,140 @@ gtk_button_finish_activate (GtkButton *button,
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
* will be NULL. This will be the case if you create an
* empty button with gtk_button_new() to use as a container.
**/
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
* @use_underline: %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 use_underline)
{
g_return_if_fail (GTK_IS_BUTTON (button));
use_underline = use_underline != FALSE;
if (use_underline != button->use_underline)
{
button->use_underline = use_underline;
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
* @use_stock: %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 use_stock)
{
g_return_if_fail (GTK_IS_BUTTON (button));
use_stock = use_stock != FALSE;
if (use_stock != button->use_stock)
{
button->use_stock = use_stock;
gtk_button_construct_child (button);
g_object_notify (G_OBJECT (button), "use_stock");
}
}
/**
* 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;
}
/**
* _gtk_button_set_depressed:
* @button: a #GtkButton
......
......@@ -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 constructed : 1;
guint in_button : 1;
guint button_down : 1;
guint relief : 2;
guint use_underline : 1;
guint use_stock : 1;
guint depressed : 1;
};
......@@ -90,8 +91,18 @@ void gtk_button_leave (GtkButton *button);
void gtk_button_set_relief (GtkButton *button,
GtkReliefStyle newstyle);
GtkReliefStyle gtk_button_get_relief (GtkButton *button);
void _gtk_button_set_depressed (GtkButton *button,
gboolean depressed);
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);
void _gtk_button_set_depressed (GtkButton *button,
gboolean depressed);
#ifdef __cplusplus
}
......
......@@ -126,17 +126,7 @@ gtk_check_button_new (void)
GtkWidget*
gtk_check_button_new_with_label (const gchar *label)
{
GtkWidget *check_button;
GtkWidget *label_widget;
check_button = gtk_check_button_new ();
label_widget = gtk_label_new (label);
gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
gtk_container_add (GTK_CONTAINER (check_button), label_widget);
gtk_widget_show (label_widget);
return check_button;
return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
}
/**
......@@ -152,17 +142,7 @@ gtk_check_button_new_with_label (const gchar *label)
GtkWidget*
gtk_check_button_new_with_mnemonic (const gchar *label)
{
GtkWidget *check_button;
GtkWidget *label_widget;
check_button = gtk_check_button_new ();
label_widget = gtk_label_new_with_mnemonic (label);
gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
gtk_container_add (GTK_CONTAINER (check_button), label_widget);
gtk_widget_show (label_widget);
return check_button;
return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, "use_underline", TRUE, NULL);
}
......@@ -306,10 +286,11 @@ gtk_check_button_size_allocate (GtkWidget *widget,
child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 + 1 +