medit/moo/mooutils/mooaction.c

900 lines
30 KiB
C

/*
* mooaction.c
*
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#include "mooutils/mooaction.h"
#include "mooutils/mooactiongroup.h"
#include "mooutils/mooaccel.h"
#include "mooutils/moomarshals.h"
#include "mooutils/moocompat.h"
#include <gtk/gtk.h>
#include <string.h>
static void moo_action_class_init (MooActionClass *klass);
static void moo_action_init (MooAction *action);
static void moo_action_finalize (GObject *object);
static void moo_action_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void moo_action_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void moo_action_activate_real (MooAction *action);
static void moo_action_set_closure (MooAction *action,
MooClosure *closure);
static void moo_action_set_label (MooAction *action,
const char *label);
static void moo_action_set_tooltip (MooAction *action,
const char *tooltip);
static void moo_action_set_icon_stock_id(MooAction *action,
const char *stock_id);
static void moo_action_set_icon (MooAction *action,
GdkPixbuf *icon);
static void moo_action_set_sensitive_real (MooAction *action,
gboolean sensitive);
static void moo_action_set_visible_real (MooAction *action,
gboolean visible);
static GtkWidget *moo_action_create_menu_item_real (MooAction *action);
static GtkWidget *moo_action_create_tool_item_real (MooAction *action,
GtkWidget *toolbar,
int position);
static void moo_action_add_proxy (MooAction *action,
GtkWidget *proxy);
#if GTK_MINOR_VERSION < 4
static void tool_item_callback (G_GNUC_UNUSED GtkWidget *widget,
MooAction *action)
{
moo_action_activate (action);
}
#endif /* GTK_MINOR_VERSION < 4 */
enum {
PROP_0,
PROP_ID,
PROP_NAME,
PROP_GROUP_NAME,
PROP_CLOSURE,
PROP_STOCK_ID,
PROP_LABEL,
PROP_TOOLTIP,
PROP_NO_ACCEL,
PROP_ACCEL,
PROP_ICON,
PROP_ICON_STOCK_ID,
PROP_DEAD,
PROP_SENSITIVE,
PROP_VISIBLE,
PROP_IS_IMPORTANT
};
enum {
ACTIVATE,
SET_SENSITIVE,
SET_VISIBLE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = {0};
/* MOO_TYPE_ACTION */
G_DEFINE_TYPE (MooAction, moo_action, G_TYPE_OBJECT)
static void moo_action_class_init (MooActionClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = moo_action_finalize;
gobject_class->set_property = moo_action_set_property;
gobject_class->get_property = moo_action_get_property;
klass->activate = moo_action_activate_real;
klass->add_proxy = moo_action_add_proxy;
klass->set_sensitive = moo_action_set_sensitive_real;
klass->set_visible = moo_action_set_visible_real;
klass->create_menu_item = moo_action_create_menu_item_real;
klass->create_tool_item = moo_action_create_tool_item_real;
g_object_class_install_property (gobject_class,
PROP_ID,
g_param_spec_string ("id",
"id",
"id",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_GROUP_NAME,
g_param_spec_string ("group-name",
"group-name",
"group-name",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"name",
"name",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_CLOSURE,
g_param_spec_object ("closure",
"closure",
"closure",
MOO_TYPE_CLOSURE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_STOCK_ID,
g_param_spec_string ("stock-id",
"stock-id",
"stock-id",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_LABEL,
g_param_spec_string ("label",
"label",
"label",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_TOOLTIP,
g_param_spec_string ("tooltip",
"tooltip",
"tooltip",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_NO_ACCEL,
g_param_spec_boolean ("no-accel",
"no-accel",
"no-accel",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_ACCEL,
g_param_spec_string ("accel",
"accel",
"accel",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_SENSITIVE,
g_param_spec_boolean ("sensitive",
"sensitive",
"sensitive",
TRUE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_VISIBLE,
g_param_spec_boolean ("visible",
"visible",
"visible",
TRUE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_IS_IMPORTANT,
g_param_spec_boolean ("is-important",
"is-important",
"is-important",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_DEAD,
g_param_spec_boolean ("dead",
"dead",
"dead",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (gobject_class,
PROP_ICON_STOCK_ID,
g_param_spec_string ("icon-stock-id",
"icon-stock-id",
"icon-stock-id",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_ICON,
g_param_spec_object ("icon",
"icon",
"icon",
GDK_TYPE_PIXBUF,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
signals[ACTIVATE] =
g_signal_new ("activate",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (MooActionClass, activate),
NULL, NULL,
_moo_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[SET_SENSITIVE] =
g_signal_new ("set-sensitive",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (MooActionClass, set_sensitive),
NULL, NULL,
_moo_marshal_VOID__BOOL,
G_TYPE_NONE, 1,
G_TYPE_BOOLEAN);
signals[SET_VISIBLE] =
g_signal_new ("set-visible",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (MooActionClass, set_visible),
NULL, NULL,
_moo_marshal_VOID__BOOL,
G_TYPE_NONE, 1,
G_TYPE_BOOLEAN);
}
static void moo_action_init (MooAction *action)
{
action->id = NULL;
action->name = NULL;
action->group_name = NULL;
action->no_accel = FALSE;
action->default_accel = NULL;
action->dead = FALSE;
action->visible = TRUE;
action->sensitive = TRUE;
action->closure = NULL;
action->stock_id = NULL;
action->label = NULL;
action->tooltip = NULL;
action->icon_stock_id = NULL;
action->icon = NULL;
}
static void moo_action_finalize (GObject *object)
{
MooAction *action = MOO_ACTION (object);
if (action->closure)
g_object_unref (action->closure);
g_free (action->id);
g_free (action->name);
g_free (action->group_name);
g_free (action->stock_id);
g_free (action->label);
g_free (action->tooltip);
g_free (action->icon_stock_id);
g_free (action->default_accel);
if (action->icon)
g_object_unref (action->icon);
G_OBJECT_CLASS (moo_action_parent_class)->finalize (object);
}
static void moo_action_activate_real (MooAction *action)
{
g_return_if_fail (MOO_IS_ACTION (action));
if (action->closure)
moo_closure_invoke (action->closure);
}
static void moo_action_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MooAction *action = MOO_ACTION (object);
switch (prop_id)
{
case PROP_ID:
g_value_set_string (value, action->id);
break;
case PROP_GROUP_NAME:
g_value_set_string (value, action->group_name);
break;
case PROP_NAME:
g_value_set_string (value, action->name);
break;
case PROP_SENSITIVE:
g_value_set_boolean (value, action->sensitive ? TRUE : FALSE);
break;
case PROP_VISIBLE:
g_value_set_boolean (value, action->visible ? TRUE : FALSE);
break;
case PROP_DEAD:
g_value_set_boolean (value, action->dead ? TRUE : FALSE);
break;
case PROP_CLOSURE:
g_value_set_object (value, action->closure);
break;
case PROP_STOCK_ID:
g_value_set_string (value, action->stock_id);
break;
case PROP_LABEL:
g_value_set_string (value, action->label);
break;
case PROP_TOOLTIP:
g_value_set_string (value, action->tooltip);
break;
case PROP_NO_ACCEL:
g_value_set_boolean (value, action->no_accel ? TRUE : FALSE);
break;
case PROP_IS_IMPORTANT:
g_value_set_boolean (value, action->is_important ? TRUE : FALSE);
break;
case PROP_ACCEL:
g_value_set_string (value, action->default_accel);
break;
case PROP_ICON_STOCK_ID:
g_value_set_string (value, action->icon_stock_id);
break;
case PROP_ICON:
g_value_set_object (value, action->icon);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void moo_action_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MooAction *action = MOO_ACTION (object);
switch (prop_id)
{
case PROP_ID:
g_free (action->id);
action->id = g_strdup (g_value_get_string (value));
g_object_notify (object, "id");
break;
case PROP_GROUP_NAME:
g_free (action->group_name);
action->group_name = g_strdup (g_value_get_string (value));
g_object_notify (object, "group-name");
break;
case PROP_NAME:
g_free (action->name);
action->name = g_strdup (g_value_get_string (value));
g_object_notify (object, "name");
break;
case PROP_SENSITIVE:
moo_action_set_sensitive (action,
g_value_get_boolean (value) ? TRUE : FALSE);
break;
case PROP_VISIBLE:
moo_action_set_visible (action,
g_value_get_boolean (value) ? TRUE : FALSE);
break;
case PROP_DEAD:
action->dead = g_value_get_boolean (value) ? TRUE : FALSE;
g_object_notify (object, "dead");
break;
case PROP_IS_IMPORTANT:
action->is_important = g_value_get_boolean (value) ? TRUE : FALSE;
g_object_notify (object, "is_important");
break;
case PROP_CLOSURE:
moo_action_set_closure (action,
MOO_CLOSURE (g_value_get_object (value)));
break;
case PROP_STOCK_ID:
g_free (action->stock_id);
action->stock_id = g_strdup (g_value_get_string (value));
g_object_notify (G_OBJECT (action), "stock_id");
break;
case PROP_LABEL:
moo_action_set_label (action,
g_value_get_string (value));
break;
case PROP_TOOLTIP:
moo_action_set_tooltip (action,
g_value_get_string (value));
break;
case PROP_NO_ACCEL:
action->no_accel = g_value_get_boolean (value) ? TRUE : FALSE;
g_object_notify (object, "no-accel");
break;
case PROP_ACCEL:
g_free (action->default_accel);
action->default_accel = g_strdup (g_value_get_string (value));
g_object_notify (object, "accel");
break;
case PROP_ICON_STOCK_ID:
moo_action_set_icon_stock_id (action,
g_value_get_string (value));
break;
case PROP_ICON:
moo_action_set_icon (action, GDK_PIXBUF (g_value_get_object (value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void moo_action_set_closure (MooAction *action,
MooClosure *closure)
{
if (action->closure == closure)
return;
if (action->closure)
g_object_unref (G_OBJECT (action->closure));
action->closure = closure;
if (action->closure)
{
g_object_ref (G_OBJECT (action->closure));
gtk_object_sink (GTK_OBJECT (action->closure));
}
}
static void moo_action_set_label (MooAction *action,
const char *label)
{
g_free (action->label);
action->label = g_strdup (label);
}
static void moo_action_set_tooltip (MooAction *action,
const char *tooltip)
{
g_free (action->tooltip);
action->tooltip = g_strdup (tooltip);
}
void moo_action_set_no_accel (MooAction *action,
gboolean no_accel)
{
g_return_if_fail (MOO_IS_ACTION (action));
action->no_accel = no_accel ? TRUE : FALSE;
}
gboolean moo_action_get_no_accel (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), TRUE);
return action->no_accel ? TRUE : FALSE;
}
GtkWidget *moo_action_create_menu_item (MooAction *action)
{
MooActionClass *klass;
g_return_val_if_fail (MOO_IS_ACTION (action), NULL);
g_return_val_if_fail (!action->dead, NULL);
klass = MOO_ACTION_GET_CLASS (action);
g_return_val_if_fail (klass != NULL && klass->create_menu_item != NULL, NULL);
return klass->create_menu_item (action);
}
GtkWidget *moo_action_create_tool_item (MooAction *action,
GtkWidget *toolbar,
int position)
{
MooActionClass *klass;
g_return_val_if_fail (MOO_IS_ACTION (action), FALSE);
g_return_val_if_fail (!action->dead, FALSE);
klass = MOO_ACTION_GET_CLASS (action);
g_return_val_if_fail (klass != NULL && klass->create_tool_item != NULL, FALSE);
return klass->create_tool_item (action, toolbar, position);
}
const char *moo_action_get_id (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), NULL);
return action->id;
}
const char *moo_action_get_group_name (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), NULL);
return action->group_name;
}
const char *moo_action_get_name (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), NULL);
return action->name;
}
const char *moo_action_make_accel_path (const char *group_id,
const char *action_id)
{
static GString *path = NULL;
g_return_val_if_fail (action_id != NULL && action_id[0] != 0, NULL);
if (!path)
path = g_string_new (NULL);
if (group_id && group_id[0])
g_string_printf (path, "<MooAction>/%s/%s", group_id, action_id);
else
g_string_printf (path, "<MooAction>/%s", action_id);
return path->str;
}
static GtkWidget *moo_action_create_menu_item_real (MooAction *action)
{
GtkWidget *item = NULL;
if (action->dead)
{
return NULL;
}
else if (action->stock_id)
{
item = gtk_image_menu_item_new_from_stock (action->stock_id, NULL);
}
else
{
GtkWidget *icon = NULL;
if (action->icon_stock_id)
{
icon = gtk_image_new_from_stock (action->icon_stock_id, GTK_ICON_SIZE_MENU);
if (!icon) g_warning ("could not create stock icon '%s'",
action->icon_stock_id);
else gtk_widget_show (icon);
}
item = gtk_image_menu_item_new_with_mnemonic (action->label);
if (icon)
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), icon);
}
if (!action->no_accel)
gtk_menu_item_set_accel_path (GTK_MENU_ITEM (item),
_moo_action_get_accel_path (action));
moo_action_add_proxy (action, item);
return item;
}
static GtkWidget*
moo_action_create_tool_item_real (MooAction *action,
GtkWidget *toolbar,
int position)
{
#if GTK_MINOR_VERSION >= 4
GtkToolItem *item = NULL;
if (action->stock_id)
{
item = gtk_tool_button_new_from_stock (action->stock_id);
}
else
{
GtkWidget *icon = NULL;
if (action->icon_stock_id)
{
icon = gtk_image_new_from_stock (action->icon_stock_id,
gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar)));
if (!icon)
g_warning ("could not create stock icon '%s'",
action->icon_stock_id);
else
gtk_widget_show (icon);
}
item = gtk_tool_button_new (icon, action->label);
gtk_tool_button_set_use_underline (GTK_TOOL_BUTTON (item), TRUE);
}
if (action->tooltip)
{
GtkTooltips *tooltips = gtk_tooltips_new ();
gtk_object_sink (g_object_ref (tooltips));
gtk_tool_item_set_tooltip (item,
tooltips,
action->tooltip,
action->tooltip);
g_object_set_data_full (G_OBJECT (item), "moo-tooltips",
tooltips, g_object_unref);
}
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, position);
gtk_container_child_set (GTK_CONTAINER (toolbar), GTK_WIDGET (item),
"homogeneous", FALSE, NULL);
#else /* GTK_MINOR_VERSION < 4 */
GtkWidget *item = NULL;
if (action->stock_id)
{
item = gtk_toolbar_insert_stock (toolbar,
action->stock_id,
action->tooltip,
action->tooltip,
(GtkSignalFunc)tool_item_callback,
action,
position);
}
else
{
GtkWidget *icon = NULL;
if (action->icon_stock_id)
{
icon = gtk_image_new_from_stock (action->icon_stock_id,
gtk_toolbar_get_icon_size (toolbar));
if (!icon)
g_warning ("could not create stock icon '%s'",
action->icon_stock_id);
else
gtk_widget_show (icon);
}
item = gtk_toolbar_insert_item (toolbar,
action->label,
action->tooltip,
action->tooltip,
icon,
NULL,
NULL,
position);
gtk_button_set_use_underline (GTK_BUTTON (item), TRUE);
}
#endif /* GTK_MINOR_VERSION < 4 */
moo_action_add_proxy (action, GTK_WIDGET (item));
return GTK_WIDGET (item);
}
void moo_action_activate (MooAction *action)
{
g_return_if_fail (MOO_IS_ACTION (action));
g_signal_emit (action, signals[ACTIVATE], 0);
}
void moo_action_set_sensitive (MooAction *action,
gboolean sensitive)
{
g_return_if_fail (MOO_IS_ACTION (action));
g_signal_emit (action, signals[SET_SENSITIVE], 0, sensitive ? TRUE : FALSE);
}
void moo_action_set_visible (MooAction *action,
gboolean visible)
{
g_return_if_fail (MOO_IS_ACTION (action));
g_signal_emit (action, signals[SET_VISIBLE], 0, visible ? TRUE : FALSE);
}
static void moo_action_set_sensitive_real (MooAction *action,
gboolean sensitive)
{
g_return_if_fail (MOO_IS_ACTION (action));
action->sensitive = sensitive ? TRUE : FALSE;
g_object_notify (G_OBJECT (action), "sensitive");
}
static void moo_action_set_visible_real (MooAction *action,
gboolean visible)
{
g_return_if_fail (MOO_IS_ACTION (action));
action->visible = visible ? TRUE : FALSE;
g_object_notify (G_OBJECT (action), "visible");
}
static void moo_action_set_icon_stock_id(MooAction *action,
const char *stock_id)
{
if (action->icon_stock_id == stock_id) return;
g_free (action->icon_stock_id);
action->icon_stock_id = g_strdup (stock_id);
g_object_notify (G_OBJECT (action), "icon-stock-id");
}
static void moo_action_set_icon (G_GNUC_UNUSED MooAction *action,
GdkPixbuf *icon)
{
if (icon)
g_warning ("%s: implement me", G_STRLOC);
}
/****************************************************************************/
static void set_visible_callback (GtkWidget *proxy,
gboolean visible);
static void proxy_destroyed (MooAction *action,
gpointer proxy);
static void action_destroyed (GtkWidget *proxy,
gpointer action);
static void set_visible_callback (GtkWidget *proxy,
gboolean visible)
{
if (visible)
gtk_widget_show (proxy);
else
gtk_widget_hide (proxy);
}
static void proxy_destroyed (MooAction *action,
gpointer proxy)
{
g_signal_handlers_disconnect_by_func (action, (gpointer)gtk_widget_set_sensitive, proxy);
g_signal_handlers_disconnect_by_func (action, (gpointer)set_visible_callback, proxy);
g_object_weak_unref (G_OBJECT (action), (GWeakNotify)action_destroyed, proxy);
}
static void action_destroyed (GtkWidget *proxy,
gpointer action)
{
g_signal_handlers_disconnect_by_func (proxy, (gpointer)moo_action_activate, action);
g_object_weak_unref (G_OBJECT (proxy), (GWeakNotify)proxy_destroyed, action);
}
static void moo_action_add_proxy (MooAction *action,
GtkWidget *proxy)
{
gtk_widget_set_sensitive (proxy, action->sensitive);
if (action->visible)
gtk_widget_show (proxy);
else
gtk_widget_hide (proxy);
if (GTK_IS_MENU_ITEM (proxy))
{
g_signal_connect_swapped (proxy, "activate",
G_CALLBACK (moo_action_activate),
action);
// moo_accel_label_set_action (GTK_BIN(proxy)->child, action);
}
#if GTK_MINOR_VERSION >= 4
else if (GTK_IS_TOOL_BUTTON (proxy))
{
g_signal_connect_swapped (proxy, "clicked",
G_CALLBACK (moo_action_activate),
action);
g_object_set (proxy, "is-important",
(gboolean) (action->is_important ? TRUE : FALSE),
NULL);
}
#endif /* GTK_MINOR_VERSION >= 4 */
else if (GTK_IS_BUTTON (proxy))
{
g_signal_connect_swapped (proxy, "clicked",
G_CALLBACK (moo_action_activate),
action);
}
else
{
g_critical ("unknown proxy type");
}
g_signal_connect_swapped (action, "set-sensitive",
G_CALLBACK (gtk_widget_set_sensitive),
proxy);
g_signal_connect_swapped (action, "set-visible",
G_CALLBACK (set_visible_callback),
proxy);
g_object_weak_ref (G_OBJECT (proxy),
(GWeakNotify)proxy_destroyed,
action);
g_object_weak_ref (G_OBJECT (action),
(GWeakNotify)action_destroyed,
proxy);
}
const char*
moo_action_get_default_accel (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), "");
if (!action->default_accel)
action->default_accel = g_strdup ("");
return action->default_accel;
}
void
_moo_action_set_accel_path (MooAction *action,
const char *accel_path)
{
g_return_if_fail (MOO_IS_ACTION (action));
g_free (action->accel_path);
action->accel_path = g_strdup (accel_path);
}
const char*
_moo_action_get_accel_path (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), NULL);
return action->accel_path;
}
const char*
_moo_action_get_accel (MooAction *action)
{
g_return_val_if_fail (MOO_IS_ACTION (action), "");
g_return_val_if_fail (action->accel_path != NULL, "");
return moo_get_accel (action->accel_path);
}