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_finish(SPEventContext *ec);
56 static void sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val);
57 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
59 static void sp_star_drag (SPStarContext * sc, Geom::Point p, guint state);
60 static void sp_star_finish (SPStarContext * sc);
61 static void sp_star_cancel(SPStarContext * sc);
63 static SPEventContextClass * parent_class;
65 GtkType
66 sp_star_context_get_type (void)
67 {
68 static GType type = 0;
69 if (!type) {
70 GTypeInfo info = {
71 sizeof (SPStarContextClass),
72 NULL, NULL,
73 (GClassInitFunc) sp_star_context_class_init,
74 NULL, NULL,
75 sizeof (SPStarContext),
76 4,
77 (GInstanceInitFunc) sp_star_context_init,
78 NULL, /* value_table */
79 };
80 type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
81 }
82 return type;
83 }
85 static void
86 sp_star_context_class_init (SPStarContextClass * klass)
87 {
88 GObjectClass *object_class = (GObjectClass *) klass;
89 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
91 parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
93 object_class->dispose = sp_star_context_dispose;
95 event_context_class->setup = sp_star_context_setup;
96 event_context_class->finish = sp_star_context_finish;
97 event_context_class->set = sp_star_context_set;
98 event_context_class->root_handler = sp_star_context_root_handler;
99 }
101 static void
102 sp_star_context_init (SPStarContext * star_context)
103 {
104 SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
106 event_context->cursor_shape = cursor_star_xpm;
107 event_context->hot_x = 4;
108 event_context->hot_y = 4;
109 event_context->xp = 0;
110 event_context->yp = 0;
111 event_context->tolerance = 0;
112 event_context->within_tolerance = false;
113 event_context->item_to_select = 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 sp_star_context_finish(SPEventContext *ec)
125 {
126 SPStarContext *sc = SP_STAR_CONTEXT(ec);
127 SPDesktop *desktop = ec->desktop;
129 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
130 sp_star_finish(sc);
131 sc->sel_changed_connection.disconnect();
133 if (((SPEventContextClass *) parent_class)->finish) {
134 ((SPEventContextClass *) parent_class)->finish(ec);
135 }
136 }
139 static void
140 sp_star_context_dispose (GObject *object)
141 {
142 SPEventContext *ec = SP_EVENT_CONTEXT (object);
143 SPStarContext *sc = SP_STAR_CONTEXT (object);
145 ec->enableGrDrag(false);
147 sc->sel_changed_connection.disconnect();
148 sc->sel_changed_connection.~connection();
150 delete ec->shape_editor;
151 ec->shape_editor = NULL;
153 /* fixme: This is necessary because we do not grab */
154 if (sc->item) sp_star_finish (sc);
156 if (sc->_message_context) {
157 delete sc->_message_context;
158 }
160 G_OBJECT_CLASS (parent_class)->dispose (object);
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 ec->shape_editor->unset_item(SH_KNOTHOLDER);
177 SPItem *item = selection->singleItem();
178 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
179 }
181 static void
182 sp_star_context_setup (SPEventContext *ec)
183 {
184 SPStarContext *sc = SP_STAR_CONTEXT (ec);
186 if (((SPEventContextClass *) parent_class)->setup)
187 ((SPEventContextClass *) parent_class)->setup (ec);
189 sp_event_context_read (ec, "magnitude");
190 sp_event_context_read (ec, "proportion");
191 sp_event_context_read (ec, "isflatsided");
192 sp_event_context_read (ec, "rounded");
193 sp_event_context_read (ec, "randomized");
195 ec->shape_editor = new ShapeEditor(ec->desktop);
197 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
198 if (item) {
199 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
200 }
202 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
203 sc->sel_changed_connection.disconnect();
204 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
206 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
207 if (prefs->getBool("/tools/shapes/selcue")) {
208 ec->enableSelectionCue();
209 }
211 if (prefs->getBool("/tools/shapes/gradientdrag")) {
212 ec->enableGrDrag();
213 }
215 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
216 }
218 static void
219 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
220 {
221 SPStarContext *sc = SP_STAR_CONTEXT (ec);
222 Glib::ustring path = val->getEntryName();
224 if (path == "magnitude") {
225 sc->magnitude = NR_CLAMP(val->getInt(5), 3, 1024);
226 } else if (path == "proportion") {
227 sc->proportion = NR_CLAMP(val->getDouble(0.5), 0.01, 2.0);
228 } else if (path == "isflatsided") {
229 sc->isflatsided = val->getBool();
230 } else if (path == "rounded") {
231 sc->rounded = val->getDouble();
232 } else if (path == "randomized") {
233 sc->randomized = val->getDouble();
234 }
235 }
237 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
238 {
239 static gboolean dragging;
241 SPDesktop *desktop = event_context->desktop;
242 Inkscape::Selection *selection = sp_desktop_selection (desktop);
243 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
245 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
247 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
249 gint ret = FALSE;
251 switch (event->type) {
252 case GDK_BUTTON_PRESS:
253 if (event->button.button == 1 && !event_context->space_panning) {
255 dragging = TRUE;
257 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
259 /* Snap center */
260 SnapManager &m = desktop->namedview->snap_manager;
261 m.setup(desktop, true);
262 Geom::Point pt2g = to_2geom(sc->center);
263 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
264 sc->center = from_2geom(pt2g);
266 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
267 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
268 GDK_POINTER_MOTION_MASK |
269 GDK_POINTER_MOTION_HINT_MASK |
270 GDK_BUTTON_PRESS_MASK,
271 NULL, event->button.time);
272 ret = TRUE;
273 }
274 break;
275 case GDK_MOTION_NOTIFY:
276 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
278 if ( event_context->within_tolerance
279 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
280 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
281 break; // do not drag if we're within tolerance from origin
282 }
283 // Once the user has moved farther than tolerance from the original location
284 // (indicating they intend to draw, not click), then always process the
285 // motion notify coordinates as given (no snapping back to origin)
286 event_context->within_tolerance = false;
288 Geom::Point const motion_w(event->motion.x, event->motion.y);
289 Geom::Point motion_dt(desktop->w2d(motion_w));
291 sp_star_drag (sc, motion_dt, event->motion.state);
293 gobble_motion_events(GDK_BUTTON1_MASK);
295 ret = TRUE;
296 }
297 break;
298 case GDK_BUTTON_RELEASE:
299 event_context->xp = event_context->yp = 0;
300 if (event->button.button == 1 && !event_context->space_panning) {
301 dragging = FALSE;
302 sp_event_context_discard_delayed_snap_event(event_context);
303 if (!event_context->within_tolerance) {
304 // we've been dragging, finish the star
305 sp_star_finish (sc);
306 } else if (event_context->item_to_select) {
307 // no dragging, select clicked item if any
308 if (event->button.state & GDK_SHIFT_MASK) {
309 selection->toggle(event_context->item_to_select);
310 } else {
311 selection->set(event_context->item_to_select);
312 }
313 } else {
314 // click in an empty space
315 selection->clear();
316 }
318 event_context->item_to_select = NULL;
319 ret = TRUE;
320 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
321 }
322 break;
323 case GDK_KEY_PRESS:
324 switch (get_group0_keyval(&event->key)) {
325 case GDK_Alt_R:
326 case GDK_Control_L:
327 case GDK_Control_R:
328 case GDK_Shift_L:
329 case GDK_Shift_R:
330 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
331 case GDK_Meta_R:
332 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
333 _("<b>Ctrl</b>: snap angle; keep rays radial"),
334 NULL,
335 NULL);
336 break;
337 case GDK_Up:
338 case GDK_Down:
339 case GDK_KP_Up:
340 case GDK_KP_Down:
341 // prevent the zoom field from activation
342 if (!MOD__CTRL_ONLY)
343 ret = TRUE;
344 break;
345 case GDK_x:
346 case GDK_X:
347 if (MOD__ALT_ONLY) {
348 desktop->setToolboxFocusTo ("altx-star");
349 ret = TRUE;
350 }
351 break;
352 case GDK_Escape:
353 if (dragging) {
354 dragging = false;
355 sp_event_context_discard_delayed_snap_event(event_context);
356 // if drawing, cancel, otherwise pass it up for deselecting
357 sp_star_cancel(sc);
358 ret = TRUE;
359 }
360 break;
361 case GDK_space:
362 if (dragging) {
363 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
364 event->button.time);
365 dragging = false;
366 sp_event_context_discard_delayed_snap_event(event_context);
367 if (!event_context->within_tolerance) {
368 // we've been dragging, finish the star
369 sp_star_finish(sc);
370 }
371 // do not return true, so that space would work switching to selector
372 }
373 break;
375 default:
376 break;
377 }
378 break;
379 case GDK_KEY_RELEASE:
380 switch (get_group0_keyval (&event->key)) {
381 case GDK_Alt_L:
382 case GDK_Alt_R:
383 case GDK_Control_L:
384 case GDK_Control_R:
385 case GDK_Shift_L:
386 case GDK_Shift_R:
387 case GDK_Meta_L: // Meta is when you press Shift+Alt
388 case GDK_Meta_R:
389 event_context->defaultMessageContext()->clear();
390 break;
391 default:
392 break;
393 }
394 break;
395 default:
396 break;
397 }
399 if (!ret) {
400 if (((SPEventContextClass *) parent_class)->root_handler)
401 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
402 }
404 return ret;
405 }
407 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
408 {
409 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
411 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
412 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
414 if (!sc->item) {
416 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
417 return;
418 }
420 /* Create object */
421 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
422 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
423 repr->setAttribute("sodipodi:type", "star");
425 /* Set style */
426 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
428 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
429 Inkscape::GC::release(repr);
430 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
431 sc->item->updateRepr();
433 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
434 }
436 /* Snap corner point with no constraints */
437 SnapManager &m = desktop->namedview->snap_manager;
438 m.setup(desktop, true, sc->item);
439 Geom::Point pt2g = to_2geom(p);
440 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
442 Geom::Point const p0 = desktop->dt2doc(sc->center);
443 Geom::Point const p1 = desktop->dt2doc(pt2g);
445 SPStar *star = SP_STAR(sc->item);
447 double const sides = (gdouble) sc->magnitude;
448 Geom::Point const d = p1 - p0;
449 Geom::Coord const r1 = Geom::L2(d);
450 double arg1 = atan2(from_2geom(d));
452 if (state & GDK_CONTROL_MASK) {
453 /* Snap angle */
454 arg1 = sp_round(arg1, M_PI / snaps);
455 }
457 sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
458 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
460 /* status text */
461 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
462 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
463 ( sc->isflatsided?
464 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
465 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
466 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
468 g_string_free(rads, FALSE);
469 }
471 static void
472 sp_star_finish (SPStarContext * sc)
473 {
474 sc->_message_context->clear();
476 if (sc->item != NULL) {
477 SPStar *star = SP_STAR(sc->item);
478 if (star->r[1] == 0) {
479 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
480 return;
481 }
483 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
484 SPObject *object = SP_OBJECT(sc->item);
486 sp_shape_set_shape(SP_SHAPE(sc->item));
488 object->updateRepr(SP_OBJECT_WRITE_EXT);
490 sp_canvas_end_forced_full_redraws(desktop->canvas);
492 sp_desktop_selection(desktop)->set(sc->item);
493 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
494 _("Create star"));
496 sc->item = NULL;
497 }
498 }
500 static void sp_star_cancel(SPStarContext *sc)
501 {
502 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
504 sp_desktop_selection(desktop)->clear();
505 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
507 if (sc->item != NULL) {
508 SP_OBJECT(sc->item)->deleteObject();
509 sc->item = NULL;
510 }
512 sc->within_tolerance = false;
513 sc->xp = 0;
514 sc->yp = 0;
515 sc->item_to_select = NULL;
517 sp_canvas_end_forced_full_redraws(desktop->canvas);
519 sp_document_cancel(sp_desktop_document(desktop));
520 }
522 /*
523 Local Variables:
524 mode:c++
525 c-file-style:"stroustrup"
526 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
527 indent-tabs-mode:nil
528 fill-column:99
529 End:
530 */
531 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :