Code

Remove double unreffing of pointer in pdf-cairo.cpp. Fixes Bug #178985 with the...
[inkscape.git] / src / desktop-events.cpp
1 #define __SP_DESKTOP_EVENTS_C__
3 /*
4  * Event handlers for SPDesktop
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 1999-2002 Lauris Kaplinski
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include <map>
18 #include <string>
19 #include "display/guideline.h"
20 #include "helper/unit-menu.h"
21 #include "helper/units.h"
22 #include "desktop.h"
23 #include "document.h"
24 #include "sp-guide.h"
25 #include "sp-namedview.h"
26 #include "desktop-handles.h"
27 #include "event-context.h"
28 #include "widgets/desktop-widget.h"
29 #include "sp-metrics.h"
30 #include <glibmm/i18n.h>
31 #include "dialogs/dialog-events.h"
32 #include "message-context.h"
33 #include "xml/repr.h"
34 #include "dialogs/guidelinedialog.h"
35 #include "snap.h"
36 #include "display/canvas-grid.h"
37 #include "display/canvas-axonomgrid.h"
38 #include "prefs-utils.h"
39 #include "helper/action.h"
40 #include "tools-switch.h"
41 #include <2geom/point.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         if ( prefs_get_int_attribute("options.useextinput", "value", 1)
55             && prefs_get_int_attribute("options.switchonextinput", "value", 0) ) {
56             watch = true;
57             init_extended();
58         }
59         first = false;
60     }
61     if ( watch ) {
62         snoop_extended(event, desktop);
63     }
65     return sp_event_context_root_handler(desktop->event_context, event);
66 }
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     NR::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;
89                 NR::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
90                 NR::Point const event_dt(desktop->w2d(event_w));
92                 // explicitly show guidelines; if I draw a guide, I want them on
93                 sp_repr_set_boolean(repr, "showguides", TRUE);
94                 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
96                 // calculate the normal of the guidelines when dragged from the edges of rulers.
97                 Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
98                 Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
99                 normal_bl_to_tr.normalize();
100                 normal_tr_to_bl.normalize();
101                 Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
102                 if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
103                     Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
104                     if (event->button.state & GDK_CONTROL_MASK) {
105                         // guidelines normal to gridlines
106                         normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
107                         normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
108                     } else {
109                         normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
110                         normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
111                     }
112                 }
113                 if (horiz) {
114                     if (wx < 50) {
115                         normal = normal_bl_to_tr;
116                     } else if (wx > width - 50) {
117                         normal = normal_tr_to_bl;
118                     } else {
119                         normal = Geom::Point(0.,1.);
120                     }
121                 } else {
122                     if (wy < 50) {
123                         normal = normal_bl_to_tr;
124                     } else if (wy > height - 50) {
125                         normal = normal_tr_to_bl;
126                     } else {
127                         normal = Geom::Point(1.,0.);
128                     }
129                 }
131                 guide = sp_guideline_new(desktop->guides, event_dt.to_2geom(), normal);
132                 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
133                 gdk_pointer_grab(widget->window, FALSE,
134                                  (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
135                                  NULL, NULL,
136                                  event->button.time);
137             }
138             break;
139     case GDK_MOTION_NOTIFY:
140             if (dragging) {
141                 NR::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
142                 NR::Point event_dt(desktop->w2d(event_w));
143                 
144                 SnapManager const &m = desktop->namedview->snap_manager;
145                 event_dt = m.guideSnap(event_dt, normal).getPoint();
146                 
147                 sp_guideline_set_position(SP_GUIDELINE(guide), event_dt.to_2geom());
148                 desktop->set_coordinate_status(event_dt);
149                 desktop->setPosition (event_dt);
150             }
151             break;
152     case GDK_BUTTON_RELEASE:
153             if (dragging && event->button.button == 1) {
154                 gdk_pointer_ungrab(event->button.time);
155                 NR::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
156                 NR::Point event_dt(desktop->w2d(event_w));
157                 
158                 SnapManager const &m = desktop->namedview->snap_manager;
159                 event_dt = m.guideSnap(event_dt, normal).getPoint();
160                                 
161                 dragging = false;
162                 gtk_object_destroy(GTK_OBJECT(guide));
163                 guide = NULL;
164                 if ((horiz ? wy : wx) >= 0) {
165                     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
166                     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
167                     sp_repr_set_point(repr, "orientation", normal);
168                     sp_repr_set_point(repr, "position", event_dt.to_2geom());
169                     SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
170                     Inkscape::GC::release(repr);
171                     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
172                                      _("Create guide"));
173                 }
174                 desktop->set_coordinate_status(event_dt);
175             }
176         default:
177             break;
178     }
180     return FALSE;
183 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
185     return sp_dt_ruler_event(widget, event, dtw, true);
188 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
190     return sp_dt_ruler_event(widget, event, dtw, false);
193 /* Guides */
195 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
197     static bool dragging = false;
198     static bool moved = false;
199     gint ret = FALSE;
201     SPGuide *guide = SP_GUIDE(data);
202     SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
204         switch (event->type) {
205         case GDK_2BUTTON_PRESS:
206             if (event->button.button == 1) {
207                 dragging = false;
208                 sp_canvas_item_ungrab(item, event->button.time);
209                 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
210                 ret = TRUE;
211             }
212             break;
213         case GDK_BUTTON_PRESS:
214             if (event->button.button == 1) {
215                 if (event->button.state & GDK_CONTROL_MASK) {
216                     SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
217                     sp_guide_remove(guide);
218                     sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
219                     ret = TRUE;
220                     break;
221                 }
222                 dragging = true;
223                 sp_canvas_item_grab(item,
224                                     ( GDK_BUTTON_RELEASE_MASK  |
225                                       GDK_BUTTON_PRESS_MASK    |
226                                       GDK_POINTER_MOTION_MASK ),
227                                     NULL,
228                                     event->button.time);
229                 ret = TRUE;
230             }
231             break;
232     case GDK_MOTION_NOTIFY:
233             if (dragging) {
234                 NR::Point const motion_w(event->motion.x,
235                                          event->motion.y);
236                 NR::Point motion_dt(desktop->w2d(motion_w));
237                 
238                 // This is for snapping while dragging existing guidelines. New guidelines, 
239                 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
240                 SnapManager const &m = desktop->namedview->snap_manager;
241                 motion_dt = m.guideSnap(motion_dt, guide->normal_to_line).getPoint();
242                 
243                 sp_guide_moveto(*guide, motion_dt.to_2geom(), false);
244                 moved = true;
245                 desktop->set_coordinate_status(motion_dt);
246                 desktop->setPosition (motion_dt);
247                 ret = TRUE;
248             }
249             break;
250     case GDK_BUTTON_RELEASE:
251             if (dragging && event->button.button == 1) {
252                 if (moved) {
253                     NR::Point const event_w(event->button.x,
254                                             event->button.y);
255                     NR::Point event_dt(desktop->w2d(event_w));
257                     SnapManager const &m = desktop->namedview->snap_manager;
258                     event_dt = m.guideSnap(event_dt, guide->normal_to_line).getPoint();
260                     if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
261                         sp_guide_moveto(*guide, event_dt.to_2geom(), true);
262                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
263                                      _("Move guide"));
264                     } else {
265                         /* Undo movement of any attached shapes. */
266                         sp_guide_moveto(*guide, guide->point_on_line, false);
267                         sp_guide_remove(guide);
268                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
269                                      _("Delete guide"));
270                     }
271                     moved = false;
272                     desktop->set_coordinate_status(event_dt);
273                     desktop->setPosition (event_dt);
274                 }
275                 dragging = false;
276                 sp_canvas_item_ungrab(item, event->button.time);
277                 ret=TRUE;
278             }
279     case GDK_ENTER_NOTIFY:
280     {
281             sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
283             char *guide_description = sp_guide_description(guide);
284             desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
285             g_free(guide_description);
286             break;
287     }
288     case GDK_LEAVE_NOTIFY:
289             sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
290             desktop->guidesMessageContext()->clear();
291             break;
292     default:
293             break;
294     }
296     return ret;
299 static std::map<GdkInputSource, std::string> switchMap;
300 static std::map<GdkInputSource, int> toolToUse;
301 static std::string lastName;
302 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
304 static void init_extended()
306     std::string avoidName = "pad";
307     GList* devices = gdk_devices_list();
308     if ( devices ) {
309         for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
310             GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
311             if ( dev->name
312                  && (avoidName != dev->name)
313                  && (switchMap.find(dev->source) == switchMap.end())
314                  && (dev->source != GDK_SOURCE_MOUSE) ) {
315                 switchMap[dev->source] = dev->name;
316 //                 g_message("Adding '%s' as [%d]", dev->name, dev->source);
318                 // Set the initial tool for the device
319                 switch ( dev->source ) {
320                     case GDK_SOURCE_PEN:
321                         toolToUse[GDK_SOURCE_PEN] = TOOLS_CALLIGRAPHIC;
322                         break;
323                     case GDK_SOURCE_ERASER:
324                         toolToUse[GDK_SOURCE_ERASER] = TOOLS_TWEAK;
325                         break;
326                     case GDK_SOURCE_CURSOR:
327                         toolToUse[GDK_SOURCE_CURSOR] = TOOLS_SELECT;
328                         break;
329                     default:
330                         ; // do not add
331                 }
332 //             } else {
333 //                 g_message("Skippn '%s' as [%s]", dev->name, namefor(dev->source));
334             }
335         }
336     }
340 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
342     GdkInputSource source = GDK_SOURCE_MOUSE;
343     std::string name;
345     switch ( event->type ) {
346         case GDK_MOTION_NOTIFY:
347         {
348             GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
349             if ( event2->device ) {
350                 source = event2->device->source;
351                 name = event2->device->name;
352             }
353         }
354         break;
356         case GDK_BUTTON_PRESS:
357         case GDK_2BUTTON_PRESS:
358         case GDK_3BUTTON_PRESS:
359         case GDK_BUTTON_RELEASE:
360         {
361             GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
362             if ( event2->device ) {
363                 source = event2->device->source;
364                 name = event2->device->name;
365             }
366         }
367         break;
369         case GDK_SCROLL:
370         {
371             GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
372             if ( event2->device ) {
373                 source = event2->device->source;
374                 name = event2->device->name;
375             }
376         }
377         break;
379         case GDK_PROXIMITY_IN:
380         case GDK_PROXIMITY_OUT:
381         {
382             GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
383             if ( event2->device ) {
384                 source = event2->device->source;
385                 name = event2->device->name;
386             }
387         }
388         break;
390         default:
391             ;
392     }
394     if (!name.empty()) {
395         if ( lastName != name || lastType != source ) {
396             // The device switched. See if it is one we 'count'
397             std::map<GdkInputSource, std::string>::iterator it = switchMap.find(source);
398             if ( (it != switchMap.end()) && (name == it->second) ) {
399                 std::map<GdkInputSource, int>::iterator it2 = toolToUse.find(source);
400                 if (it2 != toolToUse.end() ) {
401                     // Save the tool currently selected for next time the input device shows up.
402                     if ( (switchMap.find(lastType) != switchMap.end())
403                          && (lastName == switchMap.find(lastType)->second)) {
404                         toolToUse[lastType] = tools_active(desktop);
405                     }
406                     tools_switch(desktop, it2->second);
407                 }
408                 lastName = name;
409                 lastType = source;
410             }
411         }
412     }
417 /*
418   Local Variables:
419   mode:c++
420   c-file-style:"stroustrup"
421   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
422   indent-tabs-mode:nil
423   fill-column:99
424   End:
425 */
426 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :