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 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <cstring>
22 #include <string>
24 #include <gdk/gdkkeysyms.h>
26 #include "macros.h"
27 #include "display/sp-canvas.h"
28 #include "sp-star.h"
29 #include "document.h"
30 #include "sp-namedview.h"
31 #include "selection.h"
32 #include "desktop-handles.h"
33 #include "desktop-affine.h"
34 #include "snap.h"
35 #include "desktop.h"
36 #include "desktop-style.h"
37 #include "message-context.h"
38 #include "pixmaps/cursor-star.xpm"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "prefs-utils.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "object-edit.h"
45 #include "context-fns.h"
47 #include "star-context.h"
49 static void sp_star_context_class_init (SPStarContextClass * klass);
50 static void sp_star_context_init (SPStarContext * star_context);
51 static void sp_star_context_dispose (GObject *object);
53 static void sp_star_context_setup (SPEventContext *ec);
54 static void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
55 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
57 static void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
58 static void sp_star_finish (SPStarContext * sc);
60 static SPEventContextClass * parent_class;
62 GtkType
63 sp_star_context_get_type (void)
64 {
65 static GType type = 0;
66 if (!type) {
67 GTypeInfo info = {
68 sizeof (SPStarContextClass),
69 NULL, NULL,
70 (GClassInitFunc) sp_star_context_class_init,
71 NULL, NULL,
72 sizeof (SPStarContext),
73 4,
74 (GInstanceInitFunc) sp_star_context_init,
75 NULL, /* value_table */
76 };
77 type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
78 }
79 return type;
80 }
82 static void
83 sp_star_context_class_init (SPStarContextClass * klass)
84 {
85 GObjectClass *object_class = (GObjectClass *) klass;
86 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88 parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
90 object_class->dispose = sp_star_context_dispose;
92 event_context_class->setup = sp_star_context_setup;
93 event_context_class->set = sp_star_context_set;
94 event_context_class->root_handler = sp_star_context_root_handler;
95 }
97 static void
98 sp_star_context_init (SPStarContext * star_context)
99 {
100 SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
102 event_context->cursor_shape = cursor_star_xpm;
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 star_context->item = NULL;
116 star_context->magnitude = 5;
117 star_context->proportion = 0.5;
118 star_context->isflatsided = false;
120 new (&star_context->sel_changed_connection) sigc::connection();
121 }
123 static void
124 sp_star_context_dispose (GObject *object)
125 {
126 SPEventContext *ec = SP_EVENT_CONTEXT (object);
127 SPStarContext *sc = SP_STAR_CONTEXT (object);
129 ec->enableGrDrag(false);
131 sc->sel_changed_connection.disconnect();
132 sc->sel_changed_connection.~connection();
134 if (ec->shape_knot_holder) {
135 delete ec->shape_knot_holder;
136 ec->shape_knot_holder = NULL;
137 }
139 if (ec->shape_repr) { // remove old listener
140 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
141 Inkscape::GC::release(ec->shape_repr);
142 ec->shape_repr = 0;
143 }
145 /* fixme: This is necessary because we do not grab */
146 if (sc->item) sp_star_finish (sc);
148 if (sc->_message_context) {
149 delete sc->_message_context;
150 }
152 G_OBJECT_CLASS (parent_class)->dispose (object);
153 }
155 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
156 NULL, /* child_added */
157 NULL, /* child_removed */
158 ec_shape_event_attr_changed,
159 NULL, /* content_changed */
160 NULL /* order_changed */
161 };
163 /**
164 \brief Callback that processes the "changed" signal on the selection;
165 destroys old and creates new knotholder
166 \param selection Should not be NULL.
167 */
168 void
169 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
170 {
171 g_assert (selection != NULL);
173 SPStarContext *sc = SP_STAR_CONTEXT (data);
174 SPEventContext *ec = SP_EVENT_CONTEXT (sc);
176 if (ec->shape_knot_holder) { // desktroy knotholder
177 delete ec->shape_knot_holder;
178 ec->shape_knot_holder = NULL;
179 }
181 if (ec->shape_repr) { // remove old listener
182 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
183 Inkscape::GC::release(ec->shape_repr);
184 ec->shape_repr = 0;
185 }
187 SPItem *item = selection->singleItem();
188 if (item) {
189 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
190 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
191 if (shape_repr) {
192 ec->shape_repr = shape_repr;
193 Inkscape::GC::anchor(shape_repr);
194 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
195 }
196 }
197 }
199 static void
200 sp_star_context_setup (SPEventContext *ec)
201 {
202 SPStarContext *sc = SP_STAR_CONTEXT (ec);
204 if (((SPEventContextClass *) parent_class)->setup)
205 ((SPEventContextClass *) parent_class)->setup (ec);
207 sp_event_context_read (ec, "magnitude");
208 sp_event_context_read (ec, "proportion");
209 sp_event_context_read (ec, "isflatsided");
210 sp_event_context_read (ec, "rounded");
211 sp_event_context_read (ec, "randomized");
213 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
215 SPItem *item = selection->singleItem();
216 if (item) {
217 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
218 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
219 if (shape_repr) {
220 ec->shape_repr = shape_repr;
221 Inkscape::GC::anchor(shape_repr);
222 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
223 }
224 }
226 sc->sel_changed_connection.disconnect();
227 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
229 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
230 ec->enableSelectionCue();
231 }
233 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
234 ec->enableGrDrag();
235 }
237 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
238 }
240 static void
241 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
242 {
243 SPStarContext *sc = SP_STAR_CONTEXT (ec);
244 if (!strcmp (key, "magnitude")) {
245 sc->magnitude = (val) ? atoi (val) : 5;
246 sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
247 } else if (!strcmp (key, "proportion")) {
248 sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
249 sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
250 } else if (!strcmp (key, "isflatsided")) {
251 if (val && !strcmp(val, "true"))
252 sc->isflatsided = true;
253 else
254 sc->isflatsided = false;
255 } else if (!strcmp (key, "rounded")) {
256 sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
257 } else if (!strcmp (key, "randomized")) {
258 sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
259 }
260 }
262 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
263 {
264 static gboolean dragging;
266 SPDesktop *desktop = event_context->desktop;
267 Inkscape::Selection *selection = sp_desktop_selection (desktop);
269 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
271 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
273 gint ret = FALSE;
275 switch (event->type) {
276 case GDK_BUTTON_PRESS:
277 if (event->button.button == 1 && !event_context->space_panning) {
279 dragging = TRUE;
281 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
283 /* Snap center */
284 SnapManager &m = desktop->namedview->snap_manager;
285 m.setup(desktop, NULL); //null, because we don't have an item yet
286 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, sc->center);
288 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
289 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
290 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
291 NULL, event->button.time);
292 ret = TRUE;
293 }
294 break;
295 case GDK_MOTION_NOTIFY:
296 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
298 if ( event_context->within_tolerance
299 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
300 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
301 break; // do not drag if we're within tolerance from origin
302 }
303 // Once the user has moved farther than tolerance from the original location
304 // (indicating they intend to draw, not click), then always process the
305 // motion notify coordinates as given (no snapping back to origin)
306 event_context->within_tolerance = false;
308 NR::Point const motion_w(event->motion.x, event->motion.y);
309 NR::Point motion_dt(desktop->w2d(motion_w));
311 sp_star_drag (sc, motion_dt, event->motion.state);
313 gobble_motion_events(GDK_BUTTON1_MASK);
315 ret = TRUE;
316 }
317 break;
318 case GDK_BUTTON_RELEASE:
319 event_context->xp = event_context->yp = 0;
320 if (event->button.button == 1 && !event_context->space_panning) {
321 dragging = FALSE;
322 if (!event_context->within_tolerance) {
323 // we've been dragging, finish the star
324 sp_star_finish (sc);
325 } else if (event_context->item_to_select) {
326 // no dragging, select clicked item if any
327 if (event->button.state & GDK_SHIFT_MASK) {
328 selection->toggle(event_context->item_to_select);
329 } else {
330 selection->set(event_context->item_to_select);
331 }
332 } else {
333 // click in an empty space
334 selection->clear();
335 }
337 event_context->item_to_select = NULL;
338 ret = TRUE;
339 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
340 }
341 break;
342 case GDK_KEY_PRESS:
343 switch (get_group0_keyval(&event->key)) {
344 case GDK_Alt_R:
345 case GDK_Control_L:
346 case GDK_Control_R:
347 case GDK_Shift_L:
348 case GDK_Shift_R:
349 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
350 case GDK_Meta_R:
351 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
352 _("<b>Ctrl</b>: snap angle; keep rays radial"),
353 NULL,
354 NULL);
355 break;
356 case GDK_Up:
357 case GDK_Down:
358 case GDK_KP_Up:
359 case GDK_KP_Down:
360 // prevent the zoom field from activation
361 if (!MOD__CTRL_ONLY)
362 ret = TRUE;
363 break;
364 case GDK_x:
365 case GDK_X:
366 if (MOD__ALT_ONLY) {
367 desktop->setToolboxFocusTo ("altx-star");
368 ret = TRUE;
369 }
370 break;
371 case GDK_Escape:
372 sp_desktop_selection(desktop)->clear();
373 //TODO: make dragging escapable by Esc
374 break;
376 case GDK_space:
377 if (dragging) {
378 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
379 event->button.time);
380 dragging = false;
381 if (!event_context->within_tolerance) {
382 // we've been dragging, finish the rect
383 sp_star_finish(sc);
384 }
385 // do not return true, so that space would work switching to selector
386 }
387 break;
389 default:
390 break;
391 }
392 break;
393 case GDK_KEY_RELEASE:
394 switch (get_group0_keyval (&event->key)) {
395 case GDK_Alt_L:
396 case GDK_Alt_R:
397 case GDK_Control_L:
398 case GDK_Control_R:
399 case GDK_Shift_L:
400 case GDK_Shift_R:
401 case GDK_Meta_L: // Meta is when you press Shift+Alt
402 case GDK_Meta_R:
403 event_context->defaultMessageContext()->clear();
404 break;
405 default:
406 break;
407 }
408 break;
409 default:
410 break;
411 }
413 if (!ret) {
414 if (((SPEventContextClass *) parent_class)->root_handler)
415 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
416 }
418 return ret;
419 }
421 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
422 {
423 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
425 int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
427 if (!sc->item) {
429 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
430 return;
431 }
433 /* Create object */
434 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
435 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
436 repr->setAttribute("sodipodi:type", "star");
438 /* Set style */
439 sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
441 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
442 Inkscape::GC::release(repr);
443 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
444 sc->item->updateRepr();
446 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
447 }
449 /* Snap corner point with no constraints */
450 SnapManager &m = desktop->namedview->snap_manager;
451 m.setup(desktop, sc->item);
452 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p);
454 NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
455 NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
457 SPStar *star = SP_STAR(sc->item);
459 double const sides = (gdouble) sc->magnitude;
460 NR::Point const d = p1 - p0;
461 NR::Coord const r1 = NR::L2(d);
462 double arg1 = atan2(d);
464 if (state & GDK_CONTROL_MASK) {
465 /* Snap angle */
466 arg1 = sp_round(arg1, M_PI / snaps);
467 }
469 sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
470 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
472 /* status text */
473 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
474 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
475 ( sc->isflatsided?
476 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
477 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
478 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
480 g_string_free(rads, FALSE);
481 }
483 static void
484 sp_star_finish (SPStarContext * sc)
485 {
486 sc->_message_context->clear();
488 if (sc->item != NULL) {
489 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
490 SPObject *object = SP_OBJECT(sc->item);
492 sp_shape_set_shape(SP_SHAPE(sc->item));
494 object->updateRepr(SP_OBJECT_WRITE_EXT);
496 sp_canvas_end_forced_full_redraws(desktop->canvas);
498 sp_desktop_selection(desktop)->set(sc->item);
499 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
500 _("Create star"));
502 sc->item = NULL;
503 }
504 }
506 /*
507 Local Variables:
508 mode:c++
509 c-file-style:"stroustrup"
510 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
511 indent-tabs-mode:nil
512 fill-column:99
513 End:
514 */
515 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :