Code

rename guide parameters to match snapping code naming convention.
[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
21 #include "display/guideline.h"
22 #include "svg/svg.h"
23 #include "attributes.h"
24 #include "sp-guide.h"
25 #include <sp-item-notify-moveto.h>
26 #include <sp-item.h>
27 #include <sp-guide-constraint.h>
28 #include <glibmm/i18n.h>
29 #include <xml/repr.h>
30 #include <remove-last.h>
31 using std::vector;
33 enum {
34     PROP_0,
35     PROP_COLOR,
36     PROP_HICOLOR
37 };
39 static void sp_guide_class_init(SPGuideClass *gc);
40 static void sp_guide_init(SPGuide *guide);
41 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
42 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
44 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
45 static void sp_guide_release(SPObject *object);
46 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
48 static SPObjectClass *parent_class;
50 GType sp_guide_get_type(void)
51 {
52     static GType guide_type = 0;
54     if (!guide_type) {
55         GTypeInfo guide_info = {
56             sizeof(SPGuideClass),
57             NULL, NULL,
58             (GClassInitFunc) sp_guide_class_init,
59             NULL, NULL,
60             sizeof(SPGuide),
61             16,
62             (GInstanceInitFunc) sp_guide_init,
63             NULL,       /* value_table */
64         };
65         guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
66     }
68     return guide_type;
69 }
71 static void sp_guide_class_init(SPGuideClass *gc)
72 {
73     GObjectClass *gobject_class = (GObjectClass *) gc;
74     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
76     parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
78     gobject_class->set_property = sp_guide_set_property;
79     gobject_class->get_property = sp_guide_get_property;
81     sp_object_class->build = sp_guide_build;
82     sp_object_class->release = sp_guide_release;
83     sp_object_class->set = sp_guide_set;
85     g_object_class_install_property(gobject_class,
86                                     PROP_COLOR,
87                                     g_param_spec_uint("color", "Color", "Color",
88                                                       0,
89                                                       0xffffffff,
90                                                       0xff000000,
91                                                       (GParamFlags) G_PARAM_READWRITE));
93     g_object_class_install_property(gobject_class,
94                                     PROP_HICOLOR,
95                                     g_param_spec_uint("hicolor", "HiColor", "HiColor",
96                                                       0,
97                                                       0xffffffff,
98                                                       0xff000000,
99                                                       (GParamFlags) G_PARAM_READWRITE));
102 static void sp_guide_init(SPGuide *guide)
104     guide->normal_to_line = component_vectors[NR::Y];
105     /* constrain y coordinate; horizontal line.  I doubt it ever matters what we initialize
106        this to. */
107     guide->position = 0.0;
108     guide->color = 0x0000ff7f;
109     guide->hicolor = 0xff00007f;
112 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
114     SPGuide &guide = *SP_GUIDE(object);
116     switch (prop_id) {
117         case PROP_COLOR:
118             guide.color = g_value_get_uint(value);
119             for (GSList *l = guide.views; l != NULL; l = l->next) {
120                 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
121             }
122             break;
124         case PROP_HICOLOR:
125             guide.hicolor = g_value_get_uint(value);
126             break;
127     }
130 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
132     SPGuide const &guide = *SP_GUIDE(object);
134     switch (prop_id) {
135         case PROP_COLOR:
136             g_value_set_uint(value, guide.color);
137             break;
138         case PROP_HICOLOR:
139             g_value_set_uint(value, guide.hicolor);
140             break;
141     }
144 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
146     if (((SPObjectClass *) (parent_class))->build) {
147         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
148     }
150     sp_object_read_attr(object, "orientation");
151     sp_object_read_attr(object, "position");
154 static void sp_guide_release(SPObject *object)
156     SPGuide *guide = (SPGuide *) object;
158     while (guide->views) {
159         gtk_object_destroy(GTK_OBJECT(guide->views->data));
160         guide->views = g_slist_remove(guide->views, guide->views->data);
161     }
163     if (((SPObjectClass *) parent_class)->release) {
164         ((SPObjectClass *) parent_class)->release(object);
165     }
168 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
170     SPGuide *guide = SP_GUIDE(object);
172     switch (key) {
173     case SP_ATTR_ORIENTATION:
174         {
175             if (value && !strcmp(value, "horizontal")) {
176                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
177                 guide->normal_to_line = component_vectors[NR::Y];
178             } else if (value && !strcmp(value, "vertical")) {
179                 guide->normal_to_line = component_vectors[NR::X];
180             } else if (value) {
181                 gchar ** strarray = g_strsplit(value, ",", 2);
182                 double newx, newy;
183                 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
184                 success += sp_svg_number_read_d(strarray[1], &newy);
185                 g_strfreev (strarray);
186                 if (success == 2) {
187                     Geom::Point direction(newx, newy);
188                     direction.normalize();
189                     guide->normal_to_line = direction;
190                 } else {
191                     // default to vertical line for bad arguments
192                     guide->normal_to_line = component_vectors[NR::X];
193                 }
194             } else {
195                 // default to vertical line for bad arguments
196                 guide->normal_to_line = component_vectors[NR::X];
197             }
198         }
199         break;
200     case SP_ATTR_POSITION:
201         {
202             gchar ** strarray = g_strsplit(value, ",", 2);
203             double newx, newy;
204             unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
205             success += sp_svg_number_read_d(strarray[1], &newy);
206             g_strfreev (strarray);
207             if (success == 2) {
208                 guide->point_on_line = Geom::Point(newx, newy);
209             } else if (success == 1) {
210                 // before 0.46 style guideline definition.
211                 const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
212                 if (attr && !strcmp(attr, "horizontal")) {
213                     guide->point_on_line = Geom::Point(0, newx);
214                 } else {
215                     guide->point_on_line = Geom::Point(newx, 0);
216                 }
217             }
218             sp_svg_number_read_d(value, &guide->position);
220             // update position in non-committing way
221             // fixme: perhaps we need to add an update method instead, and request_update here
222             sp_guide_moveto(*guide, guide->position, false);
223         }
224         break;
225     default:
226             if (((SPObjectClass *) (parent_class))->set) {
227                 ((SPObjectClass *) (parent_class))->set(object, key, value);
228             }
229             break;
230     }
233 void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
235     bool const vertical_line_p = ( guide->normal_to_line == component_vectors[NR::X] );
236     g_assert(( guide->normal_to_line == component_vectors[NR::X] )  ||
237              ( guide->normal_to_line == component_vectors[NR::Y] ) );
238     SPCanvasItem *item = sp_guideline_new(group, guide->position, vertical_line_p ? Geom::Point(1.,0.) : Geom::Point(0.,1.));
239     sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
241     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
243     guide->views = g_slist_prepend(guide->views, item);
246 void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
248     g_assert(guide != NULL);
249     g_assert(SP_IS_GUIDE(guide));
250     g_assert(canvas != NULL);
251     g_assert(SP_IS_CANVAS(canvas));
253     for (GSList *l = guide->views; l != NULL; l = l->next) {
254         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
255             gtk_object_destroy(GTK_OBJECT(l->data));
256             guide->views = g_slist_remove(guide->views, l->data);
257             return;
258         }
259     }
261     g_assert_not_reached();
264 void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
266     g_assert(guide != NULL);
267     g_assert(SP_IS_GUIDE(guide));
268     g_assert(canvas != NULL);
269     g_assert(SP_IS_CANVAS(canvas));
271     for (GSList *l = guide->views; l != NULL; l = l->next) {
272         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
273             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
274             return;
275         }
276     }
278     g_assert_not_reached();
281 double sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
283     return dot(guide->normal_to_line, pt);
286 /**
287  * \arg commit False indicates temporary moveto in response to motion event while dragging,
288  *              true indicates a "committing" version: in response to button release event after
289  *              dragging a guideline, or clicking OK in guide editing dialog.
290  */
291 void sp_guide_moveto(SPGuide const &guide, gdouble const position, bool const commit)
293     g_assert(SP_IS_GUIDE(&guide));
295     for (GSList *l = guide.views; l != NULL; l = l->next) {
296         sp_guideline_set_position(SP_GUIDELINE(l->data),
297                                   position);
298     }
300     /* Calling sp_repr_set_svg_double must precede calling sp_item_notify_moveto in the commit
301        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
302     if (commit) {
303         sp_repr_set_svg_double(SP_OBJECT(&guide)->repr,
304                            "position", position);
305     }
307     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
308              iEnd(guide.attached_items.end());
309          i != iEnd; ++i)
310     {
311         SPGuideAttachment const &att = *i;
312         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
313     }
316 /**
317  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
318  *
319  * The caller is responsible for freeing the string.
320  */
321 char *sp_guide_description(SPGuide const *guide)
323     using NR::X;
324     using NR::Y;
326     if ( guide->normal_to_line == component_vectors[X] ) {
327         return g_strdup(_("vertical guideline"));
328     } else if ( guide->normal_to_line == component_vectors[Y] ) {
329         return g_strdup(_("horizontal guideline"));
330     } else {
331         double const radians = atan2(guide->normal_to_line[X],
332                                      guide->normal_to_line[Y]);
333         /* flip y axis and rotate 90 degrees to convert to line angle */
334         double const degrees = ( radians / M_PI ) * 180.0;
335         int const degrees_int = (int) floor( degrees + .5 );
336         return g_strdup_printf("%d degree guideline", degrees_int);
337         /* Alternative suggestion: "angled guideline". */
338     }
341 void sp_guide_remove(SPGuide *guide)
343     g_assert(SP_IS_GUIDE(guide));
345     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
346              iEnd(guide->attached_items.end());
347          i != iEnd; ++i)
348     {
349         SPGuideAttachment const &att = *i;
350         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
351     }
352     guide->attached_items.clear();
354     sp_repr_unparent(SP_OBJECT(guide)->repr);
357 /*
358   Local Variables:
359   mode:c++
360   c-file-style:"stroustrup"
361   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
362   indent-tabs-mode:nil
363   fill-column:99
364   End:
365 */
366 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :