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