Code

refactor redrawing code into pen_redraw_all; cancel current unfinished path, inctead...
[inkscape.git] / src / prefs-utils.cpp
1 /*
2  * Utility functions for reading and setting preferences
3  *
4  * Authors:
5  *   bulia byak <bulia@dr.com>
6  *
7  * Copyright (C) 2003 authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
17 #include "inkscape.h"
18 #include "xml/repr.h"
21 void
22 prefs_set_int_attribute(gchar const *path, gchar const *attr, gint value)
23 {
24     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
25     if (repr) {
26         sp_repr_set_int(repr, attr, value);
27     }
28 }
30 gint
31 prefs_get_int_attribute(gchar const *path, gchar const *attr, gint def)
32 {
33     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
34     if (repr) {
35         return sp_repr_get_int_attribute(repr, attr, def);
36     } else {
37         return def;
38     }
39 }
41 /**
42 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
43 */
44 gint
45 prefs_get_int_attribute_limited(gchar const *path, gchar const *attr, gint def, gint min, gint max)
46 {
47     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
48     if (repr) {
49         gint const v = sp_repr_get_int_attribute(repr, attr, def);
50         if (v >= min && v <= max) {
51             return v;
52         } else {
53             return def;
54         }
55     } else {
56         return def;
57     }
58 }
60 void
61 prefs_set_double_attribute(gchar const *path, gchar const *attr, double value)
62 {
63     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
64     if (repr) {
65         sp_repr_set_svg_double(repr, attr, value);
66     }
67 }
69 double
70 prefs_get_double_attribute(gchar const *path, gchar const *attr, double def)
71 {
72     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
73     if (repr) {
74         return sp_repr_get_double_attribute(repr, attr, def);
75     } else {
76         return def;
77     }
78 }
80 /**
81 \brief Retrieves an int attribute guarding against screwed-up data; if the value is beyond limits, default is returned
82 */
83 double
84 prefs_get_double_attribute_limited(gchar const *path, gchar const *attr, double def, double min, double max)
85 {
86     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
87     if (repr) {
88         double const v = sp_repr_get_double_attribute(repr, attr, def);
89         if (v >= min && v <= max) {
90             return v;
91         } else {
92             return def;
93         }
94     } else {
95         return def;
96     }
97 }
99 gchar const *
100 prefs_get_string_attribute(gchar const *path, gchar const *attr)
102     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
103     if (repr) {
104         return (char *) repr->attribute(attr);
105     }
106     return NULL;
109 void
110 prefs_set_string_attribute(gchar const *path, gchar const *attr, gchar const *value)
112     Inkscape::XML::Node *repr = inkscape_get_repr(INKSCAPE, path);
113     if (repr) {
114         repr->setAttribute(attr, value);
115     }
118 void
119 prefs_set_recent_file(gchar const *uri, gchar const *name)
121     unsigned const max_documents = prefs_get_int_attribute("options.maxrecentdocuments", "value", 20);
123     if (uri != NULL) {
124         Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
125         if (recent) {
126             Inkscape::XML::Node *child = sp_repr_lookup_child(recent, "uri", uri);
127             if (child) {
128                 recent->changeOrder(child, NULL);
129             } else {
130                 if (recent->childCount() >= max_documents) {
131                     child = recent->firstChild();
132                     // count to the last
133                     for (unsigned i = 0; i + 2 < max_documents; ++i) {
134                         child = child->next();
135                     }
136                     // remove all after the last
137                     while (child->next()) {
138                         sp_repr_unparent(child->next());
139                     }
140                 }
141                 child = sp_repr_new("document");
142                 child->setAttribute("uri", uri);
143                 recent->addChild(child, NULL);
144             }
145             child->setAttribute("name", name);
146         }
147     }
150 gchar const **
151 prefs_get_recent_files()
153     Inkscape::XML::Node *recent = inkscape_get_repr(INKSCAPE, "documents.recent");
154     if (recent) {
155         unsigned const docs = recent->childCount();
156         gchar const **datalst = (gchar const **) g_malloc(sizeof(gchar *) * ((docs * 2) + 1));
158         gint i;
159         Inkscape::XML::Node *child;
160         for (i = 0, child = recent->firstChild();
161              child != NULL;
162              child = child->next(), i += 2)
163         {
164             gchar const *uri = child->attribute("uri");
165             gchar const *name = child->attribute("name");
166             datalst[i]     = uri;
167             datalst[i + 1] = name;
168         }
170         datalst[i] = NULL;
171         return datalst;
172     }
174     return NULL;
177 /*
178   Local Variables:
179   mode:c++
180   c-file-style:"stroustrup"
181   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
182   indent-tabs-mode:nil
183   fill-column:99
184   End:
185 */
186 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :