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 "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);
61 static SPEventContextClass * parent_class;
63 GtkType
64 sp_star_context_get_type (void)
65 {
66 static GType type = 0;
67 if (!type) {
68 GTypeInfo info = {
69 sizeof (SPStarContextClass),
70 NULL, NULL,
71 (GClassInitFunc) sp_star_context_class_init,
72 NULL, NULL,
73 sizeof (SPStarContext),
74 4,
75 (GInstanceInitFunc) sp_star_context_init,
76 NULL, /* value_table */
77 };
78 type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
79 }
80 return type;
81 }
83 static void
84 sp_star_context_class_init (SPStarContextClass * klass)
85 {
86 GObjectClass *object_class = (GObjectClass *) klass;
87 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
89 parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
91 object_class->dispose = sp_star_context_dispose;
93 event_context_class->setup = sp_star_context_setup;
94 event_context_class->set = sp_star_context_set;
95 event_context_class->root_handler = sp_star_context_root_handler;
96 }
98 static void
99 sp_star_context_init (SPStarContext * star_context)
100 {
101 SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
103 event_context->cursor_shape = cursor_star_xpm;
104 event_context->hot_x = 4;
105 event_context->hot_y = 4;
106 event_context->xp = 0;
107 event_context->yp = 0;
108 event_context->tolerance = 0;
109 event_context->within_tolerance = false;
110 event_context->item_to_select = NULL;
112 star_context->item = NULL;
114 star_context->magnitude = 5;
115 star_context->proportion = 0.5;
116 star_context->isflatsided = false;
118 new (&star_context->sel_changed_connection) sigc::connection();
119 }
121 static void
122 sp_star_context_dispose (GObject *object)
123 {
124 SPEventContext *ec = SP_EVENT_CONTEXT (object);
125 SPStarContext *sc = SP_STAR_CONTEXT (object);
127 ec->enableGrDrag(false);
129 sc->sel_changed_connection.disconnect();
130 sc->sel_changed_connection.~connection();
132 delete ec->shape_editor;
133 ec->shape_editor = NULL;
135 /* fixme: This is necessary because we do not grab */
136 if (sc->item) sp_star_finish (sc);
138 if (sc->_message_context) {
139 delete sc->_message_context;
140 }
142 G_OBJECT_CLASS (parent_class)->dispose (object);
143 }
145 /**
146 \brief Callback that processes the "changed" signal on the selection;
147 destroys old and creates new knotholder
148 \param selection Should not be NULL.
149 */
150 void
151 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
152 {
153 g_assert (selection != NULL);
155 SPStarContext *sc = SP_STAR_CONTEXT (data);
156 SPEventContext *ec = SP_EVENT_CONTEXT (sc);
158 ec->shape_editor->unset_item(SH_KNOTHOLDER);
159 SPItem *item = selection->singleItem();
160 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
161 }
163 static void
164 sp_star_context_setup (SPEventContext *ec)
165 {
166 SPStarContext *sc = SP_STAR_CONTEXT (ec);
168 if (((SPEventContextClass *) parent_class)->setup)
169 ((SPEventContextClass *) parent_class)->setup (ec);
171 sp_event_context_read (ec, "magnitude");
172 sp_event_context_read (ec, "proportion");
173 sp_event_context_read (ec, "isflatsided");
174 sp_event_context_read (ec, "rounded");
175 sp_event_context_read (ec, "randomized");
177 ec->shape_editor = new ShapeEditor(ec->desktop);
179 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
180 if (item) {
181 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
182 }
184 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
185 sc->sel_changed_connection.disconnect();
186 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
188 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
189 if (prefs->getBool("/tools/shapes/selcue")) {
190 ec->enableSelectionCue();
191 }
193 if (prefs->getBool("/tools/shapes/gradientdrag")) {
194 ec->enableGrDrag();
195 }
197 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
198 }
200 static void
201 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
202 {
203 SPStarContext *sc = SP_STAR_CONTEXT (ec);
204 Glib::ustring path = val->getEntryName();
206 if (path == "magnitude") {
207 sc->magnitude = CLAMP (val->getInt(5), 3, 1024);
208 } else if (path == "proportion") {
209 sc->proportion = CLAMP (val->getDouble(0.5), 0.01, 2.0);
210 } else if (path == "isflatsided") {
211 sc->isflatsided = val->getBool();
212 } else if (path == "rounded") {
213 sc->rounded = val->getDouble();
214 } else if (path == "randomized") {
215 sc->randomized = val->getDouble();
216 }
217 }
219 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
220 {
221 static gboolean dragging;
223 SPDesktop *desktop = event_context->desktop;
224 Inkscape::Selection *selection = sp_desktop_selection (desktop);
225 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
227 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
229 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
231 gint ret = FALSE;
233 switch (event->type) {
234 case GDK_BUTTON_PRESS:
235 if (event->button.button == 1 && !event_context->space_panning) {
237 dragging = TRUE;
238 sp_canvas_set_snap_delay_active(desktop->canvas, 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);
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_canvas_set_snap_delay_active(desktop->canvas, false);
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 sp_desktop_selection(desktop)->clear();
337 //TODO: make dragging escapable by Esc
338 break;
340 case GDK_space:
341 if (dragging) {
342 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
343 event->button.time);
344 dragging = false;
345 sp_canvas_set_snap_delay_active(desktop->canvas, false);
346 if (!event_context->within_tolerance) {
347 // we've been dragging, finish the rect
348 sp_star_finish(sc);
349 }
350 // do not return true, so that space would work switching to selector
351 }
352 break;
354 default:
355 break;
356 }
357 break;
358 case GDK_KEY_RELEASE:
359 switch (get_group0_keyval (&event->key)) {
360 case GDK_Alt_L:
361 case GDK_Alt_R:
362 case GDK_Control_L:
363 case GDK_Control_R:
364 case GDK_Shift_L:
365 case GDK_Shift_R:
366 case GDK_Meta_L: // Meta is when you press Shift+Alt
367 case GDK_Meta_R:
368 event_context->defaultMessageContext()->clear();
369 break;
370 default:
371 break;
372 }
373 break;
374 default:
375 break;
376 }
378 if (!ret) {
379 if (((SPEventContextClass *) parent_class)->root_handler)
380 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
381 }
383 return ret;
384 }
386 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
387 {
388 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
390 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
391 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
393 if (!sc->item) {
395 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
396 return;
397 }
399 /* Create object */
400 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
401 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
402 repr->setAttribute("sodipodi:type", "star");
404 /* Set style */
405 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
407 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
408 Inkscape::GC::release(repr);
409 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
410 sc->item->updateRepr();
412 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
413 }
415 /* Snap corner point with no constraints */
416 SnapManager &m = desktop->namedview->snap_manager;
417 m.setup(desktop, true, sc->item);
418 Geom::Point pt2g = to_2geom(p);
419 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
421 Geom::Point const p0 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, sc->center));
422 Geom::Point const p1 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, from_2geom(pt2g)));
424 SPStar *star = SP_STAR(sc->item);
426 double const sides = (gdouble) sc->magnitude;
427 Geom::Point const d = p1 - p0;
428 Geom::Coord const r1 = Geom::L2(d);
429 double arg1 = atan2(from_2geom(d));
431 if (state & GDK_CONTROL_MASK) {
432 /* Snap angle */
433 arg1 = sp_round(arg1, M_PI / snaps);
434 }
436 sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
437 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
439 /* status text */
440 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
441 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
442 ( sc->isflatsided?
443 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
444 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
445 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
447 g_string_free(rads, FALSE);
448 }
450 static void
451 sp_star_finish (SPStarContext * sc)
452 {
453 sc->_message_context->clear();
455 if (sc->item != NULL) {
456 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
457 SPObject *object = SP_OBJECT(sc->item);
459 sp_shape_set_shape(SP_SHAPE(sc->item));
461 object->updateRepr(SP_OBJECT_WRITE_EXT);
463 sp_canvas_end_forced_full_redraws(desktop->canvas);
465 sp_desktop_selection(desktop)->set(sc->item);
466 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
467 _("Create star"));
469 sc->item = NULL;
470 }
471 }
473 /*
474 Local Variables:
475 mode:c++
476 c-file-style:"stroustrup"
477 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478 indent-tabs-mode:nil
479 fill-column:99
480 End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :