Code

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