Code

inkscape.pot update
[inkscape.git] / src / desktop-events.cpp
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;
218 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
220     return sp_dt_ruler_event(widget, event, dtw, true);
223 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
225     return sp_dt_ruler_event(widget, event, dtw, false);
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 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
242     static bool moved = false;
243     gint ret = FALSE;
245     SPGuide *guide = SP_GUIDE(data);
246     SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
248     switch (event->type) {
249         case GDK_2BUTTON_PRESS:
250             if (event->button.button == 1) {
251                 drag_type = SP_DRAG_NONE;
252                 sp_event_context_snap_window_closed(desktop->event_context);
253                 sp_canvas_item_ungrab(item, event->button.time);
254                 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
255                 ret = TRUE;
256             }
257             break;
258         case GDK_BUTTON_PRESS:
259             if (event->button.button == 1) {
260                 if (event->button.state & GDK_CONTROL_MASK) {
261                     SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
262                     sp_guide_remove(guide);
263                     sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
264                     ret = TRUE;
265                     break;
266                 }
268                 sp_event_context_snap_window_open(desktop->event_context);
269                 double tol = 40.0; // Measured in screenpixels
270                 Geom::Point const event_w(event->button.x, event->button.y);
271                 Geom::Point const event_dt(desktop->w2d(event_w));
273                 // Due to the tolerance allowed when grabbing a guide, event_dt will generally
274                 // be close to the guide but not just exactly on it. The drag origin calculated
275                 // here must be exactly on the guide line though, otherwise
276                 // small errors will occur once we snap, see
277                 // https://bugs.launchpad.net/inkscape/+bug/333762
278                 drag_origin = Geom::projection(event_dt, Geom::Line(guide->point_on_line, guide->angle()));
280                 if (Geom::L2(guide->point_on_line - event_dt) < tol/desktop->current_zoom()) {
281                     // the click was on the guide 'anchor'
282                     drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_MOVE_ORIGIN : SP_DRAG_TRANSLATE;
283                 } else {
284                     drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_ROTATE : SP_DRAG_TRANSLATE;
285                     sp_canvas_item_grab(item,
286                                         ( GDK_BUTTON_RELEASE_MASK  |
287                                           GDK_BUTTON_PRESS_MASK    |
288                                           GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
289                                         NULL,
290                                         event->button.time);
291                 }
292                 ret = TRUE;
293             }
294             break;
295         case GDK_MOTION_NOTIFY:
296             if (drag_type != SP_DRAG_NONE) {
297                 Geom::Point const motion_w(event->motion.x,
298                                            event->motion.y);
299                 Geom::Point motion_dt(desktop->w2d(motion_w));
301                 // This is for snapping while dragging existing guidelines. New guidelines,
302                 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
303                 SnapManager &m = desktop->namedview->snap_manager;
304                 m.setup(desktop, true, NULL, NULL, guide);
305                 if (drag_type == SP_DRAG_MOVE_ORIGIN) {
306                         // If we snap in guideConstrainedSnap() below, then motion_dt will be forced to be on the guide
307                                         // If we don't snap however, then it the origin should still be constrained to the guide
308                                         // So let's do that explicitly first:
309                         Geom::Line line(guide->point_on_line, guide->angle());
310                                         Geom::Coord t = line.nearestPoint(motion_dt);
311                                         motion_dt = line.pointAt(t);
312                         m.guideConstrainedSnap(motion_dt, *guide);
313                 } else {
314                                         m.guideFreeSnap(motion_dt, guide->normal_to_line);
315                 }
317                 switch (drag_type) {
318                     case SP_DRAG_TRANSLATE:
319                     {
320                         sp_guide_moveto(*guide, guide->point_on_line + motion_dt - drag_origin, false);
321                         break;
322                     }
323                     case SP_DRAG_TRANSLATE_CONSTRAINED: // Is not being used currently!
324                     {
325                         Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, motion_dt);
326                         sp_guide_moveto(*guide, pt_constr, false);
327                         break;
328                     }
329                     case SP_DRAG_ROTATE:
330                     {
331                         double angle = angle_between(drag_origin - guide->point_on_line, motion_dt - guide->point_on_line);
332                         sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), false);
333                         break;
334                     }
335                     case SP_DRAG_MOVE_ORIGIN:
336                     {
337                         sp_guide_moveto(*guide, motion_dt, false);
338                         break;
339                     }
340                     case SP_DRAG_NONE:
341                         g_assert_not_reached();
342                         break;
343                 }
344                 moved = true;
345                 desktop->set_coordinate_status(from_2geom(motion_dt));
346                 desktop->setPosition(from_2geom(motion_dt));
348                 ret = TRUE;
349             }
350             break;
351     case GDK_BUTTON_RELEASE:
352             if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
353                 if (moved) {
354                     Geom::Point const event_w(event->button.x,
355                                               event->button.y);
356                     Geom::Point event_dt(desktop->w2d(event_w));
358                     SnapManager &m = desktop->namedview->snap_manager;
359                     m.setup(desktop, true, NULL, NULL, guide);
360                     if (drag_type == SP_DRAG_MOVE_ORIGIN) {
361                         // If we snap in guideConstrainedSnap() below, then motion_dt will be forced to be on the guide
362                         // If we don't snap however, then it the origin should still be constrained to the guide
363                         // So let's do that explicitly first:
364                         Geom::Line line(guide->point_on_line, guide->angle());
365                                                 Geom::Coord t = line.nearestPoint(event_dt);
366                                                 event_dt = line.pointAt(t);
367                         m.guideConstrainedSnap(event_dt, *guide);
368                                         } else {
369                                                 m.guideFreeSnap(event_dt, guide->normal_to_line);
370                                         }
372                     if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
373                         switch (drag_type) {
374                             case SP_DRAG_TRANSLATE:
375                             {
376                                 sp_guide_moveto(*guide, guide->point_on_line + event_dt - drag_origin, true);
377                                 break;
378                             }
379                             case SP_DRAG_TRANSLATE_CONSTRAINED: // Is not being used currently!
380                             {
381                                 Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, event_dt);
382                                 sp_guide_moveto(*guide, pt_constr, true);
383                                 break;
384                             }
385                             case SP_DRAG_ROTATE:
386                             {
387                                 double angle = angle_between(drag_origin - guide->point_on_line, event_dt - guide->point_on_line);
388                                 sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), true);
389                                 break;
390                             }
391                             case SP_DRAG_MOVE_ORIGIN:
392                             {
393                                 sp_guide_moveto(*guide, event_dt, true);
394                                 break;
395                             }
396                             case SP_DRAG_NONE:
397                                 g_assert_not_reached();
398                                 break;
399                         }
400                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
401                                          _("Move guide"));
402                     } else {
403                         /* Undo movement of any attached shapes. */
404                         sp_guide_moveto(*guide, guide->point_on_line, false);
405                         sp_guide_set_normal(*guide, guide->normal_to_line, false);
406                         sp_guide_remove(guide);
407                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
408                                      _("Delete guide"));
409                     }
410                     moved = false;
411                     desktop->set_coordinate_status(from_2geom(event_dt));
412                     desktop->setPosition (from_2geom(event_dt));
413                 }
414                 drag_type = SP_DRAG_NONE;
415                 sp_event_context_snap_window_closed(desktop->event_context);
416                 sp_canvas_item_ungrab(item, event->button.time);
417                 ret=TRUE;
418             }
419     case GDK_ENTER_NOTIFY:
420     {
421             sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
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);
430             desktop->guidesMessageContext()->clear();
431             break;
432     default:
433             break;
434     }
436     return ret;
439 //static std::map<GdkInputSource, std::string> switchMap;
440 static std::map<std::string, int> toolToUse;
441 static std::string lastName;
442 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
444 static void init_extended()
446     std::string avoidName = "pad";
447     GList* devices = gdk_devices_list();
448     if ( devices ) {
449         for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
450             GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
451             if ( dev->name
452                  && (avoidName != dev->name)
453                  && (dev->source != GDK_SOURCE_MOUSE) ) {
454 //                 g_message("Adding '%s' as [%d]", dev->name, dev->source);
456                 // Set the initial tool for the device
457                 switch ( dev->source ) {
458                     case GDK_SOURCE_PEN:
459                         toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
460                         break;
461                     case GDK_SOURCE_ERASER:
462                         toolToUse[dev->name] = TOOLS_ERASER;
463                         break;
464                     case GDK_SOURCE_CURSOR:
465                         toolToUse[dev->name] = TOOLS_SELECT;
466                         break;
467                     default:
468                         ; // do not add
469                 }
470 //            } else if (dev->name) {
471 //                 g_message("Skippn '%s' as [%s]", dev->name, dev->source);
472             }
473         }
474     }
478 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
480     GdkInputSource source = GDK_SOURCE_MOUSE;
481     std::string name;
483     switch ( event->type ) {
484         case GDK_MOTION_NOTIFY:
485         {
486             GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
487             if ( event2->device ) {
488                 source = event2->device->source;
489                 name = event2->device->name;
490             }
491         }
492         break;
494         case GDK_BUTTON_PRESS:
495         case GDK_2BUTTON_PRESS:
496         case GDK_3BUTTON_PRESS:
497         case GDK_BUTTON_RELEASE:
498         {
499             GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
500             if ( event2->device ) {
501                 source = event2->device->source;
502                 name = event2->device->name;
503             }
504         }
505         break;
507         case GDK_SCROLL:
508         {
509             GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
510             if ( event2->device ) {
511                 source = event2->device->source;
512                 name = event2->device->name;
513             }
514         }
515         break;
517         case GDK_PROXIMITY_IN:
518         case GDK_PROXIMITY_OUT:
519         {
520             GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
521             if ( event2->device ) {
522                 source = event2->device->source;
523                 name = event2->device->name;
524             }
525         }
526         break;
528         default:
529             ;
530     }
532     if (!name.empty()) {
533         if ( lastType != source || lastName != name ) {
534             // The device switched. See if it is one we 'count'
535             //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
536             std::map<std::string, int>::iterator it = toolToUse.find(lastName);
537             if (it != toolToUse.end()) {
538                 // Save the tool currently selected for next time the input
539                 // device shows up.
540                 it->second = tools_active(desktop);
541             }
543             it = toolToUse.find(name);
544             if (it != toolToUse.end() ) {
545                 tools_switch(desktop, it->second);
546             }
548             lastName = name;
549             lastType = source;
550         }
551     }
556 /*
557   Local Variables:
558   mode:c++
559   c-file-style:"stroustrup"
560   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
561   indent-tabs-mode:nil
562   fill-column:99
563   End:
564 */
565 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :