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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// SPDX-License-Identifier: GPL-2.0-or-later
/** \file
* Notebook and NotebookPage parameters for extensions.
*/
/*
* Authors:
* Johan Engelen <johan@shouraizou.nl>
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright (C) 2006 Author
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include "parameter-notebook.h"
#include <unordered_set>
#include <gtkmm/box.h>
#include <gtkmm/notebook.h>
#include "preferences.h"
#include "extension/extension.h"
#include "xml/node.h"
namespace Inkscape {
namespace Extension {
ParamNotebook::ParamNotebookPage::ParamNotebookPage(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext)
: InxParameter(xml, ext)
{
// Read XML tree of page and parse parameters
if (xml) {
Inkscape::XML::Node *child_repr = xml->firstChild();
while (child_repr) {
const char *chname = child_repr->name();
if (!strncmp(chname, INKSCAPE_EXTENSION_NS_NC, strlen(INKSCAPE_EXTENSION_NS_NC))) {
chname += strlen(INKSCAPE_EXTENSION_NS);
}
if (chname[0] == '_') { // allow leading underscore in tag names for backwards-compatibility
chname++;
}
if (InxWidget::is_valid_widget_name(chname)) {
InxWidget *widget = InxWidget::make(child_repr, _extension);
if (widget) {
_children.push_back(widget);
}
} else if (child_repr->type() == XML::NodeType::ELEMENT_NODE) {
g_warning("Invalid child element ('%s') in notebook page in extension '%s'.",
chname, _extension->get_id());
} else if (child_repr->type() != XML::NodeType::COMMENT_NODE){
g_warning("Invalid child element found in notebook page in extension '%s'.", _extension->get_id());
}
child_repr = child_repr->next();
}
}
}
/**
* Creates a notebookpage widget for a notebook.
*
* Builds a notebook page (a vbox) and puts parameters on it.
*/
Gtk::Widget *ParamNotebook::ParamNotebookPage::get_widget(sigc::signal<void> *changeSignal)
{
if (_hidden) {
return nullptr;
}
Gtk::Box * vbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
vbox->set_border_width(GUI_BOX_MARGIN);
vbox->set_spacing(GUI_BOX_SPACING);
// add parameters onto page (if any)
for (auto child : _children) {
Gtk::Widget *child_widget = child->get_widget(changeSignal);
if (child_widget) {
int indent = child->get_indent();
child_widget->set_margin_start(indent *GUI_INDENTATION);
vbox->pack_start(*child_widget, false, true, 0); // fill=true does not have an effect here, but allows the
// child to choose to expand by setting hexpand/vexpand
const char *tooltip = child->get_tooltip();
if (tooltip) {
child_widget->set_tooltip_text(tooltip);
}
}
}
vbox->show();
return dynamic_cast<Gtk::Widget *>(vbox);
}
/** End ParamNotebookPage **/
/** ParamNotebook **/
ParamNotebook::ParamNotebook(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext)
: InxParameter(xml, ext)
{
// Read XML tree to add pages (allow _page for backwards compatibility)
if (xml) {
Inkscape::XML::Node *child_repr = xml->firstChild();
while (child_repr) {
const char *chname = child_repr->name();
if (chname && (!strcmp(chname, INKSCAPE_EXTENSION_NS "page") ||
!strcmp(chname, INKSCAPE_EXTENSION_NS "_page") )) {
ParamNotebookPage *page;
page = new ParamNotebookPage(child_repr, ext);
if (page) {
_children.push_back(page);
}
} else if (child_repr->type() == XML::NodeType::ELEMENT_NODE) {
g_warning("Invalid child element ('%s') for parameter '%s' in extension '%s'. Expected 'page'.",
chname, _name, _extension->get_id());
} else if (child_repr->type() != XML::NodeType::COMMENT_NODE){
g_warning("Invalid child element found in parameter '%s' in extension '%s'. Expected 'page'.",
_name, _extension->get_id());
}
child_repr = child_repr->next();
}
}
if (_children.empty()) {
g_warning("No (valid) pages for parameter '%s' in extension '%s'", _name, _extension->get_id());
}
// check for duplicate page names
std::unordered_set<std::string> names;
for (auto child : _children) {
ParamNotebookPage *page = static_cast<ParamNotebookPage *>(child);
auto ret = names.emplace(page->_name);
if (!ret.second) {
g_warning("Duplicate page name ('%s') for parameter '%s' in extension '%s'.",
page->_name, _name, _extension->get_id());
}
}
// get value (initialize with value of first page if pref is empty)
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
_value = prefs->getString(pref_name());
if (_value.empty()) {
if (!_children.empty()) {
ParamNotebookPage *first_page = dynamic_cast<ParamNotebookPage *>(_children[0]);
_value = first_page->_name;
}
}
}
/**
* A function to set the \c _value.
*
* This function sets the internal value, but it also sets the value
* in the preferences structure. To put it in the right place \c pref_name() is used.
*
* @param in The number of the page to set as new value.
*/
const Glib::ustring& ParamNotebook::set(const int in)
{
int i = in < _children.size() ? in : _children.size()-1;
ParamNotebookPage *page = dynamic_cast<ParamNotebookPage *>(_children[i]);
if (page) {
_value = page->_name;
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
prefs->setString(pref_name(), _value);
}
return _value;
}
std::string ParamNotebook::value_to_string() const
{
return _value.raw();
}
/** A special category of Gtk::Notebook to handle notebook parameters. */
class NotebookWidget : public Gtk::Notebook {
private:
ParamNotebook *_pref;
public:
/**
* Build a notebookpage preference for the given parameter.
* @param pref Where to get the string (pagename) from, and where to put it when it changes.
*/
NotebookWidget(ParamNotebook *pref)
: Gtk::Notebook()
, _pref(pref)
, activated(false)
{
// don't have to set the correct page: this is done in ParamNotebook::get_widget hook function
this->signal_switch_page().connect(sigc::mem_fun(this, &NotebookWidget::changed_page));
}
void changed_page(Gtk::Widget *page, guint pagenum);
bool activated;
};
/**
* Respond to the selected page of notebook changing.
* This function responds to the changing by reporting it to
* ParamNotebook. The change is only reported when the notebook
* is actually visible. This to exclude 'fake' changes when the
* notebookpages are added or removed.
*/
void NotebookWidget::changed_page(Gtk::Widget * /*page*/, guint pagenum)
{
if (get_visible()) {
_pref->set((int)pagenum);
}
}
/**
* Creates a Notebook widget for a notebook parameter.
*
* Builds a notebook and puts pages in it.
*/
Gtk::Widget *ParamNotebook::get_widget(sigc::signal<void> *changeSignal)
{
if (_hidden) {
return nullptr;
}
NotebookWidget *notebook = Gtk::manage(new NotebookWidget(this));
// add pages (if any) and switch to previously selected page
int current_page = -1;
int selected_page = -1;
for (auto child : _children) {
ParamNotebookPage *page = dynamic_cast<ParamNotebookPage *>(child);
g_assert(child); // A ParamNotebook has only children of type ParamNotebookPage.
// If we receive a non-page child here something is very wrong!
current_page++;
Gtk::Widget *page_widget = page->get_widget(changeSignal);
Glib::ustring page_text = page->_text;
if (page->_translatable != NO) { // translate unless explicitly marked untranslatable
page_text = page->get_translation(page_text.c_str());
}
notebook->append_page(*page_widget, page_text);
if (_value == page->_name) {
selected_page = current_page;
}
}
if (selected_page >= 0) {
notebook->set_current_page(selected_page);
}
notebook->show();
return static_cast<Gtk::Widget *>(notebook);
}
} // namespace Extension
} // 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 :
|