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;
110 event_context->tool_url = "/tools/shapes/arc";
112 arc_context->item = NULL;
114 new (&arc_context->sel_changed_connection) sigc::connection();
115 }
117 static void sp_arc_context_finish(SPEventContext *ec)
118 {
119 SPArcContext *ac = SP_ARC_CONTEXT(ec);
120 SPDesktop *desktop = ec->desktop;
122 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
123 sp_arc_finish(ac);
124 ac->sel_changed_connection.disconnect();
126 if (((SPEventContextClass *) parent_class)->finish) {
127 ((SPEventContextClass *) parent_class)->finish(ec);
128 }
129 }
131 static void sp_arc_context_dispose(GObject *object)
132 {
133 SPEventContext *ec = SP_EVENT_CONTEXT(object);
134 SPArcContext *ac = SP_ARC_CONTEXT(object);
136 ec->enableGrDrag(false);
138 ac->sel_changed_connection.disconnect();
139 ac->sel_changed_connection.~connection();
141 delete ec->shape_editor;
142 ec->shape_editor = NULL;
144 /* fixme: This is necessary because we do not grab */
145 if (ac->item) {
146 sp_arc_finish(ac);
147 }
149 delete ac->_message_context;
151 G_OBJECT_CLASS(parent_class)->dispose(object);
152 }
154 /**
155 \brief Callback that processes the "changed" signal on the selection;
156 destroys old and creates new knotholder.
157 */
158 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
159 {
160 SPArcContext *ac = SP_ARC_CONTEXT(data);
161 SPEventContext *ec = SP_EVENT_CONTEXT(ac);
163 ec->shape_editor->unset_item(SH_KNOTHOLDER);
164 SPItem *item = selection->singleItem();
165 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
166 }
168 static void sp_arc_context_setup(SPEventContext *ec)
169 {
170 SPArcContext *ac = SP_ARC_CONTEXT(ec);
171 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
173 if (((SPEventContextClass *) parent_class)->setup) {
174 ((SPEventContextClass *) parent_class)->setup(ec);
175 }
177 ec->shape_editor = new ShapeEditor(ec->desktop);
179 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
180 if (item) {
181 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
182 }
184 ac->sel_changed_connection.disconnect();
185 ac->sel_changed_connection = selection->connectChanged(
186 sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
187 );
189 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
190 if (prefs->getBool("/tools/shapes/selcue")) {
191 ec->enableSelectionCue();
192 }
194 if (prefs->getBool("/tools/shapes/gradientdrag")) {
195 ec->enableGrDrag();
196 }
198 ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
199 }
201 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
202 {
203 SPDesktop *desktop = event_context->desktop;
204 gint ret = FALSE;
206 switch (event->type) {
207 case GDK_BUTTON_PRESS:
208 if (event->button.button == 1 && !event_context->space_panning) {
209 Inkscape::setup_for_drag_start(desktop, event_context, event);
210 ret = TRUE;
211 }
212 break;
213 // motion and release are always on root (why?)
214 default:
215 break;
216 }
218 if (((SPEventContextClass *) parent_class)->item_handler) {
219 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
220 }
222 return ret;
223 }
225 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
226 {
227 static bool dragging;
229 SPDesktop *desktop = event_context->desktop;
230 Inkscape::Selection *selection = sp_desktop_selection(desktop);
231 SPArcContext *ac = SP_ARC_CONTEXT(event_context);
232 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
234 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
236 gint ret = FALSE;
238 switch (event->type) {
239 case GDK_BUTTON_PRESS:
240 if (event->button.button == 1 && !event_context->space_panning) {
242 dragging = true;
243 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
245 /* Snap center */
246 SnapManager &m = desktop->namedview->snap_manager;
247 m.setup(desktop);
248 m.freeSnapReturnByRef(ac->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
250 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
251 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
252 GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
253 NULL, event->button.time);
254 ret = TRUE;
255 m.unSetup();
256 }
257 break;
258 case GDK_MOTION_NOTIFY:
259 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
261 if ( event_context->within_tolerance
262 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
263 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
264 break; // do not drag if we're within tolerance from origin
265 }
266 // Once the user has moved farther than tolerance from the original location
267 // (indicating they intend to draw, not click), then always process the
268 // motion notify coordinates as given (no snapping back to origin)
269 event_context->within_tolerance = false;
271 Geom::Point const motion_w(event->motion.x, event->motion.y);
272 Geom::Point motion_dt(desktop->w2d(motion_w));
274 sp_arc_drag(ac, motion_dt, event->motion.state);
276 gobble_motion_events(GDK_BUTTON1_MASK);
278 ret = TRUE;
279 } else if (!sp_event_context_knot_mouseover(ac)){
280 SnapManager &m = desktop->namedview->snap_manager;
281 m.setup(desktop);
283 Geom::Point const motion_w(event->motion.x, event->motion.y);
284 Geom::Point motion_dt(desktop->w2d(motion_w));
285 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
286 m.unSetup();
287 }
288 break;
289 case GDK_BUTTON_RELEASE:
290 event_context->xp = event_context->yp = 0;
291 if (event->button.button == 1 && !event_context->space_panning) {
292 dragging = false;
293 sp_event_context_discard_delayed_snap_event(event_context);
294 if (!event_context->within_tolerance) {
295 // we've been dragging, finish the arc
296 sp_arc_finish(ac);
297 } else if (event_context->item_to_select) {
298 // no dragging, select clicked item if any
299 if (event->button.state & GDK_SHIFT_MASK) {
300 selection->toggle(event_context->item_to_select);
301 } else {
302 selection->set(event_context->item_to_select);
303 }
304 } else {
305 // click in an empty space
306 selection->clear();
307 }
308 event_context->xp = 0;
309 event_context->yp = 0;
310 event_context->item_to_select = NULL;
311 ret = TRUE;
312 }
313 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
314 break;
315 case GDK_KEY_PRESS:
316 switch (get_group0_keyval (&event->key)) {
317 case GDK_Alt_L:
318 case GDK_Alt_R:
319 case GDK_Control_L:
320 case GDK_Control_R:
321 case GDK_Shift_L:
322 case GDK_Shift_R:
323 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
324 case GDK_Meta_R:
325 if (!dragging) {
326 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
327 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
328 _("<b>Shift</b>: draw around the starting point"),
329 NULL);
330 }
331 break;
332 case GDK_Up:
333 case GDK_Down:
334 case GDK_KP_Up:
335 case GDK_KP_Down:
336 // prevent the zoom field from activation
337 if (!MOD__CTRL_ONLY)
338 ret = TRUE;
339 break;
340 case GDK_x:
341 case GDK_X:
342 if (MOD__ALT_ONLY) {
343 desktop->setToolboxFocusTo ("altx-arc");
344 ret = TRUE;
345 }
346 break;
347 case GDK_Escape:
348 if (dragging) {
349 dragging = false;
350 sp_event_context_discard_delayed_snap_event(event_context);
351 // if drawing, cancel, otherwise pass it up for deselecting
352 sp_arc_cancel(ac);
353 ret = TRUE;
354 }
355 break;
356 case GDK_space:
357 if (dragging) {
358 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
359 event->button.time);
360 dragging = false;
361 sp_event_context_discard_delayed_snap_event(event_context);
362 if (!event_context->within_tolerance) {
363 // we've been dragging, finish the arc
364 sp_arc_finish(ac);
365 }
366 // do not return true, so that space would work switching to selector
367 }
368 break;
370 default:
371 break;
372 }
373 break;
374 case GDK_KEY_RELEASE:
375 switch (event->key.keyval) {
376 case GDK_Alt_L:
377 case GDK_Alt_R:
378 case GDK_Control_L:
379 case GDK_Control_R:
380 case GDK_Shift_L:
381 case GDK_Shift_R:
382 case GDK_Meta_L: // Meta is when you press Shift+Alt
383 case GDK_Meta_R:
384 event_context->defaultMessageContext()->clear();
385 break;
386 default:
387 break;
388 }
389 break;
390 default:
391 break;
392 }
394 if (!ret) {
395 if (((SPEventContextClass *) parent_class)->root_handler) {
396 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
397 }
398 }
400 return ret;
401 }
403 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
404 {
405 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
407 if (!ac->item) {
409 if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
410 return;
411 }
413 /* Create object */
414 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
415 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
416 repr->setAttribute("sodipodi:type", "arc");
418 /* Set style */
419 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
421 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
422 Inkscape::GC::release(repr);
423 ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
424 ac->item->updateRepr();
426 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
427 }
429 bool ctrl_save = false;
430 if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
431 // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
432 // so that the ellipse is not constrained to integer ratios
433 ctrl_save = true;
434 state = state ^ GDK_CONTROL_MASK;
435 }
436 Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
437 if (ctrl_save) {
438 state = state ^ GDK_CONTROL_MASK;
439 }
441 Geom::Point dir = r.dimensions() / 2;
442 if (state & GDK_MOD1_MASK) {
443 /* With Alt let the ellipse pass through the mouse pointer */
444 Geom::Point c = r.midpoint();
445 if (!ctrl_save) {
446 if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
447 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
448 Geom::Point new_dir = pt * i2d - c;
449 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
450 double lambda = new_dir.length() / dir[Geom::Y];
451 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
452 }
453 } else {
454 /* with Alt+Ctrl (without Shift) we generate a perfect circle
455 with diameter click point <--> mouse pointer */
456 double l = dir.length();
457 Geom::Point d (l, l);
458 r = Geom::Rect (c - d, c + d);
459 }
460 }
462 sp_arc_position_set(SP_ARC(ac->item),
463 r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
464 r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
466 double rdimx = r.dimensions()[Geom::X];
467 double rdimy = r.dimensions()[Geom::Y];
468 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
469 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
470 if (state & GDK_CONTROL_MASK) {
471 int ratio_x, ratio_y;
472 if (fabs (rdimx) > fabs (rdimy)) {
473 ratio_x = (int) rint (rdimx / rdimy);
474 ratio_y = 1;
475 } else {
476 ratio_x = 1;
477 ratio_y = (int) rint (rdimy / rdimx);
478 }
479 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);
480 } else {
481 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);
482 }
483 g_string_free(xs, FALSE);
484 g_string_free(ys, FALSE);
485 }
487 static void sp_arc_finish(SPArcContext *ac)
488 {
489 ac->_message_context->clear();
491 if (ac->item != NULL) {
493 SPGenericEllipse *ge = SP_GENERICELLIPSE(SP_ARC(ac->item));
494 if (ge->rx.computed == 0 || ge->ry.computed == 0) {
495 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
496 return;
497 }
499 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
501 SP_OBJECT(ac->item)->updateRepr();
503 sp_canvas_end_forced_full_redraws(desktop->canvas);
505 sp_desktop_selection(desktop)->set(ac->item);
506 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
507 _("Create ellipse"));
509 ac->item = NULL;
510 }
511 }
513 static void sp_arc_cancel(SPArcContext *ac)
514 {
515 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
517 sp_desktop_selection(desktop)->clear();
518 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
520 if (ac->item != NULL) {
521 SP_OBJECT(ac->item)->deleteObject();
522 ac->item = NULL;
523 }
525 ac->within_tolerance = false;
526 ac->xp = 0;
527 ac->yp = 0;
528 ac->item_to_select = NULL;
530 sp_canvas_end_forced_full_redraws(desktop->canvas);
532 sp_document_cancel(sp_desktop_document(desktop));
533 }
536 /*
537 Local Variables:
538 mode:c++
539 c-file-style:"stroustrup"
540 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
541 indent-tabs-mode:nil
542 fill-column:99
543 End:
544 */
545 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :