Code

snap indicator: try a diamond shaped indicator for snapping to nodes. see how we...
[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::Document *doc, 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::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
148     SPAnchor *anchor = SP_ANCHOR(object);
150     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
151         repr = xml_doc->createElement("svg:a");
152     }
154     repr->setAttribute("xlink:href", anchor->href);
156     if (repr != SP_OBJECT_REPR(object)) {
157         COPY_ATTR(repr, object->repr, "xlink:type");
158         COPY_ATTR(repr, object->repr, "xlink:role");
159         COPY_ATTR(repr, object->repr, "xlink:arcrole");
160         COPY_ATTR(repr, object->repr, "xlink:title");
161         COPY_ATTR(repr, object->repr, "xlink:show");
162         COPY_ATTR(repr, object->repr, "xlink:actuate");
163         COPY_ATTR(repr, object->repr, "target");
164     }
166     if (((SPObjectClass *) (parent_class))->write) {
167         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
168     }
170     return repr;
173 static gchar *sp_anchor_description(SPItem *item)
175     SPAnchor *anchor = SP_ANCHOR(item);
176     if (anchor->href) {
177         char *quoted_href = xml_quote_strdup(anchor->href);
178         char *ret = g_strdup_printf(_("<b>Link</b> to %s"), quoted_href);
179         g_free(quoted_href);
180         return ret;
181     } else {
182         return g_strdup (_("<b>Link</b> without URI"));
183     }
186 /* fixme: We should forward event to appropriate container/view */
188 static gint sp_anchor_event(SPItem *item, SPEvent *event)
190     SPAnchor *anchor = SP_ANCHOR(item);
192     switch (event->type) {
193         case SP_EVENT_ACTIVATE:
194             if (anchor->href) {
195                 g_print("Activated xlink:href=\"%s\"\n", anchor->href);
196                 return TRUE;
197             }
198             break;
199         case SP_EVENT_MOUSEOVER:
200             (static_cast<Inkscape::UI::View::View*>(event->data))->mouseover();
201             break;
202         case SP_EVENT_MOUSEOUT:
203             (static_cast<Inkscape::UI::View::View*>(event->data))->mouseout();
204             break;
205         default:
206             break;
207     }
209     return FALSE;
212 /*
213   Local Variables:
214   mode:c++
215   c-file-style:"stroustrup"
216   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
217   indent-tabs-mode:nil
218   fill-column:99
219   End:
220 */
221 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :