Code

Adding axis detection to new input dialog
[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
270 sp_guide_create_guides_around_page(SPDocument *doc) {
271     std::list<std::pair<Geom::Point, Geom::Point> > pts;
273     Geom::Point A(0, 0);
274     Geom::Point C(sp_document_width(doc), sp_document_height(doc));
275     Geom::Point B(C[Geom::X], 0);
276     Geom::Point D(0, C[Geom::Y]);
278     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(A, B));
279     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(B, C));
280     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(C, D));
281     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(D, A));
283     sp_guide_pt_pairs_to_guides(doc, pts);
285     sp_document_done (doc, SP_VERB_NONE, _("Guides around page"));
288 void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
290     SPCanvasItem *item = sp_guideline_new(group, guide->point_on_line, guide->normal_to_line.to_2geom());
291     sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
293     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
295     guide->views = g_slist_prepend(guide->views, item);
298 void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
300     g_assert(guide != NULL);
301     g_assert(SP_IS_GUIDE(guide));
302     g_assert(canvas != NULL);
303     g_assert(SP_IS_CANVAS(canvas));
305     for (GSList *l = guide->views; l != NULL; l = l->next) {
306         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
307             gtk_object_destroy(GTK_OBJECT(l->data));
308             guide->views = g_slist_remove(guide->views, l->data);
309             return;
310         }
311     }
313     g_assert_not_reached();
316 void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
318     g_assert(guide != NULL);
319     g_assert(SP_IS_GUIDE(guide));
320     g_assert(canvas != NULL);
321     g_assert(SP_IS_CANVAS(canvas));
323     for (GSList *l = guide->views; l != NULL; l = l->next) {
324         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
325             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
326             return;
327         }
328     }
330     g_assert_not_reached();
333 Geom::Point sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
335     return -(pt.to_2geom() - guide->point_on_line);
338 double sp_guide_distance_from_pt(SPGuide const *guide, Geom::Point const &pt)
340     return dot(pt - guide->point_on_line, guide->normal_to_line);
343 /**
344  * \arg commit False indicates temporary moveto in response to motion event while dragging,
345  *      true indicates a "committing" version: in response to button release event after
346  *      dragging a guideline, or clicking OK in guide editing dialog.
347  */
348 void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
350     g_assert(SP_IS_GUIDE(&guide));
352     for (GSList *l = guide.views; l != NULL; l = l->next) {
353         sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
354     }
356     /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
357        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
358     if (commit) {
359         sp_repr_set_point(SP_OBJECT(&guide)->repr, "position", point_on_line);
360     }
362 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
363     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
364              iEnd(guide.attached_items.end());
365          i != iEnd; ++i)
366     {
367         SPGuideAttachment const &att = *i;
368         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
369     }
370 */
373 /**
374  * \arg commit False indicates temporary moveto in response to motion event while dragging,
375  *      true indicates a "committing" version: in response to button release event after
376  *      dragging a guideline, or clicking OK in guide editing dialog.
377  */
378 void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line, bool const commit)
380     g_assert(SP_IS_GUIDE(&guide));
382     for (GSList *l = guide.views; l != NULL; l = l->next) {
383         sp_guideline_set_normal(SP_GUIDELINE(l->data), normal_to_line);
384     }
386     /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
387        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
388     if (commit) {
389         sp_repr_set_point(SP_OBJECT(&guide)->repr, "orientation", normal_to_line);
390     }
392 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
393     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
394              iEnd(guide.attached_items.end());
395          i != iEnd; ++i)
396     {
397         SPGuideAttachment const &att = *i;
398         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
399     }
400 */
403 /**
404  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
405  *
406  * The caller is responsible for freeing the string.
407  */
408 char *sp_guide_description(SPGuide const *guide)
410     using NR::X;
411     using NR::Y;
412             
413     GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
414     GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
416     if ( guide->normal_to_line ==  component_vectors[X] ||
417          guide->normal_to_line == -component_vectors[X]) {
418         return g_strdup_printf(_("vertical, at %s"), position_string_x->str);
419     } else if ( guide->normal_to_line == component_vectors[Y] ||
420                 guide->normal_to_line == -component_vectors[Y]) {
421         return g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
422     } else {
423         double const radians = guide->angle();
424         double const degrees = Geom::rad_to_deg(radians);
425         int const degrees_int = (int) round(degrees);
426         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);
427     }
429     g_string_free(position_string_x, TRUE);
430     g_string_free(position_string_y, TRUE);
433 void sp_guide_remove(SPGuide *guide)
435     g_assert(SP_IS_GUIDE(guide));
437     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
438              iEnd(guide->attached_items.end());
439          i != iEnd; ++i)
440     {
441         SPGuideAttachment const &att = *i;
442         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
443     }
444     guide->attached_items.clear();
446     sp_repr_unparent(SP_OBJECT(guide)->repr);
449 /*
450   Local Variables:
451   mode:c++
452   c-file-style:"stroustrup"
453   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
454   indent-tabs-mode:nil
455   fill-column:99
456   End:
457 */
458 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :