Search
lxdream.org :: lxdream/src/gtkui/gtk_hotkeys.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtk_hotkeys.c
changeset 1015:ad448bedc48a
author nkeynes
date Thu Jun 25 21:21:18 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Add quick state bits to the menus
view annotate diff log raw
     1 /**
     2  * $Id:  $
     3  *
     4  * GTK dialog for defining hotkeys
     5  *
     6  * Copyright (c) 2009 wahrhaft.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <assert.h>
    20 #include <string.h>
    21 #include <gtk/gtk.h>
    22 #include <gdk/gdkkeysyms.h>
    24 #include "lxdream.h"
    25 #include "display.h"
    26 #include "gtkui/gtkui.h"
    27 #include "hotkeys.h"
    31 static void config_keysym_hook( void *data, const gchar *keysym )
    32 {
    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);
    37 }
    39 static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
    40 {
    41     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    42     if( keypress_mode ) {
    43         gchar *keysym = input_keycode_to_keysym( &system_mouse_driver, event->button);
    44         if( keysym != NULL ) {
    45             config_keysym_hook( widget, keysym );
    46             g_free(keysym);
    47         }
    48         return TRUE;
    49     } else {
    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);
    53     }
    54     return FALSE;
    55 }
    57 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
    58 {
    59     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    60     if( 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) );
    64             return TRUE;
    65         }
    66         GdkKeymap *keymap = gdk_keymap_get_default();
    67         guint keyval;
    69         gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    70                                              NULL, NULL, NULL );
    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);
    74         return TRUE;
    75     } else {
    76         switch( event->keyval ) {
    77         case GDK_Return:
    78         case GDK_KP_Enter:
    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);
    82             return TRUE;
    83         case GDK_BackSpace:
    84         case GDK_Delete:
    85             gtk_entry_set_text( GTK_ENTRY(widget), "" );
    86             return TRUE;
    87         }
    88         return FALSE;
    89     }
    91 }
    93 void hotkeys_dialog_done( GtkWidget *panel, gboolean isOK )
    94 {
    95     if( isOK ) {
    96         hotkeys_unregister_keys();
    97         lxdream_config_entry_t conf = hotkeys_get_config();
    98         int i;
    99         for( i=0; conf[i].key != NULL; i++ ) {
   100             char buf[64];
   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 );
   113             } else {
   114                 char buf[64];
   115                 snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
   116                 lxdream_set_config_value( &conf[i], buf );
   117             }
   118         }
   119         lxdream_save_config();
   120         hotkeys_register_keys();
   121     }
   122 }
   124 GtkWidget *hotkeys_panel_new()
   125 {
   126     lxdream_config_entry_t conf = hotkeys_get_config();
   127     int count, i;
   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;
   135         char buf[64];
   136         int x=0;
   137         int y=i;
   138         if( i >= (count+1)>>1 ) {
   139             x = 3;
   140             y -= (count+1)>>1;
   141         }
   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]) );
   174                 }
   175             }
   176             g_strfreev(parts);
   177         }
   178     }
   179     gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
   181     return table;
   182 }
   184 void hotkeys_dialog_run( )
   185 {
   186     gtk_gui_run_property_dialog( _("Hotkey Settings"), hotkeys_panel_new(), hotkeys_dialog_done );
   187 }
.