Code

reenable buil inkview on windows
[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-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) : TRUE);
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     if (!strcmp(child->name(), "inkscape:grid")) {
514         sp_namedview_add_grid(nv, child, NULL);
515     } else {
516         SPObject *no = object->document->getObjectByRepr(child);
517         if ( !SP_IS_OBJECT(no) )
518             return;
520         if (SP_IS_GUIDE(no)) {
521             SPGuide *g = (SPGuide *) no;
522             nv->guides = g_slist_prepend(nv->guides, g);
523             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
524             if (nv->editable) {
525                 for (GSList *l = nv->views; l != NULL; l = l->next) {
526                     sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
527                     if (static_cast<SPDesktop*>(l->data)->guides_active)
528                         sp_guide_sensitize(g,
529                                            sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
530                                            TRUE);
531                     if (nv->showguides) {
532                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
533                             sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
534                         }
535                     } else {
536                         for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
537                             sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
538                         }
539                     }
540                 }
541             }
542         }
543     }
546 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
548     SPNamedView *nv = (SPNamedView *) object;
550     if (!strcmp(child->name(), "inkscape:grid")) {
551         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
552             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
553             if ( gr->repr == child ) {
554                 delete gr;
555                 nv->grids = g_slist_remove_link(nv->grids, iter);
556                 break;
557             }
558         }
559     } else {
560         GSList **ref = &nv->guides;
561         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
562             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
563                 *ref = iter->next;
564                 iter->next = NULL;
565                 g_slist_free_1(iter);
566                 break;
567             }
568             ref = &iter->next;
569         }
570     }
572     if (((SPObjectClass *) (parent_class))->remove_child) {
573         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
574     }
577 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
579     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
580          repr != SP_OBJECT_REPR(object) )
581     {
582         if (repr) {
583             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
584         } else {
585              /// \todo FIXME:  Plumb an appropriate XML::Document into this
586              repr = SP_OBJECT_REPR(object)->duplicate(NULL);
587         }
588     }
590     return repr;
593 void SPNamedView::show(SPDesktop *desktop)
595     for (GSList *l = guides; l != NULL; l = l->next) {
596         sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
597         if (desktop->guides_active) {
598             sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
599         }
600         if (showguides) {
601             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
602                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
603             }
604         } else {
605             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
606                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
607             }
608         }
609     }
611     views = g_slist_prepend(views, desktop);
613     // generate grids specified in SVG:
614     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
615     if (repr) {
616         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
617             if (!strcmp(child->name(), "inkscape:grid")) {
618                 sp_namedview_add_grid(this, child, desktop);
619             }
620         }
621     }
623     desktop->showGrids(grids_visible, false);
626 #define MIN_ONSCREEN_DISTANCE 50
628 /*
629  * Restores window geometry from the document settings or defaults in prefs
630  */
631 void sp_namedview_window_from_document(SPDesktop *desktop)
633     SPNamedView *nv = desktop->namedview;
634     gint geometry_from_file =
635         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
637     // restore window size and position stored with the document
638     if (geometry_from_file) {
639         gint w = MIN(gdk_screen_width(), nv->window_width);
640         gint h = MIN(gdk_screen_height(), nv->window_height);
641         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
642         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
643         if (w>0 && h>0 && x>0 && y>0) {
644             x = MIN(gdk_screen_width() - w, x);
645             y = MIN(gdk_screen_height() - h, y);
646         }
647         if (w>0 && h>0) {
648             desktop->setWindowSize(w, h);
649         }
650         if (x>0 && y>0) {
651             desktop->setWindowPosition(NR::Point(x, y));
652         }
653     }
655     // restore zoom and view
656     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
657         && nv->cx != HUGE_VAL && !isNaN(nv->cx)
658         && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
659         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
660     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
661         desktop->zoom_page();
662     }
664     // cancel any history of zooms up to this point
665     if (desktop->zooms_past) {
666         g_list_free(desktop->zooms_past);
667         desktop->zooms_past = NULL;
668     }
671 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
673     SPObject *layer = NULL;
674     SPDocument *document = desktop->doc();
675     SPNamedView *nv = desktop->namedview;
676     if ( nv->default_layer_id != 0 ) {
677         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
678     }
679     // don't use that object if it's not at least group
680     if ( !layer || !SP_IS_GROUP(layer) ) {
681         layer = NULL;
682     }
683     // if that didn't work out, look for the topmost layer
684     if (!layer) {
685         SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
686         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
687             if (desktop->isLayer(iter)) {
688                 layer = iter;
689             }
690         }
691     }
692     if (layer) {
693         desktop->setCurrentLayer(layer);
694     }
696     // FIXME: find a better place to do this
697     desktop->event_log->updateUndoVerbs();
700 void sp_namedview_document_from_window(SPDesktop *desktop)
702     gint save_geometry_in_file =
703         (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
704     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
705     NR::Rect const r = desktop->get_display_area();
707     // saving window geometry is not undoable
708     bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
709     sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
711     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
712     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
713     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
715     if (save_geometry_in_file) {
716         gint w, h, x, y;
717         desktop->getWindowGeometry(x, y, w, h);
718         sp_repr_set_int(view, "inkscape:window-width", w);
719         sp_repr_set_int(view, "inkscape:window-height", h);
720         sp_repr_set_int(view, "inkscape:window-x", x);
721         sp_repr_set_int(view, "inkscape:window-y", y);
722     }
724     view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
726     // restore undoability
727     sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
730 void SPNamedView::hide(SPDesktop const *desktop)
732     g_assert(desktop != NULL);
733     g_assert(g_slist_find(views, desktop));
735     for (GSList *l = guides; l != NULL; l = l->next) {
736         sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
737     }
739     views = g_slist_remove(views, desktop);
741     // delete grids:
742     while ( grids ) {
743         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)grids->data;
744         delete gr;
745         grids = g_slist_remove_link(grids, grids);
746     }
749 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
751     g_assert(desktop != NULL);
752     g_assert(g_slist_find(views, desktop));
754     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
756     for (GSList *l = guides; l != NULL; l = l->next) {
757         sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
758     }
761 static void sp_namedview_setup_guides(SPNamedView *nv)
763     for (GSList *l = nv->guides; l != NULL; l = l->next) {
764         if (nv->showguides) {
765             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
766                 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
767             }
768         } else {
769             for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
770                 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
771             }
772         }
773     }
776 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
778     unsigned int v;
779     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
780     if (!set) { // hide guides if not specified, for backwards compatibility
781         v = FALSE;
782     } else {
783         v = !v;
784     }
786     bool saved = sp_document_get_undo_sensitive(doc);
787     sp_document_set_undo_sensitive(doc, false);
789     sp_repr_set_boolean(repr, "showguides", v);
791     doc->rroot->setAttribute("sodipodi:modified", "true");
792     sp_document_set_undo_sensitive(doc, saved);
795 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
797     namedview->grids_visible = show;
799     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
800     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
802     bool saved = sp_document_get_undo_sensitive(doc);
803     sp_document_set_undo_sensitive(doc, false);
805     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
807     /* we don't want the document to get dirty on startup; that's when
808        we call this function with dirty_document = false */
809     if (dirty_document) {
810         doc->rroot->setAttribute("sodipodi:modified", "true");
811     }
812     sp_document_set_undo_sensitive(doc, saved);
815 gchar const *SPNamedView::getName() const
817     SPException ex;
818     SP_EXCEPTION_INIT(&ex);
819     return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
822 guint SPNamedView::getViewCount()
824     return ++viewcount;
827 GSList const *SPNamedView::getViewList() const
829     return views;
832 /* This should be moved somewhere */
834 static gboolean sp_str_to_bool(const gchar *str)
836     if (str) {
837         if (!g_strcasecmp(str, "true") ||
838             !g_strcasecmp(str, "yes") ||
839             !g_strcasecmp(str, "y") ||
840             (atoi(str) != 0)) {
841             return TRUE;
842         }
843     }
845     return FALSE;
848 /* fixme: Collect all these length parsing methods and think common sane API */
850 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
852     if (!str) {
853         return FALSE;
854     }
856     gchar *u;
857     gdouble v = g_ascii_strtod(str, &u);
858     if (!u) {
859         return FALSE;
860     }
861     while (isspace(*u)) {
862         u += 1;
863     }
865     if (!*u) {
866         /* No unit specified - keep default */
867         *val = v;
868         return TRUE;
869     }
871     if (base & SP_UNIT_DEVICE) {
872         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
873             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
874             *val = v;
875             return TRUE;
876         }
877     }
879     if (base & SP_UNIT_ABSOLUTE) {
880         if (!strncmp(u, "pt", 2)) {
881             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
882         } else if (!strncmp(u, "mm", 2)) {
883             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
884         } else if (!strncmp(u, "cm", 2)) {
885             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
886         } else if (!strncmp(u, "m", 1)) {
887             *unit = &sp_unit_get_by_id(SP_UNIT_M);
888         } else if (!strncmp(u, "in", 2)) {
889             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
890         } else {
891             return FALSE;
892         }
893         *val = v;
894         return TRUE;
895     }
897     return FALSE;
900 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
902     if (!str) {
903         return FALSE;
904     }
906     gchar *u;
907     gdouble v = g_ascii_strtod(str, &u);
908     if (!u) {
909         return FALSE;
910     }
911     v = CLAMP(v, 0.0, 1.0);
913     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
915     return TRUE;
918 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
920     g_return_val_if_fail(document != NULL, NULL);
922     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
923     g_assert(nv != NULL);
925     if (id == NULL) {
926         return (SPNamedView *) nv;
927     }
929     while (nv && strcmp(nv->id, id)) {
930         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
931     }
933     return (SPNamedView *) nv;
936 /**
937  * Returns namedview's default metric.
938  */
939 SPMetric SPNamedView::getDefaultMetric() const
941     if (doc_units) {
942         return sp_unit_get_metric(doc_units);
943     } else {
944         return SP_PT;
945     }
949 /*
950   Local Variables:
951   mode:c++
952   c-file-style:"stroustrup"
953   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
954   indent-tabs-mode:nil
955   fill-column:99
956   End:
957 */
958 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :