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) 2000-2002 Lauris Kaplinski
12 * Copyright (C) 2000-2001 Ximian, Inc.
13 * Copyright (C) 2002 Mitsuru Oka
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
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 "prefs-utils.h"
38 #include "message-context.h"
39 #include "desktop.h"
40 #include "desktop-style.h"
41 #include "context-fns.h"
43 #include "arc-context.h"
45 static void sp_arc_context_class_init(SPArcContextClass *klass);
46 static void sp_arc_context_init(SPArcContext *arc_context);
47 static void sp_arc_context_dispose(GObject *object);
49 static void sp_arc_context_setup(SPEventContext *ec);
50 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void sp_arc_drag(SPArcContext *ec, NR::Point pt, guint state);
54 static void sp_arc_finish(SPArcContext *ec);
56 static SPEventContextClass *parent_class;
58 GtkType sp_arc_context_get_type()
59 {
60 static GType type = 0;
61 if (!type) {
62 GTypeInfo info = {
63 sizeof(SPArcContextClass),
64 NULL, NULL,
65 (GClassInitFunc) sp_arc_context_class_init,
66 NULL, NULL,
67 sizeof(SPArcContext),
68 4,
69 (GInstanceInitFunc) sp_arc_context_init,
70 NULL, /* value_table */
71 };
72 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
73 }
74 return type;
75 }
77 static void sp_arc_context_class_init(SPArcContextClass *klass)
78 {
80 GObjectClass *object_class = (GObjectClass *) klass;
81 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83 parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
85 object_class->dispose = sp_arc_context_dispose;
87 event_context_class->setup = sp_arc_context_setup;
88 event_context_class->root_handler = sp_arc_context_root_handler;
89 event_context_class->item_handler = sp_arc_context_item_handler;
90 }
92 static void sp_arc_context_init(SPArcContext *arc_context)
93 {
94 SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
96 event_context->cursor_shape = cursor_ellipse_xpm;
97 event_context->hot_x = 4;
98 event_context->hot_y = 4;
99 event_context->xp = 0;
100 event_context->yp = 0;
101 event_context->tolerance = 0;
102 event_context->within_tolerance = false;
103 event_context->item_to_select = NULL;
105 event_context->shape_repr = NULL;
106 event_context->shape_knot_holder = NULL;
108 arc_context->item = NULL;
110 new (&arc_context->sel_changed_connection) sigc::connection();
111 }
113 static void sp_arc_context_dispose(GObject *object)
114 {
115 SPEventContext *ec = SP_EVENT_CONTEXT(object);
116 SPArcContext *ac = SP_ARC_CONTEXT(object);
118 ec->enableGrDrag(false);
120 ac->sel_changed_connection.disconnect();
121 ac->sel_changed_connection.~connection();
123 if (ec->shape_knot_holder) {
124 sp_knot_holder_destroy(ec->shape_knot_holder);
125 ec->shape_knot_holder = NULL;
126 }
128 if (ec->shape_repr) { // remove old listener
129 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
130 Inkscape::GC::release(ec->shape_repr);
131 ec->shape_repr = 0;
132 }
134 /* fixme: This is necessary because we do not grab */
135 if (ac->item) {
136 sp_arc_finish(ac);
137 }
139 delete ac->_message_context;
141 G_OBJECT_CLASS(parent_class)->dispose(object);
142 }
144 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
145 NULL, /* child_added */
146 NULL, /* child_removed */
147 ec_shape_event_attr_changed,
148 NULL, /* content_changed */
149 NULL /* order_changed */
150 };
152 /**
153 \brief Callback that processes the "changed" signal on the selection;
154 destroys old and creates new knotholder.
155 */
156 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
157 {
158 SPArcContext *ac = SP_ARC_CONTEXT(data);
159 SPEventContext *ec = SP_EVENT_CONTEXT(ac);
161 if (ec->shape_knot_holder) { // desktroy knotholder
162 sp_knot_holder_destroy(ec->shape_knot_holder);
163 ec->shape_knot_holder = NULL;
164 }
166 if (ec->shape_repr) { // remove old listener
167 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
168 Inkscape::GC::release(ec->shape_repr);
169 ec->shape_repr = 0;
170 }
172 SPItem *item = selection ? selection->singleItem() : NULL;
173 if (item) {
174 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
175 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
176 if (shape_repr) {
177 ec->shape_repr = shape_repr;
178 Inkscape::GC::anchor(shape_repr);
179 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
180 sp_repr_synthesize_events(shape_repr, &ec_shape_repr_events, ec);
181 }
182 }
183 }
185 static void sp_arc_context_setup(SPEventContext *ec)
186 {
187 SPArcContext *ac = SP_ARC_CONTEXT(ec);
188 Inkscape::Selection *selection = SP_DT_SELECTION(ec->desktop);
190 if (((SPEventContextClass *) parent_class)->setup) {
191 ((SPEventContextClass *) parent_class)->setup(ec);
192 }
194 SPItem *item = selection->singleItem();
196 if (item) {
197 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
198 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
199 if (shape_repr) {
200 ec->shape_repr = shape_repr;
201 Inkscape::GC::anchor(shape_repr);
202 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
203 sp_repr_synthesize_events(shape_repr, &ec_shape_repr_events, ec);
204 }
205 }
207 ac->sel_changed_connection.disconnect();
208 ac->sel_changed_connection = selection->connectChanged(
209 sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
210 );
212 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
213 ec->enableSelectionCue();
214 }
216 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
217 ec->enableGrDrag();
218 }
220 ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
221 }
223 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
224 {
225 SPDesktop *desktop = event_context->desktop;
226 gint ret = FALSE;
228 switch (event->type) {
229 case GDK_BUTTON_PRESS:
230 if (event->button.button == 1) {
231 Inkscape::setup_for_drag_start(desktop, event_context, event);
232 ret = TRUE;
233 }
234 break;
235 // motion and release are always on root (why?)
236 default:
237 break;
238 }
240 if (((SPEventContextClass *) parent_class)->item_handler) {
241 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
242 }
244 return ret;
245 }
247 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
248 {
249 static bool dragging;
251 SPDesktop *desktop = event_context->desktop;
252 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
253 SPArcContext *ac = SP_ARC_CONTEXT(event_context);
255 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
257 gint ret = FALSE;
259 switch (event->type) {
260 case GDK_BUTTON_PRESS:
261 if (event->button.button == 1) {
263 dragging = true;
264 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
266 SnapManager const m(desktop->namedview);
267 ac->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, ac->center, ac->item).getPoint();
268 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
269 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
270 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
271 NULL, event->button.time);
272 ret = TRUE;
273 }
274 break;
275 case GDK_MOTION_NOTIFY:
276 if (dragging && event->motion.state && GDK_BUTTON1_MASK) {
278 if ( event_context->within_tolerance
279 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
280 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
281 break; // do not drag if we're within tolerance from origin
282 }
283 // Once the user has moved farther than tolerance from the original location
284 // (indicating they intend to draw, not click), then always process the
285 // motion notify coordinates as given (no snapping back to origin)
286 event_context->within_tolerance = false;
288 NR::Point const motion_w(event->motion.x, event->motion.y);
289 NR::Point const motion_dt(desktop->w2d(motion_w));
290 sp_arc_drag(ac, motion_dt, event->motion.state);
291 ret = TRUE;
292 }
293 break;
294 case GDK_BUTTON_RELEASE:
295 event_context->xp = event_context->yp = 0;
296 if (event->button.button == 1) {
297 dragging = false;
298 if (!event_context->within_tolerance) {
299 // we've been dragging, finish the arc
300 sp_arc_finish(ac);
301 } else if (event_context->item_to_select) {
302 // no dragging, select clicked item if any
303 if (event->button.state & GDK_SHIFT_MASK) {
304 selection->toggle(event_context->item_to_select);
305 } else {
306 selection->set(event_context->item_to_select);
307 }
308 } else {
309 // click in an empty space
310 selection->clear();
311 }
312 event_context->xp = 0;
313 event_context->yp = 0;
314 event_context->item_to_select = NULL;
315 ret = TRUE;
316 }
317 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
318 break;
319 case GDK_KEY_PRESS:
320 switch (get_group0_keyval (&event->key)) {
321 case GDK_Alt_L:
322 case GDK_Alt_R:
323 case GDK_Control_L:
324 case GDK_Control_R:
325 case GDK_Shift_L:
326 case GDK_Shift_R:
327 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
328 case GDK_Meta_R:
329 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
330 _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
331 _("<b>Shift</b>: draw around the starting point"),
332 NULL);
333 break;
334 case GDK_Up:
335 case GDK_Down:
336 case GDK_KP_Up:
337 case GDK_KP_Down:
338 // prevent the zoom field from activation
339 if (!MOD__CTRL_ONLY)
340 ret = TRUE;
341 break;
342 case GDK_x:
343 case GDK_X:
344 if (MOD__ALT_ONLY) {
345 desktop->setToolboxFocusTo ("altx-arc");
346 ret = TRUE;
347 }
348 break;
349 case GDK_Escape:
350 SP_DT_SELECTION(desktop)->clear();
351 //TODO: make dragging escapable by Esc
352 default:
353 break;
354 }
355 break;
356 case GDK_KEY_RELEASE:
357 switch (event->key.keyval) {
358 case GDK_Alt_L:
359 case GDK_Alt_R:
360 case GDK_Control_L:
361 case GDK_Control_R:
362 case GDK_Shift_L:
363 case GDK_Shift_R:
364 case GDK_Meta_L: // Meta is when you press Shift+Alt
365 case GDK_Meta_R:
366 event_context->defaultMessageContext()->clear();
367 break;
368 default:
369 break;
370 }
371 break;
372 default:
373 break;
374 }
376 if (!ret) {
377 if (((SPEventContextClass *) parent_class)->root_handler) {
378 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
379 }
380 }
382 return ret;
383 }
385 static void sp_arc_drag(SPArcContext *ac, NR::Point pt, guint state)
386 {
387 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
389 if (!ac->item) {
391 if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
392 return;
393 }
395 /* Create object */
396 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
397 repr->setAttribute("sodipodi:type", "arc");
399 /* Set style */
400 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
402 ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
403 Inkscape::GC::release(repr);
404 ac->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
405 ac->item->updateRepr();
406 }
408 NR::Rect const r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
410 sp_arc_position_set(SP_ARC(ac->item),
411 r.midpoint()[NR::X], r.midpoint()[NR::Y],
412 r.dimensions()[NR::X] / 2, r.dimensions()[NR::Y] / 2);
414 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
415 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
416 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);
417 g_string_free(xs, FALSE);
418 g_string_free(ys, FALSE);
419 }
421 static void sp_arc_finish(SPArcContext *ac)
422 {
423 ac->_message_context->clear();
425 if (ac->item != NULL) {
426 SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
428 SP_OBJECT(ac->item)->updateRepr();
430 SP_DT_SELECTION(desktop)->set(ac->item);
431 sp_document_done(SP_DT_DOCUMENT(desktop));
433 ac->item = NULL;
434 }
435 }
438 /*
439 Local Variables:
440 mode:c++
441 c-file-style:"stroustrup"
442 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
443 indent-tabs-mode:nil
444 fill-column:99
445 End:
446 */
447 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :