Code

clean left warning on revision 12272
[inkscape.git] / src / spiral-context.cpp
1 #define __SP_SPIRAL_CONTEXT_C__
3 /*
4  * Spiral drawing context
5  *
6  * Authors:
7  *   Mitsuru Oka
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 1999-2001 Lauris Kaplinski
12  * Copyright (C) 2001-2002 Mitsuru Oka
13  *
14  * Released under GNU GPL
15  */
17 #include "config.h"
19 #include <gdk/gdkkeysyms.h>
20 #include <cstring>
21 #include <string>
23 #include "macros.h"
24 #include "display/sp-canvas.h"
25 #include "sp-spiral.h"
26 #include "document.h"
27 #include "sp-namedview.h"
28 #include "selection.h"
29 #include "desktop-handles.h"
30 #include "snap.h"
31 #include "desktop.h"
32 #include "desktop-style.h"
33 #include "message-context.h"
34 #include "pixmaps/cursor-spiral.xpm"
35 #include "spiral-context.h"
36 #include "sp-metrics.h"
37 #include <glibmm/i18n.h>
38 #include "object-edit.h"
39 #include "xml/repr.h"
40 #include "xml/node-event-vector.h"
41 #include "preferences.h"
42 #include "context-fns.h"
43 #include "shape-editor.h"
45 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
46 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
47 static void sp_spiral_context_dispose(GObject *object);
48 static void sp_spiral_context_setup(SPEventContext *ec);
49 static void sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
51 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
53 static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
54 static void sp_spiral_finish(SPSpiralContext *sc);
56 static SPEventContextClass *parent_class;
58 GtkType
59 sp_spiral_context_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SPSpiralContextClass),
65             NULL, NULL,
66             (GClassInitFunc) sp_spiral_context_class_init,
67             NULL, NULL,
68             sizeof(SPSpiralContext),
69             4,
70             (GInstanceInitFunc) sp_spiral_context_init,
71             NULL,    /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSpiralContext", &info, (GTypeFlags)0);
74     }
75     return type;
76 }
78 static void
79 sp_spiral_context_class_init(SPSpiralContextClass *klass)
80 {
81     GObjectClass *object_class = (GObjectClass *) klass;
82     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
84     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
86     object_class->dispose = sp_spiral_context_dispose;
88     event_context_class->setup = sp_spiral_context_setup;
89     event_context_class->set = sp_spiral_context_set;
90     event_context_class->root_handler = sp_spiral_context_root_handler;
91 }
93 static void
94 sp_spiral_context_init(SPSpiralContext *spiral_context)
95 {
96     SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context);
98     event_context->cursor_shape = cursor_spiral_xpm;
99     event_context->hot_x = 4;
100     event_context->hot_y = 4;
101     event_context->xp = 0;
102     event_context->yp = 0;
103     event_context->tolerance = 0;
104     event_context->within_tolerance = false;
105     event_context->item_to_select = NULL;
107     spiral_context->item = NULL;
109     spiral_context->revo = 3.0;
110     spiral_context->exp = 1.0;
111     spiral_context->t0 = 0.0;
113     new (&spiral_context->sel_changed_connection) sigc::connection();
116 static void
117 sp_spiral_context_dispose(GObject *object)
119     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(object);
120     SPEventContext *ec = SP_EVENT_CONTEXT(object);
122     ec->enableGrDrag(false);
124     sc->sel_changed_connection.disconnect();
125     sc->sel_changed_connection.~connection();
127     delete ec->shape_editor;
128     ec->shape_editor = NULL;
130     /* fixme: This is necessary because we do not grab */
131     if (sc->item) sp_spiral_finish(sc);
133     if (sc->_message_context) {
134         delete sc->_message_context;
135     }
137     G_OBJECT_CLASS(parent_class)->dispose(object);
140 /**
141 \brief  Callback that processes the "changed" signal on the selection;
142 destroys old and creates new knotholder
143 */
144 void
145 sp_spiral_context_selection_changed(Inkscape::Selection *selection, gpointer data)
147     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
148     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
150     ec->shape_editor->unset_item(SH_KNOTHOLDER);
151     SPItem *item = selection->singleItem();
152     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
155 static void
156 sp_spiral_context_setup(SPEventContext *ec)
158     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
160     if (((SPEventContextClass *) parent_class)->setup)
161         ((SPEventContextClass *) parent_class)->setup(ec);
163     sp_event_context_read(ec, "expansion");
164     sp_event_context_read(ec, "revolution");
165     sp_event_context_read(ec, "t0");
167     ec->shape_editor = new ShapeEditor(ec->desktop);
169     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
170     if (item) {
171         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
172     }
174     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
175     sc->sel_changed_connection.disconnect();
176     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
178     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
179     if (prefs->getBool("/tools/shapes/selcue")) {
180         ec->enableSelectionCue();
181     }
182     if (prefs->getBool("/tools/shapes/gradientdrag")) {
183         ec->enableGrDrag();
184     }
186     sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
189 static void
190 sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
192     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
193     Glib::ustring name = val->getEntryName();
195     if (name == "expansion") {
196         sc->exp = CLAMP(val->getDouble(), 0.0, 1000.0);
197     } else if (name == "revolution") {
198         sc->revo = CLAMP(val->getDouble(3.0), 0.05, 40.0);
199     } else if (name == "t0") {
200         sc->t0 = CLAMP(val->getDouble(), 0.0, 0.999);
201     }
204 static gint
205 sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
207     static gboolean dragging;
209     SPDesktop *desktop = event_context->desktop;
210     Inkscape::Selection *selection = sp_desktop_selection (desktop);
211     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
213     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
214     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
216     gint ret = FALSE;
218     switch (event->type) {
219         case GDK_BUTTON_PRESS:
220             if (event->button.button == 1 && !event_context->space_panning) {
222                 dragging = TRUE;
223                 sp_event_context_snap_window_open(event_context);
224                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
226                 SnapManager &m = desktop->namedview->snap_manager;
227                 m.setup(desktop);
228                 Geom::Point pt2g = to_2geom(sc->center);
229                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
230                 sc->center = from_2geom(pt2g);
232                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
233                                     ( GDK_KEY_PRESS_MASK |
234                                       GDK_BUTTON_RELEASE_MASK |
235                                       GDK_POINTER_MOTION_MASK |
236                                       GDK_POINTER_MOTION_HINT_MASK |
237                                       GDK_BUTTON_PRESS_MASK    ),
238                                     NULL, event->button.time);
239                 ret = TRUE;
240             }
241             break;
242         case GDK_MOTION_NOTIFY:
243             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
245                 if ( event_context->within_tolerance
246                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
247                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
248                     break; // do not drag if we're within tolerance from origin
249                 }
250                 // Once the user has moved farther than tolerance from the original location
251                 // (indicating they intend to draw, not click), then always process the
252                 // motion notify coordinates as given (no snapping back to origin)
253                 event_context->within_tolerance = false;
255                 Geom::Point const motion_w(event->motion.x, event->motion.y);
256                 Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
258                 SnapManager &m = desktop->namedview->snap_manager;
259                 m.setup(desktop, true, sc->item);
260                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt, Inkscape::SNAPSOURCE_HANDLE);
261                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
263                 gobble_motion_events(GDK_BUTTON1_MASK);
265                 ret = TRUE;
266             }
267             break;
268         case GDK_BUTTON_RELEASE:
269             event_context->xp = event_context->yp = 0;
270             if (event->button.button == 1 && !event_context->space_panning) {
271                 dragging = FALSE;
272                 sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
273                 if (!event_context->within_tolerance) {
274                     // we've been dragging, finish the spiral
275                     sp_spiral_finish(sc);
276                 } else if (event_context->item_to_select) {
277                     // no dragging, select clicked item if any
278                     if (event->button.state & GDK_SHIFT_MASK) {
279                         selection->toggle(event_context->item_to_select);
280                     } else {
281                         selection->set(event_context->item_to_select);
282                     }
283                 } else {
284                     // click in an empty space
285                     selection->clear();
286                 }
288                 event_context->item_to_select = NULL;
289                 ret = TRUE;
290                 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
291             }
292             break;
293         case GDK_KEY_PRESS:
294             switch (get_group0_keyval(&event->key)) {
295                 case GDK_Alt_R:
296                 case GDK_Control_L:
297                 case GDK_Control_R:
298                 case GDK_Shift_L:
299                 case GDK_Shift_R:
300                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
301                 case GDK_Meta_R:
302                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
303                                                _("<b>Ctrl</b>: snap angle"),
304                                                NULL,
305                                                _("<b>Alt</b>: lock spiral radius"));
306                     break;
307                 case GDK_Up:
308                 case GDK_Down:
309                 case GDK_KP_Up:
310                 case GDK_KP_Down:
311                     // prevent the zoom field from activation
312                     if (!MOD__CTRL_ONLY)
313                         ret = TRUE;
314                     break;
315                 case GDK_x:
316                 case GDK_X:
317                     if (MOD__ALT_ONLY) {
318                         desktop->setToolboxFocusTo ("altx-spiral");
319                         ret = TRUE;
320                     }
321                     break;
322                 case GDK_Escape:
323                     sp_desktop_selection(desktop)->clear();
324                     //TODO: make dragging escapable by Esc
325                     break;
327                 case GDK_space:
328                     if (dragging) {
329                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
330                                               event->button.time);
331                         dragging = false;
332                         sp_event_context_snap_window_closed(event_context);
333                         if (!event_context->within_tolerance) {
334                             // we've been dragging, finish the rect
335                             sp_spiral_finish(sc);
336                         }
337                         // do not return true, so that space would work switching to selector
338                     }
339                     break;
341                 default:
342                     break;
343             }
344             break;
345         case GDK_KEY_RELEASE:
346             switch (get_group0_keyval(&event->key)) {
347                 case GDK_Alt_L:
348                 case GDK_Alt_R:
349                 case GDK_Control_L:
350                 case GDK_Control_R:
351                 case GDK_Shift_L:
352                 case GDK_Shift_R:
353                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
354                 case GDK_Meta_R:
355                     event_context->defaultMessageContext()->clear();
356                     break;
357                 default:
358                     break;
359             }
360             break;
361         default:
362             break;
363     }
365     if (!ret) {
366         if (((SPEventContextClass *) parent_class)->root_handler)
367             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
368     }
370     return ret;
373 static void
374 sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
376     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
378     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
379     int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
381     if (!sc->item) {
383         if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
384             return;
385         }
387         /* Create object */
388         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
389         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
390         repr->setAttribute("sodipodi:type", "spiral");
392         /* Set style */
393         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
395         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
396         Inkscape::GC::release(repr);
397         sc->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
398         sc->item->updateRepr();
400         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
401     }
403     SnapManager &m = desktop->namedview->snap_manager;
404     m.setup(desktop, true, sc->item);
405     Geom::Point pt2g = to_2geom(p);
406     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
408     Geom::Point const p0 = desktop->dt2doc(sc->center);
409     Geom::Point const p1 = desktop->dt2doc(pt2g);
411     SPSpiral *spiral = SP_SPIRAL(sc->item);
413     Geom::Point const delta = p1 - p0;
414     gdouble const rad = Geom::L2(delta);
416     gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
418     if (state & GDK_CONTROL_MASK) {
419         arg = sp_round(arg, M_PI/snaps);
420     }
422     /* Fixme: these parameters should be got from dialog box */
423     sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y],
424                            /*expansion*/ sc->exp,
425                            /*revolution*/ sc->revo,
426                            rad, arg,
427                            /*t0*/ sc->t0);
429     /* status text */
430     GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric());
431     sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
432                                _("<b>Spiral</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle"),
433                                rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001));
434     g_string_free(rads, FALSE);
437 static void
438 sp_spiral_finish(SPSpiralContext *sc)
440     sc->_message_context->clear();
442     if (sc->item != NULL) {
443         SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
444         SPSpiral  *spiral = SP_SPIRAL(sc->item);
446         sp_shape_set_shape(SP_SHAPE(spiral));
447         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
449         sp_canvas_end_forced_full_redraws(desktop->canvas);
451         sp_desktop_selection(desktop)->set(sc->item);
452         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
453                          _("Create spiral"));
455         sc->item = NULL;
456     }
460 /*
461   Local Variables:
462   mode:c++
463   c-file-style:"stroustrup"
464   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
465   indent-tabs-mode:nil
466   fill-column:99
467   End:
468 */
469 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :