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;
102 }
104 static void sp_namedview_init(SPNamedView *nv)
105 {
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);
120 }
122 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
123 {
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, "gridtolerance");
135 sp_object_read_attr(object, "guidetolerance");
136 sp_object_read_attr(object, "objecttolerance");
137 sp_object_read_attr(object, "guidecolor");
138 sp_object_read_attr(object, "guideopacity");
139 sp_object_read_attr(object, "guidehicolor");
140 sp_object_read_attr(object, "guidehiopacity");
141 sp_object_read_attr(object, "showborder");
142 sp_object_read_attr(object, "inkscape:showpageshadow");
143 sp_object_read_attr(object, "borderlayer");
144 sp_object_read_attr(object, "bordercolor");
145 sp_object_read_attr(object, "borderopacity");
146 sp_object_read_attr(object, "pagecolor");
147 sp_object_read_attr(object, "inkscape:pageopacity");
148 sp_object_read_attr(object, "inkscape:pageshadow");
149 sp_object_read_attr(object, "inkscape:zoom");
150 sp_object_read_attr(object, "inkscape:cx");
151 sp_object_read_attr(object, "inkscape:cy");
152 sp_object_read_attr(object, "inkscape:window-width");
153 sp_object_read_attr(object, "inkscape:window-height");
154 sp_object_read_attr(object, "inkscape:window-x");
155 sp_object_read_attr(object, "inkscape:window-y");
156 /* sp_object_read_attr(object, "inkscape:grid-bbox");
157 sp_object_read_attr(object, "inkscape:guide-bbox");
158 sp_object_read_attr(object, "inkscape:object-bbox");
159 sp_object_read_attr(object, "inkscape:grid-points");
160 sp_object_read_attr(object, "inkscape:guide-points");
161 sp_object_read_attr(object, "inkscape:object-points");*/
162 sp_object_read_attr(object, "inkscape:object-paths");
163 sp_object_read_attr(object, "inkscape:object-nodes");
164 sp_object_read_attr(object, "inkscape:current-layer");
165 sp_object_read_attr(object, "inkscape:connector-spacing");
167 /* Construct guideline list */
169 for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
170 if (SP_IS_GUIDE(o)) {
171 SPGuide * g = SP_GUIDE(o);
172 nv->guides = g_slist_prepend(nv->guides, g);
173 g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
174 }
175 }
176 }
178 static void sp_namedview_release(SPObject *object)
179 {
180 SPNamedView *namedview = (SPNamedView *) object;
182 if (namedview->guides) {
183 g_slist_free(namedview->guides);
184 namedview->guides = NULL;
185 }
187 // delete grids:
188 while ( namedview->grids ) {
189 Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data;
190 delete gr;
191 namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids);
192 }
194 if (((SPObjectClass *) parent_class)->release) {
195 ((SPObjectClass *) parent_class)->release(object);
196 }
198 namedview->snap_manager.~SnapManager();
199 }
201 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
202 {
203 SPNamedView *nv = SP_NAMEDVIEW(object);
204 SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
206 switch (key) {
207 case SP_ATTR_VIEWONLY:
208 nv->editable = (!value);
209 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
210 break;
211 case SP_ATTR_SHOWGUIDES:
212 if (!value) { // show guides if not specified, for backwards compatibility
213 nv->showguides = TRUE;
214 } else {
215 nv->showguides = sp_str_to_bool(value);
216 }
217 sp_namedview_setup_guides(nv);
218 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
219 break;
220 case SP_ATTR_GRIDTOLERANCE:
221 nv->gridtoleranceunit = &px;
222 nv->gridtolerance = DEFAULTTOLERANCE;
223 if (value) {
224 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
225 }
226 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
227 break;
228 case SP_ATTR_GUIDETOLERANCE:
229 nv->guidetoleranceunit = &px;
230 nv->guidetolerance = DEFAULTTOLERANCE;
231 if (value) {
232 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
233 }
234 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
235 break;
236 case SP_ATTR_OBJECTTOLERANCE:
237 nv->objecttoleranceunit = &px;
238 nv->objecttolerance = DEFAULTTOLERANCE;
239 if (value) {
240 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
241 }
242 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
243 break;
244 case SP_ATTR_GUIDECOLOR:
245 nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
246 if (value) {
247 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
248 }
249 for (GSList *l = nv->guides; l != NULL; l = l->next) {
250 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
251 }
252 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
253 break;
254 case SP_ATTR_GUIDEOPACITY:
255 nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
256 sp_nv_read_opacity(value, &nv->guidecolor);
257 for (GSList *l = nv->guides; l != NULL; l = l->next) {
258 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
259 }
260 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
261 break;
262 case SP_ATTR_GUIDEHICOLOR:
263 nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
264 if (value) {
265 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
266 }
267 for (GSList *l = nv->guides; l != NULL; l = l->next) {
268 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
269 }
270 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
271 break;
272 case SP_ATTR_GUIDEHIOPACITY:
273 nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
274 sp_nv_read_opacity(value, &nv->guidehicolor);
275 for (GSList *l = nv->guides; l != NULL; l = l->next) {
276 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
277 }
278 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
279 break;
280 case SP_ATTR_SHOWBORDER:
281 nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
282 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
283 break;
284 case SP_ATTR_BORDERLAYER:
285 nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
286 if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
287 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
288 break;
289 case SP_ATTR_BORDERCOLOR:
290 nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
291 if (value) {
292 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
293 }
294 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
295 break;
296 case SP_ATTR_BORDEROPACITY:
297 nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
298 sp_nv_read_opacity(value, &nv->bordercolor);
299 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
300 break;
301 case SP_ATTR_PAGECOLOR:
302 nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
303 if (value) {
304 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
305 }
306 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
307 break;
308 case SP_ATTR_INKSCAPE_PAGEOPACITY:
309 nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
310 sp_nv_read_opacity(value, &nv->pagecolor);
311 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
312 break;
313 case SP_ATTR_INKSCAPE_PAGESHADOW:
314 nv->pageshadow = value? atoi(value) : 2; // 2 is the default
315 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
316 break;
317 case SP_ATTR_SHOWPAGESHADOW:
318 nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
319 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
320 break;
321 case SP_ATTR_INKSCAPE_ZOOM:
322 nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
323 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
324 break;
325 case SP_ATTR_INKSCAPE_CX:
326 nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
327 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
328 break;
329 case SP_ATTR_INKSCAPE_CY:
330 nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
331 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
332 break;
333 case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
334 nv->window_width = value? atoi(value) : -1; // -1 means not set
335 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
336 break;
337 case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
338 nv->window_height = value ? atoi(value) : -1; // -1 means not set
339 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
340 break;
341 case SP_ATTR_INKSCAPE_WINDOW_X:
342 nv->window_x = value ? atoi(value) : -1; // -1 means not set
343 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
344 break;
345 case SP_ATTR_INKSCAPE_WINDOW_Y:
346 nv->window_y = value ? atoi(value) : -1; // -1 means not set
347 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
348 break;
349 /* case SP_ATTR_INKSCAPE_GRID_BBOX:
350 nv->snap_manager.grid.setSnapTo(Inkscape::Snapper::BBOX_POINT, value ? sp_str_to_bool(value) : TRUE);
351 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
352 break;
353 case SP_ATTR_INKSCAPE_GRID_POINTS:
354 nv->snap_manager.grid.setSnapTo(Inkscape::Snapper::SNAP_POINT, value ? sp_str_to_bool(value) : FALSE);
355 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
356 break;
357 case SP_ATTR_INKSCAPE_GUIDE_BBOX:
358 nv->snap_manager.guide.setSnapTo(Inkscape::Snapper::BBOX_POINT, value ? sp_str_to_bool(value) : TRUE);
359 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
360 break;
361 case SP_ATTR_INKSCAPE_GUIDE_POINTS:
362 nv->snap_manager.guide.setSnapTo(Inkscape::Snapper::SNAP_POINT, value ? sp_str_to_bool(value) : FALSE);
363 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
364 break;
365 case SP_ATTR_INKSCAPE_OBJECT_BBOX:
366 nv->snap_manager.object.setSnapTo(Inkscape::Snapper::BBOX_POINT, (value) ? sp_str_to_bool(value) : FALSE);
367 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
368 break;
369 case SP_ATTR_INKSCAPE_OBJECT_POINTS:
370 nv->snap_manager.object.setSnapTo(Inkscape::Snapper::SNAP_POINT, (value) ? sp_str_to_bool(value) : FALSE);
371 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
372 break;
373 */
374 case SP_ATTR_INKSCAPE_OBJECT_PATHS:
375 nv->snap_manager.object.setSnapToPaths(value ? sp_str_to_bool(value) : TRUE);
376 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
377 break;
378 case SP_ATTR_INKSCAPE_OBJECT_NODES:
379 nv->snap_manager.object.setSnapToNodes(value ? sp_str_to_bool(value) : TRUE);
380 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
381 break;
382 case SP_ATTR_INKSCAPE_CURRENT_LAYER:
383 nv->default_layer_id = value ? g_quark_from_string(value) : 0;
384 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
385 break;
386 case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
387 nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
388 defaultConnSpacing;
389 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
390 break;
391 case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
392 /* The default unit if the document doesn't override this: e.g. for files saved as
393 * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
394 * earlier.
395 *
396 * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
397 * about "not the same numbers as what's in the SVG file" (at least for documents
398 * without a viewBox attribute on the root <svg> element). Similarly, it's also
399 * the most reliable unit (i.e. least likely to be wrong in different viewing
400 * conditions) for viewBox-less SVG files given that it's the unit that inkscape
401 * uses for all coordinates.
402 *
403 * For documents that do have a viewBox attribute on the root <svg> element, it
404 * might be better if we used either viewBox coordinates or if we used the unit of
405 * say the width attribute of the root <svg> element. However, these pose problems
406 * in that they aren't in general absolute units as currently required by
407 * doc_units.
408 */
409 SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
411 if (value) {
412 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
413 if ( req_unit == NULL ) {
414 g_warning("Unrecognized unit `%s'", value);
415 /* fixme: Document errors should be reported in the status bar or
416 * the like (e.g. as per
417 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
418 * should be only for programmer errors. */
419 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
420 req_unit->base == SP_UNIT_DEVICE ) {
421 new_unit = req_unit;
422 } else {
423 g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
424 value);
425 /* fixme: Don't use g_log (see above). */
426 }
427 }
428 nv->doc_units = new_unit;
429 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
430 break;
431 }
432 default:
433 if (((SPObjectClass *) (parent_class))->set) {
434 ((SPObjectClass *) (parent_class))->set(object, key, value);
435 }
436 break;
437 }
438 }
440 /**
441 * 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,
442 * otherwise only add it to the specified desktop.
443 */
444 static Inkscape::CanvasGrid*
445 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
446 Inkscape::CanvasGrid* grid = NULL;
447 //check if namedview already has an object for this grid
448 for (GSList *l = nv->grids; l != NULL; l = l->next) {
449 Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
450 if (repr == g->repr) {
451 grid = g;
452 break;
453 }
454 }
456 if (!grid) {
457 //create grid object
458 const char * gridtype = repr->attribute("type");
459 if (!gridtype) {
460 gridtype = "xygrid"; // use this as default gridtype when none is specified
461 repr->setAttribute("type", gridtype);
462 }
463 grid = Inkscape::CanvasGrid::NewGrid(nv, repr, gridtype);
464 nv->grids = g_slist_append(nv->grids, grid);
465 }
467 if (!desktop) {
468 //add canvasitem to all desktops
469 for (GSList *l = nv->views; l != NULL; l = l->next) {
470 SPDesktop *dt = static_cast<SPDesktop*>(l->data);
471 grid->createCanvasItem(dt);
472 }
473 } else {
474 //add canvasitem only for specified desktop
475 grid->createCanvasItem(desktop);
476 }
478 return grid;
479 }
481 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
482 {
483 SPNamedView *nv = (SPNamedView *) object;
485 if (((SPObjectClass *) (parent_class))->child_added) {
486 (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
487 }
489 const gchar *id = child->attribute("id");
490 if (!strcmp(child->name(), "inkscape:grid")) {
491 sp_namedview_add_grid(nv, child, NULL);
492 } else {
493 SPObject *no = object->document->getObjectById(id);
494 g_assert(SP_IS_OBJECT(no));
496 if (SP_IS_GUIDE(no)) {
497 SPGuide *g = (SPGuide *) no;
498 nv->guides = g_slist_prepend(nv->guides, g);
499 g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
500 if (nv->editable) {
501 for (GSList *l = nv->views; l != NULL; l = l->next) {
502 sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
503 if (static_cast<SPDesktop*>(l->data)->guides_active)
504 sp_guide_sensitize(g,
505 sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
506 TRUE);
507 if (nv->showguides) {
508 for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
509 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
510 }
511 } else {
512 for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
513 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
514 }
515 }
516 }
517 }
518 }
519 }
520 }
522 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
523 {
524 SPNamedView *nv = (SPNamedView *) object;
526 if (!strcmp(child->name(), "inkscape:grid")) {
527 for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
528 Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
529 if ( gr->repr == child ) {
530 delete gr;
531 nv->grids = g_slist_remove_link(nv->grids, iter);
532 break;
533 }
534 }
535 } else {
536 GSList **ref = &nv->guides;
537 for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
538 if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
539 *ref = iter->next;
540 iter->next = NULL;
541 g_slist_free_1(iter);
542 break;
543 }
544 ref = &iter->next;
545 }
546 }
548 if (((SPObjectClass *) (parent_class))->remove_child) {
549 (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
550 }
551 }
553 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
554 {
555 if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
556 repr != SP_OBJECT_REPR(object) )
557 {
558 if (repr) {
559 repr->mergeFrom(SP_OBJECT_REPR(object), "id");
560 } else {
561 /// \todo FIXME: Plumb an appropriate XML::Document into this
562 repr = SP_OBJECT_REPR(object)->duplicate(NULL);
563 }
564 }
566 return repr;
567 }
569 void SPNamedView::show(SPDesktop *desktop)
570 {
571 for (GSList *l = guides; l != NULL; l = l->next) {
572 sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
573 if (desktop->guides_active) {
574 sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
575 }
576 if (showguides) {
577 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
578 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
579 }
580 } else {
581 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
582 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
583 }
584 }
585 }
587 views = g_slist_prepend(views, desktop);
589 // generate grids specified in SVG:
590 Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
591 if (repr) {
592 for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
593 if (!strcmp(child->name(), "inkscape:grid")) {
594 sp_namedview_add_grid(this, child, desktop);
595 }
596 }
597 }
598 }
600 #define MIN_ONSCREEN_DISTANCE 50
602 /*
603 * Restores window geometry from the document settings
604 */
605 void sp_namedview_window_from_document(SPDesktop *desktop)
606 {
607 SPNamedView *nv = desktop->namedview;
608 gint save_geometry = prefs_get_int_attribute("options.savewindowgeometry", "value", 0);
610 // restore window size and position
611 if (save_geometry) {
612 gint w = MIN(gdk_screen_width(), nv->window_width);
613 gint h = MIN(gdk_screen_height(), nv->window_height);
614 gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
615 gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
616 if (w>0 && h>0 && x>0 && y>0) {
617 x = MIN(gdk_screen_width() - w, x);
618 y = MIN(gdk_screen_height() - h, y);
619 }
620 if (w>0 && h>0)
621 desktop->setWindowSize(w, h);
622 if (x>0 && y>0)
623 desktop->setWindowPosition(NR::Point(x, y));
624 }
626 // restore zoom and view
627 if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
628 && nv->cx != HUGE_VAL && !isNaN(nv->cx)
629 && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
630 desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
631 } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
632 desktop->zoom_page();
633 }
635 // cancel any history of zooms up to this point
636 if (desktop->zooms_past) {
637 g_list_free(desktop->zooms_past);
638 desktop->zooms_past = NULL;
639 }
640 }
642 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
643 {
644 SPObject *layer = NULL;
645 SPDocument *document = desktop->doc();
646 SPNamedView *nv = desktop->namedview;
647 if ( nv->default_layer_id != 0 ) {
648 layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
649 }
650 // don't use that object if it's not at least group
651 if ( !layer || !SP_IS_GROUP(layer) ) {
652 layer = NULL;
653 }
654 // if that didn't work out, look for the topmost layer
655 if (!layer) {
656 SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
657 for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
658 if (desktop->isLayer(iter)) {
659 layer = iter;
660 }
661 }
662 }
663 if (layer) {
664 desktop->setCurrentLayer(layer);
665 }
667 // FIXME: find a better place to do this
668 desktop->event_log->updateUndoVerbs();
669 }
671 void sp_namedview_document_from_window(SPDesktop *desktop)
672 {
673 gint save_geometry = prefs_get_int_attribute("options.savewindowgeometry", "value", 0);
674 Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
675 NR::Rect const r = desktop->get_display_area();
677 // saving window geometry is not undoable
678 bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
679 sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
681 sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
682 sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
683 sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
685 if (save_geometry) {
686 gint w, h, x, y;
687 desktop->getWindowGeometry(x, y, w, h);
688 sp_repr_set_int(view, "inkscape:window-width", w);
689 sp_repr_set_int(view, "inkscape:window-height", h);
690 sp_repr_set_int(view, "inkscape:window-x", x);
691 sp_repr_set_int(view, "inkscape:window-y", y);
692 }
694 view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
696 // restore undoability
697 sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
698 }
700 void SPNamedView::hide(SPDesktop const *desktop)
701 {
702 g_assert(desktop != NULL);
703 g_assert(g_slist_find(views, desktop));
705 for (GSList *l = guides; l != NULL; l = l->next) {
706 sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
707 }
709 views = g_slist_remove(views, desktop);
711 // delete grids:
712 while ( grids ) {
713 Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)grids->data;
714 delete gr;
715 grids = g_slist_remove_link(grids, grids);
716 }
717 }
719 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
720 {
721 g_assert(desktop != NULL);
722 g_assert(g_slist_find(views, desktop));
724 SPDesktop *dt = static_cast<SPDesktop*>(desktop);
726 for (GSList *l = guides; l != NULL; l = l->next) {
727 sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
728 }
729 }
731 static void sp_namedview_setup_guides(SPNamedView *nv)
732 {
733 for (GSList *l = nv->guides; l != NULL; l = l->next) {
734 if (nv->showguides) {
735 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
736 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
737 }
738 } else {
739 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
740 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
741 }
742 }
743 }
744 }
746 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
747 {
748 unsigned int v;
749 unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
750 if (!set) { // hide guides if not specified, for backwards compatibility
751 v = FALSE;
752 } else {
753 v = !v;
754 }
756 bool saved = sp_document_get_undo_sensitive(doc);
757 sp_document_set_undo_sensitive(doc, false);
759 sp_repr_set_boolean(repr, "showguides", v);
761 doc->rroot->setAttribute("sodipodi:modified", "true");
762 sp_document_set_undo_sensitive(doc, saved);
763 }
765 gchar const *SPNamedView::getName() const
766 {
767 SPException ex;
768 SP_EXCEPTION_INIT(&ex);
769 return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
770 }
772 guint SPNamedView::getViewCount()
773 {
774 return ++viewcount;
775 }
777 GSList const *SPNamedView::getViewList() const
778 {
779 return views;
780 }
782 /* This should be moved somewhere */
784 static gboolean sp_str_to_bool(const gchar *str)
785 {
786 if (str) {
787 if (!g_strcasecmp(str, "true") ||
788 !g_strcasecmp(str, "yes") ||
789 !g_strcasecmp(str, "y") ||
790 (atoi(str) != 0)) {
791 return TRUE;
792 }
793 }
795 return FALSE;
796 }
798 /* fixme: Collect all these length parsing methods and think common sane API */
800 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
801 {
802 if (!str) {
803 return FALSE;
804 }
806 gchar *u;
807 gdouble v = g_ascii_strtod(str, &u);
808 if (!u) {
809 return FALSE;
810 }
811 while (isspace(*u)) {
812 u += 1;
813 }
815 if (!*u) {
816 /* No unit specified - keep default */
817 *val = v;
818 return TRUE;
819 }
821 if (base & SP_UNIT_DEVICE) {
822 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
823 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
824 *val = v;
825 return TRUE;
826 }
827 }
829 if (base & SP_UNIT_ABSOLUTE) {
830 if (!strncmp(u, "pt", 2)) {
831 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
832 } else if (!strncmp(u, "mm", 2)) {
833 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
834 } else if (!strncmp(u, "cm", 2)) {
835 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
836 } else if (!strncmp(u, "m", 1)) {
837 *unit = &sp_unit_get_by_id(SP_UNIT_M);
838 } else if (!strncmp(u, "in", 2)) {
839 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
840 } else {
841 return FALSE;
842 }
843 *val = v;
844 return TRUE;
845 }
847 return FALSE;
848 }
850 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
851 {
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 v = CLAMP(v, 0.0, 1.0);
863 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
865 return TRUE;
866 }
868 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
869 {
870 g_return_val_if_fail(document != NULL, NULL);
872 SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
873 g_assert(nv != NULL);
875 if (id == NULL) {
876 return (SPNamedView *) nv;
877 }
879 while (nv && strcmp(nv->id, id)) {
880 nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
881 }
883 return (SPNamedView *) nv;
884 }
886 /**
887 * Returns namedview's default metric.
888 */
889 SPMetric SPNamedView::getDefaultMetric() const
890 {
891 if (doc_units) {
892 return sp_unit_get_metric(doc_units);
893 } else {
894 return SP_PT;
895 }
896 }
898 /*
899 Local Variables:
900 mode:c++
901 c-file-style:"stroustrup"
902 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
903 indent-tabs-mode:nil
904 fill-column:99
905 End:
906 */
907 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtab