Code

switch to using shape_editor, instead of separate knotholders and listeners; fixes...
[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 "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)
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();
121 static void
122 sp_star_context_dispose (GObject *object)
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);
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)
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);
163 static void
164 sp_star_context_setup (SPEventContext *ec)
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());
200 static void
201 sp_star_context_set (SPEventContext *ec, Inkscape::Preferences::Entry *val)
203     SPStarContext *sc = SP_STAR_CONTEXT (ec);
204     Glib::ustring path = val->getEntryName();
205     
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     }
219 static gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
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;
239             sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
240             
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);
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));
272             
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             if (!event_context->within_tolerance) {
285                 // we've been dragging, finish the star
286                 sp_star_finish (sc);
287             } else if (event_context->item_to_select) {
288                 // no dragging, select clicked item if any
289                 if (event->button.state & GDK_SHIFT_MASK) {
290                     selection->toggle(event_context->item_to_select);
291                 } else {
292                     selection->set(event_context->item_to_select);
293                 }
294             } else {
295                 // click in an empty space
296                 selection->clear();
297             }
299             event_context->item_to_select = NULL;
300             ret = TRUE;
301             sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
302         }
303         break;
304     case GDK_KEY_PRESS:
305         switch (get_group0_keyval(&event->key)) {
306         case GDK_Alt_R:
307         case GDK_Control_L:
308         case GDK_Control_R:
309         case GDK_Shift_L:
310         case GDK_Shift_R:
311         case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
312         case GDK_Meta_R:
313             sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
314                                        _("<b>Ctrl</b>: snap angle; keep rays radial"),
315                                        NULL,
316                                        NULL);
317             break;
318         case GDK_Up:
319         case GDK_Down:
320         case GDK_KP_Up:
321         case GDK_KP_Down:
322             // prevent the zoom field from activation
323             if (!MOD__CTRL_ONLY)
324                 ret = TRUE;
325             break;
326         case GDK_x:
327         case GDK_X:
328             if (MOD__ALT_ONLY) {
329                 desktop->setToolboxFocusTo ("altx-star");
330                 ret = TRUE;
331             }
332             break;
333         case GDK_Escape:
334             sp_desktop_selection(desktop)->clear();
335             //TODO: make dragging escapable by Esc
336             break;
338         case GDK_space:
339             if (dragging) {
340                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
341                                       event->button.time);
342                 dragging = false;
343                 if (!event_context->within_tolerance) {
344                     // we've been dragging, finish the rect
345                     sp_star_finish(sc);
346                 }
347                 // do not return true, so that space would work switching to selector
348             }
349             break;
351         default:
352             break;
353         }
354         break;
355     case GDK_KEY_RELEASE:
356         switch (get_group0_keyval (&event->key)) {
357         case GDK_Alt_L:
358         case GDK_Alt_R:
359         case GDK_Control_L:
360         case GDK_Control_R:
361         case GDK_Shift_L:
362         case GDK_Shift_R:
363         case GDK_Meta_L:  // Meta is when you press Shift+Alt
364         case GDK_Meta_R:
365             event_context->defaultMessageContext()->clear();
366             break;
367         default:
368             break;
369         }
370         break;
371     default:
372         break;
373     }
375     if (!ret) {
376         if (((SPEventContextClass *) parent_class)->root_handler)
377             ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
378     }
380     return ret;
383 static void sp_star_drag(SPStarContext *sc, Geom::Point p, guint state)
385     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
387     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
388     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
390     if (!sc->item) {
392         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
393             return;
394         }
396         /* Create object */
397         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
398         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
399         repr->setAttribute("sodipodi:type", "star");
401         /* Set style */
402         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
404         sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
405         Inkscape::GC::release(repr);
406         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
407         sc->item->updateRepr();
409         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
410     }
412     /* Snap corner point with no constraints */
413     SnapManager &m = desktop->namedview->snap_manager;
414     m.setup(desktop, true, sc->item);
415     Geom::Point pt2g = to_2geom(p);
416     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);    
417     
418     Geom::Point const p0 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, sc->center));
419     Geom::Point const p1 = to_2geom(sp_desktop_dt2doc_xy_point(desktop, from_2geom(pt2g)));
420     
421     SPStar *star = SP_STAR(sc->item);
423     double const sides = (gdouble) sc->magnitude;
424     Geom::Point const d = p1 - p0;
425     Geom::Coord const r1 = Geom::L2(d);
426     double arg1 = atan2(from_2geom(d));
428     if (state & GDK_CONTROL_MASK) {
429         /* Snap angle */
430         arg1 = sp_round(arg1, M_PI / snaps);
431     }
433     sp_star_position_set(star, sc->magnitude, from_2geom(p0), r1, r1 * sc->proportion,
434                          arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
436     /* status text */
437     GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
438     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
439                                ( sc->isflatsided?
440                                  _("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
441                                  : _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
442                                rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
444     g_string_free(rads, FALSE);
447 static void
448 sp_star_finish (SPStarContext * sc)
450     sc->_message_context->clear();
452     if (sc->item != NULL) {
453         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
454         SPObject *object = SP_OBJECT(sc->item);
456         sp_shape_set_shape(SP_SHAPE(sc->item));
458         object->updateRepr(SP_OBJECT_WRITE_EXT);
460         sp_canvas_end_forced_full_redraws(desktop->canvas);
462         sp_desktop_selection(desktop)->set(sc->item);
463         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR, 
464                          _("Create star"));
466         sc->item = NULL;
467     }
470 /*
471   Local Variables:
472   mode:c++
473   c-file-style:"stroustrup"
474   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
475   indent-tabs-mode:nil
476   fill-column:99
477   End:
478 */
479 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :