summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/entity-entry.cpp
blob: cfff80f598743fe8f66872de972840f2c0d36571 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   bulia byak <buliabyak@users.sf.net>
 *   Bryce W. Harrington <bryce@bryceharrington.org>
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   Jon Phillips <jon@rejon.org>
 *   Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
 *   Jon A. Cruz <jon@joncruz.org>
 *   Abhishek Sharma
 *
 * Copyright (C) 2000 - 2005 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "entity-entry.h"

#include <gtkmm/scrolledwindow.h>
#include <gtkmm/entry.h>

#include "document-undo.h"
#include "inkscape.h"
#include "preferences.h"
#include "rdf.h"

#include "object/sp-root.h"

#include "ui/widget/registry.h"

namespace Inkscape {
namespace UI {
namespace Widget {

//===================================================

//---------------------------------------------------

EntityEntry*
EntityEntry::create (rdf_work_entity_t* ent, Registry& wr)
{
    g_assert (ent);
    EntityEntry* obj = nullptr;
    switch (ent->format)
    {
        case RDF_FORMAT_LINE: 
            obj = new EntityLineEntry (ent, wr);
            break;
        case RDF_FORMAT_MULTILINE: 
            obj = new EntityMultiLineEntry (ent, wr);
            break;
        default:
            g_warning ("An unknown RDF format was requested.");
    }

    g_assert (obj);
    obj->_label.show();
    return obj;
}

EntityEntry::EntityEntry (rdf_work_entity_t* ent, Registry& wr)
    : _label(Glib::ustring(_(ent->title)), Gtk::ALIGN_END),
      _packable(nullptr),
      _entity(ent), _wr(&wr)
{
}

EntityEntry::~EntityEntry()
{
    _changed_connection.disconnect();
}

void EntityEntry::save_to_preferences(SPDocument *doc)
{
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    const gchar *text = rdf_get_work_entity (doc, _entity);
    prefs->setString(PREFS_METADATA + Glib::ustring(_entity->name), Glib::ustring(text ? text : ""));
}

EntityLineEntry::EntityLineEntry (rdf_work_entity_t* ent, Registry& wr)
: EntityEntry (ent, wr)
{
    Gtk::Entry *e = new Gtk::Entry;
    e->set_tooltip_text (_(ent->tip));
    _packable = e;
    _changed_connection = e->signal_changed().connect (sigc::mem_fun (*this, &EntityLineEntry::on_changed));
}

EntityLineEntry::~EntityLineEntry()
{
    delete static_cast<Gtk::Entry*>(_packable);
}

void EntityLineEntry::update(SPDocument* doc, bool read_only)
{
    const char *text = rdf_get_work_entity(doc, _entity);
    // If RDF title is not set, get the document's <title> and set the RDF:
    if (!text && !strcmp(_entity->name, "title") && doc->getRoot()) {
        text = doc->getRoot()->title();
        if (!read_only) {
            rdf_set_work_entity(doc, _entity, text);
        }
    }
    static_cast<Gtk::Entry*>(_packable)->set_text(text ? text : "");
}


void EntityLineEntry::load_from_preferences()
{
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    Glib::ustring text = prefs->getString(PREFS_METADATA + Glib::ustring(_entity->name));
    if (text.length() > 0) {
        static_cast<Gtk::Entry*>(_packable)->set_text (text.c_str());
    }
}

void
EntityLineEntry::on_changed()
{
    if (_wr->isUpdating() || !_wr->desktop())
        return;

    _wr->setUpdating (true);
    SPDocument *doc = _wr->desktop()->getDocument();
    Glib::ustring text = static_cast<Gtk::Entry*>(_packable)->get_text();
    if (rdf_set_work_entity (doc, _entity, text.c_str())) {
        if (doc->isSensitive()) {
            DocumentUndo::done(doc, "Document metadata updated", "");
        }
    }
    _wr->setUpdating (false);
}

Glib::ustring EntityLineEntry::content() const {
    return static_cast<Gtk::Entry*>(_packable)->get_text();
}

EntityMultiLineEntry::EntityMultiLineEntry (rdf_work_entity_t* ent, Registry& wr)
: EntityEntry (ent, wr)
{
    Gtk::ScrolledWindow *s = new Gtk::ScrolledWindow;
    s->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
    s->set_shadow_type (Gtk::SHADOW_IN);
    _packable = s;
    _v.set_size_request (-1, 35);
    _v.set_wrap_mode (Gtk::WRAP_WORD);
    _v.set_accepts_tab (false);
    s->add (_v);
    _v.set_tooltip_text (_(ent->tip));
    _changed_connection = _v.get_buffer()->signal_changed().connect (sigc::mem_fun (*this, &EntityMultiLineEntry::on_changed));
}

Glib::ustring EntityMultiLineEntry::content() const {
    return _v.get_buffer()->get_text();
}

EntityMultiLineEntry::~EntityMultiLineEntry()
{
    delete static_cast<Gtk::ScrolledWindow*>(_packable);
}

void EntityMultiLineEntry::update(SPDocument* doc, bool read_only)
{
    const char *text = rdf_get_work_entity(doc, _entity);
    // If RDF title is not set, get the document's <title> and set the RDF:
    if (!text && !strcmp(_entity->name, "title") && doc->getRoot()) {
        text = doc->getRoot()->title();
        if (!read_only) {
            rdf_set_work_entity(doc, _entity, text);
        }
    }
    Gtk::ScrolledWindow *s = static_cast<Gtk::ScrolledWindow*>(_packable);
    Gtk::TextView *tv = static_cast<Gtk::TextView*>(s->get_child());
    tv->get_buffer()->set_text(text ? text : "");
}


void EntityMultiLineEntry::load_from_preferences()
{
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    Glib::ustring text = prefs->getString(PREFS_METADATA + Glib::ustring(_entity->name));
    if (text.length() > 0) {
        Gtk::ScrolledWindow *s = static_cast<Gtk::ScrolledWindow*>(_packable);
        Gtk::TextView *tv = static_cast<Gtk::TextView*>(s->get_child());
        tv->get_buffer()->set_text (text.c_str());
    }
}


void
EntityMultiLineEntry::on_changed()
{
    if (_wr->isUpdating() || !_wr->desktop())
        return;

    _wr->setUpdating (true);
    SPDocument *doc = _wr->desktop()->getDocument();
    Gtk::ScrolledWindow *s = static_cast<Gtk::ScrolledWindow*>(_packable);
    Gtk::TextView *tv = static_cast<Gtk::TextView*>(s->get_child());
    Glib::ustring text = tv->get_buffer()->get_text();
    if (rdf_set_work_entity (doc, _entity, text.c_str())) {
        DocumentUndo::done(doc, "Document metadata updated", "");
    }
    _wr->setUpdating (false);
}

} // namespace Dialog
} // namespace UI
} // namespace Inkscape

/*
  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:fileencoding=utf-8:textwidth=99 :