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 "snap.h"
34 #include "desktop.h"
35 #include "desktop-style.h"
36 #include "message-context.h"
37 #include "libnr/nr-macros.h"
38 #include "pixmaps/cursor-star.xpm"
39 #include "sp-metrics.h"
40 #include <glibmm/i18n.h>
41 #include "preferences.h"
42 #include "xml/repr.h"
43 #include "xml/node-event-vector.h"
44 #include "object-edit.h"
45 #include "context-fns.h"
46 #include "shape-editor.h"
48 #include "star-context.h"
50 static void sp_star_context_class_init (SPStarContextClass * klass);
51 static void sp_star_context_init (SPStarContext * star_context);
52 static void sp_star_context_dispose (GObject *object);
54 static void sp_star_context_setup (SPEventContext *ec);
55 static void sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val);
56 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
58 static void sp_star_drag (SPStarContext * sc, Geom::Point p, guint state);
59 static void sp_star_finish (SPStarContext * sc);
60 static void sp_star_cancel(SPStarContext * sc);
62 static SPEventContextClass * parent_class;
64 GtkType
65 sp_star_context_get_type (void)
66 {
67 static GType type = 0;
68 if (!type) {
69 GTypeInfo info = {
70 sizeof (SPStarContextClass),
71 NULL, NULL,
72 (GClassInitFunc) sp_star_context_class_init,
73 NULL, NULL,
74 sizeof (SPStarContext),
75 4,
76 (GInstanceInitFunc) sp_star_context_init,
77 NULL, /* value_table */
78 };
79 type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
80 }
81 return type;
82 }
84 static void
85 sp_star_context_class_init (SPStarContextClass * klass)
86 {
87 GObjectClass *object_class = (GObjectClass *) klass;
88 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
90 parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
92 object_class->dispose = sp_star_context_dispose;
94 event_context_class->setup = sp_star_context_setup;
95 event_context_class->set = sp_star_context_set;
96 event_context_class->root_handler = sp_star_context_root_handler;
97 }
99 static void
100 sp_star_context_init (SPStarContext * star_context)
101 {
102 SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
104 event_context->cursor_shape = cursor_star_xpm;
105 event_context->hot_x = 4;
106 event_context->hot_y = 4;
107 event_context->xp = 0;
108 event_context->yp = 0;
109 event_context->tolerance = 0;
110 event_context->within_tolerance = false;
111 event_context->item_to_select = NULL;
113 star_context->item = NULL;
115 star_context->magnitude = 5;
116 star_context->proportion = 0.5;
117 star_context->isflatsided = false;
119 new (&star_context->sel_changed_connection) sigc::connection();
120 }
122 static void
123 sp_star_context_dispose (GObject *object)
124 {
125 SPEventContext *ec = SP_EVENT_CONTEXT (object);
126 SPStarContext *sc = SP_STAR_CONTEXT (object);
128 ec->enableGrDrag(false);
130 sc->sel_changed_connection.disconnect();
131 sc->sel_changed_connection.~connection();
133 delete ec->shape_editor;
134 ec->shape_editor = NULL;
136 /* fixme: This is necessary because we do not grab */
137 if (sc->item) sp_star_finish (sc);
139 if (sc->_message_context) {
140 delete sc->_message_context;
141 }
143 G_OBJECT_CLASS (parent_class)->dispose (object);
144 }
146 /**
147 \brief Callback that processes the "changed" signal on the selection;
148 destroys old and creates new knotholder
149 \param selection Should not be NULL.
150 */
151 void
152 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
153 {
154 g_assert (selection != NULL);
156 SPStarContext *sc = SP_STAR_CONTEXT (data);
157 SPEventContext *ec = SP_EVENT_CONTEXT (sc);
159 ec->shape_editor->unset_item(SH_KNOTHOLDER);
160 SPItem *item = selection->singleItem();
161 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
162 }
164 static void
165 sp_star_context_setup (SPEventContext *ec)
166 {
167 SPStarContext *sc = SP_STAR_CONTEXT (ec);
169 if (((SPEventContextClass *) parent_class)->setup)
170 ((SPEventContextClass *) parent_class)->setup (ec);
172 sp_event_context_read (ec, "magnitude");
173 sp_event_context_read (ec, "proportion");
174 sp_event_context_read (ec, "isflatsided");
175 sp_event_context_read (ec, "rounded");
176 sp_event_context_read (ec, "randomized");
178 ec->shape_editor = new ShapeEditor(ec->desktop);
180 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
181 if (item) {
182 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
183 }
185 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
186 sc->sel_changed_connection.disconnect();
187 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
189 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
190 if (prefs->getBool("/tools/shapes/selcue")) {
191 ec->enableSelectionCue();
192 }
194 if (prefs->getBool("/tools/shapes/gradientdrag")) {
195 ec->enableGrDrag();
196 }
198 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
199 }
201 static void
202 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
203 {
204 SPStarContext *sc = SP_STAR_CONTEXT (ec);
205 Glib::ustring path = val->getEntryName();
207 if (path == "magnitude") {
208 sc->magnitude = NR_CLAMP(val->getInt(5), 3, 1024);
209 } else if (path == "proportion") {
210 sc->proportion = NR_CLAMP(val->getDouble(0.5), 0.01, 2.0);
211 } else if (path == "isflatsided") {
212 sc->isflatsided = val->getBool();
213 } else if (path == "rounded") {
214 sc->rounded = val->getDouble();
215 } else if (path == "randomized") {
216 sc->randomized = val->getDouble();
217 }
218 }
220 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
221 {
222 static gboolean dragging;
224 SPDesktop *desktop = event_context->desktop;
225 Inkscape::Selection *selection = sp_desktop_selection (desktop);
226 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
228 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
230 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
232 gint ret = FALSE;
234 switch (event->type) {
235 case GDK_BUTTON_PRESS:
236 if (event->button.button == 1 && !event_context->space_panning) {
238 dragging = TRUE;
240 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
242 /* Snap center */
243 SnapManager &m = desktop->namedview->snap_manager;
244 m.setup(desktop, true);
245 Geom::Point pt2g = to_2geom(sc->center);
246 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
247 sc->center = from_2geom(pt2g);
249 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
250 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
251 GDK_POINTER_MOTION_MASK |
252 GDK_POINTER_MOTION_HINT_MASK |
253 GDK_BUTTON_PRESS_MASK,
254 NULL, event->button.time);
255 ret = TRUE;
256 }
257 break;
258 case GDK_MOTION_NOTIFY:
259 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
261 if ( event_context->within_tolerance
262 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
263 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
264 break; // do not drag if we're within tolerance from origin
265 }
266 // Once the user has moved farther than tolerance from the original location
267 // (indicating they intend to draw, not click), then always process the
268 // motion notify coordinates as given (no snapping back to origin)
269 event_context->within_tolerance = false;
271 Geom::Point const motion_w(event->motion.x, event->motion.y);
272 Geom::Point motion_dt(desktop->w2d(motion_w));
274 sp_star_drag (sc, motion_dt, event->motion.state);
276 gobble_motion_events(GDK_BUTTON1_MASK);
278 ret = TRUE;
279 }
280 break;
281 case GDK_BUTTON_RELEASE:
282 event_context->xp = event_context->yp = 0;
283 if (event->button.button == 1 && !event_context->space_panning) {
284 dragging = FALSE;
285 sp_event_context_discard_delayed_snap_event(event_context);
286 if (!event_context->within_tolerance) {
287 // we've been dragging, finish the star
288 sp_star_finish (sc);
289 } else if (event_context->item_to_select) {
290 // no dragging, select clicked item if any
291 if (event->button.state & GDK_SHIFT_MASK) {
292 selection->toggle(event_context->item_to_select);
293 } else {
294 selection->set(event_context->item_to_select);
295 }
296 } else {
297 // click in an empty space
298 selection->clear();
299 }
301 event_context->item_to_select = NULL;
302 ret = TRUE;
303 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
304 }
305 break;
306 case GDK_KEY_PRESS:
307 switch (get_group0_keyval(&event->key)) {
308 case GDK_Alt_R:
309 case GDK_Control_L:
310 case GDK_Control_R:
311 case GDK_Shift_L:
312 case GDK_Shift_R:
313 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
314 case GDK_Meta_R:
315 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
316 _("<b>Ctrl</b>: snap angle; keep rays radial"),
317 NULL,
318 NULL);
319 break;
320 case GDK_Up:
321 case GDK_Down:
322 case GDK_KP_Up:
323 case GDK_KP_Down:
324 // prevent the zoom field from activation
325 if (!MOD__CTRL_ONLY)
326 ret = TRUE;
327 break;
328 case GDK_x:
329 case GDK_X:
330 if (MOD__ALT_ONLY) {
331 desktop->setToolboxFocusTo ("altx-star");
332 ret = TRUE;
333 }
334 break;
335 case GDK_Escape:
336 if (dragging) {
337 dragging = false;
338 sp_event_context_discard_delayed_snap_event(event_context);
339 // if drawing, cancel, otherwise pass it up for deselecting
340 sp_star_cancel(sc);
341 ret = TRUE;
342 }
343 break;
344 case GDK_space:
345 if (dragging) {
346 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
347 event->button.time);
348 dragging = false;
349 sp_event_context_discard_delayed_snap_event(event_context);
350 if (!event_context->within_tolerance) {
351 // we've been dragging, finish the star
352 sp_star_finish(sc);
353 }
354 // do not return true, so that space would work switching to selector
355 }
356 break;
358 default:
359 break;
360 }
361 break;
362 case GDK_KEY_RELEASE:
363 switch (get_group0_keyval (&event->key)) {
364 case GDK_Alt_L:
365 case GDK_Alt_R:
366 case GDK_Control_L:
367 case GDK_Control_R:
368 case GDK_Shift_L:
369 case GDK_Shift_R:
370 case GDK_Meta_L: // Meta is when you press Shift+Alt
371 case GDK_Meta_R:
372 event_context->defaultMessageContext()->clear();
373 break;
374 default:
375 break;
376 }
377 break;
378 default:
379 break;
380 }
382 if (!ret) {
383 if (((SPEventContextClass *) parent_class)->root_handler)
384 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
385 }
387 return ret;
388 }
390 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
391 {
392 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
394 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
395 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
397 if (!sc->item) {
399 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
400 return;
401 }
403 /* Create object */
404 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
405 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
406 repr->setAttribute("sodipodi:type", "star");
408 /* Set style */
409 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
411 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
412 Inkscape::GC::release(repr);
413 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
414 sc->item->updateRepr();
416 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
417 }
419 /* Snap corner point with no constraints */
420 SnapManager &m = desktop->namedview->snap_manager;
421 m.setup(desktop, true, sc->item);
422 Geom::Point pt2g = to_2geom(p);
423 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
425 Geom::Point const p0 = desktop->dt2doc(sc->center);
426 Geom::Point const p1 = desktop->dt2doc(pt2g);
428 SPStar *star = SP_STAR(sc->item);
430 double const sides = (gdouble) sc->magnitude;
431 Geom::Point const d = p1 - p0;
432 Geom::Coord const r1 = Geom::L2(d);
433 double arg1 = atan2(from_2geom(d));
435 if (state & GDK_CONTROL_MASK) {
436 /* Snap angle */
437 arg1 = sp_round(arg1, M_PI / snaps);
438 }
440 sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
441 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
443 /* status text */
444 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
445 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
446 ( sc->isflatsided?
447 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
448 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
449 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
451 g_string_free(rads, FALSE);
452 }
454 static void
455 sp_star_finish (SPStarContext * sc)
456 {
457 sc->_message_context->clear();
459 if (sc->item != NULL) {
460 SPStar *star = SP_STAR(sc->item);
461 if (star->r[1] == 0) {
462 sp_star_cancel(sc); // Don't allow the creating of zero sized arc, for example when the start and and point snap to the snap grid point
463 return;
464 }
466 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
467 SPObject *object = SP_OBJECT(sc->item);
469 sp_shape_set_shape(SP_SHAPE(sc->item));
471 object->updateRepr(SP_OBJECT_WRITE_EXT);
473 sp_canvas_end_forced_full_redraws(desktop->canvas);
475 sp_desktop_selection(desktop)->set(sc->item);
476 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
477 _("Create star"));
479 sc->item = NULL;
480 }
481 }
483 static void sp_star_cancel(SPStarContext *sc)
484 {
485 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
487 sp_desktop_selection(desktop)->clear();
488 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
490 if (sc->item != NULL) {
491 SP_OBJECT(sc->item)->deleteObject();
492 sc->item = NULL;
493 }
495 sc->within_tolerance = false;
496 sc->xp = 0;
497 sc->yp = 0;
498 sc->item_to_select = NULL;
500 sp_canvas_end_forced_full_redraws(desktop->canvas);
502 sp_document_cancel(sp_desktop_document(desktop));
503 }
505 /*
506 Local Variables:
507 mode:c++
508 c-file-style:"stroustrup"
509 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
510 indent-tabs-mode:nil
511 fill-column:99
512 End:
513 */
514 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :