]> icculus.org git repositories - dana/obconf.git/blob - src/keybindings.c
add key and action columns. possibly totally wrong
[dana/obconf.git] / src / keybindings.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    keybindings.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "main.h"
20 #include "tree.h"
21
22 static gboolean      mapping = FALSE;
23 static gchar        *saved_text = NULL;
24 static GtkListStore *binding_store;
25
26 static gboolean validate_key(const gchar *s);
27
28 void keybindings_setup_tab()
29 {
30     GtkWidget *w;
31     GtkCellRenderer *render;
32     GtkTreeViewColumn *column;
33     GtkTreeSelection *select;
34     gchar *s;
35
36     mapping = TRUE;
37
38     w = get_widget("chain_quit_key");
39     s = tree_get_string("keyboard/chainQuitKey", "C-g");
40     gtk_entry_set_text(GTK_ENTRY(w), s);
41     g_free(s);
42
43     /* widget setup */
44     w = get_widget("key_bindings");
45     binding_store = gtk_list_store_new(2, G_TYPE_STRING, GTK_TYPE_COMBO_BOX);
46     gtk_tree_view_set_model(GTK_TREE_VIEW(w), GTK_TREE_MODEL(binding_store));
47     g_object_unref (binding_store);
48
49     gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(w)),
50                                 GTK_SELECTION_SINGLE);
51
52     /* text column for the keys */
53     render = gtk_cell_renderer_text_new();
54     column = gtk_tree_view_column_new_with_attributes
55         ("Key", render, "text", 0, NULL);
56     gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
57
58     /* combo box column, for the actions */
59     render = gtk_cell_renderer_combo_new();
60     column = gtk_tree_view_column_new_with_attributes
61         ("Action", render, "text", 0, NULL);
62     gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
63
64     mapping = FALSE;
65 }
66
67 void on_chain_quit_key_focus_in(GtkEntry *w, gpointer data)
68 {
69     g_assert(saved_text == NULL);
70     saved_text = g_strdup(gtk_entry_get_text(w));
71 }
72
73 void on_chain_quit_key_focus_out(GtkEntry *w, gpointer data)
74 {
75     const gchar *s;
76
77     if (mapping) return;
78
79     s = gtk_entry_get_text(w);
80     if (!validate_key(s)) {
81         g_print("bad key binding: %s\n", s);
82         gtk_entry_set_text(w, saved_text);
83     } else
84         tree_set_string("keyboard/chainQuitKey", s);
85
86     g_free(saved_text);
87     saved_text = NULL;
88 }
89
90 static gboolean validate_key(const gchar *s)
91 {
92     const gchar *next;
93     const gchar *valid[] = { "Mod1", "Mod2", "Mod3", "Mod4", "Mod5",
94                              "Control", "C", "Alt", "A", "Meta", "M",
95                              "Super", "W", "Shift", "S", "Hyper", "H",
96                              NULL };
97
98     while ((next = strchr(s, '-'))) {
99         /* it's a modifier, validate it */
100         const gchar **it;
101         gboolean found = FALSE;
102
103         for (it = valid; *it && !found; ++it)
104             if (!g_ascii_strncasecmp(*it, s, strlen(*it)))
105                 found = TRUE;
106
107         if (!found) {
108             g_print("Invalid modifier\n");
109             return FALSE;
110         }
111
112         s = next + 1; /* skip past the '-' */
113     }
114     /* we're at the real key part */
115     if (!gdk_keyval_from_name(s)) {
116         g_print("Invalid key: %s\n", s);
117         return FALSE;
118     }
119
120     return TRUE;
121 }