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