Code

noop: reverted one line of commit #17642 (it's safe to call g_free with NULL)
[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-2005 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.
37 #include "isnan.h" //temp fix for isnan().  include last
39 #define DEFAULTTOLERANCE 0.4
40 #define DEFAULTGRIDCOLOR 0x3f3fff25
41 #define DEFAULTGRIDEMPCOLOR 0x3f3fff60
42 #define DEFAULTGRIDEMPSPACING 5
43 #define DEFAULTGUIDECOLOR 0x0000ff7f
44 #define DEFAULTGUIDEHICOLOR 0xff00007f
45 #define DEFAULTBORDERCOLOR 0x000000ff
46 #define DEFAULTPAGECOLOR 0xffffff00
48 static void sp_namedview_class_init(SPNamedViewClass *klass);
49 static void sp_namedview_init(SPNamedView *namedview);
51 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
52 static void sp_namedview_release(SPObject *object);
53 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value);
54 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
55 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child);
56 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
58 static void sp_namedview_setup_guides(SPNamedView * nv);
60 static gboolean sp_str_to_bool(const gchar *str);
61 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit);
62 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color);
64 static SPObjectGroupClass * parent_class;
66 GType
67 sp_namedview_get_type()
68 {
69     static GType namedview_type = 0;
70     if (!namedview_type) {
71         GTypeInfo namedview_info = {
72             sizeof(SPNamedViewClass),
73             NULL,       /* base_init */
74             NULL,       /* base_finalize */
75             (GClassInitFunc) sp_namedview_class_init,
76             NULL,       /* class_finalize */
77             NULL,       /* class_data */
78             sizeof(SPNamedView),
79             16, /* n_preallocs */
80             (GInstanceInitFunc) sp_namedview_init,
81             NULL,       /* value_table */
82         };
83         namedview_type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPNamedView", &namedview_info, (GTypeFlags)0);
84     }
85     return namedview_type;
86 }
88 static void sp_namedview_class_init(SPNamedViewClass * klass)
89 {
90     GObjectClass * gobject_class;
91     SPObjectClass * sp_object_class;
93     gobject_class = (GObjectClass *) klass;
94     sp_object_class = (SPObjectClass *) klass;
96     parent_class = (SPObjectGroupClass*) g_type_class_ref(SP_TYPE_OBJECTGROUP);
98     sp_object_class->build = sp_namedview_build;
99     sp_object_class->release = sp_namedview_release;
100     sp_object_class->set = sp_namedview_set;
101     sp_object_class->child_added = sp_namedview_child_added;
102     sp_object_class->remove_child = sp_namedview_remove_child;
103     sp_object_class->write = sp_namedview_write;
106 static void sp_namedview_init(SPNamedView *nv)
108     nv->editable = TRUE;
109     nv->showguides = TRUE;
110     nv->grids_visible = false;
111     nv->showborder = TRUE;
112     nv->showpageshadow = TRUE;
114     nv->guides = NULL;
115     nv->viewcount = 0;
116     nv->grids = NULL;
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-bbox");
250     sp_object_read_attr(object, "inkscape:snap-nodes");
251     sp_object_read_attr(object, "inkscape:snap-guide");
252     sp_object_read_attr(object, "inkscape:snap-center");
253     sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
254     sp_object_read_attr(object, "inkscape:snap-intersection-line-segments");
255     sp_object_read_attr(object, "inkscape:object-paths");
256     sp_object_read_attr(object, "inkscape:object-nodes");
257     sp_object_read_attr(object, "inkscape:bbox-paths");
258     sp_object_read_attr(object, "inkscape:bbox-nodes");
259     sp_object_read_attr(object, "inkscape:current-layer");
260     sp_object_read_attr(object, "inkscape:connector-spacing");
262     /* Construct guideline list */
263     for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
264         if (SP_IS_GUIDE(o)) {
265             SPGuide * g = SP_GUIDE(o);
266             nv->guides = g_slist_prepend(nv->guides, g);
267             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
268         }
269     }
271     // backwards compatibility with grid settings (pre 0.46)
272     sp_namedview_generate_old_grid(nv, document, repr);
275 static void sp_namedview_release(SPObject *object)
277     SPNamedView *namedview = (SPNamedView *) object;
279     if (namedview->guides) {
280         g_slist_free(namedview->guides);
281         namedview->guides = NULL;
282     }
284     // delete grids:
285     while ( namedview->grids ) {
286         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
287         delete gr;
288         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
289     }
291     if (((SPObjectClass *) parent_class)->release) {
292         ((SPObjectClass *) parent_class)->release(object);
293     }
295     namedview->snap_manager.~SnapManager();
298 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
300     SPNamedView *nv = SP_NAMEDVIEW(object);
301     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
303     switch (key) {
304     case SP_ATTR_VIEWONLY:
305             nv->editable = (!value);
306             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
307             break;
308     case SP_ATTR_SHOWGUIDES:
309             if (!value) { // show guides if not specified, for backwards compatibility
310                 nv->showguides = TRUE;
311             } else {
312                 nv->showguides = sp_str_to_bool(value);
313             }
314             sp_namedview_setup_guides(nv);
315             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
316             break;
317     case SP_ATTR_SHOWGRIDS:
318             if (!value) { // don't show grids if not specified, for backwards compatibility
319                 nv->grids_visible = false;
320             } else {
321                 nv->grids_visible = sp_str_to_bool(value);
322             }
323             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
324             break;
325     case SP_ATTR_GRIDTOLERANCE:
326             nv->gridtoleranceunit = &px;
327             nv->gridtolerance = DEFAULTTOLERANCE;
328             if (value) {
329                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
330             }
331             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
332             break;
333     case SP_ATTR_GUIDETOLERANCE:
334             nv->guidetoleranceunit = &px;
335             nv->guidetolerance = DEFAULTTOLERANCE;
336             if (value) {
337                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
338             }
339             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
340             break;
341     case SP_ATTR_OBJECTTOLERANCE:
342             nv->objecttoleranceunit = &px;
343             nv->objecttolerance = DEFAULTTOLERANCE;
344             if (value) {
345                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
346             }
347             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
348             break;
349     case SP_ATTR_GUIDECOLOR:
350             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
351             if (value) {
352                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
353             }
354             for (GSList *l = nv->guides; l != NULL; l = l->next) {
355                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
356             }
357             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
358             break;
359     case SP_ATTR_GUIDEOPACITY:
360             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
361             sp_nv_read_opacity(value, &nv->guidecolor);
362             for (GSList *l = nv->guides; l != NULL; l = l->next) {
363                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
364             }
365             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
366             break;
367     case SP_ATTR_GUIDEHICOLOR:
368             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
369             if (value) {
370                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
371             }
372             for (GSList *l = nv->guides; l != NULL; l = l->next) {
373                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
374             }
375             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
376             break;
377     case SP_ATTR_GUIDEHIOPACITY:
378             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
379             sp_nv_read_opacity(value, &nv->guidehicolor);
380             for (GSList *l = nv->guides; l != NULL; l = l->next) {
381                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
382             }
383             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
384             break;
385     case SP_ATTR_SHOWBORDER:
386             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
387             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
388             break;
389     case SP_ATTR_BORDERLAYER:
390             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
391             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
392             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
393             break;
394     case SP_ATTR_BORDERCOLOR:
395             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
396             if (value) {
397                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
398             }
399             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
400             break;
401     case SP_ATTR_BORDEROPACITY:
402             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
403             sp_nv_read_opacity(value, &nv->bordercolor);
404             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
405             break;
406     case SP_ATTR_PAGECOLOR:
407             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
408             if (value) {
409                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
410             }
411             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
412             break;
413     case SP_ATTR_INKSCAPE_PAGEOPACITY:
414             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
415             sp_nv_read_opacity(value, &nv->pagecolor);
416             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
417             break;
418     case SP_ATTR_INKSCAPE_PAGESHADOW:
419             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
420             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
421             break;
422     case SP_ATTR_SHOWPAGESHADOW:
423             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
424             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
425             break;
426     case SP_ATTR_INKSCAPE_ZOOM:
427             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
428             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
429             break;
430     case SP_ATTR_INKSCAPE_CX:
431             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
432             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
433             break;
434     case SP_ATTR_INKSCAPE_CY:
435             nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
436             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
437             break;
438     case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
439             nv->window_width = value? atoi(value) : -1; // -1 means not set
440             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
441             break;
442     case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
443             nv->window_height = value ? atoi(value) : -1; // -1 means not set
444             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
445             break;
446     case SP_ATTR_INKSCAPE_WINDOW_X:
447             nv->window_x = value ? atoi(value) : -1; // -1 means not set
448             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
449             break;
450     case SP_ATTR_INKSCAPE_WINDOW_Y:
451             nv->window_y = value ? atoi(value) : -1; // -1 means not set
452             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
453             break;
454     case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
455             nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
456             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
457             break;
458     case SP_ATTR_INKSCAPE_SNAP_BBOX:
459             nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
460             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
461             break;
462     case SP_ATTR_INKSCAPE_SNAP_NODES:
463             nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
464             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
465             break;
466     case SP_ATTR_INKSCAPE_SNAP_CENTER:
467             nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
468             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
469             break;
470     case SP_ATTR_INKSCAPE_SNAP_GUIDE:
471             nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
472             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
473             break;
474     case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
475             nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
476             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
477             break;
478     case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
479             nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
480             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
481             break;
482     case SP_ATTR_INKSCAPE_OBJECT_PATHS:
483             nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
484             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
485             break;
486     case SP_ATTR_INKSCAPE_OBJECT_NODES:
487             nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
488             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
489             break;
490     case SP_ATTR_INKSCAPE_BBOX_PATHS:
491             nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
492             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
493             break;
494     case SP_ATTR_INKSCAPE_BBOX_NODES:
495             nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);
496             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
497             break;
498     case SP_ATTR_INKSCAPE_CURRENT_LAYER:
499             nv->default_layer_id = value ? g_quark_from_string(value) : 0;
500             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
501             break;
502     case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
503             nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
504                     defaultConnSpacing;
505             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
506             break;
507     case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
508             /* The default unit if the document doesn't override this: e.g. for files saved as
509              * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
510              * earlier.
511              *
512              * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
513              * about "not the same numbers as what's in the SVG file" (at least for documents
514              * without a viewBox attribute on the root <svg> element).  Similarly, it's also
515              * the most reliable unit (i.e. least likely to be wrong in different viewing
516              * conditions) for viewBox-less SVG files given that it's the unit that inkscape
517              * uses for all coordinates.
518              *
519              * For documents that do have a viewBox attribute on the root <svg> element, it
520              * might be better if we used either viewBox coordinates or if we used the unit of
521              * say the width attribute of the root <svg> element.  However, these pose problems
522              * in that they aren't in general absolute units as currently required by
523              * doc_units.
524              */
525             SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
527             if (value) {
528                 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
529                 if ( req_unit == NULL ) {
530                     g_warning("Unrecognized unit `%s'", value);
531                     /* fixme: Document errors should be reported in the status bar or
532                      * the like (e.g. as per
533                      * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
534                      * should be only for programmer errors. */
535                 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
536                             req_unit->base == SP_UNIT_DEVICE     ) {
537                     new_unit = req_unit;
538                 } else {
539                     g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
540                               value);
541                     /* fixme: Don't use g_log (see above). */
542                 }
543             }
544             nv->doc_units = new_unit;
545             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
546             break;
547     }
548     default:
549             if (((SPObjectClass *) (parent_class))->set) {
550                 ((SPObjectClass *) (parent_class))->set(object, key, value);
551             }
552             break;
553     }
556 /**
557 * 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,
558 * otherwise only add it to the specified desktop.
559 */
560 static Inkscape::CanvasGrid*
561 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
562     Inkscape::CanvasGrid* grid = NULL;
563     //check if namedview already has an object for this grid
564     for (GSList *l = nv->grids; l != NULL; l = l->next) {
565         Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
566         if (repr == g->repr) {
567             grid = g;
568             break;
569         }
570     }
572     if (!grid) {
573         //create grid object
574         Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
575         SPDocument *doc = NULL;
576         if (desktop)
577             doc = sp_desktop_document(desktop);
578         else
579             doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
580         if (!doc) {
581             g_warning("sp_namedview_add_grid - how come doc is null here?!");
582             return NULL;
583         }
584         grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
585         nv->grids = g_slist_append(nv->grids, grid);
586         //Initialize the snapping parameters for the new grid
587         bool enabled_node = nv->snap_manager.getSnapModeNode();
588         bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
589         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
590         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
591     }
593     if (!desktop) {
594         //add canvasitem to all desktops
595         for (GSList *l = nv->views; l != NULL; l = l->next) {
596             SPDesktop *dt = static_cast<SPDesktop*>(l->data);
597             grid->createCanvasItem(dt);
598         }
599     } else {
600         //add canvasitem only for specified desktop
601         grid->createCanvasItem(desktop);
602     }
604     return grid;
607 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
609     SPNamedView *nv = (SPNamedView *) object;
611     if (((SPObjectClass *) (parent_class))->child_added) {
612         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
613     }
615     if (!strcmp(child->name(), "inkscape:grid")) {
616         sp_namedview_add_grid(nv, child, NULL);
617     } else {
618         SPObject *no = object->document->getObjectByRepr(child);
619         if ( !SP_IS_OBJECT(no) )
620             return;
622         if (SP_IS_GUIDE(no)) {
623             SPGuide *g = (SPGuide *) no;
624             nv->guides = g_slist_prepend(nv->guides, g);
625             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
626             if (nv->editable) {
627                 for (GSList *l = nv->views; l != NULL; l = l->next) {
628                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
629                     if (static_cast<SPDesktop*>(l->data)->guides_active)
630                         sp_guide_sensitize(g,
631                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
632                                            TRUE);
633                     if (nv->showguides) {
634                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
635                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
636                         }
637                     } else {
638                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
639                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
640                         }
641                     }
642                 }
643             }
644         }
645     }
648 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
650     SPNamedView *nv = (SPNamedView *) object;
652     if (!strcmp(child->name(), "inkscape:grid")) {
653         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
654             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
655             if ( gr->repr == child ) {
656                 delete gr;
657                 nv->grids = g_slist_remove_link(nv->grids, iter);
658                 break;
659             }
660         }
661     } else {
662         GSList **ref = &nv->guides;
663         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
664             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
665                 *ref = iter->next;
666                 iter->next = NULL;
667                 g_slist_free_1(iter);
668                 break;
669             }
670             ref = &iter->next;
671         }
672     }
674     if (((SPObjectClass *) (parent_class))->remove_child) {
675         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
676     }
679 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
681     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
682          repr != SP_OBJECT_REPR(object) )
683     {
684         if (repr) {
685             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
686         } else {
687              /// \todo FIXME:  Plumb an appropriate XML::Document into this
688              repr = SP_OBJECT_REPR(object)->duplicate(NULL);
689         }
690     }
692     return repr;
695 void SPNamedView::show(SPDesktop *desktop)
697     for (GSList *l = guides; l != NULL; l = l->next) {
698         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
699         if (desktop->guides_active) {
700             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
701         }
702         if (showguides) {
703             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
704                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
705             }
706         } else {
707             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
708                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
709             }
710         }
711     }
713     views = g_slist_prepend(views, desktop);
715     // generate grids specified in SVG:
716     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
717     if (repr) {
718         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
719             if (!strcmp(child->name(), "inkscape:grid")) {
720                 sp_namedview_add_grid(this, child, desktop);
721             }
722         }
723     }
725     desktop->showGrids(grids_visible, false);
728 #define MIN_ONSCREEN_DISTANCE 50
730 /*
731  * Restores window geometry from the document settings or defaults in prefs
732  */
733 void sp_namedview_window_from_document(SPDesktop *desktop)
735     SPNamedView *nv = desktop->namedview;
736     gint geometry_from_file =
737         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
739     // restore window size and position stored with the document
740     if (geometry_from_file) {
741         gint w = MIN(gdk_screen_width(), nv->window_width);
742         gint h = MIN(gdk_screen_height(), nv->window_height);
743         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
744         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
745         if (w>0 && h>0 && x>0 && y>0) {
746             x = MIN(gdk_screen_width() - w, x);
747             y = MIN(gdk_screen_height() - h, y);
748         }
749         if (w>0 && h>0) {
750             desktop->setWindowSize(w, h);
751         }
752         if (x>0 && y>0) {
753             desktop->setWindowPosition(NR::Point(x, y));
754         }
755     }
757     // restore zoom and view
758     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
759         && nv->cx != HUGE_VAL && !isNaN(nv->cx)
760         && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
761         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
762     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
763         desktop->zoom_page();
764     }
766     // cancel any history of zooms up to this point
767     if (desktop->zooms_past) {
768         g_list_free(desktop->zooms_past);
769         desktop->zooms_past = NULL;
770     }
773 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
775     SPObject *layer = NULL;
776     SPDocument *document = desktop->doc();
777     SPNamedView *nv = desktop->namedview;
778     if ( nv->default_layer_id != 0 ) {
779         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
780     }
781     // don't use that object if it's not at least group
782     if ( !layer || !SP_IS_GROUP(layer) ) {
783         layer = NULL;
784     }
785     // if that didn't work out, look for the topmost layer
786     if (!layer) {
787         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
788         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
789             if (desktop->isLayer(iter)) {
790                 layer = iter;
791             }
792         }
793     }
794     if (layer) {
795         desktop->setCurrentLayer(layer);
796     }
798     // FIXME: find a better place to do this
799     desktop->event_log->updateUndoVerbs();
802 void sp_namedview_document_from_window(SPDesktop *desktop)
804     gint save_geometry_in_file =
805         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
806     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
807     NR::Rect const r = desktop->get_display_area();
809     // saving window geometry is not undoable
810     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
811     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
813     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
814     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
815     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
817     if (save_geometry_in_file) {
818         gint w, h, x, y;
819         desktop->getWindowGeometry(x, y, w, h);
820         sp_repr_set_int(view, "inkscape:window-width", w);
821         sp_repr_set_int(view, "inkscape:window-height", h);
822         sp_repr_set_int(view, "inkscape:window-x", x);
823         sp_repr_set_int(view, "inkscape:window-y", y);
824     }
826     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
828     // restore undoability
829     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
832 void SPNamedView::hide(SPDesktop const *desktop)
834     g_assert(desktop != NULL);
835     g_assert(g_slist_find(views, desktop));
837     for (GSList *l = guides; l != NULL; l = l->next) {
838         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
839     }
841     views = g_slist_remove(views, desktop);
844 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
846     g_assert(desktop != NULL);
847     g_assert(g_slist_find(views, desktop));
849     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
851     for (GSList *l = guides; l != NULL; l = l->next) {
852         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
853     }
856 static void sp_namedview_setup_guides(SPNamedView *nv)
858     for (GSList *l = nv->guides; l != NULL; l = l->next) {
859         if (nv->showguides) {
860             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
861                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
862             }
863         } else {
864             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
865                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
866             }
867         }
868     }
871 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
873     unsigned int v;
874     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
875     if (!set) { // hide guides if not specified, for backwards compatibility
876         v = FALSE;
877     } else {
878         v = !v;
879     }
881     bool saved = sp_document_get_undo_sensitive(doc);
882     sp_document_set_undo_sensitive(doc, false);
883     sp_repr_set_boolean(repr, "showguides", v);
884     sp_document_set_undo_sensitive(doc, saved);
886     doc->setModifiedSinceSave();
889 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
891     namedview->grids_visible = show;
893     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
894     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
896     bool saved = sp_document_get_undo_sensitive(doc);
897     sp_document_set_undo_sensitive(doc, false);
898     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
899     sp_document_set_undo_sensitive(doc, saved);
901     /* we don't want the document to get dirty on startup; that's when
902        we call this function with dirty_document = false */
903     if (dirty_document) {
904         doc->setModifiedSinceSave();
905     }
908 gchar const *SPNamedView::getName() const
910     SPException ex;
911     SP_EXCEPTION_INIT(&ex);
912     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
915 guint SPNamedView::getViewCount()
917     return ++viewcount;
920 GSList const *SPNamedView::getViewList() const
922     return views;
925 /* This should be moved somewhere */
927 static gboolean sp_str_to_bool(const gchar *str)
929     if (str) {
930         if (!g_strcasecmp(str, "true") ||
931             !g_strcasecmp(str, "yes") ||
932             !g_strcasecmp(str, "y") ||
933             (atoi(str) != 0)) {
934             return TRUE;
935         }
936     }
938     return FALSE;
941 /* fixme: Collect all these length parsing methods and think common sane API */
943 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
945     if (!str) {
946         return FALSE;
947     }
949     gchar *u;
950     gdouble v = g_ascii_strtod(str, &u);
951     if (!u) {
952         return FALSE;
953     }
954     while (isspace(*u)) {
955         u += 1;
956     }
958     if (!*u) {
959         /* No unit specified - keep default */
960         *val = v;
961         return TRUE;
962     }
964     if (base & SP_UNIT_DEVICE) {
965         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
966             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
967             *val = v;
968             return TRUE;
969         }
970     }
972     if (base & SP_UNIT_ABSOLUTE) {
973         if (!strncmp(u, "pt", 2)) {
974             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
975         } else if (!strncmp(u, "mm", 2)) {
976             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
977         } else if (!strncmp(u, "cm", 2)) {
978             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
979         } else if (!strncmp(u, "m", 1)) {
980             *unit = &sp_unit_get_by_id(SP_UNIT_M);
981         } else if (!strncmp(u, "in", 2)) {
982             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
983         } else {
984             return FALSE;
985         }
986         *val = v;
987         return TRUE;
988     }
990     return FALSE;
993 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
995     if (!str) {
996         return FALSE;
997     }
999     gchar *u;
1000     gdouble v = g_ascii_strtod(str, &u);
1001     if (!u) {
1002         return FALSE;
1003     }
1004     v = CLAMP(v, 0.0, 1.0);
1006     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1008     return TRUE;
1011 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1013     g_return_val_if_fail(document != NULL, NULL);
1015     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1016     g_assert(nv != NULL);
1018     if (id == NULL) {
1019         return (SPNamedView *) nv;
1020     }
1022     while (nv && strcmp(nv->id, id)) {
1023         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1024     }
1026     return (SPNamedView *) nv;
1029 /**
1030  * Returns namedview's default metric.
1031  */
1032 SPMetric SPNamedView::getDefaultMetric() const
1034     if (doc_units) {
1035         return sp_unit_get_metric(doc_units);
1036     } else {
1037         return SP_PT;
1038     }
1041 /**
1042  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1043  */
1044 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1046     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1047         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1048         if (grid->isEnabled())
1049             return grid;
1050     }
1052     return NULL;
1056 /*
1057   Local Variables:
1058   mode:c++
1059   c-file-style:"stroustrup"
1060   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1061   indent-tabs-mode:nil
1062   fill-column:99
1063   End:
1064 */
1065 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :