Code

remove SPDesktop from SPNamedView when it is destroyed. Fixing bug 183621, but anothe...
[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"
19 #include "display/canvas-grid.h"
20 #include "helper/units.h"
21 #include "svg/svg-color.h"
22 #include "xml/repr.h"
23 #include "attributes.h"
24 #include "document.h"
25 #include "desktop-events.h"
26 #include "desktop-handles.h"
27 #include "event-log.h"
28 #include "sp-guide.h"
29 #include "sp-item-group.h"
30 #include "sp-namedview.h"
31 #include "prefs-utils.h"
32 #include "desktop.h"
33 #include "conn-avoid-ref.h" // for defaultConnSpacing.
35 #include "isnan.h" //temp fix for isnan().  include last
37 #define DEFAULTTOLERANCE 0.4
38 #define DEFAULTGRIDCOLOR 0x3f3fff25
39 #define DEFAULTGRIDEMPCOLOR 0x3f3fff60
40 #define DEFAULTGRIDEMPSPACING 5
41 #define DEFAULTGUIDECOLOR 0x0000ff7f
42 #define DEFAULTGUIDEHICOLOR 0xff00007f
43 #define DEFAULTBORDERCOLOR 0x000000ff
44 #define DEFAULTPAGECOLOR 0xffffff00
46 static void sp_namedview_class_init(SPNamedViewClass *klass);
47 static void sp_namedview_init(SPNamedView *namedview);
49 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
50 static void sp_namedview_release(SPObject *object);
51 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value);
52 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
53 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child);
54 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
56 static void sp_namedview_setup_guides(SPNamedView * nv);
58 static gboolean sp_str_to_bool(const gchar *str);
59 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit);
60 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color);
62 static SPObjectGroupClass * parent_class;
64 GType
65 sp_namedview_get_type()
66 {
67     static GType namedview_type = 0;
68     if (!namedview_type) {
69         GTypeInfo namedview_info = {
70             sizeof(SPNamedViewClass),
71             NULL,       /* base_init */
72             NULL,       /* base_finalize */
73             (GClassInitFunc) sp_namedview_class_init,
74             NULL,       /* class_finalize */
75             NULL,       /* class_data */
76             sizeof(SPNamedView),
77             16, /* n_preallocs */
78             (GInstanceInitFunc) sp_namedview_init,
79             NULL,       /* value_table */
80         };
81         namedview_type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPNamedView", &namedview_info, (GTypeFlags)0);
82     }
83     return namedview_type;
84 }
86 static void sp_namedview_class_init(SPNamedViewClass * klass)
87 {
88     GObjectClass * gobject_class;
89     SPObjectClass * sp_object_class;
91     gobject_class = (GObjectClass *) klass;
92     sp_object_class = (SPObjectClass *) klass;
94     parent_class = (SPObjectGroupClass*) g_type_class_ref(SP_TYPE_OBJECTGROUP);
96     sp_object_class->build = sp_namedview_build;
97     sp_object_class->release = sp_namedview_release;
98     sp_object_class->set = sp_namedview_set;
99     sp_object_class->child_added = sp_namedview_child_added;
100     sp_object_class->remove_child = sp_namedview_remove_child;
101     sp_object_class->write = sp_namedview_write;
104 static void sp_namedview_init(SPNamedView *nv)
106     nv->editable = TRUE;
107     nv->showguides = TRUE;
108     nv->grids_visible = false;
109     nv->showborder = TRUE;
110     nv->showpageshadow = TRUE;
112     nv->guides = NULL;
113     nv->viewcount = 0;
114     nv->grids = NULL;
116     nv->default_layer_id = 0;
118     nv->connector_spacing = defaultConnSpacing;
120     new (&nv->snap_manager) SnapManager(nv);
123 static void sp_namedview_generate_old_grid(SPNamedView * nv, SPDocument *document, Inkscape::XML::Node *repr) {
124     bool old_grid_settings_present = false;
126     // set old settings
127     const char* gridspacingx    = "1px";
128     const char* gridspacingy    = "1px";
129     const char* gridoriginy     = "0px";
130     const char* gridoriginx     = "0px";
131     const char* gridempspacing  = "5";
132     const char* gridcolor       = "#0000ff";
133     const char* gridempcolor    = "#0000ff";
134     const char* gridopacity     = "0.2";
135     const char* gridempopacity  = "0.4";
137     const char* value = NULL;
138     if ((value = repr->attribute("gridoriginx"))) {
139         gridspacingx = value;
140         old_grid_settings_present = true;
141     }
142     if ((value = repr->attribute("gridoriginy"))) {
143         gridoriginy = value;
144         old_grid_settings_present = true;
145     }
146     if ((value = repr->attribute("gridspacingx"))) {
147         gridspacingx = value;
148         old_grid_settings_present = true;
149     }
150     if ((value = repr->attribute("gridspacingy"))) {
151         gridspacingy = value;
152         old_grid_settings_present = true;
153     }
154     if ((value = repr->attribute("gridcolor"))) {
155         gridcolor = value;
156         old_grid_settings_present = true;
157     }
158     if ((value = repr->attribute("gridempcolor"))) {
159         gridempcolor = value;
160         old_grid_settings_present = true;
161     }
162     if ((value = repr->attribute("gridempspacing"))) {
163         gridempspacing = value;
164         old_grid_settings_present = true;
165     }
166     if ((value = repr->attribute("gridopacity"))) {
167         gridopacity = value;
168         old_grid_settings_present = true;
169     }
170     if ((value = repr->attribute("gridempopacity"))) {
171         gridempopacity = value;
172         old_grid_settings_present = true;
173     }
175     if (old_grid_settings_present) {
176         // generate new xy grid with the correct settings
177         // 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.
179         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
180         Inkscape::XML::Node *newnode = xml_doc->createElement("inkscape:grid");
181         newnode->setAttribute("id", "GridFromPre046Settings");
182         newnode->setAttribute("type", Inkscape::CanvasGrid::getSVGName(Inkscape::GRID_RECTANGULAR));
183         newnode->setAttribute("originx", gridoriginx);
184         newnode->setAttribute("originy", gridoriginy);
185         newnode->setAttribute("spacingx", gridspacingx);
186         newnode->setAttribute("spacingy", gridspacingy);
187         newnode->setAttribute("color", gridcolor);
188         newnode->setAttribute("empcolor", gridempcolor);
189         newnode->setAttribute("opacity", gridopacity);
190         newnode->setAttribute("empopacity", gridempopacity);
191         newnode->setAttribute("empspacing", gridempspacing);
193         repr->appendChild(newnode);
194         Inkscape::GC::release(newnode);
196         // remove all old settings
197         repr->setAttribute("gridoriginx", NULL);
198         repr->setAttribute("gridoriginy", NULL);
199         repr->setAttribute("gridspacingx", NULL);
200         repr->setAttribute("gridspacingy", NULL);
201         repr->setAttribute("gridcolor", NULL);
202         repr->setAttribute("gridempcolor", NULL);
203         repr->setAttribute("gridopacity", NULL);
204         repr->setAttribute("gridempopacity", NULL);
205         repr->setAttribute("gridempspacing", NULL);
207 //        sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid from pre0.46 grid settings"));
208     }
211 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
213     SPNamedView *nv = (SPNamedView *) object;
214     SPObjectGroup *og = (SPObjectGroup *) object;
216     if (((SPObjectClass *) (parent_class))->build) {
217         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
218     }
220     sp_object_read_attr(object, "inkscape:document-units");
221     sp_object_read_attr(object, "viewonly");
222     sp_object_read_attr(object, "showguides");
223     sp_object_read_attr(object, "showgrid");
224     sp_object_read_attr(object, "gridtolerance");
225     sp_object_read_attr(object, "guidetolerance");
226     sp_object_read_attr(object, "objecttolerance");
227     sp_object_read_attr(object, "guidecolor");
228     sp_object_read_attr(object, "guideopacity");
229     sp_object_read_attr(object, "guidehicolor");
230     sp_object_read_attr(object, "guidehiopacity");
231     sp_object_read_attr(object, "showborder");
232     sp_object_read_attr(object, "inkscape:showpageshadow");
233     sp_object_read_attr(object, "borderlayer");
234     sp_object_read_attr(object, "bordercolor");
235     sp_object_read_attr(object, "borderopacity");
236     sp_object_read_attr(object, "pagecolor");
237     sp_object_read_attr(object, "inkscape:pageopacity");
238     sp_object_read_attr(object, "inkscape:pageshadow");
239     sp_object_read_attr(object, "inkscape:zoom");
240     sp_object_read_attr(object, "inkscape:cx");
241     sp_object_read_attr(object, "inkscape:cy");
242     sp_object_read_attr(object, "inkscape:window-width");
243     sp_object_read_attr(object, "inkscape:window-height");
244     sp_object_read_attr(object, "inkscape:window-x");
245     sp_object_read_attr(object, "inkscape:window-y");
246     sp_object_read_attr(object, "inkscape:snap-global");
247     sp_object_read_attr(object, "inkscape:snap-bbox");    
248     sp_object_read_attr(object, "inkscape:snap-nodes");
249     sp_object_read_attr(object, "inkscape:snap-guide");
250     sp_object_read_attr(object, "inkscape:snap-center");
251     sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
252     sp_object_read_attr(object, "inkscape:snap-intersection-line-segments");
253     sp_object_read_attr(object, "inkscape:object-paths");
254     sp_object_read_attr(object, "inkscape:object-nodes");
255     sp_object_read_attr(object, "inkscape:bbox-paths");
256     sp_object_read_attr(object, "inkscape:bbox-nodes");    
257     sp_object_read_attr(object, "inkscape:current-layer");
258     sp_object_read_attr(object, "inkscape:connector-spacing");
260     /* Construct guideline list */
261     for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
262         if (SP_IS_GUIDE(o)) {
263             SPGuide * g = SP_GUIDE(o);
264             nv->guides = g_slist_prepend(nv->guides, g);
265             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
266         }
267     }
269     // backwards compatibility with grid settings (pre 0.46)
270     sp_namedview_generate_old_grid(nv, document, repr);
273 static void sp_namedview_release(SPObject *object)
275     SPNamedView *namedview = (SPNamedView *) object;
277     if (namedview->guides) {
278         g_slist_free(namedview->guides);
279         namedview->guides = NULL;
280     }
282     // delete grids:
283     while ( namedview->grids ) {
284         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
285         delete gr;
286         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
287     }
289     if (((SPObjectClass *) parent_class)->release) {
290         ((SPObjectClass *) parent_class)->release(object);
291     }
293     namedview->snap_manager.~SnapManager();
296 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
298     SPNamedView *nv = SP_NAMEDVIEW(object);
299     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
301     switch (key) {
302     case SP_ATTR_VIEWONLY:
303             nv->editable = (!value);
304             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
305             break;
306     case SP_ATTR_SHOWGUIDES:
307             if (!value) { // show guides if not specified, for backwards compatibility
308                 nv->showguides = TRUE;
309             } else {
310                 nv->showguides = sp_str_to_bool(value);
311             }
312             sp_namedview_setup_guides(nv);
313             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
314             break;
315     case SP_ATTR_SHOWGRIDS:
316             if (!value) { // don't show grids if not specified, for backwards compatibility
317                 nv->grids_visible = false;
318             } else {
319                 nv->grids_visible = sp_str_to_bool(value);
320             }
321             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
322             break;
323     case SP_ATTR_GRIDTOLERANCE:
324             nv->gridtoleranceunit = &px;
325             nv->gridtolerance = DEFAULTTOLERANCE;
326             if (value) {
327                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
328             }
329             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
330             break;
331     case SP_ATTR_GUIDETOLERANCE:
332             nv->guidetoleranceunit = &px;
333             nv->guidetolerance = DEFAULTTOLERANCE;
334             if (value) {
335                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
336             }
337             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
338             break;
339     case SP_ATTR_OBJECTTOLERANCE:
340             nv->objecttoleranceunit = &px;
341             nv->objecttolerance = DEFAULTTOLERANCE;
342             if (value) {
343                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
344             }
345             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
346             break;
347     case SP_ATTR_GUIDECOLOR:
348             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
349             if (value) {
350                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
351             }
352             for (GSList *l = nv->guides; l != NULL; l = l->next) {
353                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
354             }
355             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
356             break;
357     case SP_ATTR_GUIDEOPACITY:
358             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
359             sp_nv_read_opacity(value, &nv->guidecolor);
360             for (GSList *l = nv->guides; l != NULL; l = l->next) {
361                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
362             }
363             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
364             break;
365     case SP_ATTR_GUIDEHICOLOR:
366             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
367             if (value) {
368                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
369             }
370             for (GSList *l = nv->guides; l != NULL; l = l->next) {
371                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
372             }
373             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
374             break;
375     case SP_ATTR_GUIDEHIOPACITY:
376             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
377             sp_nv_read_opacity(value, &nv->guidehicolor);
378             for (GSList *l = nv->guides; l != NULL; l = l->next) {
379                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
380             }
381             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
382             break;
383     case SP_ATTR_SHOWBORDER:
384             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
385             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
386             break;
387     case SP_ATTR_BORDERLAYER:
388             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
389             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
390             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
391             break;
392     case SP_ATTR_BORDERCOLOR:
393             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
394             if (value) {
395                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
396             }
397             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
398             break;
399         case SP_ATTR_BORDEROPACITY:
400             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
401             sp_nv_read_opacity(value, &nv->bordercolor);
402             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
403             break;
404         case SP_ATTR_PAGECOLOR:
405             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
406             if (value) {
407                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
408             }
409             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
410             break;
411     case SP_ATTR_INKSCAPE_PAGEOPACITY:
412             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
413             sp_nv_read_opacity(value, &nv->pagecolor);
414             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
415             break;
416     case SP_ATTR_INKSCAPE_PAGESHADOW:
417             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
418             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
419             break;
420     case SP_ATTR_SHOWPAGESHADOW:
421             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
422             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
423             break;
424     case SP_ATTR_INKSCAPE_ZOOM:
425             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
426             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
427             break;
428     case SP_ATTR_INKSCAPE_CX:
429             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
430             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
431             break;
432     case SP_ATTR_INKSCAPE_CY:
433             nv->cy = 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_WINDOW_WIDTH:
437             nv->window_width = value? atoi(value) : -1; // -1 means not set
438             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
439             break;
440     case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
441             nv->window_height = value ? atoi(value) : -1; // -1 means not set
442             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
443             break;
444     case SP_ATTR_INKSCAPE_WINDOW_X:
445             nv->window_x = value ? atoi(value) : -1; // -1 means not set
446             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
447             break;
448     case SP_ATTR_INKSCAPE_WINDOW_Y:
449             nv->window_y = value ? atoi(value) : -1; // -1 means not set
450             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
451             break;
452     case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
453             nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
454             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
455             break;
456     case SP_ATTR_INKSCAPE_SNAP_BBOX:
457                 nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
458             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
459             break;
460     case SP_ATTR_INKSCAPE_SNAP_NODES:
461             nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
462             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
463             break;
464     case SP_ATTR_INKSCAPE_SNAP_CENTER:
465             nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
466             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
467             break;
468     case SP_ATTR_INKSCAPE_SNAP_GUIDE:
469             nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
470             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
471             break;
472     case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
473             nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
474             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
475             break;
476     case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
477             nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
478             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
479             break;
480     case SP_ATTR_INKSCAPE_OBJECT_PATHS:
481             nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
482             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
483             break;
484     case SP_ATTR_INKSCAPE_OBJECT_NODES:
485             nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
486             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
487             break;
488     case SP_ATTR_INKSCAPE_BBOX_PATHS:
489             nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
490             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
491             break;
492     case SP_ATTR_INKSCAPE_BBOX_NODES:
493             nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);            
494             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
495             break;
496     case SP_ATTR_INKSCAPE_CURRENT_LAYER:
497             nv->default_layer_id = value ? g_quark_from_string(value) : 0;
498             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
499             break;
500     case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
501             nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
502                     defaultConnSpacing;
503             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
504             break;
505     case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
506             /* The default unit if the document doesn't override this: e.g. for files saved as
507              * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
508              * earlier.
509              *
510              * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
511              * about "not the same numbers as what's in the SVG file" (at least for documents
512              * without a viewBox attribute on the root <svg> element).  Similarly, it's also
513              * the most reliable unit (i.e. least likely to be wrong in different viewing
514              * conditions) for viewBox-less SVG files given that it's the unit that inkscape
515              * uses for all coordinates.
516              *
517              * For documents that do have a viewBox attribute on the root <svg> element, it
518              * might be better if we used either viewBox coordinates or if we used the unit of
519              * say the width attribute of the root <svg> element.  However, these pose problems
520              * in that they aren't in general absolute units as currently required by
521              * doc_units.
522              */
523             SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
525             if (value) {
526                 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
527                 if ( req_unit == NULL ) {
528                     g_warning("Unrecognized unit `%s'", value);
529                     /* fixme: Document errors should be reported in the status bar or
530                      * the like (e.g. as per
531                      * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
532                      * should be only for programmer errors. */
533                 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
534                             req_unit->base == SP_UNIT_DEVICE     ) {
535                     new_unit = req_unit;
536                 } else {
537                     g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
538                               value);
539                     /* fixme: Don't use g_log (see above). */
540                 }
541             }
542             nv->doc_units = new_unit;
543             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
544             break;
545     }
546     default:
547             if (((SPObjectClass *) (parent_class))->set) {
548                 ((SPObjectClass *) (parent_class))->set(object, key, value);
549             }
550             break;
551     }
554 /**
555 * 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,
556 * otherwise only add it to the specified desktop.
557 */
558 static Inkscape::CanvasGrid*
559 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
560     Inkscape::CanvasGrid* grid = NULL;
561     //check if namedview already has an object for this grid
562     for (GSList *l = nv->grids; l != NULL; l = l->next) {
563         Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
564         if (repr == g->repr) {
565             grid = g;
566             break;
567         }
568     }
570     if (!grid) {
571         //create grid object
572         Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
573         SPDocument *doc = NULL;
574         if (desktop)
575             doc = sp_desktop_document(desktop);
576         else
577             doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
578         if (!doc) {
579             g_warning("sp_namedview_add_grid - how come doc is null here?!");
580             return NULL;
581         }
582         grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
583         nv->grids = g_slist_append(nv->grids, grid);
584         //Initialize the snapping parameters for the new grid
585         bool enabled_node = nv->snap_manager.getSnapModeNode();
586         bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
587         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
588         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
589     }
591     if (!desktop) {
592         //add canvasitem to all desktops
593         for (GSList *l = nv->views; l != NULL; l = l->next) {
594             SPDesktop *dt = static_cast<SPDesktop*>(l->data);
595             grid->createCanvasItem(dt);
596         }
597     } else {
598         //add canvasitem only for specified desktop
599         grid->createCanvasItem(desktop);
600     }
602     return grid;
605 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
607     SPNamedView *nv = (SPNamedView *) object;
609     if (((SPObjectClass *) (parent_class))->child_added) {
610         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
611     }
613     if (!strcmp(child->name(), "inkscape:grid")) {
614         sp_namedview_add_grid(nv, child, NULL);
615     } else {
616         SPObject *no = object->document->getObjectByRepr(child);
617         if ( !SP_IS_OBJECT(no) )
618             return;
620         if (SP_IS_GUIDE(no)) {
621             SPGuide *g = (SPGuide *) no;
622             nv->guides = g_slist_prepend(nv->guides, g);
623             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
624             if (nv->editable) {
625                 for (GSList *l = nv->views; l != NULL; l = l->next) {
626                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
627                     if (static_cast<SPDesktop*>(l->data)->guides_active)
628                         sp_guide_sensitize(g,
629                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
630                                            TRUE);
631                     if (nv->showguides) {
632                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
633                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
634                         }
635                     } else {
636                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
637                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
638                         }
639                     }
640                 }
641             }
642         }
643     }
646 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
648     SPNamedView *nv = (SPNamedView *) object;
650     if (!strcmp(child->name(), "inkscape:grid")) {
651         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
652             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
653             if ( gr->repr == child ) {
654                 delete gr;
655                 nv->grids = g_slist_remove_link(nv->grids, iter);
656                 break;
657             }
658         }
659     } else {
660         GSList **ref = &nv->guides;
661         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
662             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
663                 *ref = iter->next;
664                 iter->next = NULL;
665                 g_slist_free_1(iter);
666                 break;
667             }
668             ref = &iter->next;
669         }
670     }
672     if (((SPObjectClass *) (parent_class))->remove_child) {
673         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
674     }
677 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
679     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
680          repr != SP_OBJECT_REPR(object) )
681     {
682         if (repr) {
683             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
684         } else {
685              /// \todo FIXME:  Plumb an appropriate XML::Document into this
686              repr = SP_OBJECT_REPR(object)->duplicate(NULL);
687         }
688     }
690     return repr;
693 void SPNamedView::show(SPDesktop *desktop)
695     for (GSList *l = guides; l != NULL; l = l->next) {
696         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
697         if (desktop->guides_active) {
698             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
699         }
700         if (showguides) {
701             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
702                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
703             }
704         } else {
705             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
706                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
707             }
708         }
709     }
711     views = g_slist_prepend(views, desktop);
713     // generate grids specified in SVG:
714     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
715     if (repr) {
716         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
717             if (!strcmp(child->name(), "inkscape:grid")) {
718                 sp_namedview_add_grid(this, child, desktop);
719             }
720         }
721     }
723     desktop->showGrids(grids_visible, false);
726 #define MIN_ONSCREEN_DISTANCE 50
728 /*
729  * Restores window geometry from the document settings or defaults in prefs
730  */
731 void sp_namedview_window_from_document(SPDesktop *desktop)
733     SPNamedView *nv = desktop->namedview;
734     gint geometry_from_file =
735         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
737     // restore window size and position stored with the document
738     if (geometry_from_file) {
739         gint w = MIN(gdk_screen_width(), nv->window_width);
740         gint h = MIN(gdk_screen_height(), nv->window_height);
741         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
742         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
743         if (w>0 && h>0 && x>0 && y>0) {
744             x = MIN(gdk_screen_width() - w, x);
745             y = MIN(gdk_screen_height() - h, y);
746         }
747         if (w>0 && h>0) {
748             desktop->setWindowSize(w, h);
749         }
750         if (x>0 && y>0) {
751             desktop->setWindowPosition(NR::Point(x, y));
752         }
753     }
755     // restore zoom and view
756     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
757         && nv->cx != HUGE_VAL && !isNaN(nv->cx)
758         && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
759         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
760     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
761         desktop->zoom_page();
762     }
764     // cancel any history of zooms up to this point
765     if (desktop->zooms_past) {
766         g_list_free(desktop->zooms_past);
767         desktop->zooms_past = NULL;
768     }
771 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
773     SPObject *layer = NULL;
774     SPDocument *document = desktop->doc();
775     SPNamedView *nv = desktop->namedview;
776     if ( nv->default_layer_id != 0 ) {
777         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
778     }
779     // don't use that object if it's not at least group
780     if ( !layer || !SP_IS_GROUP(layer) ) {
781         layer = NULL;
782     }
783     // if that didn't work out, look for the topmost layer
784     if (!layer) {
785         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
786         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
787             if (desktop->isLayer(iter)) {
788                 layer = iter;
789             }
790         }
791     }
792     if (layer) {
793         desktop->setCurrentLayer(layer);
794     }
796     // FIXME: find a better place to do this
797     desktop->event_log->updateUndoVerbs();
800 void sp_namedview_document_from_window(SPDesktop *desktop)
802     gint save_geometry_in_file =
803         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
804     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
805     NR::Rect const r = desktop->get_display_area();
807     // saving window geometry is not undoable
808     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
809     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
811     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
812     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
813     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
815     if (save_geometry_in_file) {
816         gint w, h, x, y;
817         desktop->getWindowGeometry(x, y, w, h);
818         sp_repr_set_int(view, "inkscape:window-width", w);
819         sp_repr_set_int(view, "inkscape:window-height", h);
820         sp_repr_set_int(view, "inkscape:window-x", x);
821         sp_repr_set_int(view, "inkscape:window-y", y);
822     }
824     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
826     // restore undoability
827     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
830 void SPNamedView::hide(SPDesktop const *desktop)
832     g_assert(desktop != NULL);
833     g_assert(g_slist_find(views, desktop));
835     for (GSList *l = guides; l != NULL; l = l->next) {
836         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
837     }
839     views = g_slist_remove(views, desktop);
842 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
844     g_assert(desktop != NULL);
845     g_assert(g_slist_find(views, desktop));
847     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
849     for (GSList *l = guides; l != NULL; l = l->next) {
850         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
851     }
854 static void sp_namedview_setup_guides(SPNamedView *nv)
856     for (GSList *l = nv->guides; l != NULL; l = l->next) {
857         if (nv->showguides) {
858             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
859                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
860             }
861         } else {
862             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
863                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
864             }
865         }
866     }
869 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
871     unsigned int v;
872     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
873     if (!set) { // hide guides if not specified, for backwards compatibility
874         v = FALSE;
875     } else {
876         v = !v;
877     }
879     bool saved = sp_document_get_undo_sensitive(doc);
880     sp_document_set_undo_sensitive(doc, false);
881     sp_repr_set_boolean(repr, "showguides", v);
882     sp_document_set_undo_sensitive(doc, saved);
884     doc->setModifiedSinceSave();
887 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
889     namedview->grids_visible = show;
891     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
892     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
894     bool saved = sp_document_get_undo_sensitive(doc);
895     sp_document_set_undo_sensitive(doc, false);
896     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
897     sp_document_set_undo_sensitive(doc, saved);
899     /* we don't want the document to get dirty on startup; that's when
900        we call this function with dirty_document = false */
901     if (dirty_document) {
902         doc->setModifiedSinceSave();
903     }
906 gchar const *SPNamedView::getName() const
908     SPException ex;
909     SP_EXCEPTION_INIT(&ex);
910     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
913 guint SPNamedView::getViewCount()
915     return ++viewcount;
918 GSList const *SPNamedView::getViewList() const
920     return views;
923 /* This should be moved somewhere */
925 static gboolean sp_str_to_bool(const gchar *str)
927     if (str) {
928         if (!g_strcasecmp(str, "true") ||
929             !g_strcasecmp(str, "yes") ||
930             !g_strcasecmp(str, "y") ||
931             (atoi(str) != 0)) {
932             return TRUE;
933         }
934     }
936     return FALSE;
939 /* fixme: Collect all these length parsing methods and think common sane API */
941 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
943     if (!str) {
944         return FALSE;
945     }
947     gchar *u;
948     gdouble v = g_ascii_strtod(str, &u);
949     if (!u) {
950         return FALSE;
951     }
952     while (isspace(*u)) {
953         u += 1;
954     }
956     if (!*u) {
957         /* No unit specified - keep default */
958         *val = v;
959         return TRUE;
960     }
962     if (base & SP_UNIT_DEVICE) {
963         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
964             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
965             *val = v;
966             return TRUE;
967         }
968     }
970     if (base & SP_UNIT_ABSOLUTE) {
971         if (!strncmp(u, "pt", 2)) {
972             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
973         } else if (!strncmp(u, "mm", 2)) {
974             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
975         } else if (!strncmp(u, "cm", 2)) {
976             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
977         } else if (!strncmp(u, "m", 1)) {
978             *unit = &sp_unit_get_by_id(SP_UNIT_M);
979         } else if (!strncmp(u, "in", 2)) {
980             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
981         } else {
982             return FALSE;
983         }
984         *val = v;
985         return TRUE;
986     }
988     return FALSE;
991 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
993     if (!str) {
994         return FALSE;
995     }
997     gchar *u;
998     gdouble v = g_ascii_strtod(str, &u);
999     if (!u) {
1000         return FALSE;
1001     }
1002     v = CLAMP(v, 0.0, 1.0);
1004     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1006     return TRUE;
1009 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1011     g_return_val_if_fail(document != NULL, NULL);
1013     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1014     g_assert(nv != NULL);
1016     if (id == NULL) {
1017         return (SPNamedView *) nv;
1018     }
1020     while (nv && strcmp(nv->id, id)) {
1021         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1022     }
1024     return (SPNamedView *) nv;
1027 /**
1028  * Returns namedview's default metric.
1029  */
1030 SPMetric SPNamedView::getDefaultMetric() const
1032     if (doc_units) {
1033         return sp_unit_get_metric(doc_units);
1034     } else {
1035         return SP_PT;
1036     }
1039 /**
1040  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1041  */
1042 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1044     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1045         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1046         if (grid->isEnabled())
1047             return grid;
1048     }
1050     return NULL;
1054 /*
1055   Local Variables:
1056   mode:c++
1057   c-file-style:"stroustrup"
1058   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1059   indent-tabs-mode:nil
1060   fill-column:99
1061   End:
1062 */
1063 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :