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-object-midpoints");
253 sp_object_read_attr(object, "inkscape:snap-bbox-edge-midpoints");
254 sp_object_read_attr(object, "inkscape:snap-bbox-midpoints");
255 sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
256 sp_object_read_attr(object, "inkscape:snap-intersection-paths");
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 = 10000;
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 = 20;
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 = 20;
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.snapprefs.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
459 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
460 break;
461 case SP_ATTR_INKSCAPE_SNAP_BBOX:
462 nv->snap_manager.snapprefs.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
463 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
464 break;
465 case SP_ATTR_INKSCAPE_SNAP_NODES:
466 nv->snap_manager.snapprefs.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
467 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
468 break;
469 case SP_ATTR_INKSCAPE_SNAP_CENTER:
470 nv->snap_manager.snapprefs.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
471 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
472 break;
473 case SP_ATTR_INKSCAPE_SNAP_SMOOTH_NODES:
474 nv->snap_manager.snapprefs.setSnapSmoothNodes(value ? sp_str_to_bool(value) : FALSE);
475 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
476 break;
477 case SP_ATTR_INKSCAPE_SNAP_LINE_MIDPOINTS:
478 nv->snap_manager.snapprefs.setSnapLineMidpoints(value ? sp_str_to_bool(value) : FALSE);
479 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
480 break;
481 case SP_ATTR_INKSCAPE_SNAP_OBJECT_MIDPOINTS:
482 nv->snap_manager.snapprefs.setSnapObjectMidpoints(value ? sp_str_to_bool(value) : FALSE);
483 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
484 break;
485 case SP_ATTR_INKSCAPE_SNAP_BBOX_EDGE_MIDPOINTS:
486 nv->snap_manager.snapprefs.setSnapBBoxEdgeMidpoints(value ? sp_str_to_bool(value) : FALSE);
487 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
488 break;
489 case SP_ATTR_INKSCAPE_SNAP_BBOX_MIDPOINTS:
490 nv->snap_manager.snapprefs.setSnapBBoxMidpoints(value ? sp_str_to_bool(value) : FALSE);
491 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
492 break;
493 case SP_ATTR_INKSCAPE_SNAP_GUIDE:
494 nv->snap_manager.snapprefs.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
495 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
496 break;
497 case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
498 nv->snap_manager.snapprefs.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
499 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
500 break;
501 case SP_ATTR_INKSCAPE_SNAP_INTERS_PATHS:
502 nv->snap_manager.snapprefs.setSnapIntersectionCS(value ? sp_str_to_bool(value) : FALSE);
503 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
504 break;
505 case SP_ATTR_INKSCAPE_OBJECT_PATHS:
506 nv->snap_manager.snapprefs.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
507 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
508 break;
509 case SP_ATTR_INKSCAPE_OBJECT_NODES:
510 nv->snap_manager.snapprefs.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
511 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
512 break;
513 case SP_ATTR_INKSCAPE_BBOX_PATHS:
514 nv->snap_manager.snapprefs.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
515 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
516 break;
517 case SP_ATTR_INKSCAPE_BBOX_NODES:
518 nv->snap_manager.snapprefs.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);
519 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
520 break;
521 case SP_ATTR_INKSCAPE_SNAP_PAGE:
522 nv->snap_manager.snapprefs.setSnapToPageBorder(value ? sp_str_to_bool(value) : FALSE);
523 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
524 break;
525 case SP_ATTR_INKSCAPE_CURRENT_LAYER:
526 nv->default_layer_id = value ? g_quark_from_string(value) : 0;
527 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
528 break;
529 case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
530 nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
531 defaultConnSpacing;
532 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
533 break;
534 case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
535 /* The default unit if the document doesn't override this: e.g. for files saved as
536 * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
537 * earlier.
538 *
539 * Here we choose `px': useful for screen-destined SVGs, and fewer bug reports
540 * about "not the same numbers as what's in the SVG file" (at least for documents
541 * without a viewBox attribute on the root <svg> element). Similarly, it's also
542 * the most reliable unit (i.e. least likely to be wrong in different viewing
543 * conditions) for viewBox-less SVG files given that it's the unit that inkscape
544 * uses for all coordinates.
545 *
546 * For documents that do have a viewBox attribute on the root <svg> element, it
547 * might be better if we used either viewBox coordinates or if we used the unit of
548 * say the width attribute of the root <svg> element. However, these pose problems
549 * in that they aren't in general absolute units as currently required by
550 * doc_units.
551 */
552 SPUnit const *new_unit = &sp_unit_get_by_id(SP_UNIT_PX);
554 if (value) {
555 SPUnit const *const req_unit = sp_unit_get_by_abbreviation(value);
556 if ( req_unit == NULL ) {
557 g_warning("Unrecognized unit `%s'", value);
558 /* fixme: Document errors should be reported in the status bar or
559 * the like (e.g. as per
560 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing); g_log
561 * should be only for programmer errors. */
562 } else if ( req_unit->base == SP_UNIT_ABSOLUTE ||
563 req_unit->base == SP_UNIT_DEVICE ) {
564 new_unit = req_unit;
565 } else {
566 g_warning("Document units must be absolute like `mm', `pt' or `px', but found `%s'",
567 value);
568 /* fixme: Don't use g_log (see above). */
569 }
570 }
571 nv->doc_units = new_unit;
572 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
573 break;
574 }
575 default:
576 if (((SPObjectClass *) (parent_class))->set) {
577 ((SPObjectClass *) (parent_class))->set(object, key, value);
578 }
579 break;
580 }
581 }
583 /**
584 * 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,
585 * otherwise only add it to the specified desktop.
586 */
587 static Inkscape::CanvasGrid*
588 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
589 Inkscape::CanvasGrid* grid = NULL;
590 //check if namedview already has an object for this grid
591 for (GSList *l = nv->grids; l != NULL; l = l->next) {
592 Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
593 if (repr == g->repr) {
594 grid = g;
595 break;
596 }
597 }
599 if (!grid) {
600 //create grid object
601 Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
602 if (!nv->document) {
603 g_warning("sp_namedview_add_grid - how come doc is null here?!");
604 return NULL;
605 }
606 grid = Inkscape::CanvasGrid::NewGrid(nv, repr, nv->document, gridtype);
607 nv->grids = g_slist_append(nv->grids, grid);
608 }
610 if (!desktop) {
611 //add canvasitem to all desktops
612 for (GSList *l = nv->views; l != NULL; l = l->next) {
613 SPDesktop *dt = static_cast<SPDesktop*>(l->data);
614 grid->createCanvasItem(dt);
615 }
616 } else {
617 //add canvasitem only for specified desktop
618 grid->createCanvasItem(desktop);
619 }
621 return grid;
622 }
624 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
625 {
626 SPNamedView *nv = (SPNamedView *) object;
628 if (((SPObjectClass *) (parent_class))->child_added) {
629 (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
630 }
632 if (!strcmp(child->name(), "inkscape:grid")) {
633 sp_namedview_add_grid(nv, child, NULL);
634 } else {
635 SPObject *no = object->document->getObjectByRepr(child);
636 if ( !SP_IS_OBJECT(no) )
637 return;
639 if (SP_IS_GUIDE(no)) {
640 SPGuide *g = (SPGuide *) no;
641 nv->guides = g_slist_prepend(nv->guides, g);
642 g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
643 if (nv->editable) {
644 for (GSList *l = nv->views; l != NULL; l = l->next) {
645 sp_guide_show(g, static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
646 if (static_cast<SPDesktop*>(l->data)->guides_active)
647 sp_guide_sensitize(g,
648 sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
649 TRUE);
650 if (nv->showguides) {
651 for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
652 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
653 }
654 } else {
655 for (GSList *v = SP_GUIDE(g)->views; v != NULL; v = v->next) {
656 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
657 }
658 }
659 }
660 }
661 }
662 }
663 }
665 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
666 {
667 SPNamedView *nv = (SPNamedView *) object;
669 if (!strcmp(child->name(), "inkscape:grid")) {
670 for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
671 Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
672 if ( gr->repr == child ) {
673 delete gr;
674 nv->grids = g_slist_remove_link(nv->grids, iter);
675 break;
676 }
677 }
678 } else {
679 GSList **ref = &nv->guides;
680 for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
681 if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
682 *ref = iter->next;
683 iter->next = NULL;
684 g_slist_free_1(iter);
685 break;
686 }
687 ref = &iter->next;
688 }
689 }
691 if (((SPObjectClass *) (parent_class))->remove_child) {
692 (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
693 }
694 }
696 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
697 {
698 if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
699 repr != SP_OBJECT_REPR(object) )
700 {
701 if (repr) {
702 repr->mergeFrom(SP_OBJECT_REPR(object), "id");
703 } else {
704 repr = SP_OBJECT_REPR(object)->duplicate(doc);
705 }
706 }
708 return repr;
709 }
711 void SPNamedView::show(SPDesktop *desktop)
712 {
713 for (GSList *l = guides; l != NULL; l = l->next) {
714 sp_guide_show(SP_GUIDE(l->data), desktop->guides, (GCallback) sp_dt_guide_event);
715 if (desktop->guides_active) {
716 sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(desktop), TRUE);
717 }
718 if (showguides) {
719 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
720 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
721 }
722 } else {
723 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
724 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
725 }
726 }
727 }
729 views = g_slist_prepend(views, desktop);
731 // generate grids specified in SVG:
732 Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
733 if (repr) {
734 for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
735 if (!strcmp(child->name(), "inkscape:grid")) {
736 sp_namedview_add_grid(this, child, desktop);
737 }
738 }
739 }
741 desktop->showGrids(grids_visible, false);
742 }
744 #define MIN_ONSCREEN_DISTANCE 50
746 /*
747 * Restores window geometry from the document settings or defaults in prefs
748 */
749 void sp_namedview_window_from_document(SPDesktop *desktop)
750 {
751 SPNamedView *nv = desktop->namedview;
752 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
753 bool geometry_from_file = prefs->getBool("/options/savewindowgeometry/value");
755 // restore window size and position stored with the document
756 if (geometry_from_file) {
757 gint w = MIN(gdk_screen_width(), nv->window_width);
758 gint h = MIN(gdk_screen_height(), nv->window_height);
759 gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
760 gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
761 if (w>0 && h>0 && x>0 && y>0) {
762 x = MIN(gdk_screen_width() - w, x);
763 y = MIN(gdk_screen_height() - h, y);
764 }
765 if (w>0 && h>0) {
766 desktop->setWindowSize(w, h);
767 }
768 if (x>0 && y>0) {
769 desktop->setWindowPosition(Geom::Point(x, y));
770 }
771 }
773 // restore zoom and view
774 if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !IS_NAN(nv->zoom)
775 && nv->cx != HUGE_VAL && !IS_NAN(nv->cx)
776 && nv->cy != HUGE_VAL && !IS_NAN(nv->cy)) {
777 desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
778 } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
779 desktop->zoom_page();
780 }
782 // cancel any history of zooms up to this point
783 if (desktop->zooms_past) {
784 g_list_free(desktop->zooms_past);
785 desktop->zooms_past = NULL;
786 }
787 }
789 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
790 {
791 SPObject *layer = NULL;
792 SPDocument *document = desktop->doc();
793 SPNamedView *nv = desktop->namedview;
794 if ( nv->default_layer_id != 0 ) {
795 layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
796 }
797 // don't use that object if it's not at least group
798 if ( !layer || !SP_IS_GROUP(layer) ) {
799 layer = NULL;
800 }
801 // if that didn't work out, look for the topmost layer
802 if (!layer) {
803 SPObject *iter = sp_object_first_child(SP_DOCUMENT_ROOT(document));
804 for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
805 if (desktop->isLayer(iter)) {
806 layer = iter;
807 }
808 }
809 }
810 if (layer) {
811 desktop->setCurrentLayer(layer);
812 }
814 // FIXME: find a better place to do this
815 desktop->event_log->updateUndoVerbs();
816 }
818 void sp_namedview_document_from_window(SPDesktop *desktop)
819 {
820 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
821 bool save_geometry_in_file = prefs->getBool("/options/savewindowgeometry/value", 0);
822 Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
823 Geom::Rect const r = desktop->get_display_area();
825 // saving window geometry is not undoable
826 bool saved = sp_document_get_undo_sensitive(sp_desktop_document(desktop));
827 sp_document_set_undo_sensitive(sp_desktop_document(desktop), false);
829 sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
830 sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[Geom::X]);
831 sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[Geom::Y]);
833 if (save_geometry_in_file) {
834 gint w, h, x, y;
835 desktop->getWindowGeometry(x, y, w, h);
836 sp_repr_set_int(view, "inkscape:window-width", w);
837 sp_repr_set_int(view, "inkscape:window-height", h);
838 sp_repr_set_int(view, "inkscape:window-x", x);
839 sp_repr_set_int(view, "inkscape:window-y", y);
840 }
842 view->setAttribute("inkscape:current-layer", SP_OBJECT_ID(desktop->currentLayer()));
844 // restore undoability
845 sp_document_set_undo_sensitive(sp_desktop_document(desktop), saved);
846 }
848 void SPNamedView::hide(SPDesktop const *desktop)
849 {
850 g_assert(desktop != NULL);
851 g_assert(g_slist_find(views, desktop));
853 for (GSList *l = guides; l != NULL; l = l->next) {
854 sp_guide_hide(SP_GUIDE(l->data), sp_desktop_canvas(desktop));
855 }
857 views = g_slist_remove(views, desktop);
858 }
860 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
861 {
862 g_assert(desktop != NULL);
863 g_assert(g_slist_find(views, desktop));
865 SPDesktop *dt = static_cast<SPDesktop*>(desktop);
867 for (GSList *l = guides; l != NULL; l = l->next) {
868 sp_guide_sensitize(SP_GUIDE(l->data), sp_desktop_canvas(dt), active);
869 }
870 }
872 static void sp_namedview_setup_guides(SPNamedView *nv)
873 {
874 for (GSList *l = nv->guides; l != NULL; l = l->next) {
875 if (nv->showguides) {
876 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
877 sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
878 }
879 } else {
880 for (GSList *v = SP_GUIDE(l->data)->views; v != NULL; v = v->next) {
881 sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
882 }
883 }
884 }
885 }
887 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
888 {
889 unsigned int v;
890 unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
891 if (!set) { // hide guides if not specified, for backwards compatibility
892 v = FALSE;
893 } else {
894 v = !v;
895 }
897 bool saved = sp_document_get_undo_sensitive(doc);
898 sp_document_set_undo_sensitive(doc, false);
899 sp_repr_set_boolean(repr, "showguides", v);
900 sp_document_set_undo_sensitive(doc, saved);
902 doc->setModifiedSinceSave();
903 }
905 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
906 {
907 namedview->grids_visible = show;
909 SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
910 Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
912 bool saved = sp_document_get_undo_sensitive(doc);
913 sp_document_set_undo_sensitive(doc, false);
914 sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
915 sp_document_set_undo_sensitive(doc, saved);
917 /* we don't want the document to get dirty on startup; that's when
918 we call this function with dirty_document = false */
919 if (dirty_document) {
920 doc->setModifiedSinceSave();
921 }
922 }
924 gchar const *SPNamedView::getName() const
925 {
926 SPException ex;
927 SP_EXCEPTION_INIT(&ex);
928 return sp_object_getAttribute(SP_OBJECT(this), "id", &ex);
929 }
931 guint SPNamedView::getViewCount()
932 {
933 return ++viewcount;
934 }
936 GSList const *SPNamedView::getViewList() const
937 {
938 return views;
939 }
941 /* This should be moved somewhere */
943 static gboolean sp_str_to_bool(const gchar *str)
944 {
945 if (str) {
946 if (!g_strcasecmp(str, "true") ||
947 !g_strcasecmp(str, "yes") ||
948 !g_strcasecmp(str, "y") ||
949 (atoi(str) != 0)) {
950 return TRUE;
951 }
952 }
954 return FALSE;
955 }
957 /* fixme: Collect all these length parsing methods and think common sane API */
959 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit)
960 {
961 if (!str) {
962 return FALSE;
963 }
965 gchar *u;
966 gdouble v = g_ascii_strtod(str, &u);
967 if (!u) {
968 return FALSE;
969 }
970 while (isspace(*u)) {
971 u += 1;
972 }
974 if (!*u) {
975 /* No unit specified - keep default */
976 *val = v;
977 return TRUE;
978 }
980 if (base & SP_UNIT_DEVICE) {
981 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
982 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
983 *val = v;
984 return TRUE;
985 }
986 }
988 if (base & SP_UNIT_ABSOLUTE) {
989 if (!strncmp(u, "pt", 2)) {
990 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
991 } else if (!strncmp(u, "mm", 2)) {
992 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
993 } else if (!strncmp(u, "cm", 2)) {
994 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
995 } else if (!strncmp(u, "m", 1)) {
996 *unit = &sp_unit_get_by_id(SP_UNIT_M);
997 } else if (!strncmp(u, "in", 2)) {
998 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
999 } else {
1000 return FALSE;
1001 }
1002 *val = v;
1003 return TRUE;
1004 }
1006 return FALSE;
1007 }
1009 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
1010 {
1011 if (!str) {
1012 return FALSE;
1013 }
1015 gchar *u;
1016 gdouble v = g_ascii_strtod(str, &u);
1017 if (!u) {
1018 return FALSE;
1019 }
1020 v = CLAMP(v, 0.0, 1.0);
1022 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
1024 return TRUE;
1025 }
1027 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
1028 {
1029 g_return_val_if_fail(document != NULL, NULL);
1031 SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
1032 g_assert(nv != NULL);
1034 if (id == NULL) {
1035 return (SPNamedView *) nv;
1036 }
1038 while (nv && strcmp(nv->id, id)) {
1039 nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
1040 }
1042 return (SPNamedView *) nv;
1043 }
1045 /**
1046 * Returns namedview's default metric.
1047 */
1048 SPMetric SPNamedView::getDefaultMetric() const
1049 {
1050 if (doc_units) {
1051 return sp_unit_get_metric(doc_units);
1052 } else {
1053 return SP_PT;
1054 }
1055 }
1057 /**
1058 * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1059 */
1060 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1061 {
1062 for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1063 Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1064 if (grid->isEnabled())
1065 return grid;
1066 }
1068 return NULL;
1069 }
1071 void SPNamedView::translateGuides(Geom::Translate const &tr) {
1072 for (GSList *l = guides; l != NULL; l = l->next) {
1073 SPGuide &guide = *SP_GUIDE(l->data);
1074 Geom::Point point_on_line = guide.point_on_line;
1075 point_on_line[0] += tr[0];
1076 point_on_line[1] += tr[1];
1077 sp_guide_moveto(guide, point_on_line, true);
1078 }
1079 }
1081 void SPNamedView::scrollAllDesktops(double dx, double dy, bool is_scrolling) {
1082 for(GSList *l = views; l; l = l->next) {
1083 SPDesktop *desktop = static_cast<SPDesktop *>(l->data);
1084 desktop->scroll_world_in_svg_coords(dx, dy, is_scrolling);
1085 }
1086 }
1089 /*
1090 Local Variables:
1091 mode:c++
1092 c-file-style:"stroustrup"
1093 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1094 indent-tabs-mode:nil
1095 fill-column:99
1096 End:
1097 */
1098 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :