A model-view separation for menus and toolbars, using the EggMenu code by

2003-08-24  Matthias Clasen  <maclas@gmx.de>

	* gtk/gtkaction.[ch]:
	* gtk/gtktoggleaction.[ch]:
	* gtk/gtktoggleactionprivate.h:
	* gtk/gtkradioaction.[ch]:
	* gtk/gtkactiongroup.[ch]:
	* gtk/gtkmenumerge.[ch]: A model-view separation for menus and
	toolbars, using the EggMenu code by James Henstridge.

	* gtk/gtk.h: Include new headers.
	* gtk/Makefile.am: Add new files.

	* tests/testactions.c: Test for actions.
	* tests/testmerge.c: Test for menu merging.
	* tests/merge-[123].ui: Test data for testmerge.
	* tests/Makefile.am: Add testactions and testmerge.

	* demos/gtk-demo/appwindow.c: Use GtkMenuMerge to construct the
	menubar and toolbar.
This commit is contained in:
Matthias Clasen 2003-08-24 19:58:30 +00:00 committed by Matthias Clasen
parent 9897d659c0
commit e7e395652c
25 changed files with 4928 additions and 116 deletions

View File

@ -1,3 +1,24 @@
2003-08-24 Matthias Clasen <maclas@gmx.de>
* gtk/gtkaction.[ch]:
* gtk/gtktoggleaction.[ch]:
* gtk/gtktoggleactionprivate.h:
* gtk/gtkradioaction.[ch]:
* gtk/gtkactiongroup.[ch]:
* gtk/gtkmenumerge.[ch]: A model-view separation for menus and
toolbars, using the EggMenu code by James Henstridge.
* gtk/gtk.h: Include new headers.
* gtk/Makefile.am: Add new files.
* tests/testactions.c: Test for actions.
* tests/testmerge.c: Test for menu merging.
* tests/merge-[123].ui: Test data for testmerge.
* tests/Makefile.am: Add testactions and testmerge.
* demos/gtk-demo/appwindow.c: Use GtkMenuMerge to construct the
menubar and toolbar.
Sat Aug 23 21:40:18 2003 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c (gtk_rc_context_parse_one_file): Fix

View File

@ -1,3 +1,24 @@
2003-08-24 Matthias Clasen <maclas@gmx.de>
* gtk/gtkaction.[ch]:
* gtk/gtktoggleaction.[ch]:
* gtk/gtktoggleactionprivate.h:
* gtk/gtkradioaction.[ch]:
* gtk/gtkactiongroup.[ch]:
* gtk/gtkmenumerge.[ch]: A model-view separation for menus and
toolbars, using the EggMenu code by James Henstridge.
* gtk/gtk.h: Include new headers.
* gtk/Makefile.am: Add new files.
* tests/testactions.c: Test for actions.
* tests/testmerge.c: Test for menu merging.
* tests/merge-[123].ui: Test data for testmerge.
* tests/Makefile.am: Add testactions and testmerge.
* demos/gtk-demo/appwindow.c: Use GtkMenuMerge to construct the
menubar and toolbar.
Sat Aug 23 21:40:18 2003 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c (gtk_rc_context_parse_one_file): Fix

View File

@ -1,3 +1,24 @@
2003-08-24 Matthias Clasen <maclas@gmx.de>
* gtk/gtkaction.[ch]:
* gtk/gtktoggleaction.[ch]:
* gtk/gtktoggleactionprivate.h:
* gtk/gtkradioaction.[ch]:
* gtk/gtkactiongroup.[ch]:
* gtk/gtkmenumerge.[ch]: A model-view separation for menus and
toolbars, using the EggMenu code by James Henstridge.
* gtk/gtk.h: Include new headers.
* gtk/Makefile.am: Add new files.
* tests/testactions.c: Test for actions.
* tests/testmerge.c: Test for menu merging.
* tests/merge-[123].ui: Test data for testmerge.
* tests/Makefile.am: Add testactions and testmerge.
* demos/gtk-demo/appwindow.c: Use GtkMenuMerge to construct the
menubar and toolbar.
Sat Aug 23 21:40:18 2003 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c (gtk_rc_context_parse_one_file): Fix

View File

@ -1,3 +1,24 @@
2003-08-24 Matthias Clasen <maclas@gmx.de>
* gtk/gtkaction.[ch]:
* gtk/gtktoggleaction.[ch]:
* gtk/gtktoggleactionprivate.h:
* gtk/gtkradioaction.[ch]:
* gtk/gtkactiongroup.[ch]:
* gtk/gtkmenumerge.[ch]: A model-view separation for menus and
toolbars, using the EggMenu code by James Henstridge.
* gtk/gtk.h: Include new headers.
* gtk/Makefile.am: Add new files.
* tests/testactions.c: Test for actions.
* tests/testmerge.c: Test for menu merging.
* tests/merge-[123].ui: Test data for testmerge.
* tests/Makefile.am: Add testactions and testmerge.
* demos/gtk-demo/appwindow.c: Use GtkMenuMerge to construct the
menubar and toolbar.
Sat Aug 23 21:40:18 2003 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c (gtk_rc_context_parse_one_file): Fix

View File

@ -1,3 +1,24 @@
2003-08-24 Matthias Clasen <maclas@gmx.de>
* gtk/gtkaction.[ch]:
* gtk/gtktoggleaction.[ch]:
* gtk/gtktoggleactionprivate.h:
* gtk/gtkradioaction.[ch]:
* gtk/gtkactiongroup.[ch]:
* gtk/gtkmenumerge.[ch]: A model-view separation for menus and
toolbars, using the EggMenu code by James Henstridge.
* gtk/gtk.h: Include new headers.
* gtk/Makefile.am: Add new files.
* tests/testactions.c: Test for actions.
* tests/testmerge.c: Test for menu merging.
* tests/merge-[123].ui: Test data for testmerge.
* tests/Makefile.am: Add testactions and testmerge.
* demos/gtk-demo/appwindow.c: Use GtkMenuMerge to construct the
menubar and toolbar.
Sat Aug 23 21:40:18 2003 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c (gtk_rc_context_parse_one_file): Fix

View File

@ -8,20 +8,20 @@
static GtkWidget *window = NULL;
static void
menuitem_cb (gpointer callback_data,
guint callback_action,
GtkWidget *widget)
activate_action (GtkAction *action)
{
const gchar *name = gtk_action_get_name (action);
const gchar *typename = G_OBJECT_TYPE_NAME (action);
GtkWidget *dialog;
dialog = gtk_message_dialog_new (GTK_WINDOW (callback_data),
dialog = gtk_message_dialog_new (GTK_WINDOW (window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
"You selected or toggled the menu item: \"%s\"",
gtk_item_factory_path_from_widget (widget));
"You activated action: \"%s\" of type \"%s\"",
name, typename);
/* Close dialog on user response */
g_signal_connect (dialog,
@ -33,53 +33,71 @@ menuitem_cb (gpointer callback_data,
}
static GtkItemFactoryEntry menu_items[] =
{
{ "/_File", NULL, 0, 0, "<Branch>" },
{ "/File/_New", "<control>N", menuitem_cb, 0, "<StockItem>", GTK_STOCK_NEW },
{ "/File/_Open", "<control>O", menuitem_cb, 0, "<StockItem>", GTK_STOCK_OPEN },
{ "/File/_Save", "<control>S", menuitem_cb, 0, "<StockItem>", GTK_STOCK_SAVE },
{ "/File/Save _As...", NULL, menuitem_cb, 0, "<StockItem>", GTK_STOCK_SAVE },
{ "/File/sep1", NULL, menuitem_cb, 0, "<Separator>" },
{ "/File/_Quit", "<control>Q", menuitem_cb, 0, "<StockItem>", GTK_STOCK_QUIT },
#ifndef N_
#define N_(String) String
#endif
{ "/_Preferences", NULL, 0, 0, "<Branch>" },
{ "/_Preferences/_Color", NULL, 0, 0, "<Branch>" },
{ "/_Preferences/Color/_Red", NULL, menuitem_cb, 0, "<RadioItem>" },
{ "/_Preferences/Color/_Green", NULL, menuitem_cb, 0, "/Preferences/Color/Red" },
{ "/_Preferences/Color/_Blue", NULL, menuitem_cb, 0, "/Preferences/Color/Red" },
{ "/_Preferences/_Shape", NULL, 0, 0, "<Branch>" },
{ "/_Preferences/Shape/_Square", NULL, menuitem_cb, 0, "<RadioItem>" },
{ "/_Preferences/Shape/_Rectangle", NULL, menuitem_cb, 0, "/Preferences/Shape/Square" },
{ "/_Preferences/Shape/_Oval", NULL, menuitem_cb, 0, "/Preferences/Shape/Rectangle" },
static GtkActionGroupEntry entries[] = {
{ "FileMenu", N_("_File"), NULL, NULL, NULL, NULL, NULL },
{ "PreferencesMenu", N_("_Preferences"), NULL, NULL, NULL, NULL, NULL },
{ "ColorMenu", N_("_Color"), NULL, NULL, NULL, NULL, NULL },
{ "ShapeMenu", N_("_Shape"), NULL, NULL, NULL, NULL, NULL },
{ "HelpMenu", N_("_Help"), NULL, NULL, NULL, NULL, NULL },
/* If you wanted this to be right justified you would use "<LastBranch>", not "<Branch>".
* Right justified help menu items are generally considered a bad idea now days.
*/
{ "/_Help", NULL, 0, 0, "<Branch>" },
{ "/Help/_About", NULL, menuitem_cb, 0 },
{ "New", N_("_New"), GTK_STOCK_NEW, "<control>N", N_("Create a new file"), G_CALLBACK (activate_action), NULL },
{ "Open", N_("_Open"), GTK_STOCK_OPEN, "<control>O", N_("Open a file"), G_CALLBACK (activate_action), NULL },
{ "Save", N_("_Save"), GTK_STOCK_SAVE, "<control>S", N_("Save current file"), G_CALLBACK (activate_action), NULL },
{ "SaveAs", N_("Save _As..."), GTK_STOCK_SAVE, NULL, N_("Save to a file"), G_CALLBACK (activate_action), NULL },
{ "Quit", N_("_Quit"), GTK_STOCK_QUIT, "<control>Q", N_("Quit"), G_CALLBACK (activate_action), NULL },
{ "Red", N_("_Red"), NULL, "<control>R", N_("Blood"), G_CALLBACK (activate_action), NULL, RADIO_ACTION },
{ "Green", N_("_Green"), NULL, "<control>G", N_("Grass"), G_CALLBACK (activate_action), NULL, RADIO_ACTION, "Red" },
{ "Blue", N_("_Blue"), NULL, "<control>B", N_("Sky"), G_CALLBACK (activate_action), NULL, RADIO_ACTION, "Red" },
{ "Square", N_("_Square"), NULL, "<control>S", N_("Square"), G_CALLBACK (activate_action), NULL, RADIO_ACTION },
{ "Rectangle", N_("_Rectangle"), NULL, "<control>R", N_("Rectangle"), G_CALLBACK (activate_action), NULL, RADIO_ACTION, "Square" },
{ "Oval", N_("_Oval"), NULL, "<control>O", N_("Egg"), G_CALLBACK (activate_action), NULL, RADIO_ACTION, "Square" },
{ "About", N_("_About"), NULL, "<control>A", N_("About"), G_CALLBACK (activate_action), NULL },
{ "Logo", NULL, "demo-gtk-logo", NULL, N_("GTK+"), G_CALLBACK (activate_action), NULL },
};
static guint n_entries = G_N_ELEMENTS (entries);
static void
toolbar_cb (GtkWidget *button,
gpointer data)
{
GtkWidget *dialog;
static const gchar *ui_info =
"<Root>\n"
" <menu>\n"
" <submenu name='FileMenu'>\n"
" <menuitem name='New'/>\n"
" <menuitem name='Open'/>\n"
" <menuitem name='Save'/>\n"
" <menuitem name='SaveAs'/>\n"
" <separator name='Sep1'/>\n"
" <menuitem name='Quit'/>\n"
" </submenu>\n"
" <submenu name='PreferencesMenu'>\n"
" <submenu name='ColorMenu'>\n"
" <menuitem name='Red'/>\n"
" <menuitem name='Green'/>\n"
" <menuitem name='Blue'/>\n"
" </submenu>\n"
" <submenu name='ShapeMenu'>\n"
" <menuitem name='Square'/>\n"
" <menuitem name='Rectangle'/>\n"
" <menuitem name='Oval'/>\n"
" </submenu>\n"
" </submenu>\n"
" <submenu name='HelpMenu'>\n"
" <menuitem name='About'/>\n"
" </submenu>\n"
" </menu>\n"
" <dockitem>\n"
" <toolitem name='Open'/>\n"
" <toolitem name='Quit'/>\n"
" <separator name='Sep1'/>\n"
" <toolitem name='Logo'/>\n"
" </dockitem>\n"
"</Root>\n";
dialog = gtk_message_dialog_new (GTK_WINDOW (data),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
"You selected a toolbar button");
/* Close dialog on user response */
g_signal_connect (dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
NULL);
gtk_widget_show (dialog);
}
/* This function registers our custom toolbar icons, so they can be themed.
*
@ -195,19 +213,47 @@ update_resize_grip (GtkWidget *widget,
}
static void
add_widget (GtkMenuMerge *merge,
GtkWidget *widget,
GtkTable *table)
{
if (GTK_IS_MENU_BAR (widget))
{
gtk_table_attach (GTK_TABLE (table),
widget,
/* X direction */ /* Y direction */
0, 1, 0, 1,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
}
else if (GTK_IS_TOOLBAR (widget))
{
gtk_table_attach (GTK_TABLE (table),
widget,
/* X direction */ /* Y direction */
0, 1, 1, 2,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
}
gtk_widget_show (widget);
}
GtkWidget *
do_appwindow (void)
{
if (!window)
{
GtkWidget *table;
GtkWidget *toolbar;
GtkWidget *statusbar;
GtkWidget *contents;
GtkWidget *sw;
GtkTextBuffer *buffer;
GtkAccelGroup *accel_group;
GtkItemFactory *item_factory;
GtkActionGroup *action_group;
GtkAction *action;
GtkMenuMerge *merge;
GError *error = NULL;
register_stock_icons ();
@ -226,70 +272,28 @@ do_appwindow (void)
gtk_container_add (GTK_CONTAINER (window), table);
/* Create the menubar
/* Create the menubar and toolbar
*/
accel_group = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
g_object_unref (accel_group);
action_group = gtk_action_group_new ("AppWindowActions");
gtk_action_group_add_actions (action_group, entries, n_entries);
item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", accel_group);
action = gtk_action_group_get_action (action_group, "red");
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
action = gtk_action_group_get_action (action_group, "square");
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
/* Set up item factory to go away with the window */
g_object_ref (item_factory);
gtk_object_sink (GTK_OBJECT (item_factory));
g_object_set_data_full (G_OBJECT (window),
"<main>",
item_factory,
(GDestroyNotify) g_object_unref);
merge = gtk_menu_merge_new ();
gtk_menu_merge_insert_action_group (merge, action_group, 0);
g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), table);
gtk_window_add_accel_group (GTK_WINDOW (window),
gtk_menu_merge_get_accel_group (merge));
/* create menu items */
gtk_item_factory_create_items (item_factory, G_N_ELEMENTS (menu_items),
menu_items, window);
gtk_table_attach (GTK_TABLE (table),
gtk_item_factory_get_widget (item_factory, "<main>"),
/* X direction */ /* Y direction */
0, 1, 0, 1,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
/* Create the toolbar
*/
toolbar = gtk_toolbar_new ();
gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
GTK_STOCK_OPEN,
"This is a demo button with an 'open' icon",
NULL,
G_CALLBACK (toolbar_cb),
window, /* user data for callback */
-1); /* -1 means "append" */
gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
GTK_STOCK_QUIT,
"This is a demo button with a 'quit' icon",
NULL,
G_CALLBACK (toolbar_cb),
window, /* user data for callback */
-1); /* -1 means "append" */
gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
"demo-gtk-logo",
"This is a demo button with a 'gtk' icon",
NULL,
G_CALLBACK (toolbar_cb),
window, /* user data for callback */
-1); /* -1 means "append" */
gtk_table_attach (GTK_TABLE (table),
toolbar,
/* X direction */ /* Y direction */
0, 1, 1, 2,
GTK_EXPAND | GTK_FILL, 0,
0, 0);
if (!gtk_menu_merge_add_ui_from_string (merge, ui_info, -1, &error))
{
g_message ("building menus failed: %s", error->message);
g_error_free (error);
}
/* Create document
*/

View File

@ -91,6 +91,8 @@ gtk_public_h_sources = \
gtkaccellabel.h \
gtkaccelmap.h \
gtkaccessible.h \
gtkaction.h \
gtkactiongroup.h \
gtkadjustment.h \
gtkalignment.h \
gtkarrow.h \
@ -165,6 +167,7 @@ gtk_public_h_sources = \
gtkmenu.h \
gtkmenubar.h \
gtkmenuitem.h \
gtkmenumerge.h \
gtkmenushell.h \
gtkmessagedialog.h \
gtkmisc.h \
@ -179,11 +182,13 @@ gtk_public_h_sources = \
gtkprivate.h \
gtkprogress.h \
gtkprogressbar.h \
gtkradioaction.h \
gtkradiobutton.h \
gtkradiomenuitem.h \
gtkradiotoolbutton.h \
gtkrange.h \
gtkrc.h \
gtkresizegrip.h \
gtkruler.h \
gtkscale.h \
gtkscrollbar.h \
@ -213,6 +218,7 @@ gtk_public_h_sources = \
gtktexttagtable.h \
gtktextview.h \
gtktipsquery.h \
gtktoggleaction.h \
gtktogglebutton.h \
gtktoggletoolbutton.h \
gtktoolbar.h \
@ -257,13 +263,16 @@ gtk_private_h_sources = \
gtkthemes.h \
gtktreedatalist.h \
gtktreeprivate.h \
gtkwindow-decorate.h
gtkwindow-decorate.h \
gtktoggleactionprivate.h
# GTK+ C sources to build the library from
gtk_c_sources = \
gtkaccelgroup.c \
gtkaccelmap.c \
gtkaccellabel.c \
gtkaction.c \
gtkactiongroup.c \
gtkradiotoolbutton.c \
gtktoggletoolbutton.c \
gtktoolbar.c \
@ -350,6 +359,7 @@ gtk_c_sources = \
gtkmarshalers.c \
gtkmarshal.c \
gtkmenu.c \
gtkmenumerge.c \
gtkmenubar.c \
gtkmenuitem.c \
gtkmenushell.c \
@ -364,11 +374,13 @@ gtk_c_sources = \
gtkpreview.c \
gtkprogress.c \
gtkprogressbar.c \
gtkradioaction.c \
gtkradiobutton.c \
gtkradiomenuitem.c \
gtkrange.c \
gtkrbtree.c \
gtkrc.c \
gtkresizegrip.c \
gtkruler.c \
gtkscale.c \
gtkscrollbar.c \
@ -402,6 +414,7 @@ gtk_c_sources = \
gtktextview.c \
gtkthemes.c \
gtktipsquery.c \
gtktoggleaction.c \
gtktogglebutton.c \
gtktooltips.c \
gtktree.c \

View File

@ -33,6 +33,8 @@
#include <gtk/gtkaccellabel.h>
#include <gtk/gtkaccelmap.h>
#include <gtk/gtkaccessible.h>
#include <gtk/gtkaction.h>
#include <gtk/gtkactiongroup.h>
#include <gtk/gtkadjustment.h>
#include <gtk/gtkalignment.h>
#include <gtk/gtkarrow.h>
@ -103,6 +105,7 @@
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenubar.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkmenumerge.h>
#include <gtk/gtkmenushell.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkmisc.h>
@ -116,11 +119,13 @@
#include <gtk/gtkpreview.h>
#include <gtk/gtkprogress.h>
#include <gtk/gtkprogressbar.h>
#include <gtk/gtkradioaction.h>
#include <gtk/gtkradiobutton.h>
#include <gtk/gtkradiomenuitem.h>
#include <gtk/gtkradiotoolbutton.h>
#include <gtk/gtkrange.h>
#include <gtk/gtkrc.h>
#include <gtk/gtkresizegrip.h>
#include <gtk/gtkruler.h>
#include <gtk/gtkscale.h>
#include <gtk/gtkscrollbar.h>
@ -143,6 +148,7 @@
#include <gtk/gtktextbuffer.h>
#include <gtk/gtktextview.h>
#include <gtk/gtktipsquery.h>
#include <gtk/gtktoggleaction.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtktoggletoolbutton.h>
#include <gtk/gtktoolbar.h>

902
gtk/gtkaction.c Normal file
View File

@ -0,0 +1,902 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <config.h>
#include "gtkaction.h"
#include "gtktoolbutton.h"
#include "gtkmenuitem.h"
#include "gtkimagemenuitem.h"
#include "gtkstock.h"
#include "gtklabel.h"
#include "gtkimage.h"
#include "gtkaccellabel.h"
#include "gtkintl.h"
#define GTK_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION, GtkActionPrivate))
struct _GtkActionPrivate
{
gchar *name;
gchar *label;
gchar *short_label;
gchar *tooltip;
gchar *stock_id; /* icon */
guint sensitive : 1;
guint visible : 1;
guint label_set : 1; /* these two used so we can set label */
guint short_label_set : 1; /* based on stock id */
/* accelerator */
GQuark accel_quark;
/* list of proxy widgets */
GSList *proxies;
};
enum
{
ACTIVATE,
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_NAME,
PROP_LABEL,
PROP_SHORT_LABEL,
PROP_TOOLTIP,
PROP_STOCK_ID,
PROP_SENSITIVE,
PROP_VISIBLE,
};
static void gtk_action_init (GtkAction *action);
static void gtk_action_class_init (GtkActionClass *class);
static GQuark accel_path_id = 0;
static const gchar *accel_path_key = "GtkAction::accel_path";
GType
gtk_action_get_type (void)
{
static GtkType type = 0;
if (!type)
{
static const GTypeInfo type_info =
{
sizeof (GtkActionClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gtk_action_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (GtkAction),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_action_init,
};
type = g_type_register_static (G_TYPE_OBJECT,
"GtkAction",
&type_info, 0);
}
return type;
}
static void gtk_action_finalize (GObject *object);
static void gtk_action_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_action_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static GtkWidget *create_menu_item (GtkAction *action);
static GtkWidget *create_tool_item (GtkAction *action);
static void connect_proxy (GtkAction *action,
GtkWidget *proxy);
static void disconnect_proxy (GtkAction *action,
GtkWidget *proxy);
static GObjectClass *parent_class = NULL;
static guint action_signals[LAST_SIGNAL] = { 0 };
static void
gtk_action_class_init (GtkActionClass *klass)
{
GObjectClass *gobject_class;
accel_path_id = g_quark_from_static_string (accel_path_key);
parent_class = g_type_class_peek_parent (klass);
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = gtk_action_finalize;
gobject_class->set_property = gtk_action_set_property;
gobject_class->get_property = gtk_action_get_property;
klass->activate = NULL;
klass->create_menu_item = create_menu_item;
klass->create_tool_item = create_tool_item;
klass->connect_proxy = connect_proxy;
klass->disconnect_proxy = disconnect_proxy;
klass->menu_item_type = GTK_TYPE_IMAGE_MENU_ITEM;
klass->toolbar_item_type = GTK_TYPE_TOOL_BUTTON;
g_object_class_install_property (gobject_class,
PROP_NAME,
g_param_spec_string ("name",
_("Name"),
_("A unique name for the action."),
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (gobject_class,
PROP_LABEL,
g_param_spec_string ("label",
_("Label"),
_("The label used for menu items and buttons that activate this action."),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_SHORT_LABEL,
g_param_spec_string ("short_label",
_("Short label"),
_("A shorter label that may be used on toolbar buttons."),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_TOOLTIP,
g_param_spec_string ("tooltip",
_("Tooltip"),
_("A tooltip for this action."),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_STOCK_ID,
g_param_spec_string ("stock_id",
_("Stock Icon"),
_("The stock icon displayed in widgets representing this action."),
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_SENSITIVE,
g_param_spec_boolean ("sensitive",
_("Sensitive"),
_("Whether the action is enabled."),
TRUE,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_VISIBLE,
g_param_spec_boolean ("visible",
_("Visible"),
_("Whether the action is visible."),
TRUE,
G_PARAM_READWRITE));
action_signals[ACTIVATE] =
g_signal_new ("activate",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
G_STRUCT_OFFSET (GtkActionClass, activate), NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (gobject_class, sizeof (GtkActionPrivate));
}
static void
gtk_action_init (GtkAction *action)
{
action->private_data = GTK_ACTION_GET_PRIVATE (action);
action->private_data->name = NULL;
action->private_data->label = NULL;
action->private_data->short_label = NULL;
action->private_data->tooltip = NULL;
action->private_data->stock_id = NULL;
action->private_data->sensitive = TRUE;
action->private_data->visible = TRUE;
action->private_data->label_set = FALSE;
action->private_data->short_label_set = FALSE;
action->private_data->accel_quark = 0;
action->private_data->proxies = NULL;
}
static void
gtk_action_finalize (GObject *object)
{
GtkAction *action;
action = GTK_ACTION (object);
g_free (action->private_data->name);
g_free (action->private_data->label);
g_free (action->private_data->short_label);
g_free (action->private_data->tooltip);
g_free (action->private_data->stock_id);
}
static void
gtk_action_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkAction *action;
gchar *tmp;
action = GTK_ACTION (object);
switch (prop_id)
{
case PROP_NAME:
tmp = action->private_data->name;
action->private_data->name = g_value_dup_string (value);
g_free (tmp);
break;
case PROP_LABEL:
tmp = action->private_data->label;
action->private_data->label = g_value_dup_string (value);
g_free (tmp);
action->private_data->label_set = (action->private_data->label != NULL);
/* if label is unset, then use the label from the stock item */
if (!action->private_data->label_set && action->private_data->stock_id)
{
GtkStockItem stock_item;
if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
action->private_data->label = g_strdup (stock_item.label);
}
/* if short_label is unset, set short_label=label */
if (!action->private_data->short_label_set)
{
tmp = action->private_data->short_label;
action->private_data->short_label = g_strdup (action->private_data->label);
g_free (tmp);
g_object_notify (object, "short_label");
}
break;
case PROP_SHORT_LABEL:
tmp = action->private_data->short_label;
action->private_data->short_label = g_value_dup_string (value);
g_free (tmp);
action->private_data->short_label_set = (action->private_data->short_label != NULL);
/* if short_label is unset, then use the value of label */
if (!action->private_data->short_label_set)
{
action->private_data->short_label = g_strdup (action->private_data->label);
}
break;
case PROP_TOOLTIP:
tmp = action->private_data->tooltip;
action->private_data->tooltip = g_value_dup_string (value);
g_free (tmp);
break;
case PROP_STOCK_ID:
tmp = action->private_data->stock_id;
action->private_data->stock_id = g_value_dup_string (value);
g_free (tmp);
/* update label and short_label if appropriate */
if (!action->private_data->label_set)
{
GtkStockItem stock_item;
g_free (action->private_data->label);
if (gtk_stock_lookup (action->private_data->stock_id, &stock_item))
action->private_data->label = g_strdup (stock_item.label);
else
action->private_data->label = NULL;
g_object_notify (object, "label");
}
if (!action->private_data->short_label_set)
{
tmp = action->private_data->short_label;
action->private_data->short_label = g_strdup (action->private_data->label);
g_free (tmp);
g_object_notify (object, "short_label");
}
break;
case PROP_SENSITIVE:
action->private_data->sensitive = g_value_get_boolean (value);
break;
case PROP_VISIBLE:
action->private_data->visible = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_action_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkAction *action;
action = GTK_ACTION (object);
switch (prop_id)
{
case PROP_NAME:
g_value_set_string (value, action->private_data->name);
break;
case PROP_LABEL:
g_value_set_string (value, action->private_data->label);
break;
case PROP_SHORT_LABEL:
g_value_set_string (value, action->private_data->short_label);
break;
case PROP_TOOLTIP:
g_value_set_string (value, action->private_data->tooltip);
break;
case PROP_STOCK_ID:
g_value_set_string (value, action->private_data->stock_id);
break;
case PROP_SENSITIVE:
g_value_set_boolean (value, action->private_data->sensitive);
break;
case PROP_VISIBLE:
g_value_set_boolean (value, action->private_data->visible);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GtkWidget *
create_menu_item (GtkAction *action)
{
GType menu_item_type;
menu_item_type = GTK_ACTION_GET_CLASS (action)->menu_item_type;
return g_object_new (menu_item_type, NULL);
}
static GtkWidget *
create_tool_item (GtkAction *action)
{
GType toolbar_item_type;
toolbar_item_type = GTK_ACTION_GET_CLASS (action)->toolbar_item_type;
return g_object_new (toolbar_item_type, NULL);
}
static void
gtk_action_remove_proxy (GtkWidget *widget,
GtkAction *action)
{
action->private_data->proxies = g_slist_remove (action->private_data->proxies, widget);
}
static void
gtk_action_sync_property (GtkAction *action,
GParamSpec *pspec,
GtkWidget *proxy)
{
const gchar *property;
GValue value = { 0, };
property = g_param_spec_get_name (pspec);
g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
g_object_get_property (G_OBJECT (action), property, &value);
g_object_set_property (G_OBJECT (proxy), property, &value);
g_value_unset (&value);
}
static void
gtk_action_sync_label (GtkAction *action,
GParamSpec *pspec,
GtkWidget *proxy)
{
GtkWidget *label = NULL;
g_return_if_fail (GTK_IS_MENU_ITEM (proxy));
label = GTK_BIN (proxy)->child;
if (GTK_IS_LABEL (label))
gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
}
static void
gtk_action_sync_short_label (GtkAction *action,
GParamSpec *pspec,
GtkWidget *proxy)
{
GValue value = { 0, };
g_value_init (&value, G_TYPE_STRING);
g_object_get_property (G_OBJECT (action), "short_label", &value);
g_object_set_property (G_OBJECT (proxy), "label", &value);
g_value_unset (&value);
}
static void
gtk_action_sync_stock_id (GtkAction *action,
GParamSpec *pspec,
GtkWidget *proxy)
{
GtkWidget *image = NULL;
if (GTK_IS_IMAGE_MENU_ITEM (proxy))
{
image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
if (GTK_IS_IMAGE (image))
gtk_image_set_from_stock (GTK_IMAGE (image),
action->private_data->stock_id, GTK_ICON_SIZE_MENU);
}
}
static gboolean
gtk_action_create_menu_proxy (GtkToolItem *tool_item,
GtkAction *action)
{
GtkWidget *menu_item = gtk_action_create_menu_item (action);
g_object_ref (menu_item);
gtk_object_sink (GTK_OBJECT (menu_item));
gtk_tool_item_set_proxy_menu_item (tool_item, "gtk-action-menu-item", menu_item);
g_object_unref (menu_item);
return TRUE;
}
static void
connect_proxy (GtkAction *action,
GtkWidget *proxy)
{
g_object_ref (action);
g_object_set_data_full (G_OBJECT (proxy), "gtk-action", action,
g_object_unref);
/* add this widget to the list of proxies */
action->private_data->proxies = g_slist_prepend (action->private_data->proxies, proxy);
g_signal_connect (proxy, "destroy",
G_CALLBACK (gtk_action_remove_proxy), action);
g_signal_connect_object (action, "notify::sensitive",
G_CALLBACK (gtk_action_sync_property), proxy, 0);
gtk_widget_set_sensitive (proxy, action->private_data->sensitive);
g_signal_connect_object (action, "notify::visible",
G_CALLBACK (gtk_action_sync_property), proxy, 0);
if (action->private_data->visible)
gtk_widget_show (proxy);
else
gtk_widget_hide (proxy);
if (GTK_IS_MENU_ITEM (proxy))
{
GtkWidget *label;
/* menu item specific synchronisers ... */
label = GTK_BIN (proxy)->child;
/* make sure label is a label */
if (label && !GTK_IS_LABEL (label))
{
gtk_container_remove (GTK_CONTAINER (proxy), label);
label = NULL;
}
if (!label)
{
label = g_object_new (GTK_TYPE_ACCEL_LABEL,
"use_underline", TRUE,
"xalign", 0.0,
"visible", TRUE,
"parent", proxy,
"accel_widget", proxy,
NULL);
}
gtk_label_set_label (GTK_LABEL (label), action->private_data->label);
g_signal_connect_object (action, "notify::label",
G_CALLBACK (gtk_action_sync_label), proxy, 0);
if (GTK_IS_IMAGE_MENU_ITEM (proxy))
{
GtkWidget *image;
image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (proxy));
if (image && !GTK_IS_IMAGE (image))
{
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy), NULL);
image = NULL;
}
if (!image)
{
image = gtk_image_new_from_stock (NULL,
GTK_ICON_SIZE_MENU);
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (proxy),
image);
gtk_widget_show (image);
}
gtk_image_set_from_stock (GTK_IMAGE (image),
action->private_data->stock_id, GTK_ICON_SIZE_MENU);
g_signal_connect_object (action, "notify::stock_id",
G_CALLBACK (gtk_action_sync_stock_id),
proxy, 0);
}
if (action->private_data->accel_quark)
{
gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy),
g_quark_to_string (action->private_data->accel_quark));
}
g_signal_connect_object (proxy, "activate",
G_CALLBACK (gtk_action_activate), action,
G_CONNECT_SWAPPED);
}
else if (GTK_IS_TOOL_BUTTON (proxy))
{
/* toolbar button specific synchronisers ... */
/* synchronise the label */
g_object_set (G_OBJECT (proxy),
"label", action->private_data->short_label,
"use_underline", TRUE,
NULL);
g_signal_connect_object (action, "notify::short_label",
G_CALLBACK (gtk_action_sync_short_label),
proxy, 0);
g_object_set (G_OBJECT (proxy), "stock_id", action->private_data->stock_id, NULL);
g_signal_connect_object (action, "notify::stock_id",
G_CALLBACK (gtk_action_sync_property), proxy, 0);
g_signal_connect_object (proxy, "create_menu_proxy",
G_CALLBACK (gtk_action_create_menu_proxy),
action, 0);
g_signal_connect_object (proxy, "clicked",
G_CALLBACK (gtk_action_activate), action,
G_CONNECT_SWAPPED);
}
}
static void
disconnect_proxy (GtkAction *action,
GtkWidget *proxy)
{
static guint notify_id = 0;
if (!notify_id)
notify_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
g_object_set_data (G_OBJECT (proxy), "gtk-action", NULL);
/* remove proxy from list of proxies */
g_signal_handlers_disconnect_by_func (proxy,
G_CALLBACK (gtk_action_remove_proxy),
action);
gtk_action_remove_proxy (proxy, action);
/* disconnect the activate handler */
g_signal_handlers_disconnect_by_func (proxy,
G_CALLBACK (gtk_action_activate),
action);
/* disconnect handlers for notify::* signals */
g_signal_handlers_disconnect_by_func (proxy,
G_CALLBACK (gtk_action_sync_property),
action);
g_signal_handlers_disconnect_by_func (action,
G_CALLBACK (gtk_action_sync_stock_id), proxy);
/* menu item specific synchronisers ... */
g_signal_handlers_disconnect_by_func (action,
G_CALLBACK (gtk_action_sync_label),
proxy);
gtk_menu_item_set_accel_path (GTK_MENU_ITEM (proxy), NULL);
/* toolbar button specific synchronisers ... */
g_signal_handlers_disconnect_by_func (action,
G_CALLBACK (gtk_action_sync_short_label),
proxy);
g_signal_handlers_disconnect_by_func (proxy,
G_CALLBACK (gtk_action_create_menu_proxy),
action);
}
/**
* gtk_action_activate:
* @action: the action object
*
* Emits the "activate" signal on the specified action.
* This gets called by the proxy widgets when they get activated.
*
* It can also be used to manually activate an action.
*
* Since: 2.4
*/
void
gtk_action_activate (GtkAction *action)
{
g_signal_emit (action, action_signals[ACTIVATE], 0);
}
/**
* gtk_action_create_icon:
* @action: the action object
* @icon_size: the size of the icon that should be created.
*
* This function is intended for use by action implementations to
* create icons displayed in the proxy widgets.
*
* Returns: a widget that displays the icon for this action.
*
* Since: 2.4
*/
GtkWidget *
gtk_action_create_icon (GtkAction *action, GtkIconSize icon_size)
{
g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
if (action->private_data->stock_id)
return gtk_image_new_from_stock (action->private_data->stock_id, icon_size);
else
return NULL;
}
/**
* gtk_action_create_menu_item:
* @action: the action object
*
* Creates a menu item widget that proxies for the given action.
*
* Returns: a menu item connected to the action.
*
* Since: 2.4
*/
GtkWidget *
gtk_action_create_menu_item (GtkAction *action)
{
GtkWidget *menu_item;
g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
menu_item = (* GTK_ACTION_GET_CLASS (action)->create_menu_item) (action);
(* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, menu_item);
return menu_item;
}
/**
* gtk_action_create_tool_item:
* @action: the action object
*
* Creates a toolbar item widget that proxies for the given action.
*
* Returns: a toolbar item connected to the action.
*
* Since: 2.4
*/
GtkWidget *
gtk_action_create_tool_item (GtkAction *action)
{
GtkWidget *button;
g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
button = (* GTK_ACTION_GET_CLASS (action)->create_tool_item) (action);
(* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, button);
return button;
}
/**
* gtk_action_connect_proxy:
* @action: the action object
* @proxy: the proxy widget
*
* Connects a widget to an action object as a proxy. Synchronises
* various properties of the action with the widget (such as label
* text, icon, tooltip, etc), and attaches a callback so that the
* action gets activated when the proxy widget does.
*
* If the widget is already connected to an action, it is disconnected
* first.
*
* Since: 2.4
*/
void
gtk_action_connect_proxy (GtkAction *action,
GtkWidget *proxy)
{
GtkAction *prev_action;
g_return_if_fail (GTK_IS_ACTION (action));
g_return_if_fail (GTK_IS_WIDGET (proxy));
prev_action = g_object_get_data (G_OBJECT (proxy), "gtk-action");
if (prev_action)
{
(* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
}
(* GTK_ACTION_GET_CLASS (action)->connect_proxy) (action, proxy);
}
/**
* gtk_action_disconnect_proxy:
* @action: the action object
* @proxy: the proxy widget
*
* Disconnects a proxy widget from an action.
* Does <emphasis>not</emphasis> destroy the widget, however.
*
* Since: 2.4
*/
void
gtk_action_disconnect_proxy (GtkAction *action,
GtkWidget *proxy)
{
g_return_if_fail (GTK_IS_ACTION (action));
g_return_if_fail (GTK_IS_WIDGET (proxy));
g_return_if_fail (g_object_get_data (G_OBJECT (proxy), "gtk-action") != action);
(* GTK_ACTION_GET_CLASS (action)->disconnect_proxy) (action, proxy);
}
/**
* gtk_action_get_proxies:
* @action: the action object
*
* Returns the proxy widgets for an action.
*
* Return value: a #GSList of proxy widgets. The list is owned by the action and
* must not be modified.
*
* Since: 2.4
**/
GSList*
gtk_action_get_proxies (GtkAction *action)
{
g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
return action->private_data->proxies;
}
/**
* gtk_action_get_name:
* @action: the action object
*
* Returns the name of the action.
*
* Return value: the name of the action. The string belongs to GTK+ and should not
* be freed.
*
* Since: 2.4
**/
const gchar *
gtk_action_get_name (GtkAction *action)
{
g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
return action->private_data->name;
}
/**
* gtk_action_block_activate_from:
* @action: the action object
* @proxy: a proxy widget
*
* Disables calls to the gtk_action_activate()
* function by signals on the given proxy widget. This is used to
* break notification loops for things like check or radio actions.
*
* This function is intended for use by action implementations.
*
* Since: 2.4
*/
void
gtk_action_block_activate_from (GtkAction *action,
GtkWidget *proxy)
{
g_return_if_fail (GTK_IS_ACTION (action));
g_signal_handlers_block_by_func (proxy, G_CALLBACK (gtk_action_activate),
action);
}
/**
* gtk_action_unblock_activate_from:
* @action: the action object
* @proxy: a proxy widget
*
* Re-enables calls to the gtk_action_activate()
* function by signals on the given proxy widget. This undoes the
* blocking done by gtk_action_block_activate_from().
*
* This function is intended for use by action implementations.
*
* Since: 2.4
*/
void
gtk_action_unblock_activate_from (GtkAction *action,
GtkWidget *proxy)
{
g_return_if_fail (GTK_IS_ACTION (action));
g_signal_handlers_unblock_by_func (proxy, G_CALLBACK (gtk_action_activate),
action);
}
/**
* gtk_action_set_accel_path:
* @action: the action object
* @accel_path: the accelerator path
*
* Sets the accel path for this action. All proxy widgets associated
* with the action will have this accel path, so that their
* accelerators are consistent.
*
* Since: 2.4
*/
void
gtk_action_set_accel_path (GtkAction *action,
const gchar *accel_path)
{
action->private_data->accel_quark = g_quark_from_string (accel_path);
}

107
gtk/gtkaction.h Normal file
View File

@ -0,0 +1,107 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_ACTION_H__
#define __GTK_ACTION_H__
#include <gtk/gtkwidget.h>
#include <glib-object.h>
#define GTK_TYPE_ACTION (gtk_action_get_type ())
#define GTK_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ACTION, GtkAction))
#define GTK_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_ACTION, GtkActionClass))
#define GTK_IS_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ACTION))
#define GTK_IS_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), GTK_TYPE_ACTION))
#define GTK_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_ACTION, GtkActionClass))
typedef struct _GtkAction GtkAction;
typedef struct _GtkActionClass GtkActionClass;
typedef struct _GtkActionPrivate GtkActionPrivate;
struct _GtkAction
{
GObject object;
/*< private >*/
GtkActionPrivate *private_data;
};
struct _GtkActionClass
{
GObjectClass parent_class;
/* activation signal */
void (* activate) (GtkAction *action);
GType menu_item_type;
GType toolbar_item_type;
/* widget creation routines (not signals) */
GtkWidget *(* create_menu_item) (GtkAction *action);
GtkWidget *(* create_tool_item) (GtkAction *action);
void (* connect_proxy) (GtkAction *action,
GtkWidget *proxy);
void (* disconnect_proxy) (GtkAction *action,
GtkWidget *proxy);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_action_get_type (void);
const gchar* gtk_action_get_name (GtkAction *action);
void gtk_action_activate (GtkAction *action);
GtkWidget *gtk_action_create_icon (GtkAction *action,
GtkIconSize icon_size);
GtkWidget *gtk_action_create_menu_item (GtkAction *action);
GtkWidget *gtk_action_create_tool_item (GtkAction *action);
void gtk_action_connect_proxy (GtkAction *action,
GtkWidget *proxy);
void gtk_action_disconnect_proxy (GtkAction *action,
GtkWidget *proxy);
GSList *gtk_action_get_proxies (GtkAction *action);
/* protected ... for use by child actions */
void gtk_action_block_activate_from (GtkAction *action,
GtkWidget *proxy);
void gtk_action_unblock_activate_from (GtkAction *action,
GtkWidget *proxy);
/* protected ... for use by action groups */
void gtk_action_set_accel_path (GtkAction *action,
const gchar *accel_path);
#endif /* __GTK_ACTION_H__ */

356
gtk/gtkactiongroup.c Normal file
View File

@ -0,0 +1,356 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <config.h>
#include "gtkactiongroup.h"
#include "gtktoggleaction.h"
#include "gtkradioaction.h"
#include "gtkaccelmap.h"
#include "gtkintl.h"
#define GTK_ACTION_GROUP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION_GROUP, GtkActionGroupPrivate))
struct _GtkActionGroupPrivate
{
gchar *name;
GHashTable *actions;
};
static void gtk_action_group_init (GtkActionGroup *self);
static void gtk_action_group_class_init (GtkActionGroupClass *class);
GType
gtk_action_group_get_type (void)
{
static GType type = 0;
if (!type)
{
static const GTypeInfo type_info =
{
sizeof (GtkActionGroupClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gtk_action_group_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkActionGroup),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_action_group_init,
};
type = g_type_register_static (G_TYPE_OBJECT, "GtkActionGroup",
&type_info, 0);
}
return type;
}
static GObjectClass *parent_class = NULL;
static void gtk_action_group_finalize (GObject *object);
static GtkAction *gtk_action_group_real_get_action (GtkActionGroup *self,
const gchar *name);
static void
gtk_action_group_class_init (GtkActionGroupClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = gtk_action_group_finalize;
klass->get_action = gtk_action_group_real_get_action;
g_type_class_add_private (gobject_class, sizeof (GtkActionGroupPrivate));
}
static void
gtk_action_group_init (GtkActionGroup *self)
{
self->private_data = GTK_ACTION_GROUP_GET_PRIVATE (self);
self->private_data->name = NULL;
self->private_data->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_object_unref);
}
/**
* gtk_action_group_new:
* @name: the name of the action group
*
* Creates a new #GtkActionGroup object.
*
* Returns: the new #GtkActionGroup
*
* Since: 2.4
*/
GtkActionGroup *
gtk_action_group_new (const gchar *name)
{
GtkActionGroup *self;
self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
self->private_data->name = g_strdup (name);
return self;
}
static void
gtk_action_group_finalize (GObject *object)
{
GtkActionGroup *self;
self = GTK_ACTION_GROUP (object);
g_free (self->private_data->name);
self->private_data->name = NULL;
g_hash_table_destroy (self->private_data->actions);
self->private_data->actions = NULL;
if (parent_class->finalize)
(* parent_class->finalize) (object);
}
static GtkAction *
gtk_action_group_real_get_action (GtkActionGroup *self,
const gchar *action_name)
{
return g_hash_table_lookup (self->private_data->actions, action_name);
}
/**
* gtk_action_group_get_name:
* @action_group: the action group
*
* Gets the name of the action group.
*
* Returns: the name of the action group.
*
* Since: 2.4
*/
const gchar *
gtk_action_group_get_name (GtkActionGroup *action_group)
{
g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
return action_group->private_data->name;
}
/**
* gtk_action_group_get_action:
* @action_group: the action group
* @action_name: the name of the action
*
* Looks up an action in the action group by name.
*
* Returns: the action, or %NULL if no action by that name exists
*
* Since: 2.4
*/
GtkAction *
gtk_action_group_get_action (GtkActionGroup *action_group,
const gchar *action_name)
{
g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
return (* GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action)
(action_group, action_name);
}
/**
* gtk_action_group_add_action:
* @action_group: the action group
* @action: an action
*
* Adds an action object to the action group.
*
* Since: 2.4
*/
void
gtk_action_group_add_action (GtkActionGroup *action_group,
GtkAction *action)
{
g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
g_return_if_fail (GTK_IS_ACTION (action));
g_return_if_fail (gtk_action_get_name (action) != NULL);
g_hash_table_insert (action_group->private_data->actions,
g_strdup (gtk_action_get_name (action)),
g_object_ref (action));
}
/**
* gtk_action_group_removes_action:
* @action_group: the action group
* @action: an action
*
* Removes an action object from the action group.
*
* Since: 2.4
*/
void
gtk_action_group_remove_action (GtkActionGroup *action_group,
GtkAction *action)
{
g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
g_return_if_fail (GTK_IS_ACTION (action));
g_return_if_fail (gtk_action_get_name (action) != NULL);
/* extra protection to make sure action->name is valid */
g_object_ref (action);
g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
g_object_unref (action);
}
static void
add_single_action (gpointer key,
gpointer value,
gpointer user_data)
{
GList **list = user_data;
*list = g_list_prepend (*list, value);
}
/**
* gtk_action_group_list_actions:
* @action_group: the action group
*
* Lists the actions in the action group.
*
* Returns: an allocated list of the action objects in the action group
*
* Since: 2.4
*/
GList *
gtk_action_group_list_actions (GtkActionGroup *action_group)
{
GList *actions = NULL;
g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
return g_list_reverse (actions);
}
/**
* gtk_action_group_add_actions:
* @action_group: the action group
* @entries: an array of action descriptions
* @n_entries: the number of entries
*
* This is a convenience routine to create a number of actions and add
* them to the action group. Each member of the array describes an
* action to create.
*
* Since: 2.4
*/
void
gtk_action_group_add_actions (GtkActionGroup *action_group,
GtkActionGroupEntry *entries,
guint n_entries)
{
guint i;
for (i = 0; i < n_entries; i++)
{
GtkAction *action;
GType action_type;
gchar *accel_path;
switch (entries[i].entry_type) {
case NORMAL_ACTION:
action_type = GTK_TYPE_ACTION;
break;
case TOGGLE_ACTION:
action_type = GTK_TYPE_TOGGLE_ACTION;
break;
case RADIO_ACTION:
action_type = GTK_TYPE_RADIO_ACTION;
break;
default:
g_warning ("unsupported action type");
action_type = GTK_TYPE_ACTION;
}
action = g_object_new (action_type,
"name", entries[i].name,
"label", _(entries[i].label),
"tooltip", _(entries[i].tooltip),
"stock_id", entries[i].stock_id,
NULL);
if (entries[i].entry_type == RADIO_ACTION &&
entries[i].extra_data != NULL)
{
GtkAction *radio_action;
GSList *group;
radio_action =
gtk_action_group_get_action (GTK_ACTION_GROUP (action_group),
entries[i].extra_data);
if (radio_action)
{
group = gtk_radio_action_get_group (GTK_RADIO_ACTION (radio_action));
gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
}
else
g_warning (G_STRLOC " could not look up `%s'", entries[i].extra_data);
}
if (entries[i].callback)
g_signal_connect (action, "activate",
entries[i].callback, entries[i].user_data);
/* set the accel path for the menu item */
accel_path = g_strconcat ("<Actions>/", action_group->private_data->name, "/",
entries[i].name, NULL);
if (entries[i].accelerator)
{
guint accel_key = 0;
GdkModifierType accel_mods;
gtk_accelerator_parse (entries[i].accelerator, &accel_key,
&accel_mods);
if (accel_key)
gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
}
gtk_action_set_accel_path (action, accel_path);
g_free (accel_path);
gtk_action_group_add_action (action_group, action);
g_object_unref (action);
}
}

109
gtk/gtkactiongroup.h Normal file
View File

@ -0,0 +1,109 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_ACTION_GROUP_H__
#define __GTK_ACTION_GROUP_H__
#include <gtk/gtkaction.h>
#define GTK_TYPE_ACTION_GROUP (gtk_action_group_get_type ())
#define GTK_ACTION_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ACTION_GROUP, GtkActionGroup))
#define GTK_ACTION_GROUP_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), GTK_TYPE_ACTION_GROUP, GtkActionGroupClass))
#define GTK_IS_ACTION_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ACTION_GROUP))
#define GTK_IS_ACTION_GROUP_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), GTK_TYPE_ACTION_GROUP))
#define GTK_ACTION_GROUP_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), GTK_TYPE_ACTION_GROUP, GtkActionGroupClass))
typedef struct _GtkActionGroup GtkActionGroup;
typedef struct _GtkActionGroupPrivate GtkActionGroupPrivate;
typedef struct _GtkActionGroupClass GtkActionGroupClass;
typedef struct _GtkActionGroupEntry GtkActionGroupEntry;
struct _GtkActionGroup
{
GObject parent;
/*< private >*/
GtkActionGroupPrivate *private_data;
};
struct _GtkActionGroupClass
{
GObjectClass parent_class;
GtkAction *(* get_action) (GtkActionGroup *action_group,
const gchar *action_name);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
typedef enum
{
NORMAL_ACTION,
TOGGLE_ACTION,
RADIO_ACTION
} GtkActionGroupEntryType;
struct _GtkActionGroupEntry
{
gchar *name;
gchar *label;
gchar *stock_id;
gchar *accelerator;
gchar *tooltip;
GCallback callback;
gpointer user_data;
GtkActionGroupEntryType entry_type;
gchar *extra_data;
};
GType gtk_action_group_get_type (void);
GtkActionGroup *gtk_action_group_new (const gchar *name);
const gchar *gtk_action_group_get_name (GtkActionGroup *action_group);
GtkAction *gtk_action_group_get_action (GtkActionGroup *action_group,
const gchar *action_name);
GList *gtk_action_group_list_actions (GtkActionGroup *action_group);
void gtk_action_group_add_action (GtkActionGroup *action_group,
GtkAction *action);
void gtk_action_group_remove_action (GtkActionGroup *action_group,
GtkAction *action);
void gtk_action_group_add_actions (GtkActionGroup *action_group,
GtkActionGroupEntry *entries,
guint n_entries);
#endif /* __GTK_ACTION_GROUP_H__ */

1652
gtk/gtkmenumerge.c Normal file

File diff suppressed because it is too large Load Diff

109
gtk/gtkmenumerge.h Normal file
View File

@ -0,0 +1,109 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_MENU_MERGE_H__
#define __GTK_MENU_MERGE_H__
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtkaccelgroup.h>
#include <gtk/gtkwidget.h>
#include <gtk/gtkaction.h>
#include <gtk/gtkactiongroup.h>
#define GTK_TYPE_MENU_MERGE (gtk_menu_merge_get_type ())
#define GTK_MENU_MERGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_MENU_MERGE, GtkMenuMerge))
#define GTK_MENU_MERGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_MENU_MERGE, GtkMenuMergeClass))
#define GTK_IS_MENU_MERGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_MENU_MERGE))
#define GTK_IS_MENU_MERGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), GTK_TYPE_MENU_MERGE))
#define GTK_MENU_MERGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_MENU_MERGE, GtkMenuMergeClass))
typedef struct _GtkMenuMerge GtkMenuMerge;
typedef struct _GtkMenuMergeClass GtkMenuMergeClass;
typedef struct _GtkMenuMergePrivate GtkMenuMergePrivate;
struct _GtkMenuMerge {
GObject parent;
/*< private >*/
GtkMenuMergePrivate *private_data;
};
struct _GtkMenuMergeClass {
GObjectClass parent_class;
void (* add_widget) (GtkMenuMerge *merge,
GtkWidget *widget);
void (* remove_widget) (GtkMenuMerge *merge,
GtkWidget *widget);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_menu_merge_get_type (void);
GtkMenuMerge *gtk_menu_merge_new (void);
/* these two functions will dirty all merge nodes, as they may need to
* be connected up to different actions */
void gtk_menu_merge_insert_action_group (GtkMenuMerge *self,
GtkActionGroup *action_group,
gint pos);
void gtk_menu_merge_remove_action_group (GtkMenuMerge *self,
GtkActionGroup *action_group);
GList *gtk_menu_merge_get_action_groups (GtkMenuMerge *self);
GtkAccelGroup *gtk_menu_merge_get_accel_group (GtkMenuMerge *self);
GtkWidget *gtk_menu_merge_get_widget (GtkMenuMerge *self,
const gchar *path);
/* these two functions are for adding UI elements to the merged user
* interface */
guint gtk_menu_merge_add_ui_from_string (GtkMenuMerge *self,
const gchar *buffer,
gsize length,
GError **error);
guint gtk_menu_merge_add_ui_from_file (GtkMenuMerge *self,
const gchar *filename,
GError **error);
void gtk_menu_merge_remove_ui (GtkMenuMerge *self,
guint merge_id);
gchar *gtk_menu_merge_get_ui (GtkMenuMerge *self);
#endif /* __GTK_MENU_MERGE_H__ */

240
gtk/gtkradioaction.c Normal file
View File

@ -0,0 +1,240 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <config.h>
#include "gtkradioaction.h"
#include "gtktoggleactionprivate.h"
#define GTK_RADIO_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RADIO_ACTION, GtkRadioActionPrivate))
struct _GtkRadioActionPrivate
{
GSList *group;
};
static void gtk_radio_action_init (GtkRadioAction *action);
static void gtk_radio_action_class_init (GtkRadioActionClass *class);
GType
gtk_radio_action_get_type (void)
{
static GtkType type = 0;
if (!type)
{
static const GTypeInfo type_info =
{
sizeof (GtkRadioActionClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gtk_radio_action_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (GtkRadioAction),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_radio_action_init,
};
type = g_type_register_static (GTK_TYPE_TOGGLE_ACTION,
"GtkRadioAction",
&type_info, 0);
}
return type;
}
static void gtk_radio_action_finalize (GObject *object);
static void gtk_radio_action_activate (GtkAction *action);
static GObjectClass *parent_class = NULL;
static void
gtk_radio_action_class_init (GtkRadioActionClass *klass)
{
GObjectClass *gobject_class;
GtkActionClass *action_class;
parent_class = g_type_class_peek_parent (klass);
gobject_class = G_OBJECT_CLASS (klass);
action_class = GTK_ACTION_CLASS (klass);
gobject_class->finalize = gtk_radio_action_finalize;
action_class->activate = gtk_radio_action_activate;
g_type_class_add_private (gobject_class, sizeof (GtkRadioActionPrivate));
}
static void
gtk_radio_action_init (GtkRadioAction *action)
{
action->private_data = GTK_RADIO_ACTION_GET_PRIVATE (action);
action->private_data->group = g_slist_prepend (NULL, action);
}
static void
gtk_radio_action_finalize (GObject *object)
{
GtkRadioAction *action;
GSList *tmp_list;
action = GTK_RADIO_ACTION (object);
action->private_data->group = g_slist_remove (action->private_data->group, action);
tmp_list = action->private_data->group;
while (tmp_list)
{
GtkRadioAction *tmp_action = tmp_list->data;
tmp_list = tmp_list->next;
tmp_action->private_data->group = action->private_data->group;
}
if (parent_class->finalize)
(* parent_class->finalize) (object);
}
static void
gtk_radio_action_activate (GtkAction *action)
{
GtkRadioAction *radio_action;
GtkToggleAction *toggle_action;
GtkToggleAction *tmp_action;
GSList *tmp_list;
radio_action = GTK_RADIO_ACTION (action);
toggle_action = GTK_TOGGLE_ACTION (action);
if (toggle_action->private_data->active)
{
tmp_list = radio_action->private_data->group;
while (tmp_list)
{
tmp_action = tmp_list->data;
tmp_list = tmp_list->next;
if (tmp_action->private_data->active && (tmp_action != toggle_action))
{
toggle_action->private_data->active = !toggle_action->private_data->active;
break;
}
}
}
else
{
toggle_action->private_data->active = !toggle_action->private_data->active;
tmp_list = radio_action->private_data->group;
while (tmp_list)
{
tmp_action = tmp_list->data;
tmp_list = tmp_list->next;
if (tmp_action->private_data->active && (tmp_action != toggle_action))
{
gtk_action_activate (GTK_ACTION (tmp_action));
break;
}
}
}
gtk_toggle_action_toggled (toggle_action);
}
/**
* gtk_radio_action_get_group:
* @action: the action object
*
* Returns the list representing the radio group for this object
*
* Returns: the list representing the radio group for this object
*
* Since: 2.4
*/
GSList *
gtk_radio_action_get_group (GtkRadioAction *action)
{
g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
return action->private_data->group;
}
/**
* gtk_radio_action_set_group:
* @action: the action object
* @group: a list representing a radio group
*
* Sets the radio group for the radio action object.
*
* Since: 2.4
*/
void
gtk_radio_action_set_group (GtkRadioAction *action,
GSList *group)
{
g_return_if_fail (GTK_IS_RADIO_ACTION (action));
g_return_if_fail (!g_slist_find (group, action));
if (action->private_data->group)
{
GSList *slist;
action->private_data->group = g_slist_remove (action->private_data->group, action);
for (slist = action->private_data->group; slist; slist = slist->next)
{
GtkRadioAction *tmp_action = slist->data;
tmp_action->private_data->group = action->private_data->group;
}
}
action->private_data->group = g_slist_prepend (group, action);
if (group)
{
GSList *slist;
for (slist = action->private_data->group; slist; slist = slist->next)
{
GtkRadioAction *tmp_action = slist->data;
tmp_action->private_data->group = action->private_data->group;
}
}
else
{
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
}
}

73
gtk/gtkradioaction.h Normal file
View File

@ -0,0 +1,73 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_RADIO_ACTION_H__
#define __GTK_RADIO_ACTION_H__
#include <gtk/gtktoggleaction.h>
#define GTK_TYPE_RADIO_ACTION (gtk_radio_action_get_type ())
#define GTK_RADIO_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_RADIO_ACTION, GtkRadioAction))
#define GTK_RADIO_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_RADIO_ACTION, GtkRadioActionClass))
#define GTK_IS_RADIO_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_RADIO_ACTION))
#define GTK_IS_RADIO_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), GTK_TYPE_RADIO_ACTION))
#define GTK_RADIO_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_RADIO_ACTION, GtkRadioActionClass))
typedef struct _GtkRadioAction GtkRadioAction;
typedef struct _GtkRadioActionPrivate GtkRadioActionPrivate;
typedef struct _GtkRadioActionClass GtkRadioActionClass;
struct _GtkRadioAction
{
GtkToggleAction parent;
/*< private >*/
GtkRadioActionPrivate *private_data;
};
struct _GtkRadioActionClass
{
GtkToggleActionClass parent_class;
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_radio_action_get_type (void);
GSList *gtk_radio_action_get_group (GtkRadioAction *action);
void gtk_radio_action_set_group (GtkRadioAction *action,
GSList *group);
#endif /* __GTK_RADIO_ACTION_H__ */

246
gtk/gtktoggleaction.c Normal file
View File

@ -0,0 +1,246 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <config.h>
#include "gtktoggleaction.h"
#include "gtktoggleactionprivate.h"
#include "gtktoggletoolbutton.h"
#include "gtkcheckmenuitem.h"
enum
{
TOGGLED,
LAST_SIGNAL
};
static void gtk_toggle_action_init (GtkToggleAction *action);
static void gtk_toggle_action_class_init (GtkToggleActionClass *class);
GType
gtk_toggle_action_get_type (void)
{
static GtkType type = 0;
if (!type)
{
static const GTypeInfo type_info =
{
sizeof (GtkToggleActionClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gtk_toggle_action_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (GtkToggleAction),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_toggle_action_init,
};
type = g_type_register_static (GTK_TYPE_ACTION,
"GtkToggleAction",
&type_info, 0);
}
return type;
}
static void gtk_toggle_action_activate (GtkAction *action);
static void gtk_toggle_action_real_toggled (GtkToggleAction *action);
static void connect_proxy (GtkAction *action,
GtkWidget *proxy);
static void disconnect_proxy (GtkAction *action,
GtkWidget *proxy);
static GObjectClass *parent_class = NULL;
static guint action_signals[LAST_SIGNAL] = { 0 };
static void
gtk_toggle_action_class_init (GtkToggleActionClass *klass)
{
GObjectClass *gobject_class;
GtkActionClass *action_class;
parent_class = g_type_class_peek_parent (klass);
gobject_class = G_OBJECT_CLASS (klass);
action_class = GTK_ACTION_CLASS (klass);
action_class->activate = gtk_toggle_action_activate;
action_class->connect_proxy = connect_proxy;
action_class->disconnect_proxy = disconnect_proxy;
action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
klass->toggled = gtk_toggle_action_real_toggled;
action_signals[TOGGLED] =
g_signal_new ("toggled",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
}
static void
gtk_toggle_action_init (GtkToggleAction *action)
{
action->private_data = GTK_TOGGLE_ACTION_GET_PRIVATE (action);
action->private_data->active = FALSE;
}
static void
gtk_toggle_action_activate (GtkAction *action)
{
GtkToggleAction *toggle_action;
g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
toggle_action = GTK_TOGGLE_ACTION (action);
toggle_action->private_data->active = !toggle_action->private_data->active;
gtk_toggle_action_toggled (toggle_action);
}
static void
gtk_toggle_action_real_toggled (GtkToggleAction *action)
{
GSList *slist;
g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
for (slist = gtk_action_get_proxies (GTK_ACTION (action)); slist; slist = slist->next)
{
GtkWidget *proxy = slist->data;
gtk_action_block_activate_from (GTK_ACTION (action), proxy);
if (GTK_IS_CHECK_MENU_ITEM (proxy))
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy),
action->private_data->active);
else if (GTK_IS_TOGGLE_TOOL_BUTTON (proxy))
gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (proxy),
action->private_data->active);
else {
g_warning ("Don't know how to toggle `%s' widgets",
G_OBJECT_TYPE_NAME (proxy));
}
gtk_action_unblock_activate_from (GTK_ACTION (action), proxy);
}
}
static void
connect_proxy (GtkAction *action,
GtkWidget *proxy)
{
GtkToggleAction *toggle_action;
toggle_action = GTK_TOGGLE_ACTION (action);
/* do this before hand, so that we don't call the "activate" handler */
if (GTK_IS_MENU_ITEM (proxy))
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy),
toggle_action->private_data->active);
else if (GTK_IS_TOGGLE_TOOL_BUTTON (proxy))
gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (proxy),
toggle_action->private_data->active);
(* GTK_ACTION_CLASS (parent_class)->connect_proxy) (action, proxy);
}
static void
disconnect_proxy (GtkAction *action,
GtkWidget *proxy)
{
GtkToggleAction *toggle_action;
toggle_action = GTK_TOGGLE_ACTION (action);
(* GTK_ACTION_CLASS (parent_class)->disconnect_proxy) (action, proxy);
}
/**
* gtk_toggle_action_toggled:
* @action: the action object
*
* Emits the "toggled" signal on the toggle action.
*
* Since: 2.4
*/
void
gtk_toggle_action_toggled (GtkToggleAction *action)
{
g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
g_signal_emit (action, action_signals[TOGGLED], 0);
}
/**
* gtk_toggle_action_set_active:
* @action: the action object
* @is_active: whether the action should be checked or not
*
* Sets the checked state on the toggle action.
*
* Since: 2.4
*/
void
gtk_toggle_action_set_active (GtkToggleAction *action,
gboolean is_active)
{
g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
is_active = is_active != FALSE;
if (action->private_data->active != is_active)
{
gtk_action_activate (GTK_ACTION (action));
}
}
/**
* gtk_toggle_action_get_active:
* @action: the action object
*
* Returns: the checked state of the toggle action
*
* Since: 2.4
*/
gboolean
gtk_toggle_action_get_active (GtkToggleAction *action)
{
g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
return action->private_data->active;
}

76
gtk/gtktoggleaction.h Normal file
View File

@ -0,0 +1,76 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_TOGGLE_ACTION_H__
#define __GTK_TOGGLE_ACTION_H__
#include <gtk/gtkaction.h>
#define GTK_TYPE_TOGGLE_ACTION (gtk_toggle_action_get_type ())
#define GTK_TOGGLE_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_TOGGLE_ACTION, GtkToggleAction))
#define GTK_TOGGLE_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TOGGLE_ACTION, GtkToggleActionClass))
#define GTK_IS_TOGGLE_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_TOGGLE_ACTION))
#define GTK_IS_TOGGLE_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), GTK_TYPE_TOGGLE_ACTION))
#define GTK_TOGGLE_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_TOGGLE_ACTION, GtkToggleActionClass))
typedef struct _GtkToggleAction GtkToggleAction;
typedef struct _GtkToggleActionPrivate GtkToggleActionPrivate;
typedef struct _GtkToggleActionClass GtkToggleActionClass;
struct _GtkToggleAction
{
GtkAction parent;
/*< private >*/
GtkToggleActionPrivate *private_data;
};
struct _GtkToggleActionClass
{
GtkActionClass parent_class;
void (* toggled) (GtkToggleAction *action);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
};
GType gtk_toggle_action_get_type (void);
void gtk_toggle_action_toggled (GtkToggleAction *action);
void gtk_toggle_action_set_active (GtkToggleAction *action,
gboolean is_active);
gboolean gtk_toggle_action_get_active (GtkToggleAction *action);
#endif /* __GTK_TOGGLE_ACTION_H__ */

View File

@ -0,0 +1,42 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 1998, 1999 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Author: James Henstridge <james@daa.com.au>
*
* Modified by the GTK+ Team and others 2003. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#ifndef __GTK_TOGGLE_ACTION_PRIVATE_H__
#define __GTK_TOGGLE_ACTION_PRIVATE_H__
#define GTK_TOGGLE_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOGGLE_ACTION, GtkToggleActionPrivate))
struct _GtkToggleActionPrivate
{
guint active : 1;
};
#endif /* __GTK_TOGGLE_ACTION_PRIVATE_H__ */

View File

@ -54,7 +54,9 @@ noinst_PROGRAMS = \
pixbuf-read \
pixbuf-lowmem \
pixbuf-randomly-modified \
pixbuf-random
pixbuf-random \
testmerge \
testactions
simple_DEPENDENCIES = $(TEST_DEPS)
testicontheme_DEPENDENCIES = $(TEST_DEPS)
@ -79,6 +81,8 @@ testtreecolumns_DEPENDENCIES = $(DEPS)
testtreesort_DEPENDENCIES = $(DEPS)
treestoretest_DEPENDENCIES = $(TEST_DEPS)
testxinerama_DEPENDENCIES = $(TEST_DEPS)
testmerge_DEPENDENCIES = $(TEST_DEPS)
testactions_DEPENDENCIES = $(TEST_DEPS)
simple_LDADD = $(LDADDS)
testcalendar_LDADD = $(LDADDS)
@ -109,6 +113,8 @@ pixbuf_read_LDADD = $(LDADDS)
pixbuf_lowmem_LDADD = $(LDADDS)
pixbuf_randomly_modified_LDADD = $(LDADDS)
pixbuf_random_LDADD = $(LDADDS)
testmerge_LDADD = $(LDADDS)
testactions_LDADD = $(LDADDS)
testgtk_SOURCES = \
prop-editor.c \
@ -137,6 +143,12 @@ testsocket_child_SOURCES = \
testsocket_child.c \
testsocket_common.c
testmerge_SOURCES = \
testmerge.c
testactions_SOURCES = \
testactions.c
EXTRA_DIST = \
prop-editor.h \
testgtk.1 \
@ -152,4 +164,7 @@ EXTRA_DIST = \
test.xpm \
check-y.xpm \
check-n.xpm \
test.xpm
test.xpm \
merge-1.ui \
merge-2.ui \
merge-3.ui

20
tests/merge-1.ui Normal file
View File

@ -0,0 +1,20 @@
<!--*- xml -*-->
<Root>
<menu>
<submenu name="FileMenu" verb="StockFileMenuAction">
<menuitem name="Open" verb="OpenAction" />
</submenu>
<submenu name="EditMenu" verb="StockEditMenuAction">
<menuitem name="Cut" verb="CutAction" />
</submenu>
<placeholder name="TestPlaceholder" />
</menu>
<dockitem name="toolbar1">
<placeholder name="ToolbarPlaceholder" />
<toolitem name="NewButton" verb="NewAction" />
<toolitem name="CutButton" verb="CutAction" />
<toolitem name="CopyButton" verb="CopyAction" />
<toolitem name="PasteButton" verb="PasteAction" />
<placeholder name="JustifyToolItems"/>
</dockitem>
</Root>

27
tests/merge-2.ui Normal file
View File

@ -0,0 +1,27 @@
<!--*- xml -*-->
<Root>
<menu>
<submenu name="FileMenu" verb="StockFileMenuAction">
<menuitem name="New" verb="NewAction" pos="top" />
<separator />
<menuitem name="Quit" verb="QuitAction" />
</submenu>
<submenu name="HelpMenu" verb="StockHelpMenuAction">
<menuitem name="About" verb="AboutAction" />
</submenu>
</menu>
<dockitem name="toolbar1">
<placeholder name="ToolbarPlaceholder">
<toolitem name="Quit" verb="QuitAction" />
<separator />
</placeholder>
</dockitem>
<popups>
<popup name="FileMenu" verb="StockFileMenuAction">
<menuitem name="New" verb="NewAction" pos="top" />
<submenu name="HelpMenu" verb="StockHelpMenuAction">
<menuitem name="About" verb="AboutAction" />
</submenu>
</popup>
</popups>
</Root>

23
tests/merge-3.ui Normal file
View File

@ -0,0 +1,23 @@
<!--*- xml -*-->
<Root>
<menu>
<submenu name="FileMenu" verb="StockFileMenuAction">
<menuitem name="New" verb="New2Action" />
</submenu>
<placeholder name="TestPlaceholder">
<submenu name="Test">
<menuitem name="Cut" verb="CutAction" />
</submenu>
</placeholder>
</menu>
<dockitem name="toolbar1">
<placeholder name="JustifyToolItems">
<separator name="first-sep"/>
<toolitem name="Left" verb="justify-left"/>
<toolitem name="Centre" verb="justify-center"/>
<toolitem name="Right" verb="justify-right"/>
<toolitem name="Fill" verb="justify-fill"/>
<separator name="second-sep" />
</placeholder>
</dockitem>
</Root>

268
tests/testactions.c Normal file
View File

@ -0,0 +1,268 @@
#undef GTK_DISABLE_DEPRECATED
#include <gtk/gtk.h>
#ifndef _
# define _(String) (String)
# define N_(String) (String)
#endif
static GtkActionGroup *action_group = NULL;
static GtkToolbar *toolbar = NULL;
static void
activate_action (GtkAction *action)
{
const gchar *name = gtk_action_get_name (action);
const gchar *typename = G_OBJECT_TYPE_NAME (action);
g_message ("Action %s (type=%s) activated", name, typename);
}
static void
toggle_action (GtkAction *action)
{
const gchar *name = gtk_action_get_name (action);
const gchar *typename = G_OBJECT_TYPE_NAME (action);
g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
}
static void
toggle_cnp_actions (GtkAction *action)
{
gboolean sensitive;
sensitive = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
action = gtk_action_group_get_action (action_group, "cut");
g_object_set (action, "sensitive", sensitive, NULL);
action = gtk_action_group_get_action (action_group, "copy");
g_object_set (action, "sensitive", sensitive, NULL);
action = gtk_action_group_get_action (action_group, "paste");
g_object_set (action, "sensitive", sensitive, NULL);
action = gtk_action_group_get_action (action_group, "toggle-cnp");
if (sensitive)
g_object_set (action, "label", _("Disable Cut and paste ops"), NULL);
else
g_object_set (action, "label", _("Enable Cut and paste ops"), NULL);
}
static void
show_accel_dialog (GtkAction *action)
{
g_message ("Sorry, accel dialog not available");
}
static void
toolbar_style (GtkAction *action,
gpointer user_data)
{
GtkToolbarStyle style;
g_return_if_fail (toolbar != NULL);
style = GPOINTER_TO_INT (user_data);
gtk_toolbar_set_style (toolbar, style);
}
static void
toolbar_size (GtkAction *action,
gpointer user_data)
{
GtkIconSize size;
g_return_if_fail (toolbar != NULL);
size = GPOINTER_TO_INT (user_data);
gtk_toolbar_set_icon_size (toolbar, size);
}
/* convenience functions for declaring actions */
static GtkActionGroupEntry entries[] = {
{ "Menu1Action", N_("Menu _1"), NULL, NULL, NULL, NULL, NULL },
{ "Menu2Action", N_("Menu _2"), NULL, NULL, NULL, NULL, NULL },
{ "cut", N_("C_ut"), GTK_STOCK_CUT, "<control>X",
N_("Cut the selected text to the clipboard"),
G_CALLBACK (activate_action), NULL },
{ "copy", N_("_Copy"), GTK_STOCK_COPY, "<control>C",
N_("Copy the selected text to the clipboard"),
G_CALLBACK (activate_action), NULL },
{ "paste", N_("_Paste"), GTK_STOCK_PASTE, "<control>V",
N_("Paste the text from the clipboard"),
G_CALLBACK (activate_action), NULL },
{ "bold", N_("_Bold"), GTK_STOCK_BOLD, "<control>B",
N_("Change to bold face"),
G_CALLBACK (toggle_action), NULL, TOGGLE_ACTION },
{ "justify-left", N_("_Left"), GTK_STOCK_JUSTIFY_LEFT, "<control>L",
N_("Left justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION },
{ "justify-center", N_("C_enter"), GTK_STOCK_JUSTIFY_CENTER, "<control>E",
N_("Center justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "justify-left" },
{ "justify-right", N_("_Right"), GTK_STOCK_JUSTIFY_RIGHT, "<control>R",
N_("Right justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "justify-left" },
{ "justify-fill", N_("_Fill"), GTK_STOCK_JUSTIFY_FILL, "<control>J",
N_("Fill justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "justify-left" },
{ "quit", NULL, GTK_STOCK_QUIT, "<control>Q",
N_("Quit the application"),
G_CALLBACK (gtk_main_quit), NULL },
{ "toggle-cnp", N_("Enable Cut/Copy/Paste"), NULL, NULL,
N_("Change the sensitivity of the cut, copy and paste actions"),
G_CALLBACK (toggle_cnp_actions), NULL, TOGGLE_ACTION },
{ "customise-accels", N_("Customise _Accels"), NULL, NULL,
N_("Customise keyboard shortcuts"),
G_CALLBACK (show_accel_dialog), NULL },
{ "toolbar-icons", N_("Icons"), NULL, NULL,
NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_ICONS),
RADIO_ACTION, NULL },
{ "toolbar-text", N_("Text"), NULL, NULL,
NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_TEXT),
RADIO_ACTION, "toolbar-icons" },
{ "toolbar-both", N_("Both"), NULL, NULL,
NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_BOTH),
RADIO_ACTION, "toolbar-icons" },
{ "toolbar-both-horiz", N_("Both Horizontal"), NULL, NULL,
NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER(GTK_TOOLBAR_BOTH_HORIZ),
RADIO_ACTION, "toolbar-icons" },
{ "toolbar-small-icons", N_("Small Icons"), NULL, NULL,
NULL,
G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_SMALL_TOOLBAR) },
{ "toolbar-large-icons", N_("Large Icons"), NULL, NULL,
NULL,
G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_LARGE_TOOLBAR) },
};
static guint n_entries = G_N_ELEMENTS (entries);
/* XML description of the menus for the test app. The parser understands
* a subset of the Bonobo UI XML format, and uses GMarkup for parsing */
static const gchar *ui_info =
"<Root>\n"
" <menu>\n"
" <submenu name=\"Menu _1\" verb=\"Menu1Action\">\n"
" <menuitem name=\"cut\" verb=\"cut\" />\n"
" <menuitem name=\"copy\" verb=\"copy\" />\n"
" <menuitem name=\"paste\" verb=\"paste\" />\n"
" <separator name=\"sep1\" />\n"
" <menuitem name=\"bold1\" verb=\"bold\" />\n"
" <menuitem name=\"bold2\" verb=\"bold\" />\n"
" <separator name=\"sep2\" />\n"
" <menuitem name=\"toggle-cnp\" verb=\"toggle-cnp\" />\n"
" <separator name=\"sep3\" />\n"
" <menuitem name=\"quit\" verb=\"quit\" />\n"
" </submenu>\n"
" <submenu name=\"Menu _2\" verb=\"Menu2Action\">\n"
" <menuitem name=\"cut\" verb=\"cut\" />\n"
" <menuitem name=\"copy\" verb=\"copy\" />\n"
" <menuitem name=\"paste\" verb=\"paste\" />\n"
" <separator name=\"sep4\"/>\n"
" <menuitem name=\"bold\" verb=\"bold\" />\n"
" <separator name=\"sep5\"/>\n"
" <menuitem name=\"justify-left\" verb=\"justify-left\" />\n"
" <menuitem name=\"justify-center\" verb=\"justify-center\" />\n"
" <menuitem name=\"justify-right\" verb=\"justify-right\" />\n"
" <menuitem name=\"justify-fill\" verb=\"justify-fill\" />\n"
" <separator name=\"sep6\"/>\n"
" <menuitem name=\"customise-accels\" verb=\"customise-accels\" />\n"
" <separator name=\"sep7\"/>\n"
" <menuitem verb=\"toolbar-icons\" />\n"
" <menuitem verb=\"toolbar-text\" />\n"
" <menuitem verb=\"toolbar-both\" />\n"
" <menuitem verb=\"toolbar-both-horiz\" />\n"
" <separator name=\"sep8\"/>\n"
" <menuitem verb=\"toolbar-small-icons\" />\n"
" <menuitem verb=\"toolbar-large-icons\" />\n"
" </submenu>\n"
" </menu>\n"
" <dockitem name=\"toolbar\">\n"
" <toolitem name=\"cut\" verb=\"cut\" />\n"
" <toolitem name=\"copy\" verb=\"copy\" />\n"
" <toolitem name=\"paste\" verb=\"paste\" />\n"
" <separator name=\"sep9\" />\n"
" <toolitem name=\"bold\" verb=\"bold\" />\n"
" <separator name=\"sep10\" />\n"
" <toolitem name=\"justify-left\" verb=\"justify-left\" />\n"
" <toolitem name=\"justify-center\" verb=\"justify-center\" />\n"
" <toolitem name=\"justify-right\" verb=\"justify-right\" />\n"
" <toolitem name=\"justify-fill\" verb=\"justify-fill\" />\n"
" <separator name=\"sep11\"/>\n"
" <toolitem name=\"quit\" verb=\"quit\" />\n"
" </dockitem>\n"
"</Root>\n";
static void
add_widget (GtkMenuMerge *merge,
GtkWidget *widget,
GtkContainer *container)
{
gtk_container_add (container, widget);
gtk_widget_show (widget);
if (GTK_IS_TOOLBAR (widget))
{
toolbar = GTK_TOOLBAR (widget);
gtk_toolbar_set_show_arrow (toolbar, TRUE);
}
}
static void
create_window (GtkActionGroup *action_group)
{
GtkMenuMerge *merge;
GtkWidget *window;
GtkWidget *box;
GError *error = NULL;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), -1, -1);
gtk_window_set_title (GTK_WINDOW (window), "Action Test");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
box = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show (box);
merge = gtk_menu_merge_new ();
gtk_menu_merge_insert_action_group (merge, action_group, 0);
g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box);
gtk_window_add_accel_group (GTK_WINDOW (window),
gtk_menu_merge_get_accel_group (merge));
if (!gtk_menu_merge_add_ui_from_string (merge, ui_info, -1, &error))
{
g_message ("building menus failed: %s", error->message);
g_error_free (error);
}
gtk_widget_show (window);
}
int
main (int argc, char **argv)
{
gtk_init (&argc, &argv);
if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR))
gtk_accel_map_load ("accels");
action_group = gtk_action_group_new ("TestActions");
gtk_action_group_add_actions (action_group, entries, n_entries);
gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (gtk_action_group_get_action (action_group, "toggle-cnp")), TRUE);
create_window (action_group);
gtk_main ();
g_object_unref (action_group);
gtk_accel_map_save ("accels");
return 0;
}

419
tests/testmerge.c Normal file
View File

@ -0,0 +1,419 @@
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#ifndef _
# define _(String) (String)
# define N_(String) (String)
#endif
struct { const gchar *filename; guint merge_id; } merge_ids[] = {
{ "merge-1.ui", 0 },
{ "merge-2.ui", 0 },
{ "merge-3.ui", 0 }
};
static void
dump_tree (GtkWidget *button,
GtkMenuMerge *merge)
{
gchar *dump;
dump = gtk_menu_merge_get_ui (merge);
g_message (dump);
g_free (dump);
}
static void
activate_action (GtkAction *action)
{
const gchar *name = gtk_action_get_name (action);
const gchar *typename = G_OBJECT_TYPE_NAME (action);
g_message ("Action %s (type=%s) activated", name, typename);
}
static void
toggle_action (GtkAction *action)
{
const gchar *name = gtk_action_get_name (action);
const gchar *typename = G_OBJECT_TYPE_NAME (action);
g_message ("Action %s (type=%s) activated (active=%d)", name, typename,
gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
}
static GtkActionGroupEntry entries[] = {
{ "StockFileMenuAction", N_("_File"), NULL, NULL, NULL, NULL, NULL },
{ "StockEditMenuAction", N_("_Edit"), NULL, NULL, NULL, NULL, NULL },
{ "StockHelpMenuAction", N_("_Help"), NULL, NULL, NULL, NULL, NULL },
{ "Test", N_("Test"), NULL, NULL, NULL, NULL, NULL },
{ "NewAction", NULL, GTK_STOCK_NEW, "<control>n", NULL,
G_CALLBACK (activate_action), NULL },
{ "New2Action", NULL, GTK_STOCK_NEW, "<control>m", NULL,
G_CALLBACK (activate_action), NULL },
{ "OpenAction", NULL, GTK_STOCK_OPEN, "<control>o", NULL,
G_CALLBACK (activate_action), NULL },
{ "QuitAction", NULL, GTK_STOCK_QUIT, "<control>q", NULL,
G_CALLBACK (gtk_main_quit), NULL },
{ "CutAction", NULL, GTK_STOCK_CUT, "<control>x", NULL,
G_CALLBACK (activate_action), NULL },
{ "CopyAction", NULL, GTK_STOCK_COPY, "<control>c", NULL,
G_CALLBACK (activate_action), NULL },
{ "PasteAction", NULL, GTK_STOCK_PASTE, "<control>v", NULL,
G_CALLBACK (activate_action), NULL },
{ "justify-left", NULL, GTK_STOCK_JUSTIFY_LEFT, "<control>L",
N_("Left justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, NULL },
{ "justify-center", NULL, GTK_STOCK_JUSTIFY_CENTER, "<control>E",
N_("Center justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "justify-left" },
{ "justify-right", NULL, GTK_STOCK_JUSTIFY_RIGHT, "<control>R",
N_("Right justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "justify-left" },
{ "justify-fill", NULL, GTK_STOCK_JUSTIFY_FILL, "<control>J",
N_("Fill justify the text"),
G_CALLBACK (toggle_action), NULL, RADIO_ACTION, "justify-left" },
{ "AboutAction", N_("_About"), NULL, NULL, NULL,
G_CALLBACK (activate_action), NULL },
};
static guint n_entries = G_N_ELEMENTS (entries);
static void
add_widget (GtkMenuMerge *merge,
GtkWidget *widget,
GtkBox *box)
{
gtk_box_pack_start (box, widget, FALSE, FALSE, 0);
gtk_widget_show (widget);
}
static void
toggle_merge (GtkWidget *button,
GtkMenuMerge *merge)
{
gint mergenum;
mergenum = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button), "mergenum"));
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
{
GError *err = NULL;
g_message ("merging %s", merge_ids[mergenum].filename);
merge_ids[mergenum].merge_id =
gtk_menu_merge_add_ui_from_file (merge, merge_ids[mergenum].filename, &err);
if (err != NULL)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (button)),
0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
"could not merge %s: %s", merge_ids[mergenum].filename,
err->message);
g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gtk_object_destroy), NULL);
gtk_widget_show (dialog);
g_clear_error (&err);
}
}
else
{
g_message ("unmerging %s (merge_id=%u)", merge_ids[mergenum].filename,
merge_ids[mergenum].merge_id);
gtk_menu_merge_remove_ui (merge, merge_ids[mergenum].merge_id);
}
}
static void
set_name_func (GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer data)
{
GtkAction *action;
char *name;
gtk_tree_model_get (tree_model, iter, 0, &action, -1);
g_object_get (G_OBJECT (action), "name", &name, NULL);
g_object_set (G_OBJECT (cell), "text", name, NULL);
g_free (name);
g_object_unref (action);
}
static void
set_sensitive_func (GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer data)
{
GtkAction *action;
gboolean sensitive;
gtk_tree_model_get (tree_model, iter, 0, &action, -1);
g_object_get (G_OBJECT (action), "sensitive", &sensitive, NULL);
g_object_set (G_OBJECT (cell), "active", sensitive, NULL);
g_object_unref (action);
}
static void
set_visible_func (GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer data)
{
GtkAction *action;
gboolean visible;
gtk_tree_model_get (tree_model, iter, 0, &action, -1);
g_object_get (G_OBJECT (action), "visible", &visible, NULL);
g_object_set (G_OBJECT (cell), "active", visible, NULL);
g_object_unref (action);
}
static void
sensitivity_toggled (GtkCellRendererToggle *cell,
const gchar *path_str,
GtkTreeModel *model)
{
GtkTreePath *path;
GtkTreeIter iter;
GtkAction *action;
gboolean sensitive;
path = gtk_tree_path_new_from_string (path_str);
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, 0, &action, -1);
g_object_get (G_OBJECT (action), "sensitive", &sensitive, NULL);
g_object_set (G_OBJECT (action), "sensitive", !sensitive, NULL);
gtk_tree_model_row_changed (model, path, &iter);
gtk_tree_path_free (path);
}
static void
visibility_toggled (GtkCellRendererToggle *cell,
const gchar *path_str,
GtkTreeModel *model)
{
GtkTreePath *path;
GtkTreeIter iter;
GtkAction *action;
gboolean visible;
path = gtk_tree_path_new_from_string (path_str);
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, 0, &action, -1);
g_object_get (G_OBJECT (action), "visible", &visible, NULL);
g_object_set (G_OBJECT (action), "visible", !visible, NULL);
gtk_tree_model_row_changed (model, path, &iter);
gtk_tree_path_free (path);
}
static gint
iter_compare_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
{
GValue a_value = { 0, }, b_value = { 0, };
GtkAction *a_action, *b_action;
const gchar *a_name, *b_name;
gint retval = 0;
gtk_tree_model_get_value (model, a, 0, &a_value);
gtk_tree_model_get_value (model, b, 0, &b_value);
a_action = GTK_ACTION (g_value_get_object (&a_value));
b_action = GTK_ACTION (g_value_get_object (&b_value));
a_name = gtk_action_get_name (a_action);
b_name = gtk_action_get_name (b_action);
if (a_name == NULL && b_name == NULL)
retval = 0;
else if (a_name == NULL)
retval = -1;
else if (b_name == NULL)
retval = 1;
else
retval = strcmp (a_name, b_name);
g_value_unset (&b_value);
g_value_unset (&a_value);
return retval;
}
static GtkWidget *
create_tree_view (GtkMenuMerge *merge)
{
GtkWidget *tree_view, *sw;
GtkListStore *store;
GList *p;
GtkCellRenderer *cell;
store = gtk_list_store_new (1, GTK_TYPE_ACTION);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store), 0,
iter_compare_func, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 0,
GTK_SORT_ASCENDING);
for (p = gtk_menu_merge_get_action_groups (merge); p; p = p->next)
{
GList *actions, *l;
actions = gtk_action_group_list_actions (p->data);
for (l = actions; l; l = l->next)
{
GtkTreeIter iter;
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, l->data, -1);
}
}
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
g_object_unref (store);
gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (tree_view),
-1, "Action",
gtk_cell_renderer_text_new (),
set_name_func, NULL, NULL);
gtk_tree_view_column_set_sort_column_id (gtk_tree_view_get_column (GTK_TREE_VIEW (tree_view), 0), 0);
cell = gtk_cell_renderer_toggle_new ();
g_signal_connect (cell, "toggled", G_CALLBACK (sensitivity_toggled), store);
gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (tree_view),
-1, "Sensitive",
cell,
set_sensitive_func, NULL, NULL);
cell = gtk_cell_renderer_toggle_new ();
g_signal_connect (cell, "toggled", G_CALLBACK (visibility_toggled), store);
gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (tree_view),
-1, "Visible",
cell,
set_visible_func, NULL, NULL);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (sw), tree_view);
return sw;
}
static gboolean
area_press (GtkWidget *drawing_area,
GdkEventButton *event,
GtkMenuMerge *merge)
{
gtk_widget_grab_focus (drawing_area);
if (event->button == 3 &&
event->type == GDK_BUTTON_PRESS)
{
GtkWidget *menu = gtk_menu_merge_get_widget (merge, "/popups/FileMenu");
if (GTK_IS_MENU (menu))
{
gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
NULL, drawing_area,
3, event->time);
return TRUE;
}
}
return FALSE;
}
int
main (int argc, char **argv)
{
GtkActionGroup *action_group;
GtkMenuMerge *merge;
GtkWidget *window, *table, *frame, *menu_box, *vbox, *view, *area;
GtkWidget *button;
gint i;
gtk_init (&argc, &argv);
action_group = gtk_action_group_new ("TestActions");
gtk_action_group_add_actions (action_group, entries, n_entries);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), -1, 400);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
table = gtk_table_new (2, 2, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 2);
gtk_table_set_col_spacings (GTK_TABLE (table), 2);
gtk_container_set_border_width (GTK_CONTAINER (table), 2);
gtk_container_add (GTK_CONTAINER (window), table);
frame = gtk_frame_new ("Menus and Toolbars");
gtk_table_attach (GTK_TABLE (table), frame, 0,2, 1,2,
GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
menu_box = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (menu_box), 2);
gtk_container_add (GTK_CONTAINER (frame), menu_box);
area = gtk_drawing_area_new ();
gtk_widget_set_events (area, GDK_BUTTON_PRESS_MASK);
gtk_widget_set_size_request (area, -1, 40);
gtk_box_pack_end (GTK_BOX (menu_box), area, FALSE, FALSE, 0);
gtk_widget_show (area);
merge = gtk_menu_merge_new ();
g_signal_connect (area, "button_press_event",
G_CALLBACK (area_press), merge);
gtk_menu_merge_insert_action_group (merge, action_group, 0);
g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), menu_box);
gtk_window_add_accel_group (GTK_WINDOW (window),
gtk_menu_merge_get_accel_group (merge));
frame = gtk_frame_new ("UI Files");
gtk_table_attach (GTK_TABLE (table), frame, 0,1, 0,1,
GTK_FILL, GTK_FILL|GTK_EXPAND, 0, 0);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
gtk_container_add (GTK_CONTAINER (frame), vbox);
for (i = 0; i < G_N_ELEMENTS (merge_ids); i++)
{
button = gtk_check_button_new_with_label (merge_ids[i].filename);
g_object_set_data (G_OBJECT (button), "mergenum", GINT_TO_POINTER (i));
g_signal_connect (button, "toggled", G_CALLBACK (toggle_merge), merge);
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
}
button = gtk_button_new_with_mnemonic ("_Dump Tree");
g_signal_connect (button, "clicked", G_CALLBACK (dump_tree), merge);
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
view = create_tree_view (merge);
gtk_table_attach (GTK_TABLE (table), view, 1,2, 0,1,
GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}