Code

Implement guide behaviour as discussed on the mailing list
[inkscape.git] / src / desktop-events.cpp
1 /** @file
2  * @brief Event handlers for SPDesktop
3  */
4 /* Author:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *
7  * Copyright (C) 1999-2002 Lauris Kaplinski
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
15 #include <map>
16 #include <string>
17 #include <2geom/line.h>
18 #include <glibmm/i18n.h>
20 #include "desktop.h"
21 #include "desktop-handles.h"
22 #include "dialogs/dialog-events.h"
23 #include "display/canvas-axonomgrid.h"
24 #include "display/canvas-grid.h"
25 #include "display/guideline.h"
26 #include "display/snap-indicator.h"
27 #include "document.h"
28 #include "event-context.h"
29 #include "helper/action.h"
30 #include "helper/unit-menu.h"
31 #include "helper/units.h"
32 #include "message-context.h"
33 #include "preferences.h"
34 #include "snap.h"
35 #include "sp-guide.h"
36 #include "sp-metrics.h"
37 #include "sp-namedview.h"
38 #include "tools-switch.h"
39 #include "ui/dialog/guides.h"
40 #include "widgets/desktop-widget.h"
41 #include "xml/repr.h"
43 static void snoop_extended(GdkEvent* event, SPDesktop *desktop);
44 static void init_extended();
46 /* Root item handler */
48 int sp_desktop_root_handler(SPCanvasItem */*item*/, GdkEvent *event, SPDesktop *desktop)
49 {
50     static bool watch = false;
51     static bool first = true;
53     if ( first ) {
54         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
55         if ( prefs->getBool("/options/useextinput/value", true)
56             && prefs->getBool("/options/switchonextinput/value") ) {
57             watch = true;
58             init_extended();
59         }
60         first = false;
61     }
62     if ( watch ) {
63         snoop_extended(event, desktop);
64     }
66     return sp_event_context_root_handler(desktop->event_context, event);
67 }
69 static gint sp_dt_ruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw, bool horiz)
70 {
71     static bool dragging = false;
72     static SPCanvasItem *guide = NULL;
73     static Geom::Point normal;
74     static bool snap_window_temporarily_open = false;
75     int wx, wy;
77     SPDesktop *desktop = dtw->desktop;
78     Inkscape::XML::Node *repr = SP_OBJECT_REPR(desktop->namedview);
80     gdk_window_get_pointer(GTK_WIDGET(dtw->canvas)->window, &wx, &wy, NULL);
81     Geom::Point const event_win(wx, wy);
83     gint width, height;
84     gdk_window_get_geometry(GTK_WIDGET(dtw->canvas)->window, NULL /*x*/, NULL /*y*/, &width, &height, NULL/*depth*/);
86     switch (event->type) {
87     case GDK_BUTTON_PRESS:
88             if (event->button.button == 1) {
89                 dragging = true;
91                 // FIXME: The snap delay mechanism won't work here, because it has been implemented
92                 // for the event context. Dragging guides off the ruler will send event to the ruler
93                 // and not to the context, which bypasses sp_event_context_snap_delay_handler
94                 // The snap manager will not notice the difference, so it'll check if the snap delay
95                 // has been activated (This check is only needed for catching coding errors, i.e.
96                 // to warn if the snap window has not been implemented properly in some context)
97                 if (desktop->event_context->_snap_window_open == false) {
98                     // A dt_ruler_event might be emitted when dragging a guide off the rulers
99                     // while drawing a Bezier curve. In such a situation, we're already in that
100                     // specific context and the snap delay is already active. We should not set
101                     // the snap delay to active again, because that will trigger a similar warning
102                     // to the one above
103                     sp_event_context_snap_window_open(desktop->event_context);
104                     snap_window_temporarily_open = true;
105                 }
107                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
108                 Geom::Point const event_dt(desktop->w2d(event_w));
110                 // explicitly show guidelines; if I draw a guide, I want them on
111                 sp_repr_set_boolean(repr, "showguides", TRUE);
112                 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
114                 // calculate the normal of the guidelines when dragged from the edges of rulers.
115                 Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
116                 Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
117                 normal_bl_to_tr.normalize();
118                 normal_tr_to_bl.normalize();
119                 Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
120                 if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
121                     Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
122                     if (event->button.state & GDK_CONTROL_MASK) {
123                         // guidelines normal to gridlines
124                         normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
125                         normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
126                     } else {
127                         normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
128                         normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
129                     }
130                 }
131                 if (horiz) {
132                     if (wx < 50) {
133                         normal = normal_bl_to_tr;
134                     } else if (wx > width - 50) {
135                         normal = normal_tr_to_bl;
136                     } else {
137                         normal = Geom::Point(0.,1.);
138                     }
139                 } else {
140                     if (wy < 50) {
141                         normal = normal_bl_to_tr;
142                     } else if (wy > height - 50) {
143                         normal = normal_tr_to_bl;
144                     } else {
145                         normal = Geom::Point(1.,0.);
146                     }
147                 }
149                 guide = sp_guideline_new(desktop->guides, event_dt, normal);
150                 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
151                 gdk_pointer_grab(widget->window, FALSE,
152                                  (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
153                                  NULL, NULL,
154                                  event->button.time);
155             }
156             break;
157     case GDK_MOTION_NOTIFY:
158             if (dragging) {
159                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
160                 Geom::Point event_dt(desktop->w2d(event_w));
162                 SnapManager &m = desktop->namedview->snap_manager;
163                 m.setup(desktop);
164                 // We only have a temporary guide which is not stored in our document yet.
165                 // Because the guide snapper only looks in the document for guides to snap to,
166                 // we don't have to worry about a guide snapping to itself here
167                 m.guideFreeSnap(event_dt, normal);
169                 sp_guideline_set_position(SP_GUIDELINE(guide), from_2geom(event_dt));
170                 desktop->set_coordinate_status(to_2geom(event_dt));
171                 desktop->setPosition(to_2geom(event_dt));
172             }
173             break;
174     case GDK_BUTTON_RELEASE:
175             if (dragging && event->button.button == 1) {
176                 gdk_pointer_ungrab(event->button.time);
177                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
178                 Geom::Point event_dt(desktop->w2d(event_w));
180                 SnapManager &m = desktop->namedview->snap_manager;
181                 m.setup(desktop);
182                 // We only have a temporary guide which is not stored in our document yet.
183                 // Because the guide snapper only looks in the document for guides to snap to,
184                 // we don't have to worry about a guide snapping to itself here
185                 m.guideFreeSnap(event_dt, normal);
187                 dragging = false;
189                 // See the comments in GDK_BUTTON_PRESS
190                 if (snap_window_temporarily_open) {
191                         sp_event_context_snap_window_closed(desktop->event_context);
192                         snap_window_temporarily_open = false;
193                 }
195                 gtk_object_destroy(GTK_OBJECT(guide));
196                 guide = NULL;
197                 if ((horiz ? wy : wx) >= 0) {
198                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
199                     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
200                     sp_repr_set_point(repr, "orientation", normal);
201                     sp_repr_set_point(repr, "position", from_2geom(event_dt));
202                     SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
203                     Inkscape::GC::release(repr);
204                     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
205                                      _("Create guide"));
206                 }
207                 desktop->set_coordinate_status(from_2geom(event_dt));
209                 // A dt_ruler_event might be emitted when dragging a guide of the rulers
210                 // while drawing a Bezier curve. In such a situation, we're already in that
211                 // specific context and the snap delay is already active. We should interfere
212                 // with that context and we should therefore leave the snap delay status 
213                 // as it is. So although it might have been set to active above on
214                 // GDK_BUTTON_PRESS, we should not set it back to inactive here. That must be
215                 // done by the context.
216             }
217         default:
218             break;
219     }
221     return FALSE;
224 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
226     return sp_dt_ruler_event(widget, event, dtw, true);
229 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
231     return sp_dt_ruler_event(widget, event, dtw, false);
234 /* Guides */
235 enum SPGuideDragType {
236     SP_DRAG_TRANSLATE,
237     SP_DRAG_TRANSLATE_CONSTRAINED, // Is not being used currently!
238     SP_DRAG_ROTATE,
239     SP_DRAG_MOVE_ORIGIN,
240     SP_DRAG_NONE
241 };
243 static Geom::Point drag_origin;
244 static SPGuideDragType drag_type = SP_DRAG_NONE;
245 //static bool reset_drag_origin = false; // when Ctrl is pressed while dragging, this is used to trigger resetting of the
246 //                                       // drag origin to that location so that constrained movement is more intuitive
248 // Min distance from anchor to initiate rotation, measured in screenpixels
249 #define tol 40.0 
251 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
253     static bool moved = false;
254     gint ret = FALSE;
256     SPGuide *guide = SP_GUIDE(data);
257     SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
259     switch (event->type) {
260         case GDK_2BUTTON_PRESS:
261             if (event->button.button == 1) {
262                 drag_type = SP_DRAG_NONE;
263                 sp_event_context_snap_window_closed(desktop->event_context);
264                 sp_canvas_item_ungrab(item, event->button.time);
265                 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
266                 ret = TRUE;
267             }
268             break;
269         case GDK_BUTTON_PRESS:
270             if (event->button.button == 1) {
271                 sp_event_context_snap_window_open(desktop->event_context);
272                 Geom::Point const event_w(event->button.x, event->button.y);
273                 Geom::Point const event_dt(desktop->w2d(event_w));
275                 // Due to the tolerance allowed when grabbing a guide, event_dt will generally
276                 // be close to the guide but not just exactly on it. The drag origin calculated
277                 // here must be exactly on the guide line though, otherwise
278                 // small errors will occur once we snap, see
279                 // https://bugs.launchpad.net/inkscape/+bug/333762
280                 drag_origin = Geom::projection(event_dt, Geom::Line(guide->point_on_line, guide->angle()));
282                 if (event->button.state & GDK_SHIFT_MASK) {
283                     // with shift we rotate the guide
284                     drag_type = SP_DRAG_ROTATE;
285                 } else {
286                     if (event->button.state & GDK_CONTROL_MASK) {
287                         drag_type = SP_DRAG_MOVE_ORIGIN;
288                     } else {
289                         drag_type = SP_DRAG_TRANSLATE;
290                     }
291                 }
293                 if (drag_type == SP_DRAG_ROTATE || drag_type == SP_DRAG_TRANSLATE) {
294                     sp_canvas_item_grab(item,
295                                         ( GDK_BUTTON_RELEASE_MASK  |
296                                           GDK_BUTTON_PRESS_MASK    |
297                                           GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
298                                         NULL,
299                                         event->button.time);
300                 }
301                 ret = TRUE;
302             }
303             break;
304         case GDK_MOTION_NOTIFY:
305             if (drag_type != SP_DRAG_NONE) {
306                 Geom::Point const motion_w(event->motion.x,
307                                            event->motion.y);
308                 Geom::Point motion_dt(desktop->w2d(motion_w));
310                 // This is for snapping while dragging existing guidelines. New guidelines,
311                 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
312                 SnapManager &m = desktop->namedview->snap_manager;
313                 m.setup(desktop, true, NULL, NULL, guide);
314                 if (drag_type == SP_DRAG_MOVE_ORIGIN) {
315                     // If we snap in guideConstrainedSnap() below, then motion_dt will
316                     // be forced to be on the guide. If we don't snap however, then
317                     // the origin should still be constrained to the guide. So let's do
318                     // that explicitly first:
320                     Geom::Line line(guide->point_on_line, guide->angle());
321                     Geom::Coord t = line.nearestPoint(motion_dt);
322                     motion_dt = line.pointAt(t);
323                     m.guideConstrainedSnap(motion_dt, *guide);
324                 } else {
325                     m.guideFreeSnap(motion_dt, guide->normal_to_line);
326                 }
328                 switch (drag_type) {
329                     case SP_DRAG_TRANSLATE:
330                     {
331                         sp_guide_moveto(*guide, motion_dt, false);
332                         break;
333                     }
334                     case SP_DRAG_TRANSLATE_CONSTRAINED: // Is not being used currently!
335                     {
336                         Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, motion_dt);
337                         sp_guide_moveto(*guide, pt_constr, false);
338                         break;
339                     }
340                     case SP_DRAG_ROTATE:
341                     {
342                         Geom::Point pt = motion_dt - guide->point_on_line;
343                         double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
344                         if  (event->motion.state & GDK_CONTROL_MASK) {
345                             Inkscape::Preferences *prefs = Inkscape::Preferences::get();
346                             unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
347                             if (snaps) {
348                                 double sections = floor(angle * snaps / M_PI + .5);
349                                 angle = (M_PI / snaps) * sections;
350                             }
351                         }
352                         sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), false);
353                         break;
354                     }
355                     case SP_DRAG_MOVE_ORIGIN:
356                     {
357                         sp_guide_moveto(*guide, motion_dt, false);
358                         break;
359                     }
360                     case SP_DRAG_NONE:
361                         g_assert_not_reached();
362                         break;
363                 }
364                 moved = true;
365                 desktop->set_coordinate_status(from_2geom(motion_dt));
366                 desktop->setPosition(from_2geom(motion_dt));
368                 ret = TRUE;
369             }
370             break;
371     case GDK_BUTTON_RELEASE:
372             if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
373                 if (moved) {
374                     Geom::Point const event_w(event->button.x,
375                                               event->button.y);
376                     Geom::Point event_dt(desktop->w2d(event_w));
378                     SnapManager &m = desktop->namedview->snap_manager;
379                     m.setup(desktop, true, NULL, NULL, guide);
380                     if (drag_type == SP_DRAG_MOVE_ORIGIN) {
381                         // If we snap in guideConstrainedSnap() below, then motion_dt will
382                         // be forced to be on the guide. If we don't snap however, then
383                         // the origin should still be constrained to the guide. So let's
384                         // do that explicitly first:
385                         Geom::Line line(guide->point_on_line, guide->angle());
386                         Geom::Coord t = line.nearestPoint(event_dt);
387                         event_dt = line.pointAt(t);
388                         m.guideConstrainedSnap(event_dt, *guide);
389                     } else {
390                         m.guideFreeSnap(event_dt, guide->normal_to_line);
391                     }
393                     if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
394                         switch (drag_type) {
395                             case SP_DRAG_TRANSLATE:
396                             {
397                                 sp_guide_moveto(*guide, event_dt, true);
398                                 break;
399                             }
400                             case SP_DRAG_TRANSLATE_CONSTRAINED: // Is not being used currently!
401                             {
402                                 Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, event_dt);
403                                 sp_guide_moveto(*guide, pt_constr, true);
404                                 break;
405                             }
406                             case SP_DRAG_ROTATE:
407                             {
408                                 Geom::Point pt = event_dt - guide->point_on_line;
409                                 double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
410                                 if  (event->motion.state & GDK_CONTROL_MASK) {
411                                     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
412                                     unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
413                                     if (snaps) {
414                                         double sections = floor(angle * snaps / M_PI + .5);
415                                         angle = (M_PI / snaps) * sections;
416                                     }
417                                 }
418                                 sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), true);
419                                 break;
420                             }
421                             case SP_DRAG_MOVE_ORIGIN:
422                             {
423                                 sp_guide_moveto(*guide, event_dt, true);
424                                 break;
425                             }
426                             case SP_DRAG_NONE:
427                                 g_assert_not_reached();
428                                 break;
429                         }
430                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
431                                          _("Move guide"));
432                     } else {
433                         /* Undo movement of any attached shapes. */
434                         sp_guide_moveto(*guide, guide->point_on_line, false);
435                         sp_guide_set_normal(*guide, guide->normal_to_line, false);
436                         sp_guide_remove(guide);
437                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
438                                      _("Delete guide"));
439                     }
440                     moved = false;
441                     desktop->set_coordinate_status(from_2geom(event_dt));
442                     desktop->setPosition (from_2geom(event_dt));
443                 }
444                 drag_type = SP_DRAG_NONE;
445                 sp_event_context_snap_window_closed(desktop->event_context);
446                 sp_canvas_item_ungrab(item, event->button.time);
447                 ret=TRUE;
448             }
449     case GDK_ENTER_NOTIFY:
450     {
451             sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
453             // set move or rotate cursor
454             Geom::Point const event_w(event->crossing.x, event->crossing.y);
455             Geom::Point const event_dt(desktop->w2d(event_w));
457             if (event->crossing.state & GDK_SHIFT_MASK) {
458                 GdkCursor *guide_cursor;
459                 guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
460                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
461                 gdk_cursor_unref(guide_cursor);
462             }
464             char *guide_description = sp_guide_description(guide);
465             desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
466             g_free(guide_description);
467             break;
468     }
469     case GDK_LEAVE_NOTIFY:
470             sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
472             // restore event context's cursor
473             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, desktop->event_context->cursor);
475             desktop->guidesMessageContext()->clear();
476             break;
477         case GDK_KEY_PRESS:
478             switch (get_group0_keyval (&event->key)) {
479                 case GDK_Delete:
480                 case GDK_KP_Delete:
481                 case GDK_BackSpace:
482                 {
483                     SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
484                     sp_guide_remove(guide);
485                     sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
486                     ret = TRUE;
487                     break;
488                 }
489                 case GDK_Shift_L:
490                 case GDK_Shift_R:
491                     GdkCursor *guide_cursor;
492                     guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
493                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
494                     gdk_cursor_unref(guide_cursor);
495                     ret = TRUE;
496                 default:
497                     // do nothing;
498                     break;
499             }
500             break;
501         case GDK_KEY_RELEASE:
502             switch (get_group0_keyval (&event->key)) {
503                 case GDK_Shift_L:
504                 case GDK_Shift_R:
505                     GdkCursor *guide_cursor;
506                     guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
507                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
508                     gdk_cursor_unref(guide_cursor);
509                     break;
510                 default:
511                     // do nothing;
512                     break;
513             }
514     default:
515         break;
516     }
518     return ret;
521 //static std::map<GdkInputSource, std::string> switchMap;
522 static std::map<std::string, int> toolToUse;
523 static std::string lastName;
524 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
526 static void init_extended()
528     std::string avoidName = "pad";
529     GList* devices = gdk_devices_list();
530     if ( devices ) {
531         for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
532             GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
533             if ( dev->name
534                  && (avoidName != dev->name)
535                  && (dev->source != GDK_SOURCE_MOUSE) ) {
536 //                 g_message("Adding '%s' as [%d]", dev->name, dev->source);
538                 // Set the initial tool for the device
539                 switch ( dev->source ) {
540                     case GDK_SOURCE_PEN:
541                         toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
542                         break;
543                     case GDK_SOURCE_ERASER:
544                         toolToUse[dev->name] = TOOLS_ERASER;
545                         break;
546                     case GDK_SOURCE_CURSOR:
547                         toolToUse[dev->name] = TOOLS_SELECT;
548                         break;
549                     default:
550                         ; // do not add
551                 }
552 //            } else if (dev->name) {
553 //                 g_message("Skippn '%s' as [%s]", dev->name, dev->source);
554             }
555         }
556     }
560 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
562     GdkInputSource source = GDK_SOURCE_MOUSE;
563     std::string name;
565     switch ( event->type ) {
566         case GDK_MOTION_NOTIFY:
567         {
568             GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
569             if ( event2->device ) {
570                 source = event2->device->source;
571                 name = event2->device->name;
572             }
573         }
574         break;
576         case GDK_BUTTON_PRESS:
577         case GDK_2BUTTON_PRESS:
578         case GDK_3BUTTON_PRESS:
579         case GDK_BUTTON_RELEASE:
580         {
581             GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
582             if ( event2->device ) {
583                 source = event2->device->source;
584                 name = event2->device->name;
585             }
586         }
587         break;
589         case GDK_SCROLL:
590         {
591             GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
592             if ( event2->device ) {
593                 source = event2->device->source;
594                 name = event2->device->name;
595             }
596         }
597         break;
599         case GDK_PROXIMITY_IN:
600         case GDK_PROXIMITY_OUT:
601         {
602             GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
603             if ( event2->device ) {
604                 source = event2->device->source;
605                 name = event2->device->name;
606             }
607         }
608         break;
610         default:
611             ;
612     }
614     if (!name.empty()) {
615         if ( lastType != source || lastName != name ) {
616             // The device switched. See if it is one we 'count'
617             //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
618             std::map<std::string, int>::iterator it = toolToUse.find(lastName);
619             if (it != toolToUse.end()) {
620                 // Save the tool currently selected for next time the input
621                 // device shows up.
622                 it->second = tools_active(desktop);
623             }
625             it = toolToUse.find(name);
626             if (it != toolToUse.end() ) {
627                 tools_switch(desktop, it->second);
628             }
630             lastName = name;
631             lastType = source;
632         }
633     }
638 /*
639   Local Variables:
640   mode:c++
641   c-file-style:"stroustrup"
642   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
643   indent-tabs-mode:nil
644   fill-column:99
645   End:
646 */
647 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :