Code

From trunk
[inkscape.git] / src / dialogs / input.cpp
1 #define __SP_INPUT_C__
3 /** @file
4  * @brief Extended input devices dialog
5  */
6 /* Authors:
7  *   Nicklas Lindgren <nili@lysator.liu.se>
8  *   Johan Engelen <goejendaagh@zonnet.nl>
9  *
10  * Copyright (C) 2005-2006 Authors
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include <gtk/gtksignal.h>
19 #include <gtk/gtkinputdialog.h>
20 #include <glibmm/ustring.h>
22 #include "macros.h"
23 #include "verbs.h"
24 #include "inkscape.h"
25 #include "interface.h"
26 #include "xml/repr.h"
28 #include "dialogs/dialog-events.h"
29 #include "preferences.h"
31 #define MIN_ONSCREEN_DISTANCE 50
33 static GtkWidget *dlg = NULL;
34 static win_data wd;
36 // impossible original values to make sure they are read from prefs
37 static gint x = -1000, y = -1000, w = 0, h = 0;
38 static Glib::ustring const prefs_path = "/dialogs/input/";
40 static void
41 sp_input_dialog_destroy (GtkObject */*object*/, gpointer /*data*/)
42 {
43     sp_signal_disconnect_by_data (INKSCAPE, dlg);
44     wd.win = dlg = NULL;
45     wd.stop = 0;
46 }
48 static gboolean
49 sp_input_dialog_delete (GtkObject */*object*/, GdkEvent */*event*/, gpointer /*data*/)
50 {
51     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
52     gtk_window_get_position ((GtkWindow *) dlg, &x, &y);
53     gtk_window_get_size ((GtkWindow *) dlg, &w, &h);
55     if (x<0) x=0;
56     if (y<0) y=0;
58     prefs->setInt(prefs_path + "x", x);
59     prefs->setInt(prefs_path + "y", y);
60     prefs->setInt(prefs_path + "w", w);
61     prefs->setInt(prefs_path + "h", h);
63     return FALSE; // which means, go ahead and destroy it
65 }
67 static const gchar *axis_use_strings[GDK_AXIS_LAST] = {
68     "ignore", "x", "y", "pressure", "xtilt", "ytilt", "wheel"
69 };
71 void
72 sp_input_load_from_preferences (void)
73 {
74     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
76     for (GList *list_ptr = gdk_devices_list(); list_ptr != NULL; list_ptr = list_ptr->next) {
77         GdkDevice *device = static_cast<GdkDevice *>(list_ptr->data);
78         //repr = sp_repr_lookup_child(devices, "id", device->name);
79         Glib::ustring device_path = Glib::ustring("/devices/") + device->name;
80         if (/*repr != NULL*/ 1) {
81             GdkInputMode mode;
82             Glib::ustring device_mode = prefs->getString(device_path + "/mode");
84             if (device_mode.empty())
85                 mode = GDK_MODE_DISABLED;
86             else if (device_mode == "screen")
87                 mode = GDK_MODE_SCREEN;
88             else if (device_mode == "window")
89                 mode = GDK_MODE_WINDOW;
90             else
91                 mode = GDK_MODE_DISABLED;
93             if (device->mode != mode) {
94                 gdk_device_set_mode(device, mode);
95             }
97             Glib::ustring::size_type pos0, pos1;
98             GdkAxisUse axis_use;
100             //temp_ptr = repr->attribute("axes");
101             Glib::ustring const axes_str = prefs->getString(device_path + "/axes");
102             pos0 = pos1 = 0;
103             for (gint i=0; i < device->num_axes; i++) {
104                 pos1 = axes_str.find(';', pos0);
105                 if (pos1 == Glib::ustring::npos)
106                     break;  // Too few axis specifications
108                 axis_use = GDK_AXIS_IGNORE;
109                 for (gint j=0; j < GDK_AXIS_LAST; j++)
110                     if (!strcmp(axes_str.substr(pos0, pos1-pos0).c_str(), axis_use_strings[j])) {
111                         axis_use = static_cast<GdkAxisUse>(j);
112                         break;
113                     }
114                 gdk_device_set_axis_use(device, i, axis_use);
115                 pos0 = pos1 + 1;
116             }
118             guint keyval;
119             GdkModifierType modifier;
121             Glib::ustring const keys_str = prefs->getString(device_path + "/keys");
122             pos0 = pos1 = 0;
123             for (gint i=0; i < device->num_keys; i++) {
124                 pos1 = keys_str.find(';', pos0);
125                 if (pos1 == Glib::ustring::npos)
126                     break;  // Too few key specifications
128                 gtk_accelerator_parse(keys_str.substr(pos0, pos1-pos0).c_str(), &keyval, &modifier);
129                 gdk_device_set_key(device, i, keyval, modifier);
130                 pos0 = pos1 + 1;
131             }
132         }
133     }
136 void
137 sp_input_save_to_preferences (void)
139     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
141     for (GList *list_ptr = gdk_devices_list(); list_ptr != NULL; list_ptr = list_ptr->next) {
142         GdkDevice *device = static_cast<GdkDevice *>(list_ptr->data);
144         //repr = sp_repr_lookup_child(devices, "id", device->name);
145         Glib::ustring device_path = Glib::ustring("/devices/") + device->name;
147         switch (device->mode) {
148             default:
149             case GDK_MODE_DISABLED: {
150                 prefs->setString(device_path + "/mode", "disabled");
151                 break;
152             }
153             case GDK_MODE_SCREEN: {
154                 prefs->setString(device_path + "/mode", "screen");
155                 break;
156             }
157             case GDK_MODE_WINDOW: {
158                 prefs->setString(device_path + "/mode", "window");
159                 break;
160             }
161         }
163         Glib::ustring temp_attribute = "";
164         for (gint i=0; i < device->num_axes; i++) {
165             temp_attribute += axis_use_strings[device->axes[i].use];
166             temp_attribute += ";";
167         }
168         prefs->setString(device_path + "/axes", temp_attribute);
170         temp_attribute = "";
171         for (gint i=0; i < device->num_keys; i++) {
172             temp_attribute += gtk_accelerator_name(device->keys[i].keyval, device->keys[i].modifiers);
173             temp_attribute += ";";
174         }
175         prefs->setString(device_path + "/keys", temp_attribute);
176     }
179 static void
180 sp_input_save_button (GtkObject */*object*/, gpointer /*data*/)
182     sp_input_save_to_preferences();
185 void
186 sp_input_dialog (void)
188     if (dlg == NULL) {
189         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
191         gchar title[500];
192         sp_ui_dialog_title_string (Inkscape::Verb::get(SP_VERB_DIALOG_INPUT), title);
194         dlg = gtk_input_dialog_new();
196         if (x == -1000 || y == -1000) {
197             x = prefs->getInt(prefs_path + "x", -1000);
198             y = prefs->getInt(prefs_path + "y", -1000);
199         }
201         if (w ==0 || h == 0) {
202             w = prefs->getInt(prefs_path + "w", 0);
203             h = prefs->getInt(prefs_path + "h", 0);
204         }
206 //        if (x<0) x=0;
207 //        if (y<0) y=0;
209         if (w && h) {
210             gtk_window_resize ((GtkWindow *) dlg, w, h);
211         }
212         if (x >= 0 && y >= 0 && (x < (gdk_screen_width()-MIN_ONSCREEN_DISTANCE)) && (y < (gdk_screen_height()-MIN_ONSCREEN_DISTANCE))) {
213             gtk_window_move ((GtkWindow *) dlg, x, y);
214         } else {
215             gtk_window_set_position(GTK_WINDOW(dlg), GTK_WIN_POS_CENTER);
216         }
219         sp_transientize (dlg);
220         wd.win = dlg;
221         wd.stop = 0;
223         g_signal_connect   ( G_OBJECT (INKSCAPE), "activate_desktop", G_CALLBACK (sp_transientize_callback), &wd);
224         gtk_signal_connect ( GTK_OBJECT (dlg), "event", GTK_SIGNAL_FUNC (sp_dialog_event_handler), dlg);
225         gtk_signal_connect ( GTK_OBJECT (dlg), "destroy", G_CALLBACK (sp_input_dialog_destroy), dlg);
226         gtk_signal_connect ( GTK_OBJECT (dlg), "delete_event", G_CALLBACK (sp_input_dialog_delete), dlg);
227         g_signal_connect   ( G_OBJECT (INKSCAPE), "shut_down", G_CALLBACK (sp_input_dialog_delete), dlg);
228         g_signal_connect   ( G_OBJECT (INKSCAPE), "dialogs_hide", G_CALLBACK (sp_dialog_hide), dlg);
229         g_signal_connect   ( G_OBJECT (INKSCAPE), "dialogs_unhide", G_CALLBACK (sp_dialog_unhide), dlg);
231         // Dialog-specific stuff
232         gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(dlg)->close_button),
233                                    "clicked",
234                                    (GtkSignalFunc)gtk_widget_destroy,
235                                    GTK_OBJECT(dlg));
236         gtk_signal_connect (GTK_OBJECT(GTK_INPUT_DIALOG(dlg)->save_button),
237                             "clicked",
238                             (GtkSignalFunc)sp_input_save_button, NULL);
239     }
241     gtk_window_present ((GtkWindow *) dlg);
245 /*
246   Local Variables:
247   mode:c++
248   c-file-style:"stroustrup"
249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250   indent-tabs-mode:nil
251   fill-column:99
252   End:
253 */
254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :