Code

abstract use of sodipodi:modified
[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;
285         delete gr;
286         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids);
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         grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
579         nv->grids = g_slist_append(nv->grids, grid);
580         //Initialize the snapping parameters for the new grid
581         bool enabled_node = nv->snap_manager.getSnapModeNode();
582         bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
583         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
584         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
585     }
587     if (!desktop) {
588         //add canvasitem to all desktops
589         for (GSList *l = nv->views; l != NULL; l = l->next) {
590             SPDesktop *dt = static_cast<SPDesktop*>(l->data);
591             grid->createCanvasItem(dt);
592         }
593     } else {
594         //add canvasitem only for specified desktop
595         grid->createCanvasItem(desktop);
596     }
598     return grid;
601 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
603     SPNamedView *nv = (SPNamedView *) object;
605     if (((SPObjectClass *) (parent_class))->child_added) {
606         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
607     }
609     if (!strcmp(child->name(), "inkscape:grid")) {
610         sp_namedview_add_grid(nv, child, NULL);
611     } else {
612         SPObject *no = object->document->getObjectByRepr(child);
613         if ( !SP_IS_OBJECT(no) )
614             return;
616         if (SP_IS_GUIDE(no)) {
617             SPGuide *g = (SPGuide *) no;
618             nv->guides = g_slist_prepend(nv->guides, g);
619             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
620             if (nv->editable) {
621                 for (GSList *l = nv->views; l != NULL; l = l->next) {
622                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
623                     if (static_cast<SPDesktop*>(l->data)->guides_active)
624                         sp_guide_sensitize(g,
625                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
626                                            TRUE);
627                     if (nv->showguides) {
628                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
629                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
630                         }
631                     } else {
632                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
633                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
634                         }
635                     }
636                 }
637             }
638         }
639     }
642 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
644     SPNamedView *nv = (SPNamedView *) object;
646     if (!strcmp(child->name(), "inkscape:grid")) {
647         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
648             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
649             if ( gr->repr == child ) {
650                 delete gr;
651                 nv->grids = g_slist_remove_link(nv->grids, iter);
652                 break;
653             }
654         }
655     } else {
656         GSList **ref = &nv->guides;
657         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
658             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
659                 *ref = iter->next;
660                 iter->next = NULL;
661                 g_slist_free_1(iter);
662                 break;
663             }
664             ref = &iter->next;
665         }
666     }
668     if (((SPObjectClass *) (parent_class))->remove_child) {
669         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
670     }
673 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
675     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
676          repr != SP_OBJECT_REPR(object) )
677     {
678         if (repr) {
679             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
680         } else {
681              /// \todo FIXME:  Plumb an appropriate XML::Document into this
682              repr = SP_OBJECT_REPR(object)->duplicate(NULL);
683         }
684     }
686     return repr;
689 void SPNamedView::show(SPDesktop *desktop)
691     for (GSList *l = guides; l != NULL; l = l->next) {
692         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
693         if (desktop->guides_active) {
694             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
695         }
696         if (showguides) {
697             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
698                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
699             }
700         } else {
701             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
702                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
703             }
704         }
705     }
707     views = g_slist_prepend(views, desktop);
709     // generate grids specified in SVG:
710     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
711     if (repr) {
712         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
713             if (!strcmp(child->name(), "inkscape:grid")) {
714                 sp_namedview_add_grid(this, child, desktop);
715             }
716         }
717     }
719     desktop->showGrids(grids_visible, false);
722 #define MIN_ONSCREEN_DISTANCE 50
724 /*
725  * Restores window geometry from the document settings or defaults in prefs
726  */
727 void sp_namedview_window_from_document(SPDesktop *desktop)
729     SPNamedView *nv = desktop->namedview;
730     gint geometry_from_file =
731         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
733     // restore window size and position stored with the document
734     if (geometry_from_file) {
735         gint w = MIN(gdk_screen_width(), nv->window_width);
736         gint h = MIN(gdk_screen_height(), nv->window_height);
737         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
738         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
739         if (w>0 && h>0 && x>0 && y>0) {
740             x = MIN(gdk_screen_width() - w, x);
741             y = MIN(gdk_screen_height() - h, y);
742         }
743         if (w>0 && h>0) {
744             desktop->setWindowSize(w, h);
745         }
746         if (x>0 && y>0) {
747             desktop->setWindowPosition(NR::Point(x, y));
748         }
749     }
751     // restore zoom and view
752     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
753         && nv->cx != HUGE_VAL && !isNaN(nv->cx)
754         && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
755         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
756     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
757         desktop->zoom_page();
758     }
760     // cancel any history of zooms up to this point
761     if (desktop->zooms_past) {
762         g_list_free(desktop->zooms_past);
763         desktop->zooms_past = NULL;
764     }
767 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
769     SPObject *layer = NULL;
770     SPDocument *document = desktop->doc();
771     SPNamedView *nv = desktop->namedview;
772     if ( nv->default_layer_id != 0 ) {
773         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
774     }
775     // don't use that object if it's not at least group
776     if ( !layer || !SP_IS_GROUP(layer) ) {
777         layer = NULL;
778     }
779     // if that didn't work out, look for the topmost layer
780     if (!layer) {
781         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
782         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
783             if (desktop->isLayer(iter)) {
784                 layer = iter;
785             }
786         }
787     }
788     if (layer) {
789         desktop->setCurrentLayer(layer);
790     }
792     // FIXME: find a better place to do this
793     desktop->event_log->updateUndoVerbs();
796 void sp_namedview_document_from_window(SPDesktop *desktop)
798     gint save_geometry_in_file =
799         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
800     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
801     NR::Rect const r = desktop->get_display_area();
803     // saving window geometry is not undoable
804     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
805     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
807     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
808     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
809     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
811     if (save_geometry_in_file) {
812         gint w, h, x, y;
813         desktop->getWindowGeometry(x, y, w, h);
814         sp_repr_set_int(view, "inkscape:window-width", w);
815         sp_repr_set_int(view, "inkscape:window-height", h);
816         sp_repr_set_int(view, "inkscape:window-x", x);
817         sp_repr_set_int(view, "inkscape:window-y", y);
818     }
820     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
822     // restore undoability
823     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
826 void SPNamedView::hide(SPDesktop const *desktop)
828     g_assert(desktop != NULL);
829     g_assert(g_slist_find(views, desktop));
831     for (GSList *l = guides; l != NULL; l = l->next) {
832         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
833     }
835     views = g_slist_remove(views, desktop);
837     // delete grids:
838     while ( grids ) {
839         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)grids->data;
840         delete gr;
841         grids = g_slist_remove_link(grids, grids);
842     }
845 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
847     g_assert(desktop != NULL);
848     g_assert(g_slist_find(views, desktop));
850     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
852     for (GSList *l = guides; l != NULL; l = l->next) {
853         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
854     }
857 static void sp_namedview_setup_guides(SPNamedView *nv)
859     for (GSList *l = nv->guides; l != NULL; l = l->next) {
860         if (nv->showguides) {
861             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
862                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
863             }
864         } else {
865             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
866                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
867             }
868         }
869     }
872 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
874     unsigned int v;
875     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
876     if (!set) { // hide guides if not specified, for backwards compatibility
877         v = FALSE;
878     } else {
879         v = !v;
880     }
882     bool saved = sp_document_get_undo_sensitive(doc);
883     sp_document_set_undo_sensitive(doc, false);
885     sp_repr_set_boolean(repr, "showguides", v);
887     doc->setModified();
888     sp_document_set_undo_sensitive(doc, saved);
891 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
893     namedview->grids_visible = show;
895     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
896     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
898     bool saved = sp_document_get_undo_sensitive(doc);
899     sp_document_set_undo_sensitive(doc, false);
901     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
903     /* we don't want the document to get dirty on startup; that's when
904        we call this function with dirty_document = false */
905     if (dirty_document) {
906         doc->setModified();
907     }
908     sp_document_set_undo_sensitive(doc, saved);
911 gchar const *SPNamedView::getName() const
913     SPException ex;
914     SP_EXCEPTION_INIT(&ex);
915     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
918 guint SPNamedView::getViewCount()
920     return ++viewcount;
923 GSList const *SPNamedView::getViewList() const
925     return views;
928 /* This should be moved somewhere */
930 static gboolean sp_str_to_bool(const gchar *str)
932     if (str) {
933         if (!g_strcasecmp(str, "true") ||
934             !g_strcasecmp(str, "yes") ||
935             !g_strcasecmp(str, "y") ||
936             (atoi(str) != 0)) {
937             return TRUE;
938         }
939     }
941     return FALSE;
944 /* fixme: Collect all these length parsing methods and think common sane API */
946 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
948     if (!str) {
949         return FALSE;
950     }
952     gchar *u;
953     gdouble v = g_ascii_strtod(str, &u);
954     if (!u) {
955         return FALSE;
956     }
957     while (isspace(*u)) {
958         u += 1;
959     }
961     if (!*u) {
962         /* No unit specified - keep default */
963         *val = v;
964         return TRUE;
965     }
967     if (base & SP_UNIT_DEVICE) {
968         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
969             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
970             *val = v;
971             return TRUE;
972         }
973     }
975     if (base & SP_UNIT_ABSOLUTE) {
976         if (!strncmp(u, "pt", 2)) {
977             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
978         } else if (!strncmp(u, "mm", 2)) {
979             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
980         } else if (!strncmp(u, "cm", 2)) {
981             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
982         } else if (!strncmp(u, "m", 1)) {
983             *unit = &sp_unit_get_by_id(SP_UNIT_M);
984         } else if (!strncmp(u, "in", 2)) {
985             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
986         } else {
987             return FALSE;
988         }
989         *val = v;
990         return TRUE;
991     }
993     return FALSE;
996 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
998     if (!str) {
999         return FALSE;
1000     }
1002     gchar *u;
1003     gdouble v = g_ascii_strtod(str, &u);
1004     if (!u) {
1005         return FALSE;
1006     }
1007     v = CLAMP(v, 0.0, 1.0);
1009     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1011     return TRUE;
1014 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1016     g_return_val_if_fail(document != NULL, NULL);
1018     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1019     g_assert(nv != NULL);
1021     if (id == NULL) {
1022         return (SPNamedView *) nv;
1023     }
1025     while (nv && strcmp(nv->id, id)) {
1026         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1027     }
1029     return (SPNamedView *) nv;
1032 /**
1033  * Returns namedview's default metric.
1034  */
1035 SPMetric SPNamedView::getDefaultMetric() const
1037     if (doc_units) {
1038         return sp_unit_get_metric(doc_units);
1039     } else {
1040         return SP_PT;
1041     }
1044 /**
1045  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1046  */
1047 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1049     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1050         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1051         if (grid->isEnabled())
1052             return grid;
1053     }
1055     return NULL;
1059 /*
1060   Local Variables:
1061   mode:c++
1062   c-file-style:"stroustrup"
1063   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1064   indent-tabs-mode:nil
1065   fill-column:99
1066   End:
1067 */
1068 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :