summaryrefslogtreecommitdiffstats
path: root/src/object/sp-tag-use.cpp
blob: 5f8d44502a87d69d57495502d41a28ac905fc0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * SVG <inkscape:tagref> implementation
 *
 * Authors:
 *   Theodore Janeczko
 *   Liam P White
 *
 * Copyright (C) Theodore Janeczko 2012-2014 <flutterguy317@gmail.com>
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "sp-tag-use.h"

#include <cstring>
#include <string>

#include <glibmm/i18n.h>

#include "bad-uri-exception.h"
#include "display/drawing-group.h"
#include "attributes.h"
#include "document.h"
#include "uri.h"
#include "xml/repr.h"
#include "preferences.h"
#include "style.h"
#include "sp-factory.h"
#include "sp-symbol.h"
#include "sp-tag-use-reference.h"

SPTagUse::SPTagUse()
{
    href = nullptr;
    //new (_changed_connection) sigc::connection;
    ref = new SPTagUseReference(this);
    
    _changed_connection = ref->changedSignal().connect(sigc::mem_fun(*this, &SPTagUse::href_changed));
}

SPTagUse::~SPTagUse()
{
    ref->detach();
    delete ref;
    ref = nullptr;
}

void
SPTagUse::build(SPDocument *document, Inkscape::XML::Node *repr)
{
    SPObject::build(document, repr);
    readAttr( "xlink:href" );

    // We don't need to create child here:
    // reading xlink:href will attach ref, and that will cause the changed signal to be emitted,
    // which will call sp_tag_use_href_changed, and that will take care of the child
}

void
SPTagUse::release()
{
    _changed_connection.disconnect();

    g_free(href);
    href = nullptr;

    ref->detach();

    SPObject::release();
}

void
SPTagUse::set(SPAttributeEnum key, gchar const *value)
{

    switch (key) {
        case SP_ATTR_XLINK_HREF: {
            if ( value && href && ( strcmp(value, href) == 0 ) ) {
                /* No change, do nothing. */
            } else {
                g_free(href);
                href = nullptr;
                if (value) {
                    // First, set the href field, because sp_tag_use_href_changed will need it.
                    href = g_strdup(value);

                    // Now do the attaching, which emits the changed signal.
                    try {
                        ref->attach(Inkscape::URI(value));
                    } catch (Inkscape::BadURIException &e) {
                        g_warning("%s", e.what());
                        ref->detach();
                    }
                } else {
                    ref->detach();
                }
            }
            break;
        }

        default:
                SPObject::set(key, value);
            break;
    }
}

Inkscape::XML::Node *
SPTagUse::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
{
    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
        repr = xml_doc->createElement("inkscape:tagref");
    }

    SPObject::write(xml_doc, repr, flags);

    return repr;
}

void
SPTagUse::href_changed(SPObject *old_ref, SPObject *new_ref)
{
    if (old_ref && getRepr()) {
        auto const id = old_ref->getAttribute("id");
        if (id) {
            getRepr()->setAttribute("xlink:href", Glib::ustring("#") + id);
        }
    }
}

SPItem * SPTagUse::get_original()
{
    SPItem *ref_ = nullptr;
    if (ref) {
        ref_ = ref->getObject();
    }
    return ref_;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :