1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 * Copyright 1997 Owen Taylor <owt1@cornell.edu>
28 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
29 * file for a list of people on the GTK+ Team. See the ChangeLog
30 * files for a list of changes. These files are distributed with
31 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #include <glib/gprintf.h>
37 #include "gdk/gdkkeysyms.h"
38 #include "gtkbutton.h"
41 #include "gtkhseparator.h"
42 #include "gtkinputdialog.h"
44 #include "gtklistitem.h"
46 #include "gtkmarshalers.h"
48 #include "gtkmenuitem.h"
49 #include "gtknotebook.h"
50 #include "gtkscrolledwindow.h"
55 #undef GTK_DISABLE_DEPRECATED
56 #include "gtkoptionmenu.h"
57 #define GTK_DISABLE_DEPRECATED
61 typedef struct _GtkInputDialogPrivate GtkInputDialogPrivate;
62 typedef struct _GtkInputKeyInfo GtkInputKeyInfo;
64 struct _GtkInputDialogPrivate
66 GtkWidget *device_menu;
67 GtkWidget *device_optionmenu;
68 GtkWidget *no_devices_label;
72 struct _GtkInputKeyInfo
76 GtkInputDialog *inputd;
87 #define AXIS_LIST_WIDTH 160
88 #define AXIS_LIST_HEIGHT 175
90 #define KEYS_LIST_WIDTH 200
91 #define KEYS_LIST_HEIGHT 175
93 /* Forward declarations */
95 static void gtk_input_dialog_class_init (GtkInputDialogClass *klass);
96 static void gtk_input_dialog_init (GtkInputDialog *inputd);
97 static void gtk_input_dialog_screen_changed (GtkWidget *widget,
98 GdkScreen *previous_screen);
99 static void gtk_input_dialog_set_device (GtkWidget *widget,
101 static void gtk_input_dialog_set_mapping_mode (GtkWidget *w,
103 static void gtk_input_dialog_set_axis (GtkWidget *widget,
105 static void gtk_input_dialog_fill_axes (GtkInputDialog *inputd,
107 static void gtk_input_dialog_set_key (GtkInputKeyInfo *key,
109 GdkModifierType modifiers);
110 static gboolean gtk_input_dialog_key_press (GtkWidget *widget,
112 GtkInputKeyInfo *key);
113 static void gtk_input_dialog_clear_key (GtkWidget *widget,
114 GtkInputKeyInfo *key);
115 static void gtk_input_dialog_destroy_key (GtkWidget *widget,
116 GtkInputKeyInfo *key);
117 static void gtk_input_dialog_fill_keys (GtkInputDialog *inputd,
120 static GtkObjectClass *parent_class = NULL;
121 static guint input_dialog_signals[LAST_SIGNAL] = { 0 };
124 gtk_input_dialog_get_type (void)
126 static GType input_dialog_type = 0;
128 if (!input_dialog_type)
130 static const GTypeInfo input_dialog_info =
132 sizeof (GtkInputDialogClass),
133 NULL, /* base_init */
134 NULL, /* base_finalize */
135 (GClassInitFunc) gtk_input_dialog_class_init,
136 NULL, /* class_finalize */
137 NULL, /* class_data */
138 sizeof (GtkInputDialog),
140 (GInstanceInitFunc) gtk_input_dialog_init,
144 g_type_register_static (GTK_TYPE_DIALOG, "GtkInputDialog",
145 &input_dialog_info, 0);
148 return input_dialog_type;
151 GtkInputDialogPrivate *
152 gtk_input_dialog_get_private (GtkInputDialog *input_dialog)
154 GtkInputDialogPrivate *private;
155 static GQuark private_quark = 0;
158 private_quark = g_quark_from_static_string ("gtk-input-dialog-private");
160 private = g_object_get_qdata (G_OBJECT (input_dialog), private_quark);
164 private = g_new0 (GtkInputDialogPrivate, 1);
165 g_object_set_qdata_full (G_OBJECT (input_dialog), private_quark,
172 static GtkInputDialog *
173 input_dialog_from_widget (GtkWidget *widget)
177 if (GTK_IS_MENU_ITEM (widget))
179 GtkMenu *menu = GTK_MENU (widget->parent);
180 widget = gtk_menu_get_attach_widget (menu);
183 toplevel = gtk_widget_get_toplevel (widget);
184 return GTK_INPUT_DIALOG (toplevel);
188 gtk_input_dialog_class_init (GtkInputDialogClass *klass)
190 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
192 parent_class = g_type_class_peek_parent (klass);
194 widget_class->screen_changed = gtk_input_dialog_screen_changed;
196 klass->enable_device = NULL;
197 klass->disable_device = NULL;
199 input_dialog_signals[ENABLE_DEVICE] =
200 g_signal_new ("enable_device",
201 G_OBJECT_CLASS_TYPE (klass),
203 G_STRUCT_OFFSET (GtkInputDialogClass, enable_device),
205 _gtk_marshal_VOID__OBJECT,
209 input_dialog_signals[DISABLE_DEVICE] =
210 g_signal_new ("disable_device",
211 G_OBJECT_CLASS_TYPE (klass),
213 G_STRUCT_OFFSET (GtkInputDialogClass, disable_device),
215 _gtk_marshal_VOID__OBJECT,
221 gtk_input_dialog_init (GtkInputDialog *inputd)
223 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
226 GtkWidget *mapping_menu;
228 GtkWidget *separator;
231 gtk_widget_push_composite_child ();
233 gtk_window_set_title (GTK_WINDOW (inputd), _("Input"));
237 private->main_vbox = gtk_vbox_new (FALSE, 4);
238 gtk_container_set_border_width (GTK_CONTAINER (private->main_vbox), 5);
239 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox), private->main_vbox,
242 private->no_devices_label = gtk_label_new (_("No extended input devices"));
243 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox),
244 private->no_devices_label,
247 /* menu for selecting device */
249 private->device_menu = gtk_menu_new ();
251 util_box = gtk_hbox_new (FALSE, 2);
252 gtk_box_pack_start (GTK_BOX (private->main_vbox), util_box, FALSE, FALSE, 0);
254 label = gtk_label_new_with_mnemonic (_("_Device:"));
255 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
257 private->device_optionmenu = gtk_option_menu_new ();
258 gtk_label_set_mnemonic_widget (GTK_LABEL (label), private->device_optionmenu);
259 gtk_box_pack_start (GTK_BOX (util_box), private->device_optionmenu, TRUE, TRUE, 2);
260 gtk_widget_show (private->device_optionmenu);
261 gtk_option_menu_set_menu (GTK_OPTION_MENU (private->device_optionmenu), private->device_menu);
263 gtk_widget_show (label);
267 /* mapping mode option menu */
269 mapping_menu = gtk_menu_new ();
271 menuitem = gtk_menu_item_new_with_label(_("Disabled"));
272 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
273 gtk_widget_show (menuitem);
274 g_signal_connect (menuitem, "activate",
275 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
276 GINT_TO_POINTER (GDK_MODE_DISABLED));
278 menuitem = gtk_menu_item_new_with_label(_("Screen"));
279 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
280 gtk_widget_show (menuitem);
281 g_signal_connect (menuitem, "activate",
282 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
283 GINT_TO_POINTER (GDK_MODE_SCREEN));
285 menuitem = gtk_menu_item_new_with_label(_("Window"));
286 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
287 gtk_widget_show (menuitem);
288 g_signal_connect (menuitem, "activate",
289 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
290 GINT_TO_POINTER (GDK_MODE_WINDOW));
292 label = gtk_label_new_with_mnemonic (_("_Mode: "));
293 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
295 inputd->mode_optionmenu = gtk_option_menu_new ();
296 gtk_label_set_mnemonic_widget (GTK_LABEL (label), inputd->mode_optionmenu);
297 gtk_box_pack_start (GTK_BOX (util_box), inputd->mode_optionmenu, FALSE, FALSE, 2);
298 gtk_widget_show (inputd->mode_optionmenu);
299 gtk_option_menu_set_menu (GTK_OPTION_MENU (inputd->mode_optionmenu), mapping_menu);
301 gtk_widget_show(label);
303 gtk_widget_show (util_box);
305 util_box = gtk_hbox_new (FALSE, 2);
306 gtk_box_pack_start (GTK_BOX(private->main_vbox), util_box, FALSE, FALSE, 0);
308 gtk_widget_show (label);
309 gtk_widget_show (util_box);
311 separator = gtk_hseparator_new();
312 gtk_box_pack_start (GTK_BOX (private->main_vbox), separator, FALSE, TRUE, 0);
313 gtk_widget_show (separator);
317 notebook = gtk_notebook_new ();
318 gtk_box_pack_start (GTK_BOX (private->main_vbox), notebook, TRUE, TRUE, 0);
319 gtk_widget_show (notebook);
321 /* The axis listbox */
323 label = gtk_label_new_with_mnemonic (_("_Axes"));
325 inputd->axis_listbox = gtk_scrolled_window_new (NULL, NULL);
326 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(inputd->axis_listbox),
327 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
329 gtk_widget_set_size_request (inputd->axis_listbox,
330 AXIS_LIST_WIDTH, AXIS_LIST_HEIGHT);
331 gtk_notebook_append_page (GTK_NOTEBOOK(notebook),
332 inputd->axis_listbox, label);
334 gtk_widget_show (inputd->axis_listbox);
336 inputd->axis_list = 0;
340 label = gtk_label_new_with_mnemonic (_("_Keys"));
342 inputd->keys_listbox = gtk_scrolled_window_new (NULL, NULL);
343 gtk_widget_set_size_request (inputd->keys_listbox,
344 KEYS_LIST_WIDTH, KEYS_LIST_HEIGHT);
345 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
346 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
347 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
348 inputd->keys_listbox, label);
350 gtk_widget_show (inputd->keys_listbox);
352 inputd->keys_list = 0;
354 inputd->save_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
355 GTK_WIDGET_SET_FLAGS (inputd->save_button, GTK_CAN_DEFAULT);
356 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
357 inputd->save_button, TRUE, TRUE, 0);
358 gtk_widget_show (inputd->save_button);
360 inputd->close_button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
361 GTK_WIDGET_SET_FLAGS (inputd->close_button, GTK_CAN_DEFAULT);
362 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
363 inputd->close_button, TRUE, TRUE, 0);
365 gtk_widget_show (inputd->close_button);
366 gtk_widget_grab_default (inputd->close_button);
368 gtk_widget_pop_composite_child ();
370 gtk_input_dialog_screen_changed (GTK_WIDGET (inputd), NULL);
374 gtk_input_dialog_screen_changed (GtkWidget *widget,
375 GdkScreen *previous_screen)
377 GtkInputDialog *inputd = GTK_INPUT_DIALOG (widget);
378 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
380 GList *device_info = NULL;
381 GdkDevice *core_pointer = NULL;
384 if (gtk_widget_has_screen (widget))
388 display = gtk_widget_get_display (widget);
389 device_info = gdk_display_list_devices (display);
390 core_pointer = gdk_display_get_core_pointer (display);
393 inputd->current_device = NULL;
394 gtk_container_foreach (GTK_CONTAINER (private->device_menu),
395 (GtkCallback)gtk_widget_destroy, NULL);
397 if (g_list_length(device_info) <= 1) /* only core device */
399 gtk_widget_hide (private->main_vbox);
400 gtk_widget_show (private->no_devices_label);
401 gtk_widget_set_sensitive(inputd->save_button, FALSE);
405 gtk_widget_show (private->main_vbox);
406 gtk_widget_hide (private->no_devices_label);
407 gtk_widget_set_sensitive(inputd->save_button, TRUE);
409 for (tmp_list = device_info; tmp_list; tmp_list = tmp_list->next)
411 GdkDevice *info = tmp_list->data;
412 if (info != core_pointer)
416 menuitem = gtk_menu_item_new_with_label (info->name);
418 gtk_menu_shell_append (GTK_MENU_SHELL (private->device_menu),
420 gtk_widget_show (menuitem);
421 g_signal_connect (menuitem, "activate",
422 G_CALLBACK (gtk_input_dialog_set_device),
427 gtk_input_dialog_set_device (widget, device_info->data);
428 gtk_option_menu_set_history (GTK_OPTION_MENU (private->device_optionmenu), 0);
433 gtk_input_dialog_new (void)
435 GtkInputDialog *inputd;
437 inputd = g_object_new (GTK_TYPE_INPUT_DIALOG, NULL);
439 return GTK_WIDGET (inputd);
443 gtk_input_dialog_set_device (GtkWidget *w,
446 GdkDevice *device = data;
447 GtkInputDialog *inputd = input_dialog_from_widget (w);
449 inputd->current_device = device;
451 gtk_input_dialog_fill_axes (inputd, device);
452 gtk_input_dialog_fill_keys (inputd, device);
454 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
459 gtk_input_dialog_set_mapping_mode (GtkWidget *w,
462 GtkInputDialog *inputd = input_dialog_from_widget (w);
463 GdkDevice *info = inputd->current_device;
464 GdkInputMode old_mode = info->mode;
465 GdkInputMode mode = GPOINTER_TO_INT (data);
470 if (mode != old_mode)
472 if (gdk_device_set_mode (info, mode))
474 if (mode == GDK_MODE_DISABLED)
475 g_signal_emit (inputd,
476 input_dialog_signals[DISABLE_DEVICE],
480 g_signal_emit (inputd,
481 input_dialog_signals[ENABLE_DEVICE],
486 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
489 /* FIXME: error dialog ? */
494 gtk_input_dialog_set_axis (GtkWidget *w,
497 GdkAxisUse use = GPOINTER_TO_INT(data) & 0xFFFF;
499 GdkAxisUse *new_axes;
500 GtkInputDialog *inputd = input_dialog_from_widget (w);
501 GdkDevice *info = inputd->current_device;
503 gint axis = (GPOINTER_TO_INT(data) >> 16) - 1;
510 new_axes = g_new (GdkAxisUse, info->num_axes);
512 for (i=0;i<info->num_axes;i++)
514 new_axes[i] = info->axes[i].use;
515 if (info->axes[i].use == use)
520 old_use = info->axes[axis].use;
522 old_use = GDK_AXIS_IGNORE;
524 if (axis == old_axis)
527 /* we must always have an x and a y axis */
528 if ((axis == -1 && (use == GDK_AXIS_X || use == GDK_AXIS_Y)) ||
529 (old_axis == -1 && (old_use == GDK_AXIS_X || old_use == GDK_AXIS_Y)))
531 gtk_option_menu_set_history (
532 GTK_OPTION_MENU (inputd->axis_items[use]),
538 gdk_device_set_axis_use (info, axis, use);
541 gdk_device_set_axis_use (info, old_axis, old_use);
543 if (old_use != GDK_AXIS_IGNORE)
545 gtk_option_menu_set_history (
546 GTK_OPTION_MENU (inputd->axis_items[old_use]),
555 gtk_input_dialog_fill_axes(GtkInputDialog *inputd, GdkDevice *info)
557 static const char *axis_use_strings[GDK_AXIS_LAST] =
570 GtkWidget *option_menu;
573 /* remove all the old items */
574 if (inputd->axis_list)
576 gtk_widget_hide (inputd->axis_list); /* suppress resizes (or get warnings) */
577 gtk_widget_destroy (inputd->axis_list);
579 inputd->axis_list = gtk_table_new (GDK_AXIS_LAST, 2, 0);
580 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->axis_listbox),
582 gtk_widget_show (inputd->axis_list);
584 gtk_widget_realize (inputd->axis_list);
585 gdk_window_set_background (inputd->axis_list->window,
586 &inputd->axis_list->style->base[GTK_STATE_NORMAL]);
588 for (i=GDK_AXIS_X;i<GDK_AXIS_LAST;i++)
590 /* create the label */
592 label = gtk_label_new (_(axis_use_strings[i]));
593 gtk_table_attach (GTK_TABLE (inputd->axis_list), label, 0, 1, i, i+1,
596 /* and the use option menu */
597 menu = gtk_menu_new();
599 for (j = -1; j < info->num_axes; j++)
602 GtkWidget *menu_item;
605 menu_item = gtk_menu_item_new_with_label (_("none"));
608 g_snprintf (buffer, sizeof (buffer), "%d", j+1);
609 menu_item = gtk_menu_item_new_with_label (buffer);
611 g_signal_connect (menu_item, "activate",
612 G_CALLBACK (gtk_input_dialog_set_axis),
613 GINT_TO_POINTER (0x10000 * (j + 1) + i));
614 gtk_widget_show (menu_item);
615 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
618 inputd->axis_items[i] = option_menu = gtk_option_menu_new ();
619 gtk_table_attach (GTK_TABLE (inputd->axis_list), option_menu,
620 1, 2, i, i+1, 0, 0, 2, 2);
622 gtk_widget_show (option_menu);
623 gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
624 for (j = 0; j < info->num_axes; j++)
625 if (info->axes[j].use == (GdkAxisUse) i)
627 gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu), j+1);
631 gtk_widget_show (label);
636 gtk_input_dialog_clear_key (GtkWidget *widget, GtkInputKeyInfo *key)
638 if (!key->inputd->current_device)
641 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
642 gdk_device_set_key (key->inputd->current_device, key->index, 0, 0);
646 gtk_input_dialog_set_key (GtkInputKeyInfo *key,
647 guint keyval, GdkModifierType modifiers)
654 str = g_string_new (NULL);
656 if (modifiers & GDK_SHIFT_MASK)
657 g_string_append (str, "Shift+");
658 if (modifiers & GDK_CONTROL_MASK)
659 g_string_append (str, "Ctrl+");
660 if (modifiers & GDK_MOD1_MASK)
661 g_string_append (str, "Alt+");
663 if ((keyval >= 0x20) && (keyval <= 0xFF))
667 g_string_append (str, chars);
670 g_string_append (str, _("(unknown)"));
671 gtk_entry_set_text (GTK_ENTRY(key->entry), str->str);
673 g_string_free (str, TRUE);
677 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
682 gtk_input_dialog_key_press (GtkWidget *widget,
684 GtkInputKeyInfo *key)
686 if (!key->inputd->current_device)
689 gtk_input_dialog_set_key (key, event->keyval, event->state & 0xFF);
690 gdk_device_set_key (key->inputd->current_device, key->index,
691 event->keyval, event->state & 0xFF);
693 g_signal_stop_emission_by_name (widget, "key_press_event");
699 gtk_input_dialog_destroy_key (GtkWidget *widget, GtkInputKeyInfo *key)
705 gtk_input_dialog_fill_keys(GtkInputDialog *inputd, GdkDevice *info)
713 /* remove all the old items */
714 if (inputd->keys_list)
716 gtk_widget_hide (inputd->keys_list); /* suppress resizes (or get warnings) */
717 gtk_widget_destroy (inputd->keys_list);
720 inputd->keys_list = gtk_table_new (info->num_keys, 3, FALSE);
721 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
723 gtk_widget_show (inputd->keys_list);
725 gtk_widget_realize (inputd->keys_list);
726 gdk_window_set_background (inputd->keys_list->window,
727 &inputd->keys_list->style->base[GTK_STATE_NORMAL]);
729 for (i=0;i<info->num_keys;i++)
731 GtkInputKeyInfo *key = g_new (GtkInputKeyInfo, 1);
733 key->inputd = inputd;
735 /* create the label */
737 g_snprintf (buffer, sizeof (buffer), "%d", i+1);
738 label = gtk_label_new (buffer);
739 gtk_table_attach (GTK_TABLE (inputd->keys_list), label, 0, 1, i, i+1,
741 gtk_widget_show (label);
745 key->entry = gtk_entry_new ();
746 gtk_table_attach (GTK_TABLE (inputd->keys_list), key->entry, 1, 2, i, i+1,
747 GTK_EXPAND | GTK_FILL , 0, 2, 2);
748 gtk_widget_show (key->entry);
750 g_signal_connect (key->entry, "key_press_event",
751 G_CALLBACK (gtk_input_dialog_key_press), key);
752 g_signal_connect (key->entry, "destroy",
753 G_CALLBACK (gtk_input_dialog_destroy_key), key);
755 /* and clear button */
757 button = gtk_button_new_with_label (_("clear"));
758 gtk_table_attach (GTK_TABLE (inputd->keys_list), button, 2, 3, i, i+1,
760 gtk_widget_show (button);
762 g_signal_connect (button, "clicked",
763 G_CALLBACK (gtk_input_dialog_clear_key), key);
765 gtk_input_dialog_set_key (key, info->keys[i].keyval,
766 info->keys[i].modifiers);