Code

SPDocument->Document
[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     int wx, wy;
76     SPDesktop *desktop = dtw->desktop;
77     Inkscape::XML::Node *repr = SP_OBJECT_REPR(desktop->namedview);
79     gdk_window_get_pointer(GTK_WIDGET(dtw->canvas)->window, &wx, &wy, NULL);
80     Geom::Point const event_win(wx, wy);
82     gint width, height;
83     gdk_window_get_geometry(GTK_WIDGET(dtw->canvas)->window, NULL /*x*/, NULL /*y*/, &width, &height, NULL/*depth*/);
85     switch (event->type) {
86     case GDK_BUTTON_PRESS:
87             if (event->button.button == 1) {
88                 dragging = true;
90                 // FIXME: The snap delay mechanism won't work here, because it has been implemented for the event context. Dragging
91                 // guides off the ruler will send event to the ruler and not to the context, which bypasses sp_event_context_snap_delay_handler
93                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
94                 Geom::Point const event_dt(desktop->w2d(event_w));
96                 // explicitly show guidelines; if I draw a guide, I want them on
97                 sp_repr_set_boolean(repr, "showguides", TRUE);
98                 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
100                 // calculate the normal of the guidelines when dragged from the edges of rulers.
101                 Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
102                 Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
103                 normal_bl_to_tr.normalize();
104                 normal_tr_to_bl.normalize();
105                 Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
106                 if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
107                     Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
108                     if (event->button.state & GDK_CONTROL_MASK) {
109                         // guidelines normal to gridlines
110                         normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
111                         normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
112                     } else {
113                         normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
114                         normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
115                     }
116                 }
117                 if (horiz) {
118                     if (wx < 50) {
119                         normal = normal_bl_to_tr;
120                     } else if (wx > width - 50) {
121                         normal = normal_tr_to_bl;
122                     } else {
123                         normal = Geom::Point(0.,1.);
124                     }
125                 } else {
126                     if (wy < 50) {
127                         normal = normal_bl_to_tr;
128                     } else if (wy > height - 50) {
129                         normal = normal_tr_to_bl;
130                     } else {
131                         normal = Geom::Point(1.,0.);
132                     }
133                 }
135                 guide = sp_guideline_new(desktop->guides, event_dt, normal);
136                 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
137                 gdk_pointer_grab(widget->window, FALSE,
138                                  (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
139                                  NULL, NULL,
140                                  event->button.time);
141             }
142             break;
143     case GDK_MOTION_NOTIFY:
144             if (dragging) {
145                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
146                 Geom::Point event_dt(desktop->w2d(event_w));
148                 SnapManager &m = desktop->namedview->snap_manager;
149                 m.setup(desktop);
150                 // We only have a temporary guide which is not stored in our document yet.
151                 // Because the guide snapper only looks in the document for guides to snap to,
152                 // we don't have to worry about a guide snapping to itself here
153                 m.guideFreeSnap(event_dt, normal, SP_DRAG_MOVE_ORIGIN);
155                 sp_guideline_set_position(SP_GUIDELINE(guide), from_2geom(event_dt));
156                 desktop->set_coordinate_status(to_2geom(event_dt));
157                 desktop->setPosition(to_2geom(event_dt));
158             }
159             break;
160     case GDK_BUTTON_RELEASE:
161             if (dragging && event->button.button == 1) {
162                 gdk_pointer_ungrab(event->button.time);
163                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
164                 Geom::Point event_dt(desktop->w2d(event_w));
166                 SnapManager &m = desktop->namedview->snap_manager;
167                 m.setup(desktop);
168                 // We only have a temporary guide which is not stored in our document yet.
169                 // Because the guide snapper only looks in the document for guides to snap to,
170                 // we don't have to worry about a guide snapping to itself here
171                 m.guideFreeSnap(event_dt, normal, SP_DRAG_MOVE_ORIGIN);
173                 dragging = false;
175                 sp_event_context_discard_delayed_snap_event(desktop->event_context);
177                 gtk_object_destroy(GTK_OBJECT(guide));
178                 guide = NULL;
179                 if ((horiz ? wy : wx) >= 0) {
180                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
181                     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
182                     sp_repr_set_point(repr, "orientation", normal);
183                     sp_repr_set_point(repr, "position", from_2geom(event_dt));
184                     SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
185                     Inkscape::GC::release(repr);
186                     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
187                                      _("Create guide"));
188                 }
189                 desktop->set_coordinate_status(from_2geom(event_dt));
191                 // A dt_ruler_event might be emitted when dragging a guide of the rulers
192                 // while drawing a Bezier curve. In such a situation, we're already in that
193                 // specific context and the snap delay is already active. We should interfere
194                 // with that context and we should therefore leave the snap delay status
195                 // as it is. So although it might have been set to active above on
196                 // GDK_BUTTON_PRESS, we should not set it back to inactive here. That must be
197                 // done by the context.
198             }
199         default:
200             break;
201     }
203     return FALSE;
206 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
208     return sp_dt_ruler_event(widget, event, dtw, true);
211 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
213     return sp_dt_ruler_event(widget, event, dtw, false);
216 static Geom::Point drag_origin;
217 static SPGuideDragType drag_type = SP_DRAG_NONE;
218 //static bool reset_drag_origin = false; // when Ctrl is pressed while dragging, this is used to trigger resetting of the
219 //                                       // drag origin to that location so that constrained movement is more intuitive
221 // Min distance from anchor to initiate rotation, measured in screenpixels
222 #define tol 40.0
224 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
226     static bool moved = false;
227     gint ret = FALSE;
229     SPGuide *guide = SP_GUIDE(data);
230     SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
232     switch (event->type) {
233         case GDK_2BUTTON_PRESS:
234             if (event->button.button == 1) {
235                 drag_type = SP_DRAG_NONE;
236                 sp_event_context_discard_delayed_snap_event(desktop->event_context);
237                 sp_canvas_item_ungrab(item, event->button.time);
238                 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
239                 ret = TRUE;
240             }
241             break;
242         case GDK_BUTTON_PRESS:
243             if (event->button.button == 1) {
244                 Geom::Point const event_w(event->button.x, event->button.y);
245                 Geom::Point const event_dt(desktop->w2d(event_w));
247                 // Due to the tolerance allowed when grabbing a guide, event_dt will generally
248                 // be close to the guide but not just exactly on it. The drag origin calculated
249                 // here must be exactly on the guide line though, otherwise
250                 // small errors will occur once we snap, see
251                 // https://bugs.launchpad.net/inkscape/+bug/333762
252                 drag_origin = Geom::projection(event_dt, Geom::Line(guide->point_on_line, guide->angle()));
254                 if (event->button.state & GDK_SHIFT_MASK) {
255                     // with shift we rotate the guide
256                     drag_type = SP_DRAG_ROTATE;
257                 } else {
258                     if (event->button.state & GDK_CONTROL_MASK) {
259                         drag_type = SP_DRAG_MOVE_ORIGIN;
260                     } else {
261                         drag_type = SP_DRAG_TRANSLATE;
262                     }
263                 }
265                 if (drag_type == SP_DRAG_ROTATE || drag_type == SP_DRAG_TRANSLATE) {
266                     sp_canvas_item_grab(item,
267                                         ( GDK_BUTTON_RELEASE_MASK  |
268                                           GDK_BUTTON_PRESS_MASK    |
269                                           GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
270                                         NULL,
271                                         event->button.time);
272                 }
273                 ret = TRUE;
274             }
275             break;
276         case GDK_MOTION_NOTIFY:
277             if (drag_type != SP_DRAG_NONE) {
278                 Geom::Point const motion_w(event->motion.x,
279                                            event->motion.y);
280                 Geom::Point motion_dt(desktop->w2d(motion_w));
282                 // This is for snapping while dragging existing guidelines. New guidelines,
283                 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
284                 SnapManager &m = desktop->namedview->snap_manager;
285                 m.setup(desktop, true, NULL, NULL, guide);
286                 if (drag_type == SP_DRAG_MOVE_ORIGIN) {
287                     // If we snap in guideConstrainedSnap() below, then motion_dt will
288                     // be forced to be on the guide. If we don't snap however, then
289                     // the origin should still be constrained to the guide. So let's do
290                     // that explicitly first:
292                     Geom::Line line(guide->point_on_line, guide->angle());
293                     Geom::Coord t = line.nearestPoint(motion_dt);
294                     motion_dt = line.pointAt(t);
295                     m.guideConstrainedSnap(motion_dt, *guide);
296                 } else {
297                     m.guideFreeSnap(motion_dt, guide->normal_to_line, drag_type);
298                 }
300                 switch (drag_type) {
301                     case SP_DRAG_TRANSLATE:
302                     {
303                         sp_guide_moveto(*guide, motion_dt, false);
304                         break;
305                     }
306                     case SP_DRAG_ROTATE:
307                     {
308                         Geom::Point pt = motion_dt - guide->point_on_line;
309                         double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
310                         if  (event->motion.state & GDK_CONTROL_MASK) {
311                             Inkscape::Preferences *prefs = Inkscape::Preferences::get();
312                             unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
313                             if (snaps) {
314                                 double sections = floor(angle * snaps / M_PI + .5);
315                                 angle = (M_PI / snaps) * sections;
316                             }
317                         }
318                         sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), false);
319                         break;
320                     }
321                     case SP_DRAG_MOVE_ORIGIN:
322                     {
323                         sp_guide_moveto(*guide, motion_dt, false);
324                         break;
325                     }
326                     case SP_DRAG_NONE:
327                         g_assert_not_reached();
328                         break;
329                 }
330                 moved = true;
331                 desktop->set_coordinate_status(from_2geom(motion_dt));
332                 desktop->setPosition(from_2geom(motion_dt));
334                 ret = TRUE;
335             }
336             break;
337     case GDK_BUTTON_RELEASE:
338             if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
339                 if (moved) {
340                     Geom::Point const event_w(event->button.x,
341                                               event->button.y);
342                     Geom::Point event_dt(desktop->w2d(event_w));
344                     SnapManager &m = desktop->namedview->snap_manager;
345                     m.setup(desktop, true, NULL, NULL, guide);
346                     if (drag_type == SP_DRAG_MOVE_ORIGIN) {
347                         // If we snap in guideConstrainedSnap() below, then motion_dt will
348                         // be forced to be on the guide. If we don't snap however, then
349                         // the origin should still be constrained to the guide. So let's
350                         // do that explicitly first:
351                         Geom::Line line(guide->point_on_line, guide->angle());
352                         Geom::Coord t = line.nearestPoint(event_dt);
353                         event_dt = line.pointAt(t);
354                         m.guideConstrainedSnap(event_dt, *guide);
355                     } else {
356                         m.guideFreeSnap(event_dt, guide->normal_to_line, drag_type);
357                     }
359                     if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
360                         switch (drag_type) {
361                             case SP_DRAG_TRANSLATE:
362                             {
363                                 sp_guide_moveto(*guide, event_dt, true);
364                                 break;
365                             }
366                             case SP_DRAG_ROTATE:
367                             {
368                                 Geom::Point pt = event_dt - guide->point_on_line;
369                                 double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
370                                 if  (event->motion.state & GDK_CONTROL_MASK) {
371                                     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
372                                     unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
373                                     if (snaps) {
374                                         double sections = floor(angle * snaps / M_PI + .5);
375                                         angle = (M_PI / snaps) * sections;
376                                     }
377                                 }
378                                 sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), true);
379                                 break;
380                             }
381                             case SP_DRAG_MOVE_ORIGIN:
382                             {
383                                 sp_guide_moveto(*guide, event_dt, true);
384                                 break;
385                             }
386                             case SP_DRAG_NONE:
387                                 g_assert_not_reached();
388                                 break;
389                         }
390                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
391                                          _("Move guide"));
392                     } else {
393                         /* Undo movement of any attached shapes. */
394                         sp_guide_moveto(*guide, guide->point_on_line, false);
395                         sp_guide_set_normal(*guide, guide->normal_to_line, false);
396                         sp_guide_remove(guide);
397                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
398                                      _("Delete guide"));
399                     }
400                     moved = false;
401                     desktop->set_coordinate_status(from_2geom(event_dt));
402                     desktop->setPosition (from_2geom(event_dt));
403                 }
404                 drag_type = SP_DRAG_NONE;
405                 sp_event_context_discard_delayed_snap_event(desktop->event_context);
406                 sp_canvas_item_ungrab(item, event->button.time);
407                 ret=TRUE;
408             }
409     case GDK_ENTER_NOTIFY:
410     {
411             sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
413             // set move or rotate cursor
414             Geom::Point const event_w(event->crossing.x, event->crossing.y);
415             Geom::Point const event_dt(desktop->w2d(event_w));
417             if (event->crossing.state & GDK_SHIFT_MASK) {
418                 GdkCursor *guide_cursor;
419                 guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
420                 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
421                 gdk_cursor_unref(guide_cursor);
422             }
424             char *guide_description = sp_guide_description(guide);
425             desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
426             g_free(guide_description);
427             break;
428     }
429     case GDK_LEAVE_NOTIFY:
430             sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
432             // restore event context's cursor
433             gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, desktop->event_context->cursor);
435             desktop->guidesMessageContext()->clear();
436             break;
437         case GDK_KEY_PRESS:
438             switch (get_group0_keyval (&event->key)) {
439                 case GDK_Delete:
440                 case GDK_KP_Delete:
441                 case GDK_BackSpace:
442                 {
443                     Document *doc = SP_OBJECT_DOCUMENT(guide);
444                     sp_guide_remove(guide);
445                     sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
446                     ret = TRUE;
447                     break;
448                 }
449                 case GDK_Shift_L:
450                 case GDK_Shift_R:
451                     GdkCursor *guide_cursor;
452                     guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
453                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
454                     gdk_cursor_unref(guide_cursor);
455                     ret = TRUE;
456                 default:
457                     // do nothing;
458                     break;
459             }
460             break;
461         case GDK_KEY_RELEASE:
462             switch (get_group0_keyval (&event->key)) {
463                 case GDK_Shift_L:
464                 case GDK_Shift_R:
465                     GdkCursor *guide_cursor;
466                     guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
467                     gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
468                     gdk_cursor_unref(guide_cursor);
469                     break;
470                 default:
471                     // do nothing;
472                     break;
473             }
474     default:
475         break;
476     }
478     return ret;
481 //static std::map<GdkInputSource, std::string> switchMap;
482 static std::map<std::string, int> toolToUse;
483 static std::string lastName;
484 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
486 static void init_extended()
488     std::string avoidName = "pad";
489     GList* devices = gdk_devices_list();
490     if ( devices ) {
491         for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
492             GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
493             if ( dev->name
494                  && (avoidName != dev->name)
495                  && (dev->source != GDK_SOURCE_MOUSE) ) {
496 //                 g_message("Adding '%s' as [%d]", dev->name, dev->source);
498                 // Set the initial tool for the device
499                 switch ( dev->source ) {
500                     case GDK_SOURCE_PEN:
501                         toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
502                         break;
503                     case GDK_SOURCE_ERASER:
504                         toolToUse[dev->name] = TOOLS_ERASER;
505                         break;
506                     case GDK_SOURCE_CURSOR:
507                         toolToUse[dev->name] = TOOLS_SELECT;
508                         break;
509                     default:
510                         ; // do not add
511                 }
512 //            } else if (dev->name) {
513 //                 g_message("Skippn '%s' as [%s]", dev->name, dev->source);
514             }
515         }
516     }
520 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
522     GdkInputSource source = GDK_SOURCE_MOUSE;
523     std::string name;
525     switch ( event->type ) {
526         case GDK_MOTION_NOTIFY:
527         {
528             GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
529             if ( event2->device ) {
530                 source = event2->device->source;
531                 name = event2->device->name;
532             }
533         }
534         break;
536         case GDK_BUTTON_PRESS:
537         case GDK_2BUTTON_PRESS:
538         case GDK_3BUTTON_PRESS:
539         case GDK_BUTTON_RELEASE:
540         {
541             GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
542             if ( event2->device ) {
543                 source = event2->device->source;
544                 name = event2->device->name;
545             }
546         }
547         break;
549         case GDK_SCROLL:
550         {
551             GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
552             if ( event2->device ) {
553                 source = event2->device->source;
554                 name = event2->device->name;
555             }
556         }
557         break;
559         case GDK_PROXIMITY_IN:
560         case GDK_PROXIMITY_OUT:
561         {
562             GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
563             if ( event2->device ) {
564                 source = event2->device->source;
565                 name = event2->device->name;
566             }
567         }
568         break;
570         default:
571             ;
572     }
574     if (!name.empty()) {
575         if ( lastType != source || lastName != name ) {
576             // The device switched. See if it is one we 'count'
577             //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
578             std::map<std::string, int>::iterator it = toolToUse.find(lastName);
579             if (it != toolToUse.end()) {
580                 // Save the tool currently selected for next time the input
581                 // device shows up.
582                 it->second = tools_active(desktop);
583             }
585             it = toolToUse.find(name);
586             if (it != toolToUse.end() ) {
587                 tools_switch(desktop, it->second);
588             }
590             lastName = name;
591             lastType = source;
592         }
593     }
598 /*
599   Local Variables:
600   mode:c++
601   c-file-style:"stroustrup"
602   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
603   indent-tabs-mode:nil
604   fill-column:99
605   End:
606 */
607 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :