Inkscape
Vector Graphics Editor
widget.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later/*
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
31namespace Inkscape {
32namespace Extension {
33
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
71bool 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
86 : _extension(ext)
87{
88 // translatable (optional)
89 const char *translatable = in_repr->attribute("translatable");
90 if (translatable) {
91 if (!strcmp(translatable, "yes")) {
93 } else if (!strcmp(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 _gui_hidden = true;
115 _hidden = true;
116 }
117 }
118
119 // indent (optional)
120 const char *indent = in_repr->attribute("indent");
121 if (indent != nullptr) {
122 _indent = strtol(indent, nullptr, 0);
123 }
124
125 // appearance (optional, does not apply to all parameters)
126 const char *appearance = in_repr->attribute("appearance");
127 if (appearance) {
128 _appearance = g_strdup(appearance);
129 }
130}
131
133{
134 for (auto child : _children) {
135 delete child;
136 }
137
138 g_free(_context);
139 _context = nullptr;
140
141 g_free(_appearance);
142 _appearance = nullptr;
143}
144
145Gtk::Widget *
146InxWidget::get_widget(sigc::signal<void ()> * /*changeSignal*/)
147{
148 // if we end up here we're missing a definition of ::get_widget() in one of the subclasses
149 g_critical("InxWidget::get_widget called from widget of type '%s' in extension '%s'",
150 typeid(this).name(), _extension->get_id());
151 g_assert_not_reached();
152 return nullptr;
153}
154
155const char *InxWidget::get_translation(const char* msgid) {
156 return _extension->get_translation(msgid, _context);
157}
158
159void InxWidget::get_widgets(std::vector<InxWidget *> &list)
160{
161 list.push_back(this);
162 for (auto child : _children) {
163 child->get_widgets(list);
164 }
165}
166
167} // namespace Extension
168} // namespace Inkscape
169
170/*
171 Local Variables:
172 mode:c++
173 c-file-style:"stroustrup"
174 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
175 indent-tabs-mode:nil
176 fill-column:99
177 End:
178*/
179// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
The object that is the basis for the Extension system.
Definition: extension.h:130
char const * get_translation(char const *msgid, char const *msgctxt=nullptr) const
Gets a translation within the context of the current extension.
Definition: extension.cpp:498
char const * get_id() const
Get the ID of this extension - not a copy don't delete!
Definition: extension.cpp:316
static InxParameter * make(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *in_ext)
Creates a new extension parameter for usage in a prefdialog.
Definition: parameter.cpp:66
Base class to represent all widgets of an extension (including parameters)
Definition: widget.h:36
char * _context
context for translation of translatable strings.
Definition: widget.h:121
Inkscape::Extension::Extension * _extension
Which extension is this Widget attached to.
Definition: widget.h:102
static bool is_valid_widget_name(const char *name)
Checks if name is a valid widget name, i.e.
Definition: widget.cpp:71
bool _hidden
Whether the widget is visible.
Definition: widget.h:108
static InxWidget * make(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *in_ext)
Creates a new extension widget for usage in a prefdialog.
Definition: widget.cpp:34
virtual Gtk::Widget * get_widget(sigc::signal< void()> *changeSignal)
Return the instance's GTK::Widget representation for usage in a GUI.
Definition: widget.cpp:146
InxWidget(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *in_ext)
Definition: widget.cpp:85
int _indent
Indentation level of the widget.
Definition: widget.h:112
std::vector< InxWidget * > _children
Child widgets of this widget (might be empty if there are none)
Definition: widget.h:105
const char * get_translation(const char *msgid)
gets the gettext translation for msgid
Definition: widget.cpp:155
char * _appearance
Appearance of the widget (not used by all widgets).
Definition: widget.h:115
Translatable _translatable
Is widget translatable?
Definition: widget.h:118
virtual void get_widgets(std::vector< InxWidget * > &list)
Recursively construct a list containing the current widget and all of it's child widgets (if it has a...
Definition: widget.cpp:159
Interface for refcounted XML nodes.
Definition: node.h:80
virtual char const * name() const =0
Get the name of the element node.
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
CMYK to sRGB conversion routines.
Ocnode * child[8]
Definition: quantize.cpp:33
Box widget for extensions.
Image widget for extensions.
Description widget for extensions.
Separator widget for extensions.
Spacer widget for extensions.
Base class for extension widgets.
Interface for XML nodes.