Code

guidelines: minor corrections
[inkscape.git] / src / xml / repr.h
1 #ifndef __SP_REPR_H__
2 #define __SP_REPR_H__
4 /** \file
5  * C facade to Inkscape::XML::Node.
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *
10  * Copyright (C) 1999-2002 authors
11  * Copyright (C) 2000-2002 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #include <stdio.h>
17 #include <glib/gtypes.h>
18 #include "gc-anchored.h"
20 #include "xml/node.h"
21 #include "xml/document.h"
22 #include "xml/sp-css-attr.h"
24 #include <2geom/point.h>
26 #define SP_SODIPODI_NS_URI "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
27 #define SP_BROKEN_SODIPODI_NS_URI "http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd"
28 #define SP_INKSCAPE_NS_URI "http://www.inkscape.org/namespaces/inkscape"
29 #define SP_XLINK_NS_URI "http://www.w3.org/1999/xlink"
30 #define SP_SVG_NS_URI "http://www.w3.org/2000/svg"
31 #define SP_RDF_NS_URI "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
32 #define SP_CC_NS_URI "http://creativecommons.org/ns#"
33 #define SP_OLD_CC_NS_URI "http://web.resource.org/cc/"
34 #define SP_DC_NS_URI "http://purl.org/dc/elements/1.1/"
36 /**
37  * \note NB! Unless explicitly stated all methods are noref/nostrcpy
38  */
40 /** \todo
41  * Though Inkscape::XML::Node provides "signals" for notification when 
42  * individual nodes change, there is no mechanism to receive notification 
43  * for overall document changes.
44  * However, with the addition of the transactions code, it would not be
45  * very hard to implement if you wanted it.
46  * 
47  * \class Inkscape::XML::Node
48  * \note
49  * Inkscape::XML::Node itself doesn't use GObject signals at present -- 
50  * Inkscape::XML::Nodes maintain lists of Inkscape::XML::NodeEventVectors 
51  * (added via sp_repr_add_listener), which are used to specify callbacks 
52  * when something changes.
53  *
54  * Here are the current callbacks in an event vector (they may be NULL):
55  *
56  * void (* child_added)(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, 
57  * Inkscape::XML::Node *ref, void *data); Called once a child has been added.
58  *
59  * void (* child_removed)(Inkscape::XML::Node *repr, 
60  * Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
61  * Called after a child is removed; ref is the child that used to precede
62  * the removed child.
63  *
64  * void (* attr_changed)(Inkscape::XML::Node *repr, gchar const *key, 
65  * gchar const *oldval, gchar const *newval, void *data);
66  * Called after an attribute has been changed.
67  *
68  * void (* content_changed)(Inkscape::XML::Node *repr, gchar const *oldcontent,
69  * gchar const *newcontent, void *data);
70  * Called after an element's content has been changed.
71  *
72  * void (* order_changed)(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
73  * Inkscape::XML::Node *oldref, Inkscape::XML::Node *newref, void *data);
74  * Called once the child has been moved to its new position in the child order.
75  *
76  * <b> Inkscape::XML::Node mini-FAQ </b>
77  *
78  * Since I'm not very familiar with this section of code but I need to use
79  * it heavily for the RDF work, I'm going to answer various questions I run
80  * into with my best-guess answers so others can follow along too.
81  *
82  * \arg
83  * Q: How do I find the root Inkscape::XML::Node? <br>
84  * A: If you have an SPDocument, use doc->rroot.  For example: 
85  * 
86  * \code    SP_ACTIVE_DOCUMENT->rroot       \endcode <br>
87  * 
88  * (but it's better to arrange for your caller to pass in the relevent
89  * document rather than assuming it's necessarily the active one and
90  * using SP_ACTIVE_DOCUMENT)
91  *
92  * \arg
93  * Q: How do I find an Inkscape::XML::Node by unique key/value? <br>
94  * A: Use sp_repr_lookup_child
95  *
96  * \arg
97  * Q: How do I find an Inkscape::XML::Node by unique namespace name? <br>
98  * A: Use sp_repr_lookup_name
99  *
100  * \arg
101  * Q: How do I make an Inkscape::XML::Node namespace prefix constant in 
102  * the application? <br>
103  * A: Add the XML namespace URL as a #define to repr.h at the top with the
104  * other SP_<NAMESPACE>_NS_URI #define's, and then in repr-util.cpp,
105  * in sp_xml_ns_register_defaults, bump "defaults" up in size one, and
106  * add another section.  Don't forget to increment the array offset and
107  * keep ".next" pointed to the next (if any) array entry.
108  *
109  * \arg
110  * Q: How do I create a new Inkscape::XML::Node? <br>
111  * A: Use the appropriate create* method on Inkscape::XML::Document,
112  * parent->appendChild(child), and then use Inkscape::GC::release(child) to
113  * let go of it (the parent will hold it in memory).
114  *
115  * \arg
116  * Q: How do I destroy an Inkscape::XML::Node?
117  * A: Just call "sp_repr_unparent" on it and release any references
118  * you may be retaining to it.  Any attached SPObjects will
119  * clean themselves up automatically, as will any children.
120  *
121  * \arg
122  * Q: What about listeners? <br>
123  * A: I have no idea yet...
124  *
125  * \arg
126  * Q: How do I add a namespace to a newly created document?  <br>
127  * A: The current hack is in document.cpp:sp_document_create
128  *
129  * Kees Cook  2004-07-01, updated MenTaLguY 2005-01-25
130  */
132 namespace Geom {
133 class Point;
136 /* SPXMLNs */
137 char const *sp_xml_ns_uri_prefix(gchar const *uri, gchar const *suggested);
138 char const *sp_xml_ns_prefix_uri(gchar const *prefix);
140 Inkscape::XML::Document *sp_repr_document_new(gchar const *rootname);
142 /* Contents */
143 /// Sets the node's \a key attribute to \a value.
144 inline unsigned sp_repr_set_attr(Inkscape::XML::Node *repr, gchar const *key, gchar const *value,
145                                  bool is_interactive = false) {
146     repr->setAttribute(key, value, is_interactive);
147     return true;
150 /* Tree */
151 /// Returns the node's parent.
152 inline Inkscape::XML::Node *sp_repr_parent(Inkscape::XML::Node const *repr) {
153     return const_cast<Inkscape::XML::Node *>(repr->parent());
156 /// Returns first child of node, resets iterator.
157 inline Inkscape::XML::Node const *sp_repr_children(Inkscape::XML::Node const *repr) {
158     return ( repr ? repr->firstChild() : NULL );
161 /// Returns first child of node, resets iterator.
162 inline Inkscape::XML::Node *sp_repr_children(Inkscape::XML::Node *repr) {
163     return ( repr ? repr->firstChild() : NULL );
166 /// Returns next child of node or NULL.
167 inline Inkscape::XML::Node const *sp_repr_next(Inkscape::XML::Node const *repr) {
168     return ( repr ? repr->next() : NULL );
171 /// Returns next child of node or NULL.
172 inline Inkscape::XML::Node *sp_repr_next(Inkscape::XML::Node *repr) {
173     return ( repr ? repr->next() : NULL );
176 /* IO */
178 Inkscape::XML::Document *sp_repr_read_file(gchar const *filename, gchar const *default_ns);
179 Inkscape::XML::Document *sp_repr_read_mem(gchar const *buffer, int length, gchar const *default_ns);
180 void sp_repr_save_stream(Inkscape::XML::Document *doc, FILE *to_file, gchar const *default_ns=NULL, bool compress = false);
181 bool sp_repr_save_file(Inkscape::XML::Document *doc, gchar const *filename, gchar const *default_ns=NULL);
183 void sp_repr_print(Inkscape::XML::Node *repr);
185 /* CSS stuff */
187 SPCSSAttr *sp_repr_css_attr_new(void);
188 void sp_repr_css_attr_unref(SPCSSAttr *css);
189 SPCSSAttr *sp_repr_css_attr(Inkscape::XML::Node *repr, gchar const *attr);
190 SPCSSAttr *sp_repr_css_attr_inherited(Inkscape::XML::Node *repr, gchar const *attr);
192 gchar const *sp_repr_css_property(SPCSSAttr *css, gchar const *name, gchar const *defval);
193 void sp_repr_css_set_property(SPCSSAttr *css, gchar const *name, gchar const *value);
194 void sp_repr_css_unset_property(SPCSSAttr *css, gchar const *name);
195 bool sp_repr_css_property_is_unset(SPCSSAttr *css, gchar const *name);
196 double sp_repr_css_double_property(SPCSSAttr *css, gchar const *name, double defval);
198 gchar *sp_repr_css_write_string(SPCSSAttr *css);
199 void sp_repr_css_set(Inkscape::XML::Node *repr, SPCSSAttr *css, gchar const *key);
200 void sp_repr_css_merge(SPCSSAttr *dst, SPCSSAttr *src);
201 void sp_repr_css_attr_add_from_string(SPCSSAttr *css, const gchar *data);
202 void sp_repr_css_change(Inkscape::XML::Node *repr, SPCSSAttr *css, gchar const *key);
203 void sp_repr_css_change_recursive(Inkscape::XML::Node *repr, SPCSSAttr *css, gchar const *key);
205 void sp_repr_css_print(SPCSSAttr *css);
207 /* Utility finctions */
208 /// Remove \a repr from children of its parent node.
209 inline void sp_repr_unparent(Inkscape::XML::Node *repr) {
210     Inkscape::XML::Node *parent=repr->parent();
211     if (parent) {
212         parent->removeChild(repr);
213     }
216 /* Convenience */
217 unsigned sp_repr_get_boolean(Inkscape::XML::Node *repr, gchar const *key, unsigned *val);
218 unsigned sp_repr_get_int(Inkscape::XML::Node *repr, gchar const *key, int *val);
219 unsigned sp_repr_get_double(Inkscape::XML::Node *repr, gchar const *key, double *val);
220 unsigned sp_repr_set_boolean(Inkscape::XML::Node *repr, gchar const *key, unsigned val);
221 unsigned sp_repr_set_int(Inkscape::XML::Node *repr, gchar const *key, int val);
222 unsigned sp_repr_set_css_double(Inkscape::XML::Node *repr, gchar const *key, double val);
223 unsigned sp_repr_set_svg_double(Inkscape::XML::Node *repr, gchar const *key, double val);
224 unsigned sp_repr_set_point(Inkscape::XML::Node *repr, gchar const *key, Geom::Point val);
225 unsigned sp_repr_get_point(Inkscape::XML::Node *repr, gchar const *key, Geom::Point *val);
227 /// \deprecated !
228 double sp_repr_get_double_attribute(Inkscape::XML::Node *repr, gchar const *key, double def);
229 /// \deprecated !
230 long long int sp_repr_get_int_attribute(Inkscape::XML::Node *repr, gchar const *key, long long int def);
231 /* End Deprecated? */
233 int sp_repr_compare_position(Inkscape::XML::Node *first, Inkscape::XML::Node *second);
235 /* Searching */
236 Inkscape::XML::Node *sp_repr_lookup_name(Inkscape::XML::Node *repr,
237                                          gchar const *name,
238                                          gint maxdepth = -1);
239 Inkscape::XML::Node *sp_repr_lookup_child(Inkscape::XML::Node *repr,
240                                           gchar const *key,
241                                           gchar const *value);
244 inline Inkscape::XML::Node *sp_repr_document_first_child(Inkscape::XML::Document const *doc) {
245     return const_cast<Inkscape::XML::Node *>(doc->firstChild());
248 #endif
251 /*
252   Local Variables:
253   mode:c++
254   c-file-style:"stroustrup"
255   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
256   indent-tabs-mode:nil
257   fill-column:99
258   End:
259 */
260 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :