6bb0a6f2d50e5874712f9275674b64a66e00cf1e
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"
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, NR::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 event_context->shape_repr = NULL;
112 event_context->shape_knot_holder = NULL;
114 star_context->item = NULL;
116 star_context->magnitude = 5;
117 star_context->proportion = 0.5;
118 star_context->isflatsided = false;
120 new (&star_context->sel_changed_connection) sigc::connection();
121 }
123 static void
124 sp_star_context_dispose (GObject *object)
125 {
126 SPEventContext *ec = SP_EVENT_CONTEXT (object);
127 SPStarContext *sc = SP_STAR_CONTEXT (object);
129 ec->enableGrDrag(false);
131 sc->sel_changed_connection.disconnect();
132 sc->sel_changed_connection.~connection();
134 if (ec->shape_knot_holder) {
135 delete ec->shape_knot_holder;
136 ec->shape_knot_holder = NULL;
137 }
139 if (ec->shape_repr) { // remove old listener
140 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
141 Inkscape::GC::release(ec->shape_repr);
142 ec->shape_repr = 0;
143 }
145 /* fixme: This is necessary because we do not grab */
146 if (sc->item) sp_star_finish (sc);
148 if (sc->_message_context) {
149 delete sc->_message_context;
150 }
152 G_OBJECT_CLASS (parent_class)->dispose (object);
153 }
155 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
156 NULL, /* child_added */
157 NULL, /* child_removed */
158 ec_shape_event_attr_changed,
159 NULL, /* content_changed */
160 NULL /* order_changed */
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 if (ec->shape_knot_holder) { // desktroy knotholder
177 delete ec->shape_knot_holder;
178 ec->shape_knot_holder = NULL;
179 }
181 if (ec->shape_repr) { // remove old listener
182 sp_repr_remove_listener_by_data (ec->shape_repr, ec);
183 Inkscape::GC::release(ec->shape_repr);
184 ec->shape_repr = 0;
185 }
187 SPItem *item = selection->singleItem();
188 if (item) {
189 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
190 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
191 if (shape_repr) {
192 ec->shape_repr = shape_repr;
193 Inkscape::GC::anchor(shape_repr);
194 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
195 }
196 }
197 }
199 static void
200 sp_star_context_setup (SPEventContext *ec)
201 {
202 SPStarContext *sc = SP_STAR_CONTEXT (ec);
204 if (((SPEventContextClass *) parent_class)->setup)
205 ((SPEventContextClass *) parent_class)->setup (ec);
207 sp_event_context_read (ec, "magnitude");
208 sp_event_context_read (ec, "proportion");
209 sp_event_context_read (ec, "isflatsided");
210 sp_event_context_read (ec, "rounded");
211 sp_event_context_read (ec, "randomized");
213 Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
215 SPItem *item = selection->singleItem();
216 if (item) {
217 ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
218 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
219 if (shape_repr) {
220 ec->shape_repr = shape_repr;
221 Inkscape::GC::anchor(shape_repr);
222 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
223 }
224 }
226 sc->sel_changed_connection.disconnect();
227 sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
229 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
230 if (prefs->getBool("/tools/shapes/selcue")) {
231 ec->enableSelectionCue();
232 }
234 if (prefs->getBool("/tools/shapes/gradientdrag")) {
235 ec->enableGrDrag();
236 }
238 sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
239 }
241 static void
242 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
243 {
244 SPStarContext *sc = SP_STAR_CONTEXT (ec);
245 Glib::ustring path = val->getEntryName();
247 if (path == "magnitude") {
248 sc->magnitude = CLAMP (val->getInt(5), 3, 1024);
249 } else if (path == "proportion") {
250 sc->proportion = CLAMP (val->getDouble(0.5), 0.01, 2.0);
251 } else if (path == "isflatsided") {
252 sc->isflatsided = val->getBool();
253 } else if (path == "rounded") {
254 sc->rounded = val->getDouble();
255 } else if (path == "randomized") {
256 sc->randomized = val->getDouble();
257 }
258 }
260 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
261 {
262 static gboolean dragging;
264 SPDesktop *desktop = event_context->desktop;
265 Inkscape::Selection *selection = sp_desktop_selection (desktop);
266 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
268 SPStarContext *sc = SP_STAR_CONTEXT (event_context);
270 event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
272 gint ret = FALSE;
274 switch (event->type) {
275 case GDK_BUTTON_PRESS:
276 if (event->button.button == 1 && !event_context->space_panning) {
278 dragging = TRUE;
280 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
282 /* Snap center */
283 SnapManager &m = desktop->namedview->snap_manager;
284 m.setup(desktop, true);
285 Geom::Point pt2g = to_2geom(sc->center);
286 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
287 sc->center = from_2geom(pt2g);
289 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
290 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
291 GDK_POINTER_MOTION_MASK |
292 GDK_POINTER_MOTION_HINT_MASK |
293 GDK_BUTTON_PRESS_MASK,
294 NULL, event->button.time);
295 ret = TRUE;
296 }
297 break;
298 case GDK_MOTION_NOTIFY:
299 if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
301 if ( event_context->within_tolerance
302 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
303 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
304 break; // do not drag if we're within tolerance from origin
305 }
306 // Once the user has moved farther than tolerance from the original location
307 // (indicating they intend to draw, not click), then always process the
308 // motion notify coordinates as given (no snapping back to origin)
309 event_context->within_tolerance = false;
311 NR::Point const motion_w(event->motion.x, event->motion.y);
312 NR::Point motion_dt(desktop->w2d(motion_w));
314 sp_star_drag (sc, motion_dt, event->motion.state);
316 gobble_motion_events(GDK_BUTTON1_MASK);
318 ret = TRUE;
319 }
320 break;
321 case GDK_BUTTON_RELEASE:
322 event_context->xp = event_context->yp = 0;
323 if (event->button.button == 1 && !event_context->space_panning) {
324 dragging = FALSE;
325 if (!event_context->within_tolerance) {
326 // we've been dragging, finish the star
327 sp_star_finish (sc);
328 } else if (event_context->item_to_select) {
329 // no dragging, select clicked item if any
330 if (event->button.state & GDK_SHIFT_MASK) {
331 selection->toggle(event_context->item_to_select);
332 } else {
333 selection->set(event_context->item_to_select);
334 }
335 } else {
336 // click in an empty space
337 selection->clear();
338 }
340 event_context->item_to_select = NULL;
341 ret = TRUE;
342 sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
343 }
344 break;
345 case GDK_KEY_PRESS:
346 switch (get_group0_keyval(&event->key)) {
347 case GDK_Alt_R:
348 case GDK_Control_L:
349 case GDK_Control_R:
350 case GDK_Shift_L:
351 case GDK_Shift_R:
352 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
353 case GDK_Meta_R:
354 sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
355 _("<b>Ctrl</b>: snap angle; keep rays radial"),
356 NULL,
357 NULL);
358 break;
359 case GDK_Up:
360 case GDK_Down:
361 case GDK_KP_Up:
362 case GDK_KP_Down:
363 // prevent the zoom field from activation
364 if (!MOD__CTRL_ONLY)
365 ret = TRUE;
366 break;
367 case GDK_x:
368 case GDK_X:
369 if (MOD__ALT_ONLY) {
370 desktop->setToolboxFocusTo ("altx-star");
371 ret = TRUE;
372 }
373 break;
374 case GDK_Escape:
375 sp_desktop_selection(desktop)->clear();
376 //TODO: make dragging escapable by Esc
377 break;
379 case GDK_space:
380 if (dragging) {
381 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
382 event->button.time);
383 dragging = false;
384 if (!event_context->within_tolerance) {
385 // we've been dragging, finish the rect
386 sp_star_finish(sc);
387 }
388 // do not return true, so that space would work switching to selector
389 }
390 break;
392 default:
393 break;
394 }
395 break;
396 case GDK_KEY_RELEASE:
397 switch (get_group0_keyval (&event->key)) {
398 case GDK_Alt_L:
399 case GDK_Alt_R:
400 case GDK_Control_L:
401 case GDK_Control_R:
402 case GDK_Shift_L:
403 case GDK_Shift_R:
404 case GDK_Meta_L: // Meta is when you press Shift+Alt
405 case GDK_Meta_R:
406 event_context->defaultMessageContext()->clear();
407 break;
408 default:
409 break;
410 }
411 break;
412 default:
413 break;
414 }
416 if (!ret) {
417 if (((SPEventContextClass *) parent_class)->root_handler)
418 ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
419 }
421 return ret;
422 }
424 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
425 {
426 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
428 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
429 int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
431 if (!sc->item) {
433 if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
434 return;
435 }
437 /* Create object */
438 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
439 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
440 repr->setAttribute("sodipodi:type", "star");
442 /* Set style */
443 sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
445 sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
446 Inkscape::GC::release(repr);
447 sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
448 sc->item->updateRepr();
450 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
451 }
453 /* Snap corner point with no constraints */
454 SnapManager &m = desktop->namedview->snap_manager;
455 m.setup(desktop, true, sc->item);
456 Geom::Point pt2g = to_2geom(p);
457 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
459 Geom::Point const p0 = to_2geom(sp_desktop_dt2root_xy_point(desktop, sc->center));
460 Geom::Point const p1 = to_2geom(sp_desktop_dt2root_xy_point(desktop, from_2geom(pt2g)));
462 SPStar *star = SP_STAR(sc->item);
464 double const sides = (gdouble) sc->magnitude;
465 Geom::Point const d = p1 - p0;
466 Geom::Coord const r1 = Geom::L2(d);
467 double arg1 = atan2(from_2geom(d));
469 if (state & GDK_CONTROL_MASK) {
470 /* Snap angle */
471 arg1 = sp_round(arg1, M_PI / snaps);
472 }
474 sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
475 arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
477 /* status text */
478 GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
479 sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
480 ( sc->isflatsided?
481 _("<b>Polygon</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle")
482 : _("<b>Star</b>: radius %s, angle %5g°; with <b>Ctrl</b> to snap angle") ),
483 rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
485 g_string_free(rads, FALSE);
486 }
488 static void
489 sp_star_finish (SPStarContext * sc)
490 {
491 sc->_message_context->clear();
493 if (sc->item != NULL) {
494 SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
495 SPObject *object = SP_OBJECT(sc->item);
497 sp_shape_set_shape(SP_SHAPE(sc->item));
499 object->updateRepr(SP_OBJECT_WRITE_EXT);
501 sp_canvas_end_forced_full_redraws(desktop->canvas);
503 sp_desktop_selection(desktop)->set(sc->item);
504 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
505 _("Create star"));
507 sc->item = NULL;
508 }
509 }
511 /*
512 Local Variables:
513 mode:c++
514 c-file-style:"stroustrup"
515 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
516 indent-tabs-mode:nil
517 fill-column:99
518 End:
519 */
520 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :