Code

a947e6f17a964fa94c29bf02802d2bca9c484976
[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     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                 // FIXME: The snap delay mechanism won't work here, because it has been implemented for the canvas. Dragging
92                 // guides off the ruler will send event to the ruler and not to the canvas, which bypasses sp_canvas_motion
93                 // The snap manager will not notice the difference, so it'll check if the snap delay has been activated (This check
94                 // is only needed for catching coding errors, i.e. to warn if the snap delay has not been implemented properly
95                 // in some context)
96                 if (desktop->canvas->context_snap_delay_active == false) {
97                                         // A dt_ruler_event might be emitted when dragging a guide of the rulers while drawing a Bezier curve
98                         // In such a situation, we're already in that specific context and the snap delay is already active. We should
99                         // not set the snap delay to active again, because that will trigger a similar warning to the one above
100                         sp_canvas_set_snap_delay_active(desktop->canvas, true);
101                 }
103                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
104                 Geom::Point const event_dt(desktop->w2d(event_w));
106                 // explicitly show guidelines; if I draw a guide, I want them on
107                 sp_repr_set_boolean(repr, "showguides", TRUE);
108                 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
110                 // calculate the normal of the guidelines when dragged from the edges of rulers.
111                 Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
112                 Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
113                 normal_bl_to_tr.normalize();
114                 normal_tr_to_bl.normalize();
115                 Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
116                 if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
117                     Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
118                     if (event->button.state & GDK_CONTROL_MASK) {
119                         // guidelines normal to gridlines
120                         normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
121                         normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
122                     } else {
123                         normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
124                         normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
125                     }
126                 }
127                 if (horiz) {
128                     if (wx < 50) {
129                         normal = normal_bl_to_tr;
130                     } else if (wx > width - 50) {
131                         normal = normal_tr_to_bl;
132                     } else {
133                         normal = Geom::Point(0.,1.);
134                     }
135                 } else {
136                     if (wy < 50) {
137                         normal = normal_bl_to_tr;
138                     } else if (wy > height - 50) {
139                         normal = normal_tr_to_bl;
140                     } else {
141                         normal = Geom::Point(1.,0.);
142                     }
143                 }
145                 guide = sp_guideline_new(desktop->guides, event_dt, normal);
146                 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
147                 gdk_pointer_grab(widget->window, FALSE,
148                                  (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
149                                  NULL, NULL,
150                                  event->button.time);
151             }
152             break;
153     case GDK_MOTION_NOTIFY:
154             if (dragging) {
155                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
156                 Geom::Point event_dt(desktop->w2d(event_w));
158                 SnapManager &m = desktop->namedview->snap_manager;
159                 m.setup(desktop);
160                 // We only have a temporary guide which is not stored in our document yet. Because the guide snapper only looks
161                 // in the document for guides to snap to, we don't have to worry about a guide snapping to itself here
162                 m.guideSnap(event_dt, normal);
164                 sp_guideline_set_position(SP_GUIDELINE(guide), from_2geom(event_dt));
165                 desktop->set_coordinate_status(to_2geom(event_dt));
166                 desktop->setPosition(to_2geom(event_dt));
167             }
168             break;
169     case GDK_BUTTON_RELEASE:
170             if (dragging && event->button.button == 1) {
171                 gdk_pointer_ungrab(event->button.time);
172                 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
173                 Geom::Point event_dt(desktop->w2d(event_w));
175                 SnapManager &m = desktop->namedview->snap_manager;
176                 m.setup(desktop);
177                 // We only have a temporary guide which is not stored in our document yet. Because the guide snapper only looks
178                                 // in the document for guides to snap to, we don't have to worry about a guide snapping to itself here
179                 m.guideSnap(event_dt, normal);
181                 dragging = false;
182                 gtk_object_destroy(GTK_OBJECT(guide));
183                 guide = NULL;
184                 if ((horiz ? wy : wx) >= 0) {
185                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
186                     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
187                     sp_repr_set_point(repr, "orientation", normal);
188                     sp_repr_set_point(repr, "position", from_2geom(event_dt));
189                     SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
190                     Inkscape::GC::release(repr);
191                     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
192                                      _("Create guide"));
193                 }
194                 desktop->set_coordinate_status(from_2geom(event_dt));
196                 // A dt_ruler_event might be emitted when dragging a guide of the rulers while drawing a Bezier curve
197                                 // In such a situation, we're already in that specific context and the snap delay is already active. We should
198                 // interfere with that context and we should therefore leave the snap delay status as it is. So although it might
199                 // have been set to active above on GDK_BUTTON_PRESS, we should not set it back to inactive here. That must be
200                 // done by the context
201             }
202         default:
203             break;
204     }
206     return FALSE;
209 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
211     return sp_dt_ruler_event(widget, event, dtw, true);
214 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
216     return sp_dt_ruler_event(widget, event, dtw, false);
219 /* Guides */
221 static Geom::Point drag_origin;
223 enum SPGuideDragType {
224     SP_DRAG_TRANSLATE,
225     SP_DRAG_TRANSLATE_CONSTRAINED,
226     SP_DRAG_ROTATE,
227     SP_DRAG_MOVE_ORIGIN,
228     SP_DRAG_NONE
229 };
231 static SPGuideDragType drag_type = SP_DRAG_NONE;
233 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
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_canvas_set_snap_delay_active(desktop->canvas, false);
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                 if (event->button.state & GDK_CONTROL_MASK) {
254                     SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
255                     sp_guide_remove(guide);
256                     sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
257                     ret = TRUE;
258                     break;
259                 }
261                 sp_canvas_set_snap_delay_active(desktop->canvas, true);
262                 double tol = 40.0;
263                 Geom::Point const event_w(event->button.x, event->button.y);
264                 Geom::Point const event_dt(desktop->w2d(event_w));
265                 drag_origin = event_dt;
266                 if (Geom::L2(guide->point_on_line - event_dt) < tol) {
267                     // the click was on the guide 'anchor'
268                     drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_MOVE_ORIGIN : SP_DRAG_TRANSLATE;
269                 } else {
270                     drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_ROTATE : SP_DRAG_TRANSLATE;
271                     sp_canvas_item_grab(item,
272                                         ( GDK_BUTTON_RELEASE_MASK  |
273                                           GDK_BUTTON_PRESS_MASK    |
274                                           GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
275                                         NULL,
276                                         event->button.time);
277                 }
278                 ret = TRUE;
279             }
280             break;
281         case GDK_MOTION_NOTIFY:
282             if (drag_type != SP_DRAG_NONE) {
283                 Geom::Point const motion_w(event->motion.x,
284                                            event->motion.y);
285                 Geom::Point motion_dt(desktop->w2d(motion_w));
287                 // This is for snapping while dragging existing guidelines. New guidelines,
288                 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
289                 SnapManager &m = desktop->namedview->snap_manager;
290                 m.setup(desktop, true, NULL, NULL, guide);
291                 m.guideSnap(motion_dt, to_2geom(guide->normal_to_line));
293                 switch (drag_type) {
294                     case SP_DRAG_TRANSLATE:
295                     {
296                         sp_guide_moveto(*guide, guide->point_on_line + motion_dt - drag_origin, false);
297                         break;
298                     }
299                     case SP_DRAG_TRANSLATE_CONSTRAINED:
300                     {
301                         Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, motion_dt);
302                         sp_guide_moveto(*guide, pt_constr, false);
303                         break;
304                     }
305                     case SP_DRAG_ROTATE:
306                     {
307                         double angle = angle_between(drag_origin - guide->point_on_line, motion_dt - guide->point_on_line);
308                         sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), false);
309                         break;
310                     }
311                     case SP_DRAG_MOVE_ORIGIN:
312                     {
313                         Geom::Line line(guide->point_on_line, guide->angle());
314                         Geom::Coord t = line.nearestPoint(motion_dt);
315                         sp_guide_moveto(*guide, line.pointAt(t), false);
316                         break;
317                     }
318                     case SP_DRAG_NONE:
319                         g_assert_not_reached();
320                         break;
321                 }
322                 moved = true;
323                 desktop->set_coordinate_status(from_2geom(motion_dt));
324                 desktop->setPosition(from_2geom(motion_dt));
326                 ret = TRUE;
327             }
328             break;
329     case GDK_BUTTON_RELEASE:
330             if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
331                 if (moved) {
332                     Geom::Point const event_w(event->button.x,
333                                               event->button.y);
334                     Geom::Point event_dt(desktop->w2d(event_w));
336                     SnapManager &m = desktop->namedview->snap_manager;
337                     m.setup(desktop, true, NULL, NULL, guide);
338                                         m.guideSnap(event_dt, guide->normal_to_line);
340                     if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
341                         switch (drag_type) {
342                             case SP_DRAG_TRANSLATE:
343                             {
344                                 sp_guide_moveto(*guide, guide->point_on_line + event_dt - drag_origin, true);
345                                 break;
346                             }
347                             case SP_DRAG_TRANSLATE_CONSTRAINED:
348                             {
349                                 Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, event_dt);
350                                 sp_guide_moveto(*guide, pt_constr, true);
351                                 break;
352                             }
353                             case SP_DRAG_ROTATE:
354                             {
355                                 double angle = angle_between(drag_origin - guide->point_on_line, event_dt - guide->point_on_line);
356                                 sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), true);
357                                 break;
358                             }
359                             case SP_DRAG_MOVE_ORIGIN:
360                             {
361                                 Geom::Line line(guide->point_on_line, guide->angle());
362                                 Geom::Coord t = line.nearestPoint(event_dt);
363                                 sp_guide_moveto(*guide, line.pointAt(t), true);
364                                 break;
365                             }
366                             case SP_DRAG_NONE:
367                                 g_assert_not_reached();
368                                 break;
369                         }
370                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
371                                          _("Move guide"));
372                     } else {
373                         /* Undo movement of any attached shapes. */
374                         sp_guide_moveto(*guide, guide->point_on_line, false);
375                         sp_guide_set_normal(*guide, guide->normal_to_line, false);
376                         sp_guide_remove(guide);
377                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
378                                      _("Delete guide"));
379                     }
380                     moved = false;
381                     desktop->set_coordinate_status(from_2geom(event_dt));
382                     desktop->setPosition (from_2geom(event_dt));
383                 }
384                 drag_type = SP_DRAG_NONE;
385                 sp_canvas_set_snap_delay_active(desktop->canvas, false);
386                 sp_canvas_item_ungrab(item, event->button.time);
387                 ret=TRUE;
388             }
389     case GDK_ENTER_NOTIFY:
390     {
391             sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
393             char *guide_description = sp_guide_description(guide);
394             desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
395             g_free(guide_description);
396             break;
397     }
398     case GDK_LEAVE_NOTIFY:
399             sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
400             desktop->guidesMessageContext()->clear();
401             break;
402     default:
403             break;
404     }
406     return ret;
409 //static std::map<GdkInputSource, std::string> switchMap;
410 static std::map<std::string, int> toolToUse;
411 static std::string lastName;
412 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
414 static void init_extended()
416     std::string avoidName = "pad";
417     GList* devices = gdk_devices_list();
418     if ( devices ) {
419         for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
420             GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
421             if ( dev->name
422                  && (avoidName != dev->name)
423                  && (dev->source != GDK_SOURCE_MOUSE) ) {
424 //                 g_message("Adding '%s' as [%d]", dev->name, dev->source);
426                 // Set the initial tool for the device
427                 switch ( dev->source ) {
428                     case GDK_SOURCE_PEN:
429                         toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
430                         break;
431                     case GDK_SOURCE_ERASER:
432                         toolToUse[dev->name] = TOOLS_ERASER;
433                         break;
434                     case GDK_SOURCE_CURSOR:
435                         toolToUse[dev->name] = TOOLS_SELECT;
436                         break;
437                     default:
438                         ; // do not add
439                 }
440 //            } else if (dev->name) {
441 //                 g_message("Skippn '%s' as [%s]", dev->name, dev->source);
442             }
443         }
444     }
448 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
450     GdkInputSource source = GDK_SOURCE_MOUSE;
451     std::string name;
453     switch ( event->type ) {
454         case GDK_MOTION_NOTIFY:
455         {
456             GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
457             if ( event2->device ) {
458                 source = event2->device->source;
459                 name = event2->device->name;
460             }
461         }
462         break;
464         case GDK_BUTTON_PRESS:
465         case GDK_2BUTTON_PRESS:
466         case GDK_3BUTTON_PRESS:
467         case GDK_BUTTON_RELEASE:
468         {
469             GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
470             if ( event2->device ) {
471                 source = event2->device->source;
472                 name = event2->device->name;
473             }
474         }
475         break;
477         case GDK_SCROLL:
478         {
479             GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
480             if ( event2->device ) {
481                 source = event2->device->source;
482                 name = event2->device->name;
483             }
484         }
485         break;
487         case GDK_PROXIMITY_IN:
488         case GDK_PROXIMITY_OUT:
489         {
490             GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
491             if ( event2->device ) {
492                 source = event2->device->source;
493                 name = event2->device->name;
494             }
495         }
496         break;
498         default:
499             ;
500     }
502     if (!name.empty()) {
503         if ( lastType != source || lastName != name ) {
504             // The device switched. See if it is one we 'count'
505             //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
506             std::map<std::string, int>::iterator it = toolToUse.find(lastName);
507             if (it != toolToUse.end()) {
508                 // Save the tool currently selected for next time the input
509                 // device shows up.
510                 it->second = tools_active(desktop);
511             }
513             it = toolToUse.find(name);
514             if (it != toolToUse.end() ) {
515                 tools_switch(desktop, it->second);
516             }
518             lastName = name;
519             lastType = source;
520         }
521     }
526 /*
527   Local Variables:
528   mode:c++
529   c-file-style:"stroustrup"
530   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
531   indent-tabs-mode:nil
532   fill-column:99
533   End:
534 */
535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :