4 * GTK dialog for defining hotkeys
6 * Copyright (c) 2009 wahrhaft.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include <gdk/gdkkeysyms.h>
26 #include "gtkui/gtkui.h"
31 static void config_keysym_hook( void *data, const gchar *keysym )
33 GtkWidget *widget = (GtkWidget *)data;
34 gtk_entry_set_text( GTK_ENTRY(widget), keysym );
35 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
36 input_set_keysym_hook(NULL, NULL);
39 static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
41 gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
43 gchar *keysym = input_keycode_to_keysym( &system_mouse_driver, event->button);
44 if( keysym != NULL ) {
45 config_keysym_hook( widget, keysym );
50 gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
51 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
52 input_set_keysym_hook(config_keysym_hook, widget);
57 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
59 gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
61 if( event->keyval == GDK_Escape ) {
62 gtk_entry_set_text( GTK_ENTRY(widget), "" );
63 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
66 GdkKeymap *keymap = gdk_keymap_get_default();
69 gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
71 gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
72 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
73 input_set_keysym_hook(NULL, NULL);
76 switch( event->keyval ) {
79 gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
80 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
81 input_set_keysym_hook(config_keysym_hook, widget);
85 gtk_entry_set_text( GTK_ENTRY(widget), "" );
93 void hotkeys_dialog_done( GtkWidget *panel, gboolean isOK )
96 hotkeys_unregister_keys();
97 lxdream_config_entry_t conf = hotkeys_get_config();
99 for( i=0; conf[i].key != NULL; i++ ) {
101 GtkWidget *entry1, *entry2;
102 const gchar *key1, *key2;
103 snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
104 entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
105 key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
106 snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
107 entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
108 key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
109 if( key1 == NULL || key1[0] == '\0') {
110 lxdream_set_config_value( &conf[i], key2 );
111 } else if( key2 == NULL || key2[0] == '\0') {
112 lxdream_set_config_value( &conf[i], key1 );
115 snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
116 lxdream_set_config_value( &conf[i], buf );
119 lxdream_save_config();
120 hotkeys_register_keys();
124 GtkWidget *hotkeys_panel_new()
126 lxdream_config_entry_t conf = hotkeys_get_config();
128 for( count=0; conf[count].key != NULL; count++ );
130 GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
131 GList *focus_chain = NULL;
132 //gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
133 for( i=0; i<count; i++ ) {
134 GtkWidget *text, *text2;
138 if( i >= (count+1)>>1 ) {
142 gtk_table_attach( GTK_TABLE(table), gtk_label_new(gettext(conf[i].label)), x, x+1, y, y+1,
143 GTK_SHRINK, GTK_SHRINK, 0, 0 );
144 text = gtk_entry_new();
145 gtk_entry_set_width_chars( GTK_ENTRY(text), 11 );
146 gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
147 g_signal_connect( text, "key_press_event",
148 G_CALLBACK(config_key_keypress), NULL );
149 g_signal_connect( text, "button_press_event",
150 G_CALLBACK(config_key_buttonpress), NULL );
151 snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
152 g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
153 g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
154 gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
155 focus_chain = g_list_append( focus_chain, text );
156 text2 = gtk_entry_new();
157 gtk_entry_set_width_chars( GTK_ENTRY(text2), 11 );
158 gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
159 g_signal_connect( text2, "key_press_event",
160 G_CALLBACK(config_key_keypress), NULL );
161 g_signal_connect( text2, "button_press_event",
162 G_CALLBACK(config_key_buttonpress), NULL );
163 snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
164 g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
165 g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
166 gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
167 focus_chain = g_list_append( focus_chain, text2 );
168 if( conf[i].value != NULL ) {
169 gchar **parts = g_strsplit(conf[i].value,",",3);
170 if( parts[0] != NULL ) {
171 gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
172 if( parts[1] != NULL ) {
173 gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
179 gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
184 void hotkeys_dialog_run( )
186 gtk_gui_run_property_dialog( _("Hotkey Settings"), hotkeys_panel_new(), hotkeys_dialog_done );
.