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 }
278 break;
279 case GDK_BUTTON_RELEASE:
280 event_context->xp = event_context->yp = 0;
281 if (event->button.button == 1 && !event_context->space_panning) {
282 dragging = false;
283 sp_event_context_discard_delayed_snap_event(event_context);
284 if (!event_context->within_tolerance) {
285 // we've been dragging, finish the arc
286 sp_arc_finish(ac);
287 } else if (event_context->item_to_select) {
288 // no dragging, select clicked item if any
289 if (event->button.state & GDK_SHIFT_MASK) {
290 selection->toggle(event_context->item_to_select);
291 } else {
292 selection->set(event_context->item_to_select);
293 }
294 } else {
295 // click in an empty space
296 selection->clear();
297 }
298 event_context->xp = 0;
299 event_context->yp = 0;
300 event_context->item_to_select = NULL;
301 ret = TRUE;
302 }
303 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
304 break;
305 case GDK_KEY_PRESS:
306 switch (get_group0_keyval (&event->key)) {
307 case GDK_Alt_L:
308 case GDK_Alt_R:
309 case GDK_Control_L:
310 case GDK_Control_R:
311 case GDK_Shift_L:
312 case GDK_Shift_R:
313 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
314 case GDK_Meta_R:
315 if (!dragging) {
316 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
317 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
318 _("<b>Shift</b>: draw around the starting point"),
319 NULL);
320 }
321 break;
322 case GDK_Up:
323 case GDK_Down:
324 case GDK_KP_Up:
325 case GDK_KP_Down:
326 // prevent the zoom field from activation
327 if (!MOD__CTRL_ONLY)
328 ret = TRUE;
329 break;
330 case GDK_x:
331 case GDK_X:
332 if (MOD__ALT_ONLY) {
333 desktop->setToolboxFocusTo ("altx-arc");
334 ret = TRUE;
335 }
336 break;
337 case GDK_Escape:
338 if (dragging) {
339 dragging = false;
340 sp_event_context_discard_delayed_snap_event(event_context);
341 // if drawing, cancel, otherwise pass it up for deselecting
342 sp_arc_cancel(ac);
343 ret = TRUE;
344 }
345 break;
346 case GDK_space:
347 if (dragging) {
348 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
349 event->button.time);
350 dragging = false;
351 sp_event_context_discard_delayed_snap_event(event_context);
352 if (!event_context->within_tolerance) {
353 // we've been dragging, finish the arc
354 sp_arc_finish(ac);
355 }
356 // do not return true, so that space would work switching to selector
357 }
358 break;
360 default:
361 break;
362 }
363 break;
364 case GDK_KEY_RELEASE:
365 switch (event->key.keyval) {
366 case GDK_Alt_L:
367 case GDK_Alt_R:
368 case GDK_Control_L:
369 case GDK_Control_R:
370 case GDK_Shift_L:
371 case GDK_Shift_R:
372 case GDK_Meta_L: // Meta is when you press Shift+Alt
373 case GDK_Meta_R:
374 event_context->defaultMessageContext()->clear();
375 break;
376 default:
377 break;
378 }
379 break;
380 default:
381 break;
382 }
384 if (!ret) {
385 if (((SPEventContextClass *) parent_class)->root_handler) {
386 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
387 }
388 }
390 return ret;
391 }
393 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
394 {
395 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
397 if (!ac->item) {
399 if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
400 return;
401 }
403 /* Create object */
404 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
405 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
406 repr->setAttribute("sodipodi:type", "arc");
408 /* Set style */
409 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
411 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
412 Inkscape::GC::release(repr);
413 ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
414 ac->item->updateRepr();
416 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
417 }
419 bool ctrl_save = false;
420 if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
421 // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
422 // so that the ellipse is not constrained to integer ratios
423 ctrl_save = true;
424 state = state ^ GDK_CONTROL_MASK;
425 }
426 Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
427 if (ctrl_save) {
428 state = state ^ GDK_CONTROL_MASK;
429 }
431 Geom::Point dir = r.dimensions() / 2;
432 if (state & GDK_MOD1_MASK) {
433 /* With Alt let the ellipse pass through the mouse pointer */
434 Geom::Point c = r.midpoint();
435 if (!ctrl_save) {
436 if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
437 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
438 Geom::Point new_dir = pt * i2d - c;
439 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
440 double lambda = new_dir.length() / dir[Geom::Y];
441 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
442 }
443 } else {
444 /* with Alt+Ctrl (without Shift) we generate a perfect circle
445 with diameter click point <--> mouse pointer */
446 double l = dir.length();
447 Geom::Point d (l, l);
448 r = Geom::Rect (c - d, c + d);
449 }
450 }
452 sp_arc_position_set(SP_ARC(ac->item),
453 r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
454 r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
456 double rdimx = r.dimensions()[Geom::X];
457 double rdimy = r.dimensions()[Geom::Y];
458 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
459 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
460 if (state & GDK_CONTROL_MASK) {
461 int ratio_x, ratio_y;
462 if (fabs (rdimx) > fabs (rdimy)) {
463 ratio_x = (int) rint (rdimx / rdimy);
464 ratio_y = 1;
465 } else {
466 ratio_x = 1;
467 ratio_y = (int) rint (rdimy / rdimx);
468 }
469 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);
470 } else {
471 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);
472 }
473 g_string_free(xs, FALSE);
474 g_string_free(ys, FALSE);
475 }
477 static void sp_arc_finish(SPArcContext *ac)
478 {
479 ac->_message_context->clear();
481 if (ac->item != NULL) {
483 SPGenericEllipse *ge = SP_GENERICELLIPSE(SP_ARC(ac->item));
484 if (ge->rx.computed == 0 || ge->ry.computed == 0) {
485 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
486 return;
487 }
489 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
491 SP_OBJECT(ac->item)->updateRepr();
493 sp_canvas_end_forced_full_redraws(desktop->canvas);
495 sp_desktop_selection(desktop)->set(ac->item);
496 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
497 _("Create ellipse"));
499 ac->item = NULL;
500 }
501 }
503 static void sp_arc_cancel(SPArcContext *ac)
504 {
505 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
507 sp_desktop_selection(desktop)->clear();
508 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
510 if (ac->item != NULL) {
511 SP_OBJECT(ac->item)->deleteObject();
512 ac->item = NULL;
513 }
515 ac->within_tolerance = false;
516 ac->xp = 0;
517 ac->yp = 0;
518 ac->item_to_select = NULL;
520 sp_canvas_end_forced_full_redraws(desktop->canvas);
522 sp_document_cancel(sp_desktop_document(desktop));
523 }
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 :