Code

start of getting angled guidelines.
[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 = 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             if (value && !strcmp(value, "horizontal")) {
175                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
176                 guide->normal = component_vectors[NR::Y];
177             } else if (value && !strcmp(value, "vertical")) {
178                 guide->normal = component_vectors[NR::X];
179             } else if (value) {
180                 gchar ** strarray = g_strsplit(value, ",", 2);
181                 double newx, newy;
182                 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
183                 success += sp_svg_number_read_d(strarray[1], &newy);
184                 g_strfreev (strarray);
185                 if (success == 2) {
186                     Geom::Point direction(newx, newy);
187                     direction.normalize();
188                     guide->normal = direction;
189                 } else {
190                     // default to vertical line for bad arguments
191                     guide->normal = component_vectors[NR::X];
192                 }
193             } else {
194                 // default to vertical line for bad arguments
195                 guide->normal = component_vectors[NR::X];
196             }
197             break;
198         case SP_ATTR_POSITION:
199             sp_svg_number_read_d(value, &guide->position);
200             // update position in non-committing way
201             // fixme: perhaps we need to add an update method instead, and request_update here
202             sp_guide_moveto(*guide, guide->position, false);
203             break;
204         default:
205             if (((SPObjectClass *) (parent_class))->set) {
206                 ((SPObjectClass *) (parent_class))->set(object, key, value);
207             }
208             break;
209     }
212 void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
214     bool const vertical_line_p = ( guide->normal == component_vectors[NR::X] );
215     g_assert(( guide->normal == component_vectors[NR::X] )  ||
216              ( guide->normal == component_vectors[NR::Y] ) );
217     SPCanvasItem *item = sp_guideline_new(group, guide->position, vertical_line_p ? Geom::Point(1.,0.) : Geom::Point(0.,1.));
218     sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
220     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
222     guide->views = g_slist_prepend(guide->views, item);
225 void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
227     g_assert(guide != NULL);
228     g_assert(SP_IS_GUIDE(guide));
229     g_assert(canvas != NULL);
230     g_assert(SP_IS_CANVAS(canvas));
232     for (GSList *l = guide->views; l != NULL; l = l->next) {
233         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
234             gtk_object_destroy(GTK_OBJECT(l->data));
235             guide->views = g_slist_remove(guide->views, l->data);
236             return;
237         }
238     }
240     g_assert_not_reached();
243 void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
245     g_assert(guide != NULL);
246     g_assert(SP_IS_GUIDE(guide));
247     g_assert(canvas != NULL);
248     g_assert(SP_IS_CANVAS(canvas));
250     for (GSList *l = guide->views; l != NULL; l = l->next) {
251         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
252             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
253             return;
254         }
255     }
257     g_assert_not_reached();
260 double sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
262     return dot(guide->normal, pt);
265 /**
266  * \arg commit False indicates temporary moveto in response to motion event while dragging,
267  *              true indicates a "committing" version: in response to button release event after
268  *              dragging a guideline, or clicking OK in guide editing dialog.
269  */
270 void sp_guide_moveto(SPGuide const &guide, gdouble const position, bool const commit)
272     g_assert(SP_IS_GUIDE(&guide));
274     for (GSList *l = guide.views; l != NULL; l = l->next) {
275         sp_guideline_set_position(SP_GUIDELINE(l->data),
276                                   position);
277     }
279     /* Calling sp_repr_set_svg_double must precede calling sp_item_notify_moveto in the commit
280        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
281     if (commit) {
282         sp_repr_set_svg_double(SP_OBJECT(&guide)->repr,
283                            "position", position);
284     }
286     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
287              iEnd(guide.attached_items.end());
288          i != iEnd; ++i)
289     {
290         SPGuideAttachment const &att = *i;
291         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
292     }
295 /**
296  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
297  *
298  * The caller is responsible for freeing the string.
299  */
300 char *sp_guide_description(SPGuide const *guide)
302     using NR::X;
303     using NR::Y;
305     if ( guide->normal == component_vectors[X] ) {
306         return g_strdup(_("vertical guideline"));
307     } else if ( guide->normal == component_vectors[Y] ) {
308         return g_strdup(_("horizontal guideline"));
309     } else {
310         double const radians = atan2(guide->normal[X],
311                                      guide->normal[Y]);
312         /* flip y axis and rotate 90 degrees to convert to line angle */
313         double const degrees = ( radians / M_PI ) * 180.0;
314         int const degrees_int = (int) floor( degrees + .5 );
315         return g_strdup_printf("%d degree guideline", degrees_int);
316         /* Alternative suggestion: "angled guideline". */
317     }
320 void sp_guide_remove(SPGuide *guide)
322     g_assert(SP_IS_GUIDE(guide));
324     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
325              iEnd(guide->attached_items.end());
326          i != iEnd; ++i)
327     {
328         SPGuideAttachment const &att = *i;
329         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
330     }
331     guide->attached_items.clear();
333     sp_repr_unparent(SP_OBJECT(guide)->repr);
336 /*
337   Local Variables:
338   mode:c++
339   c-file-style:"stroustrup"
340   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
341   indent-tabs-mode:nil
342   fill-column:99
343   End:
344 */
345 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :