Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / sp-guide.cpp
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/sp-canvas.h"
27 #include "display/guideline.h"
28 #include "svg/svg.h"
29 #include "svg/stringstream.h"
30 #include "attributes.h"
31 #include "sp-guide.h"
32 #include <sp-item-notify-moveto.h>
33 #include <sp-item.h>
34 #include <sp-guide-constraint.h>
35 #include <glibmm/i18n.h>
36 #include <xml/repr.h>
37 #include <remove-last.h>
38 #include "sp-metrics.h"
39 #include "inkscape.h"
40 #include "desktop.h"
41 #include "sp-namedview.h"
42 #include <2geom/angle.h>
43 #include "document.h"
45 using Inkscape::DocumentUndo;
46 using std::vector;
48 enum {
49     PROP_0,
50     PROP_COLOR,
51     PROP_HICOLOR
52 };
54 static void sp_guide_class_init(SPGuideClass *gc);
55 static void sp_guide_init(SPGuide *guide);
56 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
57 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
59 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
60 static void sp_guide_release(SPObject *object);
61 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
63 static SPObjectClass *parent_class;
65 GType sp_guide_get_type(void)
66 {
67     static GType guide_type = 0;
69     if (!guide_type) {
70         GTypeInfo guide_info = {
71             sizeof(SPGuideClass),
72             NULL, NULL,
73             (GClassInitFunc) sp_guide_class_init,
74             NULL, NULL,
75             sizeof(SPGuide),
76             16,
77             (GInstanceInitFunc) sp_guide_init,
78             NULL,       /* value_table */
79         };
80         guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
81     }
83     return guide_type;
84 }
86 static void sp_guide_class_init(SPGuideClass *gc)
87 {
88     GObjectClass *gobject_class = (GObjectClass *) gc;
89     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
91     parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
93     gobject_class->set_property = sp_guide_set_property;
94     gobject_class->get_property = sp_guide_get_property;
96     sp_object_class->build = sp_guide_build;
97     sp_object_class->release = sp_guide_release;
98     sp_object_class->set = sp_guide_set;
100     g_object_class_install_property(gobject_class,
101                                     PROP_COLOR,
102                                     g_param_spec_uint("color", "Color", "Color",
103                                                       0,
104                                                       0xffffffff,
105                                                       0xff000000,
106                                                       (GParamFlags) G_PARAM_READWRITE));
108     g_object_class_install_property(gobject_class,
109                                     PROP_HICOLOR,
110                                     g_param_spec_uint("hicolor", "HiColor", "HiColor",
111                                                       0,
112                                                       0xffffffff,
113                                                       0xff000000,
114                                                       (GParamFlags) G_PARAM_READWRITE));
117 static void sp_guide_init(SPGuide *guide)
119     guide->normal_to_line = component_vectors[Geom::Y];
120     guide->point_on_line = Geom::Point(0.,0.);
121     guide->color = 0x0000ff7f;
122     guide->hicolor = 0xff00007f;
125 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
127     SPGuide &guide = *SP_GUIDE(object);
129     switch (prop_id) {
130         case PROP_COLOR:
131             guide.color = g_value_get_uint(value);
132             for (GSList *l = guide.views; l != NULL; l = l->next) {
133                 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
134             }
135             break;
137         case PROP_HICOLOR:
138             guide.hicolor = g_value_get_uint(value);
139             break;
140     }
143 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
145     SPGuide const &guide = *SP_GUIDE(object);
147     switch (prop_id) {
148         case PROP_COLOR:
149             g_value_set_uint(value, guide.color);
150             break;
151         case PROP_HICOLOR:
152             g_value_set_uint(value, guide.hicolor);
153             break;
154     }
157 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
159     if (((SPObjectClass *) (parent_class))->build) {
160         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
161     }
163     object->readAttr( "orientation" );
164     object->readAttr( "position" );
167 static void sp_guide_release(SPObject *object)
169     SPGuide *guide = (SPGuide *) object;
171     while (guide->views) {
172         sp_guideline_delete(SP_GUIDELINE(guide->views->data));
173         guide->views = g_slist_remove(guide->views, guide->views->data);
174     }
176     if (((SPObjectClass *) parent_class)->release) {
177         ((SPObjectClass *) parent_class)->release(object);
178     }
181 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
183     SPGuide *guide = SP_GUIDE(object);
185     switch (key) {
186     case SP_ATTR_ORIENTATION:
187         {
188             if (value && !strcmp(value, "horizontal")) {
189                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
190                 guide->normal_to_line = component_vectors[Geom::Y];
191             } else if (value && !strcmp(value, "vertical")) {
192                 guide->normal_to_line = component_vectors[Geom::X];
193             } else if (value) {
194                 gchar ** strarray = g_strsplit(value, ",", 2);
195                 double newx, newy;
196                 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
197                 success += sp_svg_number_read_d(strarray[1], &newy);
198                 g_strfreev (strarray);
199                 if (success == 2 && (fabs(newx) > 1e-6 || fabs(newy) > 1e-6)) {
200                     Geom::Point direction(newx, newy);
201                     direction.normalize();
202                     guide->normal_to_line = direction;
203                 } else {
204                     // default to vertical line for bad arguments
205                     guide->normal_to_line = component_vectors[Geom::X];
206                 }
207             } else {
208                 // default to vertical line for bad arguments
209                 guide->normal_to_line = component_vectors[Geom::X];
210             }
211             sp_guide_set_normal(*guide, guide->normal_to_line, false);
212         }
213         break;
214     case SP_ATTR_POSITION:
215         {
216             gchar ** strarray = g_strsplit(value, ",", 2);
217             double newx, newy;
218             unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
219             success += sp_svg_number_read_d(strarray[1], &newy);
220             g_strfreev (strarray);
221             if (success == 2) {
222                 guide->point_on_line = Geom::Point(newx, newy);
223             } else if (success == 1) {
224                 // before 0.46 style guideline definition.
225                 const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
226                 if (attr && !strcmp(attr, "horizontal")) {
227                     guide->point_on_line = Geom::Point(0, newx);
228                 } else {
229                     guide->point_on_line = Geom::Point(newx, 0);
230                 }
231             }
233             // update position in non-committing way
234             // fixme: perhaps we need to add an update method instead, and request_update here
235             sp_guide_moveto(*guide, guide->point_on_line, false);
236         }
237         break;
238     default:
239             if (((SPObjectClass *) (parent_class))->set) {
240                 ((SPObjectClass *) (parent_class))->set(object, key, value);
241             }
242             break;
243     }
246 SPGuide *SPGuide::createSPGuide(SPDesktop *desktop, Geom::Point const &pt1, Geom::Point const &pt2)
248     SPDocument *doc = sp_desktop_document(desktop);
249     Inkscape::XML::Document *xml_doc = doc->getReprDoc();
251     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
253     Geom::Point n = Geom::rot90(pt2 - pt1);
255     sp_repr_set_point(repr, "position", pt1);
256     sp_repr_set_point(repr, "orientation", n);
258     desktop->namedview->appendChild(repr);
259     Inkscape::GC::release(repr);
261     SPGuide *guide= SP_GUIDE(doc->getObjectByRepr(repr));
262     return guide;
265 void
266 sp_guide_pt_pairs_to_guides(SPDesktop *dt, std::list<std::pair<Geom::Point, Geom::Point> > &pts) {
267     for (std::list<std::pair<Geom::Point, Geom::Point> >::iterator i = pts.begin(); i != pts.end(); ++i) {
268         SPGuide::createSPGuide(dt, (*i).first, (*i).second);
269     }
272 void
273 sp_guide_create_guides_around_page(SPDesktop *dt) {
274     SPDocument *doc=sp_desktop_document(dt);
275     std::list<std::pair<Geom::Point, Geom::Point> > pts;
277     Geom::Point A(0, 0);
278     Geom::Point C(doc->getWidth(), doc->getHeight());
279     Geom::Point B(C[Geom::X], 0);
280     Geom::Point D(0, C[Geom::Y]);
282     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(A, B));
283     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(B, C));
284     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(C, D));
285     pts.push_back(std::make_pair<Geom::Point, Geom::Point>(D, A));
287     sp_guide_pt_pairs_to_guides(dt, pts);
289     DocumentUndo::done(doc, SP_VERB_NONE, _("Guides Around Page"));
292 void SPGuide::showSPGuide(SPCanvasGroup *group, GCallback handler)
294     SPCanvasItem *item = sp_guideline_new(group, point_on_line, normal_to_line);
295     sp_guideline_set_color(SP_GUIDELINE(item), color);
297     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), this);
299     views = g_slist_prepend(views, item);
302 void SPGuide::hideSPGuide(SPCanvas *canvas)
304     g_assert(canvas != NULL);
305     g_assert(SP_IS_CANVAS(canvas));
307     for (GSList *l = views; l != NULL; l = l->next) {
308         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
309             sp_guideline_delete(SP_GUIDELINE(l->data));
310             views = g_slist_remove(views, l->data);
311             return;
312         }
313     }
315     g_assert_not_reached();
318 void SPGuide::sensitize(SPCanvas *canvas, gboolean sensitive)
320     g_assert(canvas != NULL);
321     g_assert(SP_IS_CANVAS(canvas));
323     for (GSList *l = views; l != NULL; l = l->next) {
324         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
325             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
326             return;
327         }
328     }
330     g_assert_not_reached();
333 Geom::Point SPGuide::getPositionFrom(Geom::Point const &pt) const
335     return -(pt - point_on_line);
338 double SPGuide::getDistanceFrom(Geom::Point const &pt) const
340     return Geom::dot(pt - point_on_line, normal_to_line);
343 /**
344  * \arg commit False indicates temporary moveto in response to motion event while dragging,
345  *      true indicates a "committing" version: in response to button release event after
346  *      dragging a guideline, or clicking OK in guide editing dialog.
347  */
348 void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
350     g_assert(SP_IS_GUIDE(&guide));
352     for (GSList *l = guide.views; l != NULL; l = l->next) {
353         sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
354     }
356     /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
357        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
358     if (commit) {
359         //XML Tree being used here directly while it shouldn't be.
360         sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "position", point_on_line);
361     }
363 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
364     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
365              iEnd(guide.attached_items.end());
366          i != iEnd; ++i)
367     {
368         SPGuideAttachment const &att = *i;
369         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
370     }
371 */
374 /**
375  * \arg commit False indicates temporary moveto in response to motion event while dragging,
376  *      true indicates a "committing" version: in response to button release event after
377  *      dragging a guideline, or clicking OK in guide editing dialog.
378  */
379 void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line, bool const commit)
381     g_assert(SP_IS_GUIDE(&guide));
383     for (GSList *l = guide.views; l != NULL; l = l->next) {
384         sp_guideline_set_normal(SP_GUIDELINE(l->data), normal_to_line);
385     }
387     /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
388        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
389     if (commit) {
390         //XML Tree being used directly while it shouldn't be
391         sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "orientation", normal_to_line);
392     }
394 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
395     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
396              iEnd(guide.attached_items.end());
397          i != iEnd; ++i)
398     {
399         SPGuideAttachment const &att = *i;
400         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
401     }
402 */
405 /**
406  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
407  * If verbose is false, only positioning information is included (useful for dialogs).
408  *
409  * The caller is responsible for freeing the string.
410  */
411 char *sp_guide_description(SPGuide const *guide, const bool verbose)
413     using Geom::X;
414     using Geom::Y;
415             
416     GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X],
417                                                         SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
418     GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y],
419                                                         SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
421     gchar *shortcuts = g_strdup_printf("; %s", _("<b>Shift+drag</b> to rotate, <b>Ctrl+drag</b> to move origin, <b>Del</b> to delete"));
422     gchar *descr;
424     if ( are_near(guide->normal_to_line, component_vectors[X]) ||
425          are_near(guide->normal_to_line, -component_vectors[X]) ) {
426         descr = g_strdup_printf(_("vertical, at %s"), position_string_x->str);
427     } else if ( are_near(guide->normal_to_line, component_vectors[Y]) ||
428                 are_near(guide->normal_to_line, -component_vectors[Y]) ) {
429         descr = g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
430     } else {
431         double const radians = guide->angle();
432         double const degrees = Geom::rad_to_deg(radians);
433         int const degrees_int = (int) round(degrees);
434         descr = g_strdup_printf(_("at %d degrees, through (%s,%s)"), 
435                                 degrees_int, position_string_x->str, position_string_y->str);
436     }
438     g_string_free(position_string_x, TRUE);
439     g_string_free(position_string_y, TRUE);
441     if (verbose) {
442         descr = g_strconcat(descr, shortcuts, NULL);
443     }
444     g_free(shortcuts);
445     return descr;
448 void sp_guide_remove(SPGuide *guide)
450     g_assert(SP_IS_GUIDE(guide));
452     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
453              iEnd(guide->attached_items.end());
454          i != iEnd; ++i)
455     {
456         SPGuideAttachment const &att = *i;
457         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
458     }
459     guide->attached_items.clear();
461     //XML Tree being used directly while it shouldn't be.
462     sp_repr_unparent(SP_OBJECT(guide)->getRepr());
465 /*
466   Local Variables:
467   mode:c++
468   c-file-style:"stroustrup"
469   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
470   indent-tabs-mode:nil
471   fill-column:99
472   End:
473 */
474 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :