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-2008 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"
18 #include <cstring>
19 #include <string>
21 #include "display/canvas-grid.h"
22 #include "helper/units.h"
23 #include "svg/svg-color.h"
24 #include "xml/repr.h"
25 #include "attributes.h"
26 #include "document.h"
27 #include "desktop-events.h"
28 #include "desktop-handles.h"
29 #include "event-log.h"
30 #include "sp-guide.h"
31 #include "sp-item-group.h"
32 #include "sp-namedview.h"
33 #include "prefs-utils.h"
34 #include "desktop.h"
35 #include "conn-avoid-ref.h" // for defaultConnSpacing.
37 #include "isnan.h" //temp fix for isnan(). include last
39 #define DEFAULTTOLERANCE 0.4
40 #define DEFAULTGRIDCOLOR 0x3f3fff25
41 #define DEFAULTGRIDEMPCOLOR 0x3f3fff60
42 #define DEFAULTGRIDEMPSPACING 5
43 #define DEFAULTGUIDECOLOR 0x0000ff7f
44 #define DEFAULTGUIDEHICOLOR 0xff00007f
45 #define DEFAULTBORDERCOLOR 0x000000ff
46 #define DEFAULTPAGECOLOR 0xffffff00
48 static void sp_namedview_class_init(SPNamedViewClass *klass);
49 static void sp_namedview_init(SPNamedView *namedview);
51 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
52 static void sp_namedview_release(SPObject *object);
53 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value);
54 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
55 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child);
56 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
58 static void sp_namedview_setup_guides(SPNamedView * nv);
60 static gboolean sp_str_to_bool(const gchar *str);
61 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit);
62 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color);
64 static SPObjectGroupClass * parent_class;
66 GType
67 sp_namedview_get_type()
68 {
69 static GType namedview_type = 0;
70 if (!namedview_type) {
71 GTypeInfo namedview_info = {
72 sizeof(SPNamedViewClass),
73 NULL, /* base_init */
74 NULL, /* base_finalize */
75 (GClassInitFunc) sp_namedview_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof(SPNamedView),
79 16, /* n_preallocs */
80 (GInstanceInitFunc) sp_namedview_init,
81 NULL, /* value_table */
82 };
83 namedview_type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPNamedView", &namedview_info, (GTypeFlags)0);
84 }
85 return namedview_type;
86 }
88 static void sp_namedview_class_init(SPNamedViewClass * klass)
89 {
90 GObjectClass * gobject_class;
91 SPObjectClass * sp_object_class;
93 gobject_class = (GObjectClass *) klass;
94 sp_object_class = (SPObjectClass *) klass;
96 parent_class = (SPObjectGroupClass*) g_type_class_ref(SP_TYPE_OBJECTGROUP);
98 sp_object_class->build = sp_namedview_build;
99 sp_object_class->release = sp_namedview_release;
100 sp_object_class->set = sp_namedview_set;
101 sp_object_class->child_added = sp_namedview_child_added;
102 sp_object_class->remove_child = sp_namedview_remove_child;
103 sp_object_class->write = sp_namedview_write;
104 }
106 static void sp_namedview_init(SPNamedView *nv)
107 {
108 nv->editable = TRUE;
109 nv->showguides = TRUE;
110 nv->grids_visible = false;
111 nv->showborder = TRUE;
112 nv->showpageshadow = TRUE;
114 nv->guides = NULL;
115 nv->viewcount = 0;
116 nv->grids = NULL;
117 nv->snapindicator = false;
119 nv->default_layer_id = 0;
121 nv->connector_spacing = defaultConnSpacing;
123 new (&nv->snap_manager) SnapManager(nv);
124 }
126 static void sp_namedview_generate_old_grid(SPNamedView * /*nv*/, SPDocument *document, Inkscape::XML::Node *repr) {
127 bool old_grid_settings_present = false;
129 // set old settings
130 const char* gridspacingx = "1px";
131 const char* gridspacingy = "1px";
132 const char* gridoriginy = "0px";
133 const char* gridoriginx = "0px";
134 const char* gridempspacing = "5";
135 const char* gridcolor = "#0000ff";
136 const char* gridempcolor = "#0000ff";
137 const char* gridopacity = "0.2";
138 const char* gridempopacity = "0.4";
140 const char* value = NULL;
141 if ((value = repr->attribute("gridoriginx"))) {
142 gridspacingx = value;
143 old_grid_settings_present = true;
144 }
145 if ((value = repr->attribute("gridoriginy"))) {
146 gridoriginy = value;
147 old_grid_settings_present = true;
148 }
149 if ((value = repr->attribute("gridspacingx"))) {
150 gridspacingx = value;
151 old_grid_settings_present = true;
152 }
153 if ((value = repr->attribute("gridspacingy"))) {
154 gridspacingy = value;
155 old_grid_settings_present = true;
156 }
157 if ((value = repr->attribute("gridcolor"))) {
158 gridcolor = value;
159 old_grid_settings_present = true;
160 }
161 if ((value = repr->attribute("gridempcolor"))) {
162 gridempcolor = value;
163 old_grid_settings_present = true;
164 }
165 if ((value = repr->attribute("gridempspacing"))) {
166 gridempspacing = value;
167 old_grid_settings_present = true;
168 }
169 if ((value = repr->attribute("gridopacity"))) {
170 gridopacity = value;
171 old_grid_settings_present = true;
172 }
173 if ((value = repr->attribute("gridempopacity"))) {
174 gridempopacity = value;
175 old_grid_settings_present = true;
176 }
178 if (old_grid_settings_present) {
179 // generate new xy grid with the correct settings
180 // first create the child xml node, then hook it to repr. This order is important, to not set off listeners to repr before the new node is complete.
182 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
183 Inkscape::XML::Node *newnode = xml_doc->createElement("inkscape:grid");
184 newnode->setAttribute("id", "GridFromPre046Settings");
185 newnode->setAttribute("type", Inkscape::CanvasGrid::getSVGName(Inkscape::GRID_RECTANGULAR));
186 newnode->setAttribute("originx", gridoriginx);
187 newnode->setAttribute("originy", gridoriginy);
188 newnode->setAttribute("spacingx", gridspacingx);
189 newnode->setAttribute("spacingy", gridspacingy);
190 newnode->setAttribute("color", gridcolor);
191 newnode->setAttribute("empcolor", gridempcolor);
192 newnode->setAttribute("opacity", gridopacity);
193 newnode->setAttribute("empopacity", gridempopacity);
194 newnode->setAttribute("empspacing", gridempspacing);
196 repr->appendChild(newnode);
197 Inkscape::GC::release(newnode);
199 // remove all old settings
200 repr->setAttribute("gridoriginx", NULL);
201 repr->setAttribute("gridoriginy", NULL);
202 repr->setAttribute("gridspacingx", NULL);
203 repr->setAttribute("gridspacingy", NULL);
204 repr->setAttribute("gridcolor", NULL);
205 repr->setAttribute("gridempcolor", NULL);
206 repr->setAttribute("gridopacity", NULL);
207 repr->setAttribute("gridempopacity", NULL);
208 repr->setAttribute("gridempspacing", NULL);
210 // sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid from pre0.46 grid settings"));
211 }
212 }
214 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
215 {
216 SPNamedView *nv = (SPNamedView *) object;
217 SPObjectGroup *og = (SPObjectGroup *) object;
219 if (((SPObjectClass *) (parent_class))->build) {
220 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
221 }
223 sp_object_read_attr(object, "inkscape:document-units");
224 sp_object_read_attr(object, "viewonly");
225 sp_object_read_attr(object, "showguides");
226 sp_object_read_attr(object, "showgrid");
227 sp_object_read_attr(object, "gridtolerance");
228 sp_object_read_attr(object, "guidetolerance");
229 sp_object_read_attr(object, "objecttolerance");
230 sp_object_read_attr(object, "guidecolor");
231 sp_object_read_attr(object, "guideopacity");
232 sp_object_read_attr(object, "guidehicolor");
233 sp_object_read_attr(object, "guidehiopacity");
234 sp_object_read_attr(object, "showborder");
235 sp_object_read_attr(object, "inkscape:showpageshadow");
236 sp_object_read_attr(object, "borderlayer");
237 sp_object_read_attr(object, "bordercolor");
238 sp_object_read_attr(object, "borderopacity");
239 sp_object_read_attr(object, "pagecolor");
240 sp_object_read_attr(object, "inkscape:pageopacity");
241 sp_object_read_attr(object, "inkscape:pageshadow");
242 sp_object_read_attr(object, "inkscape:zoom");
243 sp_object_read_attr(object, "inkscape:cx");
244 sp_object_read_attr(object, "inkscape:cy");
245 sp_object_read_attr(object, "inkscape:window-width");
246 sp_object_read_attr(object, "inkscape:window-height");
247 sp_object_read_attr(object, "inkscape:window-x");
248 sp_object_read_attr(object, "inkscape:window-y");
249 sp_object_read_attr(object, "inkscape:snap-global");
250 sp_object_read_attr(object, "inkscape:snap-indicator");
251 sp_object_read_attr(object, "inkscape:snap-bbox");
252 sp_object_read_attr(object, "inkscape:snap-nodes");
253 sp_object_read_attr(object, "inkscape:snap-guide");
254 sp_object_read_attr(object, "inkscape:snap-center");
255 sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
256 sp_object_read_attr(object, "inkscape:snap-intersection-line-segments");
257 sp_object_read_attr(object, "inkscape:object-paths");
258 sp_object_read_attr(object, "inkscape:object-nodes");
259 sp_object_read_attr(object, "inkscape:bbox-paths");
260 sp_object_read_attr(object, "inkscape:bbox-nodes");
261 sp_object_read_attr(object, "inkscape:snap-page");
262 sp_object_read_attr(object, "inkscape:current-layer");
263 sp_object_read_attr(object, "inkscape:connector-spacing");
265 /* Construct guideline list */
266 for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
267 if (SP_IS_GUIDE(o)) {
268 SPGuide * g = SP_GUIDE(o);
269 nv->guides = g_slist_prepend(nv->guides, g);
270 g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
271 }
272 }
274 // backwards compatibility with grid settings (pre 0.46)
275 sp_namedview_generate_old_grid(nv, document, repr);
276 }
278 static void sp_namedview_release(SPObject *object)
279 {
280 SPNamedView *namedview = (SPNamedView *) object;
282 if (namedview->guides) {
283 g_slist_free(namedview->guides);
284 namedview->guides = NULL;
285 }
287 // delete grids:
288 while ( namedview->grids ) {
289 Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
290 delete gr;
291 namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
292 }
294 if (((SPObjectClass *) parent_class)->release) {
295 ((SPObjectClass *) parent_class)->release(object);
296 }
298 namedview->snap_manager.~SnapManager();
299 }
301 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
302 {
303 SPNamedView *nv = SP_NAMEDVIEW(object);
304 SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
306 switch (key) {
307 case SP_ATTR_VIEWONLY:
308 nv->editable = (!value);
309 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
310 break;
311 case SP_ATTR_SHOWGUIDES:
312 if (!value) { // show guides if not specified, for backwards compatibility
313 nv->showguides = TRUE;
314 } else {
315 nv->showguides = sp_str_to_bool(value);
316 }
317 sp_namedview_setup_guides(nv);
318 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
319 break;
320 case SP_ATTR_SHOWGRIDS:
321 if (!value) { // don't show grids if not specified, for backwards compatibility
322 nv->grids_visible = false;
323 } else {
324 nv->grids_visible = sp_str_to_bool(value);
325 }
326 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
327 break;
328 case SP_ATTR_GRIDTOLERANCE:
329 nv->gridtoleranceunit = &px;
330 nv->gridtolerance = DEFAULTTOLERANCE;
331 if (value) {
332 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridtolerance, &nv->gridtoleranceunit);
333 }
334 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
335 break;
336 case SP_ATTR_GUIDETOLERANCE:
337 nv->guidetoleranceunit = &px;
338 nv->guidetolerance = DEFAULTTOLERANCE;
339 if (value) {
340 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->guidetolerance, &nv->guidetoleranceunit);
341 }
342 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
343 break;
344 case SP_ATTR_OBJECTTOLERANCE:
345 nv->objecttoleranceunit = &px;
346 nv->objecttolerance = DEFAULTTOLERANCE;
347 if (value) {
348 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->objecttolerance, &nv->objecttoleranceunit);
349 }
350 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
351 break;
352 case SP_ATTR_GUIDECOLOR:
353 nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
354 if (value) {
355 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
356 }
357 for (GSList *l = nv->guides; l != NULL; l = l->next) {
358 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
359 }
360 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
361 break;
362 case SP_ATTR_GUIDEOPACITY:
363 nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
364 sp_nv_read_opacity(value, &nv->guidecolor);
365 for (GSList *l = nv->guides; l != NULL; l = l->next) {
366 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
367 }
368 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
369 break;
370 case SP_ATTR_GUIDEHICOLOR:
371 nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
372 if (value) {
373 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
374 }
375 for (GSList *l = nv->guides; l != NULL; l = l->next) {
376 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
377 }
378 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
379 break;
380 case SP_ATTR_GUIDEHIOPACITY:
381 nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
382 sp_nv_read_opacity(value, &nv->guidehicolor);
383 for (GSList *l = nv->guides; l != NULL; l = l->next) {
384 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
385 }
386 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
387 break;
388 case SP_ATTR_SHOWBORDER:
389 nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
390 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
391 break;
392 case SP_ATTR_BORDERLAYER:
393 nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
394 if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
395 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
396 break;
397 case SP_ATTR_BORDERCOLOR:
398 nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
399 if (value) {
400 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
401 }
402 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
403 break;
404 case SP_ATTR_BORDEROPACITY:
405 nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
406 sp_nv_read_opacity(value, &nv->bordercolor);
407 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
408 break;
409 case SP_ATTR_PAGECOLOR:
410 nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
411 if (value) {
412 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
413 }
414 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
415 break;
416 case SP_ATTR_INKSCAPE_PAGEOPACITY:
417 nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
418 sp_nv_read_opacity(value, &nv->pagecolor);
419 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
420 break;
421 case SP_ATTR_INKSCAPE_PAGESHADOW:
422 nv->pageshadow = value? atoi(value) : 2; // 2 is the default
423 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
424 break;
425 case SP_ATTR_SHOWPAGESHADOW:
426 nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
427 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
428 break;
429 case SP_ATTR_INKSCAPE_ZOOM:
430 nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
431 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
432 break;
433 case SP_ATTR_INKSCAPE_CX:
434 nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
435 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
436 break;
437 case SP_ATTR_INKSCAPE_CY:
438 nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
439 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
440 break;
441 case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
442 nv->window_width = value? atoi(value) : -1; // -1 means not set
443 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
444 break;
445 case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
446 nv->window_height = value ? atoi(value) : -1; // -1 means not set
447 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
448 break;
449 case SP_ATTR_INKSCAPE_WINDOW_X:
450 nv->window_x = value ? atoi(value) : -1; // -1 means not set
451 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
452 break;
453 case SP_ATTR_INKSCAPE_WINDOW_Y:
454 nv->window_y = value ? atoi(value) : -1; // -1 means not set
455 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
456 break;
457 case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
458 nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
459 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
460 break;
461 case SP_ATTR_INKSCAPE_SNAP_INDICATOR:
462 nv->snapindicator = (value) ? sp_str_to_bool (value) : TRUE;
463 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
464 break;
465 case SP_ATTR_INKSCAPE_SNAP_BBOX:
466 nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
467 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
468 break;
469 case SP_ATTR_INKSCAPE_SNAP_NODES:
470 nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
471 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
472 break;
473 case SP_ATTR_INKSCAPE_SNAP_CENTER:
474 nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
475 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
476 break;
477 case SP_ATTR_INKSCAPE_SNAP_GUIDE:
478 nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
479 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
480 break;
481 case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
482 nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
483 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
484 break;
485 case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
486 nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
487 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
488 break;
489 case SP_ATTR_INKSCAPE_OBJECT_PATHS:
490 nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
491 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
492 break;
493 case SP_ATTR_INKSCAPE_OBJECT_NODES:
494 nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
495 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
496 break;
497 case SP_ATTR_INKSCAPE_BBOX_PATHS:
498 nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
499 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
500 break;
501 case SP_ATTR_INKSCAPE_BBOX_NODES:
502 nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);
503 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
504 break;
505 case SP_ATTR_INKSCAPE_SNAP_PAGE:
506 nv->snap_manager.object.setSnapToPageBorder(value ? sp_str_to_bool(value) : FALSE);
507 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
508 break;
509 case SP_ATTR_INKSCAPE_CURRENT_LAYER:
510 nv->default_layer_id = value ? g_quark_from_string(value) : 0;
511 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
512 break;
513 case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
514 nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
515 defaultConnSpacing;
516 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
517 break;
518 case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
519 /* The default unit if the document doesn't override this: e.g. for files saved as
520 * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
521 * earlier.
522 *
523 * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
524 * about "not the same numbers as what's in the SVG file" (at least for documents
525 * without a viewBox attribute on the root <svg> element). Similarly, it's also
526 * the most reliable unit (i.e. least likely to be wrong in different viewing
527 * conditions) for viewBox-less SVG files given that it's the unit that inkscape
528 * uses for all coordinates.
529 *
530 * For documents that do have a viewBox attribute on the root <svg> element, it
531 * might be better if we used either viewBox coordinates or if we used the unit of
532 * say the width attribute of the root <svg> element. However, these pose problems
533 * in that they aren't in general absolute units as currently required by
534 * doc_units.
535 */
536 SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
538 if (value) {
539 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
540 if ( req_unit == NULL ) {
541 g_warning("Unrecognized unit `%s'", value);
542 /* fixme: Document errors should be reported in the status bar or
543 * the like (e.g. as per
544 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
545 * should be only for programmer errors. */
546 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
547 req_unit->base == SP_UNIT_DEVICE ) {
548 new_unit = req_unit;
549 } else {
550 g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
551 value);
552 /* fixme: Don't use g_log (see above). */
553 }
554 }
555 nv->doc_units = new_unit;
556 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
557 break;
558 }
559 default:
560 if (((SPObjectClass *) (parent_class))->set) {
561 ((SPObjectClass *) (parent_class))->set(object, key, value);
562 }
563 break;
564 }
565 }
567 /**
568 * 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,
569 * otherwise only add it to the specified desktop.
570 */
571 static Inkscape::CanvasGrid*
572 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
573 Inkscape::CanvasGrid* grid = NULL;
574 //check if namedview already has an object for this grid
575 for (GSList *l = nv->grids; l != NULL; l = l->next) {
576 Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
577 if (repr == g->repr) {
578 grid = g;
579 break;
580 }
581 }
583 if (!grid) {
584 //create grid object
585 Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
586 SPDocument *doc = NULL;
587 if (desktop)
588 doc = sp_desktop_document(desktop);
589 else
590 doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
591 if (!doc) {
592 g_warning("sp_namedview_add_grid - how come doc is null here?!");
593 return NULL;
594 }
595 grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
596 nv->grids = g_slist_append(nv->grids, grid);
597 //Initialize the snapping parameters for the new grid
598 bool enabled_node = nv->snap_manager.getSnapModeNode();
599 bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
600 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
601 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
602 }
604 if (!desktop) {
605 //add canvasitem to all desktops
606 for (GSList *l = nv->views; l != NULL; l = l->next) {
607 SPDesktop *dt = static_cast<SPDesktop*>(l->data);
608 grid->createCanvasItem(dt);
609 }
610 } else {
611 //add canvasitem only for specified desktop
612 grid->createCanvasItem(desktop);
613 }
615 return grid;
616 }
618 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
619 {
620 SPNamedView *nv = (SPNamedView *) object;
622 if (((SPObjectClass *) (parent_class))->child_added) {
623 (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
624 }
626 if (!strcmp(child->name(), "inkscape:grid")) {
627 sp_namedview_add_grid(nv, child, NULL);
628 } else {
629 SPObject *no = object->document->getObjectByRepr(child);
630 if ( !SP_IS_OBJECT(no) )
631 return;
633 if (SP_IS_GUIDE(no)) {
634 SPGuide *g = (SPGuide *) no;
635 nv->guides = g_slist_prepend(nv->guides, g);
636 g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
637 if (nv->editable) {
638 for (GSList *l = nv->views; l != NULL; l = l->next) {
639 sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
640 if (static_cast<SPDesktop*>(l->data)->guides_active)
641 sp_guide_sensitize(g,
642 sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
643 TRUE);
644 if (nv->showguides) {
645 for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
646 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
647 }
648 } else {
649 for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
650 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
651 }
652 }
653 }
654 }
655 }
656 }
657 }
659 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
660 {
661 SPNamedView *nv = (SPNamedView *) object;
663 if (!strcmp(child->name(), "inkscape:grid")) {
664 for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
665 Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
666 if ( gr->repr == child ) {
667 delete gr;
668 nv->grids = g_slist_remove_link(nv->grids, iter);
669 break;
670 }
671 }
672 } else {
673 GSList **ref = &nv->guides;
674 for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
675 if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
676 *ref = iter->next;
677 iter->next = NULL;
678 g_slist_free_1(iter);
679 break;
680 }
681 ref = &iter->next;
682 }
683 }
685 if (((SPObjectClass *) (parent_class))->remove_child) {
686 (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
687 }
688 }
690 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
691 {
692 if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
693 repr != SP_OBJECT_REPR(object) )
694 {
695 if (repr) {
696 repr->mergeFrom(SP_OBJECT_REPR(object), "id");
697 } else {
698 repr = SP_OBJECT_REPR(object)->duplicate(doc);
699 }
700 }
702 return repr;
703 }
705 void SPNamedView::show(SPDesktop *desktop)
706 {
707 for (GSList *l = guides; l != NULL; l = l->next) {
708 sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
709 if (desktop->guides_active) {
710 sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
711 }
712 if (showguides) {
713 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
714 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
715 }
716 } else {
717 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
718 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
719 }
720 }
721 }
723 views = g_slist_prepend(views, desktop);
725 // generate grids specified in SVG:
726 Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
727 if (repr) {
728 for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
729 if (!strcmp(child->name(), "inkscape:grid")) {
730 sp_namedview_add_grid(this, child, desktop);
731 }
732 }
733 }
735 desktop->showGrids(grids_visible, false);
736 }
738 #define MIN_ONSCREEN_DISTANCE 50
740 /*
741 * Restores window geometry from the document settings or defaults in prefs
742 */
743 void sp_namedview_window_from_document(SPDesktop *desktop)
744 {
745 SPNamedView *nv = desktop->namedview;
746 gint geometry_from_file =
747 (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
749 // restore window size and position stored with the document
750 if (geometry_from_file) {
751 gint w = MIN(gdk_screen_width(), nv->window_width);
752 gint h = MIN(gdk_screen_height(), nv->window_height);
753 gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
754 gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
755 if (w>0 && h>0 && x>0 && y>0) {
756 x = MIN(gdk_screen_width() - w, x);
757 y = MIN(gdk_screen_height() - h, y);
758 }
759 if (w>0 && h>0) {
760 desktop->setWindowSize(w, h);
761 }
762 if (x>0 && y>0) {
763 desktop->setWindowPosition(NR::Point(x, y));
764 }
765 }
767 // restore zoom and view
768 if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !IS_NAN(nv->zoom)
769 && nv->cx != HUGE_VAL && !IS_NAN(nv->cx)
770 && nv->cy != HUGE_VAL && !IS_NAN(nv->cy)) {
771 desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
772 } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
773 desktop->zoom_page();
774 }
776 // cancel any history of zooms up to this point
777 if (desktop->zooms_past) {
778 g_list_free(desktop->zooms_past);
779 desktop->zooms_past = NULL;
780 }
781 }
783 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
784 {
785 SPObject *layer = NULL;
786 SPDocument *document = desktop->doc();
787 SPNamedView *nv = desktop->namedview;
788 if ( nv->default_layer_id != 0 ) {
789 layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
790 }
791 // don't use that object if it's not at least group
792 if ( !layer || !SP_IS_GROUP(layer) ) {
793 layer = NULL;
794 }
795 // if that didn't work out, look for the topmost layer
796 if (!layer) {
797 SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
798 for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
799 if (desktop->isLayer(iter)) {
800 layer = iter;
801 }
802 }
803 }
804 if (layer) {
805 desktop->setCurrentLayer(layer);
806 }
808 // FIXME: find a better place to do this
809 desktop->event_log->updateUndoVerbs();
810 }
812 void sp_namedview_document_from_window(SPDesktop *desktop)
813 {
814 gint save_geometry_in_file =
815 (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
816 Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
817 NR::Rect const r = desktop->get_display_area();
819 // saving window geometry is not undoable
820 bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
821 sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
823 sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
824 sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
825 sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
827 if (save_geometry_in_file) {
828 gint w, h, x, y;
829 desktop->getWindowGeometry(x, y, w, h);
830 sp_repr_set_int(view, "inkscape:window-width", w);
831 sp_repr_set_int(view, "inkscape:window-height", h);
832 sp_repr_set_int(view, "inkscape:window-x", x);
833 sp_repr_set_int(view, "inkscape:window-y", y);
834 }
836 view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
838 // restore undoability
839 sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
840 }
842 void SPNamedView::hide(SPDesktop const *desktop)
843 {
844 g_assert(desktop != NULL);
845 g_assert(g_slist_find(views, desktop));
847 for (GSList *l = guides; l != NULL; l = l->next) {
848 sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
849 }
851 views = g_slist_remove(views, desktop);
852 }
854 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
855 {
856 g_assert(desktop != NULL);
857 g_assert(g_slist_find(views, desktop));
859 SPDesktop *dt = static_cast<SPDesktop*>(desktop);
861 for (GSList *l = guides; l != NULL; l = l->next) {
862 sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
863 }
864 }
866 static void sp_namedview_setup_guides(SPNamedView *nv)
867 {
868 for (GSList *l = nv->guides; l != NULL; l = l->next) {
869 if (nv->showguides) {
870 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
871 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
872 }
873 } else {
874 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
875 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
876 }
877 }
878 }
879 }
881 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
882 {
883 unsigned int v;
884 unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
885 if (!set) { // hide guides if not specified, for backwards compatibility
886 v = FALSE;
887 } else {
888 v = !v;
889 }
891 bool saved = sp_document_get_undo_sensitive(doc);
892 sp_document_set_undo_sensitive(doc, false);
893 sp_repr_set_boolean(repr, "showguides", v);
894 sp_document_set_undo_sensitive(doc, saved);
896 doc->setModifiedSinceSave();
897 }
899 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
900 {
901 namedview->grids_visible = show;
903 SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
904 Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
906 bool saved = sp_document_get_undo_sensitive(doc);
907 sp_document_set_undo_sensitive(doc, false);
908 sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
909 sp_document_set_undo_sensitive(doc, saved);
911 /* we don't want the document to get dirty on startup; that's when
912 we call this function with dirty_document = false */
913 if (dirty_document) {
914 doc->setModifiedSinceSave();
915 }
916 }
918 gchar const *SPNamedView::getName() const
919 {
920 SPException ex;
921 SP_EXCEPTION_INIT(&ex);
922 return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
923 }
925 guint SPNamedView::getViewCount()
926 {
927 return ++viewcount;
928 }
930 GSList const *SPNamedView::getViewList() const
931 {
932 return views;
933 }
935 /* This should be moved somewhere */
937 static gboolean sp_str_to_bool(const gchar *str)
938 {
939 if (str) {
940 if (!g_strcasecmp(str, "true") ||
941 !g_strcasecmp(str, "yes") ||
942 !g_strcasecmp(str, "y") ||
943 (atoi(str) != 0)) {
944 return TRUE;
945 }
946 }
948 return FALSE;
949 }
951 /* fixme: Collect all these length parsing methods and think common sane API */
953 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
954 {
955 if (!str) {
956 return FALSE;
957 }
959 gchar *u;
960 gdouble v = g_ascii_strtod(str, &u);
961 if (!u) {
962 return FALSE;
963 }
964 while (isspace(*u)) {
965 u += 1;
966 }
968 if (!*u) {
969 /* No unit specified - keep default */
970 *val = v;
971 return TRUE;
972 }
974 if (base & SP_UNIT_DEVICE) {
975 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
976 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
977 *val = v;
978 return TRUE;
979 }
980 }
982 if (base & SP_UNIT_ABSOLUTE) {
983 if (!strncmp(u, "pt", 2)) {
984 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
985 } else if (!strncmp(u, "mm", 2)) {
986 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
987 } else if (!strncmp(u, "cm", 2)) {
988 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
989 } else if (!strncmp(u, "m", 1)) {
990 *unit = &sp_unit_get_by_id(SP_UNIT_M);
991 } else if (!strncmp(u, "in", 2)) {
992 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
993 } else {
994 return FALSE;
995 }
996 *val = v;
997 return TRUE;
998 }
1000 return FALSE;
1001 }
1003 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
1004 {
1005 if (!str) {
1006 return FALSE;
1007 }
1009 gchar *u;
1010 gdouble v = g_ascii_strtod(str, &u);
1011 if (!u) {
1012 return FALSE;
1013 }
1014 v = CLAMP(v, 0.0, 1.0);
1016 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1018 return TRUE;
1019 }
1021 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1022 {
1023 g_return_val_if_fail(document != NULL, NULL);
1025 SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1026 g_assert(nv != NULL);
1028 if (id == NULL) {
1029 return (SPNamedView *) nv;
1030 }
1032 while (nv && strcmp(nv->id, id)) {
1033 nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1034 }
1036 return (SPNamedView *) nv;
1037 }
1039 /**
1040 * Returns namedview's default metric.
1041 */
1042 SPMetric SPNamedView::getDefaultMetric() const
1043 {
1044 if (doc_units) {
1045 return sp_unit_get_metric(doc_units);
1046 } else {
1047 return SP_PT;
1048 }
1049 }
1051 /**
1052 * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1053 */
1054 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1055 {
1056 for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1057 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1058 if (grid->isEnabled())
1059 return grid;
1060 }
1062 return NULL;
1063 }
1065 void SPNamedView::translateGuides(NR::translate const &tr) {
1066 for (GSList *l = guides; l != NULL; l = l->next) {
1067 SPGuide &guide = *SP_GUIDE(l->data);
1068 Geom::Point point_on_line = guide.point_on_line;
1069 point_on_line[0] += tr[0];
1070 point_on_line[1] += tr[1];
1071 sp_guide_moveto(guide, point_on_line, true);
1072 }
1073 }
1075 void SPNamedView::scrollAllDesktops(double dx, double dy, bool is_scrolling) {
1076 for(GSList *l = views; l; l = l->next) {
1077 SPDesktop *desktop = static_cast<SPDesktop *>(l->data);
1078 desktop->scroll_world_in_svg_coords(dx, dy, is_scrolling);
1079 }
1080 }
1083 /*
1084 Local Variables:
1085 mode:c++
1086 c-file-style:"stroustrup"
1087 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1088 indent-tabs-mode:nil
1089 fill-column:99
1090 End:
1091 */
1092 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :