GtkApplicationWindow: Set the accel group on constructed windows

This will make the menuitems pick up matching accel map entries
automatically. Currently, nothing creates such accel map entries,
though.
This commit is contained in:
Matthias Clasen 2011-12-05 17:14:57 -05:00 committed by Ryan Lortie
parent 9d62d24d65
commit 1c6d0c3e27
5 changed files with 48 additions and 13 deletions

View File

@ -122,7 +122,7 @@ gtk_application_window_update_menubar (GtkApplicationWindow *window)
g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (muxer));
window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (muxer), window->priv->accels);
gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
gtk_widget_show_all (window->priv->menubar);
g_object_unref (combined);

View File

@ -33,6 +33,7 @@
typedef struct {
GActionObservable *actions;
GMenuModel *model;
GtkAccelGroup *accels;
GtkMenuShell *shell;
guint update_idle;
GSList *connected;
@ -85,7 +86,7 @@ gtk_model_menu_binding_append_item (GtkModelMenuBinding *binding,
{
GtkMenuItem *item;
item = gtk_model_menu_item_new (model, item_index, binding->actions);
item = gtk_model_menu_item_new (model, item_index, binding->actions, binding->accels);
gtk_menu_shell_append (binding->shell, GTK_WIDGET (item));
gtk_widget_show (GTK_WIDGET (item));
binding->n_items++;
@ -225,6 +226,7 @@ void
gtk_model_menu_bind (GtkMenuShell *shell,
GMenuModel *model,
GActionObservable *actions,
GtkAccelGroup *accels,
gboolean with_separators)
{
GtkModelMenuBinding *binding;
@ -232,6 +234,7 @@ gtk_model_menu_bind (GtkMenuShell *shell,
binding = g_slice_new (GtkModelMenuBinding);
binding->model = g_object_ref (model);
binding->actions = g_object_ref (actions);
binding->accels = accels;
binding->shell = shell;
binding->update_idle = 0;
binding->connected = NULL;
@ -243,24 +246,27 @@ gtk_model_menu_bind (GtkMenuShell *shell,
GtkWidget *
gtk_model_menu_create_menu (GMenuModel *model,
GActionObservable *actions)
GActionObservable *actions,
GtkAccelGroup *accels)
{
GtkWidget *menu;
menu = gtk_menu_new ();
gtk_model_menu_bind (GTK_MENU_SHELL (menu), model, actions, TRUE);
gtk_menu_set_accel_group (GTK_MENU (menu), accels);
gtk_model_menu_bind (GTK_MENU_SHELL (menu), model, actions, accels, TRUE);
return menu;
}
GtkWidget *
gtk_model_menu_create_menu_bar (GMenuModel *model,
GActionObservable *actions)
GActionObservable *actions,
GtkAccelGroup *accels)
{
GtkWidget *menubar;
menubar = gtk_menu_bar_new ();
gtk_model_menu_bind (GTK_MENU_SHELL (menubar), model, actions, FALSE);
gtk_model_menu_bind (GTK_MENU_SHELL (menubar), model, actions, accels, FALSE);
return menubar;
}

View File

@ -24,20 +24,24 @@
#include <gtk/gactionobservable.h>
#include <gtk/gtkmenushell.h>
#include <gtk/gtkaccelgroup.h>
#include <gio/gio.h>
G_GNUC_INTERNAL
void gtk_model_menu_bind (GtkMenuShell *shell,
GMenuModel *model,
GActionObservable *actions,
GtkAccelGroup *accels,
gboolean with_separators);
G_GNUC_INTERNAL
GtkWidget * gtk_model_menu_create_menu_bar (GMenuModel *model,
GActionObservable *actions);
GActionObservable *actions,
GtkAccelGroup *accels);
G_GNUC_INTERNAL
GtkWidget * gtk_model_menu_create_menu (GMenuModel *model,
GActionObservable *actions);
GActionObservable *actions,
GtkAccelGroup *accels);
#endif /* __GTK_MODEL_MENU_H__ */

View File

@ -23,6 +23,7 @@
#include "gtkmodelmenuitem.h"
#include "gtkaccelmap.h"
#include "gtkmodelmenu.h"
struct _GtkModelMenuItem
@ -188,11 +189,28 @@ gtk_model_menu_item_action_removed (GActionObserver *observer,
gtk_widget_queue_resize (GTK_WIDGET (item));
}
static gchar *
get_accel_path (const gchar *action_name,
GVariant *parameter)
{
GString *s;
s = g_string_new ("<Actions>/");
g_string_append (s, action_name);
if (parameter)
{
g_string_append_c (s, '/');
g_variant_print_string (parameter, s, FALSE);
}
return g_string_free (s, FALSE);
}
static void
gtk_model_menu_item_setup (GtkModelMenuItem *item,
GMenuModel *model,
gint item_index,
GActionObservable *actions)
GActionObservable *actions,
GtkAccelGroup *accels)
{
GMenuAttributeIter *iter;
GMenuModel *submenu;
@ -201,7 +219,7 @@ gtk_model_menu_item_setup (GtkModelMenuItem *item,
if ((submenu = g_menu_model_get_item_link (model, item_index, "submenu")))
{
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_model_menu_create_menu (submenu, actions));
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_model_menu_create_menu (submenu, actions, accels));
g_object_unref (submenu);
}
@ -228,6 +246,7 @@ gtk_model_menu_item_setup (GtkModelMenuItem *item,
const GVariantType *type;
gboolean enabled;
GVariant *state;
gchar *path;
/* observer already causes us to hold a hard ref on the group */
item->actions = G_ACTION_GROUP (actions);
@ -242,6 +261,10 @@ gtk_model_menu_item_setup (GtkModelMenuItem *item,
if (state != NULL)
g_variant_unref (state);
path = get_accel_path (item->action_name, item->target);
gtk_menu_item_set_accel_path (GTK_MENU_ITEM (item), path);
g_free (path);
}
}
@ -284,13 +307,14 @@ gtk_model_menu_item_class_init (GtkModelMenuItemClass *class)
GtkMenuItem *
gtk_model_menu_item_new (GMenuModel *model,
gint item_index,
GActionObservable *actions)
GActionObservable *actions,
GtkAccelGroup *accels)
{
GtkModelMenuItem *item;
item = g_object_new (GTK_TYPE_MODEL_MENU_ITEM, NULL);
gtk_model_menu_item_setup (item, model, item_index, actions);
gtk_model_menu_item_setup (item, model, item_index, actions, accels);
return GTK_MENU_ITEM (item);
}

View File

@ -39,6 +39,7 @@ GType gtk_model_menu_item_get_type (void) G
G_GNUC_INTERNAL
GtkMenuItem * gtk_model_menu_item_new (GMenuModel *model,
gint item_index,
GActionObservable *actions);
GActionObservable *actions,
GtkAccelGroup *accels);
#endif /* __GTK_MODEL_MENU_ITEM_H__ */