1 #define __SP_ARC_CONTEXT_C__
3 /** \file Ellipse drawing context. */
5 /*
6 * Authors:
7 * Mitsuru Oka
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12 * Copyright (C) 2002 Mitsuru Oka
13 * Copyright (C) 2000-2002 Lauris Kaplinski
14 * Copyright (C) 2000-2001 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <gdk/gdkkeysyms.h>
24 #include "macros.h"
25 #include <glibmm/i18n.h>
26 #include "display/sp-canvas.h"
27 #include "sp-ellipse.h"
28 #include "document.h"
29 #include "sp-namedview.h"
30 #include "selection.h"
31 #include "desktop-handles.h"
32 #include "snap.h"
33 #include "pixmaps/cursor-ellipse.xpm"
34 #include "pixmaps/cursor-ellipse.pixbuf"
35 #include "sp-metrics.h"
36 #include "xml/repr.h"
37 #include "xml/node-event-vector.h"
38 #include "object-edit.h"
39 #include "prefs-utils.h"
40 #include "message-context.h"
41 #include "desktop.h"
42 #include "desktop-style.h"
43 #include "context-fns.h"
44 #include "verbs.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, NR::Point pt, guint state);
57 static void sp_arc_finish(SPArcContext *ec);
59 static SPEventContextClass *parent_class;
61 GtkType sp_arc_context_get_type()
62 {
63 static GType type = 0;
64 if (!type) {
65 GTypeInfo info = {
66 sizeof(SPArcContextClass),
67 NULL, NULL,
68 (GClassInitFunc) sp_arc_context_class_init,
69 NULL, NULL,
70 sizeof(SPArcContext),
71 4,
72 (GInstanceInitFunc) sp_arc_context_init,
73 NULL, /* value_table */
74 };
75 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
76 }
77 return type;
78 }
80 static void sp_arc_context_class_init(SPArcContextClass *klass)
81 {
83 GObjectClass *object_class = (GObjectClass *) klass;
84 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
86 parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
88 object_class->dispose = sp_arc_context_dispose;
90 event_context_class->setup = sp_arc_context_setup;
91 event_context_class->root_handler = sp_arc_context_root_handler;
92 event_context_class->item_handler = sp_arc_context_item_handler;
93 }
95 static void sp_arc_context_init(SPArcContext *arc_context)
96 {
97 SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
99 event_context->cursor_shape = cursor_ellipse_xpm;
100 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
101 -1,
102 cursor_ellipse_pixbuf,
103 FALSE,
104 NULL);
105 event_context->hot_x = 4;
106 event_context->hot_y = 4;
107 event_context->xp = 0;
108 event_context->yp = 0;
109 event_context->tolerance = 0;
110 event_context->within_tolerance = false;
111 event_context->item_to_select = NULL;
113 event_context->shape_repr = NULL;
114 event_context->shape_knot_holder = NULL;
116 arc_context->item = NULL;
118 new (&arc_context->sel_changed_connection) sigc::connection();
119 }
121 static void sp_arc_context_dispose(GObject *object)
122 {
123 SPEventContext *ec = SP_EVENT_CONTEXT(object);
124 SPArcContext *ac = SP_ARC_CONTEXT(object);
126 ec->enableGrDrag(false);
128 ac->sel_changed_connection.disconnect();
129 ac->sel_changed_connection.~connection();
131 if (ec->shape_knot_holder) {
132 sp_knot_holder_destroy(ec->shape_knot_holder);
133 ec->shape_knot_holder = NULL;
134 }
136 if (ec->shape_repr) { // remove old listener
137 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
138 Inkscape::GC::release(ec->shape_repr);
139 ec->shape_repr = 0;
140 }
142 /* fixme: This is necessary because we do not grab */
143 if (ac->item) {
144 sp_arc_finish(ac);
145 }
147 delete ac->_message_context;
149 G_OBJECT_CLASS(parent_class)->dispose(object);
150 }
152 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
153 NULL, /* child_added */
154 NULL, /* child_removed */
155 ec_shape_event_attr_changed,
156 NULL, /* content_changed */
157 NULL /* order_changed */
158 };
160 /**
161 \brief Callback that processes the "changed" signal on the selection;
162 destroys old and creates new knotholder.
163 */
164 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
165 {
166 SPArcContext *ac = SP_ARC_CONTEXT(data);
167 SPEventContext *ec = SP_EVENT_CONTEXT(ac);
169 if (ec->shape_knot_holder) { // desktroy knotholder
170 sp_knot_holder_destroy(ec->shape_knot_holder);
171 ec->shape_knot_holder = NULL;
172 }
174 if (ec->shape_repr) { // remove old listener
175 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
176 Inkscape::GC::release(ec->shape_repr);
177 ec->shape_repr = 0;
178 }
180 SPItem *item = selection ? selection->singleItem() : NULL;
181 if (item) {
182 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
183 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
184 if (shape_repr) {
185 ec->shape_repr = shape_repr;
186 Inkscape::GC::anchor(shape_repr);
187 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
188 }
189 }
190 }
192 static void sp_arc_context_setup(SPEventContext *ec)
193 {
194 SPArcContext *ac = SP_ARC_CONTEXT(ec);
195 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
197 if (((SPEventContextClass *) parent_class)->setup) {
198 ((SPEventContextClass *) parent_class)->setup(ec);
199 }
201 SPItem *item = selection->singleItem();
203 if (item) {
204 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
205 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
206 if (shape_repr) {
207 ec->shape_repr = shape_repr;
208 Inkscape::GC::anchor(shape_repr);
209 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
210 }
211 }
213 ac->sel_changed_connection.disconnect();
214 ac->sel_changed_connection = selection->connectChanged(
215 sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
216 );
218 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
219 ec->enableSelectionCue();
220 }
222 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
223 ec->enableGrDrag();
224 }
226 ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
227 }
229 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
230 {
231 SPDesktop *desktop = event_context->desktop;
232 gint ret = FALSE;
234 switch (event->type) {
235 case GDK_BUTTON_PRESS:
236 if (event->button.button == 1) {
237 Inkscape::setup_for_drag_start(desktop, event_context, event);
238 ret = TRUE;
239 }
240 break;
241 // motion and release are always on root (why?)
242 default:
243 break;
244 }
246 if (((SPEventContextClass *) parent_class)->item_handler) {
247 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
248 }
250 return ret;
251 }
253 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
254 {
255 static bool dragging;
257 SPDesktop *desktop = event_context->desktop;
258 Inkscape::Selection *selection = sp_desktop_selection(desktop);
259 SPArcContext *ac = SP_ARC_CONTEXT(event_context);
261 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
263 gint ret = FALSE;
265 switch (event->type) {
266 case GDK_BUTTON_PRESS:
267 if (event->button.button == 1) {
269 dragging = true;
270 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
272 SnapManager const &m = desktop->namedview->snap_manager;
273 ac->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, ac->center, ac->item).getPoint();
274 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
275 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
276 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
277 NULL, event->button.time);
278 ret = TRUE;
279 }
280 break;
281 case GDK_MOTION_NOTIFY:
282 if (dragging && event->motion.state && GDK_BUTTON1_MASK) {
284 if ( event_context->within_tolerance
285 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
286 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
287 break; // do not drag if we're within tolerance from origin
288 }
289 // Once the user has moved farther than tolerance from the original location
290 // (indicating they intend to draw, not click), then always process the
291 // motion notify coordinates as given (no snapping back to origin)
292 event_context->within_tolerance = false;
294 NR::Point const motion_w(event->motion.x, event->motion.y);
295 NR::Point const motion_dt(desktop->w2d(motion_w));
296 sp_arc_drag(ac, motion_dt, event->motion.state);
297 ret = TRUE;
298 }
299 break;
300 case GDK_BUTTON_RELEASE:
301 event_context->xp = event_context->yp = 0;
302 if (event->button.button == 1) {
303 dragging = false;
304 if (!event_context->within_tolerance) {
305 // we've been dragging, finish the arc
306 sp_arc_finish(ac);
307 } else if (event_context->item_to_select) {
308 // no dragging, select clicked item if any
309 if (event->button.state & GDK_SHIFT_MASK) {
310 selection->toggle(event_context->item_to_select);
311 } else {
312 selection->set(event_context->item_to_select);
313 }
314 } else {
315 // click in an empty space
316 selection->clear();
317 }
318 event_context->xp = 0;
319 event_context->yp = 0;
320 event_context->item_to_select = NULL;
321 ret = TRUE;
322 }
323 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
324 break;
325 case GDK_KEY_PRESS:
326 switch (get_group0_keyval (&event->key)) {
327 case GDK_Alt_L:
328 case GDK_Alt_R:
329 case GDK_Control_L:
330 case GDK_Control_R:
331 case GDK_Shift_L:
332 case GDK_Shift_R:
333 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
334 case GDK_Meta_R:
335 if (!dragging) {
336 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
337 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
338 _("<b>Shift</b>: draw around the starting point"),
339 NULL);
340 }
341 break;
342 case GDK_Up:
343 case GDK_Down:
344 case GDK_KP_Up:
345 case GDK_KP_Down:
346 // prevent the zoom field from activation
347 if (!MOD__CTRL_ONLY)
348 ret = TRUE;
349 break;
350 case GDK_x:
351 case GDK_X:
352 if (MOD__ALT_ONLY) {
353 desktop->setToolboxFocusTo ("altx-arc");
354 ret = TRUE;
355 }
356 break;
357 case GDK_Escape:
358 sp_desktop_selection(desktop)->clear();
359 //TODO: make dragging escapable by Esc
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, NR::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::Node *repr = sp_repr_new("svg:path");
405 repr->setAttribute("sodipodi:type", "arc");
407 /* Set style */
408 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
410 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
411 Inkscape::GC::release(repr);
412 ac->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
413 ac->item->updateRepr();
415 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
416 }
418 NR::Rect const r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
420 sp_arc_position_set(SP_ARC(ac->item),
421 r.midpoint()[NR::X], r.midpoint()[NR::Y],
422 r.dimensions()[NR::X] / 2, r.dimensions()[NR::Y] / 2);
424 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
425 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
426 ac->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Ellipse</b>: %s × %s; with <b>Ctrl</b> to make circle or integer-ratio ellipse; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
427 g_string_free(xs, FALSE);
428 g_string_free(ys, FALSE);
429 }
431 static void sp_arc_finish(SPArcContext *ac)
432 {
433 ac->_message_context->clear();
435 if (ac->item != NULL) {
436 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
438 SP_OBJECT(ac->item)->updateRepr();
440 sp_canvas_end_forced_full_redraws(desktop->canvas);
442 sp_desktop_selection(desktop)->set(ac->item);
443 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
444 _("Create ellipse"));
446 ac->item = NULL;
447 }
448 }
451 /*
452 Local Variables:
453 mode:c++
454 c-file-style:"stroustrup"
455 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
456 indent-tabs-mode:nil
457 fill-column:99
458 End:
459 */
460 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :