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 "pixmaps/cursor-star.pixbuf"
35 #include "sp-metrics.h"
36 #include <glibmm/i18n.h>
37 #include "prefs-utils.h"
38 #include "xml/repr.h"
39 #include "xml/node-event-vector.h"
40 #include "object-edit.h"
41 #include "context-fns.h"
43 #include "star-context.h"
45 static void sp_star_context_class_init (SPStarContextClass * klass);
46 static void sp_star_context_init (SPStarContext * star_context);
47 static void sp_star_context_dispose (GObject *object);
49 static void sp_star_context_setup (SPEventContext *ec);
50 static void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
51 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
53 static void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
54 static void sp_star_finish (SPStarContext * sc);
56 static SPEventContextClass * parent_class;
58 GtkType
59 sp_star_context_get_type (void)
60 {
61 static GType type = 0;
62 if (!type) {
63 GTypeInfo info = {
64 sizeof (SPStarContextClass),
65 NULL, NULL,
66 (GClassInitFunc) sp_star_context_class_init,
67 NULL, NULL,
68 sizeof (SPStarContext),
69 4,
70 (GInstanceInitFunc) sp_star_context_init,
71 NULL, /* value_table */
72 };
73 type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
74 }
75 return type;
76 }
78 static void
79 sp_star_context_class_init (SPStarContextClass * klass)
80 {
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_star_context_dispose;
88 event_context_class->setup = sp_star_context_setup;
89 event_context_class->set = sp_star_context_set;
90 event_context_class->root_handler = sp_star_context_root_handler;
91 }
93 static void
94 sp_star_context_init (SPStarContext * star_context)
95 {
96 SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
98 event_context->cursor_shape = cursor_star_xpm;
99 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
100 -1,
101 cursor_star_pixbuf,
102 FALSE,
103 NULL);
104 event_context->hot_x = 4;
105 event_context->hot_y = 4;
106 event_context->xp = 0;
107 event_context->yp = 0;
108 event_context->tolerance = 0;
109 event_context->within_tolerance = false;
110 event_context->item_to_select = NULL;
112 event_context->shape_repr = NULL;
113 event_context->shape_knot_holder = NULL;
115 star_context->item = NULL;
117 star_context->magnitude = 5;
118 star_context->proportion = 0.5;
119 star_context->isflatsided = false;
121 new (&star_context->sel_changed_connection) sigc::connection();
122 }
124 static void
125 sp_star_context_dispose (GObject *object)
126 {
127 SPEventContext *ec = SP_EVENT_CONTEXT (object);
128 SPStarContext *sc = SP_STAR_CONTEXT (object);
130 ec->enableGrDrag(false);
132 sc->sel_changed_connection.disconnect();
133 sc->sel_changed_connection.~connection();
135 if (ec->shape_knot_holder) {
136 sp_knot_holder_destroy (ec->shape_knot_holder);
137 ec->shape_knot_holder = NULL;
138 }
140 if (ec->shape_repr) { // remove old listener
141 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
142 Inkscape::GC::release(ec->shape_repr);
143 ec->shape_repr = 0;
144 }
146 /* fixme: This is necessary because we do not grab */
147 if (sc->item) sp_star_finish (sc);
149 if (sc->_message_context) {
150 delete sc->_message_context;
151 }
153 G_OBJECT_CLASS (parent_class)->dispose (object);
154 }
156 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
157 NULL, /* child_added */
158 NULL, /* child_removed */
159 ec_shape_event_attr_changed,
160 NULL, /* content_changed */
161 NULL /* order_changed */
162 };
164 /**
165 \brief Callback that processes the "changed" signal on the selection;
166 destroys old and creates new knotholder
167 \param selection Should not be NULL.
168 */
169 void
170 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
171 {
172 g_assert (selection != NULL);
174 SPStarContext *sc = SP_STAR_CONTEXT (data);
175 SPEventContext *ec = SP_EVENT_CONTEXT (sc);
177 if (ec->shape_knot_holder) { // desktroy knotholder
178 sp_knot_holder_destroy (ec->shape_knot_holder);
179 ec->shape_knot_holder = NULL;
180 }
182 if (ec->shape_repr) { // remove old listener
183 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
184 Inkscape::GC::release(ec->shape_repr);
185 ec->shape_repr = 0;
186 }
188 SPItem *item = selection->singleItem();
189 if (item) {
190 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
191 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
192 if (shape_repr) {
193 ec->shape_repr = shape_repr;
194 Inkscape::GC::anchor(shape_repr);
195 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
196 }
197 }
198 }
200 static void
201 sp_star_context_setup (SPEventContext *ec)
202 {
203 SPStarContext *sc = SP_STAR_CONTEXT (ec);
205 if (((SPEventContextClass *) parent_class)->setup)
206 ((SPEventContextClass *) parent_class)->setup (ec);
208 sp_event_context_read (ec, "magnitude");
209 sp_event_context_read (ec, "proportion");
210 sp_event_context_read (ec, "isflatsided");
211 sp_event_context_read (ec, "rounded");
212 sp_event_context_read (ec, "randomized");
214 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
216 SPItem *item = selection->singleItem();
217 if (item) {
218 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
219 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
220 if (shape_repr) {
221 ec->shape_repr = shape_repr;
222 Inkscape::GC::anchor(shape_repr);
223 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
224 }
225 }
227 sc->sel_changed_connection.disconnect();
228 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
230 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
231 ec->enableSelectionCue();
232 }
234 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
235 ec->enableGrDrag();
236 }
238 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
239 }
241 static void
242 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
243 {
244 SPStarContext *sc = SP_STAR_CONTEXT (ec);
245 if (!strcmp (key, "magnitude")) {
246 sc->magnitude = (val) ? atoi (val) : 5;
247 sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
248 } else if (!strcmp (key, "proportion")) {
249 sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
250 sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
251 } else if (!strcmp (key, "isflatsided")) {
252 if (val && !strcmp(val, "true"))
253 sc->isflatsided = true;
254 else
255 sc->isflatsided = false;
256 } else if (!strcmp (key, "rounded")) {
257 sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
258 } else if (!strcmp (key, "randomized")) {
259 sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
260 }
261 }
263 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
264 {
265 static gboolean dragging;
267 SPDesktop *desktop = event_context->desktop;
268 Inkscape::Selection *selection = sp_desktop_selection (desktop);
270 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
272 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
274 gint ret = FALSE;
276 switch (event->type) {
277 case GDK_BUTTON_PRESS:
278 if (event->button.button == 1) {
280 dragging = TRUE;
282 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
284 SnapManager const &m = desktop->namedview->snap_manager;
285 sc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
287 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
288 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
289 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
290 NULL, event->button.time);
291 ret = TRUE;
292 }
293 break;
294 case GDK_MOTION_NOTIFY:
295 if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
297 if ( event_context->within_tolerance
298 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
299 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
300 break; // do not drag if we're within tolerance from origin
301 }
302 // Once the user has moved farther than tolerance from the original location
303 // (indicating they intend to draw, not click), then always process the
304 // motion notify coordinates as given (no snapping back to origin)
305 event_context->within_tolerance = false;
307 NR::Point const motion_w(event->motion.x, event->motion.y);
308 NR::Point const motion_dt(event_context->desktop->w2d(motion_w));
309 sp_star_drag (sc, motion_dt, event->motion.state);
310 ret = TRUE;
311 }
312 break;
313 case GDK_BUTTON_RELEASE:
314 event_context->xp = event_context->yp = 0;
315 if (event->button.button == 1) {
316 dragging = FALSE;
317 if (!event_context->within_tolerance) {
318 // we've been dragging, finish the star
319 sp_star_finish (sc);
320 } else if (event_context->item_to_select) {
321 // no dragging, select clicked item if any
322 if (event->button.state & GDK_SHIFT_MASK) {
323 selection->toggle(event_context->item_to_select);
324 } else {
325 selection->set(event_context->item_to_select);
326 }
327 } else {
328 // click in an empty space
329 selection->clear();
330 }
332 event_context->item_to_select = NULL;
333 ret = TRUE;
334 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
335 }
336 break;
337 case GDK_KEY_PRESS:
338 switch (get_group0_keyval(&event->key)) {
339 case GDK_Alt_R:
340 case GDK_Control_L:
341 case GDK_Control_R:
342 case GDK_Shift_L:
343 case GDK_Shift_R:
344 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
345 case GDK_Meta_R:
346 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
347 _("<b>Ctrl</b>: snap angle; keep rays radial"),
348 NULL,
349 NULL);
350 break;
351 case GDK_Up:
352 case GDK_Down:
353 case GDK_KP_Up:
354 case GDK_KP_Down:
355 // prevent the zoom field from activation
356 if (!MOD__CTRL_ONLY)
357 ret = TRUE;
358 break;
359 case GDK_x:
360 case GDK_X:
361 if (MOD__ALT_ONLY) {
362 desktop->setToolboxFocusTo ("altx-star");
363 ret = TRUE;
364 }
365 break;
366 case GDK_Escape:
367 sp_desktop_selection(desktop)->clear();
368 //TODO: make dragging escapable by Esc
369 default:
370 break;
371 }
372 break;
373 case GDK_KEY_RELEASE:
374 switch (get_group0_keyval (&event->key)) {
375 case GDK_Alt_L:
376 case GDK_Alt_R:
377 case GDK_Control_L:
378 case GDK_Control_R:
379 case GDK_Shift_L:
380 case GDK_Shift_R:
381 case GDK_Meta_L: // Meta is when you press Shift+Alt
382 case GDK_Meta_R:
383 event_context->defaultMessageContext()->clear();
384 break;
385 default:
386 break;
387 }
388 break;
389 default:
390 break;
391 }
393 if (!ret) {
394 if (((SPEventContextClass *) parent_class)->root_handler)
395 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
396 }
398 return ret;
399 }
401 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
402 {
403 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
405 int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
407 if (!sc->item) {
409 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
410 return;
411 }
413 /* Create object */
414 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
415 repr->setAttribute("sodipodi:type", "star");
417 /* Set style */
418 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
420 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
421 Inkscape::GC::release(repr);
422 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
423 sc->item->updateRepr();
424 }
426 NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
427 NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
429 /* Snap corner point with no constraints */
430 SnapManager const &m = desktop->namedview->snap_manager;
431 p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
433 SPStar *star = SP_STAR(sc->item);
435 double const sides = (gdouble) sc->magnitude;
436 NR::Point const d = p1 - p0;
437 NR::Coord const r1 = NR::L2(d);
438 double arg1 = atan2(d);
440 if (state & GDK_CONTROL_MASK) {
441 /* Snap angle */
442 arg1 = sp_round(arg1, M_PI / snaps);
443 }
445 sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
446 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
448 /* status text */
449 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
450 sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
451 ( sc->isflatsided?
452 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
453 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
454 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
456 g_string_free(rads, FALSE);
457 }
459 static void
460 sp_star_finish (SPStarContext * sc)
461 {
462 sc->_message_context->clear();
464 if (sc->item != NULL) {
465 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
466 SPObject *object = SP_OBJECT(sc->item);
468 sp_shape_set_shape(SP_SHAPE(sc->item));
470 object->updateRepr(NULL, SP_OBJECT_WRITE_EXT);
472 sp_desktop_selection(desktop)->set(sc->item);
473 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
474 /* TODO: annotate */ "star-context.cpp:474");
476 sc->item = NULL;
477 }
478 }
480 /*
481 Local Variables:
482 mode:c++
483 c-file-style:"stroustrup"
484 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
485 indent-tabs-mode:nil
486 fill-column:99
487 End:
488 */
489 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :