1 /*
2  *
3  *  Copyright (C) 2010-2011  Colomban Wendling <ban@herbesfolles.org>
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #include "gwh-keybindings.h"
21 
22 #include <glib.h>
23 #include <gtk/gtk.h>
24 
25 #include <geanyplugin.h>
26 #include <geany.h>
27 
28 #include "gwh-plugin.h"
29 
30 
31 static GeanyKeyGroup *G_key_group;
32 
33 
34 void
gwh_keybindings_init(void)35 gwh_keybindings_init (void)
36 {
37   G_key_group = plugin_set_key_group (geany_plugin, "webhelper", GWH_KB_COUNT,
38                                       NULL);
39 }
40 
41 void
gwh_keybindings_cleanup(void)42 gwh_keybindings_cleanup (void)
43 {
44   G_key_group = NULL;
45 }
46 
47 GeanyKeyGroup *
gwh_keybindings_get_group(void)48 gwh_keybindings_get_group (void)
49 {
50   return G_key_group;
51 }
52 
53 /**
54  * ghw_keybindings_handle_event:
55  * @widget: Unused, may be %NULL
56  * @event: A GdkEventKey to handle
57  * @data: Unused, may be %NULL
58  *
59  * Handles a GDK key event and calls corresponding keybindings callbacks.
60  * This function is meant to be used as the callback for a
61  * GtkWidget:key-press-event signal.
62  *
63  * Returns: Whether the event was handled or not
64  */
65 gboolean
gwh_keybindings_handle_event(GtkWidget * widget,GdkEventKey * event,gpointer data)66 gwh_keybindings_handle_event (GtkWidget    *widget,
67                               GdkEventKey  *event,
68                               gpointer      data)
69 {
70   guint     mods = event->state & gtk_accelerator_get_default_mod_mask ();
71   gboolean  handled = FALSE;
72   guint     keyval = event->keyval;
73   guint     i;
74 
75   if (event->state & (GDK_SHIFT_MASK | GDK_LOCK_MASK)) {
76     keyval = gdk_keyval_to_lower (keyval);
77   }
78   for (i = 0; ! handled && i < GWH_KB_COUNT; i++) {
79     GeanyKeyBinding *kb;
80 
81     kb = keybindings_get_item (G_key_group, i);
82     if (kb->key == keyval && kb->mods == mods) {
83       if (kb->callback) {
84         kb->callback (i);
85       /* We can't handle key group callback since we can't acces key group
86        * fields. However, we don't use it so it's not a real problem. */
87       /*} else if (G_key_group->callback) {
88         G_key_group->callback (i);*/
89       }
90       handled = TRUE;
91     }
92   }
93 
94   return handled;
95 }
96