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 void sp_arc_context_finish(SPEventContext *ec);
54 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
55 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
57 static void sp_arc_drag(SPArcContext *ec, Geom::Point pt, guint state);
58 static void sp_arc_finish(SPArcContext *ec);
59 static void sp_arc_cancel(SPArcContext *ec);
61 static SPEventContextClass *parent_class;
63 GtkType sp_arc_context_get_type()
64 {
65 static GType type = 0;
66 if (!type) {
67 GTypeInfo info = {
68 sizeof(SPArcContextClass),
69 NULL, NULL,
70 (GClassInitFunc) sp_arc_context_class_init,
71 NULL, NULL,
72 sizeof(SPArcContext),
73 4,
74 (GInstanceInitFunc) sp_arc_context_init,
75 NULL, /* value_table */
76 };
77 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
78 }
79 return type;
80 }
82 static void sp_arc_context_class_init(SPArcContextClass *klass)
83 {
85 GObjectClass *object_class = (GObjectClass *) klass;
86 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88 parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
90 object_class->dispose = sp_arc_context_dispose;
92 event_context_class->setup = sp_arc_context_setup;
93 event_context_class->finish = sp_arc_context_finish;
94 event_context_class->root_handler = sp_arc_context_root_handler;
95 event_context_class->item_handler = sp_arc_context_item_handler;
96 }
98 static void sp_arc_context_init(SPArcContext *arc_context)
99 {
100 SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
102 event_context->cursor_shape = cursor_ellipse_xpm;
103 event_context->hot_x = 4;
104 event_context->hot_y = 4;
105 event_context->xp = 0;
106 event_context->yp = 0;
107 event_context->tolerance = 0;
108 event_context->within_tolerance = false;
109 event_context->item_to_select = NULL;
111 arc_context->item = NULL;
113 new (&arc_context->sel_changed_connection) sigc::connection();
114 }
116 static void sp_arc_context_finish(SPEventContext *ec)
117 {
118 SPArcContext *ac = SP_ARC_CONTEXT(ec);
119 SPDesktop *desktop = ec->desktop;
121 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
122 sp_arc_finish(ac);
123 ac->sel_changed_connection.disconnect();
125 if (((SPEventContextClass *) parent_class)->finish) {
126 ((SPEventContextClass *) parent_class)->finish(ec);
127 }
128 }
130 static void sp_arc_context_dispose(GObject *object)
131 {
132 SPEventContext *ec = SP_EVENT_CONTEXT(object);
133 SPArcContext *ac = SP_ARC_CONTEXT(object);
135 ec->enableGrDrag(false);
137 ac->sel_changed_connection.disconnect();
138 ac->sel_changed_connection.~connection();
140 delete ec->shape_editor;
141 ec->shape_editor = NULL;
143 /* fixme: This is necessary because we do not grab */
144 if (ac->item) {
145 sp_arc_finish(ac);
146 }
148 delete ac->_message_context;
150 G_OBJECT_CLASS(parent_class)->dispose(object);
151 }
153 /**
154 \brief Callback that processes the "changed" signal on the selection;
155 destroys old and creates new knotholder.
156 */
157 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
158 {
159 SPArcContext *ac = SP_ARC_CONTEXT(data);
160 SPEventContext *ec = SP_EVENT_CONTEXT(ac);
162 ec->shape_editor->unset_item(SH_KNOTHOLDER);
163 SPItem *item = selection->singleItem();
164 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
165 }
167 static void sp_arc_context_setup(SPEventContext *ec)
168 {
169 SPArcContext *ac = SP_ARC_CONTEXT(ec);
170 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
172 if (((SPEventContextClass *) parent_class)->setup) {
173 ((SPEventContextClass *) parent_class)->setup(ec);
174 }
176 ec->shape_editor = new ShapeEditor(ec->desktop);
178 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
179 if (item) {
180 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
181 }
183 ac->sel_changed_connection.disconnect();
184 ac->sel_changed_connection = selection->connectChanged(
185 sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
186 );
188 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
189 if (prefs->getBool("/tools/shapes/selcue")) {
190 ec->enableSelectionCue();
191 }
193 if (prefs->getBool("/tools/shapes/gradientdrag")) {
194 ec->enableGrDrag();
195 }
197 ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
198 }
200 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
201 {
202 SPDesktop *desktop = event_context->desktop;
203 gint ret = FALSE;
205 switch (event->type) {
206 case GDK_BUTTON_PRESS:
207 if (event->button.button == 1 && !event_context->space_panning) {
208 Inkscape::setup_for_drag_start(desktop, event_context, event);
209 ret = TRUE;
210 }
211 break;
212 // motion and release are always on root (why?)
213 default:
214 break;
215 }
217 if (((SPEventContextClass *) parent_class)->item_handler) {
218 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
219 }
221 return ret;
222 }
224 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
225 {
226 static bool dragging;
228 SPDesktop *desktop = event_context->desktop;
229 Inkscape::Selection *selection = sp_desktop_selection(desktop);
230 SPArcContext *ac = SP_ARC_CONTEXT(event_context);
231 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
233 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
235 gint ret = FALSE;
237 switch (event->type) {
238 case GDK_BUTTON_PRESS:
239 if (event->button.button == 1 && !event_context->space_panning) {
241 dragging = true;
242 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
244 /* Snap center */
245 SnapManager &m = desktop->namedview->snap_manager;
246 m.setup(desktop);
247 m.freeSnapReturnByRef(ac->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
249 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
250 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
251 GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
252 NULL, event->button.time);
253 ret = TRUE;
254 }
255 break;
256 case GDK_MOTION_NOTIFY:
257 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
259 if ( event_context->within_tolerance
260 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
261 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
262 break; // do not drag if we're within tolerance from origin
263 }
264 // Once the user has moved farther than tolerance from the original location
265 // (indicating they intend to draw, not click), then always process the
266 // motion notify coordinates as given (no snapping back to origin)
267 event_context->within_tolerance = false;
269 Geom::Point const motion_w(event->motion.x, event->motion.y);
270 Geom::Point motion_dt(desktop->w2d(motion_w));
272 sp_arc_drag(ac, motion_dt, event->motion.state);
274 gobble_motion_events(GDK_BUTTON1_MASK);
276 ret = TRUE;
277 } else if (!sp_event_context_knot_mouseover(ac)){
278 SnapManager &m = desktop->namedview->snap_manager;
279 m.setup(desktop);
281 Geom::Point const motion_w(event->motion.x, event->motion.y);
282 Geom::Point motion_dt(desktop->w2d(motion_w));
283 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
284 }
285 break;
286 case GDK_BUTTON_RELEASE:
287 event_context->xp = event_context->yp = 0;
288 if (event->button.button == 1 && !event_context->space_panning) {
289 dragging = false;
290 sp_event_context_discard_delayed_snap_event(event_context);
291 if (!event_context->within_tolerance) {
292 // we've been dragging, finish the arc
293 sp_arc_finish(ac);
294 } else if (event_context->item_to_select) {
295 // no dragging, select clicked item if any
296 if (event->button.state & GDK_SHIFT_MASK) {
297 selection->toggle(event_context->item_to_select);
298 } else {
299 selection->set(event_context->item_to_select);
300 }
301 } else {
302 // click in an empty space
303 selection->clear();
304 }
305 event_context->xp = 0;
306 event_context->yp = 0;
307 event_context->item_to_select = NULL;
308 ret = TRUE;
309 }
310 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
311 break;
312 case GDK_KEY_PRESS:
313 switch (get_group0_keyval (&event->key)) {
314 case GDK_Alt_L:
315 case GDK_Alt_R:
316 case GDK_Control_L:
317 case GDK_Control_R:
318 case GDK_Shift_L:
319 case GDK_Shift_R:
320 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
321 case GDK_Meta_R:
322 if (!dragging) {
323 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
324 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
325 _("<b>Shift</b>: draw around the starting point"),
326 NULL);
327 }
328 break;
329 case GDK_Up:
330 case GDK_Down:
331 case GDK_KP_Up:
332 case GDK_KP_Down:
333 // prevent the zoom field from activation
334 if (!MOD__CTRL_ONLY)
335 ret = TRUE;
336 break;
337 case GDK_x:
338 case GDK_X:
339 if (MOD__ALT_ONLY) {
340 desktop->setToolboxFocusTo ("altx-arc");
341 ret = TRUE;
342 }
343 break;
344 case GDK_Escape:
345 if (dragging) {
346 dragging = false;
347 sp_event_context_discard_delayed_snap_event(event_context);
348 // if drawing, cancel, otherwise pass it up for deselecting
349 sp_arc_cancel(ac);
350 ret = TRUE;
351 }
352 break;
353 case GDK_space:
354 if (dragging) {
355 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
356 event->button.time);
357 dragging = false;
358 sp_event_context_discard_delayed_snap_event(event_context);
359 if (!event_context->within_tolerance) {
360 // we've been dragging, finish the arc
361 sp_arc_finish(ac);
362 }
363 // do not return true, so that space would work switching to selector
364 }
365 break;
367 default:
368 break;
369 }
370 break;
371 case GDK_KEY_RELEASE:
372 switch (event->key.keyval) {
373 case GDK_Alt_L:
374 case GDK_Alt_R:
375 case GDK_Control_L:
376 case GDK_Control_R:
377 case GDK_Shift_L:
378 case GDK_Shift_R:
379 case GDK_Meta_L: // Meta is when you press Shift+Alt
380 case GDK_Meta_R:
381 event_context->defaultMessageContext()->clear();
382 break;
383 default:
384 break;
385 }
386 break;
387 default:
388 break;
389 }
391 if (!ret) {
392 if (((SPEventContextClass *) parent_class)->root_handler) {
393 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
394 }
395 }
397 return ret;
398 }
400 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
401 {
402 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
404 if (!ac->item) {
406 if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
407 return;
408 }
410 /* Create object */
411 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
412 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
413 repr->setAttribute("sodipodi:type", "arc");
415 /* Set style */
416 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
418 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
419 Inkscape::GC::release(repr);
420 ac->item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
421 ac->item->updateRepr();
423 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
424 }
426 bool ctrl_save = false;
427 if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
428 // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
429 // so that the ellipse is not constrained to integer ratios
430 ctrl_save = true;
431 state = state ^ GDK_CONTROL_MASK;
432 }
433 Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
434 if (ctrl_save) {
435 state = state ^ GDK_CONTROL_MASK;
436 }
438 Geom::Point dir = r.dimensions() / 2;
439 if (state & GDK_MOD1_MASK) {
440 /* With Alt let the ellipse pass through the mouse pointer */
441 Geom::Point c = r.midpoint();
442 if (!ctrl_save) {
443 if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
444 Geom::Matrix const i2d ((ac->item)->i2d_affine ());
445 Geom::Point new_dir = pt * i2d - c;
446 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
447 double lambda = new_dir.length() / dir[Geom::Y];
448 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
449 }
450 } else {
451 /* with Alt+Ctrl (without Shift) we generate a perfect circle
452 with diameter click point <--> mouse pointer */
453 double l = dir.length();
454 Geom::Point d (l, l);
455 r = Geom::Rect (c - d, c + d);
456 }
457 }
459 sp_arc_position_set(SP_ARC(ac->item),
460 r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
461 r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
463 double rdimx = r.dimensions()[Geom::X];
464 double rdimy = r.dimensions()[Geom::Y];
465 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
466 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
467 if (state & GDK_CONTROL_MASK) {
468 int ratio_x, ratio_y;
469 if (fabs (rdimx) > fabs (rdimy)) {
470 ratio_x = (int) rint (rdimx / rdimy);
471 ratio_y = 1;
472 } else {
473 ratio_x = 1;
474 ratio_y = (int) rint (rdimy / rdimx);
475 }
476 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);
477 } else {
478 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);
479 }
480 g_string_free(xs, FALSE);
481 g_string_free(ys, FALSE);
482 }
484 static void sp_arc_finish(SPArcContext *ac)
485 {
486 ac->_message_context->clear();
488 if (ac->item != NULL) {
490 SPGenericEllipse *ge = SP_GENERICELLIPSE(SP_ARC(ac->item));
491 if (ge->rx.computed == 0 || ge->ry.computed == 0) {
492 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
493 return;
494 }
496 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
498 SP_OBJECT(ac->item)->updateRepr();
500 sp_canvas_end_forced_full_redraws(desktop->canvas);
502 sp_desktop_selection(desktop)->set(ac->item);
503 SPDocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
504 _("Create ellipse"));
506 ac->item = NULL;
507 }
508 }
510 static void sp_arc_cancel(SPArcContext *ac)
511 {
512 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
514 sp_desktop_selection(desktop)->clear();
515 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
517 if (ac->item != NULL) {
518 SP_OBJECT(ac->item)->deleteObject();
519 ac->item = NULL;
520 }
522 ac->within_tolerance = false;
523 ac->xp = 0;
524 ac->yp = 0;
525 ac->item_to_select = NULL;
527 sp_canvas_end_forced_full_redraws(desktop->canvas);
529 SPDocumentUndo::cancel(sp_desktop_document(desktop));
530 }
533 /*
534 Local Variables:
535 mode:c++
536 c-file-style:"stroustrup"
537 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
538 indent-tabs-mode:nil
539 fill-column:99
540 End:
541 */
542 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :