Code

d65c9997a1fadbe8a351b6ace9f34aa6008a8fd9
[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  *
10  * Copyright (C) 2000-2002 authors
11  * Copyright (C) 2004 Monash University
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include "display/guideline.h"
20 #include "svg/svg.h"
21 #include "attributes.h"
22 #include "sp-guide.h"
23 #include <sp-item-notify-moveto.h>
24 #include <sp-item.h>
25 #include <sp-guide-constraint.h>
26 #include <glibmm/i18n.h>
27 #include <xml/repr.h>
28 #include <remove-last.h>
29 using std::vector;
31 enum {
32     PROP_0,
33     PROP_COLOR,
34     PROP_HICOLOR
35 };
37 static void sp_guide_class_init(SPGuideClass *gc);
38 static void sp_guide_init(SPGuide *guide);
39 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
40 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
42 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
43 static void sp_guide_release(SPObject *object);
44 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
46 static SPObjectClass *parent_class;
48 GType sp_guide_get_type(void)
49 {
50     static GType guide_type = 0;
52     if (!guide_type) {
53         GTypeInfo guide_info = {
54             sizeof(SPGuideClass),
55             NULL, NULL,
56             (GClassInitFunc) sp_guide_class_init,
57             NULL, NULL,
58             sizeof(SPGuide),
59             16,
60             (GInstanceInitFunc) sp_guide_init,
61             NULL,       /* value_table */
62         };
63         guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
64     }
66     return guide_type;
67 }
69 static void sp_guide_class_init(SPGuideClass *gc)
70 {
71     GObjectClass *gobject_class = (GObjectClass *) gc;
72     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
74     parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
76     gobject_class->set_property = sp_guide_set_property;
77     gobject_class->get_property = sp_guide_get_property;
79     sp_object_class->build = sp_guide_build;
80     sp_object_class->release = sp_guide_release;
81     sp_object_class->set = sp_guide_set;
83     g_object_class_install_property(gobject_class,
84                                     PROP_COLOR,
85                                     g_param_spec_uint("color", "Color", "Color",
86                                                       0,
87                                                       0xffffffff,
88                                                       0xff000000,
89                                                       (GParamFlags) G_PARAM_READWRITE));
91     g_object_class_install_property(gobject_class,
92                                     PROP_HICOLOR,
93                                     g_param_spec_uint("hicolor", "HiColor", "HiColor",
94                                                       0,
95                                                       0xffffffff,
96                                                       0xff000000,
97                                                       (GParamFlags) G_PARAM_READWRITE));
98 }
100 static void sp_guide_init(SPGuide *guide)
102     guide->normal = component_vectors[NR::Y];
103     /* constrain y coordinate; horizontal line.  I doubt it ever matters what we initialize
104        this to. */
105     guide->position = 0.0;
106     guide->color = 0x0000ff7f;
107     guide->hicolor = 0xff00007f;
110 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
112     SPGuide &guide = *SP_GUIDE(object);
114     switch (prop_id) {
115         case PROP_COLOR:
116             guide.color = g_value_get_uint(value);
117             for (GSList *l = guide.views; l != NULL; l = l->next) {
118                 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
119             }
120             break;
122         case PROP_HICOLOR:
123             guide.hicolor = g_value_get_uint(value);
124             break;
125     }
128 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
130     SPGuide const &guide = *SP_GUIDE(object);
132     switch (prop_id) {
133         case PROP_COLOR:
134             g_value_set_uint(value, guide.color);
135             break;
136         case PROP_HICOLOR:
137             g_value_set_uint(value, guide.hicolor);
138             break;
139     }
142 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
144     if (((SPObjectClass *) (parent_class))->build) {
145         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
146     }
148     sp_object_read_attr(object, "orientation");
149     sp_object_read_attr(object, "position");
152 static void sp_guide_release(SPObject *object)
154     SPGuide *guide = (SPGuide *) object;
156     while (guide->views) {
157         gtk_object_destroy(GTK_OBJECT(guide->views->data));
158         guide->views = g_slist_remove(guide->views, guide->views->data);
159     }
161     if (((SPObjectClass *) parent_class)->release) {
162         ((SPObjectClass *) parent_class)->release(object);
163     }
166 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
168     SPGuide *guide = SP_GUIDE(object);
170     switch (key) {
171         case SP_ATTR_ORIENTATION:
172             if (value && !strcmp(value, "horizontal")) {
173                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
174                 guide->normal = component_vectors[NR::Y];
175             } else {
176                 guide->normal = component_vectors[NR::X];
177             }
178             break;
179         case SP_ATTR_POSITION:
180             sp_svg_number_read_d(value, &guide->position);
181             // update position in non-committing way
182             // fixme: perhaps we need to add an update method instead, and request_update here
183             sp_guide_moveto(*guide, guide->position, false);
184             break;
185         default:
186             if (((SPObjectClass *) (parent_class))->set) {
187                 ((SPObjectClass *) (parent_class))->set(object, key, value);
188             }
189             break;
190     }
193 void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
195     bool const vertical_line_p = ( guide->normal == component_vectors[NR::X] );
196     g_assert(( guide->normal == component_vectors[NR::X] )  ||
197              ( guide->normal == component_vectors[NR::Y] ) );
198     SPCanvasItem *item = sp_guideline_new(group, guide->position, vertical_line_p);
199     sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
201     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
203     guide->views = g_slist_prepend(guide->views, item);
206 void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
208     g_assert(guide != NULL);
209     g_assert(SP_IS_GUIDE(guide));
210     g_assert(canvas != NULL);
211     g_assert(SP_IS_CANVAS(canvas));
213     for (GSList *l = guide->views; l != NULL; l = l->next) {
214         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
215             gtk_object_destroy(GTK_OBJECT(l->data));
216             guide->views = g_slist_remove(guide->views, l->data);
217             return;
218         }
219     }
221     g_assert_not_reached();
224 void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
226     g_assert(guide != NULL);
227     g_assert(SP_IS_GUIDE(guide));
228     g_assert(canvas != NULL);
229     g_assert(SP_IS_CANVAS(canvas));
231     for (GSList *l = guide->views; l != NULL; l = l->next) {
232         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
233             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
234             return;
235         }
236     }
238     g_assert_not_reached();
241 double sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
243     return dot(guide->normal, pt);
246 /**
247  * \arg commit False indicates temporary moveto in response to motion event while dragging,
248  *              true indicates a "committing" version: in response to button release event after
249  *              dragging a guideline, or clicking OK in guide editing dialog.
250  */
251 void sp_guide_moveto(SPGuide const &guide, gdouble const position, bool const commit)
253     g_assert(SP_IS_GUIDE(&guide));
255     for (GSList *l = guide.views; l != NULL; l = l->next) {
256         sp_guideline_set_position(SP_GUIDELINE(l->data),
257                                   position);
258     }
260     /* Calling sp_repr_set_svg_double must precede calling sp_item_notify_moveto in the commit
261        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
262     if (commit) {
263         sp_repr_set_svg_double(SP_OBJECT(&guide)->repr,
264                            "position", position);
265     }
267     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
268              iEnd(guide.attached_items.end());
269          i != iEnd; ++i)
270     {
271         SPGuideAttachment const &att = *i;
272         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
273     }
276 /**
277  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
278  *
279  * The caller is responsible for freeing the string.
280  */
281 char *sp_guide_description(SPGuide const *guide)
283     using NR::X;
284     using NR::Y;
286     if ( guide->normal == component_vectors[X] ) {
287         return g_strdup(_("vertical guideline"));
288     } else if ( guide->normal == component_vectors[Y] ) {
289         return g_strdup(_("horizontal guideline"));
290     } else {
291         double const radians = atan2(guide->normal[X],
292                                      guide->normal[Y]);
293         /* flip y axis and rotate 90 degrees to convert to line angle */
294         double const degrees = ( radians / M_PI ) * 180.0;
295         int const degrees_int = (int) floor( degrees + .5 );
296         return g_strdup_printf("%d degree guideline", degrees_int);
297         /* Alternative suggestion: "angled guideline". */
298     }
301 void sp_guide_remove(SPGuide *guide)
303     g_assert(SP_IS_GUIDE(guide));
305     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
306              iEnd(guide->attached_items.end());
307          i != iEnd; ++i)
308     {
309         SPGuideAttachment const &att = *i;
310         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
311     }
312     guide->attached_items.clear();
314     sp_repr_unparent(SP_OBJECT(guide)->repr);
317 /*
318   Local Variables:
319   mode:c++
320   c-file-style:"stroustrup"
321   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
322   indent-tabs-mode:nil
323   fill-column:99
324   End:
325 */
326 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :