1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
* Copyright (C) 2013-2020 Peter Bloomfield
*
* 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, see <https://www.gnu.org/licenses/>.
*/
/*
* Helpers for GtkApplicationWindow
*/
#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <gtk/gtk.h>
#include <string.h>
#include "application-helpers.h"
typedef struct {
GAction *action;
GVariant *parameter;
} AccelInfo;
static void
accel_info_free(AccelInfo * info)
{
if (info->parameter)
g_variant_unref(info->parameter);
g_free(info);
}
static gboolean
accel_activate(GtkAccelGroup * accel_group,
GObject * acceleratable,
guint keyval,
GdkModifierType modifier,
gpointer user_data)
{
AccelInfo *info = user_data;
gboolean block_accels;
block_accels = GPOINTER_TO_INT(g_object_get_data(acceleratable, "block-accels"));
if (block_accels)
return FALSE;
g_action_activate(info->action, info->parameter);
return TRUE;
}
static void
extract_accel_from_menu_item(GMenuModel * model,
gint item,
GActionMap * action_map,
GtkAccelGroup * accel_group)
{
GMenuAttributeIter *iter;
const gchar *key;
GVariant *value;
const gchar *accel = NULL;
const gchar *action = NULL;
GVariant *target = NULL;
iter = g_menu_model_iterate_item_attributes(model, item);
while (g_menu_attribute_iter_get_next(iter, &key, &value)) {
if (g_str_equal(key, "action")
&& g_variant_is_of_type(value, G_VARIANT_TYPE_STRING))
action = g_variant_get_string(value, NULL);
else if (g_str_equal(key, "accel")
&& g_variant_is_of_type(value, G_VARIANT_TYPE_STRING))
accel = g_variant_get_string(value, NULL);
else if (g_str_equal(key, "target"))
target = g_variant_ref(value);
g_variant_unref(value);
}
g_object_unref(iter);
if (accel && action) {
guint accel_key;
GdkModifierType accel_mods;
AccelInfo *info;
const gchar *basename;
GClosure *closure;
gtk_accelerator_parse(accel, &accel_key, &accel_mods);
basename = strchr(action, '.');
basename = basename ? basename + 1 : action;
info = g_new(AccelInfo, 1);
info->action = g_action_map_lookup_action(action_map, basename);
info->parameter = target ? g_variant_ref(target) : NULL;
closure = g_cclosure_new(G_CALLBACK(accel_activate), info,
(GClosureNotify) accel_info_free);
gtk_accel_group_connect(accel_group, accel_key, accel_mods, 0,
closure);
}
if (target)
g_variant_unref(target);
}
static void
extract_accels_from_menu(GMenuModel * model,
GActionMap * action_map,
GtkAccelGroup * accel_group)
{
gint i, n = g_menu_model_get_n_items(model);
GMenuLinkIter *iter;
const gchar *key;
GMenuModel *m;
for (i = 0; i < n; i++) {
extract_accel_from_menu_item(model, i, action_map, accel_group);
iter = g_menu_model_iterate_item_links(model, i);
while (g_menu_link_iter_get_next(iter, &key, &m)) {
extract_accels_from_menu(m, action_map, accel_group);
g_object_unref(m);
}
g_object_unref(iter);
}
}
static GtkAccelGroup *
get_accel_group(GMenuModel * model,
GActionMap * action_map)
{
GtkAccelGroup *accel_group;
accel_group = gtk_accel_group_new();
extract_accels_from_menu(model, action_map, accel_group);
return accel_group;
}
/*
* libbalsa_window_get_menu_bar
*
* Construct a menu-bar for a GtkApplicationWindow that does not use the
* GApplication's menubar
*
* window the GtkApplicationWindow
* entries array of GActionEntry structures
* n_entries length of the array
* resource_path resource path for GtkBuilder input defining a menu named
* "menubar"
* error GError for returning error information
* cb_data user data for GAction callbacks
*
* returns: the GtkMenuBar
*/
GtkWidget *
libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
const GActionEntry * entries,
gint n_entries,
const gchar * resource_path,
GError ** error,
gpointer cb_data)
{
GActionMap *map = G_ACTION_MAP(window);
GtkBuilder *builder;
GtkWidget *menu_bar = NULL;
g_action_map_add_action_entries(map, entries, n_entries, cb_data);
builder = gtk_builder_new();
if (gtk_builder_add_from_resource(builder, resource_path, error)) {
GMenuModel *menu_model;
menu_model =
G_MENU_MODEL(gtk_builder_get_object(builder, "menubar"));
menu_bar = gtk_menu_bar_new_from_model(menu_model);
libbalsa_window_set_accels(window, menu_model);
gtk_application_window_set_show_menubar(window, FALSE);
}
g_object_unref(builder);
return menu_bar;
}
/*
* libbalsa_window_set_accels
*
* Get the accelerators from a GMenuModel and add them to a
* GtkApplicationWindow
*
* window the GtkApplicationWindow
* menumodel the GMenuModel
*/
void
libbalsa_window_set_accels(GtkApplicationWindow * window,
GMenuModel * menu_model)
{
GSList *accel_groups;
GtkAccelGroup *accel_group;
/* Remove current accelerators: */
accel_groups = gtk_accel_groups_from_object(G_OBJECT(window));
if (accel_groups)
/* Last is first... */
gtk_window_remove_accel_group(GTK_WINDOW(window),
accel_groups->data);
accel_group = get_accel_group(menu_model, G_ACTION_MAP(window));
gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
g_object_unref(accel_group);
}
void
libbalsa_window_block_accels(GtkApplicationWindow * window,
gboolean block)
{
g_object_set_data(G_OBJECT(window), "block-accels", GINT_TO_POINTER(!!block));
}
/*
* libbalsa_window_add_accelerator
*
* Add an accelerator key combination for an action
*
* window the GtkApplicationWindow
* accel the accelerator string
* action_name name of the GAction
*/
void
libbalsa_window_add_accelerator(GtkApplicationWindow * window,
const gchar * accel,
const gchar * action_name)
{
GActionMap *action_map = G_ACTION_MAP(window);
guint accel_key;
GdkModifierType accel_mods;
const gchar *basename;
GAction *action;
AccelInfo *info;
GClosure *closure;
GtkAccelGroup *accel_group;
gtk_accelerator_parse(accel, &accel_key, &accel_mods);
if (!accel_key) {
g_warning("%s: could not parse accelerator ā%sā", __func__,
accel);
return;
}
basename = strchr(action_name, '.');
basename = basename ? basename + 1 : action_name;
action = g_action_map_lookup_action(action_map, basename);
if (!action) {
g_warning("%s: could not lookup action ā%sā", __func__,
action_name);
return;
}
info = g_new(AccelInfo, 1);
info->action = action;
info->parameter = NULL;
closure = g_cclosure_new(G_CALLBACK(accel_activate), info,
(GClosureNotify) accel_info_free);
accel_group = gtk_accel_group_new();
gtk_accel_group_connect(accel_group, accel_key, accel_mods, 0,
closure);
gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
}
|