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