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
63 typedef struct _GtkInputDialogPrivate GtkInputDialogPrivate;
64 typedef struct _GtkInputKeyInfo GtkInputKeyInfo;
66 struct _GtkInputDialogPrivate
68 GtkWidget *device_menu;
69 GtkWidget *device_optionmenu;
70 GtkWidget *no_devices_label;
74 struct _GtkInputKeyInfo
78 GtkInputDialog *inputd;
89 #define AXIS_LIST_WIDTH 160
90 #define AXIS_LIST_HEIGHT 175
92 #define KEYS_LIST_WIDTH 200
93 #define KEYS_LIST_HEIGHT 175
95 /* Forward declarations */
97 static void gtk_input_dialog_class_init (GtkInputDialogClass *klass);
98 static void gtk_input_dialog_init (GtkInputDialog *inputd);
99 static void gtk_input_dialog_screen_changed (GtkWidget *widget,
100 GdkScreen *previous_screen);
101 static void gtk_input_dialog_set_device (GtkWidget *widget,
103 static void gtk_input_dialog_set_mapping_mode (GtkWidget *w,
105 static void gtk_input_dialog_set_axis (GtkWidget *widget,
107 static void gtk_input_dialog_fill_axes (GtkInputDialog *inputd,
109 static void gtk_input_dialog_set_key (GtkInputKeyInfo *key,
111 GdkModifierType modifiers);
112 static gboolean gtk_input_dialog_key_press (GtkWidget *widget,
114 GtkInputKeyInfo *key);
115 static void gtk_input_dialog_clear_key (GtkWidget *widget,
116 GtkInputKeyInfo *key);
117 static void gtk_input_dialog_destroy_key (GtkWidget *widget,
118 GtkInputKeyInfo *key);
119 static void gtk_input_dialog_fill_keys (GtkInputDialog *inputd,
122 static GtkObjectClass *parent_class = NULL;
123 static guint input_dialog_signals[LAST_SIGNAL] = { 0 };
126 gtk_input_dialog_get_type (void)
128 static GType input_dialog_type = 0;
130 if (!input_dialog_type)
132 static const GTypeInfo input_dialog_info =
134 sizeof (GtkInputDialogClass),
135 NULL, /* base_init */
136 NULL, /* base_finalize */
137 (GClassInitFunc) gtk_input_dialog_class_init,
138 NULL, /* class_finalize */
139 NULL, /* class_data */
140 sizeof (GtkInputDialog),
142 (GInstanceInitFunc) gtk_input_dialog_init,
146 g_type_register_static (GTK_TYPE_DIALOG, "GtkInputDialog",
147 &input_dialog_info, 0);
150 return input_dialog_type;
153 static GtkInputDialogPrivate *
154 gtk_input_dialog_get_private (GtkInputDialog *input_dialog)
156 GtkInputDialogPrivate *private;
157 static GQuark private_quark = 0;
160 private_quark = g_quark_from_static_string ("gtk-input-dialog-private");
162 private = g_object_get_qdata (G_OBJECT (input_dialog), private_quark);
166 private = g_new0 (GtkInputDialogPrivate, 1);
167 g_object_set_qdata_full (G_OBJECT (input_dialog), private_quark,
174 static GtkInputDialog *
175 input_dialog_from_widget (GtkWidget *widget)
179 if (GTK_IS_MENU_ITEM (widget))
181 GtkMenu *menu = GTK_MENU (widget->parent);
182 widget = gtk_menu_get_attach_widget (menu);
185 toplevel = gtk_widget_get_toplevel (widget);
186 return GTK_INPUT_DIALOG (toplevel);
190 gtk_input_dialog_class_init (GtkInputDialogClass *klass)
192 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
194 parent_class = g_type_class_peek_parent (klass);
196 widget_class->screen_changed = gtk_input_dialog_screen_changed;
198 klass->enable_device = NULL;
199 klass->disable_device = NULL;
201 input_dialog_signals[ENABLE_DEVICE] =
202 g_signal_new ("enable_device",
203 G_OBJECT_CLASS_TYPE (klass),
205 G_STRUCT_OFFSET (GtkInputDialogClass, enable_device),
207 _gtk_marshal_VOID__OBJECT,
211 input_dialog_signals[DISABLE_DEVICE] =
212 g_signal_new ("disable_device",
213 G_OBJECT_CLASS_TYPE (klass),
215 G_STRUCT_OFFSET (GtkInputDialogClass, disable_device),
217 _gtk_marshal_VOID__OBJECT,
223 gtk_input_dialog_init (GtkInputDialog *inputd)
225 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
228 GtkWidget *mapping_menu;
230 GtkWidget *separator;
233 gtk_widget_push_composite_child ();
235 gtk_window_set_title (GTK_WINDOW (inputd), _("Input"));
239 private->main_vbox = gtk_vbox_new (FALSE, 4);
240 gtk_container_set_border_width (GTK_CONTAINER (private->main_vbox), 5);
241 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox), private->main_vbox,
244 private->no_devices_label = gtk_label_new (_("No extended input devices"));
245 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (inputd)->vbox),
246 private->no_devices_label,
249 /* menu for selecting device */
251 private->device_menu = gtk_menu_new ();
253 util_box = gtk_hbox_new (FALSE, 2);
254 gtk_box_pack_start (GTK_BOX (private->main_vbox), util_box, FALSE, FALSE, 0);
256 label = gtk_label_new_with_mnemonic (_("_Device:"));
257 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
259 private->device_optionmenu = gtk_option_menu_new ();
260 gtk_label_set_mnemonic_widget (GTK_LABEL (label), private->device_optionmenu);
261 gtk_box_pack_start (GTK_BOX (util_box), private->device_optionmenu, TRUE, TRUE, 2);
262 gtk_widget_show (private->device_optionmenu);
263 gtk_option_menu_set_menu (GTK_OPTION_MENU (private->device_optionmenu), private->device_menu);
265 gtk_widget_show (label);
269 /* mapping mode option menu */
271 mapping_menu = gtk_menu_new ();
273 menuitem = gtk_menu_item_new_with_label(_("Disabled"));
274 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
275 gtk_widget_show (menuitem);
276 g_signal_connect (menuitem, "activate",
277 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
278 GINT_TO_POINTER (GDK_MODE_DISABLED));
280 menuitem = gtk_menu_item_new_with_label(_("Screen"));
281 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
282 gtk_widget_show (menuitem);
283 g_signal_connect (menuitem, "activate",
284 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
285 GINT_TO_POINTER (GDK_MODE_SCREEN));
287 menuitem = gtk_menu_item_new_with_label(_("Window"));
288 gtk_menu_shell_append (GTK_MENU_SHELL (mapping_menu), menuitem);
289 gtk_widget_show (menuitem);
290 g_signal_connect (menuitem, "activate",
291 G_CALLBACK (gtk_input_dialog_set_mapping_mode),
292 GINT_TO_POINTER (GDK_MODE_WINDOW));
294 label = gtk_label_new_with_mnemonic (_("_Mode: "));
295 gtk_box_pack_start (GTK_BOX (util_box), label, FALSE, FALSE, 2);
297 inputd->mode_optionmenu = gtk_option_menu_new ();
298 gtk_label_set_mnemonic_widget (GTK_LABEL (label), inputd->mode_optionmenu);
299 gtk_box_pack_start (GTK_BOX (util_box), inputd->mode_optionmenu, FALSE, FALSE, 2);
300 gtk_widget_show (inputd->mode_optionmenu);
301 gtk_option_menu_set_menu (GTK_OPTION_MENU (inputd->mode_optionmenu), mapping_menu);
303 gtk_widget_show(label);
305 gtk_widget_show (util_box);
307 util_box = gtk_hbox_new (FALSE, 2);
308 gtk_box_pack_start (GTK_BOX(private->main_vbox), util_box, FALSE, FALSE, 0);
310 gtk_widget_show (label);
311 gtk_widget_show (util_box);
313 separator = gtk_hseparator_new();
314 gtk_box_pack_start (GTK_BOX (private->main_vbox), separator, FALSE, TRUE, 0);
315 gtk_widget_show (separator);
319 notebook = gtk_notebook_new ();
320 gtk_box_pack_start (GTK_BOX (private->main_vbox), notebook, TRUE, TRUE, 0);
321 gtk_widget_show (notebook);
323 /* The axis listbox */
325 label = gtk_label_new_with_mnemonic (_("_Axes"));
327 inputd->axis_listbox = gtk_scrolled_window_new (NULL, NULL);
328 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(inputd->axis_listbox),
329 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
331 gtk_widget_set_size_request (inputd->axis_listbox,
332 AXIS_LIST_WIDTH, AXIS_LIST_HEIGHT);
333 gtk_notebook_append_page (GTK_NOTEBOOK(notebook),
334 inputd->axis_listbox, label);
336 gtk_widget_show (inputd->axis_listbox);
338 inputd->axis_list = NULL;
342 label = gtk_label_new_with_mnemonic (_("_Keys"));
344 inputd->keys_listbox = gtk_scrolled_window_new (NULL, NULL);
345 gtk_widget_set_size_request (inputd->keys_listbox,
346 KEYS_LIST_WIDTH, KEYS_LIST_HEIGHT);
347 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
348 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
349 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
350 inputd->keys_listbox, label);
352 gtk_widget_show (inputd->keys_listbox);
354 inputd->keys_list = NULL;
356 inputd->save_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
357 GTK_WIDGET_SET_FLAGS (inputd->save_button, GTK_CAN_DEFAULT);
358 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
359 inputd->save_button, TRUE, TRUE, 0);
360 gtk_widget_show (inputd->save_button);
362 inputd->close_button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
363 GTK_WIDGET_SET_FLAGS (inputd->close_button, GTK_CAN_DEFAULT);
364 gtk_box_pack_start (GTK_BOX (GTK_DIALOG(inputd)->action_area),
365 inputd->close_button, TRUE, TRUE, 0);
367 gtk_widget_show (inputd->close_button);
368 gtk_widget_grab_default (inputd->close_button);
370 gtk_widget_pop_composite_child ();
372 gtk_input_dialog_screen_changed (GTK_WIDGET (inputd), NULL);
376 gtk_input_dialog_screen_changed (GtkWidget *widget,
377 GdkScreen *previous_screen)
379 GtkInputDialog *inputd = GTK_INPUT_DIALOG (widget);
380 GtkInputDialogPrivate *private = gtk_input_dialog_get_private (inputd);
382 GList *device_info = NULL;
383 GdkDevice *core_pointer = NULL;
386 if (gtk_widget_has_screen (widget))
390 display = gtk_widget_get_display (widget);
391 device_info = gdk_display_list_devices (display);
392 core_pointer = gdk_display_get_core_pointer (display);
395 inputd->current_device = NULL;
396 gtk_container_foreach (GTK_CONTAINER (private->device_menu),
397 (GtkCallback)gtk_widget_destroy, NULL);
399 if (g_list_length(device_info) <= 1) /* only core device */
401 gtk_widget_hide (private->main_vbox);
402 gtk_widget_show (private->no_devices_label);
403 gtk_widget_set_sensitive(inputd->save_button, FALSE);
407 gtk_widget_show (private->main_vbox);
408 gtk_widget_hide (private->no_devices_label);
409 gtk_widget_set_sensitive(inputd->save_button, TRUE);
411 for (tmp_list = device_info; tmp_list; tmp_list = tmp_list->next)
413 GdkDevice *info = tmp_list->data;
414 if (info != core_pointer)
418 menuitem = gtk_menu_item_new_with_label (info->name);
420 gtk_menu_shell_append (GTK_MENU_SHELL (private->device_menu),
422 gtk_widget_show (menuitem);
423 g_signal_connect (menuitem, "activate",
424 G_CALLBACK (gtk_input_dialog_set_device),
429 gtk_input_dialog_set_device (widget, device_info->data);
430 gtk_option_menu_set_history (GTK_OPTION_MENU (private->device_optionmenu), 0);
435 gtk_input_dialog_new (void)
437 GtkInputDialog *inputd;
439 inputd = g_object_new (GTK_TYPE_INPUT_DIALOG, NULL);
441 return GTK_WIDGET (inputd);
445 gtk_input_dialog_set_device (GtkWidget *w,
448 GdkDevice *device = data;
449 GtkInputDialog *inputd = input_dialog_from_widget (w);
451 inputd->current_device = device;
453 gtk_input_dialog_fill_axes (inputd, device);
454 gtk_input_dialog_fill_keys (inputd, device);
456 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
461 gtk_input_dialog_set_mapping_mode (GtkWidget *w,
464 GtkInputDialog *inputd = input_dialog_from_widget (w);
465 GdkDevice *info = inputd->current_device;
466 GdkInputMode old_mode = info->mode;
467 GdkInputMode mode = GPOINTER_TO_INT (data);
472 if (mode != old_mode)
474 if (gdk_device_set_mode (info, mode))
476 if (mode == GDK_MODE_DISABLED)
477 g_signal_emit (inputd,
478 input_dialog_signals[DISABLE_DEVICE],
482 g_signal_emit (inputd,
483 input_dialog_signals[ENABLE_DEVICE],
488 gtk_option_menu_set_history (GTK_OPTION_MENU (inputd->mode_optionmenu),
491 /* FIXME: error dialog ? */
496 gtk_input_dialog_set_axis (GtkWidget *w,
499 GdkAxisUse use = GPOINTER_TO_INT(data) & 0xFFFF;
501 GdkAxisUse *new_axes;
502 GtkInputDialog *inputd = input_dialog_from_widget (w);
503 GdkDevice *info = inputd->current_device;
505 gint axis = (GPOINTER_TO_INT(data) >> 16) - 1;
512 new_axes = g_new (GdkAxisUse, info->num_axes);
514 for (i=0;i<info->num_axes;i++)
516 new_axes[i] = info->axes[i].use;
517 if (info->axes[i].use == use)
522 old_use = info->axes[axis].use;
524 old_use = GDK_AXIS_IGNORE;
526 if (axis == old_axis)
529 /* we must always have an x and a y axis */
530 if ((axis == -1 && (use == GDK_AXIS_X || use == GDK_AXIS_Y)) ||
531 (old_axis == -1 && (old_use == GDK_AXIS_X || old_use == GDK_AXIS_Y)))
533 gtk_option_menu_set_history (
534 GTK_OPTION_MENU (inputd->axis_items[use]),
540 gdk_device_set_axis_use (info, axis, use);
543 gdk_device_set_axis_use (info, old_axis, old_use);
545 if (old_use != GDK_AXIS_IGNORE)
547 gtk_option_menu_set_history (
548 GTK_OPTION_MENU (inputd->axis_items[old_use]),
557 gtk_input_dialog_fill_axes(GtkInputDialog *inputd, GdkDevice *info)
559 static const char *const axis_use_strings[GDK_AXIS_LAST] =
572 GtkWidget *option_menu;
575 /* remove all the old items */
576 if (inputd->axis_list)
578 gtk_widget_hide (inputd->axis_list); /* suppress resizes (or get warnings) */
579 gtk_widget_destroy (inputd->axis_list);
581 inputd->axis_list = gtk_table_new (GDK_AXIS_LAST, 2, 0);
582 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->axis_listbox),
584 gtk_widget_show (inputd->axis_list);
586 gtk_widget_realize (inputd->axis_list);
587 gdk_window_set_background (inputd->axis_list->window,
588 &inputd->axis_list->style->base[GTK_STATE_NORMAL]);
590 for (i=GDK_AXIS_X;i<GDK_AXIS_LAST;i++)
592 /* create the label */
594 label = gtk_label_new (_(axis_use_strings[i]));
595 gtk_table_attach (GTK_TABLE (inputd->axis_list), label, 0, 1, i, i+1,
598 /* and the use option menu */
599 menu = gtk_menu_new();
601 for (j = -1; j < info->num_axes; j++)
604 GtkWidget *menu_item;
607 menu_item = gtk_menu_item_new_with_label (_("none"));
610 g_snprintf (buffer, sizeof (buffer), "%d", j+1);
611 menu_item = gtk_menu_item_new_with_label (buffer);
613 g_signal_connect (menu_item, "activate",
614 G_CALLBACK (gtk_input_dialog_set_axis),
615 GINT_TO_POINTER (0x10000 * (j + 1) + i));
616 gtk_widget_show (menu_item);
617 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
620 inputd->axis_items[i] = option_menu = gtk_option_menu_new ();
621 gtk_table_attach (GTK_TABLE (inputd->axis_list), option_menu,
622 1, 2, i, i+1, 0, 0, 2, 2);
624 gtk_widget_show (option_menu);
625 gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
626 for (j = 0; j < info->num_axes; j++)
627 if (info->axes[j].use == (GdkAxisUse) i)
629 gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu), j+1);
633 gtk_widget_show (label);
638 gtk_input_dialog_clear_key (GtkWidget *widget, GtkInputKeyInfo *key)
640 if (!key->inputd->current_device)
643 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
644 gdk_device_set_key (key->inputd->current_device, key->index, 0, 0);
648 gtk_input_dialog_set_key (GtkInputKeyInfo *key,
649 guint keyval, GdkModifierType modifiers)
656 str = g_string_new (NULL);
658 if (modifiers & GDK_SHIFT_MASK)
659 g_string_append (str, "Shift+");
660 if (modifiers & GDK_CONTROL_MASK)
661 g_string_append (str, "Ctrl+");
662 if (modifiers & GDK_MOD1_MASK)
663 g_string_append (str, "Alt+");
665 if ((keyval >= 0x20) && (keyval <= 0xFF))
669 g_string_append (str, chars);
672 g_string_append (str, _("(unknown)"));
673 gtk_entry_set_text (GTK_ENTRY(key->entry), str->str);
675 g_string_free (str, TRUE);
679 gtk_entry_set_text (GTK_ENTRY(key->entry), _("(disabled)"));
684 gtk_input_dialog_key_press (GtkWidget *widget,
686 GtkInputKeyInfo *key)
688 if (!key->inputd->current_device)
691 gtk_input_dialog_set_key (key, event->keyval, event->state & 0xFF);
692 gdk_device_set_key (key->inputd->current_device, key->index,
693 event->keyval, event->state & 0xFF);
695 g_signal_stop_emission_by_name (widget, "key_press_event");
701 gtk_input_dialog_destroy_key (GtkWidget *widget, GtkInputKeyInfo *key)
707 gtk_input_dialog_fill_keys(GtkInputDialog *inputd, GdkDevice *info)
715 /* remove all the old items */
716 if (inputd->keys_list)
718 gtk_widget_hide (inputd->keys_list); /* suppress resizes (or get warnings) */
719 gtk_widget_destroy (inputd->keys_list);
722 inputd->keys_list = gtk_table_new (info->num_keys, 3, FALSE);
723 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (inputd->keys_listbox),
725 gtk_widget_show (inputd->keys_list);
727 gtk_widget_realize (inputd->keys_list);
728 gdk_window_set_background (inputd->keys_list->window,
729 &inputd->keys_list->style->base[GTK_STATE_NORMAL]);
731 for (i=0;i<info->num_keys;i++)
733 GtkInputKeyInfo *key = g_new (GtkInputKeyInfo, 1);
735 key->inputd = inputd;
737 /* create the label */
739 g_snprintf (buffer, sizeof (buffer), "%d", i+1);
740 label = gtk_label_new (buffer);
741 gtk_table_attach (GTK_TABLE (inputd->keys_list), label, 0, 1, i, i+1,
743 gtk_widget_show (label);
747 key->entry = gtk_entry_new ();
748 gtk_table_attach (GTK_TABLE (inputd->keys_list), key->entry, 1, 2, i, i+1,
749 GTK_EXPAND | GTK_FILL , 0, 2, 2);
750 gtk_widget_show (key->entry);
752 g_signal_connect (key->entry, "key_press_event",
753 G_CALLBACK (gtk_input_dialog_key_press), key);
754 g_signal_connect (key->entry, "destroy",
755 G_CALLBACK (gtk_input_dialog_destroy_key), key);
757 /* and clear button */
759 button = gtk_button_new_with_label (_("clear"));
760 gtk_table_attach (GTK_TABLE (inputd->keys_list), button, 2, 3, i, i+1,
762 gtk_widget_show (button);
764 g_signal_connect (button, "clicked",
765 G_CALLBACK (gtk_input_dialog_clear_key), key);
767 gtk_input_dialog_set_key (key, info->keys[i].keyval,
768 info->keys[i].modifiers);
772 #define __GTK_INPUTDIALOG_C__
773 #include "gtkaliasdef.c"