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/.
36 #include <glib/gprintf.h>
38 #include "gdk/gdkkeysyms.h"
39 #include "gtkbutton.h"
42 #include "gtkhseparator.h"
43 #include "gtkinputdialog.h"
45 #include "gtklistitem.h"
47 #include "gtkmarshalers.h"
49 #include "gtkmenuitem.h"
50 #include "gtknotebook.h"
51 #include "gtkscrolledwindow.h"
56 #undef GTK_DISABLE_DEPRECATED
57 #include "gtkoptionmenu.h"
58 #define GTK_DISABLE_DEPRECATED
62 typedef struct _GtkInputDialogPrivate GtkInputDialogPrivate;
63 typedef struct _GtkInputKeyInfo GtkInputKeyInfo;
65 struct _GtkInputDialogPrivate
67 GtkWidget *device_menu;
68 GtkWidget *device_optionmenu;
69 GtkWidget *no_devices_label;
73 struct _GtkInputKeyInfo
77 GtkInputDialog *inputd;
88 #define AXIS_LIST_WIDTH 160
89 #define AXIS_LIST_HEIGHT 175
91 #define KEYS_LIST_WIDTH 200
92 #define KEYS_LIST_HEIGHT 175
94 /* Forward declarations */
96 static void gtk_input_dialog_class_init (GtkInputDialogClass *klass);
97 static void gtk_input_dialog_init (GtkInputDialog *inputd);
98 static void gtk_input_dialog_screen_changed (GtkWidget *widget,
99 GdkScreen *previous_screen);
100 static void gtk_input_dialog_set_device (GtkWidget *widget,
102 static void gtk_input_dialog_set_mapping_mode (GtkWidget *w,
104 static void gtk_input_dialog_set_axis (GtkWidget *widget,
106 static void gtk_input_dialog_fill_axes (GtkInputDialog *inputd,
108 static void gtk_input_dialog_set_key (GtkInputKeyInfo *key,
110 GdkModifierType modifiers);
111 static gboolean gtk_input_dialog_key_press (GtkWidget *widget,
113 GtkInputKeyInfo *key);
114 static void gtk_input_dialog_clear_key (GtkWidget *widget,
115 GtkInputKeyInfo *key);
116 static void gtk_input_dialog_destroy_key (GtkWidget *widget,
117 GtkInputKeyInfo *key);
118 static void gtk_input_dialog_fill_keys (GtkInputDialog *inputd,
121 static GtkObjectClass *parent_class = NULL;
122 static guint input_dialog_signals[LAST_SIGNAL] = { 0 };
125 gtk_input_dialog_get_type (void)
127 static GType input_dialog_type = 0;
129 if (!input_dialog_type)
131 static const GTypeInfo input_dialog_info =
133 sizeof (GtkInputDialogClass),
134 NULL, /* base_init */
135 NULL, /* base_finalize */
136 (GClassInitFunc) gtk_input_dialog_class_init,
137 NULL, /* class_finalize */
138 NULL, /* class_data */
139 sizeof (GtkInputDialog),
141 (GInstanceInitFunc) gtk_input_dialog_init,
145 g_type_register_static (GTK_TYPE_DIALOG, "GtkInputDialog",
146 &input_dialog_info, 0);
149 return input_dialog_type;
152 GtkInputDialogPrivate *
153 gtk_input_dialog_get_private (GtkInputDialog *input_dialog)
155 GtkInputDialogPrivate *private;
156 static GQuark private_quark = 0;
159 private_quark = g_quark_from_static_string ("gtk-input-dialog-private");
161 private = g_object_get_qdata (G_OBJECT (input_dialog), private_quark);
165 private = g_new0 (GtkInputDialogPrivate, 1);
166 g_object_set_qdata_full (G_OBJECT (input_dialog), private_quark,
173 static GtkInputDialog *
174 input_dialog_from_widget (GtkWidget *widget)
178 if (GTK_IS_MENU_ITEM (widget))
180 GtkMenu *menu = GTK_MENU (widget->parent);
181 widget = gtk_menu_get_attach_widget (menu);
184 toplevel = gtk_widget_get_toplevel (widget);
185 return GTK_INPUT_DIALOG (toplevel);
189 gtk_input_dialog_class_init (GtkInputDialogClass *klass)
191 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
193 parent_class = g_type_class_peek_parent (klass);
195 widget_class->screen_changed = gtk_input_dialog_screen_changed;
197 klass->enable_device = NULL;
198 klass->disable_device = NULL;
200 input_dialog_signals[ENABLE_DEVICE] =
201 g_signal_new ("enable_device",
202 G_OBJECT_CLASS_TYPE (klass),
204 G_STRUCT_OFFSET (GtkInputDialogClass, enable_device),
206 _gtk_marshal_VOID__OBJECT,
210 input_dialog_signals[DISABLE_DEVICE] =
211 g_signal_new ("disable_device",
212 G_OBJECT_CLASS_TYPE (klass),
214 G_STRUCT_OFFSET (GtkInputDialogClass, disable_device),
216 _gtk_marshal_VOID__OBJECT,
222 gtk_input_dialog_init (GtkInputDialog *inputd)
224 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
227 GtkWidget *mapping_menu;
229 GtkWidget *separator;
232 gtk_widget_push_composite_child ();
234 gtk_window_set_title (GTK_WINDOW (inputd), _("Input"));
238 private->main_vbox = gtk_vbox_new (FALSE, 4);
239 gtk_container_set_border_width (GTK_CONTAINER (private->main_vbox), 5);
240 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox), private->main_vbox,
243 private->no_devices_label = gtk_label_new (_("No extended input devices"));
244 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox),
245 private->no_devices_label,
248 /* menu for selecting device */
250 private->device_menu = gtk_menu_new ();
252 util_box = gtk_hbox_new (FALSE, 2);
253 gtk_box_pack_start (GTK_BOX (private->main_vbox), util_box, FALSE, FALSE, 0);
255 label = gtk_label_new_with_mnemonic (_("_Device:"));
256 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
258 private->device_optionmenu = gtk_option_menu_new ();
259 gtk_label_set_mnemonic_widget (GTK_LABEL (label), private->device_optionmenu);
260 gtk_box_pack_start (GTK_BOX (util_box), private->device_optionmenu, TRUE, TRUE, 2);
261 gtk_widget_show (private->device_optionmenu);
262 gtk_option_menu_set_menu (GTK_OPTION_MENU (private->device_optionmenu), private->device_menu);
264 gtk_widget_show (label);
268 /* mapping mode option menu */
270 mapping_menu = gtk_menu_new ();
272 menuitem = gtk_menu_item_new_with_label(_("Disabled"));
273 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
274 gtk_widget_show (menuitem);
275 g_signal_connect (menuitem, "activate",
276 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
277 GINT_TO_POINTER (GDK_MODE_DISABLED));
279 menuitem = gtk_menu_item_new_with_label(_("Screen"));
280 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
281 gtk_widget_show (menuitem);
282 g_signal_connect (menuitem, "activate",
283 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
284 GINT_TO_POINTER (GDK_MODE_SCREEN));
286 menuitem = gtk_menu_item_new_with_label(_("Window"));
287 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
288 gtk_widget_show (menuitem);
289 g_signal_connect (menuitem, "activate",
290 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
291 GINT_TO_POINTER (GDK_MODE_WINDOW));
293 label = gtk_label_new_with_mnemonic (_("_Mode: "));
294 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
296 inputd->mode_optionmenu = gtk_option_menu_new ();
297 gtk_label_set_mnemonic_widget (GTK_LABEL (label), inputd->mode_optionmenu);
298 gtk_box_pack_start (GTK_BOX (util_box), inputd->mode_optionmenu, FALSE, FALSE, 2);
299 gtk_widget_show (inputd->mode_optionmenu);
300 gtk_option_menu_set_menu (GTK_OPTION_MENU (inputd->mode_optionmenu), mapping_menu);
302 gtk_widget_show(label);
304 gtk_widget_show (util_box);
306 util_box = gtk_hbox_new (FALSE, 2);
307 gtk_box_pack_start (GTK_BOX(private->main_vbox), util_box, FALSE, FALSE, 0);
309 gtk_widget_show (label);
310 gtk_widget_show (util_box);
312 separator = gtk_hseparator_new();
313 gtk_box_pack_start (GTK_BOX (private->main_vbox), separator, FALSE, TRUE, 0);
314 gtk_widget_show (separator);
318 notebook = gtk_notebook_new ();
319 gtk_box_pack_start (GTK_BOX (private->main_vbox), notebook, TRUE, TRUE, 0);
320 gtk_widget_show (notebook);
322 /* The axis listbox */
324 label = gtk_label_new_with_mnemonic (_("_Axes"));
326 inputd->axis_listbox = gtk_scrolled_window_new (NULL, NULL);
327 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(inputd->axis_listbox),
328 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
330 gtk_widget_set_size_request (inputd->axis_listbox,
331 AXIS_LIST_WIDTH, AXIS_LIST_HEIGHT);
332 gtk_notebook_append_page (GTK_NOTEBOOK(notebook),
333 inputd->axis_listbox, label);
335 gtk_widget_show (inputd->axis_listbox);
337 inputd->axis_list = 0;
341 label = gtk_label_new_with_mnemonic (_("_Keys"));
343 inputd->keys_listbox = gtk_scrolled_window_new (NULL, NULL);
344 gtk_widget_set_size_request (inputd->keys_listbox,
345 KEYS_LIST_WIDTH, KEYS_LIST_HEIGHT);
346 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
347 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
348 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
349 inputd->keys_listbox, label);
351 gtk_widget_show (inputd->keys_listbox);
353 inputd->keys_list = 0;
355 inputd->save_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
356 GTK_WIDGET_SET_FLAGS (inputd->save_button, GTK_CAN_DEFAULT);
357 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
358 inputd->save_button, TRUE, TRUE, 0);
359 gtk_widget_show (inputd->save_button);
361 inputd->close_button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
362 GTK_WIDGET_SET_FLAGS (inputd->close_button, GTK_CAN_DEFAULT);
363 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
364 inputd->close_button, TRUE, TRUE, 0);
366 gtk_widget_show (inputd->close_button);
367 gtk_widget_grab_default (inputd->close_button);
369 gtk_widget_pop_composite_child ();
371 gtk_input_dialog_screen_changed (GTK_WIDGET (inputd), NULL);
375 gtk_input_dialog_screen_changed (GtkWidget *widget,
376 GdkScreen *previous_screen)
378 GtkInputDialog *inputd = GTK_INPUT_DIALOG (widget);
379 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
381 GList *device_info = NULL;
382 GdkDevice *core_pointer = NULL;
385 if (gtk_widget_has_screen (widget))
389 display = gtk_widget_get_display (widget);
390 device_info = gdk_display_list_devices (display);
391 core_pointer = gdk_display_get_core_pointer (display);
394 inputd->current_device = NULL;
395 gtk_container_foreach (GTK_CONTAINER (private->device_menu),
396 (GtkCallback)gtk_widget_destroy, NULL);
398 if (g_list_length(device_info) <= 1) /* only core device */
400 gtk_widget_hide (private->main_vbox);
401 gtk_widget_show (private->no_devices_label);
402 gtk_widget_set_sensitive(inputd->save_button, FALSE);
406 gtk_widget_show (private->main_vbox);
407 gtk_widget_hide (private->no_devices_label);
408 gtk_widget_set_sensitive(inputd->save_button, TRUE);
410 for (tmp_list = device_info; tmp_list; tmp_list = tmp_list->next)
412 GdkDevice *info = tmp_list->data;
413 if (info != core_pointer)
417 menuitem = gtk_menu_item_new_with_label (info->name);
419 gtk_menu_shell_append (GTK_MENU_SHELL (private->device_menu),
421 gtk_widget_show (menuitem);
422 g_signal_connect (menuitem, "activate",
423 G_CALLBACK (gtk_input_dialog_set_device),
428 gtk_input_dialog_set_device (widget, device_info->data);
429 gtk_option_menu_set_history (GTK_OPTION_MENU (private->device_optionmenu), 0);
434 gtk_input_dialog_new (void)
436 GtkInputDialog *inputd;
438 inputd = g_object_new (GTK_TYPE_INPUT_DIALOG, NULL);
440 return GTK_WIDGET (inputd);
444 gtk_input_dialog_set_device (GtkWidget *w,
447 GdkDevice *device = data;
448 GtkInputDialog *inputd = input_dialog_from_widget (w);
450 inputd->current_device = device;
452 gtk_input_dialog_fill_axes (inputd, device);
453 gtk_input_dialog_fill_keys (inputd, device);
455 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
460 gtk_input_dialog_set_mapping_mode (GtkWidget *w,
463 GtkInputDialog *inputd = input_dialog_from_widget (w);
464 GdkDevice *info = inputd->current_device;
465 GdkInputMode old_mode = info->mode;
466 GdkInputMode mode = GPOINTER_TO_INT (data);
471 if (mode != old_mode)
473 if (gdk_device_set_mode (info, mode))
475 if (mode == GDK_MODE_DISABLED)
476 g_signal_emit (inputd,
477 input_dialog_signals[DISABLE_DEVICE],
481 g_signal_emit (inputd,
482 input_dialog_signals[ENABLE_DEVICE],
487 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
490 /* FIXME: error dialog ? */
495 gtk_input_dialog_set_axis (GtkWidget *w,
498 GdkAxisUse use = GPOINTER_TO_INT(data) & 0xFFFF;
500 GdkAxisUse *new_axes;
501 GtkInputDialog *inputd = input_dialog_from_widget (w);
502 GdkDevice *info = inputd->current_device;
504 gint axis = (GPOINTER_TO_INT(data) >> 16) - 1;
511 new_axes = g_new (GdkAxisUse, info->num_axes);
513 for (i=0;i<info->num_axes;i++)
515 new_axes[i] = info->axes[i].use;
516 if (info->axes[i].use == use)
521 old_use = info->axes[axis].use;
523 old_use = GDK_AXIS_IGNORE;
525 if (axis == old_axis)
528 /* we must always have an x and a y axis */
529 if ((axis == -1 && (use == GDK_AXIS_X || use == GDK_AXIS_Y)) ||
530 (old_axis == -1 && (old_use == GDK_AXIS_X || old_use == GDK_AXIS_Y)))
532 gtk_option_menu_set_history (
533 GTK_OPTION_MENU (inputd->axis_items[use]),
539 gdk_device_set_axis_use (info, axis, use);
542 gdk_device_set_axis_use (info, old_axis, old_use);
544 if (old_use != GDK_AXIS_IGNORE)
546 gtk_option_menu_set_history (
547 GTK_OPTION_MENU (inputd->axis_items[old_use]),
556 gtk_input_dialog_fill_axes(GtkInputDialog *inputd, GdkDevice *info)
558 static const char *axis_use_strings[GDK_AXIS_LAST] =
571 GtkWidget *option_menu;
574 /* remove all the old items */
575 if (inputd->axis_list)
577 gtk_widget_hide (inputd->axis_list); /* suppress resizes (or get warnings) */
578 gtk_widget_destroy (inputd->axis_list);
580 inputd->axis_list = gtk_table_new (GDK_AXIS_LAST, 2, 0);
581 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->axis_listbox),
583 gtk_widget_show (inputd->axis_list);
585 gtk_widget_realize (inputd->axis_list);
586 gdk_window_set_background (inputd->axis_list->window,
587 &inputd->axis_list->style->base[GTK_STATE_NORMAL]);
589 for (i=GDK_AXIS_X;i<GDK_AXIS_LAST;i++)
591 /* create the label */
593 label = gtk_label_new (_(axis_use_strings[i]));
594 gtk_table_attach (GTK_TABLE (inputd->axis_list), label, 0, 1, i, i+1,
597 /* and the use option menu */
598 menu = gtk_menu_new();
600 for (j = -1; j < info->num_axes; j++)
603 GtkWidget *menu_item;
606 menu_item = gtk_menu_item_new_with_label (_("none"));
609 g_snprintf (buffer, sizeof (buffer), "%d", j+1);
610 menu_item = gtk_menu_item_new_with_label (buffer);
612 g_signal_connect (menu_item, "activate",
613 G_CALLBACK (gtk_input_dialog_set_axis),
614 GINT_TO_POINTER (0x10000 * (j + 1) + i));
615 gtk_widget_show (menu_item);
616 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
619 inputd->axis_items[i] = option_menu = gtk_option_menu_new ();
620 gtk_table_attach (GTK_TABLE (inputd->axis_list), option_menu,
621 1, 2, i, i+1, 0, 0, 2, 2);
623 gtk_widget_show (option_menu);
624 gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
625 for (j = 0; j < info->num_axes; j++)
626 if (info->axes[j].use == (GdkAxisUse) i)
628 gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu), j+1);
632 gtk_widget_show (label);
637 gtk_input_dialog_clear_key (GtkWidget *widget, GtkInputKeyInfo *key)
639 if (!key->inputd->current_device)
642 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
643 gdk_device_set_key (key->inputd->current_device, key->index, 0, 0);
647 gtk_input_dialog_set_key (GtkInputKeyInfo *key,
648 guint keyval, GdkModifierType modifiers)
655 str = g_string_new (NULL);
657 if (modifiers & GDK_SHIFT_MASK)
658 g_string_append (str, "Shift+");
659 if (modifiers & GDK_CONTROL_MASK)
660 g_string_append (str, "Ctrl+");
661 if (modifiers & GDK_MOD1_MASK)
662 g_string_append (str, "Alt+");
664 if ((keyval >= 0x20) && (keyval <= 0xFF))
668 g_string_append (str, chars);
671 g_string_append (str, _("(unknown)"));
672 gtk_entry_set_text (GTK_ENTRY(key->entry), str->str);
674 g_string_free (str, TRUE);
678 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
683 gtk_input_dialog_key_press (GtkWidget *widget,
685 GtkInputKeyInfo *key)
687 if (!key->inputd->current_device)
690 gtk_input_dialog_set_key (key, event->keyval, event->state & 0xFF);
691 gdk_device_set_key (key->inputd->current_device, key->index,
692 event->keyval, event->state & 0xFF);
694 g_signal_stop_emission_by_name (widget, "key_press_event");
700 gtk_input_dialog_destroy_key (GtkWidget *widget, GtkInputKeyInfo *key)
706 gtk_input_dialog_fill_keys(GtkInputDialog *inputd, GdkDevice *info)
714 /* remove all the old items */
715 if (inputd->keys_list)
717 gtk_widget_hide (inputd->keys_list); /* suppress resizes (or get warnings) */
718 gtk_widget_destroy (inputd->keys_list);
721 inputd->keys_list = gtk_table_new (info->num_keys, 3, FALSE);
722 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
724 gtk_widget_show (inputd->keys_list);
726 gtk_widget_realize (inputd->keys_list);
727 gdk_window_set_background (inputd->keys_list->window,
728 &inputd->keys_list->style->base[GTK_STATE_NORMAL]);
730 for (i=0;i<info->num_keys;i++)
732 GtkInputKeyInfo *key = g_new (GtkInputKeyInfo, 1);
734 key->inputd = inputd;
736 /* create the label */
738 g_snprintf (buffer, sizeof (buffer), "%d", i+1);
739 label = gtk_label_new (buffer);
740 gtk_table_attach (GTK_TABLE (inputd->keys_list), label, 0, 1, i, i+1,
742 gtk_widget_show (label);
746 key->entry = gtk_entry_new ();
747 gtk_table_attach (GTK_TABLE (inputd->keys_list), key->entry, 1, 2, i, i+1,
748 GTK_EXPAND | GTK_FILL , 0, 2, 2);
749 gtk_widget_show (key->entry);
751 g_signal_connect (key->entry, "key_press_event",
752 G_CALLBACK (gtk_input_dialog_key_press), key);
753 g_signal_connect (key->entry, "destroy",
754 G_CALLBACK (gtk_input_dialog_destroy_key), key);
756 /* and clear button */
758 button = gtk_button_new_with_label (_("clear"));
759 gtk_table_attach (GTK_TABLE (inputd->keys_list), button, 2, 3, i, i+1,
761 gtk_widget_show (button);
763 g_signal_connect (button, "clicked",
764 G_CALLBACK (gtk_input_dialog_clear_key), key);
766 gtk_input_dialog_set_key (key, info->keys[i].keyval,
767 info->keys[i].modifiers);