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