Code

Set up toolbox so that paint bucket defaults can be reset
[inkscape.git] / src / sp-anchor.cpp
1 #define __SP_ANCHOR_C__
3 /*
4  * SVG <a> element implementation
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2001-2002 Lauris Kaplinski
10  * Copyright (C) 2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #define noSP_ANCHOR_VERBOSE
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <glibmm/i18n.h>
22 #include "xml/quote.h"
23 #include "xml/repr.h"
24 #include "attributes.h"
25 #include "sp-anchor.h"
26 #include "ui/view/view.h"
27 #include "document.h"
29 static void sp_anchor_class_init(SPAnchorClass *ac);
30 static void sp_anchor_init(SPAnchor *anchor);
32 static void sp_anchor_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
33 static void sp_anchor_release(SPObject *object);
34 static void sp_anchor_set(SPObject *object, unsigned int key, const gchar *value);
35 static Inkscape::XML::Node *sp_anchor_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
37 static gchar *sp_anchor_description(SPItem *item);
38 static gint sp_anchor_event(SPItem *item, SPEvent *event);
40 static SPGroupClass *parent_class;
42 GType sp_anchor_get_type(void)
43 {
44     static GType type = 0;
46     if (!type) {
47         GTypeInfo info = {
48             sizeof(SPAnchorClass),
49             NULL,       /* base_init */
50             NULL,       /* base_finalize */
51             (GClassInitFunc) sp_anchor_class_init,
52             NULL,       /* class_finalize */
53             NULL,       /* class_data */
54             sizeof(SPAnchor),
55             16, /* n_preallocs */
56             (GInstanceInitFunc) sp_anchor_init,
57             NULL,       /* value_table */
58         };
59         type = g_type_register_static(SP_TYPE_GROUP, "SPAnchor", &info, (GTypeFlags) 0);
60     }
62     return type;
63 }
65 static void sp_anchor_class_init(SPAnchorClass *ac)
66 {
67     SPObjectClass *sp_object_class = (SPObjectClass *) ac;
68     SPItemClass *item_class = (SPItemClass *) ac;
70     parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
72     sp_object_class->build = sp_anchor_build;
73     sp_object_class->release = sp_anchor_release;
74     sp_object_class->set = sp_anchor_set;
75     sp_object_class->write = sp_anchor_write;
77     item_class->description = sp_anchor_description;
78     item_class->event = sp_anchor_event;
79 }
81 static void sp_anchor_init(SPAnchor *anchor)
82 {
83     anchor->href = NULL;
84 }
86 static void sp_anchor_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
87 {
88     if (((SPObjectClass *) (parent_class))->build) {
89         ((SPObjectClass *) (parent_class))->build(object, document, repr);
90     }
92     sp_object_read_attr(object, "xlink:type");
93     sp_object_read_attr(object, "xlink:role");
94     sp_object_read_attr(object, "xlink:arcrole");
95     sp_object_read_attr(object, "xlink:title");
96     sp_object_read_attr(object, "xlink:show");
97     sp_object_read_attr(object, "xlink:actuate");
98     sp_object_read_attr(object, "xlink:href");
99     sp_object_read_attr(object, "target");
102 static void sp_anchor_release(SPObject *object)
104     SPAnchor *anchor = SP_ANCHOR(object);
106     if (anchor->href) {
107         g_free(anchor->href);
108         anchor->href = NULL;
109     }
111     if (((SPObjectClass *) parent_class)->release) {
112         ((SPObjectClass *) parent_class)->release(object);
113     }
116 static void sp_anchor_set(SPObject *object, unsigned int key, const gchar *value)
118     SPAnchor *anchor = SP_ANCHOR(object);
120     switch (key) {
121         case SP_ATTR_XLINK_HREF:
122             g_free(anchor->href);
123             anchor->href = g_strdup(value);
124             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
125             break;
126         case SP_ATTR_XLINK_TYPE:
127         case SP_ATTR_XLINK_ROLE:
128         case SP_ATTR_XLINK_ARCROLE:
129         case SP_ATTR_XLINK_TITLE:
130         case SP_ATTR_XLINK_SHOW:
131         case SP_ATTR_XLINK_ACTUATE:
132         case SP_ATTR_TARGET:
133             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
134             break;
135         default:
136             if (((SPObjectClass *) (parent_class))->set) {
137                 ((SPObjectClass *) (parent_class))->set(object, key, value);
138             }
139             break;
140     }
144 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
146 static Inkscape::XML::Node *sp_anchor_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
148     SPAnchor *anchor = SP_ANCHOR(object);
150     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
151         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
152         repr = xml_doc->createElement("svg:a");
153     }
155     repr->setAttribute("xlink:href", anchor->href);
157     if (repr != SP_OBJECT_REPR(object)) {
158         COPY_ATTR(repr, object->repr, "xlink:type");
159         COPY_ATTR(repr, object->repr, "xlink:role");
160         COPY_ATTR(repr, object->repr, "xlink:arcrole");
161         COPY_ATTR(repr, object->repr, "xlink:title");
162         COPY_ATTR(repr, object->repr, "xlink:show");
163         COPY_ATTR(repr, object->repr, "xlink:actuate");
164         COPY_ATTR(repr, object->repr, "target");
165     }
167     if (((SPObjectClass *) (parent_class))->write) {
168         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
169     }
171     return repr;
174 static gchar *sp_anchor_description(SPItem *item)
176     SPAnchor *anchor = SP_ANCHOR(item);
177     if (anchor->href) {
178         char *quoted_href = xml_quote_strdup(anchor->href);
179         char *ret = g_strdup_printf(_("<b>Link</b> to %s"), quoted_href);
180         g_free(quoted_href);
181         return ret;
182     } else {
183         return g_strdup (_("<b>Link</b> without URI"));
184     }
187 /* fixme: We should forward event to appropriate container/view */
189 static gint sp_anchor_event(SPItem *item, SPEvent *event)
191     SPAnchor *anchor = SP_ANCHOR(item);
193     switch (event->type) {
194         case SP_EVENT_ACTIVATE:
195             if (anchor->href) {
196                 g_print("Activated xlink:href=\"%s\"\n", anchor->href);
197                 return TRUE;
198             }
199             break;
200         case SP_EVENT_MOUSEOVER:
201             (static_cast<Inkscape::UI::View::View*>(event->data))->mouseover();
202             break;
203         case SP_EVENT_MOUSEOUT:
204             (static_cast<Inkscape::UI::View::View*>(event->data))->mouseout();
205             break;
206         default:
207             break;
208     }
210     return FALSE;
213 /*
214   Local Variables:
215   mode:c++
216   c-file-style:"stroustrup"
217   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218   indent-tabs-mode:nil
219   fill-column:99
220   End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :