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