On Wed, 2005-09-07 at 22:53 -0400, Tristan Van Berkom wrote:
[...]

> Come to think of it theres another complication; the project window
> menubar accelerators are not a part of the libgladeui.so library, so
> shortcuts need to be implemented seperately per application using the
> glade3 core library.
> 
> So what we probably want is for anjuta/glade-project-window to decide what
> shortcuts do what in there respective apps and use the glade_shortcuts api
> to add shortcuts to the glade windows/runtime widgets.

Yes, this patch works fine.

But maybe it's better to add this api to glade-app.c and then add an
accel_group member to GladeAppPriv instead of using g_object_set_data()
in glade_shortcut_app_get_accel_group().

This way we just provide an automatic way to set a acceleration group
for every window created by glade widgets.
And glade_shortcut_change_entry() would be just a convenience function
since acceleration group management is now in charge of the main
application (anjuta/glade-project-window)

If you think the later approach is nicer, drop me a line. changes should
be trivial to do :D

well hope this helps

Juan Pablo

Index: src/Makefile.am
===================================================================
RCS file: /cvs/gnome/glade3/src/Makefile.am,v
retrieving revision 1.44
diff -u -r1.44 Makefile.am
--- src/Makefile.am	9 Aug 2005 14:29:06 -0000	1.44
+++ src/Makefile.am	8 Sep 2005 20:47:53 -0000
@@ -61,7 +61,8 @@
 	glade-parser.c \
 	glade-builtins.c \
 	glade-fixed-manager.c \
-	glade-app.c
+	glade-app.c \
+	glade-shortcut.c
 
 libgladeui_1_la_CFLAGS = -DINSIDE_LIBGLADEUI $(AM_CFLAGS)
 libgladeui_1_la_LDFLAGS = -version-info $(GLADE_CURRENT):$(GLADE_REVISION):$(GLADE_AGE)
@@ -100,6 +101,7 @@
 	glade-app.h \
 	glade-builtins.h \
 	glade-fixed-manager.h \
+	glade-shortcut.h \
 	fixed_bg.xpm
 
 pkgconfigdir = $(libdir)/pkgconfig
Index: src/glade-app.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-app.c,v
retrieving revision 1.18
diff -u -r1.18 glade-app.c
--- src/glade-app.c	24 Aug 2005 20:31:29 -0000	1.18
+++ src/glade-app.c	8 Sep 2005 20:48:15 -0000
@@ -48,6 +48,7 @@
 #include "glade-catalog.h"
 #include "glade-app.h"
 #include "glade-paths.h"
+#include "glade-shortcut.h"
 
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtkstock.h>
@@ -179,14 +180,23 @@
 on_palette_button_clicked (GladePalette *palette, GladeApp *app)
 {
 	GladeWidgetClass *class;
-
+	GladeWidget *widget;
+	
 	g_return_if_fail (GLADE_IS_PALETTE (palette));
 	class = palette->current;
 
 	/* class may be NULL if the selector was pressed */
 	if (class && g_type_is_a (class->type, GTK_TYPE_WINDOW))
 	{
-		glade_command_create (class, NULL, NULL, app->priv->active_project);
+		widget=glade_command_create (class, NULL, NULL, app->priv->active_project);
+		
+		/* if this is a top level widget set the accel group */
+		if (GTK_IS_WINDOW(widget->object))
+		{
+			gtk_window_add_accel_group (GTK_WINDOW(widget->object),
+						    glade_shortcut_app_get_accel_group (app));
+		}
+
 		glade_palette_unselect_widget (palette);
 		app->priv->add_class = NULL;
 	}
@@ -242,7 +252,7 @@
 glade_app_config_save (GladeApp *app)
 {
 	GIOChannel *fd;
-	gchar *data, *filename;
+	gchar *data=NULL, *filename;
 	const gchar *config_dir = g_get_user_config_dir ();
 	GError *error = NULL;
 	gsize size;
@@ -266,12 +276,11 @@
 	
 	fd = g_io_channel_new_file (filename, "w", &error);
 
-	if (error == NULL)
+	if (error == NULL){
 		data = g_key_file_to_data (app->priv->config, &size, &error);
-	
-	if (error == NULL)
 		g_io_channel_write_chars (fd, data, size, NULL, &error);
-
+	}
+	
 	/* Free resources */	
 	if (error)
 	{
@@ -594,6 +603,9 @@
 	g_signal_connect (G_OBJECT (project), "selection_changed",
 			  G_CALLBACK (on_project_selection_changed_cb), app);
 	
+	/* add acceleration groups to top level widgets */
+	glade_shortcut_project_set_accel_group (project, glade_shortcut_app_get_accel_group (app));
+
 	glade_app_set_project (app, project);
 	/* make sure the palette is sensitive */
 	gtk_widget_set_sensitive (GTK_WIDGET (app->priv->palette), TRUE);
Index: src/glade-project-window.c
===================================================================
RCS file: /cvs/gnome/glade3/src/glade-project-window.c,v
retrieving revision 1.110
diff -u -r1.110 glade-project-window.c
--- src/glade-project-window.c	2 Sep 2005 02:54:48 -0000	1.110
+++ src/glade-project-window.c	8 Sep 2005 20:49:27 -0000
@@ -31,12 +31,16 @@
 #include <gtk/gtkstock.h>
 
 #include "glade.h"
-
 #include "glade-project-window.h"
+#include "glade-shortcut.h"
 
 #define CONFIG_RECENT_PROJECTS     "Recent Projects"
 #define CONFIG_RECENT_PROJECTS_MAX "max_recent_projects"
 
+#define GLADE_ACTION_GROUP_MENU "GladeMenu"
+#define GLADE_ACTION_GROUP_PROJECT "GladeProject"
+#define GLADE_ACTION_GROUP_RECENT "GladeRecent"
+
 struct _GladeProjectWindowPriv {
 	/* Application widgets */
 	GtkWidget *window; /* Main window */
@@ -48,9 +52,9 @@
 	
 
 	GtkUIManager *ui;		/* The UIManager */
-	GtkActionGroup *actions;	/* All the static actions */
-	GtkActionGroup *p_actions;	/* Projects actions */
-	GtkActionGroup *rp_actions;	/* Recent projects actions */
+	GtkActionGroup *menu_actions;	/* All the static actions */
+	GtkActionGroup *project_actions;	/* Projects actions */
+	GtkActionGroup *recent_actions;	/* Recent projects actions */
 
 	GQueue *recent_projects;	/* A GtkAction queue */
 	gint rp_max;			/* Maximun Recent Projects entries */
@@ -117,7 +121,7 @@
 	gtk_ui_manager_remove_ui(gpw->priv->ui,
 				 glade_project_get_menuitem_merge_id(project));
 
-	gtk_action_group_remove_action (gpw->priv->p_actions,
+	gtk_action_group_remove_action (gpw->priv->project_actions,
 					GTK_ACTION (project->action));
 	
 	g_object_unref (G_OBJECT (project->action));
@@ -141,7 +145,7 @@
 	
 	gtk_ui_manager_remove_ui(gpw->priv->ui,	merge_id);
 
-	gtk_action_group_remove_action (gpw->priv->rp_actions, action);
+	gtk_action_group_remove_action (gpw->priv->recent_actions, action);
 	
 	g_queue_remove (gpw->priv->recent_projects, action);
 	
@@ -187,7 +191,7 @@
 	
 	/* Add action */
 	action = gtk_action_new (action_name, label, NULL, NULL);
-	gtk_action_group_add_action_with_accel (gpw->priv->rp_actions, action, "");
+	gtk_action_group_add_action_with_accel (gpw->priv->recent_actions, action, "");
 	g_signal_connect (G_OBJECT (action), "activate", (GCallback)gpw_recent_project_open_cb, gpw);
 	
 	/* Add menuitem */
@@ -1181,22 +1185,22 @@
 {
 	GError *error = NULL;
 	
-	gpw->priv->actions = gtk_action_group_new ("actions");
-	gtk_action_group_add_actions (gpw->priv->actions, entries, n_entries, gpw);
-	gtk_action_group_add_toggle_actions (gpw->priv->actions, view_entries,
+	gpw->priv->menu_actions = gtk_action_group_new (GLADE_ACTION_GROUP_MENU);
+	gtk_action_group_add_actions (gpw->priv->menu_actions, entries, n_entries, gpw);
+	gtk_action_group_add_toggle_actions (gpw->priv->menu_actions, view_entries,
 						n_view_entries, gpw);
 
-	gpw->priv->p_actions = gtk_action_group_new ("p_actions");
+	gpw->priv->project_actions = gtk_action_group_new (GLADE_ACTION_GROUP_PROJECT);
 
-	gpw->priv->rp_actions = gtk_action_group_new ("rp_actions");
+	gpw->priv->recent_actions = gtk_action_group_new (GLADE_ACTION_GROUP_RECENT);
 	
 	gpw->priv->ui = gtk_ui_manager_new ();
 	g_signal_connect(G_OBJECT(gpw->priv->ui), "connect-proxy",
 			 (GCallback)gpw_ui_connect_proxy_cb, gpw);
 
-	gtk_ui_manager_insert_action_group (gpw->priv->ui, gpw->priv->actions, 0);
-	gtk_ui_manager_insert_action_group (gpw->priv->ui, gpw->priv->p_actions, 1);
-	gtk_ui_manager_insert_action_group (gpw->priv->ui, gpw->priv->rp_actions, 2);
+	gtk_ui_manager_insert_action_group (gpw->priv->ui, gpw->priv->menu_actions, 0);
+	gtk_ui_manager_insert_action_group (gpw->priv->ui, gpw->priv->project_actions, 1);
+	gtk_ui_manager_insert_action_group (gpw->priv->ui, gpw->priv->recent_actions, 2);
 	
 	gtk_window_add_accel_group (GTK_WINDOW (gpw->priv->window), 
 				  gtk_ui_manager_get_accel_group (gpw->priv->ui));
@@ -1361,7 +1365,7 @@
 	g_signal_connect (G_OBJECT (project->action), "activate",
 			  (GCallback) glade_project_window_set_project, project);
 	
-	gtk_action_group_add_action_with_accel (gpw->priv->p_actions,
+	gtk_action_group_add_action_with_accel (gpw->priv->project_actions,
 						GTK_ACTION (project->action), "");
 	
 	/* Add menuitem to menu */
@@ -1631,6 +1635,7 @@
 glade_project_window_new (void)
 {
 	GladeProjectWindow *gpw;
+	GtkAccelGroup *accel_group;
 	
 	gpw = g_object_new (GLADE_TYPE_PROJECT_WINDOW, NULL);
 
@@ -1639,5 +1644,13 @@
 	gpw_create_editor  (gpw);
 
 	glade_app_set_window (GLADE_APP (gpw), gpw->priv->window);
+
+	accel_group = gtk_ui_manager_get_accel_group(gpw->priv->ui);
+	glade_shortcut_app_set_accel_group (GLADE_APP (gpw), accel_group);
+
+	gtk_window_add_accel_group(gpw->priv->palette_window, accel_group);
+	gtk_window_add_accel_group(gpw->priv->editor_window, accel_group);
+	gtk_window_add_accel_group(GTK_WINDOW (glade_app_get_clipboard_view (GLADE_APP (gpw))), accel_group);
+
 	return gpw;
 }
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
#ifndef __GLADE_SHORTCUT_H__
#define __GLADE_SHORTCUT_H__

G_BEGIN_DECLS

void glade_shortcut_app_set_accel_group (GladeApp *app, GtkAccelGroup *accel_group);
GtkAccelGroup *glade_shortcut_app_get_accel_group (GladeApp *app);
void glade_shortcut_project_set_accel_group (GladeProject *project, GtkAccelGroup *accel_group);
gboolean glade_shortcut_change_entry (const gchar *accel_path, const gchar *accelerator);

G_END_DECLS

#endif   /* __GLADE_SHORTCUT_H__ */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * glade-shortcut.c - Functions to manipulate shortcuts.
 *
 * Copyright (C) 2005 Juan Pablo Ugarte.
 *
 * Author(s):
 *      Juan Pablo Ugarte <[EMAIL PROTECTED]>
 *
 * 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 "glade.h"
#include "glade-app.h"
#include "glade-project.h"


#define GLADE_SHORTCUT_ACCEL_GROUP_KEY "accel_group"

/*
 * glade_shortcut_app_set_accel_group:
 *
 * Sets @accel_group to the @app's window.
 */
void
glade_shortcut_app_set_accel_group (GladeApp *app, GtkAccelGroup *accel_group)
{
	GtkWidget *window;

	g_return_if_fail(GLADE_IS_APP(app));
	
	window = glade_app_get_window (app);
	
	if (window && accel_group)
		g_object_set_data (G_OBJECT(window),
				   GLADE_SHORTCUT_ACCEL_GROUP_KEY,
				   (gpointer) accel_group);
}

/*
 * glade_shortcut_get_accel_group:
 *
 * Gets the acceleration group from the @app's window.
 */
GtkAccelGroup *
glade_shortcut_app_get_accel_group (GladeApp *app)
{
	GtkWidget *window;
	gpointer retval = NULL;
	
	if (GLADE_IS_APP(app) == FALSE) return NULL;
	
	window = glade_app_get_window (app);
	
	if (window)
		retval = g_object_get_data (G_OBJECT(window),
					    GLADE_SHORTCUT_ACCEL_GROUP_KEY);

	return (GtkAccelGroup *) retval;
}


/*
 * glade_shortcut_project_set_accel_group:
 *
 * Set @accel_group to every top level widget in @project.
 */
void
glade_shortcut_project_set_accel_group (GladeProject *project,
					GtkAccelGroup *accel_group)
{
	GList *objects;
	
	g_return_if_fail (GLADE_IS_PROJECT (project));
	
	/* maybe it is better to add a funcion in glade-project to access to objects */
	objects = project->objects;
	while (objects)
	{
		if(GTK_IS_WINDOW (objects->data))
			gtk_window_add_accel_group (GTK_WINDOW (objects->data), accel_group);
		
		objects = objects->next;
	}
}

/**
 * glade_shortcut_change_entry:
 *
 * @accel_path: The action name in the form of an acceleration path. ("<Actions>/group-name/action-name")
 * @accelerator: The accelerator for the action. ("<Control>Q"), "" for none and NULL for default.
 *
 * Changes the @accel_path's accelerator to @accelerator.
 */
gboolean
glade_shortcut_change_entry (const gchar *accel_path,
			     const gchar *accelerator)
{
	guint key;
	GdkModifierType mod;
	
	if (accel_path == NULL || accelerator == NULL) return FALSE;
	
	gtk_accelerator_parse (accelerator, &key, &mod);
	
	return gtk_accel_map_change_entry (accel_path, key, mod, TRUE);
}
_______________________________________________
Glade-devel maillist  -  Glade-devel@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/glade-devel

Reply via email to