Code

Applying fixes for gcc 4.3 build issues (closes LP: #169115)
[inkscape.git] / src / sp-guide.cpp
1 #define __SP_GUIDE_C__
3 /*
4  * Inkscape guideline implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
9  *   Johan Engelen
10  *
11  * Copyright (C) 2000-2002 authors
12  * Copyright (C) 2004 Monash University
13  * Copyright (C) 2007 Johan Engelen
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <algorithm>
23 #include <cstring>
24 #include <string>
25 #include "display/guideline.h"
26 #include "svg/svg.h"
27 #include "svg/stringstream.h"
28 #include "attributes.h"
29 #include "sp-guide.h"
30 #include <sp-item-notify-moveto.h>
31 #include <sp-item.h>
32 #include <sp-guide-constraint.h>
33 #include <glibmm/i18n.h>
34 #include <xml/repr.h>
35 #include <remove-last.h>
36 #include "sp-metrics.h"
37 #include "inkscape.h"
38 #include "desktop.h"
39 #include "sp-namedview.h"
40 #include <2geom/angle.h>
41 #include "document.h"
43 using std::vector;
45 enum {
46     PROP_0,
47     PROP_COLOR,
48     PROP_HICOLOR
49 };
51 static void sp_guide_class_init(SPGuideClass *gc);
52 static void sp_guide_init(SPGuide *guide);
53 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
54 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
56 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
57 static void sp_guide_release(SPObject *object);
58 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
60 static SPObjectClass *parent_class;
62 GType sp_guide_get_type(void)
63 {
64     static GType guide_type = 0;
66     if (!guide_type) {
67         GTypeInfo guide_info = {
68             sizeof(SPGuideClass),
69             NULL, NULL,
70             (GClassInitFunc) sp_guide_class_init,
71             NULL, NULL,
72             sizeof(SPGuide),
73             16,
74             (GInstanceInitFunc) sp_guide_init,
75             NULL,       /* value_table */
76         };
77         guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
78     }
80     return guide_type;
81 }
83 static void sp_guide_class_init(SPGuideClass *gc)
84 {
85     GObjectClass *gobject_class = (GObjectClass *) gc;
86     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
88     parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
90     gobject_class->set_property = sp_guide_set_property;
91     gobject_class->get_property = sp_guide_get_property;
93     sp_object_class->build = sp_guide_build;
94     sp_object_class->release = sp_guide_release;
95     sp_object_class->set = sp_guide_set;
97     g_object_class_install_property(gobject_class,
98                                     PROP_COLOR,
99                                     g_param_spec_uint("color", "Color", "Color",
100                                                       0,
101                                                       0xffffffff,
102                                                       0xff000000,
103                                                       (GParamFlags) G_PARAM_READWRITE));
105     g_object_class_install_property(gobject_class,
106                                     PROP_HICOLOR,
107                                     g_param_spec_uint("hicolor", "HiColor", "HiColor",
108                                                       0,
109                                                       0xffffffff,
110                                                       0xff000000,
111                                                       (GParamFlags) G_PARAM_READWRITE));
114 static void sp_guide_init(SPGuide *guide)
116     guide->normal_to_line = component_vectors[NR::Y];
117     guide->point_on_line = Geom::Point(0.,0.);
118     guide->color = 0x0000ff7f;
119     guide->hicolor = 0xff00007f;
122 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
124     SPGuide &guide = *SP_GUIDE(object);
126     switch (prop_id) {
127         case PROP_COLOR:
128             guide.color = g_value_get_uint(value);
129             for (GSList *l = guide.views; l != NULL; l = l->next) {
130                 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
131             }
132             break;
134         case PROP_HICOLOR:
135             guide.hicolor = g_value_get_uint(value);
136             break;
137     }
140 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
142     SPGuide const &guide = *SP_GUIDE(object);
144     switch (prop_id) {
145         case PROP_COLOR:
146             g_value_set_uint(value, guide.color);
147             break;
148         case PROP_HICOLOR:
149             g_value_set_uint(value, guide.hicolor);
150             break;
151     }
154 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
156     if (((SPObjectClass *) (parent_class))->build) {
157         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
158     }
160     sp_object_read_attr(object, "orientation");
161     sp_object_read_attr(object, "position");
164 static void sp_guide_release(SPObject *object)
166     SPGuide *guide = (SPGuide *) object;
168     while (guide->views) {
169         gtk_object_destroy(GTK_OBJECT(guide->views->data));
170         guide->views = g_slist_remove(guide->views, guide->views->data);
171     }
173     if (((SPObjectClass *) parent_class)->release) {
174         ((SPObjectClass *) parent_class)->release(object);
175     }
178 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
180     SPGuide *guide = SP_GUIDE(object);
182     switch (key) {
183     case SP_ATTR_ORIENTATION:
184         {
185             if (value && !strcmp(value, "horizontal")) {
186                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
187                 guide->normal_to_line = component_vectors[NR::Y];
188             } else if (value && !strcmp(value, "vertical")) {
189                 guide->normal_to_line = component_vectors[NR::X];
190             } else if (value) {
191                 gchar ** strarray = g_strsplit(value, ",", 2);
192                 double newx, newy;
193                 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
194                 success += sp_svg_number_read_d(strarray[1], &newy);
195                 g_strfreev (strarray);
196                 if (success == 2) {
197                     Geom::Point direction(newx, newy);
198                     direction.normalize();
199                     guide->normal_to_line = direction;
200                 } else {
201                     // default to vertical line for bad arguments
202                     guide->normal_to_line = component_vectors[NR::X];
203                 }
204             } else {
205                 // default to vertical line for bad arguments
206                 guide->normal_to_line = component_vectors[NR::X];
207             }
208             sp_guide_set_normal(*guide, guide->normal_to_line.to_2geom(), false);
209         }
210         break;
211     case SP_ATTR_POSITION:
212         {
213             gchar ** strarray = g_strsplit(value, ",", 2);
214             double newx, newy;
215             unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
216             success += sp_svg_number_read_d(strarray[1], &newy);
217             g_strfreev (strarray);
218             if (success == 2) {
219                 guide->point_on_line = Geom::Point(newx, newy);
220             } else if (success == 1) {
221                 // before 0.46 style guideline definition.
222                 const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
223                 if (attr && !strcmp(attr, "horizontal")) {
224                     guide->point_on_line = Geom::Point(0, newx);
225                 } else {
226                     guide->point_on_line = Geom::Point(newx, 0);
227                 }
228             }
230             // update position in non-committing way
231             // fixme: perhaps we need to add an update method instead, and request_update here
232             sp_guide_moveto(*guide, guide->point_on_line, false);
233         }
234         break;
235     default:
236             if (((SPObjectClass *) (parent_class))->set) {
237                 ((SPObjectClass *) (parent_class))->set(object, key, value);
238             }
239             break;
240     }
243 SPGuide *
244 sp_guide_create(SPDocument *doc, Geom::Point const &pt1, Geom::Point const &pt2) {
245     SPDesktop *desktop = inkscape_active_desktop();
246     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
248     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
250     Geom::Point n = Geom::rot90(pt2 - pt1);
252     sp_repr_set_point(repr, "position", pt1);
253     sp_repr_set_point(repr, "orientation", n);
255     SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
256     Inkscape::GC::release(repr);
258     SPGuide *guide= SP_GUIDE(doc->getObjectByRepr(repr));
259     return guide;
262 void
263 sp_guide_pt_pairs_to_guides(SPDocument *doc, std::list<std::pair<Geom::Point, Geom::Point> > &pts) {
264     for (std::list<std::pair<Geom::Point, Geom::Point> >::iterator i = pts.begin(); i != pts.end(); ++i) {
265         sp_guide_create(doc, (*i).first, (*i).second);
266     }
269 void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
271     SPCanvasItem *item = sp_guideline_new(group, guide->point_on_line, guide->normal_to_line.to_2geom());
272     sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
274     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
276     guide->views = g_slist_prepend(guide->views, item);
279 void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
281     g_assert(guide != NULL);
282     g_assert(SP_IS_GUIDE(guide));
283     g_assert(canvas != NULL);
284     g_assert(SP_IS_CANVAS(canvas));
286     for (GSList *l = guide->views; l != NULL; l = l->next) {
287         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
288             gtk_object_destroy(GTK_OBJECT(l->data));
289             guide->views = g_slist_remove(guide->views, l->data);
290             return;
291         }
292     }
294     g_assert_not_reached();
297 void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
299     g_assert(guide != NULL);
300     g_assert(SP_IS_GUIDE(guide));
301     g_assert(canvas != NULL);
302     g_assert(SP_IS_CANVAS(canvas));
304     for (GSList *l = guide->views; l != NULL; l = l->next) {
305         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
306             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
307             return;
308         }
309     }
311     g_assert_not_reached();
314 Geom::Point sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
316     return -(pt.to_2geom() - guide->point_on_line);
319 double sp_guide_distance_from_pt(SPGuide const *guide, Geom::Point const &pt)
321     return dot(pt - guide->point_on_line, guide->normal_to_line);
324 /**
325  * \arg commit False indicates temporary moveto in response to motion event while dragging,
326  *      true indicates a "committing" version: in response to button release event after
327  *      dragging a guideline, or clicking OK in guide editing dialog.
328  */
329 void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
331     g_assert(SP_IS_GUIDE(&guide));
333     for (GSList *l = guide.views; l != NULL; l = l->next) {
334         sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
335     }
337     /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
338        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
339     if (commit) {
340         sp_repr_set_point(SP_OBJECT(&guide)->repr, "position", point_on_line);
341     }
343 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
344     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
345              iEnd(guide.attached_items.end());
346          i != iEnd; ++i)
347     {
348         SPGuideAttachment const &att = *i;
349         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
350     }
351 */
354 /**
355  * \arg commit False indicates temporary moveto in response to motion event while dragging,
356  *      true indicates a "committing" version: in response to button release event after
357  *      dragging a guideline, or clicking OK in guide editing dialog.
358  */
359 void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line, bool const commit)
361     g_assert(SP_IS_GUIDE(&guide));
363     for (GSList *l = guide.views; l != NULL; l = l->next) {
364         sp_guideline_set_normal(SP_GUIDELINE(l->data), normal_to_line);
365     }
367     /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
368        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
369     if (commit) {
370         sp_repr_set_point(SP_OBJECT(&guide)->repr, "orientation", normal_to_line);
371     }
373 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
374     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
375              iEnd(guide.attached_items.end());
376          i != iEnd; ++i)
377     {
378         SPGuideAttachment const &att = *i;
379         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
380     }
381 */
384 /**
385  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
386  *
387  * The caller is responsible for freeing the string.
388  */
389 char *sp_guide_description(SPGuide const *guide)
391     using NR::X;
392     using NR::Y;
393             
394     GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
395     GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
397     if ( guide->normal_to_line == component_vectors[X] ) {
398         return g_strdup_printf(_("vertical, at %s"), position_string_x->str);
399     } else if ( guide->normal_to_line == component_vectors[Y] ) {
400         return g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
401     } else {
402         double const radians = guide->angle();
403         double const degrees = Geom::rad_to_deg(radians);
404         int const degrees_int = (int) round(degrees);
405         return g_strdup_printf(_("at %d degrees, through (%s,%s); <b>Ctrl</b>+click to delete"), degrees_int, position_string_x->str, position_string_y->str);
406     }
408     g_string_free(position_string_x, TRUE);
409     g_string_free(position_string_y, TRUE);
412 void sp_guide_remove(SPGuide *guide)
414     g_assert(SP_IS_GUIDE(guide));
416     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
417              iEnd(guide->attached_items.end());
418          i != iEnd; ++i)
419     {
420         SPGuideAttachment const &att = *i;
421         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
422     }
423     guide->attached_items.clear();
425     sp_repr_unparent(SP_OBJECT(guide)->repr);
428 /*
429   Local Variables:
430   mode:c++
431   c-file-style:"stroustrup"
432   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
433   indent-tabs-mode:nil
434   fill-column:99
435   End:
436 */
437 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :