Code

* src/2geom/isnan.h, src/libcola/cola.cpp, src/style.cpp, src/seltrans.cpp,
[inkscape.git] / src / sp-namedview.cpp
1 #define __SP_NAMEDVIEW_C__
3 /*
4  * <sodipodi:namedview> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
11  * Copyright (C) 1999-2008 Authors
12  * Copyright (C) 2000-2001 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #include "config.h"
18 #include <cstring>
19 #include <string>
21 #include "display/canvas-grid.h"
22 #include "helper/units.h"
23 #include "svg/svg-color.h"
24 #include "xml/repr.h"
25 #include "attributes.h"
26 #include "document.h"
27 #include "desktop-events.h"
28 #include "desktop-handles.h"
29 #include "event-log.h"
30 #include "sp-guide.h"
31 #include "sp-item-group.h"
32 #include "sp-namedview.h"
33 #include "prefs-utils.h"
34 #include "desktop.h"
35 #include "conn-avoid-ref.h" // for defaultConnSpacing.
38 #define DEFAULTTOLERANCE 0.4
39 #define DEFAULTGRIDCOLOR 0x3f3fff25
40 #define DEFAULTGRIDEMPCOLOR 0x3f3fff60
41 #define DEFAULTGRIDEMPSPACING 5
42 #define DEFAULTGUIDECOLOR 0x0000ff7f
43 #define DEFAULTGUIDEHICOLOR 0xff00007f
44 #define DEFAULTBORDERCOLOR 0x000000ff
45 #define DEFAULTPAGECOLOR 0xffffff00
47 static void sp_namedview_class_init(SPNamedViewClass *klass);
48 static void sp_namedview_init(SPNamedView *namedview);
50 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
51 static void sp_namedview_release(SPObject *object);
52 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value);
53 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
54 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child);
55 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
57 static void sp_namedview_setup_guides(SPNamedView * nv);
59 static gboolean sp_str_to_bool(const gchar *str);
60 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit);
61 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color);
63 static SPObjectGroupClass * parent_class;
65 GType
66 sp_namedview_get_type()
67 {
68     static GType namedview_type = 0;
69     if (!namedview_type) {
70         GTypeInfo namedview_info = {
71             sizeof(SPNamedViewClass),
72             NULL,       /* base_init */
73             NULL,       /* base_finalize */
74             (GClassInitFunc) sp_namedview_class_init,
75             NULL,       /* class_finalize */
76             NULL,       /* class_data */
77             sizeof(SPNamedView),
78             16, /* n_preallocs */
79             (GInstanceInitFunc) sp_namedview_init,
80             NULL,       /* value_table */
81         };
82         namedview_type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPNamedView", &namedview_info, (GTypeFlags)0);
83     }
84     return namedview_type;
85 }
87 static void sp_namedview_class_init(SPNamedViewClass * klass)
88 {
89     GObjectClass * gobject_class;
90     SPObjectClass * sp_object_class;
92     gobject_class = (GObjectClass *) klass;
93     sp_object_class = (SPObjectClass *) klass;
95     parent_class = (SPObjectGroupClass*) g_type_class_ref(SP_TYPE_OBJECTGROUP);
97     sp_object_class->build = sp_namedview_build;
98     sp_object_class->release = sp_namedview_release;
99     sp_object_class->set = sp_namedview_set;
100     sp_object_class->child_added = sp_namedview_child_added;
101     sp_object_class->remove_child = sp_namedview_remove_child;
102     sp_object_class->write = sp_namedview_write;
105 static void sp_namedview_init(SPNamedView *nv)
107     nv->editable = TRUE;
108     nv->showguides = TRUE;
109     nv->grids_visible = false;
110     nv->showborder = TRUE;
111     nv->showpageshadow = TRUE;
113     nv->guides = NULL;
114     nv->viewcount = 0;
115     nv->grids = NULL;
116     nv->snapindicator = false;
118     nv->default_layer_id = 0;
120     nv->connector_spacing = defaultConnSpacing;
122     new (&nv->snap_manager) SnapManager(nv);
125 static void sp_namedview_generate_old_grid(SPNamedView * /*nv*/, SPDocument *document, Inkscape::XML::Node *repr) {
126     bool old_grid_settings_present = false;
128     // set old settings
129     const char* gridspacingx    = "1px";
130     const char* gridspacingy    = "1px";
131     const char* gridoriginy     = "0px";
132     const char* gridoriginx     = "0px";
133     const char* gridempspacing  = "5";
134     const char* gridcolor       = "#0000ff";
135     const char* gridempcolor    = "#0000ff";
136     const char* gridopacity     = "0.2";
137     const char* gridempopacity  = "0.4";
139     const char* value = NULL;
140     if ((value = repr->attribute("gridoriginx"))) {
141         gridspacingx = value;
142         old_grid_settings_present = true;
143     }
144     if ((value = repr->attribute("gridoriginy"))) {
145         gridoriginy = value;
146         old_grid_settings_present = true;
147     }
148     if ((value = repr->attribute("gridspacingx"))) {
149         gridspacingx = value;
150         old_grid_settings_present = true;
151     }
152     if ((value = repr->attribute("gridspacingy"))) {
153         gridspacingy = value;
154         old_grid_settings_present = true;
155     }
156     if ((value = repr->attribute("gridcolor"))) {
157         gridcolor = value;
158         old_grid_settings_present = true;
159     }
160     if ((value = repr->attribute("gridempcolor"))) {
161         gridempcolor = value;
162         old_grid_settings_present = true;
163     }
164     if ((value = repr->attribute("gridempspacing"))) {
165         gridempspacing = value;
166         old_grid_settings_present = true;
167     }
168     if ((value = repr->attribute("gridopacity"))) {
169         gridopacity = value;
170         old_grid_settings_present = true;
171     }
172     if ((value = repr->attribute("gridempopacity"))) {
173         gridempopacity = value;
174         old_grid_settings_present = true;
175     }
177     if (old_grid_settings_present) {
178         // generate new xy grid with the correct settings
179         // first create the child xml node, then hook it to repr. This order is important, to not set off listeners to repr before the new node is complete.
181         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
182         Inkscape::XML::Node *newnode = xml_doc->createElement("inkscape:grid");
183         newnode->setAttribute("id", "GridFromPre046Settings");
184         newnode->setAttribute("type", Inkscape::CanvasGrid::getSVGName(Inkscape::GRID_RECTANGULAR));
185         newnode->setAttribute("originx", gridoriginx);
186         newnode->setAttribute("originy", gridoriginy);
187         newnode->setAttribute("spacingx", gridspacingx);
188         newnode->setAttribute("spacingy", gridspacingy);
189         newnode->setAttribute("color", gridcolor);
190         newnode->setAttribute("empcolor", gridempcolor);
191         newnode->setAttribute("opacity", gridopacity);
192         newnode->setAttribute("empopacity", gridempopacity);
193         newnode->setAttribute("empspacing", gridempspacing);
195         repr->appendChild(newnode);
196         Inkscape::GC::release(newnode);
198         // remove all old settings
199         repr->setAttribute("gridoriginx", NULL);
200         repr->setAttribute("gridoriginy", NULL);
201         repr->setAttribute("gridspacingx", NULL);
202         repr->setAttribute("gridspacingy", NULL);
203         repr->setAttribute("gridcolor", NULL);
204         repr->setAttribute("gridempcolor", NULL);
205         repr->setAttribute("gridopacity", NULL);
206         repr->setAttribute("gridempopacity", NULL);
207         repr->setAttribute("gridempspacing", NULL);
209 //        sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid from pre0.46 grid settings"));
210     }
213 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
215     SPNamedView *nv = (SPNamedView *) object;
216     SPObjectGroup *og = (SPObjectGroup *) object;
218     if (((SPObjectClass *) (parent_class))->build) {
219         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
220     }
222     sp_object_read_attr(object, "inkscape:document-units");
223     sp_object_read_attr(object, "viewonly");
224     sp_object_read_attr(object, "showguides");
225     sp_object_read_attr(object, "showgrid");
226     sp_object_read_attr(object, "gridtolerance");
227     sp_object_read_attr(object, "guidetolerance");
228     sp_object_read_attr(object, "objecttolerance");
229     sp_object_read_attr(object, "guidecolor");
230     sp_object_read_attr(object, "guideopacity");
231     sp_object_read_attr(object, "guidehicolor");
232     sp_object_read_attr(object, "guidehiopacity");
233     sp_object_read_attr(object, "showborder");
234     sp_object_read_attr(object, "inkscape:showpageshadow");
235     sp_object_read_attr(object, "borderlayer");
236     sp_object_read_attr(object, "bordercolor");
237     sp_object_read_attr(object, "borderopacity");
238     sp_object_read_attr(object, "pagecolor");
239     sp_object_read_attr(object, "inkscape:pageopacity");
240     sp_object_read_attr(object, "inkscape:pageshadow");
241     sp_object_read_attr(object, "inkscape:zoom");
242     sp_object_read_attr(object, "inkscape:cx");
243     sp_object_read_attr(object, "inkscape:cy");
244     sp_object_read_attr(object, "inkscape:window-width");
245     sp_object_read_attr(object, "inkscape:window-height");
246     sp_object_read_attr(object, "inkscape:window-x");
247     sp_object_read_attr(object, "inkscape:window-y");
248     sp_object_read_attr(object, "inkscape:snap-global");
249     sp_object_read_attr(object, "inkscape:snap-indicator");
250     sp_object_read_attr(object, "inkscape:snap-bbox");
251     sp_object_read_attr(object, "inkscape:snap-nodes");
252     sp_object_read_attr(object, "inkscape:snap-guide");
253     sp_object_read_attr(object, "inkscape:snap-center");
254     sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
255     sp_object_read_attr(object, "inkscape:snap-intersection-line-segments");
256     sp_object_read_attr(object, "inkscape:object-paths");
257     sp_object_read_attr(object, "inkscape:object-nodes");
258     sp_object_read_attr(object, "inkscape:bbox-paths");
259     sp_object_read_attr(object, "inkscape:bbox-nodes");
260     sp_object_read_attr(object, "inkscape:snap-page");
261     sp_object_read_attr(object, "inkscape:current-layer");
262     sp_object_read_attr(object, "inkscape:connector-spacing");
264     /* Construct guideline list */
265     for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
266         if (SP_IS_GUIDE(o)) {
267             SPGuide * g = SP_GUIDE(o);
268             nv->guides = g_slist_prepend(nv->guides, g);
269             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
270         }
271     }
273     // backwards compatibility with grid settings (pre 0.46)
274     sp_namedview_generate_old_grid(nv, document, repr);
277 static void sp_namedview_release(SPObject *object)
279     SPNamedView *namedview = (SPNamedView *) object;
281     if (namedview->guides) {
282         g_slist_free(namedview->guides);
283         namedview->guides = NULL;
284     }
286     // delete grids:
287     while ( namedview->grids ) {
288         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
289         delete gr;
290         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
291     }
293     if (((SPObjectClass *) parent_class)->release) {
294         ((SPObjectClass *) parent_class)->release(object);
295     }
297     namedview->snap_manager.~SnapManager();
300 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
302     SPNamedView *nv = SP_NAMEDVIEW(object);
303     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
305     switch (key) {
306     case SP_ATTR_VIEWONLY:
307             nv->editable = (!value);
308             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
309             break;
310     case SP_ATTR_SHOWGUIDES:
311             if (!value) { // show guides if not specified, for backwards compatibility
312                 nv->showguides = TRUE;
313             } else {
314                 nv->showguides = sp_str_to_bool(value);
315             }
316             sp_namedview_setup_guides(nv);
317             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
318             break;
319     case SP_ATTR_SHOWGRIDS:
320             if (!value) { // don't show grids if not specified, for backwards compatibility
321                 nv->grids_visible = false;
322             } else {
323                 nv->grids_visible = sp_str_to_bool(value);
324             }
325             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
326             break;
327     case SP_ATTR_GRIDTOLERANCE:
328             nv->gridtoleranceunit = &px;
329             nv->gridtolerance = DEFAULTTOLERANCE;
330             if (value) {
331                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
332             }
333             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
334             break;
335     case SP_ATTR_GUIDETOLERANCE:
336             nv->guidetoleranceunit = &px;
337             nv->guidetolerance = DEFAULTTOLERANCE;
338             if (value) {
339                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
340             }
341             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
342             break;
343     case SP_ATTR_OBJECTTOLERANCE:
344             nv->objecttoleranceunit = &px;
345             nv->objecttolerance = DEFAULTTOLERANCE;
346             if (value) {
347                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
348             }
349             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
350             break;
351     case SP_ATTR_GUIDECOLOR:
352             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
353             if (value) {
354                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
355             }
356             for (GSList *l = nv->guides; l != NULL; l = l->next) {
357                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
358             }
359             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
360             break;
361     case SP_ATTR_GUIDEOPACITY:
362             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
363             sp_nv_read_opacity(value, &nv->guidecolor);
364             for (GSList *l = nv->guides; l != NULL; l = l->next) {
365                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
366             }
367             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
368             break;
369     case SP_ATTR_GUIDEHICOLOR:
370             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
371             if (value) {
372                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
373             }
374             for (GSList *l = nv->guides; l != NULL; l = l->next) {
375                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
376             }
377             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
378             break;
379     case SP_ATTR_GUIDEHIOPACITY:
380             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
381             sp_nv_read_opacity(value, &nv->guidehicolor);
382             for (GSList *l = nv->guides; l != NULL; l = l->next) {
383                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
384             }
385             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
386             break;
387     case SP_ATTR_SHOWBORDER:
388             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
389             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
390             break;
391     case SP_ATTR_BORDERLAYER:
392             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
393             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
394             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
395             break;
396     case SP_ATTR_BORDERCOLOR:
397             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
398             if (value) {
399                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
400             }
401             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
402             break;
403     case SP_ATTR_BORDEROPACITY:
404             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
405             sp_nv_read_opacity(value, &nv->bordercolor);
406             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
407             break;
408     case SP_ATTR_PAGECOLOR:
409             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
410             if (value) {
411                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
412             }
413             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
414             break;
415     case SP_ATTR_INKSCAPE_PAGEOPACITY:
416             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
417             sp_nv_read_opacity(value, &nv->pagecolor);
418             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
419             break;
420     case SP_ATTR_INKSCAPE_PAGESHADOW:
421             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
422             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
423             break;
424     case SP_ATTR_SHOWPAGESHADOW:
425             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
426             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
427             break;
428     case SP_ATTR_INKSCAPE_ZOOM:
429             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
430             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
431             break;
432     case SP_ATTR_INKSCAPE_CX:
433             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
434             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
435             break;
436     case SP_ATTR_INKSCAPE_CY:
437             nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
438             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
439             break;
440     case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
441             nv->window_width = value? atoi(value) : -1; // -1 means not set
442             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
443             break;
444     case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
445             nv->window_height = value ? atoi(value) : -1; // -1 means not set
446             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
447             break;
448     case SP_ATTR_INKSCAPE_WINDOW_X:
449             nv->window_x = value ? atoi(value) : -1; // -1 means not set
450             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
451             break;
452     case SP_ATTR_INKSCAPE_WINDOW_Y:
453             nv->window_y = value ? atoi(value) : -1; // -1 means not set
454             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
455             break;
456     case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
457             nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
458             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
459             break;
460     case SP_ATTR_INKSCAPE_SNAP_INDICATOR:
461             nv->snapindicator = (value) ? sp_str_to_bool (value) : TRUE;
462             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
463             break;
464     case SP_ATTR_INKSCAPE_SNAP_BBOX:
465             nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
466             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
467             break;
468     case SP_ATTR_INKSCAPE_SNAP_NODES:
469             nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
470             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
471             break;
472     case SP_ATTR_INKSCAPE_SNAP_CENTER:
473             nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
474             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
475             break;
476     case SP_ATTR_INKSCAPE_SNAP_GUIDE:
477             nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
478             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
479             break;
480     case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
481             nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
482             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
483             break;
484     case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
485             nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
486             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
487             break;
488     case SP_ATTR_INKSCAPE_OBJECT_PATHS:
489             nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
490             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
491             break;
492     case SP_ATTR_INKSCAPE_OBJECT_NODES:
493             nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
494             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
495             break;
496     case SP_ATTR_INKSCAPE_BBOX_PATHS:
497             nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
498             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
499             break;
500     case SP_ATTR_INKSCAPE_BBOX_NODES:
501             nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);
502             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
503             break;
504     case SP_ATTR_INKSCAPE_SNAP_PAGE:
505             nv->snap_manager.object.setSnapToPageBorder(value ? sp_str_to_bool(value) : FALSE);
506             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
507             break;    
508     case SP_ATTR_INKSCAPE_CURRENT_LAYER:
509             nv->default_layer_id = value ? g_quark_from_string(value) : 0;
510             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
511             break;
512     case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
513             nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
514                     defaultConnSpacing;
515             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
516             break;
517     case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
518             /* The default unit if the document doesn't override this: e.g. for files saved as
519              * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
520              * earlier.
521              *
522              * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
523              * about "not the same numbers as what's in the SVG file" (at least for documents
524              * without a viewBox attribute on the root <svg> element).  Similarly, it's also
525              * the most reliable unit (i.e. least likely to be wrong in different viewing
526              * conditions) for viewBox-less SVG files given that it's the unit that inkscape
527              * uses for all coordinates.
528              *
529              * For documents that do have a viewBox attribute on the root <svg> element, it
530              * might be better if we used either viewBox coordinates or if we used the unit of
531              * say the width attribute of the root <svg> element.  However, these pose problems
532              * in that they aren't in general absolute units as currently required by
533              * doc_units.
534              */
535             SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
537             if (value) {
538                 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
539                 if ( req_unit == NULL ) {
540                     g_warning("Unrecognized unit `%s'", value);
541                     /* fixme: Document errors should be reported in the status bar or
542                      * the like (e.g. as per
543                      * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
544                      * should be only for programmer errors. */
545                 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
546                             req_unit->base == SP_UNIT_DEVICE     ) {
547                     new_unit = req_unit;
548                 } else {
549                     g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
550                               value);
551                     /* fixme: Don't use g_log (see above). */
552                 }
553             }
554             nv->doc_units = new_unit;
555             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
556             break;
557     }
558     default:
559             if (((SPObjectClass *) (parent_class))->set) {
560                 ((SPObjectClass *) (parent_class))->set(object, key, value);
561             }
562             break;
563     }
566 /**
567 * add a grid item from SVG-repr. Check if this namedview already has a gridobject for this one! If desktop=null, add grid-canvasitem to all desktops of this namedview,
568 * otherwise only add it to the specified desktop.
569 */
570 static Inkscape::CanvasGrid*
571 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
572     Inkscape::CanvasGrid* grid = NULL;
573     //check if namedview already has an object for this grid
574     for (GSList *l = nv->grids; l != NULL; l = l->next) {
575         Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
576         if (repr == g->repr) {
577             grid = g;
578             break;
579         }
580     }
582     if (!grid) {
583         //create grid object
584         Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
585         SPDocument *doc = NULL;
586         if (desktop)
587             doc = sp_desktop_document(desktop);
588         else
589             doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
590         if (!doc) {
591             g_warning("sp_namedview_add_grid - how come doc is null here?!");
592             return NULL;
593         }
594         grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
595         nv->grids = g_slist_append(nv->grids, grid);
596         //Initialize the snapping parameters for the new grid
597         bool enabled_node = nv->snap_manager.getSnapModeNode();
598         bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
599         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
600         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
601     }
603     if (!desktop) {
604         //add canvasitem to all desktops
605         for (GSList *l = nv->views; l != NULL; l = l->next) {
606             SPDesktop *dt = static_cast<SPDesktop*>(l->data);
607             grid->createCanvasItem(dt);
608         }
609     } else {
610         //add canvasitem only for specified desktop
611         grid->createCanvasItem(desktop);
612     }
614     return grid;
617 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
619     SPNamedView *nv = (SPNamedView *) object;
621     if (((SPObjectClass *) (parent_class))->child_added) {
622         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
623     }
625     if (!strcmp(child->name(), "inkscape:grid")) {
626         sp_namedview_add_grid(nv, child, NULL);
627     } else {
628         SPObject *no = object->document->getObjectByRepr(child);
629         if ( !SP_IS_OBJECT(no) )
630             return;
632         if (SP_IS_GUIDE(no)) {
633             SPGuide *g = (SPGuide *) no;
634             nv->guides = g_slist_prepend(nv->guides, g);
635             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
636             if (nv->editable) {
637                 for (GSList *l = nv->views; l != NULL; l = l->next) {
638                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
639                     if (static_cast<SPDesktop*>(l->data)->guides_active)
640                         sp_guide_sensitize(g,
641                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
642                                            TRUE);
643                     if (nv->showguides) {
644                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
645                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
646                         }
647                     } else {
648                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
649                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
650                         }
651                     }
652                 }
653             }
654         }
655     }
658 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
660     SPNamedView *nv = (SPNamedView *) object;
662     if (!strcmp(child->name(), "inkscape:grid")) {
663         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
664             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
665             if ( gr->repr == child ) {
666                 delete gr;
667                 nv->grids = g_slist_remove_link(nv->grids, iter);
668                 break;
669             }
670         }
671     } else {
672         GSList **ref = &nv->guides;
673         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
674             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
675                 *ref = iter->next;
676                 iter->next = NULL;
677                 g_slist_free_1(iter);
678                 break;
679             }
680             ref = &iter->next;
681         }
682     }
684     if (((SPObjectClass *) (parent_class))->remove_child) {
685         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
686     }
689 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
691     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
692          repr != SP_OBJECT_REPR(object) )
693     {
694         if (repr) {
695             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
696         } else {
697              repr = SP_OBJECT_REPR(object)->duplicate(doc);
698         }
699     }
701     return repr;
704 void SPNamedView::show(SPDesktop *desktop)
706     for (GSList *l = guides; l != NULL; l = l->next) {
707         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
708         if (desktop->guides_active) {
709             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
710         }
711         if (showguides) {
712             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
713                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
714             }
715         } else {
716             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
717                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
718             }
719         }
720     }
722     views = g_slist_prepend(views, desktop);
724     // generate grids specified in SVG:
725     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
726     if (repr) {
727         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
728             if (!strcmp(child->name(), "inkscape:grid")) {
729                 sp_namedview_add_grid(this, child, desktop);
730             }
731         }
732     }
734     desktop->showGrids(grids_visible, false);
737 #define MIN_ONSCREEN_DISTANCE 50
739 /*
740  * Restores window geometry from the document settings or defaults in prefs
741  */
742 void sp_namedview_window_from_document(SPDesktop *desktop)
744     SPNamedView *nv = desktop->namedview;
745     gint geometry_from_file =
746         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
748     // restore window size and position stored with the document
749     if (geometry_from_file) {
750         gint w = MIN(gdk_screen_width(), nv->window_width);
751         gint h = MIN(gdk_screen_height(), nv->window_height);
752         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
753         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
754         if (w>0 && h>0 && x>0 && y>0) {
755             x = MIN(gdk_screen_width() - w, x);
756             y = MIN(gdk_screen_height() - h, y);
757         }
758         if (w>0 && h>0) {
759             desktop->setWindowSize(w, h);
760         }
761         if (x>0 && y>0) {
762             desktop->setWindowPosition(NR::Point(x, y));
763         }
764     }
766     // restore zoom and view
767     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !IS_NAN(nv->zoom)
768         && nv->cx != HUGE_VAL && !IS_NAN(nv->cx)
769         && nv->cy != HUGE_VAL && !IS_NAN(nv->cy)) {
770         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
771     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
772         desktop->zoom_page();
773     }
775     // cancel any history of zooms up to this point
776     if (desktop->zooms_past) {
777         g_list_free(desktop->zooms_past);
778         desktop->zooms_past = NULL;
779     }
782 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
784     SPObject *layer = NULL;
785     SPDocument *document = desktop->doc();
786     SPNamedView *nv = desktop->namedview;
787     if ( nv->default_layer_id != 0 ) {
788         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
789     }
790     // don't use that object if it's not at least group
791     if ( !layer || !SP_IS_GROUP(layer) ) {
792         layer = NULL;
793     }
794     // if that didn't work out, look for the topmost layer
795     if (!layer) {
796         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
797         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
798             if (desktop->isLayer(iter)) {
799                 layer = iter;
800             }
801         }
802     }
803     if (layer) {
804         desktop->setCurrentLayer(layer);
805     }
807     // FIXME: find a better place to do this
808     desktop->event_log->updateUndoVerbs();
811 void sp_namedview_document_from_window(SPDesktop *desktop)
813     gint save_geometry_in_file =
814         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
815     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
816     NR::Rect const r = desktop->get_display_area();
818     // saving window geometry is not undoable
819     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
820     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
822     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
823     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
824     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
826     if (save_geometry_in_file) {
827         gint w, h, x, y;
828         desktop->getWindowGeometry(x, y, w, h);
829         sp_repr_set_int(view, "inkscape:window-width", w);
830         sp_repr_set_int(view, "inkscape:window-height", h);
831         sp_repr_set_int(view, "inkscape:window-x", x);
832         sp_repr_set_int(view, "inkscape:window-y", y);
833     }
835     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
837     // restore undoability
838     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
841 void SPNamedView::hide(SPDesktop const *desktop)
843     g_assert(desktop != NULL);
844     g_assert(g_slist_find(views, desktop));
846     for (GSList *l = guides; l != NULL; l = l->next) {
847         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
848     }
850     views = g_slist_remove(views, desktop);
853 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
855     g_assert(desktop != NULL);
856     g_assert(g_slist_find(views, desktop));
858     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
860     for (GSList *l = guides; l != NULL; l = l->next) {
861         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
862     }
865 static void sp_namedview_setup_guides(SPNamedView *nv)
867     for (GSList *l = nv->guides; l != NULL; l = l->next) {
868         if (nv->showguides) {
869             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
870                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
871             }
872         } else {
873             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
874                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
875             }
876         }
877     }
880 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
882     unsigned int v;
883     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
884     if (!set) { // hide guides if not specified, for backwards compatibility
885         v = FALSE;
886     } else {
887         v = !v;
888     }
890     bool saved = sp_document_get_undo_sensitive(doc);
891     sp_document_set_undo_sensitive(doc, false);
892     sp_repr_set_boolean(repr, "showguides", v);
893     sp_document_set_undo_sensitive(doc, saved);
895     doc->setModifiedSinceSave();
898 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
900     namedview->grids_visible = show;
902     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
903     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
905     bool saved = sp_document_get_undo_sensitive(doc);
906     sp_document_set_undo_sensitive(doc, false);
907     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
908     sp_document_set_undo_sensitive(doc, saved);
910     /* we don't want the document to get dirty on startup; that's when
911        we call this function with dirty_document = false */
912     if (dirty_document) {
913         doc->setModifiedSinceSave();
914     }
917 gchar const *SPNamedView::getName() const
919     SPException ex;
920     SP_EXCEPTION_INIT(&ex);
921     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
924 guint SPNamedView::getViewCount()
926     return ++viewcount;
929 GSList const *SPNamedView::getViewList() const
931     return views;
934 /* This should be moved somewhere */
936 static gboolean sp_str_to_bool(const gchar *str)
938     if (str) {
939         if (!g_strcasecmp(str, "true") ||
940             !g_strcasecmp(str, "yes") ||
941             !g_strcasecmp(str, "y") ||
942             (atoi(str) != 0)) {
943             return TRUE;
944         }
945     }
947     return FALSE;
950 /* fixme: Collect all these length parsing methods and think common sane API */
952 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
954     if (!str) {
955         return FALSE;
956     }
958     gchar *u;
959     gdouble v = g_ascii_strtod(str, &u);
960     if (!u) {
961         return FALSE;
962     }
963     while (isspace(*u)) {
964         u += 1;
965     }
967     if (!*u) {
968         /* No unit specified - keep default */
969         *val = v;
970         return TRUE;
971     }
973     if (base & SP_UNIT_DEVICE) {
974         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
975             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
976             *val = v;
977             return TRUE;
978         }
979     }
981     if (base & SP_UNIT_ABSOLUTE) {
982         if (!strncmp(u, "pt", 2)) {
983             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
984         } else if (!strncmp(u, "mm", 2)) {
985             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
986         } else if (!strncmp(u, "cm", 2)) {
987             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
988         } else if (!strncmp(u, "m", 1)) {
989             *unit = &sp_unit_get_by_id(SP_UNIT_M);
990         } else if (!strncmp(u, "in", 2)) {
991             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
992         } else {
993             return FALSE;
994         }
995         *val = v;
996         return TRUE;
997     }
999     return FALSE;
1002 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
1004     if (!str) {
1005         return FALSE;
1006     }
1008     gchar *u;
1009     gdouble v = g_ascii_strtod(str, &u);
1010     if (!u) {
1011         return FALSE;
1012     }
1013     v = CLAMP(v, 0.0, 1.0);
1015     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1017     return TRUE;
1020 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1022     g_return_val_if_fail(document != NULL, NULL);
1024     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1025     g_assert(nv != NULL);
1027     if (id == NULL) {
1028         return (SPNamedView *) nv;
1029     }
1031     while (nv && strcmp(nv->id, id)) {
1032         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1033     }
1035     return (SPNamedView *) nv;
1038 /**
1039  * Returns namedview's default metric.
1040  */
1041 SPMetric SPNamedView::getDefaultMetric() const
1043     if (doc_units) {
1044         return sp_unit_get_metric(doc_units);
1045     } else {
1046         return SP_PT;
1047     }
1050 /**
1051  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1052  */
1053 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1055     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1056         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1057         if (grid->isEnabled())
1058             return grid;
1059     }
1061     return NULL;
1064 void SPNamedView::translateGuides(NR::translate const &tr) {
1065     for (GSList *l = guides; l != NULL; l = l->next) {
1066         SPGuide &guide = *SP_GUIDE(l->data);
1067         Geom::Point point_on_line = guide.point_on_line;
1068         point_on_line[0] += tr[0];
1069         point_on_line[1] += tr[1];
1070         sp_guide_moveto(guide, point_on_line, true);
1071     }
1074 void SPNamedView::scrollAllDesktops(double dx, double dy, bool is_scrolling) {
1075         for(GSList *l = views; l; l = l->next) {
1076             SPDesktop *desktop = static_cast<SPDesktop *>(l->data);
1077             desktop->scroll_world_in_svg_coords(dx, dy, is_scrolling);
1078         }
1082 /*
1083   Local Variables:
1084   mode:c++
1085   c-file-style:"stroustrup"
1086   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1087   indent-tabs-mode:nil
1088   fill-column:99
1089   End:
1090 */
1091 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :