Code

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