[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

GtkAction, GtkUIManager, and Accelerators



Hello,

I have been experimenting with these new features of GTK 2.4.  I am
unable to get the keyboard shortcuts to work.  I have attached my
example source and XML file.  The shortcut Alt-F works to cause the menu
to activate, but none of the action shortcuts work, eg Control-Q to
quit.

Can anyone offer any insight?

Thanks,

Keith.

/* vim: set ai ts=4 sw=4: */
/*
 * Copyright 2004 Keith Sharp <kms@passback.co.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
#include <gtk/gtk.h>

#define UI_FILENAME "ui_manager.xml"		/* Filename for XML file with menus and buttons */

void 
destroy_callback (GtkWidget *widget, gpointer data)
{
    gtk_main_quit ();
	return;
}

void
activate_action (GtkAction *action, gpointer data)
{
	guint cid = gtk_statusbar_get_context_id (GTK_STATUSBAR(data), "Context Description");
	gtk_statusbar_push (GTK_STATUSBAR(data), cid, g_strconcat ("Action \"", gtk_action_get_name (action), 
						"\" activated", NULL));
}

gint
main (gint argc, gchar *argv[])
{
	GtkWidget *window, *vbox, *status;
	GtkUIManager *ui;
	GtkActionGroup *actions;
	GtkAction *remove;
	GError *error = NULL;

	static GtkActionEntry entries[] = {
		{ "FileMenu", NULL, "_File" },
		{ "New", GTK_STOCK_NEW, "_New", "<Control>N", "Create a new file", G_CALLBACK (activate_action) },
		{ "Open", GTK_STOCK_OPEN, "_Open", "<Control>O", "Open a file", G_CALLBACK (activate_action) },
		{ "Save", GTK_STOCK_SAVE, "_Save", "<Control>S", "Save a file", G_CALLBACK (activate_action) },
		{ "Quit", GTK_STOCK_QUIT, "_Quit", "<Control>Q", "Quit application", G_CALLBACK (destroy_callback) },
		{ "Add", GTK_STOCK_ADD, "_Add", "<Control>A", "Add an item", G_CALLBACK (activate_action) }
	};
	guint n_entries = G_N_ELEMENTS (entries);

	gtk_init (&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK(destroy_callback), NULL);
	gtk_window_set_default_size (GTK_WINDOW(window), 200, 50);

	status = gtk_statusbar_new ();

	actions = gtk_action_group_new ("Actions");
	gtk_action_group_add_actions (actions, entries, n_entries, status);

	ui = gtk_ui_manager_new ();
	if (!gtk_ui_manager_add_ui_from_file (ui, UI_FILENAME, &error)) {
		g_error ("Could not merge UI from %s, error was: %s\n", UI_FILENAME, error->message);
	}
	gtk_ui_manager_insert_action_group (ui, actions, 0);

	vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_box_pack_start (GTK_BOX (vbox), gtk_ui_manager_get_widget (ui, "/MenuBar"), FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), gtk_ui_manager_get_widget (ui, "/ToolBar"), TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), status, FALSE, FALSE, 0);

	gtk_widget_show_all (window);
	gtk_main ();

	return 1;
}
#

BINARIES = gtkaction
CC = gcc
FLAGS = `pkg-config --cflags --libs gtk+-2.0`

all: $(BINARIES)

gtkaction: gtkaction.c
	$(CC) -o gtkaction gtkaction.c $(FLAGS)

clean:
	rm -rf *.o *~ $(BINARIES)
<ui>
	<menubar name="MenuBar">
		<menu action="FileMenu">
			<menuitem action="New"/>
			<menuitem action="Open"/>
			<menuitem action="Save"/>
		</menu>
	</menubar>
	<toolbar name="ToolBar">
		<toolitem action="Quit"/>
		<toolitem action="Add"/>
		<separator/>
		<toolitem action="New"/>
	</toolbar>
</ui>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]