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::BBOX_POINT | 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 motion_dt(event_context->desktop->w2d(motion_w));
304 SnapManager const &m = desktop->namedview->snap_manager;
305 motion_dt = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, motion_dt, sc->item).getPoint();
307 sp_star_drag (sc, motion_dt, event->motion.state);
308 ret = TRUE;
309 }
310 break;
311 case GDK_BUTTON_RELEASE:
312 event_context->xp = event_context->yp = 0;
313 if (event->button.button == 1) {
314 dragging = FALSE;
315 if (!event_context->within_tolerance) {
316 // we've been dragging, finish the star
317 sp_star_finish (sc);
318 } else if (event_context->item_to_select) {
319 // no dragging, select clicked item if any
320 if (event->button.state & GDK_SHIFT_MASK) {
321 selection->toggle(event_context->item_to_select);
322 } else {
323 selection->set(event_context->item_to_select);
324 }
325 } else {
326 // click in an empty space
327 selection->clear();
328 }
330 event_context->item_to_select = NULL;
331 ret = TRUE;
332 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
333 }
334 break;
335 case GDK_KEY_PRESS:
336 switch (get_group0_keyval(&event->key)) {
337 case GDK_Alt_R:
338 case GDK_Control_L:
339 case GDK_Control_R:
340 case GDK_Shift_L:
341 case GDK_Shift_R:
342 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
343 case GDK_Meta_R:
344 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
345 _("<b>Ctrl</b>: snap angle; keep rays radial"),
346 NULL,
347 NULL);
348 break;
349 case GDK_Up:
350 case GDK_Down:
351 case GDK_KP_Up:
352 case GDK_KP_Down:
353 // prevent the zoom field from activation
354 if (!MOD__CTRL_ONLY)
355 ret = TRUE;
356 break;
357 case GDK_x:
358 case GDK_X:
359 if (MOD__ALT_ONLY) {
360 desktop->setToolboxFocusTo ("altx-star");
361 ret = TRUE;
362 }
363 break;
364 case GDK_Escape:
365 sp_desktop_selection(desktop)->clear();
366 //TODO: make dragging escapable by Esc
367 break;
369 case GDK_space:
370 if (dragging) {
371 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
372 event->button.time);
373 dragging = false;
374 if (!event_context->within_tolerance) {
375 // we've been dragging, finish the rect
376 sp_star_finish(sc);
377 }
378 // do not return true, so that space would work switching to selector
379 }
380 break;
382 default:
383 break;
384 }
385 break;
386 case GDK_KEY_RELEASE:
387 switch (get_group0_keyval (&event->key)) {
388 case GDK_Alt_L:
389 case GDK_Alt_R:
390 case GDK_Control_L:
391 case GDK_Control_R:
392 case GDK_Shift_L:
393 case GDK_Shift_R:
394 case GDK_Meta_L: // Meta is when you press Shift+Alt
395 case GDK_Meta_R:
396 event_context->defaultMessageContext()->clear();
397 break;
398 default:
399 break;
400 }
401 break;
402 default:
403 break;
404 }
406 if (!ret) {
407 if (((SPEventContextClass *) parent_class)->root_handler)
408 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
409 }
411 return ret;
412 }
414 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
415 {
416 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
418 int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
420 if (!sc->item) {
422 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
423 return;
424 }
426 /* Create object */
427 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
428 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
429 repr->setAttribute("sodipodi:type", "star");
431 /* Set style */
432 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
434 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
435 Inkscape::GC::release(repr);
436 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
437 sc->item->updateRepr();
439 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
440 }
442 NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
443 NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
445 /* Snap corner point with no constraints */
446 SnapManager const &m = desktop->namedview->snap_manager;
447 p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint();
449 SPStar *star = SP_STAR(sc->item);
451 double const sides = (gdouble) sc->magnitude;
452 NR::Point const d = p1 - p0;
453 NR::Coord const r1 = NR::L2(d);
454 double arg1 = atan2(d);
456 if (state & GDK_CONTROL_MASK) {
457 /* Snap angle */
458 arg1 = sp_round(arg1, M_PI / snaps);
459 }
461 sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
462 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
464 /* status text */
465 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
466 sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
467 ( sc->isflatsided?
468 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
469 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
470 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
472 g_string_free(rads, FALSE);
473 }
475 static void
476 sp_star_finish (SPStarContext * sc)
477 {
478 sc->_message_context->clear();
480 if (sc->item != NULL) {
481 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
482 SPObject *object = SP_OBJECT(sc->item);
484 sp_shape_set_shape(SP_SHAPE(sc->item));
486 object->updateRepr(SP_OBJECT_WRITE_EXT);
488 sp_canvas_end_forced_full_redraws(desktop->canvas);
490 sp_desktop_selection(desktop)->set(sc->item);
491 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
492 _("Create star"));
494 sc->item = NULL;
495 }
496 }
498 /*
499 Local Variables:
500 mode:c++
501 c-file-style:"stroustrup"
502 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
503 indent-tabs-mode:nil
504 fill-column:99
505 End:
506 */
507 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :