1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** @file
3 * Parameters for extensions.
4 *//*
5 * Author:
6 * Patrick Storz <eduard.braun2@gmx.de>
7 *
8 * Copyright (C) 2019 Authors
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13 #include "parameter.h"
14 #include "widget.h"
15 #include "widget-box.h"
16 #include "widget-image.h"
17 #include "widget-label.h"
18 #include "widget-separator.h"
19 #include "widget-spacer.h"
20
21 #include <algorithm>
22 #include <cstring>
23
24 #include <sigc++/sigc++.h>
25
26 #include "extension/extension.h"
27
28 #include "xml/node.h"
29
30
31 namespace Inkscape {
32 namespace Extension {
33
make(Inkscape::XML::Node * in_repr,Inkscape::Extension::Extension * in_ext)34 InxWidget *InxWidget::make(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *in_ext)
35 {
36 InxWidget *widget = nullptr;
37
38 const char *name = in_repr->name();
39 if (!strncmp(name, INKSCAPE_EXTENSION_NS_NC, strlen(INKSCAPE_EXTENSION_NS_NC))) {
40 name += strlen(INKSCAPE_EXTENSION_NS);
41 }
42 if (name[0] == '_') { // allow leading underscore in tag names for backwards-compatibility
43 name++;
44 }
45
46 // decide on widget type based on tag name
47 // keep in sync with list of names supported in InxWidget::is_valid_widget_name() below
48 if (!name) {
49 // we can't create a widget without name
50 g_warning("InxWidget without name in extension '%s'.", in_ext->get_id());
51 } else if (!strcmp(name, "hbox") || !strcmp(name, "vbox")) {
52 widget = new WidgetBox(in_repr, in_ext);
53 } else if (!strcmp(name, "image")) {
54 widget = new WidgetImage(in_repr, in_ext);
55 } else if (!strcmp(name, "label")) {
56 widget = new WidgetLabel(in_repr, in_ext);
57 } else if (!strcmp(name, "separator")) {
58 widget = new WidgetSeparator(in_repr, in_ext);
59 } else if (!strcmp(name, "spacer")) {
60 widget = new WidgetSpacer(in_repr, in_ext);
61 } else if (!strcmp(name, "param")) {
62 widget = InxParameter::make(in_repr, in_ext);
63 } else {
64 g_warning("Unknown widget name ('%s') in extension '%s'", name, in_ext->get_id());
65 }
66
67 // Note: widget could equal nullptr
68 return widget;
69 }
70
is_valid_widget_name(const char * name)71 bool InxWidget::is_valid_widget_name(const char *name)
72 {
73 // keep in sync with names supported in InxWidget::make() above
74 static const std::vector<std::string> valid_names =
75 {"hbox", "vbox", "image", "label", "separator", "spacer", "param"};
76
77 if (std::find(valid_names.begin(), valid_names.end(), name) != valid_names.end()) {
78 return true;
79 } else {
80 return false;
81 }
82 }
83
84
InxWidget(Inkscape::XML::Node * in_repr,Inkscape::Extension::Extension * ext)85 InxWidget::InxWidget(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *ext)
86 : _extension(ext)
87 {
88 // translatable (optional)
89 const char *translatable = in_repr->attribute("translatable");
90 if (translatable) {
91 if (!strcmp(translatable, "yes")) {
92 _translatable = YES;
93 } else if (!strcmp(translatable, "no")) {
94 _translatable = NO;
95 } else {
96 g_warning("Invalid value ('%s') for translatable attribute of widget '%s' in extension '%s'",
97 translatable, in_repr->name(), _extension->get_id());
98 }
99 }
100
101 // context (optional)
102 const char *context = in_repr->attribute("context");
103 if (!context) {
104 context = in_repr->attribute("msgctxt"); // backwards-compatibility with previous name
105 }
106 if (context) {
107 _context = g_strdup(context);
108 }
109
110 // gui-hidden (optional)
111 const char *gui_hidden = in_repr->attribute("gui-hidden");
112 if (gui_hidden != nullptr) {
113 if (strcmp(gui_hidden, "true") == 0) {
114 _hidden = true;
115 }
116 }
117
118 // indent (optional)
119 const char *indent = in_repr->attribute("indent");
120 if (indent != nullptr) {
121 _indent = strtol(indent, nullptr, 0);
122 }
123
124 // appearance (optional, does not apply to all parameters)
125 const char *appearance = in_repr->attribute("appearance");
126 if (appearance) {
127 _appearance = g_strdup(appearance);
128 }
129 }
130
~InxWidget()131 InxWidget::~InxWidget()
132 {
133 for (auto child : _children) {
134 delete child;
135 }
136
137 g_free(_context);
138 _context = nullptr;
139
140 g_free(_appearance);
141 _appearance = nullptr;
142 }
143
144 Gtk::Widget *
get_widget(sigc::signal<void> *)145 InxWidget::get_widget(sigc::signal<void> * /*changeSignal*/)
146 {
147 // if we end up here we're missing a definition of ::get_widget() in one of the subclasses
148 g_critical("InxWidget::get_widget called from widget of type '%s' in extension '%s'",
149 typeid(this).name(), _extension->get_id());
150 g_assert_not_reached();
151 return nullptr;
152 }
153
get_translation(const char * msgid)154 const char *InxWidget::get_translation(const char* msgid) {
155 return _extension->get_translation(msgid, _context);
156 }
157
get_widgets(std::vector<InxWidget * > & list)158 void InxWidget::get_widgets(std::vector<InxWidget *> &list)
159 {
160 list.push_back(this);
161 for (auto child : _children) {
162 child->get_widgets(list);
163 }
164 }
165
166 } // namespace Extension
167 } // namespace Inkscape
168
169 /*
170 Local Variables:
171 mode:c++
172 c-file-style:"stroustrup"
173 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
174 indent-tabs-mode:nil
175 fill-column:99
176 End:
177 */
178 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
179