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