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;
204 }
206 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
207 {
208 if (event->type == GDK_MOTION_NOTIFY) {
209 sp_event_context_snap_delay_handler(dtw->desktop->event_context, (gpointer) widget, (gpointer) dtw, (GdkEventMotion *)event, DelayedSnapEvent::GUIDE_HRULER);
210 }
211 return sp_dt_ruler_event(widget, event, dtw, true);
212 }
214 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
215 {
216 if (event->type == GDK_MOTION_NOTIFY) {
217 sp_event_context_snap_delay_handler(dtw->desktop->event_context, (gpointer) widget, (gpointer) dtw, (GdkEventMotion *)event, DelayedSnapEvent::GUIDE_VRULER);
218 }
219 return sp_dt_ruler_event(widget, event, dtw, false);
220 }
222 static Geom::Point drag_origin;
223 static SPGuideDragType drag_type = SP_DRAG_NONE;
224 //static bool reset_drag_origin = false; // when Ctrl is pressed while dragging, this is used to trigger resetting of the
225 // // drag origin to that location so that constrained movement is more intuitive
227 // Min distance from anchor to initiate rotation, measured in screenpixels
228 #define tol 40.0
230 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
231 {
232 static bool moved = false;
233 gint ret = FALSE;
235 SPGuide *guide = SP_GUIDE(data);
236 SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
238 switch (event->type) {
239 case GDK_2BUTTON_PRESS:
240 if (event->button.button == 1) {
241 drag_type = SP_DRAG_NONE;
242 sp_event_context_discard_delayed_snap_event(desktop->event_context);
243 sp_canvas_item_ungrab(item, event->button.time);
244 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
245 ret = TRUE;
246 }
247 break;
248 case GDK_BUTTON_PRESS:
249 if (event->button.button == 1) {
250 Geom::Point const event_w(event->button.x, event->button.y);
251 Geom::Point const event_dt(desktop->w2d(event_w));
253 // Due to the tolerance allowed when grabbing a guide, event_dt will generally
254 // be close to the guide but not just exactly on it. The drag origin calculated
255 // here must be exactly on the guide line though, otherwise
256 // small errors will occur once we snap, see
257 // https://bugs.launchpad.net/inkscape/+bug/333762
258 drag_origin = Geom::projection(event_dt, Geom::Line(guide->point_on_line, guide->angle()));
260 if (event->button.state & GDK_SHIFT_MASK) {
261 // with shift we rotate the guide
262 drag_type = SP_DRAG_ROTATE;
263 } else {
264 if (event->button.state & GDK_CONTROL_MASK) {
265 drag_type = SP_DRAG_MOVE_ORIGIN;
266 } else {
267 drag_type = SP_DRAG_TRANSLATE;
268 }
269 }
271 if (drag_type == SP_DRAG_ROTATE || drag_type == SP_DRAG_TRANSLATE) {
272 sp_canvas_item_grab(item,
273 ( GDK_BUTTON_RELEASE_MASK |
274 GDK_BUTTON_PRESS_MASK |
275 GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
276 NULL,
277 event->button.time);
278 }
279 ret = TRUE;
280 }
281 break;
282 case GDK_MOTION_NOTIFY:
283 if (drag_type != SP_DRAG_NONE) {
284 Geom::Point const motion_w(event->motion.x,
285 event->motion.y);
286 Geom::Point motion_dt(desktop->w2d(motion_w));
288 sp_event_context_snap_delay_handler(desktop->event_context, (gpointer) item, data, (GdkEventMotion *)event, DelayedSnapEvent::GUIDE_HANDLER);
290 // This is for snapping while dragging existing guidelines. New guidelines,
291 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
292 SnapManager &m = desktop->namedview->snap_manager;
293 m.setup(desktop, true, NULL, NULL, guide);
294 if (drag_type == SP_DRAG_MOVE_ORIGIN) {
295 // If we snap in guideConstrainedSnap() below, then motion_dt will
296 // be forced to be on the guide. If we don't snap however, then
297 // the origin should still be constrained to the guide. So let's do
298 // that explicitly first:
300 Geom::Line line(guide->point_on_line, guide->angle());
301 Geom::Coord t = line.nearestPoint(motion_dt);
302 motion_dt = line.pointAt(t);
303 m.guideConstrainedSnap(motion_dt, *guide);
304 } else {
305 m.guideFreeSnap(motion_dt, guide->normal_to_line, drag_type);
306 }
308 switch (drag_type) {
309 case SP_DRAG_TRANSLATE:
310 {
311 sp_guide_moveto(*guide, motion_dt, false);
312 break;
313 }
314 case SP_DRAG_ROTATE:
315 {
316 Geom::Point pt = motion_dt - guide->point_on_line;
317 double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
318 if (event->motion.state & GDK_CONTROL_MASK) {
319 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
320 unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
321 if (snaps) {
322 double sections = floor(angle * snaps / M_PI + .5);
323 angle = (M_PI / snaps) * sections;
324 }
325 }
326 sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), false);
327 break;
328 }
329 case SP_DRAG_MOVE_ORIGIN:
330 {
331 sp_guide_moveto(*guide, motion_dt, false);
332 break;
333 }
334 case SP_DRAG_NONE:
335 g_assert_not_reached();
336 break;
337 }
338 moved = true;
339 desktop->set_coordinate_status(from_2geom(motion_dt));
340 desktop->setPosition(from_2geom(motion_dt));
342 ret = TRUE;
343 }
344 break;
345 case GDK_BUTTON_RELEASE:
346 if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
347 if (moved) {
348 Geom::Point const event_w(event->button.x,
349 event->button.y);
350 Geom::Point event_dt(desktop->w2d(event_w));
352 SnapManager &m = desktop->namedview->snap_manager;
353 m.setup(desktop, true, NULL, NULL, guide);
354 if (drag_type == SP_DRAG_MOVE_ORIGIN) {
355 // If we snap in guideConstrainedSnap() below, then motion_dt will
356 // be forced to be on the guide. If we don't snap however, then
357 // the origin should still be constrained to the guide. So let's
358 // do that explicitly first:
359 Geom::Line line(guide->point_on_line, guide->angle());
360 Geom::Coord t = line.nearestPoint(event_dt);
361 event_dt = line.pointAt(t);
362 m.guideConstrainedSnap(event_dt, *guide);
363 } else {
364 m.guideFreeSnap(event_dt, guide->normal_to_line, drag_type);
365 }
367 if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
368 switch (drag_type) {
369 case SP_DRAG_TRANSLATE:
370 {
371 sp_guide_moveto(*guide, event_dt, true);
372 break;
373 }
374 case SP_DRAG_ROTATE:
375 {
376 Geom::Point pt = event_dt - guide->point_on_line;
377 double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
378 if (event->motion.state & GDK_CONTROL_MASK) {
379 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
380 unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
381 if (snaps) {
382 double sections = floor(angle * snaps / M_PI + .5);
383 angle = (M_PI / snaps) * sections;
384 }
385 }
386 sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), true);
387 break;
388 }
389 case SP_DRAG_MOVE_ORIGIN:
390 {
391 sp_guide_moveto(*guide, event_dt, true);
392 break;
393 }
394 case SP_DRAG_NONE:
395 g_assert_not_reached();
396 break;
397 }
398 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
399 _("Move guide"));
400 } else {
401 /* Undo movement of any attached shapes. */
402 sp_guide_moveto(*guide, guide->point_on_line, false);
403 sp_guide_set_normal(*guide, guide->normal_to_line, false);
404 sp_guide_remove(guide);
405 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
406 _("Delete guide"));
407 }
408 sp_event_context_discard_delayed_snap_event(desktop->event_context);
409 moved = false;
410 desktop->set_coordinate_status(from_2geom(event_dt));
411 desktop->setPosition (from_2geom(event_dt));
412 }
413 drag_type = SP_DRAG_NONE;
414 sp_canvas_item_ungrab(item, event->button.time);
415 ret=TRUE;
416 }
417 case GDK_ENTER_NOTIFY:
418 {
419 sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
421 // set move or rotate cursor
422 Geom::Point const event_w(event->crossing.x, event->crossing.y);
423 Geom::Point const event_dt(desktop->w2d(event_w));
425 if (event->crossing.state & GDK_SHIFT_MASK) {
426 GdkCursor *guide_cursor;
427 guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
428 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
429 gdk_cursor_unref(guide_cursor);
430 }
432 char *guide_description = sp_guide_description(guide);
433 desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
434 g_free(guide_description);
435 break;
436 }
437 case GDK_LEAVE_NOTIFY:
438 sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
440 // restore event context's cursor
441 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, desktop->event_context->cursor);
443 desktop->guidesMessageContext()->clear();
444 break;
445 case GDK_KEY_PRESS:
446 switch (get_group0_keyval (&event->key)) {
447 case GDK_Delete:
448 case GDK_KP_Delete:
449 case GDK_BackSpace:
450 {
451 SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
452 sp_guide_remove(guide);
453 sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
454 ret = TRUE;
455 sp_event_context_discard_delayed_snap_event(desktop->event_context);
456 break;
457 }
458 case GDK_Shift_L:
459 case GDK_Shift_R:
460 GdkCursor *guide_cursor;
461 guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
462 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
463 gdk_cursor_unref(guide_cursor);
464 ret = TRUE;
465 default:
466 // do nothing;
467 break;
468 }
469 break;
470 case GDK_KEY_RELEASE:
471 switch (get_group0_keyval (&event->key)) {
472 case GDK_Shift_L:
473 case GDK_Shift_R:
474 GdkCursor *guide_cursor;
475 guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
476 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
477 gdk_cursor_unref(guide_cursor);
478 break;
479 default:
480 // do nothing;
481 break;
482 }
483 default:
484 break;
485 }
487 return ret;
488 }
490 //static std::map<GdkInputSource, std::string> switchMap;
491 static std::map<std::string, int> toolToUse;
492 static std::string lastName;
493 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
495 static void init_extended()
496 {
497 std::string avoidName = "pad";
498 GList* devices = gdk_devices_list();
499 if ( devices ) {
500 for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
501 GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
502 if ( dev->name
503 && (avoidName != dev->name)
504 && (dev->source != GDK_SOURCE_MOUSE) ) {
505 // g_message("Adding '%s' as [%d]", dev->name, dev->source);
507 // Set the initial tool for the device
508 switch ( dev->source ) {
509 case GDK_SOURCE_PEN:
510 toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
511 break;
512 case GDK_SOURCE_ERASER:
513 toolToUse[dev->name] = TOOLS_ERASER;
514 break;
515 case GDK_SOURCE_CURSOR:
516 toolToUse[dev->name] = TOOLS_SELECT;
517 break;
518 default:
519 ; // do not add
520 }
521 // } else if (dev->name) {
522 // g_message("Skippn '%s' as [%s]", dev->name, dev->source);
523 }
524 }
525 }
526 }
529 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
530 {
531 GdkInputSource source = GDK_SOURCE_MOUSE;
532 std::string name;
534 switch ( event->type ) {
535 case GDK_MOTION_NOTIFY:
536 {
537 GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
538 if ( event2->device ) {
539 source = event2->device->source;
540 name = event2->device->name;
541 }
542 }
543 break;
545 case GDK_BUTTON_PRESS:
546 case GDK_2BUTTON_PRESS:
547 case GDK_3BUTTON_PRESS:
548 case GDK_BUTTON_RELEASE:
549 {
550 GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
551 if ( event2->device ) {
552 source = event2->device->source;
553 name = event2->device->name;
554 }
555 }
556 break;
558 case GDK_SCROLL:
559 {
560 GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
561 if ( event2->device ) {
562 source = event2->device->source;
563 name = event2->device->name;
564 }
565 }
566 break;
568 case GDK_PROXIMITY_IN:
569 case GDK_PROXIMITY_OUT:
570 {
571 GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
572 if ( event2->device ) {
573 source = event2->device->source;
574 name = event2->device->name;
575 }
576 }
577 break;
579 default:
580 ;
581 }
583 if (!name.empty()) {
584 if ( lastType != source || lastName != name ) {
585 // The device switched. See if it is one we 'count'
586 //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
587 std::map<std::string, int>::iterator it = toolToUse.find(lastName);
588 if (it != toolToUse.end()) {
589 // Save the tool currently selected for next time the input
590 // device shows up.
591 it->second = tools_active(desktop);
592 }
594 it = toolToUse.find(name);
595 if (it != toolToUse.end() ) {
596 tools_switch(desktop, it->second);
597 }
599 lastName = name;
600 lastType = source;
601 }
602 }
603 }
607 /*
608 Local Variables:
609 mode:c++
610 c-file-style:"stroustrup"
611 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
612 indent-tabs-mode:nil
613 fill-column:99
614 End:
615 */
616 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :