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 m.unSetup();
255 }
256 break;
257 case GDK_MOTION_NOTIFY:
258 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
260 if ( event_context->within_tolerance
261 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
262 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
263 break; // do not drag if we're within tolerance from origin
264 }
265 // Once the user has moved farther than tolerance from the original location
266 // (indicating they intend to draw, not click), then always process the
267 // motion notify coordinates as given (no snapping back to origin)
268 event_context->within_tolerance = false;
270 Geom::Point const motion_w(event->motion.x, event->motion.y);
271 Geom::Point motion_dt(desktop->w2d(motion_w));
273 sp_arc_drag(ac, motion_dt, event->motion.state);
275 gobble_motion_events(GDK_BUTTON1_MASK);
277 ret = TRUE;
278 } else if (!sp_event_context_knot_mouseover(ac)){
279 SnapManager &m = desktop->namedview->snap_manager;
280 m.setup(desktop);
282 Geom::Point const motion_w(event->motion.x, event->motion.y);
283 Geom::Point motion_dt(desktop->w2d(motion_w));
284 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
285 m.unSetup();
286 }
287 break;
288 case GDK_BUTTON_RELEASE:
289 event_context->xp = event_context->yp = 0;
290 if (event->button.button == 1 && !event_context->space_panning) {
291 dragging = false;
292 sp_event_context_discard_delayed_snap_event(event_context);
293 if (!event_context->within_tolerance) {
294 // we've been dragging, finish the arc
295 sp_arc_finish(ac);
296 } else if (event_context->item_to_select) {
297 // no dragging, select clicked item if any
298 if (event->button.state & GDK_SHIFT_MASK) {
299 selection->toggle(event_context->item_to_select);
300 } else {
301 selection->set(event_context->item_to_select);
302 }
303 } else {
304 // click in an empty space
305 selection->clear();
306 }
307 event_context->xp = 0;
308 event_context->yp = 0;
309 event_context->item_to_select = NULL;
310 ret = TRUE;
311 }
312 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
313 break;
314 case GDK_KEY_PRESS:
315 switch (get_group0_keyval (&event->key)) {
316 case GDK_Alt_L:
317 case GDK_Alt_R:
318 case GDK_Control_L:
319 case GDK_Control_R:
320 case GDK_Shift_L:
321 case GDK_Shift_R:
322 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
323 case GDK_Meta_R:
324 if (!dragging) {
325 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
326 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
327 _("<b>Shift</b>: draw around the starting point"),
328 NULL);
329 }
330 break;
331 case GDK_Up:
332 case GDK_Down:
333 case GDK_KP_Up:
334 case GDK_KP_Down:
335 // prevent the zoom field from activation
336 if (!MOD__CTRL_ONLY)
337 ret = TRUE;
338 break;
339 case GDK_x:
340 case GDK_X:
341 if (MOD__ALT_ONLY) {
342 desktop->setToolboxFocusTo ("altx-arc");
343 ret = TRUE;
344 }
345 break;
346 case GDK_Escape:
347 if (dragging) {
348 dragging = false;
349 sp_event_context_discard_delayed_snap_event(event_context);
350 // if drawing, cancel, otherwise pass it up for deselecting
351 sp_arc_cancel(ac);
352 ret = TRUE;
353 }
354 break;
355 case GDK_space:
356 if (dragging) {
357 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
358 event->button.time);
359 dragging = false;
360 sp_event_context_discard_delayed_snap_event(event_context);
361 if (!event_context->within_tolerance) {
362 // we've been dragging, finish the arc
363 sp_arc_finish(ac);
364 }
365 // do not return true, so that space would work switching to selector
366 }
367 break;
369 default:
370 break;
371 }
372 break;
373 case GDK_KEY_RELEASE:
374 switch (event->key.keyval) {
375 case GDK_Alt_L:
376 case GDK_Alt_R:
377 case GDK_Control_L:
378 case GDK_Control_R:
379 case GDK_Shift_L:
380 case GDK_Shift_R:
381 case GDK_Meta_L: // Meta is when you press Shift+Alt
382 case GDK_Meta_R:
383 event_context->defaultMessageContext()->clear();
384 break;
385 default:
386 break;
387 }
388 break;
389 default:
390 break;
391 }
393 if (!ret) {
394 if (((SPEventContextClass *) parent_class)->root_handler) {
395 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
396 }
397 }
399 return ret;
400 }
402 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
403 {
404 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
406 if (!ac->item) {
408 if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
409 return;
410 }
412 /* Create object */
413 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
414 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
415 repr->setAttribute("sodipodi:type", "arc");
417 /* Set style */
418 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
420 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
421 Inkscape::GC::release(repr);
422 ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
423 ac->item->updateRepr();
425 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
426 }
428 bool ctrl_save = false;
429 if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
430 // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
431 // so that the ellipse is not constrained to integer ratios
432 ctrl_save = true;
433 state = state ^ GDK_CONTROL_MASK;
434 }
435 Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
436 if (ctrl_save) {
437 state = state ^ GDK_CONTROL_MASK;
438 }
440 Geom::Point dir = r.dimensions() / 2;
441 if (state & GDK_MOD1_MASK) {
442 /* With Alt let the ellipse pass through the mouse pointer */
443 Geom::Point c = r.midpoint();
444 if (!ctrl_save) {
445 if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
446 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
447 Geom::Point new_dir = pt * i2d - c;
448 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
449 double lambda = new_dir.length() / dir[Geom::Y];
450 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
451 }
452 } else {
453 /* with Alt+Ctrl (without Shift) we generate a perfect circle
454 with diameter click point <--> mouse pointer */
455 double l = dir.length();
456 Geom::Point d (l, l);
457 r = Geom::Rect (c - d, c + d);
458 }
459 }
461 sp_arc_position_set(SP_ARC(ac->item),
462 r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
463 r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
465 double rdimx = r.dimensions()[Geom::X];
466 double rdimy = r.dimensions()[Geom::Y];
467 GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
468 GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
469 if (state & GDK_CONTROL_MASK) {
470 int ratio_x, ratio_y;
471 if (fabs (rdimx) > fabs (rdimy)) {
472 ratio_x = (int) rint (rdimx / rdimy);
473 ratio_y = 1;
474 } else {
475 ratio_x = 1;
476 ratio_y = (int) rint (rdimy / rdimx);
477 }
478 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);
479 } else {
480 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);
481 }
482 g_string_free(xs, FALSE);
483 g_string_free(ys, FALSE);
484 }
486 static void sp_arc_finish(SPArcContext *ac)
487 {
488 ac->_message_context->clear();
490 if (ac->item != NULL) {
492 SPGenericEllipse *ge = SP_GENERICELLIPSE(SP_ARC(ac->item));
493 if (ge->rx.computed == 0 || ge->ry.computed == 0) {
494 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
495 return;
496 }
498 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
500 SP_OBJECT(ac->item)->updateRepr();
502 sp_canvas_end_forced_full_redraws(desktop->canvas);
504 sp_desktop_selection(desktop)->set(ac->item);
505 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
506 _("Create ellipse"));
508 ac->item = NULL;
509 }
510 }
512 static void sp_arc_cancel(SPArcContext *ac)
513 {
514 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
516 sp_desktop_selection(desktop)->clear();
517 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
519 if (ac->item != NULL) {
520 SP_OBJECT(ac->item)->deleteObject();
521 ac->item = NULL;
522 }
524 ac->within_tolerance = false;
525 ac->xp = 0;
526 ac->yp = 0;
527 ac->item_to_select = NULL;
529 sp_canvas_end_forced_full_redraws(desktop->canvas);
531 sp_document_cancel(sp_desktop_document(desktop));
532 }
535 /*
536 Local Variables:
537 mode:c++
538 c-file-style:"stroustrup"
539 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
540 indent-tabs-mode:nil
541 fill-column:99
542 End:
543 */
544 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :