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 "display/snap-indicator.h"
21 #include "helper/unit-menu.h"
22 #include "helper/units.h"
23 #include "desktop.h"
24 #include "document.h"
25 #include "sp-guide.h"
26 #include "sp-namedview.h"
27 #include "desktop-handles.h"
28 #include "event-context.h"
29 #include "widgets/desktop-widget.h"
30 #include "sp-metrics.h"
31 #include <glibmm/i18n.h>
32 #include "dialogs/dialog-events.h"
33 #include "message-context.h"
34 #include "xml/repr.h"
35 #include "dialogs/guidelinedialog.h"
36 #include "snap.h"
37 #include "display/canvas-grid.h"
38 #include "display/canvas-axonomgrid.h"
39 #include "preferences.h"
40 #include "helper/action.h"
41 #include "tools-switch.h"
42 #include <2geom/line.h>
44 static void snoop_extended(GdkEvent* event, SPDesktop *desktop);
45 static void init_extended();
47 /* Root item handler */
49 int sp_desktop_root_handler(SPCanvasItem */*item*/, GdkEvent *event, SPDesktop *desktop)
50 {
51 static bool watch = false;
52 static bool first = true;
54 if ( first ) {
55 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
56 if ( prefs->getBool("/options/useextinput/value", true)
57 && prefs->getBool("/options/switchonextinput/value") ) {
58 watch = true;
59 init_extended();
60 }
61 first = false;
62 }
63 if ( watch ) {
64 snoop_extended(event, desktop);
65 }
67 return sp_event_context_root_handler(desktop->event_context, event);
68 }
71 static gint sp_dt_ruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw, bool horiz)
72 {
73 static bool dragging = false;
74 static SPCanvasItem *guide = NULL;
75 static Geom::Point normal;
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;
91 sp_canvas_set_snap_delay_active(desktop->canvas, true);
92 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
93 Geom::Point const event_dt(desktop->w2d(event_w));
95 // explicitly show guidelines; if I draw a guide, I want them on
96 sp_repr_set_boolean(repr, "showguides", TRUE);
97 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
99 // calculate the normal of the guidelines when dragged from the edges of rulers.
100 Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
101 Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
102 normal_bl_to_tr.normalize();
103 normal_tr_to_bl.normalize();
104 Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
105 if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
106 Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
107 if (event->button.state & GDK_CONTROL_MASK) {
108 // guidelines normal to gridlines
109 normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
110 normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
111 } else {
112 normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
113 normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
114 }
115 }
116 if (horiz) {
117 if (wx < 50) {
118 normal = normal_bl_to_tr;
119 } else if (wx > width - 50) {
120 normal = normal_tr_to_bl;
121 } else {
122 normal = Geom::Point(0.,1.);
123 }
124 } else {
125 if (wy < 50) {
126 normal = normal_bl_to_tr;
127 } else if (wy > height - 50) {
128 normal = normal_tr_to_bl;
129 } else {
130 normal = Geom::Point(1.,0.);
131 }
132 }
134 guide = sp_guideline_new(desktop->guides, event_dt, normal);
135 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
136 gdk_pointer_grab(widget->window, FALSE,
137 (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
138 NULL, NULL,
139 event->button.time);
140 }
141 break;
142 case GDK_MOTION_NOTIFY:
143 if (dragging) {
144 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
145 Geom::Point event_dt(desktop->w2d(event_w));
147 SnapManager &m = desktop->namedview->snap_manager;
148 m.setup(desktop);
149 m.guideSnap(event_dt, normal);
151 sp_guideline_set_position(SP_GUIDELINE(guide), from_2geom(event_dt));
152 desktop->set_coordinate_status(to_2geom(event_dt));
153 desktop->setPosition(to_2geom(event_dt));
154 }
155 break;
156 case GDK_BUTTON_RELEASE:
157 if (dragging && event->button.button == 1) {
158 gdk_pointer_ungrab(event->button.time);
159 Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
160 Geom::Point event_dt(desktop->w2d(event_w));
162 SnapManager &m = desktop->namedview->snap_manager;
163 m.setup(desktop);
164 m.guideSnap(event_dt, normal);
166 dragging = false;
167 sp_canvas_set_snap_delay_active(desktop->canvas, false);
168 gtk_object_destroy(GTK_OBJECT(guide));
169 guide = NULL;
170 if ((horiz ? wy : wx) >= 0) {
171 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
172 Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
173 sp_repr_set_point(repr, "orientation", normal);
174 sp_repr_set_point(repr, "position", from_2geom(event_dt));
175 SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
176 Inkscape::GC::release(repr);
177 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
178 _("Create guide"));
179 }
180 desktop->set_coordinate_status(from_2geom(event_dt));
181 }
182 default:
183 break;
184 }
186 return FALSE;
187 }
189 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
190 {
191 return sp_dt_ruler_event(widget, event, dtw, true);
192 }
194 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
195 {
196 return sp_dt_ruler_event(widget, event, dtw, false);
197 }
199 /* Guides */
201 static Geom::Point drag_origin;
203 enum SPGuideDragType {
204 SP_DRAG_TRANSLATE,
205 SP_DRAG_TRANSLATE_CONSTRAINED,
206 SP_DRAG_ROTATE,
207 SP_DRAG_MOVE_ORIGIN,
208 SP_DRAG_NONE
209 };
211 static SPGuideDragType drag_type = SP_DRAG_NONE;
213 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
214 {
215 static bool moved = false;
216 gint ret = FALSE;
218 SPGuide *guide = SP_GUIDE(data);
219 SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
221 switch (event->type) {
222 case GDK_2BUTTON_PRESS:
223 if (event->button.button == 1) {
224 drag_type = SP_DRAG_NONE;
225 sp_canvas_set_snap_delay_active(desktop->canvas, false);
226 sp_canvas_item_ungrab(item, event->button.time);
227 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
228 ret = TRUE;
229 }
230 break;
231 case GDK_BUTTON_PRESS:
232 if (event->button.button == 1) {
233 if (event->button.state & GDK_CONTROL_MASK) {
234 SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
235 sp_guide_remove(guide);
236 sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
237 ret = TRUE;
238 break;
239 }
241 sp_canvas_set_snap_delay_active(desktop->canvas, true);
242 double tol = 40.0;
243 Geom::Point const event_w(event->button.x, event->button.y);
244 Geom::Point const event_dt(desktop->w2d(event_w));
245 drag_origin = event_dt;
246 if (Geom::L2(guide->point_on_line - event_dt) < tol) {
247 // the click was on the guide 'anchor'
248 drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_MOVE_ORIGIN : SP_DRAG_TRANSLATE;
249 } else {
250 drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_ROTATE : SP_DRAG_TRANSLATE;
251 sp_canvas_item_grab(item,
252 ( GDK_BUTTON_RELEASE_MASK |
253 GDK_BUTTON_PRESS_MASK |
254 GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
255 NULL,
256 event->button.time);
257 }
258 ret = TRUE;
259 }
260 break;
261 case GDK_MOTION_NOTIFY:
262 if (drag_type != SP_DRAG_NONE) {
263 Geom::Point const motion_w(event->motion.x,
264 event->motion.y);
265 Geom::Point motion_dt(desktop->w2d(motion_w));
267 // This is for snapping while dragging existing guidelines. New guidelines,
268 // which are dragged off the ruler, are being snapped in sp_dt_ruler_event
269 SnapManager &m = desktop->namedview->snap_manager;
270 m.setup(desktop);
271 m.guideSnap(motion_dt, to_2geom(guide->normal_to_line));
273 switch (drag_type) {
274 case SP_DRAG_TRANSLATE:
275 {
276 sp_guide_moveto(*guide, guide->point_on_line + motion_dt - drag_origin, false);
277 break;
278 }
279 case SP_DRAG_TRANSLATE_CONSTRAINED:
280 {
281 Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, motion_dt);
282 sp_guide_moveto(*guide, pt_constr, false);
283 break;
284 }
285 case SP_DRAG_ROTATE:
286 {
287 double angle = angle_between(drag_origin - guide->point_on_line, motion_dt - guide->point_on_line);
288 sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), false);
289 break;
290 }
291 case SP_DRAG_MOVE_ORIGIN:
292 {
293 Geom::Line line(guide->point_on_line, guide->angle());
294 Geom::Coord t = line.nearestPoint(motion_dt);
295 sp_guide_moveto(*guide, line.pointAt(t), false);
296 break;
297 }
298 case SP_DRAG_NONE:
299 g_assert_not_reached();
300 break;
301 }
302 moved = true;
303 desktop->set_coordinate_status(from_2geom(motion_dt));
304 desktop->setPosition(from_2geom(motion_dt));
306 ret = TRUE;
307 }
308 break;
309 case GDK_BUTTON_RELEASE:
310 if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
311 if (moved) {
312 Geom::Point const event_w(event->button.x,
313 event->button.y);
314 Geom::Point event_dt(desktop->w2d(event_w));
316 SnapManager &m = desktop->namedview->snap_manager;
317 m.setup(desktop);
318 m.guideSnap(event_dt, guide->normal_to_line);
320 if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
321 switch (drag_type) {
322 case SP_DRAG_TRANSLATE:
323 {
324 sp_guide_moveto(*guide, guide->point_on_line + event_dt - drag_origin, true);
325 break;
326 }
327 case SP_DRAG_TRANSLATE_CONSTRAINED:
328 {
329 Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, event_dt);
330 sp_guide_moveto(*guide, pt_constr, true);
331 break;
332 }
333 case SP_DRAG_ROTATE:
334 {
335 double angle = angle_between(drag_origin - guide->point_on_line, event_dt - guide->point_on_line);
336 sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), true);
337 break;
338 }
339 case SP_DRAG_MOVE_ORIGIN:
340 {
341 Geom::Line line(guide->point_on_line, guide->angle());
342 Geom::Coord t = line.nearestPoint(event_dt);
343 sp_guide_moveto(*guide, line.pointAt(t), true);
344 break;
345 }
346 case SP_DRAG_NONE:
347 g_assert_not_reached();
348 break;
349 }
350 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
351 _("Move guide"));
352 } else {
353 /* Undo movement of any attached shapes. */
354 sp_guide_moveto(*guide, guide->point_on_line, false);
355 sp_guide_set_normal(*guide, guide->normal_to_line, false);
356 sp_guide_remove(guide);
357 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
358 _("Delete guide"));
359 }
360 moved = false;
361 desktop->set_coordinate_status(from_2geom(event_dt));
362 desktop->setPosition (from_2geom(event_dt));
363 }
364 drag_type = SP_DRAG_NONE;
365 sp_canvas_set_snap_delay_active(desktop->canvas, false);
366 sp_canvas_item_ungrab(item, event->button.time);
367 ret=TRUE;
368 }
369 case GDK_ENTER_NOTIFY:
370 {
371 sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
373 char *guide_description = sp_guide_description(guide);
374 desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
375 g_free(guide_description);
376 break;
377 }
378 case GDK_LEAVE_NOTIFY:
379 sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
380 desktop->guidesMessageContext()->clear();
381 break;
382 default:
383 break;
384 }
386 return ret;
387 }
389 //static std::map<GdkInputSource, std::string> switchMap;
390 static std::map<std::string, int> toolToUse;
391 static std::string lastName;
392 static GdkInputSource lastType = GDK_SOURCE_MOUSE;
394 static void init_extended()
395 {
396 std::string avoidName = "pad";
397 GList* devices = gdk_devices_list();
398 if ( devices ) {
399 for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
400 GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
401 if ( dev->name
402 && (avoidName != dev->name)
403 && (dev->source != GDK_SOURCE_MOUSE) ) {
404 // g_message("Adding '%s' as [%d]", dev->name, dev->source);
406 // Set the initial tool for the device
407 switch ( dev->source ) {
408 case GDK_SOURCE_PEN:
409 toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
410 break;
411 case GDK_SOURCE_ERASER:
412 toolToUse[dev->name] = TOOLS_ERASER;
413 break;
414 case GDK_SOURCE_CURSOR:
415 toolToUse[dev->name] = TOOLS_SELECT;
416 break;
417 default:
418 ; // do not add
419 }
420 // } else if (dev->name) {
421 // g_message("Skippn '%s' as [%s]", dev->name, dev->source);
422 }
423 }
424 }
425 }
428 void snoop_extended(GdkEvent* event, SPDesktop *desktop)
429 {
430 GdkInputSource source = GDK_SOURCE_MOUSE;
431 std::string name;
433 switch ( event->type ) {
434 case GDK_MOTION_NOTIFY:
435 {
436 GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
437 if ( event2->device ) {
438 source = event2->device->source;
439 name = event2->device->name;
440 }
441 }
442 break;
444 case GDK_BUTTON_PRESS:
445 case GDK_2BUTTON_PRESS:
446 case GDK_3BUTTON_PRESS:
447 case GDK_BUTTON_RELEASE:
448 {
449 GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
450 if ( event2->device ) {
451 source = event2->device->source;
452 name = event2->device->name;
453 }
454 }
455 break;
457 case GDK_SCROLL:
458 {
459 GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
460 if ( event2->device ) {
461 source = event2->device->source;
462 name = event2->device->name;
463 }
464 }
465 break;
467 case GDK_PROXIMITY_IN:
468 case GDK_PROXIMITY_OUT:
469 {
470 GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
471 if ( event2->device ) {
472 source = event2->device->source;
473 name = event2->device->name;
474 }
475 }
476 break;
478 default:
479 ;
480 }
482 if (!name.empty()) {
483 if ( lastType != source || lastName != name ) {
484 // The device switched. See if it is one we 'count'
485 //g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
486 std::map<std::string, int>::iterator it = toolToUse.find(lastName);
487 if (it != toolToUse.end()) {
488 // Save the tool currently selected for next time the input
489 // device shows up.
490 it->second = tools_active(desktop);
491 }
493 it = toolToUse.find(name);
494 if (it != toolToUse.end() ) {
495 tools_switch(desktop, it->second);
496 }
498 lastName = name;
499 lastType = source;
500 }
501 }
502 }
506 /*
507 Local Variables:
508 mode:c++
509 c-file-style:"stroustrup"
510 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
511 indent-tabs-mode:nil
512 fill-column:99
513 End:
514 */
515 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :