Code

make win32 compile using libxslt
[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 #include "config.h"
19 #include <gdk/gdkkeysyms.h>
21 #include "macros.h"
22 #include "display/sp-canvas.h"
23 #include "sp-star.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "desktop-affine.h"
29 #include "snap.h"
30 #include "desktop.h"
31 #include "desktop-style.h"
32 #include "message-context.h"
33 #include "pixmaps/cursor-star.xpm"
34 #include "sp-metrics.h"
35 #include <glibmm/i18n.h>
36 #include "prefs-utils.h"
37 #include "xml/repr.h"
38 #include "xml/node-event-vector.h"
39 #include "object-edit.h"
40 #include "context-fns.h"
42 #include "star-context.h"
44 static void sp_star_context_class_init (SPStarContextClass * klass);
45 static void sp_star_context_init (SPStarContext * star_context);
46 static void sp_star_context_dispose (GObject *object);
48 static void sp_star_context_setup (SPEventContext *ec);
49 static void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
50 static gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
52 static void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
53 static void sp_star_finish (SPStarContext * sc);
55 static SPEventContextClass * parent_class;
57 GtkType
58 sp_star_context_get_type (void)
59 {
60     static GType type = 0;
61     if (!type) {
62         GTypeInfo info = {
63             sizeof (SPStarContextClass),
64             NULL, NULL,
65             (GClassInitFunc) sp_star_context_class_init,
66             NULL, NULL,
67             sizeof (SPStarContext),
68             4,
69             (GInstanceInitFunc) sp_star_context_init,
70             NULL,    /* value_table */
71         };
72         type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
73     }
74     return type;
75 }
77 static void
78 sp_star_context_class_init (SPStarContextClass * klass)
79 {
80     GObjectClass *object_class = (GObjectClass *) klass;
81     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83     parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
85     object_class->dispose = sp_star_context_dispose;
87     event_context_class->setup = sp_star_context_setup;
88     event_context_class->set = sp_star_context_set;
89     event_context_class->root_handler = sp_star_context_root_handler;
90 }
92 static void
93 sp_star_context_init (SPStarContext * star_context)
94 {
95     SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
97     event_context->cursor_shape = cursor_star_xpm;
98     event_context->hot_x = 4;
99     event_context->hot_y = 4;
100     event_context->xp = 0;
101     event_context->yp = 0;
102     event_context->tolerance = 0;
103     event_context->within_tolerance = false;
104     event_context->item_to_select = NULL;
106     event_context->shape_repr = NULL;
107     event_context->shape_knot_holder = NULL;
109     star_context->item = NULL;
111     star_context->magnitude = 5;
112     star_context->proportion = 0.5;
113     star_context->isflatsided = false;
115     new (&star_context->sel_changed_connection) sigc::connection();
118 static void
119 sp_star_context_dispose (GObject *object)
121     SPEventContext *ec = SP_EVENT_CONTEXT (object);
122     SPStarContext *sc = SP_STAR_CONTEXT (object);
124     ec->enableGrDrag(false);
126     sc->sel_changed_connection.disconnect();
127     sc->sel_changed_connection.~connection();
129     if (ec->shape_knot_holder) {
130         sp_knot_holder_destroy (ec->shape_knot_holder);
131         ec->shape_knot_holder = NULL;
132     }
134     if (ec->shape_repr) { // remove old listener
135         sp_repr_remove_listener_by_data (ec->shape_repr, ec);
136         Inkscape::GC::release(ec->shape_repr);
137         ec->shape_repr = 0;
138     }
140     /* fixme: This is necessary because we do not grab */
141     if (sc->item) sp_star_finish (sc);
143     if (sc->_message_context) {
144         delete sc->_message_context;
145     }
147     G_OBJECT_CLASS (parent_class)->dispose (object);
150 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
151     NULL, /* child_added */
152     NULL, /* child_removed */
153     ec_shape_event_attr_changed,
154     NULL, /* content_changed */
155     NULL  /* order_changed */
156 };
158 /**
159 \brief  Callback that processes the "changed" signal on the selection;
160 destroys old and creates new knotholder
161 \param  selection Should not be NULL.
162 */
163 void
164 sp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
166     g_assert (selection != NULL);
168     SPStarContext *sc = SP_STAR_CONTEXT (data);
169     SPEventContext *ec = SP_EVENT_CONTEXT (sc);
171     if (ec->shape_knot_holder) { // desktroy knotholder
172         sp_knot_holder_destroy (ec->shape_knot_holder);
173         ec->shape_knot_holder = NULL;
174     }
176     if (ec->shape_repr) { // remove old listener
177         sp_repr_remove_listener_by_data (ec->shape_repr, ec);
178         Inkscape::GC::release(ec->shape_repr);
179         ec->shape_repr = 0;
180     }
182     SPItem *item = selection->singleItem();
183     if (item) {
184         ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
185         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
186         if (shape_repr) {
187             ec->shape_repr = shape_repr;
188             Inkscape::GC::anchor(shape_repr);
189             sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
190         }
191     }
194 static void
195 sp_star_context_setup (SPEventContext *ec)
197    SPStarContext *sc = SP_STAR_CONTEXT (ec);
199     if (((SPEventContextClass *) parent_class)->setup)
200         ((SPEventContextClass *) parent_class)->setup (ec);
202     sp_event_context_read (ec, "magnitude");
203     sp_event_context_read (ec, "proportion");
204     sp_event_context_read (ec, "isflatsided");
205     sp_event_context_read (ec, "rounded");
206     sp_event_context_read (ec, "randomized");
208     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
210     SPItem *item = selection->singleItem();
211         if (item) {
212             ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
213             Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
214             if (shape_repr) {
215                 ec->shape_repr = shape_repr;
216                 Inkscape::GC::anchor(shape_repr);
217                 sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
218             }
219         }
221     sc->sel_changed_connection.disconnect();
222     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
224     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
225         ec->enableSelectionCue();
226     }
228     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
229         ec->enableGrDrag();
230     }
232     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
235 static void
236 sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
238     SPStarContext *sc = SP_STAR_CONTEXT (ec);
239     if (!strcmp (key, "magnitude")) {
240         sc->magnitude = (val) ? atoi (val) : 5;
241         sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
242     } else if (!strcmp (key, "proportion")) {
243         sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
244         sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
245     } else if (!strcmp (key, "isflatsided")) {
246         if (val && !strcmp(val, "true"))
247             sc->isflatsided = true;
248         else
249             sc->isflatsided = false;
250     } else if (!strcmp (key, "rounded")) {
251         sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
252     } else if (!strcmp (key, "randomized")) {
253         sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
254     }
257 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
259     static gboolean dragging;
261     SPDesktop *desktop = event_context->desktop;
262     Inkscape::Selection *selection = sp_desktop_selection (desktop);
264     SPStarContext *sc = SP_STAR_CONTEXT (event_context);
266     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
268     gint ret = FALSE;
270     switch (event->type) {
271     case GDK_BUTTON_PRESS:
272         if (event->button.button == 1 && !event_context->space_panning) {
274             dragging = TRUE;
276             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
278             SnapManager const &m = desktop->namedview->snap_manager;
279             sc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, sc->center, sc->item).getPoint();
281             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
282                                 GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
283                                 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
284                                 NULL, event->button.time);
285             ret = TRUE;
286         }
287         break;
288     case GDK_MOTION_NOTIFY:
289         if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
291             if ( event_context->within_tolerance
292                  && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
293                  && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
294                 break; // do not drag if we're within tolerance from origin
295             }
296             // Once the user has moved farther than tolerance from the original location
297             // (indicating they intend to draw, not click), then always process the
298             // motion notify coordinates as given (no snapping back to origin)
299             event_context->within_tolerance = false;
301             NR::Point const motion_w(event->motion.x, event->motion.y);
302             NR::Point motion_dt(event_context->desktop->w2d(motion_w));
303             
304             SnapManager const &m = desktop->namedview->snap_manager;
305             motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, sc->item).getPoint();
306             
307             sp_star_drag (sc, motion_dt, event->motion.state);
309             gobble_motion_events(GDK_BUTTON1_MASK);
311             ret = TRUE;
312         }
313         break;
314     case GDK_BUTTON_RELEASE:
315         event_context->xp = event_context->yp = 0;
316         if (event->button.button == 1 && !event_context->space_panning) {
317             dragging = FALSE;
318             if (!event_context->within_tolerance) {
319                 // we've been dragging, finish the star
320                 sp_star_finish (sc);
321             } else if (event_context->item_to_select) {
322                 // no dragging, select clicked item if any
323                 if (event->button.state & GDK_SHIFT_MASK) {
324                     selection->toggle(event_context->item_to_select);
325                 } else {
326                     selection->set(event_context->item_to_select);
327                 }
328             } else {
329                 // click in an empty space
330                 selection->clear();
331             }
333             event_context->item_to_select = NULL;
334             ret = TRUE;
335             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
336         }
337         break;
338     case GDK_KEY_PRESS:
339         switch (get_group0_keyval(&event->key)) {
340         case GDK_Alt_R:
341         case GDK_Control_L:
342         case GDK_Control_R:
343         case GDK_Shift_L:
344         case GDK_Shift_R:
345         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
346         case GDK_Meta_R:
347             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
348                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
349                                        NULL,
350                                        NULL);
351             break;
352         case GDK_Up:
353         case GDK_Down:
354         case GDK_KP_Up:
355         case GDK_KP_Down:
356             // prevent the zoom field from activation
357             if (!MOD__CTRL_ONLY)
358                 ret = TRUE;
359             break;
360         case GDK_x:
361         case GDK_X:
362             if (MOD__ALT_ONLY) {
363                 desktop->setToolboxFocusTo ("altx-star");
364                 ret = TRUE;
365             }
366             break;
367         case GDK_Escape:
368             sp_desktop_selection(desktop)->clear();
369             //TODO: make dragging escapable by Esc
370             break;
372         case GDK_space:
373             if (dragging) {
374                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
375                                       event->button.time);
376                 dragging = false;
377                 if (!event_context->within_tolerance) {
378                     // we've been dragging, finish the rect
379                     sp_star_finish(sc);
380                 }
381                 // do not return true, so that space would work switching to selector
382             }
383             break;
385         default:
386             break;
387         }
388         break;
389     case GDK_KEY_RELEASE:
390         switch (get_group0_keyval (&event->key)) {
391         case GDK_Alt_L:
392         case GDK_Alt_R:
393         case GDK_Control_L:
394         case GDK_Control_R:
395         case GDK_Shift_L:
396         case GDK_Shift_R:
397         case GDK_Meta_L:  // Meta is when you press Shift+Alt
398         case GDK_Meta_R:
399             event_context->defaultMessageContext()->clear();
400             break;
401         default:
402             break;
403         }
404         break;
405     default:
406         break;
407     }
409     if (!ret) {
410         if (((SPEventContextClass *) parent_class)->root_handler)
411             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
412     }
414     return ret;
417 static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
419     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
421     int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
423     if (!sc->item) {
425         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
426             return;
427         }
429         /* Create object */
430         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
431         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
432         repr->setAttribute("sodipodi:type", "star");
434         /* Set style */
435         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.star", false);
437         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
438         Inkscape::GC::release(repr);
439         sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
440         sc->item->updateRepr();
442         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
443     }
445     NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
446     NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
448     /* Snap corner point with no constraints */
449     SnapManager const &m = desktop->namedview->snap_manager;
450     p1 = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p1, sc->item).getPoint();
452     SPStar *star = SP_STAR(sc->item);
454     double const sides = (gdouble) sc->magnitude;
455     NR::Point const d = p1 - p0;
456     NR::Coord const r1 = NR::L2(d);
457     double arg1 = atan2(d);
459     if (state & GDK_CONTROL_MASK) {
460         /* Snap angle */
461         arg1 = sp_round(arg1, M_PI / snaps);
462     }
464     sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
465                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
467     /* status text */
468     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
469     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
470                                ( sc->isflatsided?
471                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
472                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
473                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
475     g_string_free(rads, FALSE);
478 static void
479 sp_star_finish (SPStarContext * sc)
481     sc->_message_context->clear();
483     if (sc->item != NULL) {
484         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
485         SPObject *object = SP_OBJECT(sc->item);
487         sp_shape_set_shape(SP_SHAPE(sc->item));
489         object->updateRepr(SP_OBJECT_WRITE_EXT);
491         sp_canvas_end_forced_full_redraws(desktop->canvas);
493         sp_desktop_selection(desktop)->set(sc->item);
494         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR, 
495                          _("Create star"));
497         sc->item = NULL;
498     }
501 /*
502   Local Variables:
503   mode:c++
504   c-file-style:"stroustrup"
505   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
506   indent-tabs-mode:nil
507   fill-column:99
508   End:
509 */
510 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :