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