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 }
191 }
192 }
194 static void
195 sp_star_context_setup (SPEventContext *ec)
196 {
197 SPStarContext *sc = SP_STAR_CONTEXT (ec);
199 if (((SPEventContextClass *) parent_class)->setup)
200 ((SPEventContextClass *) parent_class)->setup (ec);
202 sp_event_context_read (ec, "magnitude");
203 sp_event_context_read (ec, "proportion");
204 sp_event_context_read (ec, "isflatsided");
205 sp_event_context_read (ec, "rounded");
206 sp_event_context_read (ec, "randomized");
208 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
210 SPItem *item = selection->singleItem();
211 if (item) {
212 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
213 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
214 if (shape_repr) {
215 ec->shape_repr = shape_repr;
216 Inkscape::GC::anchor(shape_repr);
217 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
218 }
219 }
221 sc->sel_changed_connection.disconnect();
222 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
224 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
225 ec->enableSelectionCue();
226 }
228 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
229 ec->enableGrDrag();
230 }
232 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
233 }
235 static void
236 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
237 {
238 SPStarContext *sc = SP_STAR_CONTEXT (ec);
239 if (!strcmp (key, "magnitude")) {
240 sc->magnitude = (val) ? atoi (val) : 5;
241 sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
242 } else if (!strcmp (key, "proportion")) {
243 sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
244 sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
245 } else if (!strcmp (key, "isflatsided")) {
246 if (val && !strcmp(val, "true"))
247 sc->isflatsided = true;
248 else
249 sc->isflatsided = false;
250 } else if (!strcmp (key, "rounded")) {
251 sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
252 } else if (!strcmp (key, "randomized")) {
253 sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
254 }
255 }
257 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
258 {
259 static gboolean dragging;
261 SPDesktop *desktop = event_context->desktop;
262 Inkscape::Selection *selection = sp_desktop_selection (desktop);
264 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
266 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
268 gint ret = FALSE;
270 switch (event->type) {
271 case GDK_BUTTON_PRESS:
272 if (event->button.button == 1) {
274 dragging = TRUE;
276 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
278 SnapManager const &m = desktop->namedview->snap_manager;
279 sc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint();
281 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
282 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
283 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
284 NULL, event->button.time);
285 ret = TRUE;
286 }
287 break;
288 case GDK_MOTION_NOTIFY:
289 if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
291 if ( event_context->within_tolerance
292 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
293 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
294 break; // do not drag if we're within tolerance from origin
295 }
296 // Once the user has moved farther than tolerance from the original location
297 // (indicating they intend to draw, not click), then always process the
298 // motion notify coordinates as given (no snapping back to origin)
299 event_context->within_tolerance = false;
301 NR::Point const motion_w(event->motion.x, event->motion.y);
302 NR::Point const motion_dt(event_context->desktop->w2d(motion_w));
303 sp_star_drag (sc, motion_dt, event->motion.state);
304 ret = TRUE;
305 }
306 break;
307 case GDK_BUTTON_RELEASE:
308 event_context->xp = event_context->yp = 0;
309 if (event->button.button == 1) {
310 dragging = FALSE;
311 if (!event_context->within_tolerance) {
312 // we've been dragging, finish the star
313 sp_star_finish (sc);
314 } else if (event_context->item_to_select) {
315 // no dragging, select clicked item if any
316 if (event->button.state & GDK_SHIFT_MASK) {
317 selection->toggle(event_context->item_to_select);
318 } else {
319 selection->set(event_context->item_to_select);
320 }
321 } else {
322 // click in an empty space
323 selection->clear();
324 }
326 event_context->item_to_select = NULL;
327 ret = TRUE;
328 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
329 }
330 break;
331 case GDK_KEY_PRESS:
332 switch (get_group0_keyval(&event->key)) {
333 case GDK_Alt_R:
334 case GDK_Control_L:
335 case GDK_Control_R:
336 case GDK_Shift_L:
337 case GDK_Shift_R:
338 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
339 case GDK_Meta_R:
340 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
341 _("<b>Ctrl</b>: snap angle; keep rays radial"),
342 NULL,
343 NULL);
344 break;
345 case GDK_Up:
346 case GDK_Down:
347 case GDK_KP_Up:
348 case GDK_KP_Down:
349 // prevent the zoom field from activation
350 if (!MOD__CTRL_ONLY)
351 ret = TRUE;
352 break;
353 case GDK_x:
354 case GDK_X:
355 if (MOD__ALT_ONLY) {
356 desktop->setToolboxFocusTo ("altx-star");
357 ret = TRUE;
358 }
359 break;
360 case GDK_Escape:
361 sp_desktop_selection(desktop)->clear();
362 //TODO: make dragging escapable by Esc
363 default:
364 break;
365 }
366 break;
367 case GDK_KEY_RELEASE:
368 switch (get_group0_keyval (&event->key)) {
369 case GDK_Alt_L:
370 case GDK_Alt_R:
371 case GDK_Control_L:
372 case GDK_Control_R:
373 case GDK_Shift_L:
374 case GDK_Shift_R:
375 case GDK_Meta_L: // Meta is when you press Shift+Alt
376 case GDK_Meta_R:
377 event_context->defaultMessageContext()->clear();
378 break;
379 default:
380 break;
381 }
382 break;
383 default:
384 break;
385 }
387 if (!ret) {
388 if (((SPEventContextClass *) parent_class)->root_handler)
389 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
390 }
392 return ret;
393 }
395 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
396 {
397 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
399 int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
401 if (!sc->item) {
403 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
404 return;
405 }
407 /* Create object */
408 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
409 repr->setAttribute("sodipodi:type", "star");
411 /* Set style */
412 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
414 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
415 Inkscape::GC::release(repr);
416 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
417 sc->item->updateRepr();
419 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
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->snap_manager;
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_canvas_end_forced_full_redraws(desktop->canvas);
470 sp_desktop_selection(desktop)->set(sc->item);
471 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
472 _("Create star"));
474 sc->item = NULL;
475 }
476 }
478 /*
479 Local Variables:
480 mode:c++
481 c-file-style:"stroustrup"
482 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
483 indent-tabs-mode:nil
484 fill-column:99
485 End:
486 */
487 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :