1 /** @file
2 * @brief Ellipse drawing context
3 */
4 /* Authors:
5 * Mitsuru Oka
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <johan@shouraizou.nl>
9 *
10 * Copyright (C) 2000-2006 Authors
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #define __SP_ARC_CONTEXT_C__
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "macros.h"
24 #include <glibmm/i18n.h>
25 #include "display/sp-canvas.h"
26 #include "sp-ellipse.h"
27 #include "document.h"
28 #include "sp-namedview.h"
29 #include "selection.h"
30 #include "desktop-handles.h"
31 #include "snap.h"
32 #include "pixmaps/cursor-ellipse.xpm"
33 #include "sp-metrics.h"
34 #include "xml/repr.h"
35 #include "xml/node-event-vector.h"
36 #include "object-edit.h"
37 #include "preferences.h"
38 #include "message-context.h"
39 #include "desktop.h"
40 #include "desktop-style.h"
41 #include "context-fns.h"
42 #include "verbs.h"
43 #include "shape-editor.h"
44 #include "event-context.h"
46 #include "arc-context.h"
48 static void sp_arc_context_class_init(SPArcContextClass *klass);
49 static void sp_arc_context_init(SPArcContext *arc_context);
50 static void sp_arc_context_dispose(GObject *object);
52 static void sp_arc_context_setup(SPEventContext *ec);
53 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
54 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void sp_arc_drag(SPArcContext *ec, Geom::Point pt, guint state);
57 static void sp_arc_finish(SPArcContext *ec);
58 static void sp_arc_cancel(SPArcContext *ec);
60 static SPEventContextClass *parent_class;
62 GtkType sp_arc_context_get_type()
63 {
64 static GType type = 0;
65 if (!type) {
66 GTypeInfo info = {
67 sizeof(SPArcContextClass),
68 NULL, NULL,
69 (GClassInitFunc) sp_arc_context_class_init,
70 NULL, NULL,
71 sizeof(SPArcContext),
72 4,
73 (GInstanceInitFunc) sp_arc_context_init,
74 NULL, /* value_table */
75 };
76 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
77 }
78 return type;
79 }
81 static void sp_arc_context_class_init(SPArcContextClass *klass)
82 {
84 GObjectClass *object_class = (GObjectClass *) klass;
85 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
87 parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
89 object_class->dispose = sp_arc_context_dispose;
91 event_context_class->setup = sp_arc_context_setup;
92 event_context_class->root_handler = sp_arc_context_root_handler;
93 event_context_class->item_handler = sp_arc_context_item_handler;
94 }
96 static void sp_arc_context_init(SPArcContext *arc_context)
97 {
98 SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
100 event_context->cursor_shape = cursor_ellipse_xpm;
101 event_context->hot_x = 4;
102 event_context->hot_y = 4;
103 event_context->xp = 0;
104 event_context->yp = 0;
105 event_context->tolerance = 0;
106 event_context->within_tolerance = false;
107 event_context->item_to_select = NULL;
109 arc_context->item = NULL;
111 new (&arc_context->sel_changed_connection) sigc::connection();
112 }
114 static void sp_arc_context_dispose(GObject *object)
115 {
116 SPEventContext *ec = SP_EVENT_CONTEXT(object);
117 SPArcContext *ac = SP_ARC_CONTEXT(object);
119 ec->enableGrDrag(false);
121 ac->sel_changed_connection.disconnect();
122 ac->sel_changed_connection.~connection();
124 delete ec->shape_editor;
125 ec->shape_editor = NULL;
127 /* fixme: This is necessary because we do not grab */
128 if (ac->item) {
129 sp_arc_finish(ac);
130 }
132 delete ac->_message_context;
134 G_OBJECT_CLASS(parent_class)->dispose(object);
135 }
137 /**
138 \brief Callback that processes the "changed" signal on the selection;
139 destroys old and creates new knotholder.
140 */
141 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
142 {
143 SPArcContext *ac = SP_ARC_CONTEXT(data);
144 SPEventContext *ec = SP_EVENT_CONTEXT(ac);
146 ec->shape_editor->unset_item(SH_KNOTHOLDER);
147 SPItem *item = selection->singleItem();
148 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
149 }
151 static void sp_arc_context_setup(SPEventContext *ec)
152 {
153 SPArcContext *ac = SP_ARC_CONTEXT(ec);
154 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
156 if (((SPEventContextClass *) parent_class)->setup) {
157 ((SPEventContextClass *) parent_class)->setup(ec);
158 }
160 ec->shape_editor = new ShapeEditor(ec->desktop);
162 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
163 if (item) {
164 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
165 }
167 ac->sel_changed_connection.disconnect();
168 ac->sel_changed_connection = selection->connectChanged(
169 sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
170 );
172 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
173 if (prefs->getBool("/tools/shapes/selcue")) {
174 ec->enableSelectionCue();
175 }
177 if (prefs->getBool("/tools/shapes/gradientdrag")) {
178 ec->enableGrDrag();
179 }
181 ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
182 }
184 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
185 {
186 SPDesktop *desktop = event_context->desktop;
187 gint ret = FALSE;
189 switch (event->type) {
190 case GDK_BUTTON_PRESS:
191 if (event->button.button == 1 && !event_context->space_panning) {
192 Inkscape::setup_for_drag_start(desktop, event_context, event);
193 ret = TRUE;
194 }
195 break;
196 // motion and release are always on root (why?)
197 default:
198 break;
199 }
201 if (((SPEventContextClass *) parent_class)->item_handler) {
202 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
203 }
205 return ret;
206 }
208 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
209 {
210 static bool dragging;
212 SPDesktop *desktop = event_context->desktop;
213 Inkscape::Selection *selection = sp_desktop_selection(desktop);
214 SPArcContext *ac = SP_ARC_CONTEXT(event_context);
215 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
217 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
219 gint ret = FALSE;
221 switch (event->type) {
222 case GDK_BUTTON_PRESS:
223 if (event->button.button == 1 && !event_context->space_panning) {
225 dragging = true;
226 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
228 /* Snap center */
229 SnapManager &m = desktop->namedview->snap_manager;
230 m.setup(desktop);
231 Geom::Point pt2g = to_2geom(ac->center);
232 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
233 ac->center = from_2geom(pt2g);
235 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
236 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
237 GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
238 NULL, event->button.time);
239 ret = TRUE;
240 }
241 break;
242 case GDK_MOTION_NOTIFY:
243 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
245 if ( event_context->within_tolerance
246 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
247 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
248 break; // do not drag if we're within tolerance from origin
249 }
250 // Once the user has moved farther than tolerance from the original location
251 // (indicating they intend to draw, not click), then always process the
252 // motion notify coordinates as given (no snapping back to origin)
253 event_context->within_tolerance = false;
255 Geom::Point const motion_w(event->motion.x, event->motion.y);
256 Geom::Point motion_dt(desktop->w2d(motion_w));
258 sp_arc_drag(ac, motion_dt, event->motion.state);
260 gobble_motion_events(GDK_BUTTON1_MASK);
262 ret = TRUE;
263 }
264 break;
265 case GDK_BUTTON_RELEASE:
266 event_context->xp = event_context->yp = 0;
267 if (event->button.button == 1 && !event_context->space_panning) {
268 dragging = false;
269 sp_event_context_discard_delayed_snap_event(event_context);
270 if (!event_context->within_tolerance) {
271 // we've been dragging, finish the arc
272 sp_arc_finish(ac);
273 } else if (event_context->item_to_select) {
274 // no dragging, select clicked item if any
275 if (event->button.state & GDK_SHIFT_MASK) {
276 selection->toggle(event_context->item_to_select);
277 } else {
278 selection->set(event_context->item_to_select);
279 }
280 } else {
281 // click in an empty space
282 selection->clear();
283 }
284 event_context->xp = 0;
285 event_context->yp = 0;
286 event_context->item_to_select = NULL;
287 ret = TRUE;
288 }
289 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
290 break;
291 case GDK_KEY_PRESS:
292 switch (get_group0_keyval (&event->key)) {
293 case GDK_Alt_L:
294 case GDK_Alt_R:
295 case GDK_Control_L:
296 case GDK_Control_R:
297 case GDK_Shift_L:
298 case GDK_Shift_R:
299 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
300 case GDK_Meta_R:
301 if (!dragging) {
302 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
303 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
304 _("<b>Shift</b>: draw around the starting point"),
305 NULL);
306 }
307 break;
308 case GDK_Up:
309 case GDK_Down:
310 case GDK_KP_Up:
311 case GDK_KP_Down:
312 // prevent the zoom field from activation
313 if (!MOD__CTRL_ONLY)
314 ret = TRUE;
315 break;
316 case GDK_x:
317 case GDK_X:
318 if (MOD__ALT_ONLY) {
319 desktop->setToolboxFocusTo ("altx-arc");
320 ret = TRUE;
321 }
322 break;
323 case GDK_Escape:
324 if (dragging) {
325 dragging = false;
326 sp_event_context_discard_delayed_snap_event(event_context);
327 // if drawing, cancel, otherwise pass it up for deselecting
328 sp_arc_cancel(ac);
329 ret = TRUE;
330 }
331 break;
332 case GDK_space:
333 if (dragging) {
334 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
335 event->button.time);
336 dragging = false;
337 sp_event_context_discard_delayed_snap_event(event_context);
338 if (!event_context->within_tolerance) {
339 // we've been dragging, finish the arc
340 sp_arc_finish(ac);
341 }
342 // do not return true, so that space would work switching to selector
343 }
344 break;
346 default:
347 break;
348 }
349 break;
350 case GDK_KEY_RELEASE:
351 switch (event->key.keyval) {
352 case GDK_Alt_L:
353 case GDK_Alt_R:
354 case GDK_Control_L:
355 case GDK_Control_R:
356 case GDK_Shift_L:
357 case GDK_Shift_R:
358 case GDK_Meta_L: // Meta is when you press Shift+Alt
359 case GDK_Meta_R:
360 event_context->defaultMessageContext()->clear();
361 break;
362 default:
363 break;
364 }
365 break;
366 default:
367 break;
368 }
370 if (!ret) {
371 if (((SPEventContextClass *) parent_class)->root_handler) {
372 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
373 }
374 }
376 return ret;
377 }
379 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
380 {
381 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
383 if (!ac->item) {
385 if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
386 return;
387 }
389 /* Create object */
390 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
391 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
392 repr->setAttribute("sodipodi:type", "arc");
394 /* Set style */
395 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
397 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
398 Inkscape::GC::release(repr);
399 ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
400 ac->item->updateRepr();
402 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
403 }
405 bool ctrl_save = false;
406 if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
407 // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
408 // so that the ellipse is not constrained to integer ratios
409 ctrl_save = true;
410 state = state ^ GDK_CONTROL_MASK;
411 }
412 Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
413 if (ctrl_save) {
414 state = state ^ GDK_CONTROL_MASK;
415 }
417 Geom::Point dir = r.dimensions() / 2;
418 if (state & GDK_MOD1_MASK) {
419 /* With Alt let the ellipse pass through the mouse pointer */
420 Geom::Point c = r.midpoint();
421 if (!ctrl_save) {
422 if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
423 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
424 Geom::Point new_dir = pt * i2d - c;
425 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
426 double lambda = new_dir.length() / dir[Geom::Y];
427 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
428 }
429 } else {
430 /* with Alt+Ctrl (without Shift) we generate a perfect circle
431 with diameter click point <--> mouse pointer */
432 double l = dir.length();
433 Geom::Point d (l, l);
434 r = Geom::Rect (c - d, c + d);
435 }
436 }
438 sp_arc_position_set(SP_ARC(ac->item),
439 r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
440 r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
442 double rdimx = r.dimensions()[Geom::X];
443 double rdimy = r.dimensions()[Geom::Y];
444 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
445 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
446 if (state & GDK_CONTROL_MASK) {
447 int ratio_x, ratio_y;
448 if (fabs (rdimx) > fabs (rdimy)) {
449 ratio_x = (int) rint (rdimx / rdimy);
450 ratio_y = 1;
451 } else {
452 ratio_x = 1;
453 ratio_y = (int) rint (rdimy / rdimx);
454 }
455 ac->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Ellipse</b>: %s × %s (constrained to ratio %d:%d); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str, ratio_x, ratio_y);
456 } else {
457 ac->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Ellipse</b>: %s × %s; with <b>Ctrl</b> to make square or integer-ratio ellipse; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
458 }
459 g_string_free(xs, FALSE);
460 g_string_free(ys, FALSE);
461 }
463 static void sp_arc_finish(SPArcContext *ac)
464 {
465 ac->_message_context->clear();
467 if (ac->item != NULL) {
469 SPGenericEllipse *ge = SP_GENERICELLIPSE(SP_ARC(ac->item));
470 if (ge->rx.computed == 0 || ge->ry.computed == 0) {
471 sp_arc_cancel(ac); // Don't allow the creating of zero sized arc, for example when the start and and point snap to the snap grid point
472 return;
473 }
475 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
477 SP_OBJECT(ac->item)->updateRepr();
479 sp_canvas_end_forced_full_redraws(desktop->canvas);
481 sp_desktop_selection(desktop)->set(ac->item);
482 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
483 _("Create ellipse"));
485 ac->item = NULL;
486 }
487 }
489 static void sp_arc_cancel(SPArcContext *ac)
490 {
491 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
493 sp_desktop_selection(desktop)->clear();
494 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
496 if (ac->item != NULL) {
497 SP_OBJECT(ac->item)->deleteObject();
498 ac->item = NULL;
499 }
501 ac->within_tolerance = false;
502 ac->xp = 0;
503 ac->yp = 0;
504 ac->item_to_select = NULL;
506 sp_canvas_end_forced_full_redraws(desktop->canvas);
508 sp_document_cancel(sp_desktop_document(desktop));
509 }
512 /*
513 Local Variables:
514 mode:c++
515 c-file-style:"stroustrup"
516 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
517 indent-tabs-mode:nil
518 fill-column:99
519 End:
520 */
521 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :