source: pkg/main/gnome-panel/branches/upstream/current/gnome-panel/panel-bindings.c @ 180

Revision 180, 5.0 KB checked in by alanbach-guest, 6 years ago (diff)

[svn-inject] Installing original source of gnome-panel

Line 
1/*
2 * panel-bindings.c: panel keybindings support module
3 *
4 * Copyright (C) 2003 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 *
21 * Authors:
22 *      Mark McLoughlin <mark@skynet.ie>
23 */
24
25#include <config.h>
26
27#include "panel-bindings.h"
28
29#include <string.h>
30#include <glib/gi18n.h>
31
32#include "panel-gconf.h"
33#include "panel-profile.h"
34
35#define BINDINGS_PREFIX "/apps/metacity/window_keybindings"
36
37typedef struct {
38        char            *key;
39        char            *signal;
40        guint            keyval;
41        GdkModifierType  modifiers;
42} PanelBinding;
43
44static gboolean initialised = FALSE;
45
46static PanelBinding bindings [] = {
47        { "activate_window_menu", "popup-panel-menu", 0, 0 },
48        { "toggle_maximized",     "toggle-expand",    0, 0 },
49        { "maximize",             "expand",           0, 0 },
50        { "unmaximize",           "unexpand",         0, 0 },
51        { "toggle_shaded",        "toggle-hidden",    0, 0 },
52        { "begin_move",           "begin-move",       0, 0 },
53        { "begin_resize",         "begin-resize",     0, 0 },
54};
55
56static void
57panel_binding_set_from_string (PanelBinding *binding,
58                               const char   *str)
59{
60        g_assert (binding->keyval == 0);
61        g_assert (binding->modifiers == 0);
62
63        if (!str || !str [0] || !strcmp (str, "disabled")) {
64                binding->keyval = 0;
65                binding->modifiers = 0;
66                return;
67        }
68
69        gtk_accelerator_parse (str, &binding->keyval, &binding->modifiers);
70        if (binding->keyval == 0 && binding->modifiers == 0) {
71                g_warning ("Enable to parse binding '%s'\n", str);
72                return;
73        }
74}
75
76static inline GtkBindingSet *
77get_binding_set (GtkBindingSet *binding_set)
78{
79        if (!binding_set) {
80                PanelToplevelClass *toplevel_class;
81
82                toplevel_class = g_type_class_peek (PANEL_TYPE_TOPLEVEL);
83                if (!toplevel_class)
84                        return NULL;
85
86                g_assert (PANEL_IS_TOPLEVEL_CLASS (toplevel_class));
87
88                binding_set = gtk_binding_set_by_class (toplevel_class);
89        }
90
91        return binding_set;
92}
93
94static void
95panel_binding_clear_entry (PanelBinding  *binding,
96                           GtkBindingSet *binding_set)
97{
98        binding_set = get_binding_set (binding_set);
99
100        gtk_binding_entry_clear (binding_set,   binding->keyval, binding->modifiers);
101}
102
103static void
104panel_binding_set_entry (PanelBinding  *binding,
105                         GtkBindingSet *binding_set)
106{
107        binding_set = get_binding_set (binding_set);
108
109        gtk_binding_entry_add_signal (binding_set,     
110                                      binding->keyval,
111                                      binding->modifiers,
112                                      binding->signal,
113                                      0);
114}
115
116static void
117panel_binding_changed (GConfClient  *client,
118                       guint         cnxn_id,
119                       GConfEntry   *entry,
120                       PanelBinding *binding)
121{
122        GConfValue *value;
123
124        if (binding->keyval)
125                panel_binding_clear_entry (binding, NULL);
126
127        binding->keyval    = 0;
128        binding->modifiers = 0;
129
130        value = gconf_entry_get_value (entry);
131
132        if (!value || value->type != GCONF_VALUE_STRING)
133                return;
134
135        panel_binding_set_from_string (binding, gconf_value_get_string (value));
136
137        if (!binding->keyval)
138                return;
139
140        panel_binding_set_entry (binding, NULL);
141}
142
143static void
144panel_binding_watch (PanelBinding *binding,
145                     const char   *key)
146{
147        GError *error = NULL;
148
149        gconf_client_notify_add (panel_gconf_get_client (), key,
150                                (GConfClientNotifyFunc) panel_binding_changed,
151                                binding, NULL, &error);
152        if (error) {
153                g_warning (_("Error watching gconf key '%s': %s"), key, error->message);
154                g_error_free (error);
155        }
156}
157
158static void
159panel_bindings_initialise (void)
160{
161        GConfClient *client;
162        GError      *error;
163        int          i;
164
165        client = panel_gconf_get_client ();
166
167        error = NULL;
168        gconf_client_add_dir (client, BINDINGS_PREFIX,
169                              GCONF_CLIENT_PRELOAD_ONELEVEL, &error);
170        if (error) {
171                g_warning (_("Error loading gconf directory '%s': %s"),
172                           BINDINGS_PREFIX, error->message),
173                g_error_free (error);
174        }
175
176        for (i = 0; i < G_N_ELEMENTS (bindings); i++) {
177                const char *key;
178                char       *str;
179
180                key = panel_gconf_sprintf ("%s/%s", BINDINGS_PREFIX, bindings [i].key);
181
182                error = NULL;
183                str = gconf_client_get_string (client, key, &error);
184                if (error) {
185                        g_warning (_("Error getting value for '%s': %s"),
186                                   key, error->message);
187                        continue;
188                }
189
190                panel_binding_set_from_string (&bindings [i], str);
191                panel_binding_watch (&bindings [i], key);
192
193                g_free (str);
194        }
195}
196
197void
198panel_bindings_set_entries (GtkBindingSet *binding_set)
199{
200        int i;
201
202        if (!initialised) {
203                panel_bindings_initialise ();
204                initialised = TRUE;
205        }
206
207        for (i = 0; i < G_N_ELEMENTS (bindings); i++) {
208                if (!bindings [i].keyval)
209                        continue;
210
211                panel_binding_set_entry (&bindings [i], binding_set);
212        }
213}
Note: See TracBrowser for help on using the repository browser.