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/.
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 "gtkoptionmenu.h"
51 #include "gtkscrolledwindow.h"
58 typedef struct _GtkInputDialogPrivate GtkInputDialogPrivate;
59 typedef struct _GtkInputKeyInfo GtkInputKeyInfo;
61 struct _GtkInputDialogPrivate
63 GtkWidget *device_menu;
64 GtkWidget *device_optionmenu;
65 GtkWidget *no_devices_label;
69 struct _GtkInputKeyInfo
73 GtkInputDialog *inputd;
84 #define AXIS_LIST_WIDTH 160
85 #define AXIS_LIST_HEIGHT 175
87 #define KEYS_LIST_WIDTH 200
88 #define KEYS_LIST_HEIGHT 175
90 /* Forward declarations */
92 static void gtk_input_dialog_class_init (GtkInputDialogClass *klass);
93 static void gtk_input_dialog_init (GtkInputDialog *inputd);
94 static void gtk_input_dialog_screen_changed (GtkWidget *widget,
95 GdkScreen *previous_screen);
96 static void gtk_input_dialog_set_device (GtkWidget *widget,
98 static void gtk_input_dialog_set_mapping_mode (GtkWidget *w,
100 static void gtk_input_dialog_set_axis (GtkWidget *widget,
102 static void gtk_input_dialog_fill_axes (GtkInputDialog *inputd,
104 static void gtk_input_dialog_set_key (GtkInputKeyInfo *key,
106 GdkModifierType modifiers);
107 static gboolean gtk_input_dialog_key_press (GtkWidget *widget,
109 GtkInputKeyInfo *key);
110 static void gtk_input_dialog_clear_key (GtkWidget *widget,
111 GtkInputKeyInfo *key);
112 static void gtk_input_dialog_destroy_key (GtkWidget *widget,
113 GtkInputKeyInfo *key);
114 static void gtk_input_dialog_fill_keys (GtkInputDialog *inputd,
117 static GtkObjectClass *parent_class = NULL;
118 static guint input_dialog_signals[LAST_SIGNAL] = { 0 };
121 gtk_input_dialog_get_type (void)
123 static GType input_dialog_type = 0;
125 if (!input_dialog_type)
127 static const GTypeInfo input_dialog_info =
129 sizeof (GtkInputDialogClass),
130 NULL, /* base_init */
131 NULL, /* base_finalize */
132 (GClassInitFunc) gtk_input_dialog_class_init,
133 NULL, /* class_finalize */
134 NULL, /* class_data */
135 sizeof (GtkInputDialog),
137 (GInstanceInitFunc) gtk_input_dialog_init,
141 g_type_register_static (GTK_TYPE_DIALOG, "GtkInputDialog",
142 &input_dialog_info, 0);
145 return input_dialog_type;
148 GtkInputDialogPrivate *
149 gtk_input_dialog_get_private (GtkInputDialog *menu)
151 GtkInputDialogPrivate *private;
152 static GQuark private_quark = 0;
155 private_quark = g_quark_from_static_string ("gtk-input-dialog-private");
157 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
161 private = g_new0 (GtkInputDialogPrivate, 1);
162 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
169 static GtkInputDialog *
170 input_dialog_from_widget (GtkWidget *widget)
174 if (GTK_IS_MENU_ITEM (widget))
176 GtkMenu *menu = GTK_MENU (widget->parent);
177 widget = gtk_menu_get_attach_widget (menu);
180 toplevel = gtk_widget_get_toplevel (widget);
181 return GTK_INPUT_DIALOG (toplevel);
185 gtk_input_dialog_class_init (GtkInputDialogClass *klass)
187 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
189 parent_class = g_type_class_peek_parent (klass);
191 widget_class->screen_changed = gtk_input_dialog_screen_changed;
193 klass->enable_device = NULL;
194 klass->disable_device = NULL;
196 input_dialog_signals[ENABLE_DEVICE] =
197 g_signal_new ("enable_device",
198 G_OBJECT_CLASS_TYPE (klass),
200 G_STRUCT_OFFSET (GtkInputDialogClass, enable_device),
202 _gtk_marshal_VOID__OBJECT,
206 input_dialog_signals[DISABLE_DEVICE] =
207 g_signal_new ("disable_device",
208 G_OBJECT_CLASS_TYPE (klass),
210 G_STRUCT_OFFSET (GtkInputDialogClass, disable_device),
212 _gtk_marshal_VOID__OBJECT,
218 gtk_input_dialog_init (GtkInputDialog *inputd)
220 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
223 GtkWidget *mapping_menu;
225 GtkWidget *separator;
228 gtk_widget_push_composite_child ();
230 gtk_window_set_title (GTK_WINDOW (inputd), _("Input"));
234 private->main_vbox = gtk_vbox_new (FALSE, 4);
235 gtk_container_set_border_width (GTK_CONTAINER (private->main_vbox), 5);
236 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox), private->main_vbox,
239 private->no_devices_label = gtk_label_new (_("No input devices"));
240 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox),
241 private->no_devices_label,
244 /* menu for selecting device */
246 private->device_menu = gtk_menu_new ();
248 util_box = gtk_hbox_new (FALSE, 2);
249 gtk_box_pack_start (GTK_BOX (private->main_vbox), util_box, FALSE, FALSE, 0);
251 label = gtk_label_new_with_mnemonic (_("_Device:"));
252 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
254 private->device_optionmenu = gtk_option_menu_new ();
255 gtk_label_set_mnemonic_widget (GTK_LABEL (label), private->device_optionmenu);
256 gtk_box_pack_start (GTK_BOX (util_box), private->device_optionmenu, TRUE, TRUE, 2);
257 gtk_widget_show (private->device_optionmenu);
258 gtk_option_menu_set_menu (GTK_OPTION_MENU (private->device_optionmenu), private->device_menu);
260 gtk_widget_show (label);
264 /* mapping mode option menu */
266 mapping_menu = gtk_menu_new ();
268 menuitem = gtk_menu_item_new_with_label(_("Disabled"));
269 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
270 gtk_widget_show (menuitem);
271 g_signal_connect (menuitem, "activate",
272 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
273 GINT_TO_POINTER (GDK_MODE_DISABLED));
275 menuitem = gtk_menu_item_new_with_label(_("Screen"));
276 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
277 gtk_widget_show (menuitem);
278 g_signal_connect (menuitem, "activate",
279 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
280 GINT_TO_POINTER (GDK_MODE_SCREEN));
282 menuitem = gtk_menu_item_new_with_label(_("Window"));
283 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
284 gtk_widget_show (menuitem);
285 g_signal_connect (menuitem, "activate",
286 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
287 GINT_TO_POINTER (GDK_MODE_WINDOW));
289 label = gtk_label_new_with_mnemonic (_("_Mode: "));
290 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
292 inputd->mode_optionmenu = gtk_option_menu_new ();
293 gtk_label_set_mnemonic_widget (GTK_LABEL (label), inputd->mode_optionmenu);
294 gtk_box_pack_start (GTK_BOX (util_box), inputd->mode_optionmenu, FALSE, FALSE, 2);
295 gtk_widget_show (inputd->mode_optionmenu);
296 gtk_option_menu_set_menu (GTK_OPTION_MENU (inputd->mode_optionmenu), mapping_menu);
298 gtk_widget_show(label);
300 gtk_widget_show (util_box);
302 util_box = gtk_hbox_new (FALSE, 2);
303 gtk_box_pack_start (GTK_BOX(private->main_vbox), util_box, FALSE, FALSE, 0);
305 gtk_widget_show (label);
306 gtk_widget_show (util_box);
308 separator = gtk_hseparator_new();
309 gtk_box_pack_start (GTK_BOX (private->main_vbox), separator, FALSE, TRUE, 0);
310 gtk_widget_show (separator);
314 notebook = gtk_notebook_new ();
315 gtk_box_pack_start (GTK_BOX (private->main_vbox), notebook, TRUE, TRUE, 0);
316 gtk_widget_show (notebook);
318 /* The axis listbox */
320 label = gtk_label_new_with_mnemonic (_("_Axes"));
322 inputd->axis_listbox = gtk_scrolled_window_new (NULL, NULL);
323 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(inputd->axis_listbox),
324 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
326 gtk_widget_set_size_request (inputd->axis_listbox,
327 AXIS_LIST_WIDTH, AXIS_LIST_HEIGHT);
328 gtk_notebook_append_page (GTK_NOTEBOOK(notebook),
329 inputd->axis_listbox, label);
331 gtk_widget_show (inputd->axis_listbox);
333 inputd->axis_list = 0;
337 label = gtk_label_new_with_mnemonic (_("_Keys"));
339 inputd->keys_listbox = gtk_scrolled_window_new (NULL, NULL);
340 gtk_widget_set_size_request (inputd->keys_listbox,
341 KEYS_LIST_WIDTH, KEYS_LIST_HEIGHT);
342 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
343 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
344 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
345 inputd->keys_listbox, label);
347 gtk_widget_show (inputd->keys_listbox);
349 inputd->keys_list = 0;
351 inputd->save_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
352 GTK_WIDGET_SET_FLAGS (inputd->save_button, GTK_CAN_DEFAULT);
353 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
354 inputd->save_button, TRUE, TRUE, 0);
355 gtk_widget_show (inputd->save_button);
357 inputd->close_button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
358 GTK_WIDGET_SET_FLAGS (inputd->close_button, GTK_CAN_DEFAULT);
359 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
360 inputd->close_button, TRUE, TRUE, 0);
362 gtk_widget_show (inputd->close_button);
363 gtk_widget_grab_default (inputd->close_button);
365 gtk_widget_pop_composite_child ();
367 gtk_input_dialog_screen_changed (GTK_WIDGET (inputd), NULL);
371 gtk_input_dialog_screen_changed (GtkWidget *widget,
372 GdkScreen *previous_screen)
374 GtkInputDialog *inputd = GTK_INPUT_DIALOG (widget);
375 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
377 GList *device_info = NULL;
378 GdkDevice *core_pointer = NULL;
381 if (gtk_widget_has_screen (widget))
385 display = gtk_widget_get_display (widget);
386 device_info = gdk_display_list_devices (display);
387 core_pointer = gdk_display_get_core_pointer (display);
390 inputd->current_device = NULL;
391 gtk_container_foreach (GTK_CONTAINER (private->device_menu),
392 (GtkCallback)gtk_widget_destroy, NULL);
394 if (g_list_length(device_info) <= 1) /* only core device */
396 gtk_widget_hide (private->main_vbox);
397 gtk_widget_show (private->no_devices_label);
398 gtk_widget_set_sensitive(inputd->save_button, FALSE);
402 gtk_widget_show (private->main_vbox);
403 gtk_widget_hide (private->no_devices_label);
404 gtk_widget_set_sensitive(inputd->save_button, TRUE);
406 for (tmp_list = device_info; tmp_list; tmp_list = tmp_list->next)
408 GdkDevice *info = tmp_list->data;
409 if (info != core_pointer)
413 menuitem = gtk_menu_item_new_with_label (info->name);
415 gtk_menu_shell_append (GTK_MENU_SHELL (private->device_menu),
417 gtk_widget_show (menuitem);
418 g_signal_connect (menuitem, "activate",
419 G_CALLBACK (gtk_input_dialog_set_device),
424 gtk_input_dialog_set_device (widget, device_info->data);
425 gtk_option_menu_set_history (GTK_OPTION_MENU (private->device_optionmenu), 0);
430 gtk_input_dialog_new (void)
432 GtkInputDialog *inputd;
434 inputd = g_object_new (GTK_TYPE_INPUT_DIALOG, NULL);
436 return GTK_WIDGET (inputd);
440 gtk_input_dialog_set_device (GtkWidget *w,
443 GdkDevice *device = data;
444 GtkInputDialog *inputd = input_dialog_from_widget (w);
446 inputd->current_device = device;
448 gtk_input_dialog_fill_axes (inputd, device);
449 gtk_input_dialog_fill_keys (inputd, device);
451 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
456 gtk_input_dialog_set_mapping_mode (GtkWidget *w,
459 GtkInputDialog *inputd = input_dialog_from_widget (w);
460 GdkDevice *info = inputd->current_device;
461 GdkInputMode old_mode = info->mode;
462 GdkInputMode mode = GPOINTER_TO_INT (data);
467 if (mode != old_mode)
469 if (gdk_device_set_mode (info, mode))
471 if (mode == GDK_MODE_DISABLED)
472 g_signal_emit (inputd,
473 input_dialog_signals[DISABLE_DEVICE],
477 g_signal_emit (inputd,
478 input_dialog_signals[ENABLE_DEVICE],
483 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
486 /* FIXME: error dialog ? */
491 gtk_input_dialog_set_axis (GtkWidget *w,
494 GdkAxisUse use = GPOINTER_TO_INT(data) & 0xFFFF;
496 GdkAxisUse *new_axes;
497 GtkInputDialog *inputd = input_dialog_from_widget (w);
498 GdkDevice *info = inputd->current_device;
500 gint axis = (GPOINTER_TO_INT(data) >> 16) - 1;
507 new_axes = g_new (GdkAxisUse, info->num_axes);
509 for (i=0;i<info->num_axes;i++)
511 new_axes[i] = info->axes[i].use;
512 if (info->axes[i].use == use)
517 old_use = info->axes[axis].use;
519 old_use = GDK_AXIS_IGNORE;
521 if (axis == old_axis)
524 /* we must always have an x and a y axis */
525 if ((axis == -1 && (use == GDK_AXIS_X || use == GDK_AXIS_Y)) ||
526 (old_axis == -1 && (old_use == GDK_AXIS_X || old_use == GDK_AXIS_Y)))
528 gtk_option_menu_set_history (
529 GTK_OPTION_MENU (inputd->axis_items[use]),
535 gdk_device_set_axis_use (info, axis, use);
538 gdk_device_set_axis_use (info, old_axis, old_use);
540 if (old_use != GDK_AXIS_IGNORE)
542 gtk_option_menu_set_history (
543 GTK_OPTION_MENU (inputd->axis_items[old_use]),
552 gtk_input_dialog_fill_axes(GtkInputDialog *inputd, GdkDevice *info)
554 static const char *axis_use_strings[GDK_AXIS_LAST] =
567 GtkWidget *option_menu;
570 /* remove all the old items */
571 if (inputd->axis_list)
573 gtk_widget_hide (inputd->axis_list); /* suppress resizes (or get warnings) */
574 gtk_widget_destroy (inputd->axis_list);
576 inputd->axis_list = gtk_table_new (GDK_AXIS_LAST, 2, 0);
577 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->axis_listbox),
579 gtk_widget_show (inputd->axis_list);
581 gtk_widget_realize (inputd->axis_list);
582 gdk_window_set_background (inputd->axis_list->window,
583 &inputd->axis_list->style->base[GTK_STATE_NORMAL]);
585 for (i=GDK_AXIS_X;i<GDK_AXIS_LAST;i++)
587 /* create the label */
589 label = gtk_label_new (_(axis_use_strings[i]));
590 gtk_table_attach (GTK_TABLE (inputd->axis_list), label, 0, 1, i, i+1,
593 /* and the use option menu */
594 menu = gtk_menu_new();
596 for (j = -1; j < info->num_axes; j++)
599 GtkWidget *menu_item;
602 menu_item = gtk_menu_item_new_with_label (_("none"));
605 sprintf (buffer,"%d",j+1);
606 menu_item = gtk_menu_item_new_with_label (buffer);
608 g_signal_connect (menu_item, "activate",
609 G_CALLBACK (gtk_input_dialog_set_axis),
610 GINT_TO_POINTER (0x10000 * (j + 1) + i));
611 gtk_widget_show (menu_item);
612 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
615 inputd->axis_items[i] = option_menu = gtk_option_menu_new ();
616 gtk_table_attach (GTK_TABLE (inputd->axis_list), option_menu,
617 1, 2, i, i+1, 0, 0, 2, 2);
619 gtk_widget_show (option_menu);
620 gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
621 for (j = 0; j < info->num_axes; j++)
622 if (info->axes[j].use == (GdkAxisUse) i)
624 gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu), j+1);
628 gtk_widget_show (label);
633 gtk_input_dialog_clear_key (GtkWidget *widget, GtkInputKeyInfo *key)
635 if (!key->inputd->current_device)
638 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
639 gdk_device_set_key (key->inputd->current_device, key->index, 0, 0);
643 gtk_input_dialog_set_key (GtkInputKeyInfo *key,
644 guint keyval, GdkModifierType modifiers)
651 str = g_string_new("");
653 if (modifiers & GDK_SHIFT_MASK)
654 g_string_append (str, "Shift+");
655 if (modifiers & GDK_CONTROL_MASK)
656 g_string_append (str, "Ctrl+");
657 if (modifiers & GDK_MOD1_MASK)
658 g_string_append (str, "Alt+");
660 if ((keyval >= 0x20) && (keyval <= 0xFF))
664 g_string_append (str, chars);
667 g_string_append (str, _("(unknown)"));
668 gtk_entry_set_text (GTK_ENTRY(key->entry), str->str);
670 g_string_free (str, TRUE);
674 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
679 gtk_input_dialog_key_press (GtkWidget *widget,
681 GtkInputKeyInfo *key)
683 if (!key->inputd->current_device)
686 gtk_input_dialog_set_key (key, event->keyval, event->state & 0xFF);
687 gdk_device_set_key (key->inputd->current_device, key->index,
688 event->keyval, event->state & 0xFF);
690 g_signal_stop_emission_by_name (widget, "key_press_event");
696 gtk_input_dialog_destroy_key (GtkWidget *widget, GtkInputKeyInfo *key)
702 gtk_input_dialog_fill_keys(GtkInputDialog *inputd, GdkDevice *info)
710 /* remove all the old items */
711 if (inputd->keys_list)
713 gtk_widget_hide (inputd->keys_list); /* suppress resizes (or get warnings) */
714 gtk_widget_destroy (inputd->keys_list);
717 inputd->keys_list = gtk_table_new (info->num_keys, 3, FALSE);
718 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
720 gtk_widget_show (inputd->keys_list);
722 gtk_widget_realize (inputd->keys_list);
723 gdk_window_set_background (inputd->keys_list->window,
724 &inputd->keys_list->style->base[GTK_STATE_NORMAL]);
726 for (i=0;i<info->num_keys;i++)
728 GtkInputKeyInfo *key = g_new (GtkInputKeyInfo, 1);
730 key->inputd = inputd;
732 /* create the label */
734 sprintf (buffer, "%d", i+1);
735 label = gtk_label_new (buffer);
736 gtk_table_attach (GTK_TABLE (inputd->keys_list), label, 0, 1, i, i+1,
738 gtk_widget_show (label);
742 key->entry = gtk_entry_new ();
743 gtk_table_attach (GTK_TABLE (inputd->keys_list), key->entry, 1, 2, i, i+1,
744 GTK_EXPAND | GTK_FILL , 0, 2, 2);
745 gtk_widget_show (key->entry);
747 g_signal_connect (key->entry, "key_press_event",
748 G_CALLBACK (gtk_input_dialog_key_press), key);
749 g_signal_connect (key->entry, "destroy",
750 G_CALLBACK (gtk_input_dialog_destroy_key), key);
752 /* and clear button */
754 button = gtk_button_new_with_label (_("clear"));
755 gtk_table_attach (GTK_TABLE (inputd->keys_list), button, 2, 3, i, i+1,
757 gtk_widget_show (button);
759 g_signal_connect (button, "clicked",
760 G_CALLBACK (gtk_input_dialog_clear_key), key);
762 gtk_input_dialog_set_key (key, info->keys[i].keyval,
763 info->keys[i].modifiers);