Code

Applying fixes for gcc 4.3 build issues (closes LP: #169115)
[inkscape.git] / src / star-context.cpp
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 "prefs-utils.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, const gchar *key, const gchar *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();
123 static void
124 sp_star_context_dispose (GObject *object)
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         sp_knot_holder_destroy (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);
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)
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         sp_knot_holder_destroy (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     }
199 static void
200 sp_star_context_setup (SPEventContext *ec)
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     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
230         ec->enableSelectionCue();
231     }
233     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
234         ec->enableGrDrag();
235     }
237     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
240 static void
241 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
243     SPStarContext *sc = SP_STAR_CONTEXT (ec);
244     if (!strcmp (key, "magnitude")) {
245         sc->magnitude = (val) ? atoi (val) : 5;
246         sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
247     } else if (!strcmp (key, "proportion")) {
248         sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
249         sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
250     } else if (!strcmp (key, "isflatsided")) {
251         if (val && !strcmp(val, "true"))
252             sc->isflatsided = true;
253         else
254             sc->isflatsided = false;
255     } else if (!strcmp (key, "rounded")) {
256         sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
257     } else if (!strcmp (key, "randomized")) {
258         sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
259     }
262 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
264     static gboolean dragging;
266     SPDesktop *desktop = event_context->desktop;
267     Inkscape::Selection *selection = sp_desktop_selection (desktop);
269     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
271     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
273     gint ret = FALSE;
275     switch (event->type) {
276     case GDK_BUTTON_PRESS:
277         if (event->button.button == 1 && !event_context->space_panning) {
279             dragging = TRUE;
281             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
283             SnapManager const &m = desktop->namedview->snap_manager;
284             sc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, sc->center, sc->item).getPoint();
286             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
287                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
288                                 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
289                                 NULL, event->button.time);
290             ret = TRUE;
291         }
292         break;
293     case GDK_MOTION_NOTIFY:
294         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
296             if ( event_context->within_tolerance
297                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
298                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
299                 break; // do not drag if we're within tolerance from origin
300             }
301             // Once the user has moved farther than tolerance from the original location
302             // (indicating they intend to draw, not click), then always process the
303             // motion notify coordinates as given (no snapping back to origin)
304             event_context->within_tolerance = false;
306             NR::Point const motion_w(event->motion.x, event->motion.y);
307             NR::Point motion_dt(event_context->desktop->w2d(motion_w));
308             
309             SnapManager const &m = desktop->namedview->snap_manager;
310             motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, sc->item).getPoint();
311             
312             sp_star_drag (sc, motion_dt, event->motion.state);
314             gobble_motion_events(GDK_BUTTON1_MASK);
316             ret = TRUE;
317         }
318         break;
319     case GDK_BUTTON_RELEASE:
320         event_context->xp = event_context->yp = 0;
321         if (event->button.button == 1 && !event_context->space_panning) {
322             dragging = FALSE;
323             if (!event_context->within_tolerance) {
324                 // we've been dragging, finish the star
325                 sp_star_finish (sc);
326             } else if (event_context->item_to_select) {
327                 // no dragging, select clicked item if any
328                 if (event->button.state & GDK_SHIFT_MASK) {
329                     selection->toggle(event_context->item_to_select);
330                 } else {
331                     selection->set(event_context->item_to_select);
332                 }
333             } else {
334                 // click in an empty space
335                 selection->clear();
336             }
338             event_context->item_to_select = NULL;
339             ret = TRUE;
340             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
341         }
342         break;
343     case GDK_KEY_PRESS:
344         switch (get_group0_keyval(&event->key)) {
345         case GDK_Alt_R:
346         case GDK_Control_L:
347         case GDK_Control_R:
348         case GDK_Shift_L:
349         case GDK_Shift_R:
350         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
351         case GDK_Meta_R:
352             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
353                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
354                                        NULL,
355                                        NULL);
356             break;
357         case GDK_Up:
358         case GDK_Down:
359         case GDK_KP_Up:
360         case GDK_KP_Down:
361             // prevent the zoom field from activation
362             if (!MOD__CTRL_ONLY)
363                 ret = TRUE;
364             break;
365         case GDK_x:
366         case GDK_X:
367             if (MOD__ALT_ONLY) {
368                 desktop->setToolboxFocusTo ("altx-star");
369                 ret = TRUE;
370             }
371             break;
372         case GDK_Escape:
373             sp_desktop_selection(desktop)->clear();
374             //TODO: make dragging escapable by Esc
375             break;
377         case GDK_space:
378             if (dragging) {
379                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
380                                       event->button.time);
381                 dragging = false;
382                 if (!event_context->within_tolerance) {
383                     // we've been dragging, finish the rect
384                     sp_star_finish(sc);
385                 }
386                 // do not return true, so that space would work switching to selector
387             }
388             break;
390         default:
391             break;
392         }
393         break;
394     case GDK_KEY_RELEASE:
395         switch (get_group0_keyval (&event->key)) {
396         case GDK_Alt_L:
397         case GDK_Alt_R:
398         case GDK_Control_L:
399         case GDK_Control_R:
400         case GDK_Shift_L:
401         case GDK_Shift_R:
402         case GDK_Meta_L:  // Meta is when you press Shift+Alt
403         case GDK_Meta_R:
404             event_context->defaultMessageContext()->clear();
405             break;
406         default:
407             break;
408         }
409         break;
410     default:
411         break;
412     }
414     if (!ret) {
415         if (((SPEventContextClass *) parent_class)->root_handler)
416             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
417     }
419     return ret;
422 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
424     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
426     int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
428     if (!sc->item) {
430         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
431             return;
432         }
434         /* Create object */
435         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
436         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
437         repr->setAttribute("sodipodi:type", "star");
439         /* Set style */
440         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
442         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
443         Inkscape::GC::release(repr);
444         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
445         sc->item->updateRepr();
447         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
448     }
450     NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
451     NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
453     /* Snap corner point with no constraints */
454     SnapManager const &m = desktop->namedview->snap_manager;
455     p1 = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p1, sc->item).getPoint();
457     SPStar *star = SP_STAR(sc->item);
459     double const sides = (gdouble) sc->magnitude;
460     NR::Point const d = p1 - p0;
461     NR::Coord const r1 = NR::L2(d);
462     double arg1 = atan2(d);
464     if (state & GDK_CONTROL_MASK) {
465         /* Snap angle */
466         arg1 = sp_round(arg1, M_PI / snaps);
467     }
469     sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
470                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
472     /* status text */
473     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
474     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
475                                ( sc->isflatsided?
476                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
477                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
478                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
480     g_string_free(rads, FALSE);
483 static void
484 sp_star_finish (SPStarContext * sc)
486     sc->_message_context->clear();
488     if (sc->item != NULL) {
489         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
490         SPObject *object = SP_OBJECT(sc->item);
492         sp_shape_set_shape(SP_SHAPE(sc->item));
494         object->updateRepr(SP_OBJECT_WRITE_EXT);
496         sp_canvas_end_forced_full_redraws(desktop->canvas);
498         sp_desktop_selection(desktop)->set(sc->item);
499         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR, 
500                          _("Create star"));
502         sc->item = NULL;
503     }
506 /*
507   Local Variables:
508   mode:c++
509   c-file-style:"stroustrup"
510   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
511   indent-tabs-mode:nil
512   fill-column:99
513   End:
514 */
515 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :