Code

replace text strings by ints for tools/bounding_box
[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:snap-page");
260     sp_object_read_attr(object, "inkscape:current-layer");
261     sp_object_read_attr(object, "inkscape:connector-spacing");
263     /* Construct guideline list */
264     for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
265         if (SP_IS_GUIDE(o)) {
266             SPGuide * g = SP_GUIDE(o);
267             nv->guides = g_slist_prepend(nv->guides, g);
268             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
269         }
270     }
272     // backwards compatibility with grid settings (pre 0.46)
273     sp_namedview_generate_old_grid(nv, document, repr);
276 static void sp_namedview_release(SPObject *object)
278     SPNamedView *namedview = (SPNamedView *) object;
280     if (namedview->guides) {
281         g_slist_free(namedview->guides);
282         namedview->guides = NULL;
283     }
285     // delete grids:
286     while ( namedview->grids ) {
287         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
288         delete gr;
289         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
290     }
292     if (((SPObjectClass *) parent_class)->release) {
293         ((SPObjectClass *) parent_class)->release(object);
294     }
296     namedview->snap_manager.~SnapManager();
299 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
301     SPNamedView *nv = SP_NAMEDVIEW(object);
302     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
304     switch (key) {
305     case SP_ATTR_VIEWONLY:
306             nv->editable = (!value);
307             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
308             break;
309     case SP_ATTR_SHOWGUIDES:
310             if (!value) { // show guides if not specified, for backwards compatibility
311                 nv->showguides = TRUE;
312             } else {
313                 nv->showguides = sp_str_to_bool(value);
314             }
315             sp_namedview_setup_guides(nv);
316             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
317             break;
318     case SP_ATTR_SHOWGRIDS:
319             if (!value) { // don't show grids if not specified, for backwards compatibility
320                 nv->grids_visible = false;
321             } else {
322                 nv->grids_visible = sp_str_to_bool(value);
323             }
324             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
325             break;
326     case SP_ATTR_GRIDTOLERANCE:
327             nv->gridtoleranceunit = &px;
328             nv->gridtolerance = DEFAULTTOLERANCE;
329             if (value) {
330                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
331             }
332             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
333             break;
334     case SP_ATTR_GUIDETOLERANCE:
335             nv->guidetoleranceunit = &px;
336             nv->guidetolerance = DEFAULTTOLERANCE;
337             if (value) {
338                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
339             }
340             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
341             break;
342     case SP_ATTR_OBJECTTOLERANCE:
343             nv->objecttoleranceunit = &px;
344             nv->objecttolerance = DEFAULTTOLERANCE;
345             if (value) {
346                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
347             }
348             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
349             break;
350     case SP_ATTR_GUIDECOLOR:
351             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
352             if (value) {
353                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
354             }
355             for (GSList *l = nv->guides; l != NULL; l = l->next) {
356                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
357             }
358             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
359             break;
360     case SP_ATTR_GUIDEOPACITY:
361             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
362             sp_nv_read_opacity(value, &nv->guidecolor);
363             for (GSList *l = nv->guides; l != NULL; l = l->next) {
364                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
365             }
366             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
367             break;
368     case SP_ATTR_GUIDEHICOLOR:
369             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
370             if (value) {
371                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
372             }
373             for (GSList *l = nv->guides; l != NULL; l = l->next) {
374                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
375             }
376             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
377             break;
378     case SP_ATTR_GUIDEHIOPACITY:
379             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
380             sp_nv_read_opacity(value, &nv->guidehicolor);
381             for (GSList *l = nv->guides; l != NULL; l = l->next) {
382                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
383             }
384             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
385             break;
386     case SP_ATTR_SHOWBORDER:
387             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
388             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
389             break;
390     case SP_ATTR_BORDERLAYER:
391             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
392             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
393             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
394             break;
395     case SP_ATTR_BORDERCOLOR:
396             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
397             if (value) {
398                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
399             }
400             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
401             break;
402     case SP_ATTR_BORDEROPACITY:
403             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
404             sp_nv_read_opacity(value, &nv->bordercolor);
405             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
406             break;
407     case SP_ATTR_PAGECOLOR:
408             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
409             if (value) {
410                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
411             }
412             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
413             break;
414     case SP_ATTR_INKSCAPE_PAGEOPACITY:
415             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
416             sp_nv_read_opacity(value, &nv->pagecolor);
417             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
418             break;
419     case SP_ATTR_INKSCAPE_PAGESHADOW:
420             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
421             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
422             break;
423     case SP_ATTR_SHOWPAGESHADOW:
424             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
425             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
426             break;
427     case SP_ATTR_INKSCAPE_ZOOM:
428             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
429             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
430             break;
431     case SP_ATTR_INKSCAPE_CX:
432             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
433             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
434             break;
435     case SP_ATTR_INKSCAPE_CY:
436             nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
437             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
438             break;
439     case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
440             nv->window_width = value? atoi(value) : -1; // -1 means not set
441             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
442             break;
443     case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
444             nv->window_height = value ? atoi(value) : -1; // -1 means not set
445             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
446             break;
447     case SP_ATTR_INKSCAPE_WINDOW_X:
448             nv->window_x = value ? atoi(value) : -1; // -1 means not set
449             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
450             break;
451     case SP_ATTR_INKSCAPE_WINDOW_Y:
452             nv->window_y = value ? atoi(value) : -1; // -1 means not set
453             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
454             break;
455     case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
456             nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
457             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
458             break;
459     case SP_ATTR_INKSCAPE_SNAP_BBOX:
460             nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
461             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
462             break;
463     case SP_ATTR_INKSCAPE_SNAP_NODES:
464             nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
465             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
466             break;
467     case SP_ATTR_INKSCAPE_SNAP_CENTER:
468             nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
469             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
470             break;
471     case SP_ATTR_INKSCAPE_SNAP_GUIDE:
472             nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
473             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
474             break;
475     case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
476             nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
477             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
478             break;
479     case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
480             nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
481             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
482             break;
483     case SP_ATTR_INKSCAPE_OBJECT_PATHS:
484             nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
485             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
486             break;
487     case SP_ATTR_INKSCAPE_OBJECT_NODES:
488             nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
489             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
490             break;
491     case SP_ATTR_INKSCAPE_BBOX_PATHS:
492             nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
493             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
494             break;
495     case SP_ATTR_INKSCAPE_BBOX_NODES:
496             nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);
497             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
498             break;
499     case SP_ATTR_INKSCAPE_SNAP_PAGE:
500             nv->snap_manager.object.setSnapToPageBorder(value ? sp_str_to_bool(value) : FALSE);
501             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
502             break;    
503     case SP_ATTR_INKSCAPE_CURRENT_LAYER:
504             nv->default_layer_id = value ? g_quark_from_string(value) : 0;
505             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
506             break;
507     case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
508             nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
509                     defaultConnSpacing;
510             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
511             break;
512     case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
513             /* The default unit if the document doesn't override this: e.g. for files saved as
514              * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
515              * earlier.
516              *
517              * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
518              * about "not the same numbers as what's in the SVG file" (at least for documents
519              * without a viewBox attribute on the root <svg> element).  Similarly, it's also
520              * the most reliable unit (i.e. least likely to be wrong in different viewing
521              * conditions) for viewBox-less SVG files given that it's the unit that inkscape
522              * uses for all coordinates.
523              *
524              * For documents that do have a viewBox attribute on the root <svg> element, it
525              * might be better if we used either viewBox coordinates or if we used the unit of
526              * say the width attribute of the root <svg> element.  However, these pose problems
527              * in that they aren't in general absolute units as currently required by
528              * doc_units.
529              */
530             SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
532             if (value) {
533                 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
534                 if ( req_unit == NULL ) {
535                     g_warning("Unrecognized unit `%s'", value);
536                     /* fixme: Document errors should be reported in the status bar or
537                      * the like (e.g. as per
538                      * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
539                      * should be only for programmer errors. */
540                 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
541                             req_unit->base == SP_UNIT_DEVICE     ) {
542                     new_unit = req_unit;
543                 } else {
544                     g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
545                               value);
546                     /* fixme: Don't use g_log (see above). */
547                 }
548             }
549             nv->doc_units = new_unit;
550             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
551             break;
552     }
553     default:
554             if (((SPObjectClass *) (parent_class))->set) {
555                 ((SPObjectClass *) (parent_class))->set(object, key, value);
556             }
557             break;
558     }
561 /**
562 * 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,
563 * otherwise only add it to the specified desktop.
564 */
565 static Inkscape::CanvasGrid*
566 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
567     Inkscape::CanvasGrid* grid = NULL;
568     //check if namedview already has an object for this grid
569     for (GSList *l = nv->grids; l != NULL; l = l->next) {
570         Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
571         if (repr == g->repr) {
572             grid = g;
573             break;
574         }
575     }
577     if (!grid) {
578         //create grid object
579         Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
580         SPDocument *doc = NULL;
581         if (desktop)
582             doc = sp_desktop_document(desktop);
583         else
584             doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
585         if (!doc) {
586             g_warning("sp_namedview_add_grid - how come doc is null here?!");
587             return NULL;
588         }
589         grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
590         nv->grids = g_slist_append(nv->grids, grid);
591         //Initialize the snapping parameters for the new grid
592         bool enabled_node = nv->snap_manager.getSnapModeNode();
593         bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
594         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
595         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
596     }
598     if (!desktop) {
599         //add canvasitem to all desktops
600         for (GSList *l = nv->views; l != NULL; l = l->next) {
601             SPDesktop *dt = static_cast<SPDesktop*>(l->data);
602             grid->createCanvasItem(dt);
603         }
604     } else {
605         //add canvasitem only for specified desktop
606         grid->createCanvasItem(desktop);
607     }
609     return grid;
612 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
614     SPNamedView *nv = (SPNamedView *) object;
616     if (((SPObjectClass *) (parent_class))->child_added) {
617         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
618     }
620     if (!strcmp(child->name(), "inkscape:grid")) {
621         sp_namedview_add_grid(nv, child, NULL);
622     } else {
623         SPObject *no = object->document->getObjectByRepr(child);
624         if ( !SP_IS_OBJECT(no) )
625             return;
627         if (SP_IS_GUIDE(no)) {
628             SPGuide *g = (SPGuide *) no;
629             nv->guides = g_slist_prepend(nv->guides, g);
630             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
631             if (nv->editable) {
632                 for (GSList *l = nv->views; l != NULL; l = l->next) {
633                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
634                     if (static_cast<SPDesktop*>(l->data)->guides_active)
635                         sp_guide_sensitize(g,
636                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
637                                            TRUE);
638                     if (nv->showguides) {
639                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
640                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
641                         }
642                     } else {
643                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
644                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
645                         }
646                     }
647                 }
648             }
649         }
650     }
653 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
655     SPNamedView *nv = (SPNamedView *) object;
657     if (!strcmp(child->name(), "inkscape:grid")) {
658         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
659             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
660             if ( gr->repr == child ) {
661                 delete gr;
662                 nv->grids = g_slist_remove_link(nv->grids, iter);
663                 break;
664             }
665         }
666     } else {
667         GSList **ref = &nv->guides;
668         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
669             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
670                 *ref = iter->next;
671                 iter->next = NULL;
672                 g_slist_free_1(iter);
673                 break;
674             }
675             ref = &iter->next;
676         }
677     }
679     if (((SPObjectClass *) (parent_class))->remove_child) {
680         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
681     }
684 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
686     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
687          repr != SP_OBJECT_REPR(object) )
688     {
689         if (repr) {
690             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
691         } else {
692              /// \todo FIXME:  Plumb an appropriate XML::Document into this
693              repr = SP_OBJECT_REPR(object)->duplicate(NULL);
694         }
695     }
697     return repr;
700 void SPNamedView::show(SPDesktop *desktop)
702     for (GSList *l = guides; l != NULL; l = l->next) {
703         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
704         if (desktop->guides_active) {
705             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
706         }
707         if (showguides) {
708             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
709                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
710             }
711         } else {
712             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
713                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
714             }
715         }
716     }
718     views = g_slist_prepend(views, desktop);
720     // generate grids specified in SVG:
721     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
722     if (repr) {
723         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
724             if (!strcmp(child->name(), "inkscape:grid")) {
725                 sp_namedview_add_grid(this, child, desktop);
726             }
727         }
728     }
730     desktop->showGrids(grids_visible, false);
733 #define MIN_ONSCREEN_DISTANCE 50
735 /*
736  * Restores window geometry from the document settings or defaults in prefs
737  */
738 void sp_namedview_window_from_document(SPDesktop *desktop)
740     SPNamedView *nv = desktop->namedview;
741     gint geometry_from_file =
742         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
744     // restore window size and position stored with the document
745     if (geometry_from_file) {
746         gint w = MIN(gdk_screen_width(), nv->window_width);
747         gint h = MIN(gdk_screen_height(), nv->window_height);
748         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
749         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
750         if (w>0 && h>0 && x>0 && y>0) {
751             x = MIN(gdk_screen_width() - w, x);
752             y = MIN(gdk_screen_height() - h, y);
753         }
754         if (w>0 && h>0) {
755             desktop->setWindowSize(w, h);
756         }
757         if (x>0 && y>0) {
758             desktop->setWindowPosition(NR::Point(x, y));
759         }
760     }
762     // restore zoom and view
763     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
764         && nv->cx != HUGE_VAL && !isNaN(nv->cx)
765         && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
766         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
767     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
768         desktop->zoom_page();
769     }
771     // cancel any history of zooms up to this point
772     if (desktop->zooms_past) {
773         g_list_free(desktop->zooms_past);
774         desktop->zooms_past = NULL;
775     }
778 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
780     SPObject *layer = NULL;
781     SPDocument *document = desktop->doc();
782     SPNamedView *nv = desktop->namedview;
783     if ( nv->default_layer_id != 0 ) {
784         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
785     }
786     // don't use that object if it's not at least group
787     if ( !layer || !SP_IS_GROUP(layer) ) {
788         layer = NULL;
789     }
790     // if that didn't work out, look for the topmost layer
791     if (!layer) {
792         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
793         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
794             if (desktop->isLayer(iter)) {
795                 layer = iter;
796             }
797         }
798     }
799     if (layer) {
800         desktop->setCurrentLayer(layer);
801     }
803     // FIXME: find a better place to do this
804     desktop->event_log->updateUndoVerbs();
807 void sp_namedview_document_from_window(SPDesktop *desktop)
809     gint save_geometry_in_file =
810         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
811     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
812     NR::Rect const r = desktop->get_display_area();
814     // saving window geometry is not undoable
815     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
816     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
818     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
819     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
820     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
822     if (save_geometry_in_file) {
823         gint w, h, x, y;
824         desktop->getWindowGeometry(x, y, w, h);
825         sp_repr_set_int(view, "inkscape:window-width", w);
826         sp_repr_set_int(view, "inkscape:window-height", h);
827         sp_repr_set_int(view, "inkscape:window-x", x);
828         sp_repr_set_int(view, "inkscape:window-y", y);
829     }
831     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
833     // restore undoability
834     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
837 void SPNamedView::hide(SPDesktop const *desktop)
839     g_assert(desktop != NULL);
840     g_assert(g_slist_find(views, desktop));
842     for (GSList *l = guides; l != NULL; l = l->next) {
843         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
844     }
846     views = g_slist_remove(views, desktop);
849 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
851     g_assert(desktop != NULL);
852     g_assert(g_slist_find(views, desktop));
854     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
856     for (GSList *l = guides; l != NULL; l = l->next) {
857         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
858     }
861 static void sp_namedview_setup_guides(SPNamedView *nv)
863     for (GSList *l = nv->guides; l != NULL; l = l->next) {
864         if (nv->showguides) {
865             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
866                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
867             }
868         } else {
869             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
870                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
871             }
872         }
873     }
876 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
878     unsigned int v;
879     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
880     if (!set) { // hide guides if not specified, for backwards compatibility
881         v = FALSE;
882     } else {
883         v = !v;
884     }
886     bool saved = sp_document_get_undo_sensitive(doc);
887     sp_document_set_undo_sensitive(doc, false);
888     sp_repr_set_boolean(repr, "showguides", v);
889     sp_document_set_undo_sensitive(doc, saved);
891     doc->setModifiedSinceSave();
894 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
896     namedview->grids_visible = show;
898     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
899     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
901     bool saved = sp_document_get_undo_sensitive(doc);
902     sp_document_set_undo_sensitive(doc, false);
903     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
904     sp_document_set_undo_sensitive(doc, saved);
906     /* we don't want the document to get dirty on startup; that's when
907        we call this function with dirty_document = false */
908     if (dirty_document) {
909         doc->setModifiedSinceSave();
910     }
913 gchar const *SPNamedView::getName() const
915     SPException ex;
916     SP_EXCEPTION_INIT(&ex);
917     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
920 guint SPNamedView::getViewCount()
922     return ++viewcount;
925 GSList const *SPNamedView::getViewList() const
927     return views;
930 /* This should be moved somewhere */
932 static gboolean sp_str_to_bool(const gchar *str)
934     if (str) {
935         if (!g_strcasecmp(str, "true") ||
936             !g_strcasecmp(str, "yes") ||
937             !g_strcasecmp(str, "y") ||
938             (atoi(str) != 0)) {
939             return TRUE;
940         }
941     }
943     return FALSE;
946 /* fixme: Collect all these length parsing methods and think common sane API */
948 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
950     if (!str) {
951         return FALSE;
952     }
954     gchar *u;
955     gdouble v = g_ascii_strtod(str, &u);
956     if (!u) {
957         return FALSE;
958     }
959     while (isspace(*u)) {
960         u += 1;
961     }
963     if (!*u) {
964         /* No unit specified - keep default */
965         *val = v;
966         return TRUE;
967     }
969     if (base & SP_UNIT_DEVICE) {
970         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
971             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
972             *val = v;
973             return TRUE;
974         }
975     }
977     if (base & SP_UNIT_ABSOLUTE) {
978         if (!strncmp(u, "pt", 2)) {
979             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
980         } else if (!strncmp(u, "mm", 2)) {
981             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
982         } else if (!strncmp(u, "cm", 2)) {
983             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
984         } else if (!strncmp(u, "m", 1)) {
985             *unit = &sp_unit_get_by_id(SP_UNIT_M);
986         } else if (!strncmp(u, "in", 2)) {
987             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
988         } else {
989             return FALSE;
990         }
991         *val = v;
992         return TRUE;
993     }
995     return FALSE;
998 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
1000     if (!str) {
1001         return FALSE;
1002     }
1004     gchar *u;
1005     gdouble v = g_ascii_strtod(str, &u);
1006     if (!u) {
1007         return FALSE;
1008     }
1009     v = CLAMP(v, 0.0, 1.0);
1011     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1013     return TRUE;
1016 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1018     g_return_val_if_fail(document != NULL, NULL);
1020     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1021     g_assert(nv != NULL);
1023     if (id == NULL) {
1024         return (SPNamedView *) nv;
1025     }
1027     while (nv && strcmp(nv->id, id)) {
1028         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1029     }
1031     return (SPNamedView *) nv;
1034 /**
1035  * Returns namedview's default metric.
1036  */
1037 SPMetric SPNamedView::getDefaultMetric() const
1039     if (doc_units) {
1040         return sp_unit_get_metric(doc_units);
1041     } else {
1042         return SP_PT;
1043     }
1046 /**
1047  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1048  */
1049 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1051     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1052         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1053         if (grid->isEnabled())
1054             return grid;
1055     }
1057     return NULL;
1061 /*
1062   Local Variables:
1063   mode:c++
1064   c-file-style:"stroustrup"
1065   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1066   indent-tabs-mode:nil
1067   fill-column:99
1068   End:
1069 */
1070 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :