gtk2/gtk/gtktoolitem.c

746 lines
20 KiB
C
Raw Normal View History

/* gtktoolitem.c
*
* Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se>
* Copyright (C) 2002 James Henstridge <james@daa.com.au>
* Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
*
* 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 "gtktoolitem.h"
#include "gtkmarshalers.h"
#include "gtktoolbar.h"
#include "gtkseparatormenuitem.h"
#include "gtkintl.h"
#include "gtkmain.h"
#include <string.h>
#define MENU_ID "gtk-tool-item-menu-id"
enum {
CREATE_MENU_PROXY,
TOOLBAR_RECONFIGURED,
SET_TOOLTIP,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_VISIBLE_HORIZONTAL,
PROP_VISIBLE_VERTICAL,
};
struct _GtkToolItemPrivate
{
gchar *tip_text;
gchar *tip_private;
guint visible_horizontal : 1;
guint visible_vertical : 1;
guint homogeneous : 1;
guint expand : 1;
guint pack_end : 1;
guint use_drag_window : 1;
GdkWindow *drag_window;
gchar *menu_item_id;
GtkWidget *menu_item;
};
static void gtk_tool_item_init (GtkToolItem *toolitem);
static void gtk_tool_item_class_init (GtkToolItemClass *class);
static void gtk_tool_item_finalize (GObject *object);
static void gtk_tool_item_parent_set (GtkWidget *toolitem,
GtkWidget *parent);
static void gtk_tool_item_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_tool_item_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gtk_tool_item_realize (GtkWidget *widget);
static void gtk_tool_item_unrealize (GtkWidget *widget);
static void gtk_tool_item_map (GtkWidget *widget);
static void gtk_tool_item_unmap (GtkWidget *widget);
static void gtk_tool_item_size_request (GtkWidget *widget,
GtkRequisition *requisition);
static void gtk_tool_item_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gboolean gtk_tool_item_real_set_tooltip (GtkToolItem *tool_item,
GtkTooltips *tooltips,
const gchar *tip_text,
const gchar *tip_private);
static gboolean gtk_tool_item_create_menu_proxy (GtkToolItem *item);
static GObjectClass *parent_class = NULL;
static guint toolitem_signals[LAST_SIGNAL] = { 0 };
GType
gtk_tool_item_get_type (void)
{
static GtkType type = 0;
if (!type)
{
static const GTypeInfo type_info =
{
sizeof (GtkToolItemClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gtk_tool_item_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (GtkToolItem),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_tool_item_init,
};
type = g_type_register_static (GTK_TYPE_BIN,
"GtkToolItem",
&type_info, 0);
}
return type;
}
static void
gtk_tool_item_class_init (GtkToolItemClass *klass)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
parent_class = g_type_class_peek_parent (klass);
object_class = (GObjectClass *)klass;
widget_class = (GtkWidgetClass *)klass;
object_class->set_property = gtk_tool_item_set_property;
object_class->get_property = gtk_tool_item_get_property;
object_class->finalize = gtk_tool_item_finalize;
widget_class->realize = gtk_tool_item_realize;
widget_class->unrealize = gtk_tool_item_unrealize;
widget_class->map = gtk_tool_item_map;
widget_class->unmap = gtk_tool_item_unmap;
widget_class->size_request = gtk_tool_item_size_request;
widget_class->size_allocate = gtk_tool_item_size_allocate;
widget_class->parent_set = gtk_tool_item_parent_set;
klass->create_menu_proxy = gtk_tool_item_create_menu_proxy;
klass->set_tooltip = gtk_tool_item_real_set_tooltip;
g_object_class_install_property (object_class,
PROP_VISIBLE_HORIZONTAL,
g_param_spec_boolean ("visible_horizontal",
_("Visible when horizontal"),
_("Whether the toolbar item is visible when the toolbar is in a horizontal orientation."),
TRUE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_VISIBLE_VERTICAL,
g_param_spec_boolean ("visible_vertical",
_("Visible when vertical"),
_("Whether the toolbar item is visible when the toolbar is in a vertical orientation."),
TRUE,
G_PARAM_READWRITE));
toolitem_signals[CREATE_MENU_PROXY] =
g_signal_new ("create_menu_proxy",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkToolItemClass, create_menu_proxy),
_gtk_boolean_handled_accumulator, NULL,
_gtk_marshal_BOOLEAN__VOID,
G_TYPE_BOOLEAN, 0);
toolitem_signals[TOOLBAR_RECONFIGURED] =
g_signal_new ("toolbar_reconfigured",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkToolItemClass, toolbar_reconfigured),
NULL, NULL,
_gtk_marshal_VOID__VOID,
G_TYPE_NONE, 0);
toolitem_signals[SET_TOOLTIP] =
g_signal_new ("set_tooltip",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkToolItemClass, set_tooltip),
_gtk_boolean_handled_accumulator, NULL,
_gtk_marshal_BOOLEAN__OBJECT_STRING_STRING,
G_TYPE_BOOLEAN, 3,
GTK_TYPE_TOOLTIPS,
G_TYPE_STRING,
G_TYPE_STRING);
g_type_class_add_private (object_class, sizeof (GtkToolItemPrivate));
}
static void
gtk_tool_item_init (GtkToolItem *toolitem)
{
GTK_WIDGET_UNSET_FLAGS (toolitem, GTK_CAN_FOCUS);
toolitem->priv = GTK_TOOL_ITEM_GET_PRIVATE (toolitem);
toolitem->priv->visible_horizontal = TRUE;
toolitem->priv->visible_vertical = TRUE;
toolitem->priv->homogeneous = FALSE;
toolitem->priv->expand = FALSE;
}
static void
gtk_tool_item_finalize (GObject *object)
{
GtkToolItem *item = GTK_TOOL_ITEM (object);
if (item->priv->menu_item)
g_object_unref (item->priv->menu_item);
if (G_OBJECT_CLASS (parent_class)->finalize)
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gtk_tool_item_parent_set (GtkWidget *toolitem,
GtkWidget *prev_parent)
{
_gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (toolitem));
}
static void
gtk_tool_item_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkToolItem *toolitem = GTK_TOOL_ITEM (object);
switch (prop_id)
{
case PROP_VISIBLE_HORIZONTAL:
gtk_tool_item_set_visible_horizontal (toolitem, g_value_get_boolean (value));
break;
case PROP_VISIBLE_VERTICAL:
gtk_tool_item_set_visible_horizontal (toolitem, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gtk_tool_item_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkToolItem *toolitem = GTK_TOOL_ITEM (object);
switch (prop_id)
{
case PROP_VISIBLE_HORIZONTAL:
g_value_set_boolean (value, toolitem->priv->visible_horizontal);
break;
case PROP_VISIBLE_VERTICAL:
g_value_set_boolean (value, toolitem->priv->visible_vertical);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
create_drag_window (GtkToolItem *toolitem)
{
GtkWidget *widget;
GdkWindowAttr attributes;
gint attributes_mask, border_width;
g_return_if_fail (toolitem->priv->use_drag_window == TRUE);
widget = GTK_WIDGET (toolitem);
border_width = GTK_CONTAINER (toolitem)->border_width;
attributes.window_type = GDK_WINDOW_CHILD;
attributes.x = widget->allocation.x + border_width;
attributes.y = widget->allocation.y + border_width;
attributes.width = widget->allocation.width - border_width * 2;
attributes.height = widget->allocation.height - border_width * 2;
attributes.wclass = GDK_INPUT_ONLY;
attributes.event_mask = gtk_widget_get_events (widget);
attributes.event_mask |= (GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
attributes_mask = GDK_WA_X | GDK_WA_Y;
toolitem->priv->drag_window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask);
gdk_window_set_user_data (toolitem->priv->drag_window, toolitem);
}
static void
gtk_tool_item_realize (GtkWidget *widget)
{
GtkToolItem *toolitem;
toolitem = GTK_TOOL_ITEM (widget);
GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
widget->window = gtk_widget_get_parent_window (widget);
g_object_ref (widget->window);
if (toolitem->priv->use_drag_window)
create_drag_window(toolitem);
widget->style = gtk_style_attach (widget->style, widget->window);
}
static void
destroy_drag_window (GtkToolItem *toolitem)
{
if (toolitem->priv->drag_window)
{
gdk_window_set_user_data (toolitem->priv->drag_window, NULL);
gdk_window_destroy (toolitem->priv->drag_window);
toolitem->priv->drag_window = NULL;
}
}
static void
gtk_tool_item_unrealize (GtkWidget *widget)
{
GtkToolItem *toolitem;
toolitem = GTK_TOOL_ITEM (widget);
destroy_drag_window (toolitem);
GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}
static void
gtk_tool_item_map (GtkWidget *widget)
{
GtkToolItem *toolitem;
toolitem = GTK_TOOL_ITEM (widget);
GTK_WIDGET_CLASS (parent_class)->map (widget);
if (toolitem->priv->drag_window)
gdk_window_show (toolitem->priv->drag_window);
}
static void
gtk_tool_item_unmap (GtkWidget *widget)
{
GtkToolItem *toolitem;
toolitem = GTK_TOOL_ITEM (widget);
if (toolitem->priv->drag_window)
gdk_window_hide (toolitem->priv->drag_window);
GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}
static void
gtk_tool_item_size_request (GtkWidget *widget,
GtkRequisition *requisition)
{
GtkWidget *child = GTK_BIN (widget)->child;
if (child && GTK_WIDGET_VISIBLE (child))
{
gtk_widget_size_request (child, requisition);
}
else
{
requisition->height = 0;
requisition->width = 0;
}
requisition->width += (GTK_CONTAINER (widget)->border_width) * 2;
requisition->height += (GTK_CONTAINER (widget)->border_width) * 2;
}
static void
gtk_tool_item_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkToolItem *toolitem = GTK_TOOL_ITEM (widget);
GtkAllocation child_allocation;
gint border_width;
GtkWidget *child = GTK_BIN (widget)->child;
widget->allocation = *allocation;
border_width = GTK_CONTAINER (widget)->border_width;
if (toolitem->priv->drag_window)
gdk_window_move_resize (toolitem->priv->drag_window,
widget->allocation.x + border_width,
widget->allocation.y + border_width,
widget->allocation.width - border_width * 2,
widget->allocation.height - border_width * 2);
if (child && GTK_WIDGET_VISIBLE (child))
{
child_allocation.x = allocation->x + border_width;
child_allocation.y = allocation->y + border_width;
child_allocation.width = allocation->width - 2 * border_width;
child_allocation.height = allocation->height - 2 * border_width;
gtk_widget_size_allocate (child, &child_allocation);
}
}
static gboolean
gtk_tool_item_create_menu_proxy (GtkToolItem *item)
{
if (!GTK_BIN (item)->child)
{
GtkWidget *menu_item = NULL;
menu_item = gtk_separator_menu_item_new();
gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
return TRUE;
}
return FALSE;
}
GtkToolItem *
gtk_tool_item_new (void)
{
GtkToolItem *item;
item = g_object_new (GTK_TYPE_TOOL_ITEM, NULL);
return item;
}
GtkIconSize
gtk_tool_item_get_icon_size (GtkToolItem *tool_item)
{
GtkWidget *parent;
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), GTK_ICON_SIZE_LARGE_TOOLBAR);
parent = GTK_WIDGET (tool_item)->parent;
if (!parent || !GTK_IS_TOOLBAR (parent))
return GTK_ICON_SIZE_LARGE_TOOLBAR;
return gtk_toolbar_get_icon_size (GTK_TOOLBAR (parent));
}
GtkOrientation
gtk_tool_item_get_orientation (GtkToolItem *tool_item)
{
GtkWidget *parent;
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), GTK_ORIENTATION_HORIZONTAL);
parent = GTK_WIDGET (tool_item)->parent;
if (!parent || !GTK_IS_TOOLBAR (parent))
return GTK_ORIENTATION_HORIZONTAL;
return gtk_toolbar_get_orientation (GTK_TOOLBAR (parent));
}
GtkToolbarStyle
gtk_tool_item_get_toolbar_style (GtkToolItem *tool_item)
{
GtkWidget *parent;
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), GTK_TOOLBAR_ICONS);
parent = GTK_WIDGET (tool_item)->parent;
if (!parent || !GTK_IS_TOOLBAR (parent))
return GTK_TOOLBAR_ICONS;
return gtk_toolbar_get_style (GTK_TOOLBAR (parent));
}
GtkReliefStyle
gtk_tool_item_get_relief_style (GtkToolItem *tool_item)
{
GtkWidget *parent;
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), GTK_RELIEF_NONE);
parent = GTK_WIDGET (tool_item)->parent;
if (!parent || !GTK_IS_TOOLBAR (parent))
return GTK_RELIEF_NONE;
return gtk_toolbar_get_relief_style (GTK_TOOLBAR (parent));
}
void
gtk_tool_item_set_expand (GtkToolItem *tool_item,
gboolean expand)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (tool_item));
expand = expand != FALSE;
if (tool_item->priv->expand != expand)
{
tool_item->priv->expand = expand;
gtk_widget_child_notify (GTK_WIDGET (tool_item), "expand");
gtk_widget_queue_resize (GTK_WIDGET (tool_item));
}
}
gboolean
gtk_tool_item_get_expand (GtkToolItem *tool_item)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), FALSE);
return tool_item->priv->expand;
}
void
gtk_tool_item_set_pack_end (GtkToolItem *tool_item,
gboolean pack_end)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (tool_item));
pack_end = pack_end != FALSE;
if (tool_item->priv->pack_end != pack_end)
{
tool_item->priv->pack_end = pack_end;
gtk_widget_child_notify (GTK_WIDGET (tool_item), "pack_end");
gtk_widget_queue_resize (GTK_WIDGET (tool_item));
}
}
gboolean
gtk_tool_item_get_pack_end (GtkToolItem *tool_item)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), FALSE);
return tool_item->priv->pack_end;
}
void
gtk_tool_item_set_homogeneous (GtkToolItem *tool_item,
gboolean homogeneous)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (tool_item));
homogeneous = homogeneous != FALSE;
if (tool_item->priv->homogeneous != homogeneous)
{
tool_item->priv->homogeneous = homogeneous;
gtk_widget_child_notify (GTK_WIDGET (tool_item), "homogeneous");
gtk_widget_queue_resize (GTK_WIDGET (tool_item));
}
}
gboolean
gtk_tool_item_get_homogeneous (GtkToolItem *tool_item)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), FALSE);
return tool_item->priv->homogeneous;
}
static gboolean
gtk_tool_item_real_set_tooltip (GtkToolItem *tool_item,
GtkTooltips *tooltips,
const gchar *tip_text,
const gchar *tip_private)
{
GtkWidget *child = GTK_BIN (tool_item)->child;
if (!child)
return FALSE;
gtk_tooltips_set_tip (tooltips, child, tip_text, tip_private);
return TRUE;
}
void
gtk_tool_item_set_tooltip (GtkToolItem *tool_item,
GtkTooltips *tooltips,
const gchar *tip_text,
const gchar *tip_private)
{
gboolean retval;
g_return_if_fail (GTK_IS_TOOL_ITEM (tool_item));
g_signal_emit (tool_item, toolitem_signals[SET_TOOLTIP], 0,
tooltips, tip_text, tip_private, &retval);
}
void
gtk_tool_item_set_use_drag_window (GtkToolItem *toolitem,
gboolean use_drag_window)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (toolitem));
use_drag_window = use_drag_window != FALSE;
if (toolitem->priv->use_drag_window != use_drag_window)
{
toolitem->priv->use_drag_window = use_drag_window;
if (use_drag_window)
{
if (!toolitem->priv->drag_window && GTK_WIDGET_REALIZED (toolitem))
{
create_drag_window(toolitem);
if (GTK_WIDGET_MAPPED (toolitem))
gdk_window_show (toolitem->priv->drag_window);
}
}
else
{
destroy_drag_window (toolitem);
}
}
}
gboolean
gtk_tool_item_get_use_drag_window (GtkToolItem *toolitem)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (toolitem), FALSE);
return toolitem->priv->use_drag_window;
}
void
gtk_tool_item_set_visible_horizontal (GtkToolItem *toolitem,
gboolean visible_horizontal)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (toolitem));
visible_horizontal = visible_horizontal != FALSE;
if (toolitem->priv->visible_horizontal != visible_horizontal)
{
toolitem->priv->visible_horizontal = visible_horizontal;
g_object_notify (G_OBJECT (toolitem), "visible_horizontal");
gtk_widget_queue_resize (GTK_WIDGET (toolitem));
}
}
gboolean
gtk_tool_item_get_visible_horizontal (GtkToolItem *toolitem)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (toolitem), FALSE);
return toolitem->priv->visible_horizontal;
}
void
gtk_tool_item_set_visible_vertical (GtkToolItem *toolitem,
gboolean visible_vertical)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (toolitem));
visible_vertical = visible_vertical != FALSE;
if (toolitem->priv->visible_vertical != visible_vertical)
{
toolitem->priv->visible_vertical = visible_vertical;
g_object_notify (G_OBJECT (toolitem), "visible_vertical");
gtk_widget_queue_resize (GTK_WIDGET (toolitem));
}
}
gboolean
gtk_tool_item_get_visible_vertical (GtkToolItem *toolitem)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (toolitem), FALSE);
return toolitem->priv->visible_vertical;
}
GtkWidget *
gtk_tool_item_retrieve_proxy_menu_item (GtkToolItem *tool_item)
{
gboolean retval;
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), NULL);
g_signal_emit (tool_item, toolitem_signals[CREATE_MENU_PROXY], 0, &retval);
return tool_item->priv->menu_item;
}
GtkWidget *
gtk_tool_item_get_proxy_menu_item (GtkToolItem *tool_item,
const gchar *menu_item_id)
{
g_return_val_if_fail (GTK_IS_TOOL_ITEM (tool_item), NULL);
g_return_val_if_fail (menu_item_id != NULL, NULL);
if (tool_item->priv->menu_item_id && strcmp (tool_item->priv->menu_item_id, menu_item_id) == 0)
return tool_item->priv->menu_item;
return NULL;
}
void
gtk_tool_item_set_proxy_menu_item (GtkToolItem *tool_item,
const gchar *menu_item_id,
GtkWidget *menu_item)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (tool_item));
g_return_if_fail (menu_item == NULL || GTK_IS_MENU_ITEM (menu_item));
g_return_if_fail (menu_item_id != NULL);
if (tool_item->priv->menu_item_id)
g_free (tool_item->priv->menu_item_id);
tool_item->priv->menu_item_id = g_strdup (menu_item_id);
if (tool_item->priv->menu_item != menu_item)
{
if (tool_item->priv->menu_item)
g_object_unref (G_OBJECT (tool_item->priv->menu_item));
if (menu_item)
{
g_object_ref (menu_item);
gtk_object_sink (GTK_OBJECT (menu_item));
}
tool_item->priv->menu_item = menu_item;
}
}
void
_gtk_tool_item_toolbar_reconfigured (GtkToolItem *tool_item)
{
g_return_if_fail (GTK_IS_TOOL_ITEM (tool_item));
g_signal_emit (tool_item, toolitem_signals[TOOLBAR_RECONFIGURED], 0);
gtk_widget_queue_resize (GTK_WIDGET (tool_item));
}