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