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