1 #define __SP_STAR_CONTEXT_C__
3 /*
4 * Star drawing context
5 *
6 * Authors:
7 * Mitsuru Oka <oka326@parkcity.ne.jp>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 1999-2002 Lauris Kaplinski
12 * Copyright (C) 2001-2002 Mitsuru Oka
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #include "config.h"
19 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-star.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "desktop-affine.h"
29 #include "snap.h"
30 #include "desktop.h"
31 #include "desktop-style.h"
32 #include "message-context.h"
33 #include "pixmaps/cursor-star.xpm"
34 #include "sp-metrics.h"
35 #include <glibmm/i18n.h>
36 #include "prefs-utils.h"
37 #include "xml/repr.h"
38 #include "xml/node-event-vector.h"
39 #include "object-edit.h"
40 #include "context-fns.h"
42 #include "star-context.h"
44 static void sp_star_context_class_init (SPStarContextClass * klass);
45 static void sp_star_context_init (SPStarContext * star_context);
46 static void sp_star_context_dispose (GObject *object);
48 static void sp_star_context_setup (SPEventContext *ec);
49 static void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
50 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
52 static void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
53 static void sp_star_finish (SPStarContext * sc);
55 static SPEventContextClass * parent_class;
57 GtkType
58 sp_star_context_get_type (void)
59 {
60 static GType type = 0;
61 if (!type) {
62 GTypeInfo info = {
63 sizeof (SPStarContextClass),
64 NULL, NULL,
65 (GClassInitFunc) sp_star_context_class_init,
66 NULL, NULL,
67 sizeof (SPStarContext),
68 4,
69 (GInstanceInitFunc) sp_star_context_init,
70 NULL, /* value_table */
71 };
72 type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
73 }
74 return type;
75 }
77 static void
78 sp_star_context_class_init (SPStarContextClass * klass)
79 {
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_star_context_dispose;
87 event_context_class->setup = sp_star_context_setup;
88 event_context_class->set = sp_star_context_set;
89 event_context_class->root_handler = sp_star_context_root_handler;
90 }
92 static void
93 sp_star_context_init (SPStarContext * star_context)
94 {
95 SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
97 event_context->cursor_shape = cursor_star_xpm;
98 event_context->hot_x = 4;
99 event_context->hot_y = 4;
100 event_context->xp = 0;
101 event_context->yp = 0;
102 event_context->tolerance = 0;
103 event_context->within_tolerance = false;
104 event_context->item_to_select = NULL;
106 event_context->shape_repr = NULL;
107 event_context->shape_knot_holder = NULL;
109 star_context->item = NULL;
111 star_context->magnitude = 5;
112 star_context->proportion = 0.5;
113 star_context->isflatsided = false;
115 new (&star_context->sel_changed_connection) sigc::connection();
116 }
118 static void
119 sp_star_context_dispose (GObject *object)
120 {
121 SPEventContext *ec = SP_EVENT_CONTEXT (object);
122 SPStarContext *sc = SP_STAR_CONTEXT (object);
124 ec->enableGrDrag(false);
126 sc->sel_changed_connection.disconnect();
127 sc->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 (sc->item) sp_star_finish (sc);
143 if (sc->_message_context) {
144 delete sc->_message_context;
145 }
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 \param selection Should not be NULL.
162 */
163 void
164 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
165 {
166 g_assert (selection != NULL);
168 SPStarContext *sc = SP_STAR_CONTEXT (data);
169 SPEventContext *ec = SP_EVENT_CONTEXT (sc);
171 if (ec->shape_knot_holder) { // desktroy knotholder
172 sp_knot_holder_destroy (ec->shape_knot_holder);
173 ec->shape_knot_holder = NULL;
174 }
176 if (ec->shape_repr) { // remove old listener
177 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
178 Inkscape::GC::release(ec->shape_repr);
179 ec->shape_repr = 0;
180 }
182 SPItem *item = selection->singleItem();
183 if (item) {
184 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
185 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
186 if (shape_repr) {
187 ec->shape_repr = shape_repr;
188 Inkscape::GC::anchor(shape_repr);
189 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
190 sp_repr_synthesize_events (shape_repr, &ec_shape_repr_events, ec);
191 }
192 }
193 }
195 static void
196 sp_star_context_setup (SPEventContext *ec)
197 {
198 SPStarContext *sc = SP_STAR_CONTEXT (ec);
200 if (((SPEventContextClass *) parent_class)->setup)
201 ((SPEventContextClass *) parent_class)->setup (ec);
203 sp_event_context_read (ec, "magnitude");
204 sp_event_context_read (ec, "proportion");
205 sp_event_context_read (ec, "isflatsided");
206 sp_event_context_read (ec, "rounded");
207 sp_event_context_read (ec, "randomized");
209 Inkscape::Selection *selection = SP_DT_SELECTION(ec->desktop);
211 SPItem *item = selection->singleItem();
212 if (item) {
213 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
214 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
215 if (shape_repr) {
216 ec->shape_repr = shape_repr;
217 Inkscape::GC::anchor(shape_repr);
218 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
219 sp_repr_synthesize_events (shape_repr, &ec_shape_repr_events, ec);
220 }
221 }
223 sc->sel_changed_connection.disconnect();
224 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
226 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
227 ec->enableSelectionCue();
228 }
230 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
231 ec->enableGrDrag();
232 }
234 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
235 }
237 static void
238 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
239 {
240 SPStarContext *sc = SP_STAR_CONTEXT (ec);
241 if (!strcmp (key, "magnitude")) {
242 sc->magnitude = (val) ? atoi (val) : 5;
243 sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
244 } else if (!strcmp (key, "proportion")) {
245 sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
246 sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
247 } else if (!strcmp (key, "isflatsided")) {
248 if (val && !strcmp(val, "true"))
249 sc->isflatsided = true;
250 else
251 sc->isflatsided = false;
252 } else if (!strcmp (key, "rounded")) {
253 sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
254 } else if (!strcmp (key, "randomized")) {
255 sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
256 }
257 }
259 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
260 {
261 static gboolean dragging;
263 SPDesktop *desktop = event_context->desktop;
264 Inkscape::Selection *selection = SP_DT_SELECTION (desktop);
266 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
268 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
270 gint ret = FALSE;
272 switch (event->type) {
273 case GDK_BUTTON_PRESS:
274 if (event->button.button == 1) {
276 dragging = TRUE;
278 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
280 SnapManager const m(desktop->namedview);
281 sc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
283 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
284 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
285 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
286 NULL, event->button.time);
287 ret = TRUE;
288 }
289 break;
290 case GDK_MOTION_NOTIFY:
291 if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
293 if ( event_context->within_tolerance
294 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
295 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
296 break; // do not drag if we're within tolerance from origin
297 }
298 // Once the user has moved farther than tolerance from the original location
299 // (indicating they intend to draw, not click), then always process the
300 // motion notify coordinates as given (no snapping back to origin)
301 event_context->within_tolerance = false;
303 NR::Point const motion_w(event->motion.x, event->motion.y);
304 NR::Point const motion_dt(event_context->desktop->w2d(motion_w));
305 sp_star_drag (sc, motion_dt, event->motion.state);
306 ret = TRUE;
307 }
308 break;
309 case GDK_BUTTON_RELEASE:
310 event_context->xp = event_context->yp = 0;
311 if (event->button.button == 1) {
312 dragging = FALSE;
313 if (!event_context->within_tolerance) {
314 // we've been dragging, finish the star
315 sp_star_finish (sc);
316 } else if (event_context->item_to_select) {
317 // no dragging, select clicked item if any
318 if (event->button.state & GDK_SHIFT_MASK) {
319 selection->toggle(event_context->item_to_select);
320 } else {
321 selection->set(event_context->item_to_select);
322 }
323 } else {
324 // click in an empty space
325 selection->clear();
326 }
328 event_context->item_to_select = NULL;
329 ret = TRUE;
330 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
331 }
332 break;
333 case GDK_KEY_PRESS:
334 switch (get_group0_keyval(&event->key)) {
335 case GDK_Alt_R:
336 case GDK_Control_L:
337 case GDK_Control_R:
338 case GDK_Shift_L:
339 case GDK_Shift_R:
340 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
341 case GDK_Meta_R:
342 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
343 _("<b>Ctrl</b>: snap angle; keep rays radial"),
344 NULL,
345 NULL);
346 break;
347 case GDK_Up:
348 case GDK_Down:
349 case GDK_KP_Up:
350 case GDK_KP_Down:
351 // prevent the zoom field from activation
352 if (!MOD__CTRL_ONLY)
353 ret = TRUE;
354 break;
355 case GDK_x:
356 case GDK_X:
357 if (MOD__ALT_ONLY) {
358 desktop->setToolboxFocusTo ("altx-star");
359 ret = TRUE;
360 }
361 break;
362 case GDK_Escape:
363 SP_DT_SELECTION(desktop)->clear();
364 //TODO: make dragging escapable by Esc
365 default:
366 break;
367 }
368 break;
369 case GDK_KEY_RELEASE:
370 switch (get_group0_keyval (&event->key)) {
371 case GDK_Alt_L:
372 case GDK_Alt_R:
373 case GDK_Control_L:
374 case GDK_Control_R:
375 case GDK_Shift_L:
376 case GDK_Shift_R:
377 case GDK_Meta_L: // Meta is when you press Shift+Alt
378 case GDK_Meta_R:
379 event_context->defaultMessageContext()->clear();
380 break;
381 default:
382 break;
383 }
384 break;
385 default:
386 break;
387 }
389 if (!ret) {
390 if (((SPEventContextClass *) parent_class)->root_handler)
391 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
392 }
394 return ret;
395 }
397 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
398 {
399 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
401 int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
403 if (!sc->item) {
405 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
406 return;
407 }
409 /* Create object */
410 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
411 repr->setAttribute("sodipodi:type", "star");
413 /* Set style */
414 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
416 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
417 Inkscape::GC::release(repr);
418 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
419 sc->item->updateRepr();
420 }
422 NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
423 NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
425 /* Snap corner point with no constraints */
426 SnapManager const m(desktop->namedview);;
427 p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
429 SPStar *star = SP_STAR(sc->item);
431 double const sides = (gdouble) sc->magnitude;
432 NR::Point const d = p1 - p0;
433 NR::Coord const r1 = NR::L2(d);
434 double arg1 = atan2(d);
436 if (state & GDK_CONTROL_MASK) {
437 /* Snap angle */
438 arg1 = sp_round(arg1, M_PI / snaps);
439 }
441 sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
442 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
444 /* status text */
445 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
446 sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
447 ( sc->isflatsided?
448 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
449 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
450 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
452 g_string_free(rads, FALSE);
453 }
455 static void
456 sp_star_finish (SPStarContext * sc)
457 {
458 sc->_message_context->clear();
460 if (sc->item != NULL) {
461 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
462 SPObject *object = SP_OBJECT(sc->item);
464 sp_shape_set_shape(SP_SHAPE(sc->item));
466 object->updateRepr(NULL, SP_OBJECT_WRITE_EXT);
468 SP_DT_SELECTION(desktop)->set(sc->item);
469 sp_document_done(SP_DT_DOCUMENT(desktop));
471 sc->item = NULL;
472 }
473 }
475 /*
476 Local Variables:
477 mode:c++
478 c-file-style:"stroustrup"
479 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
480 indent-tabs-mode:nil
481 fill-column:99
482 End:
483 */
484 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :