Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-anchor.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * SVG <a> element implementation
4 *
5 * Author:
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * Abhishek Sharma
8 *
9 * Copyright (C) 2017 Martin Owens
10 * Copyright (C) 2001-2002 Lauris Kaplinski
11 * Copyright (C) 2001 Ximian, Inc.
12 *
13 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14 */
15
16#define noSP_ANCHOR_VERBOSE
17
18#include <glibmm/i18n.h>
19
20#include "attributes.h"
21#include "document.h"
22#include "sp-anchor.h"
23
24#include "xml/document.h" // for Document
25#include "xml/href-attribute-helper.h" // for setHrefAttribute
26#include "xml/quote.h" // for xml_quote_strdup
27
40
42 if (this->href) {
43 g_free(this->href);
44 this->href = nullptr;
45 }
46 if (this->type) {
47 g_free(this->type);
48 this->type = nullptr;
49 }
50 if (this->title) {
51 g_free(this->title);
52 this->title = nullptr;
53 }
54 if (this->page) {
55 g_free(this->page);
56 this->page = nullptr;
57 }
58
60}
61
62void SPAnchor::set(SPAttr key, const gchar* value) {
63 switch (key) {
65 g_free(this->href);
66 this->href = g_strdup(value);
67 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
68 this->updatePageAnchor();
69 break;
71 g_free(this->type);
72 this->type = g_strdup(value);
73 this->updatePageAnchor();
74 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
75 break;
79 g_free(this->title);
80 this->title = g_strdup(value);
81 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
82 break;
85 case SPAttr::TARGET:
86 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
87 break;
88
89 default:
90 SPGroup::set(key, value);
91 break;
92 }
93}
94
95/*
96 * Detect if this anchor qualifies as a page link and append
97 * the new page document to this document.
98 */
100 if (this->type && !strcmp(this->type, "page")) {
101 local_link.reset();
102 if (this->href && !this->page) {
103 this->page = this->document->createChildDoc(this->href);
104 }
105 } else if (this->href) {
106 local_link = std::make_unique<Inkscape::URIReference>(this);
107 local_link->try_attach(this->href);
108 }
109}
110
111#define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
112
114 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
115 repr = xml_doc->createElement("svg:a");
116 }
117
119 if (this->type) repr->setAttribute("xlink:type", this->type);
120 if (this->title) repr->setAttribute("xlink:title", this->title);
121
122 if (repr != this->getRepr()) {
123 // XML Tree being directly used while it shouldn't be in the
124 // below COPY_ATTR lines
125 COPY_ATTR(repr, this->getRepr(), "xlink:role");
126 COPY_ATTR(repr, this->getRepr(), "xlink:arcrole");
127 COPY_ATTR(repr, this->getRepr(), "xlink:show");
128 COPY_ATTR(repr, this->getRepr(), "xlink:actuate");
129 COPY_ATTR(repr, this->getRepr(), "target");
130 }
131
132 SPGroup::write(xml_doc, repr, flags);
133
134 return repr;
135}
136
137const char* SPAnchor::typeName() const {
138 return "link";
139}
140
141const char* SPAnchor::displayName() const {
142 return C_("Hyperlink|Noun", "Link");
143}
144
145gchar* SPAnchor::description() const {
146 if (this->href) {
147 char *quoted_href = xml_quote_strdup(this->href);
148 char *ret = g_strdup_printf(_("to %s"), quoted_href);
149 g_free(quoted_href);
150 return ret;
151 } else {
152 return g_strdup (_("without URI"));
153 }
154}
155
156void SPAnchor::getLinked(std::vector<SPObject *> &objects, LinkedObjectNature direction) const
157{
158 if (direction == LinkedObjectNature::ANY || direction == LinkedObjectNature::DEPENDENCY) {
159 if (auto linked = (local_link ? local_link->getObject() : nullptr)) {
160 objects.emplace_back(linked);
161 }
162 }
163 SPObject::getLinked(objects, direction);
164}
165
166/*
167 Local Variables:
168 mode:c++
169 c-file-style:"stroustrup"
170 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
171 indent-tabs-mode:nil
172 fill-column:99
173 End:
174*/
175// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
Lookup dictionary for attributes/properties.
SPAttr
Definition attributes.h:27
@ XLINK_SHOW
@ XLINK_ARCROLE
@ XLINK_ACTUATE
@ XLINK_HREF
@ XLINK_TITLE
@ XLINK_ROLE
@ XLINK_TYPE
Interface for refcounted XML nodes.
Definition node.h:80
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:25
void set(SPAttr key, char const *value) override
Definition sp-anchor.cpp:62
const char * typeName() const override
The item's type name, not node tag name.
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Definition sp-anchor.cpp:28
void getLinked(std::vector< SPObject * > &objects, LinkedObjectNature direction) const override
Get objects which are linked to this object as either a source or a target.
char * description() const override
const char * displayName() const override
The item's type name as a translated human string.
void release() override
Definition sp-anchor.cpp:41
char * type
Definition sp-anchor.h:26
std::unique_ptr< Inkscape::URIReference > local_link
Definition sp-anchor.h:41
virtual void updatePageAnchor()
Definition sp-anchor.cpp:99
char * href
Definition sp-anchor.h:25
Inkscape::XML::Node * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned flags) override
char * title
Definition sp-anchor.h:27
SPDocument * page
Definition sp-anchor.h:28
Typed SVG document implementation.
Definition document.h:101
SPDocument * createChildDoc(std::string const &filename)
Fetches a document and attaches it to the current document as a child href.
Definition document.cpp:610
void release() override
Inkscape::XML::Node * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned int flags) override
void set(SPAttr key, char const *value) override
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Inkscape::XML::Node * repr
Definition sp-object.h:193
void requestModified(unsigned int flags)
Requests that a modification notification signal be emitted later (e.g.
SPDocument * document
Definition sp-object.h:188
void readAttr(char const *key)
Read value of key attribute from XML node into object.
Inkscape::XML::Node * getRepr()
Returns the XML representation of tree.
virtual void getLinked(std::vector< SPObject * > &objects, LinkedObjectNature direction=LinkedObjectNature::ANY) const
Get objects which are linked to this object as either a source or a target.
LinkedObjectNature
Definition sp-object.h:168
void setHrefAttribute(XML::Node &node, Util::const_char_ptr value)
If the 'href' attribute already exists for the given node, then set a new value for it.
static cairo_user_data_key_t key
char * xml_quote_strdup(char const *src)
Definition quote.cpp:43
TODO: insert short description here.
Interface for XML documents.
Definition document.h:43
virtual Node * createElement(char const *name)=0
Interface for XML documents.