Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / sp-namedview.cpp
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;
104 static void sp_namedview_init(SPNamedView *nv)
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);
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 //        SPDocumentUndo::done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid from pre0.46 grid settings"));
208     }
211 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
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     object->readAttr( "inkscape:document-units");
221     object->readAttr( "viewonly");
222     object->readAttr( "showguides");
223     object->readAttr( "showgrid");
224     object->readAttr( "gridtolerance");
225     object->readAttr( "guidetolerance");
226     object->readAttr( "objecttolerance");
227     object->readAttr( "guidecolor");
228     object->readAttr( "guideopacity");
229     object->readAttr( "guidehicolor");
230     object->readAttr( "guidehiopacity");
231     object->readAttr( "showborder");
232     object->readAttr( "inkscape:showpageshadow");
233     object->readAttr( "borderlayer");
234     object->readAttr( "bordercolor");
235     object->readAttr( "borderopacity");
236     object->readAttr( "pagecolor");
237     object->readAttr( "inkscape:pageopacity");
238     object->readAttr( "inkscape:pageshadow");
239     object->readAttr( "inkscape:zoom");
240     object->readAttr( "inkscape:cx");
241     object->readAttr( "inkscape:cy");
242     object->readAttr( "inkscape:window-width");
243     object->readAttr( "inkscape:window-height");
244     object->readAttr( "inkscape:window-x");
245     object->readAttr( "inkscape:window-y");
246     object->readAttr( "inkscape:window-maximized");
247     object->readAttr( "inkscape:snap-global");
248     object->readAttr( "inkscape:snap-bbox");
249     object->readAttr( "inkscape:snap-nodes");
250     object->readAttr( "inkscape:snap-from-guide");
251     object->readAttr( "inkscape:snap-center");
252     object->readAttr( "inkscape:snap-smooth-nodes");
253     object->readAttr( "inkscape:snap-midpoints");
254     object->readAttr( "inkscape:snap-object-midpoints");
255     object->readAttr( "inkscape:snap-bbox-edge-midpoints");
256     object->readAttr( "inkscape:snap-bbox-midpoints");
257         object->readAttr( "inkscape:snap-to-guides");
258         object->readAttr( "inkscape:snap-grids");
259     object->readAttr( "inkscape:snap-intersection-paths");
260     object->readAttr( "inkscape:object-paths");
261     object->readAttr( "inkscape:object-nodes");
262     object->readAttr( "inkscape:bbox-paths");
263     object->readAttr( "inkscape:bbox-nodes");
264     object->readAttr( "inkscape:snap-page");
265     object->readAttr( "inkscape:current-layer");
266     object->readAttr( "inkscape:connector-spacing");
268     /* Construct guideline list */
269     for (SPObject *o = SP_OBJECT(og)->first_child() ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
270         if (SP_IS_GUIDE(o)) {
271             SPGuide * g = SP_GUIDE(o);
272             nv->guides = g_slist_prepend(nv->guides, g);
273             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
274         }
275     }
277     // backwards compatibility with grid settings (pre 0.46)
278     sp_namedview_generate_old_grid(nv, document, repr);
281 static void sp_namedview_release(SPObject *object)
283     SPNamedView *namedview = (SPNamedView *) object;
285     if (namedview->guides) {
286         g_slist_free(namedview->guides);
287         namedview->guides = NULL;
288     }
290     // delete grids:
291     while ( namedview->grids ) {
292         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
293         delete gr;
294         namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
295     }
297     if (((SPObjectClass *) parent_class)->release) {
298         ((SPObjectClass *) parent_class)->release(object);
299     }
301     namedview->snap_manager.~SnapManager();
304 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value)
306     SPNamedView *nv = SP_NAMEDVIEW(object);
307     // TODO investigate why we grab this and then never use it
308     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
310     switch (key) {
311     case SP_ATTR_VIEWONLY:
312             nv->editable = (!value);
313             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
314             break;
315     case SP_ATTR_SHOWGUIDES:
316             if (!value) { // show guides if not specified, for backwards compatibility
317                 nv->showguides = TRUE;
318             } else {
319                 nv->showguides = sp_str_to_bool(value);
320             }
321             sp_namedview_setup_guides(nv);
322             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
323             break;
324     case SP_ATTR_SHOWGRIDS:
325             if (!value) { // don't show grids if not specified, for backwards compatibility
326                 nv->grids_visible = false;
327             } else {
328                 nv->grids_visible = sp_str_to_bool(value);
329             }
330             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
331             break;
332     case SP_ATTR_GRIDTOLERANCE:
333                         nv->snap_manager.snapprefs.setGridTolerance(value ? g_ascii_strtod(value, NULL) : 10000);
334                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
335                         break;
336     case SP_ATTR_GUIDETOLERANCE:
337                         nv->snap_manager.snapprefs.setGuideTolerance(value ? g_ascii_strtod(value, NULL) : 20);
338             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
339             break;
340     case SP_ATTR_OBJECTTOLERANCE:
341                         nv->snap_manager.snapprefs.setObjectTolerance(value ? g_ascii_strtod(value, NULL) : 20);
342             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
343             break;
344     case SP_ATTR_GUIDECOLOR:
345             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
346             if (value) {
347                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
348             }
349             for (GSList *l = nv->guides; l != NULL; l = l->next) {
350                 g_object_set(G_OBJECT(l->data), "color", nv->guidecolor, NULL);
351             }
352             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
353             break;
354     case SP_ATTR_GUIDEOPACITY:
355             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
356             sp_nv_read_opacity(value, &nv->guidecolor);
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_GUIDEHICOLOR:
363             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
364             if (value) {
365                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
366             }
367             for (GSList *l = nv->guides; l != NULL; l = l->next) {
368                 g_object_set(G_OBJECT(l->data), "hicolor", nv->guidehicolor, NULL);
369             }
370             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
371             break;
372     case SP_ATTR_GUIDEHIOPACITY:
373             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
374             sp_nv_read_opacity(value, &nv->guidehicolor);
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_SHOWBORDER:
381             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
382             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
383             break;
384     case SP_ATTR_BORDERLAYER:
385             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
386             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
387             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
388             break;
389     case SP_ATTR_BORDERCOLOR:
390             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
391             if (value) {
392                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
393             }
394             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
395             break;
396     case SP_ATTR_BORDEROPACITY:
397             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
398             sp_nv_read_opacity(value, &nv->bordercolor);
399             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
400             break;
401     case SP_ATTR_PAGECOLOR:
402             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
403             if (value) {
404                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
405             }
406             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
407             break;
408     case SP_ATTR_INKSCAPE_PAGEOPACITY:
409             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
410             sp_nv_read_opacity(value, &nv->pagecolor);
411             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
412             break;
413     case SP_ATTR_INKSCAPE_PAGESHADOW:
414             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
415             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
416             break;
417     case SP_ATTR_SHOWPAGESHADOW:
418             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
419             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
420             break;
421     case SP_ATTR_INKSCAPE_ZOOM:
422             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
423             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
424             break;
425     case SP_ATTR_INKSCAPE_CX:
426             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
427             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
428             break;
429     case SP_ATTR_INKSCAPE_CY:
430             nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
431             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
432             break;
433     case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
434             nv->window_width = value? atoi(value) : -1; // -1 means not set
435             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
436             break;
437     case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
438             nv->window_height = value ? atoi(value) : -1; // -1 means not set
439             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
440             break;
441     case SP_ATTR_INKSCAPE_WINDOW_X:
442             nv->window_x = value ? atoi(value) : 0;
443             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
444             break;
445     case SP_ATTR_INKSCAPE_WINDOW_Y:
446             nv->window_y = value ? atoi(value) : 0;
447             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
448             break;
449     case SP_ATTR_INKSCAPE_WINDOW_MAXIMIZED:
450                         nv->window_maximized = value ? atoi(value) : 0;
451                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
452                         break;
453         case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
454             nv->snap_manager.snapprefs.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
455             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
456             break;
457     case SP_ATTR_INKSCAPE_SNAP_BBOX:
458             nv->snap_manager.snapprefs.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
459             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
460             break;
461     case SP_ATTR_INKSCAPE_SNAP_NODES:
462             nv->snap_manager.snapprefs.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
463             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
464             break;
465     case SP_ATTR_INKSCAPE_SNAP_CENTER:
466             nv->snap_manager.snapprefs.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
467             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
468             break;
469     case SP_ATTR_INKSCAPE_SNAP_GRIDS:
470                         nv->snap_manager.snapprefs.setSnapToGrids(value ? sp_str_to_bool(value) : TRUE);
471                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
472                         break;
473     case SP_ATTR_INKSCAPE_SNAP_TO_GUIDES:
474                         nv->snap_manager.snapprefs.setSnapToGuides(value ? sp_str_to_bool(value) : TRUE);
475                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
476                         break;
477         case SP_ATTR_INKSCAPE_SNAP_SMOOTH_NODES:
478             nv->snap_manager.snapprefs.setSnapSmoothNodes(value ? sp_str_to_bool(value) : FALSE);
479             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
480             break;
481     case SP_ATTR_INKSCAPE_SNAP_LINE_MIDPOINTS:
482             nv->snap_manager.snapprefs.setSnapLineMidpoints(value ? sp_str_to_bool(value) : FALSE);
483             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
484             break;
485     case SP_ATTR_INKSCAPE_SNAP_OBJECT_MIDPOINTS:
486                         nv->snap_manager.snapprefs.setSnapObjectMidpoints(value ? sp_str_to_bool(value) : FALSE);
487                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
488                         break;
489     case SP_ATTR_INKSCAPE_SNAP_BBOX_EDGE_MIDPOINTS:
490                         nv->snap_manager.snapprefs.setSnapBBoxEdgeMidpoints(value ? sp_str_to_bool(value) : FALSE);
491                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
492                         break;
493         case SP_ATTR_INKSCAPE_SNAP_BBOX_MIDPOINTS:
494                         nv->snap_manager.snapprefs.setSnapBBoxMidpoints(value ? sp_str_to_bool(value) : FALSE);
495                         object->requestModified(SP_OBJECT_MODIFIED_FLAG);
496                         break;
497         case SP_ATTR_INKSCAPE_SNAP_FROM_GUIDE:
498             nv->snap_manager.snapprefs.setSnapModeGuide(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     }
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;
624 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
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                     g->SPGuide::showSPGuide(static_cast<SPDesktop*>(l->data)->guides, (GCallback) sp_dt_guide_event);
646                     if (static_cast<SPDesktop*>(l->data)->guides_active)
647                         g->sensitize(sp_desktop_canvas(static_cast<SPDesktop*> (l->data)),
648                                            TRUE);
649                     sp_namedview_show_single_guide(SP_GUIDE(g), nv->showguides);
650                 }
651             }
652         }
653     }
656 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
658     SPNamedView *nv = (SPNamedView *) object;
660     if (!strcmp(child->name(), "inkscape:grid")) {
661         for ( GSList *iter = nv->grids ; iter ; iter = iter->next ) {
662             Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)iter->data;
663             if ( gr->repr == child ) {
664                 delete gr;
665                 nv->grids = g_slist_remove_link(nv->grids, iter);
666                 break;
667             }
668         }
669     } else {
670         GSList **ref = &nv->guides;
671         for ( GSList *iter = nv->guides ; iter ; iter = iter->next ) {
672             if ( SP_OBJECT_REPR((SPObject *)iter->data) == child ) {
673                 *ref = iter->next;
674                 iter->next = NULL;
675                 g_slist_free_1(iter);
676                 break;
677             }
678             ref = &iter->next;
679         }
680     }
682     if (((SPObjectClass *) (parent_class))->remove_child) {
683         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
684     }
687 static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
689     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
690          repr != SP_OBJECT_REPR(object) )
691     {
692         if (repr) {
693             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
694         } else {
695              repr = SP_OBJECT_REPR(object)->duplicate(doc);
696         }
697     }
699     return repr;
702 void SPNamedView::show(SPDesktop *desktop)
704     for (GSList *l = guides; l != NULL; l = l->next) {
705         SP_GUIDE(l->data)->showSPGuide( desktop->guides, (GCallback) sp_dt_guide_event);
706         if (desktop->guides_active) {
707             SP_GUIDE(l->data)->sensitize(sp_desktop_canvas(desktop), TRUE);
708         }
709         sp_namedview_show_single_guide(SP_GUIDE(l->data), showguides);
710     }
712     views = g_slist_prepend(views, desktop);
714     // generate grids specified in SVG:
715     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
716     if (repr) {
717         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
718             if (!strcmp(child->name(), "inkscape:grid")) {
719                 sp_namedview_add_grid(this, child, desktop);
720             }
721         }
722     }
724     desktop->showGrids(grids_visible, false);
727 #define MIN_ONSCREEN_DISTANCE 50
729 void SPNamedView::writeNewGrid(SPDocument *document,int gridtype)
731         g_assert(this->getRepr() != NULL);
732         Inkscape::CanvasGrid::writeNewGridToRepr(this->getRepr(),document,static_cast<Inkscape::GridType>(gridtype));
735 /*
736  * Restores window geometry from the document settings or defaults in prefs
737  */
738 void sp_namedview_window_from_document(SPDesktop *desktop)
740     SPNamedView *nv = desktop->namedview;
741     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
742     bool geometry_from_file = prefs->getBool("/options/savewindowgeometry/value");
744     // restore window size and position stored with the document
745     if (geometry_from_file) {
746         if (nv->window_maximized) {
747                 Gtk::Window *win = desktop->getToplevel();
748                 if (win){
749                         win->maximize();
750                 }
751         } else {
752                 gint w = MIN(gdk_screen_width(), nv->window_width);
753                         gint h = MIN(gdk_screen_height(), nv->window_height);
754                         // prevent the window from moving off the screen to the right or to the bottom
755                         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
756                         gint y = MIN(gdk_screen_height() - MIN_ONSCREEN_DISTANCE, nv->window_y);
757                         // prevent the window from moving off the screen to the left or to the top
758                         x = MAX(MIN_ONSCREEN_DISTANCE - nv->window_width, x);
759                         y = MAX(MIN_ONSCREEN_DISTANCE - nv->window_height, y);
760                         if (w>0 && h>0) {
761                                 desktop->setWindowSize(w, h);
762                                 desktop->setWindowPosition(Geom::Point(x, y));
763                         }
764         }
765     }
767     // restore zoom and view
768     if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !IS_NAN(nv->zoom)
769         && nv->cx != HUGE_VAL && !IS_NAN(nv->cx)
770         && nv->cy != HUGE_VAL && !IS_NAN(nv->cy)) {
771         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
772     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
773         desktop->zoom_page();
774     }
776     // cancel any history of zooms up to this point
777     if (desktop->zooms_past) {
778         g_list_free(desktop->zooms_past);
779         desktop->zooms_past = NULL;
780     }
783 bool SPNamedView::getSnapGlobal() const
785         return this->snap_manager.snapprefs.getSnapEnabledGlobally();
788 void SPNamedView::setSnapGlobal(bool v)
790         g_assert(this->getRepr() != NULL);
791         sp_repr_set_boolean(this->getRepr(), "inkscape:snap-global", v);
794 void sp_namedview_update_layers_from_document (SPDesktop *desktop)
796     SPObject *layer = NULL;
797     SPDocument *document = desktop->doc();
798     SPNamedView *nv = desktop->namedview;
799     if ( nv->default_layer_id != 0 ) {
800         layer = document->getObjectById(g_quark_to_string(nv->default_layer_id));
801     }
802     // don't use that object if it's not at least group
803     if ( !layer || !SP_IS_GROUP(layer) ) {
804         layer = NULL;
805     }
806     // if that didn't work out, look for the topmost layer
807     if (!layer) {
808         SPObject *iter = SP_DOCUMENT_ROOT(document)->first_child();
809         for ( ; iter ; iter = SP_OBJECT_NEXT(iter) ) {
810             if (desktop->isLayer(iter)) {
811                 layer = iter;
812             }
813         }
814     }
815     if (layer) {
816         desktop->setCurrentLayer(layer);
817     }
819     // FIXME: find a better place to do this
820     desktop->event_log->updateUndoVerbs();
823 void sp_namedview_document_from_window(SPDesktop *desktop)
825     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
826     bool save_geometry_in_file = prefs->getBool("/options/savewindowgeometry/value", 0);
827     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
828     Geom::Rect const r = desktop->get_display_area();
830     // saving window geometry is not undoable
831     bool saved = SPDocumentUndo::get_undo_sensitive(sp_desktop_document(desktop));
832         SPDocumentUndo::set_undo_sensitive(sp_desktop_document(desktop), false);
834     sp_repr_set_svg_double(view, "inkscape:zoom", desktop->current_zoom());
835     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[Geom::X]);
836     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[Geom::Y]);
838     if (save_geometry_in_file) {
839         gint w, h, x, y;
840         desktop->getWindowGeometry(x, y, w, h);
841         sp_repr_set_int(view, "inkscape:window-width", w);
842         sp_repr_set_int(view, "inkscape:window-height", h);
843         sp_repr_set_int(view, "inkscape:window-x", x);
844         sp_repr_set_int(view, "inkscape:window-y", y);
845         sp_repr_set_int(view, "inkscape:window-maximized", desktop->is_maximized());
846     }
848     view->setAttribute("inkscape:current-layer", desktop->currentLayer()->getId());
850     // restore undoability
851         SPDocumentUndo::set_undo_sensitive(sp_desktop_document(desktop), saved);
854 void SPNamedView::hide(SPDesktop const *desktop)
856     g_assert(desktop != NULL);
857     g_assert(g_slist_find(views, desktop));
859     for (GSList *l = guides; l != NULL; l = l->next) {
860         SP_GUIDE(l->data)->hideSPGuide(sp_desktop_canvas(desktop));
861     }
863     views = g_slist_remove(views, desktop);
866 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
868     g_assert(desktop != NULL);
869     g_assert(g_slist_find(views, desktop));
871     SPDesktop *dt = static_cast<SPDesktop*>(desktop);
873     for (GSList *l = guides; l != NULL; l = l->next) {
874         SP_GUIDE(l->data)->sensitize( sp_desktop_canvas(dt), active);
875     }
878 static void sp_namedview_setup_guides(SPNamedView *nv)
880     for (GSList *l = nv->guides; l != NULL; l = l->next) {
881         sp_namedview_show_single_guide(SP_GUIDE(l->data), nv->showguides);
882     }
885 static void sp_namedview_show_single_guide(SPGuide* guide, bool show)
887         for (GSList *v = guide->views; v != NULL; v = v->next) {
888                 if (show) {
889                         sp_canvas_item_show(SP_CANVAS_ITEM(v->data));
890                         sp_canvas_item_show(SP_CANVAS_ITEM(SP_GUIDELINE(v->data)->origin));
891                 } else {
892                         sp_canvas_item_hide(SP_CANVAS_ITEM(v->data));
893                         sp_canvas_item_hide(SP_CANVAS_ITEM(SP_GUIDELINE(v->data)->origin));
894                 }
895         }
898 void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
900     unsigned int v;
901     unsigned int set = sp_repr_get_boolean(repr, "showguides", &v);
902     if (!set) { // hide guides if not specified, for backwards compatibility
903         v = FALSE;
904     } else {
905         v = !v;
906     }
908     bool saved = SPDocumentUndo::get_undo_sensitive(doc);
909         SPDocumentUndo::set_undo_sensitive(doc, false);
910     sp_repr_set_boolean(repr, "showguides", v);
911         SPDocumentUndo::set_undo_sensitive(doc, saved);
913     doc->setModifiedSinceSave();
916 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
918     namedview->grids_visible = show;
920     SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
921     Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
923     bool saved = SPDocumentUndo::get_undo_sensitive(doc);
924         SPDocumentUndo::set_undo_sensitive(doc, false);
925     sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
926         SPDocumentUndo::set_undo_sensitive(doc, saved);
928     /* we don't want the document to get dirty on startup; that's when
929        we call this function with dirty_document = false */
930     if (dirty_document) {
931         doc->setModifiedSinceSave();
932     }
935 gchar const *SPNamedView::getName() const
937     SPException ex;
938     SP_EXCEPTION_INIT(&ex);
939     return SP_OBJECT(this)->getAttribute("id", &ex);
942 guint SPNamedView::getViewCount()
944     return ++viewcount;
947 GSList const *SPNamedView::getViewList() const
949     return views;
952 /* This should be moved somewhere */
954 static gboolean sp_str_to_bool(const gchar *str)
956     if (str) {
957         if (!g_strcasecmp(str, "true") ||
958             !g_strcasecmp(str, "yes") ||
959             !g_strcasecmp(str, "y") ||
960             (atoi(str) != 0)) {
961             return TRUE;
962         }
963     }
965     return FALSE;
968 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color)
970     if (!str) {
971         return FALSE;
972     }
974     gchar *u;
975     gdouble v = g_ascii_strtod(str, &u);
976     if (!u) {
977         return FALSE;
978     }
979     v = CLAMP(v, 0.0, 1.0);
981     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
983     return TRUE;
986 SPNamedView *sp_document_namedview(SPDocument *document, const gchar *id)
988     g_return_val_if_fail(document != NULL, NULL);
990     SPObject *nv = sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview");
991     g_assert(nv != NULL);
993     if (id == NULL) {
994         return (SPNamedView *) nv;
995     }
997     while (nv && strcmp(nv->getId(), id)) {
998         nv = sp_item_group_get_child_by_name((SPGroup *) document->root, nv, "sodipodi:namedview");
999     }
1001     return (SPNamedView *) nv;
1004 void SPNamedView::setGuides(bool v)
1006         g_assert(this->getRepr() != NULL);
1007         sp_repr_set_boolean(this->getRepr(), "showguides", v);
1008         sp_repr_set_boolean(this->getRepr(), "inkscape:guide-bbox", v);
1012 /**
1013  * Gets page fitting margin information from the namedview node in the XML.
1014  * \param nv_repr reference to this document's namedview
1015  * \param key the same key used by the RegisteredScalarUnit in
1016  *        ui/widget/page-sizer.cpp
1017  * \param margin_units units for the margin
1018  * \param return_units units to return the result in
1019  * \param width width in px (for percentage margins)
1020  * \param height height in px (for percentage margins)
1021  * \param use_width true if the this key is left or right margins, false
1022  *        otherwise.  Used for percentage margins.
1023  * \return the margin size in px, else 0.0 if anything is invalid.
1024  */
1025 double SPNamedView::getMarginLength(gchar const * const key,
1026                              SPUnit const * const margin_units,
1027                              SPUnit const * const return_units,
1028                              double const width,
1029                              double const height,
1030                              bool const use_width)
1032     double value;
1033         if(!this->storeAsDouble(key,&value)) {
1034         return 0.0;
1035     }
1036     if (margin_units == &sp_unit_get_by_id (SP_UNIT_PERCENT)) {
1037         return (use_width)? width * value : height * value; 
1038     }
1039     if (!sp_convert_distance (&value, margin_units, return_units)) {
1040         return 0.0;
1041     }
1042     return value;
1046 /**
1047  * Returns namedview's default metric.
1048  */
1049 SPMetric SPNamedView::getDefaultMetric() const
1051     if (doc_units) {
1052         return sp_unit_get_metric(doc_units);
1053     } else {
1054         return SP_PT;
1055     }
1058 /**
1059  * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
1060  */
1061 Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
1063     for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
1064         Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
1065         if (grid->isEnabled())
1066             return grid;
1067     }
1069     return NULL;
1072 void SPNamedView::translateGuides(Geom::Translate const &tr) {
1073     for (GSList *l = guides; l != NULL; l = l->next) {
1074         SPGuide &guide = *SP_GUIDE(l->data);
1075         Geom::Point point_on_line = guide.point_on_line;
1076         point_on_line[0] += tr[0];
1077         point_on_line[1] += tr[1];
1078         sp_guide_moveto(guide, point_on_line, true);
1079     }
1082 void SPNamedView::scrollAllDesktops(double dx, double dy, bool is_scrolling) {
1083         for(GSList *l = views; l; l = l->next) {
1084             SPDesktop *desktop = static_cast<SPDesktop *>(l->data);
1085             desktop->scroll_world_in_svg_coords(dx, dy, is_scrolling);
1086         }
1090 /*
1091   Local Variables:
1092   mode:c++
1093   c-file-style:"stroustrup"
1094   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1095   indent-tabs-mode:nil
1096   fill-column:99
1097   End:
1098 */
1099 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :