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 * Copyright (C) 1999-2010 Others
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16 #include <map>
17 #include <string>
18 #include <2geom/line.h>
19 #include <glibmm/i18n.h>
21 #include "desktop.h"
22 #include "desktop-handles.h"
23 #include "dialogs/dialog-events.h"
24 #include "display/canvas-axonomgrid.h"
25 #include "display/canvas-grid.h"
26 #include "display/guideline.h"
27 #include "display/snap-indicator.h"
28 #include "document.h"
29 #include "event-context.h"
30 #include "helper/action.h"
31 #include "helper/unit-menu.h"
32 #include "helper/units.h"
33 #include "message-context.h"
34 #include "preferences.h"
35 #include "snap.h"
36 #include "sp-guide.h"
37 #include "sp-metrics.h"
38 #include "sp-namedview.h"
39 #include "tools-switch.h"
40 #include "ui/dialog/guides.h"
41 #include "widgets/desktop-widget.h"
42 #include "xml/repr.h"
44 static void snoop_extended(GdkEvent* event, SPDesktop *desktop);
45 static void init_extended();
47 /* Root item handler */
49 int sp_desktop_root_handler(SPCanvasItem */*item*/, GdkEvent *event, SPDesktop *desktop)
50 {
51 static bool watch = false;
52 static bool first = true;
54 if ( first ) {
55 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
56 if ( prefs->getBool("/options/useextinput/value", true)
57 && prefs->getBool("/options/switchonextinput/value") ) {
58 watch = true;
59 init_extended();
60 }
61 first = false;
62 }
63 if ( watch ) {
64 snoop_extended(event, desktop);
65 }
67 return sp_event_context_root_handler(desktop->event_context, event);
68 }
70 static gint sp_dt_ruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw, bool horiz)
71 {
72 static bool dragging = false;
73 static SPCanvasItem *guide = NULL;
74 static Geom::Point normal;
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 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
92 Geom::Point const event_dt(desktop->w2d(event_w));
94 // explicitly show guidelines; if I draw a guide, I want them on
95 sp_repr_set_boolean(repr, "showguides", TRUE);
96 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
98 // calculate the normal of the guidelines when dragged from the edges of rulers.
99 Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
100 Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
101 normal_bl_to_tr.normalize();
102 normal_tr_to_bl.normalize();
103 Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
104 if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
105 Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
106 if (event->button.state & GDK_CONTROL_MASK) {
107 // guidelines normal to gridlines
108 normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
109 normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
110 } else {
111 normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
112 normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
113 }
114 }
115 if (horiz) {
116 if (wx < 50) {
117 normal = normal_bl_to_tr;
118 } else if (wx > width - 50) {
119 normal = normal_tr_to_bl;
120 } else {
121 normal = Geom::Point(0.,1.);
122 }
123 } else {
124 if (wy < 50) {
125 normal = normal_bl_to_tr;
126 } else if (wy > height - 50) {
127 normal = normal_tr_to_bl;
128 } else {
129 normal = Geom::Point(1.,0.);
130 }
131 }
133 guide = sp_guideline_new(desktop->guides, event_dt, normal);
134 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
135 gdk_pointer_grab(widget->window, FALSE,
136 (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
137 NULL, NULL,
138 event->button.time);
139 }
140 break;
141 case GDK_MOTION_NOTIFY:
142 if (dragging) {
143 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
144 Geom::Point event_dt(desktop->w2d(event_w));
146 SnapManager &m = desktop->namedview->snap_manager;
147 m.setup(desktop);
148 // We only have a temporary guide which is not stored in our document yet.
149 // Because the guide snapper only looks in the document for guides to snap to,
150 // we don't have to worry about a guide snapping to itself here
151 m.guideFreeSnap(event_dt, normal, SP_DRAG_MOVE_ORIGIN);
153 sp_guideline_set_position(SP_GUIDELINE(guide), from_2geom(event_dt));
154 desktop->set_coordinate_status(to_2geom(event_dt));
155 desktop->setPosition(to_2geom(event_dt));
156 }
157 break;
158 case GDK_BUTTON_RELEASE:
159 if (dragging && event->button.button == 1) {
160 sp_event_context_discard_delayed_snap_event(desktop->event_context);
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 gtk_object_destroy(GTK_OBJECT(guide));
176 guide = NULL;
177 if ((horiz ? wy : wx) >= 0) {
178 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
179 Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
180 sp_repr_set_point(repr, "orientation", normal);
181 sp_repr_set_point(repr, "position", from_2geom(event_dt));
182 SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
183 Inkscape::GC::release(repr);
184 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
185 _("Create guide"));
186 }
187 desktop->set_coordinate_status(from_2geom(event_dt));
188 }
189 default:
190 break;
191 }
193 return FALSE;
194 }
196 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
197 {
198 if (event->type == GDK_MOTION_NOTIFY) {
199 sp_event_context_snap_delay_handler(dtw->desktop->event_context, (gpointer) widget, (gpointer) dtw, (GdkEventMotion *)event, DelayedSnapEvent::GUIDE_HRULER);
200 }
201 return sp_dt_ruler_event(widget, event, dtw, true);
202 }
204 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
205 {
206 if (event->type == GDK_MOTION_NOTIFY) {
207 sp_event_context_snap_delay_handler(dtw->desktop->event_context, (gpointer) widget, (gpointer) dtw, (GdkEventMotion *)event, DelayedSnapEvent::GUIDE_VRULER);
208 }
209 return sp_dt_ruler_event(widget, event, dtw, false);
210 }
212 static Geom::Point drag_origin;
213 static SPGuideDragType drag_type = SP_DRAG_NONE;
214 //static bool reset_drag_origin = false; // when Ctrl is pressed while dragging, this is used to trigger resetting of the
215 // // drag origin to that location so that constrained movement is more intuitive
217 // Min distance from anchor to initiate rotation, measured in screenpixels
218 #define tol 40.0
220 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
221 {
222 static bool moved = false;
223 gint ret = FALSE;
225 SPGuide *guide = SP_GUIDE(data);
226 SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
228 switch (event->type) {
229 case GDK_2BUTTON_PRESS:
230 if (event->button.button == 1) {
231 drag_type = SP_DRAG_NONE;
232 sp_event_context_discard_delayed_snap_event(desktop->event_context);
233 sp_canvas_item_ungrab(item, event->button.time);
234 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
235 ret = TRUE;
236 }
237 break;
238 case GDK_BUTTON_PRESS:
239 if (event->button.button == 1) {
240 Geom::Point const event_w(event->button.x, event->button.y);
241 Geom::Point const event_dt(desktop->w2d(event_w));
243 // Due to the tolerance allowed when grabbing a guide, event_dt will generally
244 // be close to the guide but not just exactly on it. The drag origin calculated
245 // here must be exactly on the guide line though, otherwise
246 // small errors will occur once we snap, see
247 // https://bugs.launchpad.net/inkscape/+bug/333762
248 drag_origin = Geom::projection(event_dt, Geom::Line(guide->point_on_line, guide->angle()));
250 if (event->button.state & GDK_SHIFT_MASK) {
251 // with shift we rotate the guide
252 drag_type = SP_DRAG_ROTATE;
253 } else {
254 if (event->button.state & GDK_CONTROL_MASK) {
255 drag_type = SP_DRAG_MOVE_ORIGIN;
256 } else {
257 drag_type = SP_DRAG_TRANSLATE;
258 }
259 }
261 if (drag_type == SP_DRAG_ROTATE || drag_type == SP_DRAG_TRANSLATE) {
262 sp_canvas_item_grab(item,
263 ( GDK_BUTTON_RELEASE_MASK |
264 GDK_BUTTON_PRESS_MASK |
265 GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
266 NULL,
267 event->button.time);
268 }
269 ret = TRUE;
270 }
271 break;
272 case GDK_MOTION_NOTIFY:
273 if (drag_type != SP_DRAG_NONE) {
274 Geom::Point const motion_w(event->motion.x,
275 event->motion.y);
276 Geom::Point motion_dt(desktop->w2d(motion_w));
278 sp_event_context_snap_delay_handler(desktop->event_context, (gpointer) item, data, (GdkEventMotion *)event, DelayedSnapEvent::GUIDE_HANDLER);
280 // This is for snapping while dragging existing guidelines. New guidelines,
281 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
282 SnapManager &m = desktop->namedview->snap_manager;
283 m.setup(desktop, true, NULL, NULL, guide);
284 if (drag_type == SP_DRAG_MOVE_ORIGIN) {
285 // If we snap in guideConstrainedSnap() below, then motion_dt will
286 // be forced to be on the guide. If we don't snap however, then
287 // the origin should still be constrained to the guide. So let's do
288 // that explicitly first:
290 Geom::Line line(guide->point_on_line, guide->angle());
291 Geom::Coord t = line.nearestPoint(motion_dt);
292 motion_dt = line.pointAt(t);
293 m.guideConstrainedSnap(motion_dt, *guide);
294 } else {
295 m.guideFreeSnap(motion_dt, guide->normal_to_line, drag_type);
296 }
298 switch (drag_type) {
299 case SP_DRAG_TRANSLATE:
300 {
301 sp_guide_moveto(*guide, motion_dt, false);
302 break;
303 }
304 case SP_DRAG_ROTATE:
305 {
306 Geom::Point pt = motion_dt - guide->point_on_line;
307 double angle = std::atan2(pt[Geom::Y], pt[Geom::X]);
308 if (event->motion.state & GDK_CONTROL_MASK) {
309 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
310 unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12));
311 if (snaps) {
312 double sections = floor(angle * snaps / M_PI + .5);
313 angle = (M_PI / snaps) * sections;
314 }
315 }
316 sp_guide_set_normal(*guide, Geom::Point(1,0) * Geom::Rotate(angle + M_PI_2), false);
317 break;
318 }
319 case SP_DRAG_MOVE_ORIGIN:
320 {
321 sp_guide_moveto(*guide, motion_dt, false);
322 break;
323 }
324 case SP_DRAG_NONE:
325 g_assert_not_reached();
326 break;
327 }
328 moved = true;
329 desktop->set_coordinate_status(from_2geom(motion_dt));
330 desktop->setPosition(from_2geom(motion_dt));
332 ret = TRUE;
333 }
334 break;
335 case GDK_BUTTON_RELEASE:
336 if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
337 sp_event_context_discard_delayed_snap_event(desktop->event_context);
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_canvas_item_ungrab(item, event->button.time);
406 ret=TRUE;
407 }
408 case GDK_ENTER_NOTIFY:
409 {
410 sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
412 // set move or rotate cursor
413 Geom::Point const event_w(event->crossing.x, event->crossing.y);
414 Geom::Point const event_dt(desktop->w2d(event_w));
416 if (event->crossing.state & GDK_SHIFT_MASK) {
417 GdkCursor *guide_cursor;
418 guide_cursor = gdk_cursor_new (GDK_EXCHANGE);
419 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, guide_cursor);
420 gdk_cursor_unref(guide_cursor);
421 }
423 char *guide_description = sp_guide_description(guide);
424 desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
425 g_free(guide_description);
426 break;
427 }
428 case GDK_LEAVE_NOTIFY:
429 sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
431 // restore event context's cursor
432 gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, desktop->event_context->cursor);
434 desktop->guidesMessageContext()->clear();
435 break;
436 case GDK_KEY_PRESS:
437 switch (get_group0_keyval (&event->key)) {
438 case GDK_Delete:
439 case GDK_KP_Delete:
440 case GDK_BackSpace:
441 {
442 SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
443 sp_guide_remove(guide);
444 sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
445 ret = TRUE;
446 sp_event_context_discard_delayed_snap_event(desktop->event_context);
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;
479 }
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()
487 {
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 }
517 }
520 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
521 {
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 }
594 }
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 :