Code

fdbedab4e5b3a9ab766e81be660410151a26618c
[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->showborder = TRUE;
109     nv->showpageshadow = TRUE;
111     nv->guides = NULL;
112     nv->viewcount = 0;
113     nv->grids = NULL;
115     nv->default_layer_id = 0;
117     nv->connector_spacing = defaultConnSpacing;
119     new (&nv->snap_manager) SnapManager(nv);
122 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
124     SPNamedView *nv = (SPNamedView *) object;
125     SPObjectGroup *og = (SPObjectGroup *) object;
127     if (((SPObjectClass *) (parent_class))->build) {
128         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
129     }
131     sp_object_read_attr(object, "inkscape:document-units");
132     sp_object_read_attr(object, "viewonly");
133     sp_object_read_attr(object, "showguides");
134     sp_object_read_attr(object, "showgrid");
135     sp_object_read_attr(object, "gridtolerance");
136     sp_object_read_attr(object, "guidetolerance");
137     sp_object_read_attr(object, "objecttolerance");
138     sp_object_read_attr(object, "guidecolor");
139     sp_object_read_attr(object, "guideopacity");
140     sp_object_read_attr(object, "guidehicolor");
141     sp_object_read_attr(object, "guidehiopacity");
142     sp_object_read_attr(object, "showborder");
143     sp_object_read_attr(object, "inkscape:showpageshadow");
144     sp_object_read_attr(object, "borderlayer");
145     sp_object_read_attr(object, "bordercolor");
146     sp_object_read_attr(object, "borderopacity");
147     sp_object_read_attr(object, "pagecolor");
148     sp_object_read_attr(object, "inkscape:pageopacity");
149     sp_object_read_attr(object, "inkscape:pageshadow");
150     sp_object_read_attr(object, "inkscape:zoom");
151     sp_object_read_attr(object, "inkscape:cx");
152     sp_object_read_attr(object, "inkscape:cy");
153     sp_object_read_attr(object, "inkscape:window-width");
154     sp_object_read_attr(object, "inkscape:window-height");
155     sp_object_read_attr(object, "inkscape:window-x");
156     sp_object_read_attr(object, "inkscape:window-y");
157     sp_object_read_attr(object, "inkscape:snap-global");
158     sp_object_read_attr(object, "inkscape:snap-bbox");    
159     sp_object_read_attr(object, "inkscape:snap-nodes");
160     sp_object_read_attr(object, "inkscape:snap-guide");
161     sp_object_read_attr(object, "inkscape:snap-center");
162     sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
163     sp_object_read_attr(object, "inkscape:snap-intersection-line-segments");
164     sp_object_read_attr(object, "inkscape:object-paths");
165     sp_object_read_attr(object, "inkscape:object-nodes");
166     sp_object_read_attr(object, "inkscape:bbox-paths");
167     sp_object_read_attr(object, "inkscape:bbox-nodes");    
168     sp_object_read_attr(object, "inkscape:current-layer");
169     sp_object_read_attr(object, "inkscape:connector-spacing");
171     /* Construct guideline list */
173     for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
174         if (SP_IS_GUIDE(o)) {
175             SPGuide * g = SP_GUIDE(o);
176             nv->guides = g_slist_prepend(nv->guides, g);
177             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
178         }
179     }
182 static void sp_namedview_release(SPObject *object)
184     SPNamedView *namedview = (SPNamedView *) object;
186     if (namedview->guides) {
187         g_slist_free(namedview->guides);
188         namedview->guides = NULL;
189     }
191     // delete grids:
192     while ( namedview->grids ) {
193         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data;
194         delete gr;
195         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids);
196     }
198     if (((SPObjectClass *) parent_class)->release) {
199         ((SPObjectClass *) parent_class)->release(object);
200     }
202     namedview->snap_manager.~SnapManager();
205 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
207     SPNamedView *nv = SP_NAMEDVIEW(object);
208     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
210     switch (key) {
211     case SP_ATTR_VIEWONLY:
212             nv->editable = (!value);
213             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
214             break;
215     case SP_ATTR_SHOWGUIDES:
216             if (!value) { // show guides if not specified, for backwards compatibility
217                 nv->showguides = TRUE;
218             } else {
219                 nv->showguides = sp_str_to_bool(value);
220             }
221             sp_namedview_setup_guides(nv);
222             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
223             break;
224     case SP_ATTR_SHOWGRIDS:
225             if (!value) { // show grids if not specified, for backwards compatibility
226                 nv->grids_visible = false;
227             } else {
228                 nv->grids_visible = sp_str_to_bool(value);
229             }
230             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
231             break;
232     case SP_ATTR_GRIDTOLERANCE:
233             nv->gridtoleranceunit = &px;
234             nv->gridtolerance = DEFAULTTOLERANCE;
235             if (value) {
236                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
237             }
238             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
239             break;
240     case SP_ATTR_GUIDETOLERANCE:
241             nv->guidetoleranceunit = &px;
242             nv->guidetolerance = DEFAULTTOLERANCE;
243             if (value) {
244                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
245             }
246             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
247             break;
248     case SP_ATTR_OBJECTTOLERANCE:
249             nv->objecttoleranceunit = &px;
250             nv->objecttolerance = DEFAULTTOLERANCE;
251             if (value) {
252                 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
253             }
254             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
255             break;
256     case SP_ATTR_GUIDECOLOR:
257             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
258             if (value) {
259                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
260             }
261             for (GSList *l = nv->guides; l != NULL; l = l->next) {
262                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
263             }
264             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
265             break;
266     case SP_ATTR_GUIDEOPACITY:
267             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
268             sp_nv_read_opacity(value, &nv->guidecolor);
269             for (GSList *l = nv->guides; l != NULL; l = l->next) {
270                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
271             }
272             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
273             break;
274     case SP_ATTR_GUIDEHICOLOR:
275             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
276             if (value) {
277                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
278             }
279             for (GSList *l = nv->guides; l != NULL; l = l->next) {
280                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
281             }
282             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
283             break;
284     case SP_ATTR_GUIDEHIOPACITY:
285             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
286             sp_nv_read_opacity(value, &nv->guidehicolor);
287             for (GSList *l = nv->guides; l != NULL; l = l->next) {
288                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
289             }
290             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
291             break;
292     case SP_ATTR_SHOWBORDER:
293             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
294             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
295             break;
296     case SP_ATTR_BORDERLAYER:
297             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
298             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
299             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
300             break;
301     case SP_ATTR_BORDERCOLOR:
302             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
303             if (value) {
304                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
305             }
306             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
307             break;
308         case SP_ATTR_BORDEROPACITY:
309             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
310             sp_nv_read_opacity(value, &nv->bordercolor);
311             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
312             break;
313         case SP_ATTR_PAGECOLOR:
314             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
315             if (value) {
316                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
317             }
318             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
319             break;
320     case SP_ATTR_INKSCAPE_PAGEOPACITY:
321             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
322             sp_nv_read_opacity(value, &nv->pagecolor);
323             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
324             break;
325     case SP_ATTR_INKSCAPE_PAGESHADOW:
326             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
327             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
328             break;
329     case SP_ATTR_SHOWPAGESHADOW:
330             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
331             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
332             break;
333     case SP_ATTR_INKSCAPE_ZOOM:
334             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
335             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
336             break;
337     case SP_ATTR_INKSCAPE_CX:
338             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
339             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
340             break;
341     case SP_ATTR_INKSCAPE_CY:
342             nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
343             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
344             break;
345     case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
346             nv->window_width = value? atoi(value) : -1; // -1 means not set
347             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
348             break;
349     case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
350             nv->window_height = value ? atoi(value) : -1; // -1 means not set
351             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
352             break;
353     case SP_ATTR_INKSCAPE_WINDOW_X:
354             nv->window_x = value ? atoi(value) : -1; // -1 means not set
355             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
356             break;
357     case SP_ATTR_INKSCAPE_WINDOW_Y:
358             nv->window_y = value ? atoi(value) : -1; // -1 means not set
359             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
360             break;
361     case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
362             nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
363             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
364             break;
365     case SP_ATTR_INKSCAPE_SNAP_BBOX:
366                 nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
367             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
368             break;
369     case SP_ATTR_INKSCAPE_SNAP_NODES:
370             nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
371             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
372             break;
373     case SP_ATTR_INKSCAPE_SNAP_CENTER:
374             nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
375             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
376             break;
377     case SP_ATTR_INKSCAPE_SNAP_GUIDE:
378             nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
379             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
380             break;
381     case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
382             nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
383             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
384             break;
385     case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
386             nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
387             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
388             break;
389     case SP_ATTR_INKSCAPE_OBJECT_PATHS:
390             nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
391             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
392             break;
393     case SP_ATTR_INKSCAPE_OBJECT_NODES:
394             nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
395             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
396             break;
397     case SP_ATTR_INKSCAPE_BBOX_PATHS:
398             nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
399             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
400             break;
401     case SP_ATTR_INKSCAPE_BBOX_NODES:
402             nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);            
403             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
404             break;
405     case SP_ATTR_INKSCAPE_CURRENT_LAYER:
406             nv->default_layer_id = value ? g_quark_from_string(value) : 0;
407             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
408             break;
409     case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
410             nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
411                     defaultConnSpacing;
412             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
413             break;
414     case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
415             /* The default unit if the document doesn't override this: e.g. for files saved as
416              * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
417              * earlier.
418              *
419              * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
420              * about "not the same numbers as what's in the SVG file" (at least for documents
421              * without a viewBox attribute on the root <svg> element).  Similarly, it's also
422              * the most reliable unit (i.e. least likely to be wrong in different viewing
423              * conditions) for viewBox-less SVG files given that it's the unit that inkscape
424              * uses for all coordinates.
425              *
426              * For documents that do have a viewBox attribute on the root <svg> element, it
427              * might be better if we used either viewBox coordinates or if we used the unit of
428              * say the width attribute of the root <svg> element.  However, these pose problems
429              * in that they aren't in general absolute units as currently required by
430              * doc_units.
431              */
432             SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
434             if (value) {
435                 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
436                 if ( req_unit == NULL ) {
437                     g_warning("Unrecognized unit `%s'", value);
438                     /* fixme: Document errors should be reported in the status bar or
439                      * the like (e.g. as per
440                      * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
441                      * should be only for programmer errors. */
442                 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
443                             req_unit->base == SP_UNIT_DEVICE     ) {
444                     new_unit = req_unit;
445                 } else {
446                     g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
447                               value);
448                     /* fixme: Don't use g_log (see above). */
449                 }
450             }
451             nv->doc_units = new_unit;
452             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
453             break;
454     }
455     default:
456             if (((SPObjectClass *) (parent_class))->set) {
457                 ((SPObjectClass *) (parent_class))->set(object, key, value);
458             }
459             break;
460     }
463 /**
464 * 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,
465 * otherwise only add it to the specified desktop.
466 */
467 static Inkscape::CanvasGrid*
468 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
469     Inkscape::CanvasGrid* grid = NULL;
470     //check if namedview already has an object for this grid
471     for (GSList *l = nv->grids; l != NULL; l = l->next) {
472         Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
473         if (repr == g->repr) {
474             grid = g;
475             break;
476         }
477     }
479     if (!grid) {
480         //create grid object
481         Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
482         SPDocument *doc = NULL;
483         if (desktop)
484             doc = sp_desktop_document(desktop);
485         else
486             doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
487         grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
488         nv->grids = g_slist_append(nv->grids, grid);
489         //Initialize the snapping parameters for the new grid
490         bool enabled_node = nv->snap_manager.getSnapModeNode();
491         bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
492         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
493         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
494     }
496     if (!desktop) {
497         //add canvasitem to all desktops
498         for (GSList *l = nv->views; l != NULL; l = l->next) {
499             SPDesktop *dt = static_cast<SPDesktop*>(l->data);
500             grid->createCanvasItem(dt);
501         }
502     } else {
503         //add canvasitem only for specified desktop
504         grid->createCanvasItem(desktop);
505     }
507     return grid;
510 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
512     SPNamedView *nv = (SPNamedView *) object;
514     if (((SPObjectClass *) (parent_class))->child_added) {
515         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
516     }
518     if (!strcmp(child->name(), "inkscape:grid")) {
519         sp_namedview_add_grid(nv, child, NULL);
520     } else {
521         SPObject *no = object->document->getObjectByRepr(child);
522         if ( !SP_IS_OBJECT(no) )
523             return;
525         if (SP_IS_GUIDE(no)) {
526             SPGuide *g = (SPGuide *) no;
527             nv->guides = g_slist_prepend(nv->guides, g);
528             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
529             if (nv->editable) {
530                 for (GSList *l = nv->views; l != NULL; l = l->next) {
531                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
532                     if (static_cast<SPDesktop*>(l->data)->guides_active)
533                         sp_guide_sensitize(g,
534                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
535                                            TRUE);
536                     if (nv->showguides) {
537                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
538                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
539                         }
540                     } else {
541                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
542                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
543                         }
544                     }
545                 }
546             }
547         }
548     }
551 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
553     SPNamedView *nv = (SPNamedView *) object;
555     if (!strcmp(child->name(), "inkscape:grid")) {
556         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
557             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
558             if ( gr->repr == child ) {
559                 delete gr;
560                 nv->grids = g_slist_remove_link(nv->grids, iter);
561                 break;
562             }
563         }
564     } else {
565         GSList **ref = &nv->guides;
566         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
567             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
568                 *ref = iter->next;
569                 iter->next = NULL;
570                 g_slist_free_1(iter);
571                 break;
572             }
573             ref = &iter->next;
574         }
575     }
577     if (((SPObjectClass *) (parent_class))->remove_child) {
578         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
579     }
582 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
584     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
585          repr != SP_OBJECT_REPR(object) )
586     {
587         if (repr) {
588             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
589         } else {
590              /// \todo FIXME:  Plumb an appropriate XML::Document into this
591              repr = SP_OBJECT_REPR(object)->duplicate(NULL);
592         }
593     }
595     return repr;
598 void SPNamedView::show(SPDesktop *desktop)
600     for (GSList *l = guides; l != NULL; l = l->next) {
601         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
602         if (desktop->guides_active) {
603             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
604         }
605         if (showguides) {
606             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
607                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
608             }
609         } else {
610             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
611                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
612             }
613         }
614     }
616     views = g_slist_prepend(views, desktop);
618     // generate grids specified in SVG:
619     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
620     if (repr) {
621         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
622             if (!strcmp(child->name(), "inkscape:grid")) {
623                 sp_namedview_add_grid(this, child, desktop);
624             }
625         }
626     }
628     desktop->showGrids(grids_visible, false);
631 #define MIN_ONSCREEN_DISTANCE 50
633 /*
634  * Restores window geometry from the document settings or defaults in prefs
635  */
636 void sp_namedview_window_from_document(SPDesktop *desktop)
638     SPNamedView *nv = desktop->namedview;
639     gint geometry_from_file =
640         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
642     // restore window size and position stored with the document
643     if (geometry_from_file) {
644         gint w = MIN(gdk_screen_width(), nv->window_width);
645         gint h = MIN(gdk_screen_height(), nv->window_height);
646         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
647         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
648         if (w>0 && h>0 && x>0 && y>0) {
649             x = MIN(gdk_screen_width() - w, x);
650             y = MIN(gdk_screen_height() - h, y);
651         }
652         if (w>0 && h>0) {
653             desktop->setWindowSize(w, h);
654         }
655         if (x>0 && y>0) {
656             desktop->setWindowPosition(NR::Point(x, y));
657         }
658     }
660     // restore zoom and view
661     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
662         && nv->cx != HUGE_VAL && !isNaN(nv->cx)
663         && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
664         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
665     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
666         desktop->zoom_page();
667     }
669     // cancel any history of zooms up to this point
670     if (desktop->zooms_past) {
671         g_list_free(desktop->zooms_past);
672         desktop->zooms_past = NULL;
673     }
676 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
678     SPObject *layer = NULL;
679     SPDocument *document = desktop->doc();
680     SPNamedView *nv = desktop->namedview;
681     if ( nv->default_layer_id != 0 ) {
682         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
683     }
684     // don't use that object if it's not at least group
685     if ( !layer || !SP_IS_GROUP(layer) ) {
686         layer = NULL;
687     }
688     // if that didn't work out, look for the topmost layer
689     if (!layer) {
690         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
691         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
692             if (desktop->isLayer(iter)) {
693                 layer = iter;
694             }
695         }
696     }
697     if (layer) {
698         desktop->setCurrentLayer(layer);
699     }
701     // FIXME: find a better place to do this
702     desktop->event_log->updateUndoVerbs();
705 void sp_namedview_document_from_window(SPDesktop *desktop)
707     gint save_geometry_in_file =
708         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
709     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
710     NR::Rect const r = desktop->get_display_area();
712     // saving window geometry is not undoable
713     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
714     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
716     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
717     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
718     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
720     if (save_geometry_in_file) {
721         gint w, h, x, y;
722         desktop->getWindowGeometry(x, y, w, h);
723         sp_repr_set_int(view, "inkscape:window-width", w);
724         sp_repr_set_int(view, "inkscape:window-height", h);
725         sp_repr_set_int(view, "inkscape:window-x", x);
726         sp_repr_set_int(view, "inkscape:window-y", y);
727     }
729     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
731     // restore undoability
732     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
735 void SPNamedView::hide(SPDesktop const *desktop)
737     g_assert(desktop != NULL);
738     g_assert(g_slist_find(views, desktop));
740     for (GSList *l = guides; l != NULL; l = l->next) {
741         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
742     }
744     views = g_slist_remove(views, desktop);
746     // delete grids:
747     while ( grids ) {
748         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)grids->data;
749         delete gr;
750         grids = g_slist_remove_link(grids, grids);
751     }
754 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
756     g_assert(desktop != NULL);
757     g_assert(g_slist_find(views, desktop));
759     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
761     for (GSList *l = guides; l != NULL; l = l->next) {
762         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
763     }
766 static void sp_namedview_setup_guides(SPNamedView *nv)
768     for (GSList *l = nv->guides; l != NULL; l = l->next) {
769         if (nv->showguides) {
770             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
771                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
772             }
773         } else {
774             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
775                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
776             }
777         }
778     }
781 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
783     unsigned int v;
784     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
785     if (!set) { // hide guides if not specified, for backwards compatibility
786         v = FALSE;
787     } else {
788         v = !v;
789     }
791     bool saved = sp_document_get_undo_sensitive(doc);
792     sp_document_set_undo_sensitive(doc, false);
794     sp_repr_set_boolean(repr, "showguides", v);
796     doc->rroot->setAttribute("sodipodi:modified", "true");
797     sp_document_set_undo_sensitive(doc, saved);
800 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
802     namedview->grids_visible = show;
804     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
805     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
807     bool saved = sp_document_get_undo_sensitive(doc);
808     sp_document_set_undo_sensitive(doc, false);
810     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
812     /* we don't want the document to get dirty on startup; that's when
813        we call this function with dirty_document = false */
814     if (dirty_document) {
815         doc->rroot->setAttribute("sodipodi:modified", "true");
816     }
817     sp_document_set_undo_sensitive(doc, saved);
820 gchar const *SPNamedView::getName() const
822     SPException ex;
823     SP_EXCEPTION_INIT(&ex);
824     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
827 guint SPNamedView::getViewCount()
829     return ++viewcount;
832 GSList const *SPNamedView::getViewList() const
834     return views;
837 /* This should be moved somewhere */
839 static gboolean sp_str_to_bool(const gchar *str)
841     if (str) {
842         if (!g_strcasecmp(str, "true") ||
843             !g_strcasecmp(str, "yes") ||
844             !g_strcasecmp(str, "y") ||
845             (atoi(str) != 0)) {
846             return TRUE;
847         }
848     }
850     return FALSE;
853 /* fixme: Collect all these length parsing methods and think common sane API */
855 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
857     if (!str) {
858         return FALSE;
859     }
861     gchar *u;
862     gdouble v = g_ascii_strtod(str, &u);
863     if (!u) {
864         return FALSE;
865     }
866     while (isspace(*u)) {
867         u += 1;
868     }
870     if (!*u) {
871         /* No unit specified - keep default */
872         *val = v;
873         return TRUE;
874     }
876     if (base & SP_UNIT_DEVICE) {
877         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
878             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
879             *val = v;
880             return TRUE;
881         }
882     }
884     if (base & SP_UNIT_ABSOLUTE) {
885         if (!strncmp(u, "pt", 2)) {
886             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
887         } else if (!strncmp(u, "mm", 2)) {
888             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
889         } else if (!strncmp(u, "cm", 2)) {
890             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
891         } else if (!strncmp(u, "m", 1)) {
892             *unit = &sp_unit_get_by_id(SP_UNIT_M);
893         } else if (!strncmp(u, "in", 2)) {
894             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
895         } else {
896             return FALSE;
897         }
898         *val = v;
899         return TRUE;
900     }
902     return FALSE;
905 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
907     if (!str) {
908         return FALSE;
909     }
911     gchar *u;
912     gdouble v = g_ascii_strtod(str, &u);
913     if (!u) {
914         return FALSE;
915     }
916     v = CLAMP(v, 0.0, 1.0);
918     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
920     return TRUE;
923 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
925     g_return_val_if_fail(document != NULL, NULL);
927     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
928     g_assert(nv != NULL);
930     if (id == NULL) {
931         return (SPNamedView *) nv;
932     }
934     while (nv && strcmp(nv->id, id)) {
935         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
936     }
938     return (SPNamedView *) nv;
941 /**
942  * Returns namedview's default metric.
943  */
944 SPMetric SPNamedView::getDefaultMetric() const
946     if (doc_units) {
947         return sp_unit_get_metric(doc_units);
948     } else {
949         return SP_PT;
950     }
953 /**
954  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
955  */
956 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
958     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
959         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
960         if (grid->isEnabled())
961             return grid;
962     }
964     return NULL;
968 /*
969   Local Variables:
970   mode:c++
971   c-file-style:"stroustrup"
972   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
973   indent-tabs-mode:nil
974   fill-column:99
975   End:
976 */
977 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :