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 m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
264 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
265 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
266 GDK_POINTER_MOTION_MASK |
267 GDK_POINTER_MOTION_HINT_MASK |
268 GDK_BUTTON_PRESS_MASK,
269 NULL, event->button.time);
270 ret = TRUE;
271 }
272 break;
273 case GDK_MOTION_NOTIFY:
274 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
276 if ( event_context->within_tolerance
277 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
278 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
279 break; // do not drag if we're within tolerance from origin
280 }
281 // Once the user has moved farther than tolerance from the original location
282 // (indicating they intend to draw, not click), then always process the
283 // motion notify coordinates as given (no snapping back to origin)
284 event_context->within_tolerance = false;
286 Geom::Point const motion_w(event->motion.x, event->motion.y);
287 Geom::Point motion_dt(desktop->w2d(motion_w));
289 sp_star_drag (sc, motion_dt, event->motion.state);
291 gobble_motion_events(GDK_BUTTON1_MASK);
293 ret = TRUE;
294 } else if (!sp_event_context_knot_mouseover(event_context)) {
295 SnapManager &m = desktop->namedview->snap_manager;
296 m.setup(desktop);
298 Geom::Point const motion_w(event->motion.x, event->motion.y);
299 Geom::Point motion_dt(desktop->w2d(motion_w));
300 m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
301 }
302 break;
303 case GDK_BUTTON_RELEASE:
304 event_context->xp = event_context->yp = 0;
305 if (event->button.button == 1 && !event_context->space_panning) {
306 dragging = FALSE;
307 sp_event_context_discard_delayed_snap_event(event_context);
308 if (!event_context->within_tolerance) {
309 // we've been dragging, finish the star
310 sp_star_finish (sc);
311 } else if (event_context->item_to_select) {
312 // no dragging, select clicked item if any
313 if (event->button.state & GDK_SHIFT_MASK) {
314 selection->toggle(event_context->item_to_select);
315 } else {
316 selection->set(event_context->item_to_select);
317 }
318 } else {
319 // click in an empty space
320 selection->clear();
321 }
323 event_context->item_to_select = NULL;
324 ret = TRUE;
325 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
326 }
327 break;
328 case GDK_KEY_PRESS:
329 switch (get_group0_keyval(&event->key)) {
330 case GDK_Alt_R:
331 case GDK_Control_L:
332 case GDK_Control_R:
333 case GDK_Shift_L:
334 case GDK_Shift_R:
335 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
336 case GDK_Meta_R:
337 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
338 _("<b>Ctrl</b>: snap angle; keep rays radial"),
339 NULL,
340 NULL);
341 break;
342 case GDK_Up:
343 case GDK_Down:
344 case GDK_KP_Up:
345 case GDK_KP_Down:
346 // prevent the zoom field from activation
347 if (!MOD__CTRL_ONLY)
348 ret = TRUE;
349 break;
350 case GDK_x:
351 case GDK_X:
352 if (MOD__ALT_ONLY) {
353 desktop->setToolboxFocusTo ("altx-star");
354 ret = TRUE;
355 }
356 break;
357 case GDK_Escape:
358 if (dragging) {
359 dragging = false;
360 sp_event_context_discard_delayed_snap_event(event_context);
361 // if drawing, cancel, otherwise pass it up for deselecting
362 sp_star_cancel(sc);
363 ret = TRUE;
364 }
365 break;
366 case GDK_space:
367 if (dragging) {
368 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
369 event->button.time);
370 dragging = false;
371 sp_event_context_discard_delayed_snap_event(event_context);
372 if (!event_context->within_tolerance) {
373 // we've been dragging, finish the star
374 sp_star_finish(sc);
375 }
376 // do not return true, so that space would work switching to selector
377 }
378 break;
380 default:
381 break;
382 }
383 break;
384 case GDK_KEY_RELEASE:
385 switch (get_group0_keyval (&event->key)) {
386 case GDK_Alt_L:
387 case GDK_Alt_R:
388 case GDK_Control_L:
389 case GDK_Control_R:
390 case GDK_Shift_L:
391 case GDK_Shift_R:
392 case GDK_Meta_L: // Meta is when you press Shift+Alt
393 case GDK_Meta_R:
394 event_context->defaultMessageContext()->clear();
395 break;
396 default:
397 break;
398 }
399 break;
400 default:
401 break;
402 }
404 if (!ret) {
405 if (((SPEventContextClass *) parent_class)->root_handler)
406 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
407 }
409 return ret;
410 }
412 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
413 {
414 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
416 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
417 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
419 if (!sc->item) {
421 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
422 return;
423 }
425 /* Create object */
426 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
427 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
428 repr->setAttribute("sodipodi:type", "star");
430 /* Set style */
431 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
433 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
434 Inkscape::GC::release(repr);
435 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
436 sc->item->updateRepr();
438 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
439 }
441 /* Snap corner point with no constraints */
442 SnapManager &m = desktop->namedview->snap_manager;
443 m.setup(desktop, true, sc->item);
444 Geom::Point pt2g = to_2geom(p);
445 m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
447 Geom::Point const p0 = desktop->dt2doc(sc->center);
448 Geom::Point const p1 = desktop->dt2doc(pt2g);
450 SPStar *star = SP_STAR(sc->item);
452 double const sides = (gdouble) sc->magnitude;
453 Geom::Point const d = p1 - p0;
454 Geom::Coord const r1 = Geom::L2(d);
455 double arg1 = atan2(from_2geom(d));
457 if (state & GDK_CONTROL_MASK) {
458 /* Snap angle */
459 arg1 = sp_round(arg1, M_PI / snaps);
460 }
462 sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
463 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
465 /* status text */
466 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
467 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
468 ( sc->isflatsided?
469 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
470 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
471 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
473 g_string_free(rads, FALSE);
474 }
476 static void
477 sp_star_finish (SPStarContext * sc)
478 {
479 sc->_message_context->clear();
481 if (sc->item != NULL) {
482 SPStar *star = SP_STAR(sc->item);
483 if (star->r[1] == 0) {
484 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
485 return;
486 }
488 // Set transform center, so that odd stars rotate correctly
489 // LP #462157
490 sc->item->setCenter(sc->center);
492 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
493 SPObject *object = SP_OBJECT(sc->item);
495 sp_shape_set_shape(SP_SHAPE(sc->item));
497 object->updateRepr(SP_OBJECT_WRITE_EXT);
499 sp_canvas_end_forced_full_redraws(desktop->canvas);
501 sp_desktop_selection(desktop)->set(sc->item);
502 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
503 _("Create star"));
505 sc->item = NULL;
506 }
507 }
509 static void sp_star_cancel(SPStarContext *sc)
510 {
511 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
513 sp_desktop_selection(desktop)->clear();
514 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
516 if (sc->item != NULL) {
517 SP_OBJECT(sc->item)->deleteObject();
518 sc->item = NULL;
519 }
521 sc->within_tolerance = false;
522 sc->xp = 0;
523 sc->yp = 0;
524 sc->item_to_select = NULL;
526 sp_canvas_end_forced_full_redraws(desktop->canvas);
528 sp_document_cancel(sp_desktop_document(desktop));
529 }
531 /*
532 Local Variables:
533 mode:c++
534 c-file-style:"stroustrup"
535 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
536 indent-tabs-mode:nil
537 fill-column:99
538 End:
539 */
540 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :