Code

Purged unused variable
[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     gint ret = FALSE;
214     switch (event->type) {
215         case GDK_KEY_PRESS:
216             switch (get_group0_keyval (&event->key)) {
217                 case GDK_Up:
218                 case GDK_Down:
219                 case GDK_KP_Up:
220                 case GDK_KP_Down:
221                     // prevent the zoom field from activation
222                     if (!MOD__CTRL_ONLY) {
223                         ret = TRUE;
224                     }
225                     break;
226                 default:
227             break;
228         }
229         break;
230     default:
231         break;
232     }
234     if (!ret) {
235         if (((SPEventContextClass *) draw_parent_class)->root_handler) {
236             ret = ((SPEventContextClass *) draw_parent_class)->root_handler(ec, event);
237         }
238     }
240     return ret;
244 /*
245  * Selection handlers
246  */
248 static void
249 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
251     if (dc->attach) {
252         spdc_attach_selection(dc, sel);
253     }
256 /* fixme: We have to ensure this is not delayed (Lauris) */
258 static void
259 spdc_selection_modified(Inkscape::Selection *sel, guint flags, SPDrawContext *dc)
261     if (dc->attach) {
262         spdc_attach_selection(dc, sel);
263     }
266 static void
267 spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection *sel)
269     /* We reset white and forget white/start/end anchors */
270     spdc_reset_white(dc);
271     dc->sa = NULL;
272     dc->ea = NULL;
274     SPItem *item = dc->selection ? dc->selection->singleItem() : NULL;
276     if ( item && SP_IS_PATH(item) ) {
277         /* Create new white data */
278         /* Item */
279         dc->white_item = item;
280         /* Curve list */
281         /* We keep it in desktop coordinates to eliminate calculation errors */
282         SPCurve *norm = sp_shape_get_curve(SP_SHAPE(item));
283         sp_curve_transform(norm, sp_item_i2d_affine(dc->white_item));
284         g_return_if_fail( norm != NULL );
285         dc->white_curves = g_slist_reverse(sp_curve_split(norm));
286         sp_curve_unref(norm);
287         /* Anchor list */
288         for (GSList *l = dc->white_curves; l != NULL; l = l->next) {
289             SPCurve *c;
290             c = (SPCurve*)l->data;
291             g_return_if_fail( c->end > 1 );
292             if ( SP_CURVE_BPATH(c)->code == NR_MOVETO_OPEN ) {
293                 NArtBpath *s, *e;
294                 SPDrawAnchor *a;
295                 s = sp_curve_first_bpath(c);
296                 e = sp_curve_last_bpath(c);
297                 a = sp_draw_anchor_new(dc, c, TRUE, NR::Point(s->x3, s->y3));
298                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
299                 a = sp_draw_anchor_new(dc, c, FALSE, NR::Point(e->x3, e->y3));
300                 dc->white_anchors = g_slist_prepend(dc->white_anchors, a);
301             }
302         }
303         /* fixme: recalculate active anchor? */
304     }
308 /**
309  *  Snaps node or handle to PI/rotationsnapsperpi degree increments.
310  *
311  *  \param dc draw context
312  *  \param p cursor point (to be changed by snapping)
313  *  \param o origin point
314  *  \param state  keyboard state to check if ctrl was pressed
315 */
317 void spdc_endpoint_snap_rotation(SPEventContext const *const ec, NR::Point &p, NR::Point const o,
318                                  guint state)
320     /* Control must be down for this snap to work */
321     if ((state & GDK_CONTROL_MASK) == 0) {
322         return;
323     }
325     unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12));
326     /* 0 means no snapping. */
328     /* mirrored by fabs, so this corresponds to 15 degrees */
329     NR::Point best; /* best solution */
330     double bn = NR_HUGE; /* best normal */
331     double bdot = 0;
332     NR::Point v = NR::Point(0, 1);
333     double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps);
334     double const r10 = -r01, r11 = r00;
336     NR::Point delta = p - o;
338     for (unsigned i = 0; i < snaps; i++) {
339         double const ndot = fabs(dot(v,NR::rot90(delta)));
340         NR::Point t(r00*v[NR::X] + r01*v[NR::Y],
341                     r10*v[NR::X] + r11*v[NR::Y]);
342         if (ndot < bn) {
343             /* I think it is better numerically to use the normal, rather than the dot product
344              * to assess solutions, but I haven't proven it. */
345             bn = ndot;
346             best = v;
347             bdot = dot(v, delta);
348         }
349         v = t;
350     }
352     if (fabs(bdot) > 0) {
353         p = o + bdot * best;
355         /* Snap it along best vector */
356         SnapManager const &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
357         p = m.constrainedSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
358                               p, Inkscape::Snapper::ConstraintLine(best), NULL).getPoint();
359     }
363 void spdc_endpoint_snap_free(SPEventContext const * const ec, NR::Point& p, guint const state)
365     /* Shift disables this snap */
366     if (state & GDK_SHIFT_MASK) {
367         return;
368     }
370     /* FIXME: this should be doing bbox snap as well */
371     SnapManager const &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager;
372     p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
375 static SPCurve *
376 reverse_then_unref(SPCurve *orig)
378     SPCurve *ret = sp_curve_reverse(orig);
379     sp_curve_unref(orig);
380     return ret;
383 /**
384  * Concats red, blue and green.
385  * If any anchors are defined, process these, optionally removing curves from white list
386  * Invoke _flush_white to write result back to object.
387  */
388 void
389 spdc_concat_colors_and_flush(SPDrawContext *dc, gboolean forceclosed)
391     /* Concat RBG */
392     SPCurve *c = dc->green_curve;
394     /* Green */
395     dc->green_curve = sp_curve_new_sized(64);
396     while (dc->green_bpaths) {
397         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
398         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
399     }
400     /* Blue */
401     sp_curve_append_continuous(c, dc->blue_curve, 0.0625);
402     sp_curve_reset(dc->blue_curve);
403     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->blue_bpath), NULL);
404     /* Red */
405     if (dc->red_curve_is_valid) {
406         sp_curve_append_continuous(c, dc->red_curve, 0.0625);
407     }
408     sp_curve_reset(dc->red_curve);
409     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->red_bpath), NULL);
411     if (sp_curve_empty(c)) {
412         sp_curve_unref(c);
413         return;
414     }
416     /* Step A - test, whether we ended on green anchor */
417     if ( forceclosed || ( dc->green_anchor && dc->green_anchor->active ) ) {
418         // We hit green anchor, closing Green-Blue-Red
419         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Path is closed."));
420         sp_curve_closepath_current(c);
421         /* Closed path, just flush */
422         spdc_flush_white(dc, c);
423         sp_curve_unref(c);
424         return;
425     }
427     /* Step B - both start and end anchored to same curve */
428     if ( dc->sa && dc->ea
429          && ( dc->sa->curve == dc->ea->curve )
430          && ( ( dc->sa != dc->ea )
431               || dc->sa->curve->closed ) )
432     {
433         // We hit bot start and end of single curve, closing paths
434         SP_EVENT_CONTEXT_DESKTOP(dc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Closing path."));
435         if (dc->sa->start && !(dc->sa->curve->closed) ) {
436             c = reverse_then_unref(c);
437         }
438         sp_curve_append_continuous(dc->sa->curve, c, 0.0625);
439         sp_curve_unref(c);
440         sp_curve_closepath_current(dc->sa->curve);
441         spdc_flush_white(dc, NULL);
442         return;
443     }
445     /* Step C - test start */
446     if (dc->sa) {
447         SPCurve *s = dc->sa->curve;
448         dc->white_curves = g_slist_remove(dc->white_curves, s);
449         if (dc->sa->start) {
450             s = reverse_then_unref(s);
451         }
452         sp_curve_append_continuous(s, c, 0.0625);
453         sp_curve_unref(c);
454         c = s;
455     } else /* Step D - test end */ if (dc->ea) {
456         SPCurve *e = dc->ea->curve;
457         dc->white_curves = g_slist_remove(dc->white_curves, e);
458         if (!dc->ea->start) {
459             e = reverse_then_unref(e);
460         }
461         sp_curve_append_continuous(c, e, 0.0625);
462         sp_curve_unref(e);
463     }
466     spdc_flush_white(dc, c);
468     sp_curve_unref(c);
471 static char const *
472 tool_name(SPDrawContext *dc)
474     return ( SP_IS_PEN_CONTEXT(dc)
475              ? "tools.freehand.pen"
476              : "tools.freehand.pencil" );
479 /*
480  * Flushes white curve(s) and additional curve into object
481  *
482  * No cleaning of colored curves - this has to be done by caller
483  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
484  *
485  */
487 static void
488 spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
490     SPCurve *c;
492     if (dc->white_curves) {
493         g_assert(dc->white_item);
494         c = sp_curve_concat(dc->white_curves);
495         g_slist_free(dc->white_curves);
496         dc->white_curves = NULL;
497         if (gc) {
498             sp_curve_append(c, gc, FALSE);
499         }
500     } else if (gc) {
501         c = gc;
502         sp_curve_ref(c);
503     } else {
504         return;
505     }
507     /* Now we have to go back to item coordinates at last */
508     sp_curve_transform(c, ( dc->white_item
509                             ? sp_item_dt2i_affine(dc->white_item)
510                             : sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc)) ));
512     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
513     SPDocument *doc = sp_desktop_document(desktop);
514     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
516     if ( c && !sp_curve_empty(c) ) {
517         /* We actually have something to write */
519         Inkscape::XML::Node *repr;
520         if (dc->white_item) {
521             repr = SP_OBJECT_REPR(dc->white_item);
522         } else {
523             repr = xml_doc->createElement("svg:path");
524             /* Set style */
525             sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false);
526         }
528         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
529         g_assert( str != NULL );
530         repr->setAttribute("d", str);
531         g_free(str);
533         if (!dc->white_item) {
534             /* Attach repr */
535             SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
536             dc->selection->set(repr);
537             Inkscape::GC::release(repr);
538             item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
539             item->updateRepr();
540         }
542         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, 
543                          _("Draw path"));
544     }
546     sp_curve_unref(c);
548     /* Flush pending updates */
549     sp_document_ensure_up_to_date(doc);
552 /**
553  * Returns FIRST active anchor (the activated one).
554  */
555 SPDrawAnchor *
556 spdc_test_inside(SPDrawContext *dc, NR::Point p)
558     SPDrawAnchor *active = NULL;
560     /* Test green anchor */
561     if (dc->green_anchor) {
562         active = sp_draw_anchor_test(dc->green_anchor, p, TRUE);
563     }
565     for (GSList *l = dc->white_anchors; l != NULL; l = l->next) {
566         SPDrawAnchor *na = sp_draw_anchor_test((SPDrawAnchor *) l->data, p, !active);
567         if ( !active && na ) {
568             active = na;
569         }
570     }
572     return active;
575 static void
576 spdc_reset_white(SPDrawContext *dc)
578     if (dc->white_item) {
579         /* We do not hold refcount */
580         dc->white_item = NULL;
581     }
582     while (dc->white_curves) {
583         sp_curve_unref((SPCurve *) dc->white_curves->data);
584         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
585     }
586     while (dc->white_anchors) {
587         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
588         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
589     }
592 static void
593 spdc_free_colors(SPDrawContext *dc)
595     /* Red */
596     if (dc->red_bpath) {
597         gtk_object_destroy(GTK_OBJECT(dc->red_bpath));
598         dc->red_bpath = NULL;
599     }
600     if (dc->red_curve) {
601         dc->red_curve = sp_curve_unref(dc->red_curve);
602     }
603     /* Blue */
604     if (dc->blue_bpath) {
605         gtk_object_destroy(GTK_OBJECT(dc->blue_bpath));
606         dc->blue_bpath = NULL;
607     }
608     if (dc->blue_curve) {
609         dc->blue_curve = sp_curve_unref(dc->blue_curve);
610     }
611     /* Green */
612     while (dc->green_bpaths) {
613         gtk_object_destroy(GTK_OBJECT(dc->green_bpaths->data));
614         dc->green_bpaths = g_slist_remove(dc->green_bpaths, dc->green_bpaths->data);
615     }
616     if (dc->green_curve) {
617         dc->green_curve = sp_curve_unref(dc->green_curve);
618     }
619     if (dc->green_anchor) {
620         dc->green_anchor = sp_draw_anchor_destroy(dc->green_anchor);
621     }
622     /* White */
623     if (dc->white_item) {
624         /* We do not hold refcount */
625         dc->white_item = NULL;
626     }
627     while (dc->white_curves) {
628         sp_curve_unref((SPCurve *) dc->white_curves->data);
629         dc->white_curves = g_slist_remove(dc->white_curves, dc->white_curves->data);
630     }
631     while (dc->white_anchors) {
632         sp_draw_anchor_destroy((SPDrawAnchor *) dc->white_anchors->data);
633         dc->white_anchors = g_slist_remove(dc->white_anchors, dc->white_anchors->data);
634     }
638 /*
639   Local Variables:
640   mode:c++
641   c-file-style:"stroustrup"
642   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
643   indent-tabs-mode:nil
644   fill-column:99
645   End:
646 */
647 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :