/* eggtoolbutton.c
*
* Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se>
* Copyright (C) 2002 James Henstridge <james@daa.com.au>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "eggtoolbutton.h"
#include <gtk/gtkbutton.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkiconfactory.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkimagemenuitem.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkvbox.h>
#include <string.h>
#ifndef _
# define _(s) (s)
#endif
enum {
CLICKED,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_LABEL,
PROP_USE_UNDERLINE,
PROP_STOCK_ID,
PROP_ICON_SET,
PROP_ICON_WIDGET,
};
static void egg_tool_button_init (EggToolButton *button,
EggToolButtonClass *klass);
static void egg_tool_button_class_init (EggToolButtonClass *klass);
static void egg_tool_button_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void egg_tool_button_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void egg_tool_button_finalize (GObject *object);
static void egg_tool_button_toolbar_reconfigured (EggToolItem *tool_item);
static void egg_tool_button_parent_set (GtkWidget *widget, GtkWidget *parent);
static void egg_tool_button_show_all (GtkWidget *widget);
static GtkWidget *egg_tool_button_create_menu_proxy (EggToolItem *item);
static void button_clicked (GtkWidget *widget,
EggToolButton *button);
static GObjectClass *parent_class = NULL;
static guint toolbutton_signals[LAST_SIGNAL] = { 0 };
GType
egg_tool_button_get_type (void)
{
static GtkType type = 0;
if (!type)
{
static const GTypeInfo type_info =
{
sizeof (EggToolButtonClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) egg_tool_button_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (EggToolButton),
0, /* n_preallocs */
(GInstanceInitFunc) egg_tool_button_init,
};
type = g_type_register_static (EGG_TYPE_TOOL_ITEM,
"EggToolButton",
&type_info, 0);
}
return type;
}
static void
egg_tool_button_class_init (EggToolButtonClass *klass)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
EggToolItemClass *tool_item_class;
parent_class = g_type_class_peek_parent (klass);
object_class = (GObjectClass *)klass;
widget_class = (GtkWidgetClass *)klass;
tool_item_class = (EggToolItemClass *)klass;
object_class->set_property = egg_tool_button_set_property;
object_class->get_property = egg_tool_button_get_property;
object_class->finalize = egg_tool_button_finalize;
widget_class->show_all = egg_tool_button_show_all;
widget_class->parent_set = egg_tool_button_parent_set;
tool_item_class->create_menu_proxy = egg_tool_button_create_menu_proxy;
tool_item_class->toolbar_reconfigured = egg_tool_button_toolbar_reconfigured;
klass->button_type = GTK_TYPE_BUTTON;
g_object_class_install_property (object_class,
PROP_LABEL,
g_param_spec_string ("label",
_("Label"),
_("Text to show in the item."),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_USE_UNDERLINE,
g_param_spec_boolean ("use_underline",
_("Use underline"),
_("Interpret underlines in the item label."),
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_STOCK_ID,
g_param_spec_string ("stock_id",
_("Stock Id"),
_("The stock icon displayed on the item."),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_ICON_SET,
g_param_spec_boxed ("icon_set",
_("Icon set"),
_("Icon set to use to draw the item's icon."),
GTK_TYPE_ICON_SET,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_ICON_WIDGET,
g_param_spec_object ("icon_widget",
_("Icon widget"),
_("Icon widget to display in the item."),
GTK_TYPE_WIDGET,
G_PARAM_READWRITE));
toolbutton_signals[CLICKED] =
g_signal_new ("clicked",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EggToolButtonClass, clicked),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
egg_tool_button_init (EggToolButton *button, EggToolButtonClass *klass)
{
EggToolItem *toolitem = EGG_TOOL_ITEM (button);
toolitem->homogeneous = TRUE;
/* create button */
button->button = g_object_new (klass->button_type, NULL);
#if 0
/* FIXME: enable this when we can depend on gtk+ 2.3.0 */
gtk_button_set_focus_on_click (button->button, FALSE);
#endif
g_signal_connect_object (button->button, "clicked",
G_CALLBACK (button_clicked), button, 0);
button->box = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (button->button), button->box);
gtk_widget_show (button->box);
#if 0
button->icon = gtk_image_new();
gtk_box_pack_start (GTK_BOX (button->box), button->icon, TRUE, TRUE, 0);
gtk_widget_show (button->icon);
#endif
button->label = gtk_label_new (NULL);
gtk_label_set_use_underline (GTK_LABEL (button->label), TRUE);
gtk_box_pack_start (GTK_BOX (button->box), button->label, FALSE, TRUE, 0);
gtk_widget_show (button->label);
gtk_container_add (GTK_CONTAINER (button), button->button);
gtk_widget_show (button->button);
}
static gchar *
elide_underscores (const gchar *original)
{
gchar *q, *result;
const gchar *p;
gboolean last_underscore;
q = result = g_malloc (strlen (original) + 1);
last_underscore = FALSE;
for (p = original; *p; p++)
{
if (!last_underscore && *p == '_')
last_underscore = TRUE;
else
{
last_underscore = FALSE;
*q++ = *p;
}
}
*q = '\0';
return result;
}
static void
egg_tool_button_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
EggToolButton *button = EGG_TOOL_BUTTON (object);
switch (prop_id)
{
case PROP_LABEL:
egg_tool_button_set_label (button, g_value_get_string (value));
break;
case PROP_USE_UNDERLINE:
egg_tool_button_set_use_underline (button, g_value_get_boolean (value));
break;
case PROP_STOCK_ID:
egg_tool_button_set_stock_id (button, g_value_get_string (value));
break;
case PROP_ICON_SET:
egg_tool_button_set_icon_set (button, g_value_get_boxed (value));
break;
case PROP_ICON_WIDGET:
egg_tool_button_set_icon_widget (button, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
egg_tool_button_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EggToolButton *button = EGG_TOOL_BUTTON (object);
switch (prop_id)
{
case PROP_LABEL:
g_value_set_string (value, egg_tool_button_get_label (button));
break;
case PROP_USE_UNDERLINE:
g_value_set_boolean (value,
gtk_label_get_use_underline (GTK_LABEL (button->label)));
break;
case PROP_STOCK_ID:
g_value_set_string (value, button->stock_id);
break;
case PROP_ICON_SET:
g_value_set_boxed (value, egg_tool_button_get_icon_set (button));
break;
case PROP_ICON_WIDGET:
g_value_set_object (value, button->icon);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
egg_tool_button_finalize (GObject *object)
{
EggToolButton *button = EGG_TOOL_BUTTON (object);
g_free (button->stock_id);
button->stock_id = NULL;
parent_class->finalize (object);
}
static void
egg_tool_button_show_all (GtkWidget *widget)
{
EggToolButton *button = EGG_TOOL_BUTTON (widget);
GtkToolbarStyle style = egg_tool_item_get_toolbar_style (EGG_TOOL_ITEM (button));
switch (style)
{
case GTK_TOOLBAR_ICONS:
if (button->icon) gtk_widget_show_all (button->icon);
gtk_widget_hide (button->label);
gtk_widget_show (button->box);
gtk_widget_show (button->button);
break;
case GTK_TOOLBAR_TEXT:
if (button->icon) gtk_widget_hide (button->icon);
gtk_widget_show_all (button->label);
gtk_widget_show (button->box);
gtk_widget_show (button->button);
break;
case GTK_TOOLBAR_BOTH:
case GTK_TOOLBAR_BOTH_HORIZ:
gtk_widget_show_all (button->button);
}
gtk_widget_show (GTK_WIDGET (button));
}
static GtkWidget *
egg_tool_button_create_menu_proxy (EggToolItem *item)
{
EggToolButton *button = EGG_TOOL_BUTTON (item);
GtkWidget *menu_item;
GtkWidget *image;
const char *label;
label = gtk_label_get_text (GTK_LABEL (button->label));
menu_item = gtk_image_menu_item_new_with_label (label);
if (GTK_IS_IMAGE (button->icon))
{
image = gtk_image_new ();
if (GTK_IMAGE (button->icon)->storage_type == GTK_IMAGE_STOCK)
{
gchar *stock_id;
gtk_image_get_stock (GTK_IMAGE (button->icon),
&stock_id, NULL);
gtk_image_set_from_stock (GTK_IMAGE (image), stock_id,
GTK_ICON_SIZE_MENU);
}
else if (GTK_IMAGE (button->icon)->storage_type == GTK_IMAGE_ICON_SET)
{
GtkIconSet *icon_set;
gtk_image_get_icon_set (GTK_IMAGE (button->icon), &icon_set, NULL);
gtk_image_set_from_icon_set (GTK_IMAGE (image), icon_set,
GTK_ICON_SIZE_MENU);
}
else
{
g_warning ("FIXME: Add more cases here");
}
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
}
g_signal_connect_object (menu_item, "activate",
G_CALLBACK (gtk_button_clicked),
EGG_TOOL_BUTTON (button)->button,
G_CONNECT_SWAPPED);
return menu_item;
}
static void
use_new_box (EggToolButton *tool_button, GtkWidget *new_box)
{
if (tool_button->icon)
gtk_widget_reparent (tool_button->icon, new_box);
gtk_widget_reparent (tool_button->label, new_box);
gtk_container_remove (GTK_CONTAINER (tool_button->button), tool_button->box);
tool_button->box = new_box;
gtk_container_add (GTK_CONTAINER (tool_button->button), new_box);
}
static void
show_both (EggToolButton *tool_button)
{
gtk_widget_show (tool_button->label);
if (tool_button->icon)
gtk_widget_show (tool_button->icon);
}
static void
egg_tool_button_toolbar_reconfigured (EggToolItem *tool_item)
{
EggToolButton *tool_button = EGG_TOOL_BUTTON (tool_item);
/* icon size */
if (tool_button->icon && GTK_IS_IMAGE (tool_button->icon))
{
GtkImage *image = GTK_IMAGE (tool_button->icon);
GtkImageType storage = gtk_image_get_storage_type (image);
GtkIconSize new_icon_size = egg_tool_item_get_icon_size (EGG_TOOL_ITEM (tool_button));
if (storage == GTK_IMAGE_STOCK)
{
gchar *stock_id;
gtk_image_get_stock (image, &stock_id, NULL);
gtk_image_set_from_stock (image, stock_id, new_icon_size);
}
else if (storage == GTK_IMAGE_ICON_SET)
{
GtkIconSet *icon_set;
gtk_image_get_icon_set (image, &icon_set, NULL);
gtk_image_set_from_icon_set (image, icon_set, new_icon_size);
}
}
/* toolbar style */
switch (egg_tool_item_get_toolbar_style (EGG_TOOL_ITEM (tool_button)))
{
case GTK_TOOLBAR_ICONS:
gtk_widget_hide (tool_button->label);
if (tool_button->icon)
{
gtk_box_set_child_packing (GTK_BOX (tool_button->box), tool_button->icon,
TRUE, TRUE, 0, GTK_PACK_START);
gtk_widget_show (tool_button->icon);
}
break;
case GTK_TOOLBAR_TEXT:
gtk_box_set_child_packing (GTK_BOX (tool_button->box), tool_button->label,
TRUE, TRUE, 0, GTK_PACK_START);
gtk_widget_show (tool_button->label);
if (tool_button->icon)
gtk_widget_hide (tool_button->icon);
break;
case GTK_TOOLBAR_BOTH:
if (GTK_IS_HBOX (tool_button->box))
{
GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox);
use_new_box (tool_button, vbox);
}
show_both (tool_button);
gtk_box_set_child_packing (GTK_BOX (tool_button->box), tool_button->label,
FALSE, TRUE, 0, GTK_PACK_START);
if (tool_button->icon)
gtk_box_set_child_packing (GTK_BOX (tool_button->box), tool_button->icon,
TRUE, TRUE, 0, GTK_PACK_START);
break;
case GTK_TOOLBAR_BOTH_HORIZ:
if (GTK_IS_VBOX (tool_button->box))
{
GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
gtk_widget_show (hbox);
use_new_box (tool_button, hbox);
}
show_both (tool_button);
gtk_box_set_child_packing (GTK_BOX (tool_button->box), tool_button->label,
TRUE, TRUE, 0, GTK_PACK_START);
if (tool_button->icon)
gtk_box_set_child_packing (GTK_BOX (tool_button->box), tool_button->icon,
FALSE, TRUE, 0, GTK_PACK_START);
break;
}
/* relief style */
gtk_button_set_relief (GTK_BUTTON (tool_button->button),
egg_tool_item_get_relief_style (EGG_TOOL_ITEM (tool_button)));
gtk_widget_queue_resize (GTK_WIDGET (tool_button));
}
static void
egg_tool_button_parent_set (GtkWidget *widget, GtkWidget *parent)
{
egg_tool_button_toolbar_reconfigured (EGG_TOOL_ITEM (widget));
}
static void
button_clicked (GtkWidget *widget, EggToolButton *button)
{
g_signal_emit_by_name (button, "clicked");
}
EggToolItem *
egg_tool_button_new_from_stock (const gchar *stock_id)
{
EggToolButton *button;
g_return_val_if_fail (stock_id != NULL, NULL);
button = g_object_new (EGG_TYPE_TOOL_BUTTON,
"stock_id", stock_id,
"use_underline", TRUE,
NULL);
return EGG_TOOL_ITEM (button);
}
EggToolItem *
egg_tool_button_new (void)
{
EggToolButton *button;
button = g_object_new (EGG_TYPE_TOOL_BUTTON,
NULL);
return EGG_TOOL_ITEM (button);
}
GtkWidget *
egg_tool_button_get_icon_widget (EggToolButton *button)
{
g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
return button->icon;
}
void
egg_tool_button_set_icon_widget (EggToolButton *button,
GtkWidget *icon)
{
g_return_if_fail (EGG_IS_TOOL_BUTTON (button));
g_return_if_fail (icon == NULL || GTK_IS_WIDGET (icon));
if (button->icon)
gtk_container_remove (GTK_CONTAINER (button->box), button->icon);
button->icon = NULL;
button->icon_set = (icon != NULL);
if (icon)
{
button->icon = icon;
gtk_box_pack_start (GTK_BOX (button->box), button->icon,
TRUE, TRUE, 0);
gtk_box_reorder_child (GTK_BOX (button->box), button->icon, 0);
}
else if (button->stock_id)
{
button->icon = gtk_image_new_from_stock (button->stock_id,
egg_tool_item_get_icon_size (EGG_TOOL_ITEM (button)));
gtk_box_pack_start (GTK_BOX (button->box), button->icon,
TRUE, TRUE, 0);
}
}
void
egg_tool_button_set_label (EggToolButton *button,
const gchar *label)
{
gchar *label_no_mnemonic = NULL;
GtkStockItem stock_item;
g_return_if_fail (EGG_IS_TOOL_BUTTON (button));
button->label_set = (label != NULL);
if (label)
label_no_mnemonic = elide_underscores (label);
else if (button->stock_id && gtk_stock_lookup (button->stock_id, &stock_item))
label_no_mnemonic = elide_underscores (stock_item.label);
gtk_label_set_label (GTK_LABEL (button->label), label_no_mnemonic);
g_free (label_no_mnemonic);
}
G_CONST_RETURN gchar *
egg_tool_button_get_label (EggToolButton *button)
{
g_return_val_if_fail (EGG_IS_TOOL_BUTTON (button), NULL);
if (button->label_set)
return gtk_label_get_label (GTK_LABEL (button->label));
/* FIXME: is it right to return NULL here? */
return NULL;
}
void
egg_tool_button_set_use_underline (EggToolButton *button,
gboolean use_underline)
{
g_return_if_fail (EGG_IS_TOOL_BUTTON (button));
gtk_label_set_use_underline (GTK_LABEL (button->label), use_underline);
}
gboolean
egg_tool_button_get_use_underline (EggToolButton *button)
{
g_return_val_if_fail (EGG_IS_TOOL_BUTTON (button), FALSE);
return gtk_label_get_use_underline (GTK_LABEL (button->label));
}
void
egg_tool_button_set_stock_id (EggToolButton *button,
const gchar *stock_id)
{
g_return_if_fail (EGG_IS_TOOL_BUTTON (button));
g_return_if_fail (stock_id != NULL);
g_free (button->stock_id);
button->stock_id = g_strdup (stock_id);
if (!button->label_set)
{
GtkStockItem stock_item;
gchar *label_no_mnemonic;
if (gtk_stock_lookup (button->stock_id, &stock_item))
{
label_no_mnemonic = elide_underscores (stock_item.label);
gtk_label_set_label (GTK_LABEL (button->label), label_no_mnemonic);
g_free (label_no_mnemonic);
}
}
if (!button->icon_set)
{
if (button->icon && !GTK_IS_IMAGE (button->icon))
{
gtk_container_remove (GTK_CONTAINER (button->box), button->icon);
button->icon = NULL;
}
if (!button->icon)
{
button->icon = gtk_image_new ();
gtk_box_pack_start (GTK_BOX (button->box), button->icon,
TRUE, TRUE, 0);
gtk_box_reorder_child (GTK_BOX (button->box), button->icon, 0);
}
gtk_image_set_from_stock (GTK_IMAGE (button->icon), button->stock_id,
egg_tool_item_get_icon_size (EGG_TOOL_ITEM (button)));
if (egg_tool_item_get_toolbar_style (EGG_TOOL_ITEM (button)) != GTK_TOOLBAR_TEXT)
gtk_widget_show (button->icon);
}
}
G_CONST_RETURN gchar *
egg_tool_button_get_stock_id (EggToolButton *button)
{
g_return_val_if_fail (EGG_IS_TOOL_BUTTON (button), NULL);
return button->stock_id;
}
GtkIconSet *
egg_tool_button_get_icon_set (EggToolButton *button)
{
GtkIconSet *icon_set = NULL;
if (GTK_IS_IMAGE (button->icon) &&
GTK_IMAGE (button->icon)->storage_type == GTK_IMAGE_ICON_SET)
{
gtk_image_get_icon_set (GTK_IMAGE (button->icon), &icon_set, NULL);
}
return icon_set;
}
void
egg_tool_button_set_icon_set (EggToolButton *button,
GtkIconSet *icon_set)
{
g_return_if_fail (EGG_IS_TOOL_BUTTON (button));
if (button->icon && !GTK_IS_IMAGE (button->icon))
{
gtk_container_remove (GTK_CONTAINER (button->box), button->icon);
button->icon = NULL;
}
if (!button->icon)
{
button->icon = gtk_image_new ();
gtk_box_pack_start (GTK_BOX (button->box), button->icon,
TRUE, TRUE, 0);
gtk_box_reorder_child (GTK_BOX (button->box), button->icon, 0);
}
button->icon_set = (icon_set != NULL);
if (!button->icon_set && button->stock_id)
gtk_image_set_from_stock (GTK_IMAGE (button->icon), button->stock_id,
egg_tool_item_get_icon_size (EGG_TOOL_ITEM (button)));
else
gtk_image_set_from_icon_set (GTK_IMAGE (button->icon), icon_set,
egg_tool_item_get_icon_size (EGG_TOOL_ITEM (button)));
}