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 "svg/stringstream.h"
24 #include "attributes.h"
25 #include "sp-guide.h"
26 #include <sp-item-notify-moveto.h>
27 #include <sp-item.h>
28 #include <sp-guide-constraint.h>
29 #include <glibmm/i18n.h>
30 #include <xml/repr.h>
31 #include <remove-last.h>
32 #include "sp-metrics.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "sp-namedview.h"
37 using std::vector;
39 enum {
40 PROP_0,
41 PROP_COLOR,
42 PROP_HICOLOR
43 };
45 static void sp_guide_class_init(SPGuideClass *gc);
46 static void sp_guide_init(SPGuide *guide);
47 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
48 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
50 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
51 static void sp_guide_release(SPObject *object);
52 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
54 static SPObjectClass *parent_class;
56 GType sp_guide_get_type(void)
57 {
58 static GType guide_type = 0;
60 if (!guide_type) {
61 GTypeInfo guide_info = {
62 sizeof(SPGuideClass),
63 NULL, NULL,
64 (GClassInitFunc) sp_guide_class_init,
65 NULL, NULL,
66 sizeof(SPGuide),
67 16,
68 (GInstanceInitFunc) sp_guide_init,
69 NULL, /* value_table */
70 };
71 guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
72 }
74 return guide_type;
75 }
77 static void sp_guide_class_init(SPGuideClass *gc)
78 {
79 GObjectClass *gobject_class = (GObjectClass *) gc;
80 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
82 parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
84 gobject_class->set_property = sp_guide_set_property;
85 gobject_class->get_property = sp_guide_get_property;
87 sp_object_class->build = sp_guide_build;
88 sp_object_class->release = sp_guide_release;
89 sp_object_class->set = sp_guide_set;
91 g_object_class_install_property(gobject_class,
92 PROP_COLOR,
93 g_param_spec_uint("color", "Color", "Color",
94 0,
95 0xffffffff,
96 0xff000000,
97 (GParamFlags) G_PARAM_READWRITE));
99 g_object_class_install_property(gobject_class,
100 PROP_HICOLOR,
101 g_param_spec_uint("hicolor", "HiColor", "HiColor",
102 0,
103 0xffffffff,
104 0xff000000,
105 (GParamFlags) G_PARAM_READWRITE));
106 }
108 static void sp_guide_init(SPGuide *guide)
109 {
110 guide->normal_to_line = component_vectors[NR::Y];
111 guide->point_on_line = Geom::Point(0.,0.);
112 guide->color = 0x0000ff7f;
113 guide->hicolor = 0xff00007f;
114 }
116 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
117 {
118 SPGuide &guide = *SP_GUIDE(object);
120 switch (prop_id) {
121 case PROP_COLOR:
122 guide.color = g_value_get_uint(value);
123 for (GSList *l = guide.views; l != NULL; l = l->next) {
124 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
125 }
126 break;
128 case PROP_HICOLOR:
129 guide.hicolor = g_value_get_uint(value);
130 break;
131 }
132 }
134 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
135 {
136 SPGuide const &guide = *SP_GUIDE(object);
138 switch (prop_id) {
139 case PROP_COLOR:
140 g_value_set_uint(value, guide.color);
141 break;
142 case PROP_HICOLOR:
143 g_value_set_uint(value, guide.hicolor);
144 break;
145 }
146 }
148 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
149 {
150 if (((SPObjectClass *) (parent_class))->build) {
151 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
152 }
154 sp_object_read_attr(object, "orientation");
155 sp_object_read_attr(object, "position");
156 }
158 static void sp_guide_release(SPObject *object)
159 {
160 SPGuide *guide = (SPGuide *) object;
162 while (guide->views) {
163 gtk_object_destroy(GTK_OBJECT(guide->views->data));
164 guide->views = g_slist_remove(guide->views, guide->views->data);
165 }
167 if (((SPObjectClass *) parent_class)->release) {
168 ((SPObjectClass *) parent_class)->release(object);
169 }
170 }
172 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
173 {
174 SPGuide *guide = SP_GUIDE(object);
176 switch (key) {
177 case SP_ATTR_ORIENTATION:
178 {
179 if (value && !strcmp(value, "horizontal")) {
180 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
181 guide->normal_to_line = component_vectors[NR::Y];
182 } else if (value && !strcmp(value, "vertical")) {
183 guide->normal_to_line = component_vectors[NR::X];
184 } else if (value) {
185 gchar ** strarray = g_strsplit(value, ",", 2);
186 double newx, newy;
187 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
188 success += sp_svg_number_read_d(strarray[1], &newy);
189 g_strfreev (strarray);
190 if (success == 2) {
191 Geom::Point direction(newx, newy);
192 direction.normalize();
193 guide->normal_to_line = direction;
194 } else {
195 // default to vertical line for bad arguments
196 guide->normal_to_line = component_vectors[NR::X];
197 }
198 } else {
199 // default to vertical line for bad arguments
200 guide->normal_to_line = component_vectors[NR::X];
201 }
202 sp_guide_set_normal(*guide, guide->normal_to_line.to_2geom(), false);
203 }
204 break;
205 case SP_ATTR_POSITION:
206 {
207 gchar ** strarray = g_strsplit(value, ",", 2);
208 double newx, newy;
209 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
210 success += sp_svg_number_read_d(strarray[1], &newy);
211 g_strfreev (strarray);
212 if (success == 2) {
213 guide->point_on_line = Geom::Point(newx, newy);
214 } else if (success == 1) {
215 // before 0.46 style guideline definition.
216 const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
217 if (attr && !strcmp(attr, "horizontal")) {
218 guide->point_on_line = Geom::Point(0, newx);
219 } else {
220 guide->point_on_line = Geom::Point(newx, 0);
221 }
222 }
224 // update position in non-committing way
225 // fixme: perhaps we need to add an update method instead, and request_update here
226 sp_guide_moveto(*guide, guide->point_on_line, false);
227 }
228 break;
229 default:
230 if (((SPObjectClass *) (parent_class))->set) {
231 ((SPObjectClass *) (parent_class))->set(object, key, value);
232 }
233 break;
234 }
235 }
237 void sp_guide_show(SPGuide *guide, SPCanvasGroup *group, GCallback handler)
238 {
239 SPCanvasItem *item = sp_guideline_new(group, guide->point_on_line, guide->normal_to_line.to_2geom());
240 sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
242 g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), guide);
244 guide->views = g_slist_prepend(guide->views, item);
245 }
247 void sp_guide_hide(SPGuide *guide, SPCanvas *canvas)
248 {
249 g_assert(guide != NULL);
250 g_assert(SP_IS_GUIDE(guide));
251 g_assert(canvas != NULL);
252 g_assert(SP_IS_CANVAS(canvas));
254 for (GSList *l = guide->views; l != NULL; l = l->next) {
255 if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
256 gtk_object_destroy(GTK_OBJECT(l->data));
257 guide->views = g_slist_remove(guide->views, l->data);
258 return;
259 }
260 }
262 g_assert_not_reached();
263 }
265 void sp_guide_sensitize(SPGuide *guide, SPCanvas *canvas, gboolean sensitive)
266 {
267 g_assert(guide != NULL);
268 g_assert(SP_IS_GUIDE(guide));
269 g_assert(canvas != NULL);
270 g_assert(SP_IS_CANVAS(canvas));
272 for (GSList *l = guide->views; l != NULL; l = l->next) {
273 if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
274 sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
275 return;
276 }
277 }
279 g_assert_not_reached();
280 }
282 Geom::Point sp_guide_position_from_pt(SPGuide const *guide, NR::Point const &pt)
283 {
284 return -(pt.to_2geom() - guide->point_on_line);
285 }
287 double sp_guide_distance_from_pt(SPGuide const *guide, Geom::Point const &pt)
288 {
289 return dot(pt - guide->point_on_line, guide->normal_to_line);
290 }
292 /**
293 * \arg commit False indicates temporary moveto in response to motion event while dragging,
294 * true indicates a "committing" version: in response to button release event after
295 * dragging a guideline, or clicking OK in guide editing dialog.
296 */
297 void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
298 {
299 g_assert(SP_IS_GUIDE(&guide));
301 for (GSList *l = guide.views; l != NULL; l = l->next) {
302 sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
303 }
305 /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
306 case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
307 if (commit) {
308 sp_repr_set_point(SP_OBJECT(&guide)->repr, "position", point_on_line);
309 }
311 /* DISABLED CODE BECAUSE SPGuideAttachment IS NOT USE AT THE MOMENT (johan)
312 for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
313 iEnd(guide.attached_items.end());
314 i != iEnd; ++i)
315 {
316 SPGuideAttachment const &att = *i;
317 sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
318 }
319 */
320 }
322 /**
323 * \arg commit False indicates temporary moveto in response to motion event while dragging,
324 * true indicates a "committing" version: in response to button release event after
325 * dragging a guideline, or clicking OK in guide editing dialog.
326 */
327 void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line, bool const commit)
328 {
329 g_assert(SP_IS_GUIDE(&guide));
331 for (GSList *l = guide.views; l != NULL; l = l->next) {
332 sp_guideline_set_normal(SP_GUIDELINE(l->data), normal_to_line);
333 }
335 /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
336 case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
337 if (commit) {
338 sp_repr_set_point(SP_OBJECT(&guide)->repr, "orientation", normal_to_line);
339 }
341 /* DISABLED CODE BECAUSE SPGuideAttachment IS NOT USE AT THE MOMENT (johan)
342 for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
343 iEnd(guide.attached_items.end());
344 i != iEnd; ++i)
345 {
346 SPGuideAttachment const &att = *i;
347 sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
348 }
349 */
350 }
352 /**
353 * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
354 *
355 * The caller is responsible for freeing the string.
356 */
357 char *sp_guide_description(SPGuide const *guide)
358 {
359 using NR::X;
360 using NR::Y;
362 GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
363 GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y], SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
365 if ( guide->normal_to_line == component_vectors[X] ) {
366 return g_strdup_printf(_("vertical guideline at %s"), position_string_x->str);
367 } else if ( guide->normal_to_line == component_vectors[Y] ) {
368 return g_strdup_printf(_("horizontal guideline at %s"), position_string_y->str);
369 } else {
370 double const radians = atan2(guide->normal_to_line[X],
371 guide->normal_to_line[Y]);
372 /* flip y axis and rotate 90 degrees to convert to line angle */
373 double const degrees = ( radians / M_PI ) * 180.0;
374 int const degrees_int = (int) floor( degrees + .5 );
375 return g_strdup_printf("%d degree guideline at (%s,%s)", degrees_int, position_string_x->str, position_string_y->str);
376 /* Alternative suggestion: "angled guideline". */
377 }
379 g_string_free(position_string_x, TRUE);
380 g_string_free(position_string_y, TRUE);
381 }
383 void sp_guide_remove(SPGuide *guide)
384 {
385 g_assert(SP_IS_GUIDE(guide));
387 for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
388 iEnd(guide->attached_items.end());
389 i != iEnd; ++i)
390 {
391 SPGuideAttachment const &att = *i;
392 remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
393 }
394 guide->attached_items.clear();
396 sp_repr_unparent(SP_OBJECT(guide)->repr);
397 }
399 /*
400 Local Variables:
401 mode:c++
402 c-file-style:"stroustrup"
403 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
404 indent-tabs-mode:nil
405 fill-column:99
406 End:
407 */
408 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :