1 /*
2 * Inkscape guideline implementation
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * Peter Moulder <pmoulder@mail.csse.monash.edu.au>
7 * Johan Engelen
8 * Jon A. Cruz <jon@joncruz.org>
9 * Abhishek Sharma
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 "desktop-handles.h"
26 #include "display/guideline.h"
27 #include "svg/svg.h"
28 #include "svg/stringstream.h"
29 #include "attributes.h"
30 #include "sp-guide.h"
31 #include <sp-item-notify-moveto.h>
32 #include <sp-item.h>
33 #include <sp-guide-constraint.h>
34 #include <glibmm/i18n.h>
35 #include <xml/repr.h>
36 #include <remove-last.h>
37 #include "sp-metrics.h"
38 #include "inkscape.h"
39 #include "desktop.h"
40 #include "sp-namedview.h"
41 #include <2geom/angle.h>
42 #include "document.h"
44 using Inkscape::DocumentUndo;
45 using std::vector;
47 enum {
48 PROP_0,
49 PROP_COLOR,
50 PROP_HICOLOR
51 };
53 static void sp_guide_class_init(SPGuideClass *gc);
54 static void sp_guide_init(SPGuide *guide);
55 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
56 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
58 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
59 static void sp_guide_release(SPObject *object);
60 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
62 static SPObjectClass *parent_class;
64 GType sp_guide_get_type(void)
65 {
66 static GType guide_type = 0;
68 if (!guide_type) {
69 GTypeInfo guide_info = {
70 sizeof(SPGuideClass),
71 NULL, NULL,
72 (GClassInitFunc) sp_guide_class_init,
73 NULL, NULL,
74 sizeof(SPGuide),
75 16,
76 (GInstanceInitFunc) sp_guide_init,
77 NULL, /* value_table */
78 };
79 guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
80 }
82 return guide_type;
83 }
85 static void sp_guide_class_init(SPGuideClass *gc)
86 {
87 GObjectClass *gobject_class = (GObjectClass *) gc;
88 SPObjectClass *sp_object_class = (SPObjectClass *) gc;
90 parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
92 gobject_class->set_property = sp_guide_set_property;
93 gobject_class->get_property = sp_guide_get_property;
95 sp_object_class->build = sp_guide_build;
96 sp_object_class->release = sp_guide_release;
97 sp_object_class->set = sp_guide_set;
99 g_object_class_install_property(gobject_class,
100 PROP_COLOR,
101 g_param_spec_uint("color", "Color", "Color",
102 0,
103 0xffffffff,
104 0xff000000,
105 (GParamFlags) G_PARAM_READWRITE));
107 g_object_class_install_property(gobject_class,
108 PROP_HICOLOR,
109 g_param_spec_uint("hicolor", "HiColor", "HiColor",
110 0,
111 0xffffffff,
112 0xff000000,
113 (GParamFlags) G_PARAM_READWRITE));
114 }
116 static void sp_guide_init(SPGuide *guide)
117 {
118 guide->normal_to_line = component_vectors[Geom::Y];
119 guide->point_on_line = Geom::Point(0.,0.);
120 guide->color = 0x0000ff7f;
121 guide->hicolor = 0xff00007f;
122 }
124 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
125 {
126 SPGuide &guide = *SP_GUIDE(object);
128 switch (prop_id) {
129 case PROP_COLOR:
130 guide.color = g_value_get_uint(value);
131 for (GSList *l = guide.views; l != NULL; l = l->next) {
132 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
133 }
134 break;
136 case PROP_HICOLOR:
137 guide.hicolor = g_value_get_uint(value);
138 break;
139 }
140 }
142 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
143 {
144 SPGuide const &guide = *SP_GUIDE(object);
146 switch (prop_id) {
147 case PROP_COLOR:
148 g_value_set_uint(value, guide.color);
149 break;
150 case PROP_HICOLOR:
151 g_value_set_uint(value, guide.hicolor);
152 break;
153 }
154 }
156 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
157 {
158 if (((SPObjectClass *) (parent_class))->build) {
159 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
160 }
162 object->readAttr( "orientation" );
163 object->readAttr( "position" );
164 }
166 static void sp_guide_release(SPObject *object)
167 {
168 SPGuide *guide = (SPGuide *) object;
170 while (guide->views) {
171 sp_guideline_delete(SP_GUIDELINE(guide->views->data));
172 guide->views = g_slist_remove(guide->views, guide->views->data);
173 }
175 if (((SPObjectClass *) parent_class)->release) {
176 ((SPObjectClass *) parent_class)->release(object);
177 }
178 }
180 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
181 {
182 SPGuide *guide = SP_GUIDE(object);
184 switch (key) {
185 case SP_ATTR_ORIENTATION:
186 {
187 if (value && !strcmp(value, "horizontal")) {
188 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
189 guide->normal_to_line = component_vectors[Geom::Y];
190 } else if (value && !strcmp(value, "vertical")) {
191 guide->normal_to_line = component_vectors[Geom::X];
192 } else if (value) {
193 gchar ** strarray = g_strsplit(value, ",", 2);
194 double newx, newy;
195 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
196 success += sp_svg_number_read_d(strarray[1], &newy);
197 g_strfreev (strarray);
198 if (success == 2 && (fabs(newx) > 1e-6 || fabs(newy) > 1e-6)) {
199 Geom::Point direction(newx, newy);
200 direction.normalize();
201 guide->normal_to_line = direction;
202 } else {
203 // default to vertical line for bad arguments
204 guide->normal_to_line = component_vectors[Geom::X];
205 }
206 } else {
207 // default to vertical line for bad arguments
208 guide->normal_to_line = component_vectors[Geom::X];
209 }
210 sp_guide_set_normal(*guide, guide->normal_to_line, false);
211 }
212 break;
213 case SP_ATTR_POSITION:
214 {
215 gchar ** strarray = g_strsplit(value, ",", 2);
216 double newx, newy;
217 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
218 success += sp_svg_number_read_d(strarray[1], &newy);
219 g_strfreev (strarray);
220 if (success == 2) {
221 guide->point_on_line = Geom::Point(newx, newy);
222 } else if (success == 1) {
223 // before 0.46 style guideline definition.
224 const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
225 if (attr && !strcmp(attr, "horizontal")) {
226 guide->point_on_line = Geom::Point(0, newx);
227 } else {
228 guide->point_on_line = Geom::Point(newx, 0);
229 }
230 }
232 // update position in non-committing way
233 // fixme: perhaps we need to add an update method instead, and request_update here
234 sp_guide_moveto(*guide, guide->point_on_line, false);
235 }
236 break;
237 default:
238 if (((SPObjectClass *) (parent_class))->set) {
239 ((SPObjectClass *) (parent_class))->set(object, key, value);
240 }
241 break;
242 }
243 }
245 SPGuide *SPGuide::createSPGuide(SPDesktop *desktop, Geom::Point const &pt1, Geom::Point const &pt2)
246 {
247 SPDocument *doc = sp_desktop_document(desktop);
248 Inkscape::XML::Document *xml_doc = doc->getReprDoc();
250 Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
252 Geom::Point n = Geom::rot90(pt2 - pt1);
254 sp_repr_set_point(repr, "position", pt1);
255 sp_repr_set_point(repr, "orientation", n);
257 desktop->namedview->appendChild(repr);
258 Inkscape::GC::release(repr);
260 SPGuide *guide= SP_GUIDE(doc->getObjectByRepr(repr));
261 return guide;
262 }
264 void
265 sp_guide_pt_pairs_to_guides(SPDesktop *dt, std::list<std::pair<Geom::Point, Geom::Point> > &pts) {
266 for (std::list<std::pair<Geom::Point, Geom::Point> >::iterator i = pts.begin(); i != pts.end(); ++i) {
267 SPGuide::createSPGuide(dt, (*i).first, (*i).second);
268 }
269 }
271 void
272 sp_guide_create_guides_around_page(SPDesktop *dt) {
273 SPDocument *doc=sp_desktop_document(dt);
274 std::list<std::pair<Geom::Point, Geom::Point> > pts;
276 Geom::Point A(0, 0);
277 Geom::Point C(doc->getWidth(), doc->getHeight());
278 Geom::Point B(C[Geom::X], 0);
279 Geom::Point D(0, C[Geom::Y]);
281 pts.push_back(std::make_pair<Geom::Point, Geom::Point>(A, B));
282 pts.push_back(std::make_pair<Geom::Point, Geom::Point>(B, C));
283 pts.push_back(std::make_pair<Geom::Point, Geom::Point>(C, D));
284 pts.push_back(std::make_pair<Geom::Point, Geom::Point>(D, A));
286 sp_guide_pt_pairs_to_guides(dt, pts);
288 DocumentUndo::done(doc, SP_VERB_NONE, _("Guides Around Page"));
289 }
291 void SPGuide::showSPGuide(SPCanvasGroup *group, GCallback handler)
292 {
293 SPCanvasItem *item = sp_guideline_new(group, point_on_line, normal_to_line);
294 sp_guideline_set_color(SP_GUIDELINE(item), color);
296 g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), this);
298 views = g_slist_prepend(views, item);
299 }
301 void SPGuide::hideSPGuide(SPCanvas *canvas)
302 {
303 g_assert(canvas != NULL);
304 g_assert(SP_IS_CANVAS(canvas));
306 for (GSList *l = views; l != NULL; l = l->next) {
307 if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
308 sp_guideline_delete(SP_GUIDELINE(l->data));
309 views = g_slist_remove(views, l->data);
310 return;
311 }
312 }
314 g_assert_not_reached();
315 }
317 void SPGuide::sensitize(SPCanvas *canvas, gboolean sensitive)
318 {
319 g_assert(canvas != NULL);
320 g_assert(SP_IS_CANVAS(canvas));
322 for (GSList *l = views; l != NULL; l = l->next) {
323 if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
324 sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
325 return;
326 }
327 }
329 g_assert_not_reached();
330 }
332 Geom::Point SPGuide::getPositionFrom(Geom::Point const &pt) const
333 {
334 return -(pt - point_on_line);
335 }
337 double SPGuide::getDistanceFrom(Geom::Point const &pt) const
338 {
339 return Geom::dot(pt - point_on_line, normal_to_line);
340 }
342 /**
343 * \arg commit False indicates temporary moveto in response to motion event while dragging,
344 * true indicates a "committing" version: in response to button release event after
345 * dragging a guideline, or clicking OK in guide editing dialog.
346 */
347 void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
348 {
349 g_assert(SP_IS_GUIDE(&guide));
351 for (GSList *l = guide.views; l != NULL; l = l->next) {
352 sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
353 }
355 /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
356 case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
357 if (commit) {
358 //XML Tree being used here directly while it shouldn't be.
359 sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "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 */
371 }
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)
379 {
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 //XML Tree being used directly while it shouldn't be
390 sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "orientation", normal_to_line);
391 }
393 /* DISABLED CODE BECAUSE SPGuideAttachment IS NOT USE AT THE MOMENT (johan)
394 for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
395 iEnd(guide.attached_items.end());
396 i != iEnd; ++i)
397 {
398 SPGuideAttachment const &att = *i;
399 sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
400 }
401 */
402 }
404 /**
405 * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
406 * If verbose is false, only positioning information is included (useful for dialogs).
407 *
408 * The caller is responsible for freeing the string.
409 */
410 char *sp_guide_description(SPGuide const *guide, const bool verbose)
411 {
412 using Geom::X;
413 using Geom::Y;
415 GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X],
416 SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
417 GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y],
418 SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
420 gchar *shortcuts = g_strdup_printf("; %s", _("<b>Shift+drag</b> to rotate, <b>Ctrl+drag</b> to move origin, <b>Del</b> to delete"));
421 gchar *descr;
423 if ( are_near(guide->normal_to_line, component_vectors[X]) ||
424 are_near(guide->normal_to_line, -component_vectors[X]) ) {
425 descr = g_strdup_printf(_("vertical, at %s"), position_string_x->str);
426 } else if ( are_near(guide->normal_to_line, component_vectors[Y]) ||
427 are_near(guide->normal_to_line, -component_vectors[Y]) ) {
428 descr = g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
429 } else {
430 double const radians = guide->angle();
431 double const degrees = Geom::rad_to_deg(radians);
432 int const degrees_int = (int) round(degrees);
433 descr = g_strdup_printf(_("at %d degrees, through (%s,%s)"),
434 degrees_int, position_string_x->str, position_string_y->str);
435 }
437 g_string_free(position_string_x, TRUE);
438 g_string_free(position_string_y, TRUE);
440 if (verbose) {
441 descr = g_strconcat(descr, shortcuts, NULL);
442 }
443 g_free(shortcuts);
444 return descr;
445 }
447 void sp_guide_remove(SPGuide *guide)
448 {
449 g_assert(SP_IS_GUIDE(guide));
451 for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
452 iEnd(guide->attached_items.end());
453 i != iEnd; ++i)
454 {
455 SPGuideAttachment const &att = *i;
456 remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
457 }
458 guide->attached_items.clear();
460 //XML Tree being used directly while it shouldn't be.
461 sp_repr_unparent(SP_OBJECT(guide)->getRepr());
462 }
464 /*
465 Local Variables:
466 mode:c++
467 c-file-style:"stroustrup"
468 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
469 indent-tabs-mode:nil
470 fill-column:99
471 End:
472 */
473 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :