3d8e88fba7ea1c3422bfac0d51c7808732fb4d6a
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.guideSnap(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.guideSnap(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 */
230 static Geom::Point drag_origin;
232 enum SPGuideDragType {
233 SP_DRAG_TRANSLATE,
234 SP_DRAG_TRANSLATE_CONSTRAINED,
235 SP_DRAG_ROTATE,
236 SP_DRAG_MOVE_ORIGIN,
237 SP_DRAG_NONE
238 };
240 static SPGuideDragType drag_type = SP_DRAG_NONE;
242 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
243 {
244 static bool moved = false;
245 gint ret = FALSE;
247 SPGuide *guide = SP_GUIDE(data);
248 SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
250 switch (event->type) {
251 case GDK_2BUTTON_PRESS:
252 if (event->button.button == 1) {
253 drag_type = SP_DRAG_NONE;
254 sp_event_context_snap_window_closed(desktop->event_context);
255 sp_canvas_item_ungrab(item, event->button.time);
256 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
257 ret = TRUE;
258 }
259 break;
260 case GDK_BUTTON_PRESS:
261 if (event->button.button == 1) {
262 if (event->button.state & GDK_CONTROL_MASK) {
263 SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
264 sp_guide_remove(guide);
265 sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
266 ret = TRUE;
267 break;
268 }
270 sp_event_context_snap_window_open(desktop->event_context);
271 double tol = 40.0; // Measured in screenpixels
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 m.guideSnap(motion_dt, to_2geom(guide->normal_to_line));
309 switch (drag_type) {
310 case SP_DRAG_TRANSLATE:
311 {
312 sp_guide_moveto(*guide, guide->point_on_line + motion_dt - drag_origin, false);
313 break;
314 }
315 case SP_DRAG_TRANSLATE_CONSTRAINED:
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 double angle = angle_between(drag_origin - guide->point_on_line, motion_dt - guide->point_on_line);
324 sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), false);
325 break;
326 }
327 case SP_DRAG_MOVE_ORIGIN:
328 {
329 Geom::Line line(guide->point_on_line, guide->angle());
330 Geom::Coord t = line.nearestPoint(motion_dt);
331 sp_guide_moveto(*guide, line.pointAt(t), 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 m.guideSnap(event_dt, guide->normal_to_line);
356 if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
357 switch (drag_type) {
358 case SP_DRAG_TRANSLATE:
359 {
360 sp_guide_moveto(*guide, guide->point_on_line + event_dt - drag_origin, true);
361 break;
362 }
363 case SP_DRAG_TRANSLATE_CONSTRAINED:
364 {
365 Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, event_dt);
366 sp_guide_moveto(*guide, pt_constr, true);
367 break;
368 }
369 case SP_DRAG_ROTATE:
370 {
371 double angle = angle_between(drag_origin - guide->point_on_line, event_dt - guide->point_on_line);
372 sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), true);
373 break;
374 }
375 case SP_DRAG_MOVE_ORIGIN:
376 {
377 Geom::Line line(guide->point_on_line, guide->angle());
378 Geom::Coord t = line.nearestPoint(event_dt);
379 sp_guide_moveto(*guide, line.pointAt(t), true);
380 break;
381 }
382 case SP_DRAG_NONE:
383 g_assert_not_reached();
384 break;
385 }
386 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
387 _("Move guide"));
388 } else {
389 /* Undo movement of any attached shapes. */
390 sp_guide_moveto(*guide, guide->point_on_line, false);
391 sp_guide_set_normal(*guide, guide->normal_to_line, false);
392 sp_guide_remove(guide);
393 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
394 _("Delete guide"));
395 }
396 moved = false;
397 desktop->set_coordinate_status(from_2geom(event_dt));
398 desktop->setPosition (from_2geom(event_dt));
399 }
400 drag_type = SP_DRAG_NONE;
401 sp_event_context_snap_window_closed(desktop->event_context);
402 sp_canvas_item_ungrab(item, event->button.time);
403 ret=TRUE;
404 }
405 case GDK_ENTER_NOTIFY:
406 {
407 sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
409 char *guide_description = sp_guide_description(guide);
410 desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
411 g_free(guide_description);
412 break;
413 }
414 case GDK_LEAVE_NOTIFY:
415 sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
416 desktop->guidesMessageContext()->clear();
417 break;
418 default:
419 break;
420 }
422 return ret;
423 }
425 //static std::map<GdkInputSource, std::string> switchMap;
426 static std::map<std::string, int> toolToUse;
427 static std::string lastName;
428 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
430 static void init_extended()
431 {
432 std::string avoidName = "pad";
433 GList* devices = gdk_devices_list();
434 if ( devices ) {
435 for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
436 GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
437 if ( dev->name
438 && (avoidName != dev->name)
439 && (dev->source != GDK_SOURCE_MOUSE) ) {
440 // g_message("Adding '%s' as [%d]", dev->name, dev->source);
442 // Set the initial tool for the device
443 switch ( dev->source ) {
444 case GDK_SOURCE_PEN:
445 toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
446 break;
447 case GDK_SOURCE_ERASER:
448 toolToUse[dev->name] = TOOLS_ERASER;
449 break;
450 case GDK_SOURCE_CURSOR:
451 toolToUse[dev->name] = TOOLS_SELECT;
452 break;
453 default:
454 ; // do not add
455 }
456 // } else if (dev->name) {
457 // g_message("Skippn '%s' as [%s]", dev->name, dev->source);
458 }
459 }
460 }
461 }
464 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
465 {
466 GdkInputSource source = GDK_SOURCE_MOUSE;
467 std::string name;
469 switch ( event->type ) {
470 case GDK_MOTION_NOTIFY:
471 {
472 GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
473 if ( event2->device ) {
474 source = event2->device->source;
475 name = event2->device->name;
476 }
477 }
478 break;
480 case GDK_BUTTON_PRESS:
481 case GDK_2BUTTON_PRESS:
482 case GDK_3BUTTON_PRESS:
483 case GDK_BUTTON_RELEASE:
484 {
485 GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
486 if ( event2->device ) {
487 source = event2->device->source;
488 name = event2->device->name;
489 }
490 }
491 break;
493 case GDK_SCROLL:
494 {
495 GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
496 if ( event2->device ) {
497 source = event2->device->source;
498 name = event2->device->name;
499 }
500 }
501 break;
503 case GDK_PROXIMITY_IN:
504 case GDK_PROXIMITY_OUT:
505 {
506 GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
507 if ( event2->device ) {
508 source = event2->device->source;
509 name = event2->device->name;
510 }
511 }
512 break;
514 default:
515 ;
516 }
518 if (!name.empty()) {
519 if ( lastType != source || lastName != name ) {
520 // The device switched. See if it is one we 'count'
521 //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
522 std::map<std::string, int>::iterator it = toolToUse.find(lastName);
523 if (it != toolToUse.end()) {
524 // Save the tool currently selected for next time the input
525 // device shows up.
526 it->second = tools_active(desktop);
527 }
529 it = toolToUse.find(name);
530 if (it != toolToUse.end() ) {
531 tools_switch(desktop, it->second);
532 }
534 lastName = name;
535 lastType = source;
536 }
537 }
538 }
542 /*
543 Local Variables:
544 mode:c++
545 c-file-style:"stroustrup"
546 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
547 indent-tabs-mode:nil
548 fill-column:99
549 End:
550 */
551 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :