Code

implement select next/prev as verbs
[inkscape.git] / src / draw-context.cpp
1 #define __SP_DRAW_CONTEXT_C__
3 /*
4  * Generic drawing context
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2000 Lauris Kaplinski
10  * Copyright (C) 2000-2001 Ximian, Inc.
11  * Copyright (C) 2002 Lauris Kaplinski
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #define DRAW_VERBOSE
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "display/canvas-bpath.h"
24 #include "xml/repr.h"
25 #include "svg/svg.h"
26 #include <glibmm/i18n.h>
27 #include "libnr/n-art-bpath.h"
28 #include "desktop.h"
29 #include "desktop-affine.h"
30 #include "desktop-handles.h"
31 #include "desktop-style.h"
32 #include "document.h"
33 #include "draw-anchor.h"
34 #include "macros.h"
35 #include "message-stack.h"
36 #include "pen-context.h"
37 #include "prefs-utils.h"
38 #include "selection.h"
39 #include "selection-chemistry.h"
40 #include "snap.h"
41 #include "sp-path.h"
42 #include "sp-namedview.h"
44 static void sp_draw_context_class_init(SPDrawContextClass *klass);
45 static void sp_draw_context_init(SPDrawContext *dc);
46 static void sp_draw_context_dispose(GObject *object);
48 static void sp_draw_context_setup(SPEventContext *ec);
49 static void sp_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *value);
50 static void sp_draw_context_finish(SPEventContext *ec);
52 static gint sp_draw_context_root_handler(SPEventContext *event_context, GdkEvent *event);
54 static void spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc);
55 static void spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc);
57 static void spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel);
59 static void spdc_flush_white(SPDrawContext *dc, SPCurve *gc);
61 static void spdc_reset_white(SPDrawContext *dc);
62 static void spdc_free_colors(SPDrawContext *dc);
65 static SPEventContextClass *draw_parent_class;
68 GType
69 sp_draw_context_get_type(void)
70 {
71     static GType type = 0;
72     if (!type) {
73         GTypeInfo info = {
74             sizeof(SPDrawContextClass),
75             NULL, NULL,
76             (GClassInitFunc) sp_draw_context_class_init,
77             NULL, NULL,
78             sizeof(SPDrawContext),
79             4,
80             (GInstanceInitFunc) sp_draw_context_init,
81             NULL,   /* value_table */
82         };
83         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDrawContext", &info, (GTypeFlags)0);
84     }
85     return type;
86 }
88 static void
89 sp_draw_context_class_init(SPDrawContextClass *klass)
90 {
91     GObjectClass *object_class;
92     SPEventContextClass *ec_class;
94     object_class = (GObjectClass *)klass;
95     ec_class = (SPEventContextClass *) klass;
97     draw_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
99     object_class->dispose = sp_draw_context_dispose;
101     ec_class->setup = sp_draw_context_setup;
102     ec_class->set = sp_draw_context_set;
103     ec_class->finish = sp_draw_context_finish;
104     ec_class->root_handler = sp_draw_context_root_handler;
107 static void
108 sp_draw_context_init(SPDrawContext *dc)
110     dc->attach = FALSE;
112     dc->red_color = 0xff00007f;
113     dc->blue_color = 0x0000ff7f;
114     dc->green_color = 0x00ff007f;
115     dc->red_curve_is_valid = false;
117     new (&dc->sel_changed_connection) sigc::connection();
118     new (&dc->sel_modified_connection) sigc::connection();
121 static void
122 sp_draw_context_dispose(GObject *object)
124     SPDrawContext *dc = SP_DRAW_CONTEXT(object);
126     dc->sel_changed_connection.~connection();
127     dc->sel_modified_connection.~connection();
129     if (dc->grab) {
130         sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
131         dc->grab = NULL;
132     }
134     if (dc->selection) {
135         dc->selection = NULL;
136     }
138     spdc_free_colors(dc);
140     G_OBJECT_CLASS(draw_parent_class)->dispose(object);
143 static void
144 sp_draw_context_setup(SPEventContext *ec)
146     SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
147     SPDesktop *dt = ec->desktop;
149     if (((SPEventContextClass *) draw_parent_class)->setup) {
150         ((SPEventContextClass *) draw_parent_class)->setup(ec);
151     }
153     dc->selection = sp_desktop_selection(dt);
155     /* Connect signals to track selection changes */
156     dc->sel_changed_connection = dc->selection->connectChanged(
157         sigc::bind(sigc::ptr_fun(&spdc_selection_changed), dc)
158     );
159     dc->sel_modified_connection = dc->selection->connectModified(
160         sigc::bind(sigc::ptr_fun(&spdc_selection_modified), dc)
161     );
163     /* Create red bpath */
164     dc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
165     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->red_bpath), dc->red_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
166     /* Create red curve */
167     dc->red_curve = sp_curve_new_sized(4);
169     /* Create blue bpath */
170     dc->blue_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
171     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->blue_bpath), dc->blue_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
172     /* Create blue curve */
173     dc->blue_curve = sp_curve_new_sized(8);
175     /* Create green curve */
176     dc->green_curve = sp_curve_new_sized(64);
177     /* No green anchor by default */
178     dc->green_anchor = NULL;
179     dc->green_closed = FALSE;
181     dc->attach = TRUE;
182     spdc_attach_selection(dc, dc->selection);
185 static void
186 sp_draw_context_finish(SPEventContext *ec)
188     SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
190     dc->sel_changed_connection.disconnect();
191     dc->sel_modified_connection.disconnect();
193     if (dc->grab) {
194         sp_canvas_item_ungrab(dc->grab, GDK_CURRENT_TIME);
195     }
197     if (dc->selection) {
198         dc->selection = NULL;
199     }
201     spdc_free_colors(dc);
204 static void
205 sp_draw_context_set(SPEventContext *ec, const gchar *key, const gchar *value)
209 gint
210 sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
212     //SPDrawContext *dc = SP_DRAW_CONTEXT(ec);
213     SPDesktop *desktop = ec->desktop;
215     gint ret = FALSE;
217     switch (event->type) {
218         case GDK_KEY_PRESS:
219             switch (get_group0_keyval (&event->key)) {
220                 case GDK_Escape:
221                     sp_desktop_selection(desktop)->clear();
222                     ret = TRUE;
223                     break;
224                 case GDK_Up:
225                 case GDK_Down:
226                 case GDK_KP_Up:
227                 case GDK_KP_Down:
228                     // prevent the zoom field from activation
229                     if (!MOD__CTRL_ONLY) {
230                         ret = TRUE;
231                     }
232                     break;
233                 default:
234             break;
235         }
236         break;
237     default:
238         break;
239     }
241     if (!ret) {
242         if (((SPEventContextClass *) draw_parent_class)->root_handler) {
243             ret = ((SPEventContextClass *) draw_parent_class)->root_handler(ec, event);
244         }
245     }
247     return ret;
251 /*
252  * Selection handlers
253  */
255 static void
256 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
258     if (dc->attach) {
259         spdc_attach_selection(dc, sel);
260     }
263 /* fixme: We have to ensure this is not delayed (Lauris) */
265 static void
266 spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc)
268     if (dc->attach) {
269         spdc_attach_selection(dc, sel);
270     }
273 static void
274 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel)
276     /* We reset white and forget white/start/end anchors */
277     spdc_reset_white(dc);
278     dc->sa = NULL;
279     dc->ea = NULL;
281     SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
283     if ( item && SP_IS_PATH(item) ) {
284         /* Create new white data */
285         /* Item */
286         dc->white_item = item;
287         /* Curve list */
288         /* We keep it in desktop coordinates to eliminate calculation errors */
289         SPCurve *norm = sp_shape_get_curve(SP_SHAPE(item));
290         sp_curve_transform(norm, sp_item_i2d_affine(dc->white_item));
291         g_return_if_fail( norm != NULL );
292         dc->white_curves = g_slist_reverse(sp_curve_split(norm));
293         sp_curve_unref(norm);
294         /* Anchor list */
295         for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
296             SPCurve *c;
297             c = (SPCurve*)l->data;
298             g_return_if_fail( c->end > 1 );
299             if ( SP_CURVE_BPATH(c)->code == NR_MOVETO_OPEN ) {
300                 NArtBpath *s, *e;
301                 SPDrawAnchor *a;
302                 s = sp_curve_first_bpath(c);
303                 e = sp_curve_last_bpath(c);
304                 a = sp_draw_anchor_new(dc, c, TRUE, NR::Point(s->x3, s->y3));
305                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
306                 a = sp_draw_anchor_new(dc, c, FALSE, NR::Point(e->x3, e->y3));
307                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
308             }
309         }
310         /* fixme: recalculate active anchor? */
311     }
315 /**
316  *  Snaps node or handle to PI/rotationsnapsperpi degree increments.
317  *
318  *  \param dc draw context
319  *  \param p cursor point (to be changed by snapping)
320  *  \param o origin point
321  *  \param state  keyboard state to check if ctrl was pressed
322 */
324 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, NR::Point &p, NR::Point const o,
325                                  guint state)
327     /* Control must be down for this snap to work */
328     if ((state & GDK_CONTROL_MASK) == 0) {
329         return;
330     }
332     unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
333     /* 0 means no snapping. */
335     /* mirrored by fabs, so this corresponds to 15 degrees */
336     NR::Point best; /* best solution */
337     double bn = NR_HUGE; /* best normal */
338     double bdot = 0;
339     NR::Point v = NR::Point(0, 1);
340     double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
341     double const r10 = -r01, r11 = r00;
343     NR::Point delta = p - o;
345     for (unsigned i = 0; i < snaps; i++) {
346         double const ndot = fabs(dot(v,NR::rot90(delta)));
347         NR::Point t(r00*v[NR::X] + r01*v[NR::Y],
348                     r10*v[NR::X] + r11*v[NR::Y]);
349         if (ndot < bn) {
350             /* I think it is better numerically to use the normal, rather than the dot product
351              * to assess solutions, but I haven't proven it. */
352             bn = ndot;
353             best = v;
354             bdot = dot(v, delta);
355         }
356         v = t;
357     }
359     if (fabs(bdot) > 0) {
360         p = o + bdot * best;
362         /* Snap it along best vector */
363         SnapManager const &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
364         p = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
365                               p, Inkscape::Snapper::ConstraintLine(best), NULL).getPoint();
366     }
370 void spdc_endpoint_snap_free(SPEventContext const * const ec, NR::Point& p, guint const state)
372     /* Shift disables this snap */
373     if (state & GDK_SHIFT_MASK) {
374         return;
375     }
377     /* FIXME: this should be doing bbox snap as well */
378     SnapManager const &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
379     p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
382 static SPCurve *
383 reverse_then_unref(SPCurve *orig)
385     SPCurve *ret = sp_curve_reverse(orig);
386     sp_curve_unref(orig);
387     return ret;
390 /**
391  * Concats red, blue and green.
392  * If any anchors are defined, process these, optionally removing curves from white list
393  * Invoke _flush_white to write result back to object.
394  */
395 void
396 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
398     /* Concat RBG */
399     SPCurve *c = dc->green_curve;
401     /* Green */
402     dc->green_curve = sp_curve_new_sized(64);
403     while (dc->green_bpaths) {
404         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
405         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
406     }
407     /* Blue */
408     sp_curve_append_continuous(c, dc->blue_curve, 0.0625);
409     sp_curve_reset(dc->blue_curve);
410     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
411     /* Red */
412     if (dc->red_curve_is_valid) {
413         sp_curve_append_continuous(c, dc->red_curve, 0.0625);
414     }
415     sp_curve_reset(dc->red_curve);
416     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
418     if (sp_curve_empty(c)) {
419         sp_curve_unref(c);
420         return;
421     }
423     /* Step A - test, whether we ended on green anchor */
424     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
425         // We hit green anchor, closing Green-Blue-Red
426         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
427         sp_curve_closepath_current(c);
428         /* Closed path, just flush */
429         spdc_flush_white(dc, c);
430         sp_curve_unref(c);
431         return;
432     }
434     /* Step B - both start and end anchored to same curve */
435     if ( dc->sa && dc->ea
436          && ( dc->sa->curve == dc->ea->curve )
437          && ( ( dc->sa != dc->ea )
438               || dc->sa->curve->closed ) )
439     {
440         // We hit bot start and end of single curve, closing paths
441         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
442         if (dc->sa->start && !(dc->sa->curve->closed) ) {
443             c = reverse_then_unref(c);
444         }
445         sp_curve_append_continuous(dc->sa->curve, c, 0.0625);
446         sp_curve_unref(c);
447         sp_curve_closepath_current(dc->sa->curve);
448         spdc_flush_white(dc, NULL);
449         return;
450     }
452     /* Step C - test start */
453     if (dc->sa) {
454         SPCurve *s = dc->sa->curve;
455         dc->white_curves = g_slist_remove(dc->white_curves, s);
456         if (dc->sa->start) {
457             s = reverse_then_unref(s);
458         }
459         sp_curve_append_continuous(s, c, 0.0625);
460         sp_curve_unref(c);
461         c = s;
462     } else /* Step D - test end */ if (dc->ea) {
463         SPCurve *e = dc->ea->curve;
464         dc->white_curves = g_slist_remove(dc->white_curves, e);
465         if (!dc->ea->start) {
466             e = reverse_then_unref(e);
467         }
468         sp_curve_append_continuous(c, e, 0.0625);
469         sp_curve_unref(e);
470     }
473     spdc_flush_white(dc, c);
475     sp_curve_unref(c);
478 static char const *
479 tool_name(SPDrawContext *dc)
481     return ( SP_IS_PEN_CONTEXT(dc)
482              ? "tools.freehand.pen"
483              : "tools.freehand.pencil" );
486 /*
487  * Flushes white curve(s) and additional curve into object
488  *
489  * No cleaning of colored curves - this has to be done by caller
490  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
491  *
492  */
494 static void
495 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
497     SPCurve *c;
499     if (dc->white_curves) {
500         g_assert(dc->white_item);
501         c = sp_curve_concat(dc->white_curves);
502         g_slist_free(dc->white_curves);
503         dc->white_curves = NULL;
504         if (gc) {
505             sp_curve_append(c, gc, FALSE);
506         }
507     } else if (gc) {
508         c = gc;
509         sp_curve_ref(c);
510     } else {
511         return;
512     }
514     /* Now we have to go back to item coordinates at last */
515     sp_curve_transform(c, ( dc->white_item
516                             ? sp_item_dt2i_affine(dc->white_item)
517                             : sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc)) ));
519     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
520     SPDocument *doc = sp_desktop_document(desktop);
522     if ( c && !sp_curve_empty(c) ) {
523         /* We actually have something to write */
525         Inkscape::XML::Node *repr;
526         if (dc->white_item) {
527             repr = SP_OBJECT_REPR(dc->white_item);
528         } else {
529             repr = sp_repr_new("svg:path");
530             /* Set style */
531             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
532         }
534         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
535         g_assert( str != NULL );
536         repr->setAttribute("d", str);
537         g_free(str);
539         if (!dc->white_item) {
540             /* Attach repr */
541             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
542             dc->selection->set(repr);
543             Inkscape::GC::release(repr);
544             item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
545             item->updateRepr();
546         }
548         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, 
549                          _("Draw path"));
550     }
552     sp_curve_unref(c);
554     /* Flush pending updates */
555     sp_document_ensure_up_to_date(doc);
558 /**
559  * Returns FIRST active anchor (the activated one).
560  */
561 SPDrawAnchor *
562 spdc_test_inside(SPDrawContext *dc, NR::Point p)
564     SPDrawAnchor *active = NULL;
566     /* Test green anchor */
567     if (dc->green_anchor) {
568         active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
569     }
571     for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
572         SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
573         if ( !active && na ) {
574             active = na;
575         }
576     }
578     return active;
581 static void
582 spdc_reset_white(SPDrawContext *dc)
584     if (dc->white_item) {
585         /* We do not hold refcount */
586         dc->white_item = NULL;
587     }
588     while (dc->white_curves) {
589         sp_curve_unref((SPCurve *) dc->white_curves->data);
590         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
591     }
592     while (dc->white_anchors) {
593         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
594         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
595     }
598 static void
599 spdc_free_colors(SPDrawContext *dc)
601     /* Red */
602     if (dc->red_bpath) {
603         gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
604         dc->red_bpath = NULL;
605     }
606     if (dc->red_curve) {
607         dc->red_curve = sp_curve_unref(dc->red_curve);
608     }
609     /* Blue */
610     if (dc->blue_bpath) {
611         gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
612         dc->blue_bpath = NULL;
613     }
614     if (dc->blue_curve) {
615         dc->blue_curve = sp_curve_unref(dc->blue_curve);
616     }
617     /* Green */
618     while (dc->green_bpaths) {
619         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
620         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
621     }
622     if (dc->green_curve) {
623         dc->green_curve = sp_curve_unref(dc->green_curve);
624     }
625     if (dc->green_anchor) {
626         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
627     }
628     /* White */
629     if (dc->white_item) {
630         /* We do not hold refcount */
631         dc->white_item = NULL;
632     }
633     while (dc->white_curves) {
634         sp_curve_unref((SPCurve *) dc->white_curves->data);
635         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
636     }
637     while (dc->white_anchors) {
638         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
639         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
640     }
644 /*
645   Local Variables:
646   mode:c++
647   c-file-style:"stroustrup"
648   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
649   indent-tabs-mode:nil
650   fill-column:99
651   End:
652 */
653 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :