Code

A simple layout document as to what, why and how is cppification.
[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
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 std::vector;
46 enum {
47     PROP_0,
48     PROP_COLOR,
49     PROP_HICOLOR
50 };
52 static void sp_guide_class_init(SPGuideClass *gc);
53 static void sp_guide_init(SPGuide *guide);
54 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
55 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
57 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
58 static void sp_guide_release(SPObject *object);
59 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value);
61 static SPObjectClass *parent_class;
63 GType sp_guide_get_type(void)
64 {
65     static GType guide_type = 0;
67     if (!guide_type) {
68         GTypeInfo guide_info = {
69             sizeof(SPGuideClass),
70             NULL, NULL,
71             (GClassInitFunc) sp_guide_class_init,
72             NULL, NULL,
73             sizeof(SPGuide),
74             16,
75             (GInstanceInitFunc) sp_guide_init,
76             NULL,       /* value_table */
77         };
78         guide_type = g_type_register_static(SP_TYPE_OBJECT, "SPGuide", &guide_info, (GTypeFlags) 0);
79     }
81     return guide_type;
82 }
84 static void sp_guide_class_init(SPGuideClass *gc)
85 {
86     GObjectClass *gobject_class = (GObjectClass *) gc;
87     SPObjectClass *sp_object_class = (SPObjectClass *) gc;
89     parent_class = (SPObjectClass*) g_type_class_ref(SP_TYPE_OBJECT);
91     gobject_class->set_property = sp_guide_set_property;
92     gobject_class->get_property = sp_guide_get_property;
94     sp_object_class->build = sp_guide_build;
95     sp_object_class->release = sp_guide_release;
96     sp_object_class->set = sp_guide_set;
98     g_object_class_install_property(gobject_class,
99                                     PROP_COLOR,
100                                     g_param_spec_uint("color", "Color", "Color",
101                                                       0,
102                                                       0xffffffff,
103                                                       0xff000000,
104                                                       (GParamFlags) G_PARAM_READWRITE));
106     g_object_class_install_property(gobject_class,
107                                     PROP_HICOLOR,
108                                     g_param_spec_uint("hicolor", "HiColor", "HiColor",
109                                                       0,
110                                                       0xffffffff,
111                                                       0xff000000,
112                                                       (GParamFlags) G_PARAM_READWRITE));
115 static void sp_guide_init(SPGuide *guide)
117     guide->normal_to_line = component_vectors[Geom::Y];
118     guide->point_on_line = Geom::Point(0.,0.);
119     guide->color = 0x0000ff7f;
120     guide->hicolor = 0xff00007f;
123 static void sp_guide_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec */*pspec*/)
125     SPGuide &guide = *SP_GUIDE(object);
127     switch (prop_id) {
128         case PROP_COLOR:
129             guide.color = g_value_get_uint(value);
130             for (GSList *l = guide.views; l != NULL; l = l->next) {
131                 sp_guideline_set_color(SP_GUIDELINE(l->data), guide.color);
132             }
133             break;
135         case PROP_HICOLOR:
136             guide.hicolor = g_value_get_uint(value);
137             break;
138     }
141 static void sp_guide_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec */*pspec*/)
143     SPGuide const &guide = *SP_GUIDE(object);
145     switch (prop_id) {
146         case PROP_COLOR:
147             g_value_set_uint(value, guide.color);
148             break;
149         case PROP_HICOLOR:
150             g_value_set_uint(value, guide.hicolor);
151             break;
152     }
155 static void sp_guide_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
157     if (((SPObjectClass *) (parent_class))->build) {
158         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
159     }
161     object->readAttr( "orientation");
162     object->readAttr( "position");
165 static void sp_guide_release(SPObject *object)
167     SPGuide *guide = (SPGuide *) object;
169     while (guide->views) {
170         sp_guideline_delete(SP_GUIDELINE(guide->views->data));
171         guide->views = g_slist_remove(guide->views, guide->views->data);
172     }
174     if (((SPObjectClass *) parent_class)->release) {
175         ((SPObjectClass *) parent_class)->release(object);
176     }
179 static void sp_guide_set(SPObject *object, unsigned int key, const gchar *value)
181     SPGuide *guide = SP_GUIDE(object);
183     switch (key) {
184     case SP_ATTR_ORIENTATION:
185         {
186             if (value && !strcmp(value, "horizontal")) {
187                 /* Visual representation of a horizontal line, constrain vertically (y coordinate). */
188                 guide->normal_to_line = component_vectors[Geom::Y];
189             } else if (value && !strcmp(value, "vertical")) {
190                 guide->normal_to_line = component_vectors[Geom::X];
191             } else if (value) {
192                 gchar ** strarray = g_strsplit(value, ",", 2);
193                 double newx, newy;
194                 unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
195                 success += sp_svg_number_read_d(strarray[1], &newy);
196                 g_strfreev (strarray);
197                 if (success == 2 && (fabs(newx) > 1e-6 || fabs(newy) > 1e-6)) {
198                     Geom::Point direction(newx, newy);
199                     direction.normalize();
200                     guide->normal_to_line = direction;
201                 } else {
202                     // default to vertical line for bad arguments
203                     guide->normal_to_line = component_vectors[Geom::X];
204                 }
205             } else {
206                 // default to vertical line for bad arguments
207                 guide->normal_to_line = component_vectors[Geom::X];
208             }
209             sp_guide_set_normal(*guide, guide->normal_to_line, false);
210         }
211         break;
212     case SP_ATTR_POSITION:
213         {
214             gchar ** strarray = g_strsplit(value, ",", 2);
215             double newx, newy;
216             unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
217             success += sp_svg_number_read_d(strarray[1], &newy);
218             g_strfreev (strarray);
219             if (success == 2) {
220                 guide->point_on_line = Geom::Point(newx, newy);
221             } else if (success == 1) {
222                 // before 0.46 style guideline definition.
223                 const gchar *attr = SP_OBJECT_REPR(object)->attribute("orientation");
224                 if (attr && !strcmp(attr, "horizontal")) {
225                     guide->point_on_line = Geom::Point(0, newx);
226                 } else {
227                     guide->point_on_line = Geom::Point(newx, 0);
228                 }
229             }
231             // update position in non-committing way
232             // fixme: perhaps we need to add an update method instead, and request_update here
233             sp_guide_moveto(*guide, guide->point_on_line, false);
234         }
235         break;
236     default:
237             if (((SPObjectClass *) (parent_class))->set) {
238                 ((SPObjectClass *) (parent_class))->set(object, key, value);
239             }
240             break;
241     }
244 SPGuide *
245 SPGuide::createSPGuide(SPDesktop *desktop, Geom::Point const &pt1, Geom::Point const &pt2) {
246     SPDocument *doc=sp_desktop_document(desktop);
247     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
249     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
251     Geom::Point n = Geom::rot90(pt2 - pt1);
253     sp_repr_set_point(repr, "position", pt1);
254     sp_repr_set_point(repr, "orientation", n);
256     //SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
257     desktop->namedview->appendChild(repr);
258     Inkscape::GC::release(repr);
260     SPGuide *guide= SP_GUIDE(doc->getObjectByRepr(repr));
261     return guide;
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     }
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     SPDocumentUndo::done (doc, SP_VERB_NONE, _("Guides Around Page"));
291 void SPGuide::showSPGuide(SPCanvasGroup *group, GCallback handler)
293     SPCanvasItem *item = sp_guideline_new(group, this->point_on_line, this->normal_to_line);
294     sp_guideline_set_color(SP_GUIDELINE(item), this->color);
296     g_signal_connect(G_OBJECT(item), "event", G_CALLBACK(handler), this);
298     this->views = g_slist_prepend(this->views, item);
301 void SPGuide::hideSPGuide(SPCanvas *canvas)
303     //g_assert(guide != NULL);
304     //g_assert(SP_IS_GUIDE(guide));
305     g_assert(canvas != NULL);
306     g_assert(SP_IS_CANVAS(canvas));
308     for (GSList *l = this->views; l != NULL; l = l->next) {
309         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
310             sp_guideline_delete(SP_GUIDELINE(l->data));
311             this->views = g_slist_remove(this->views, l->data);
312             return;
313         }
314     }
316     g_assert_not_reached();
319 void SPGuide::sensitize(SPCanvas *canvas, gboolean sensitive)
321     //g_assert(guide != NULL);
322     //g_assert(SP_IS_GUIDE(guide));
323     g_assert(canvas != NULL);
324     g_assert(SP_IS_CANVAS(canvas));
326     for (GSList *l = this->views; l != NULL; l = l->next) {
327         if (canvas == SP_CANVAS_ITEM(l->data)->canvas) {
328             sp_guideline_set_sensitive(SP_GUIDELINE(l->data), sensitive);
329             return;
330         }
331     }
333     g_assert_not_reached();
336 Geom::Point SPGuide::getPositionFrom(Geom::Point const &pt) const
338     return -(pt - this->point_on_line);
341 double SPGuide::getDistanceFrom(Geom::Point const &pt) const
343     return Geom::dot(pt - this->point_on_line, this->normal_to_line);
346 /**
347  * \arg commit False indicates temporary moveto in response to motion event while dragging,
348  *      true indicates a "committing" version: in response to button release event after
349  *      dragging a guideline, or clicking OK in guide editing dialog.
350  */
351 void sp_guide_moveto(SPGuide const &guide, Geom::Point const point_on_line, bool const commit)
353     g_assert(SP_IS_GUIDE(&guide));
355     for (GSList *l = guide.views; l != NULL; l = l->next) {
356         sp_guideline_set_position(SP_GUIDELINE(l->data), point_on_line);
357     }
359     /* Calling sp_repr_set_point must precede calling sp_item_notify_moveto in the commit
360        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
361     if (commit) {
362                 //XML Tree being used here directly while it shouldn't be.
363         sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "position", point_on_line);
364     }
366 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
367     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
368              iEnd(guide.attached_items.end());
369          i != iEnd; ++i)
370     {
371         SPGuideAttachment const &att = *i;
372         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
373     }
374 */
377 /**
378  * \arg commit False indicates temporary moveto in response to motion event while dragging,
379  *      true indicates a "committing" version: in response to button release event after
380  *      dragging a guideline, or clicking OK in guide editing dialog.
381  */
382 void sp_guide_set_normal(SPGuide const &guide, Geom::Point const normal_to_line, bool const commit)
384     g_assert(SP_IS_GUIDE(&guide));
386     for (GSList *l = guide.views; l != NULL; l = l->next) {
387         sp_guideline_set_normal(SP_GUIDELINE(l->data), normal_to_line);
388     }
390     /* Calling sp_repr_set_svg_point must precede calling sp_item_notify_moveto in the commit
391        case, so that the guide's new position is available for sp_item_rm_unsatisfied_cns. */
392     if (commit) {
393                 //XML Tree being used directly while it shouldn't be
394         sp_repr_set_point(SP_OBJECT(&guide)->getRepr(), "orientation", normal_to_line);
395     }
397 /*  DISABLED CODE BECAUSE  SPGuideAttachment  IS NOT USE AT THE MOMENT (johan)
398     for (vector<SPGuideAttachment>::const_iterator i(guide.attached_items.begin()),
399              iEnd(guide.attached_items.end());
400          i != iEnd; ++i)
401     {
402         SPGuideAttachment const &att = *i;
403         sp_item_notify_moveto(*att.item, guide, att.snappoint_ix, position, commit);
404     }
405 */
408 /**
409  * Returns a human-readable description of the guideline for use in dialog boxes and status bar.
410  * If verbose is false, only positioning information is included (useful for dialogs).
411  *
412  * The caller is responsible for freeing the string.
413  */
414 char *sp_guide_description(SPGuide const *guide, const bool verbose)
416     using Geom::X;
417     using Geom::Y;
418             
419     GString *position_string_x = SP_PX_TO_METRIC_STRING(guide->point_on_line[X],
420                                                         SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
421     GString *position_string_y = SP_PX_TO_METRIC_STRING(guide->point_on_line[Y],
422                                                         SP_ACTIVE_DESKTOP->namedview->getDefaultMetric());
424     gchar *shortcuts = g_strdup_printf("; %s", _("<b>Shift+drag</b> to rotate, <b>Ctrl+drag</b> to move origin, <b>Del</b> to delete"));
425     gchar *descr;
427     if ( are_near(guide->normal_to_line, component_vectors[X]) ||
428          are_near(guide->normal_to_line, -component_vectors[X]) ) {
429         descr = g_strdup_printf(_("vertical, at %s"), position_string_x->str);
430     } else if ( are_near(guide->normal_to_line, component_vectors[Y]) ||
431                 are_near(guide->normal_to_line, -component_vectors[Y]) ) {
432         descr = g_strdup_printf(_("horizontal, at %s"), position_string_y->str);
433     } else {
434         double const radians = guide->angle();
435         double const degrees = Geom::rad_to_deg(radians);
436         int const degrees_int = (int) round(degrees);
437         descr = g_strdup_printf(_("at %d degrees, through (%s,%s)"), 
438                                 degrees_int, position_string_x->str, position_string_y->str);
439     }
441     g_string_free(position_string_x, TRUE);
442     g_string_free(position_string_y, TRUE);
444     if (verbose) {
445         descr = g_strconcat(descr, shortcuts, NULL);
446     }
447     g_free(shortcuts);
448     return descr;
451 void sp_guide_remove(SPGuide *guide)
453     g_assert(SP_IS_GUIDE(guide));
455     for (vector<SPGuideAttachment>::const_iterator i(guide->attached_items.begin()),
456              iEnd(guide->attached_items.end());
457          i != iEnd; ++i)
458     {
459         SPGuideAttachment const &att = *i;
460         remove_last(att.item->constraints, SPGuideConstraint(guide, att.snappoint_ix));
461     }
462     guide->attached_items.clear();
463         
464         //XML Tree being used directly while it shouldn't be.
465     sp_repr_unparent(SP_OBJECT(guide)->getRepr());
468 /*
469   Local Variables:
470   mode:c++
471   c-file-style:"stroustrup"
472   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
473   indent-tabs-mode:nil
474   fill-column:99
475   End:
476 */
477 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :