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 "pixmaps/cursor-star.xpm"
38 #include "sp-metrics.h"
39 #include <glibmm/i18n.h>
40 #include "preferences.h"
41 #include "xml/repr.h"
42 #include "xml/node-event-vector.h"
43 #include "object-edit.h"
44 #include "context-fns.h"
45 #include "shape-editor.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, Inkscape::Preferences::Entry *val);
55 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
57 static void sp_star_drag (SPStarContext * sc, Geom::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 star_context->item = NULL;
113 star_context->magnitude = 5;
114 star_context->proportion = 0.5;
115 star_context->isflatsided = false;
117 new (&star_context->sel_changed_connection) sigc::connection();
118 }
120 static void
121 sp_star_context_dispose (GObject *object)
122 {
123 SPEventContext *ec = SP_EVENT_CONTEXT (object);
124 SPStarContext *sc = SP_STAR_CONTEXT (object);
126 ec->enableGrDrag(false);
128 sc->sel_changed_connection.disconnect();
129 sc->sel_changed_connection.~connection();
131 delete ec->shape_editor;
132 ec->shape_editor = NULL;
134 /* fixme: This is necessary because we do not grab */
135 if (sc->item) sp_star_finish (sc);
137 if (sc->_message_context) {
138 delete sc->_message_context;
139 }
141 G_OBJECT_CLASS (parent_class)->dispose (object);
142 }
144 /**
145 \brief Callback that processes the "changed" signal on the selection;
146 destroys old and creates new knotholder
147 \param selection Should not be NULL.
148 */
149 void
150 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
151 {
152 g_assert (selection != NULL);
154 SPStarContext *sc = SP_STAR_CONTEXT (data);
155 SPEventContext *ec = SP_EVENT_CONTEXT (sc);
157 ec->shape_editor->unset_item(SH_KNOTHOLDER);
158 SPItem *item = selection->singleItem();
159 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
160 }
162 static void
163 sp_star_context_setup (SPEventContext *ec)
164 {
165 SPStarContext *sc = SP_STAR_CONTEXT (ec);
167 if (((SPEventContextClass *) parent_class)->setup)
168 ((SPEventContextClass *) parent_class)->setup (ec);
170 sp_event_context_read (ec, "magnitude");
171 sp_event_context_read (ec, "proportion");
172 sp_event_context_read (ec, "isflatsided");
173 sp_event_context_read (ec, "rounded");
174 sp_event_context_read (ec, "randomized");
176 ec->shape_editor = new ShapeEditor(ec->desktop);
178 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
179 if (item) {
180 ec->shape_editor->set_item(item, SH_KNOTHOLDER);
181 }
183 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
184 sc->sel_changed_connection.disconnect();
185 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
187 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
188 if (prefs->getBool("/tools/shapes/selcue")) {
189 ec->enableSelectionCue();
190 }
192 if (prefs->getBool("/tools/shapes/gradientdrag")) {
193 ec->enableGrDrag();
194 }
196 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
197 }
199 static void
200 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
201 {
202 SPStarContext *sc = SP_STAR_CONTEXT (ec);
203 Glib::ustring path = val->getEntryName();
205 if (path == "magnitude") {
206 sc->magnitude = CLAMP (val->getInt(5), 3, 1024);
207 } else if (path == "proportion") {
208 sc->proportion = CLAMP (val->getDouble(0.5), 0.01, 2.0);
209 } else if (path == "isflatsided") {
210 sc->isflatsided = val->getBool();
211 } else if (path == "rounded") {
212 sc->rounded = val->getDouble();
213 } else if (path == "randomized") {
214 sc->randomized = val->getDouble();
215 }
216 }
218 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
219 {
220 static gboolean dragging;
222 SPDesktop *desktop = event_context->desktop;
223 Inkscape::Selection *selection = sp_desktop_selection (desktop);
224 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
226 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
228 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
230 gint ret = FALSE;
232 switch (event->type) {
233 case GDK_BUTTON_PRESS:
234 if (event->button.button == 1 && !event_context->space_panning) {
236 dragging = TRUE;
237 sp_event_context_snap_window_open(event_context);
239 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
241 /* Snap center */
242 SnapManager &m = desktop->namedview->snap_manager;
243 m.setup(desktop, true);
244 Geom::Point pt2g = to_2geom(sc->center);
245 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
246 sc->center = from_2geom(pt2g);
248 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
249 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
250 GDK_POINTER_MOTION_MASK |
251 GDK_POINTER_MOTION_HINT_MASK |
252 GDK_BUTTON_PRESS_MASK,
253 NULL, event->button.time);
254 ret = TRUE;
255 }
256 break;
257 case GDK_MOTION_NOTIFY:
258 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
260 if ( event_context->within_tolerance
261 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
262 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
263 break; // do not drag if we're within tolerance from origin
264 }
265 // Once the user has moved farther than tolerance from the original location
266 // (indicating they intend to draw, not click), then always process the
267 // motion notify coordinates as given (no snapping back to origin)
268 event_context->within_tolerance = false;
270 Geom::Point const motion_w(event->motion.x, event->motion.y);
271 Geom::Point motion_dt(desktop->w2d(motion_w));
273 sp_star_drag (sc, motion_dt, event->motion.state);
275 gobble_motion_events(GDK_BUTTON1_MASK);
277 ret = TRUE;
278 }
279 break;
280 case GDK_BUTTON_RELEASE:
281 event_context->xp = event_context->yp = 0;
282 if (event->button.button == 1 && !event_context->space_panning) {
283 dragging = FALSE;
284 sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
285 if (!event_context->within_tolerance) {
286 // we've been dragging, finish the star
287 sp_star_finish (sc);
288 } else if (event_context->item_to_select) {
289 // no dragging, select clicked item if any
290 if (event->button.state & GDK_SHIFT_MASK) {
291 selection->toggle(event_context->item_to_select);
292 } else {
293 selection->set(event_context->item_to_select);
294 }
295 } else {
296 // click in an empty space
297 selection->clear();
298 }
300 event_context->item_to_select = NULL;
301 ret = TRUE;
302 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
303 }
304 break;
305 case GDK_KEY_PRESS:
306 switch (get_group0_keyval(&event->key)) {
307 case GDK_Alt_R:
308 case GDK_Control_L:
309 case GDK_Control_R:
310 case GDK_Shift_L:
311 case GDK_Shift_R:
312 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
313 case GDK_Meta_R:
314 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
315 _("<b>Ctrl</b>: snap angle; keep rays radial"),
316 NULL,
317 NULL);
318 break;
319 case GDK_Up:
320 case GDK_Down:
321 case GDK_KP_Up:
322 case GDK_KP_Down:
323 // prevent the zoom field from activation
324 if (!MOD__CTRL_ONLY)
325 ret = TRUE;
326 break;
327 case GDK_x:
328 case GDK_X:
329 if (MOD__ALT_ONLY) {
330 desktop->setToolboxFocusTo ("altx-star");
331 ret = TRUE;
332 }
333 break;
334 case GDK_Escape:
335 sp_desktop_selection(desktop)->clear();
336 //TODO: make dragging escapable by Esc
337 break;
339 case GDK_space:
340 if (dragging) {
341 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
342 event->button.time);
343 dragging = false;
344 sp_event_context_snap_window_closed(event_context);
345 if (!event_context->within_tolerance) {
346 // we've been dragging, finish the rect
347 sp_star_finish(sc);
348 }
349 // do not return true, so that space would work switching to selector
350 }
351 break;
353 default:
354 break;
355 }
356 break;
357 case GDK_KEY_RELEASE:
358 switch (get_group0_keyval (&event->key)) {
359 case GDK_Alt_L:
360 case GDK_Alt_R:
361 case GDK_Control_L:
362 case GDK_Control_R:
363 case GDK_Shift_L:
364 case GDK_Shift_R:
365 case GDK_Meta_L: // Meta is when you press Shift+Alt
366 case GDK_Meta_R:
367 event_context->defaultMessageContext()->clear();
368 break;
369 default:
370 break;
371 }
372 break;
373 default:
374 break;
375 }
377 if (!ret) {
378 if (((SPEventContextClass *) parent_class)->root_handler)
379 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
380 }
382 return ret;
383 }
385 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
386 {
387 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
389 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
390 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
392 if (!sc->item) {
394 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
395 return;
396 }
398 /* Create object */
399 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
400 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
401 repr->setAttribute("sodipodi:type", "star");
403 /* Set style */
404 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
406 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
407 Inkscape::GC::release(repr);
408 sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
409 sc->item->updateRepr();
411 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
412 }
414 /* Snap corner point with no constraints */
415 SnapManager &m = desktop->namedview->snap_manager;
416 m.setup(desktop, true, sc->item);
417 Geom::Point pt2g = to_2geom(p);
418 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
420 Geom::Point const p0 = desktop->dt2doc(sc->center);
421 Geom::Point const p1 = desktop->dt2doc(pt2g);
423 SPStar *star = SP_STAR(sc->item);
425 double const sides = (gdouble) sc->magnitude;
426 Geom::Point const d = p1 - p0;
427 Geom::Coord const r1 = Geom::L2(d);
428 double arg1 = atan2(from_2geom(d));
430 if (state & GDK_CONTROL_MASK) {
431 /* Snap angle */
432 arg1 = sp_round(arg1, M_PI / snaps);
433 }
435 sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
436 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
438 /* status text */
439 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
440 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
441 ( sc->isflatsided?
442 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
443 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
444 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
446 g_string_free(rads, FALSE);
447 }
449 static void
450 sp_star_finish (SPStarContext * sc)
451 {
452 sc->_message_context->clear();
454 if (sc->item != NULL) {
455 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
456 SPObject *object = SP_OBJECT(sc->item);
458 sp_shape_set_shape(SP_SHAPE(sc->item));
460 object->updateRepr(SP_OBJECT_WRITE_EXT);
462 sp_canvas_end_forced_full_redraws(desktop->canvas);
464 sp_desktop_selection(desktop)->set(sc->item);
465 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
466 _("Create star"));
468 sc->item = NULL;
469 }
470 }
472 /*
473 Local Variables:
474 mode:c++
475 c-file-style:"stroustrup"
476 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
477 indent-tabs-mode:nil
478 fill-column:99
479 End:
480 */
481 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :