summaryrefslogtreecommitdiffstats
path: root/src/extension/prefdialog/parameter-string.cpp
diff options
context:
space:
mode:
authorPatrick Storz <eduard.braun2@gmx.de>2019-09-01 16:56:35 +0000
committerPatrick Storz <eduard.braun2@gmx.de>2019-09-01 16:56:35 +0000
commitc6067b75a239a1322598f2d85ef0ce7cc59df0d4 (patch)
treef91f4514e7fc563d01a0cbe04a863ab0a488acfd /src/extension/prefdialog/parameter-string.cpp
parentAdd appearance="colorbutton" to parameters of type color (diff)
downloadinkscape-c6067b75a239a1322598f2d85ef0ce7cc59df0d4.tar.gz
inkscape-c6067b75a239a1322598f2d85ef0ce7cc59df0d4.zip
Add appearance="multiline" to parameters of type string
Renders a Gtk::TextView that automatically fills the available space in the extension's preferences dialog and can be used to accept longer multi-line strings. Newlines in the string value will be passed as "\\n" to the extension script (i.e. literal '\n' with the backspace escaped).
Diffstat (limited to 'src/extension/prefdialog/parameter-string.cpp')
-rw-r--r--src/extension/prefdialog/parameter-string.cpp96
1 files changed, 88 insertions, 8 deletions
diff --git a/src/extension/prefdialog/parameter-string.cpp b/src/extension/prefdialog/parameter-string.cpp
index bf8ad14a3..6cf57c20e 100644
--- a/src/extension/prefdialog/parameter-string.cpp
+++ b/src/extension/prefdialog/parameter-string.cpp
@@ -11,6 +11,9 @@
#include <gtkmm/box.h>
#include <gtkmm/entry.h>
+#include <gtkmm/scrolledwindow.h>
+#include <gtkmm/textview.h>
+#include <glibmm/regex.h>
#include "xml/node.h"
#include "extension/extension.h"
@@ -50,6 +53,16 @@ ParamString::ParamString(Inkscape::XML::Node *xml, Inkscape::Extension::Extensio
if (max_length) {
_max_length = strtoul(max_length, nullptr, 0);
}
+
+ // parse appearance
+ if (_appearance) {
+ if (!strcmp(_appearance, "multiline")) {
+ _mode = MULTILINE;
+ } else {
+ g_warning("Invalid value ('%s') for appearance of parameter '%s' in extension '%s'",
+ _appearance, _name, _extension->get_id());
+ }
+ }
}
/**
@@ -80,7 +93,8 @@ std::string ParamString::value_to_string() const
}
-/** A special type of Gtk::Entry to handle string parameteres. */
+
+/** A special type of Gtk::Entry to handle string parameters. */
class ParamStringEntry : public Gtk::Entry {
private:
ParamString *_pref;
@@ -119,6 +133,54 @@ void ParamStringEntry::changed_text()
}
}
+
+
+/** A special type of Gtk::TextView to handle multiline string parameters. */
+class ParamMultilineStringEntry : public Gtk::TextView {
+private:
+ ParamString *_pref;
+ sigc::signal<void> *_changeSignal;
+public:
+ /**
+ * Build a string preference for the given parameter.
+ * @param pref Where to get the string from, and where to put it
+ * when it changes.
+ */
+ ParamMultilineStringEntry(ParamString *pref, sigc::signal<void> *changeSignal)
+ : Gtk::TextView()
+ , _pref(pref)
+ , _changeSignal(changeSignal)
+ {
+ // replace literal '\n' with actual newlines for multiline strings
+ Glib::ustring value = Glib::Regex::create("\\\\n")->replace_literal(_pref->get(), 0, "\n", (Glib::RegexMatchFlags)0);
+
+ this->get_buffer()->set_text(value);
+ this->get_buffer()->signal_changed().connect(sigc::mem_fun(this, &ParamMultilineStringEntry::changed_text));
+ };
+ void changed_text();
+};
+
+/**
+ * Respond to the text box changing.
+ *
+ * This function responds to the box changing by grabbing the value
+ * from the text box and putting it in the parameter.
+ */
+void ParamMultilineStringEntry::changed_text()
+{
+ Glib::ustring data = this->get_buffer()->get_text();
+
+ // always store newlines as literal '\n'
+ data = Glib::Regex::create("\n")->replace_literal(data, 0, "\\n", (Glib::RegexMatchFlags)0);
+
+ _pref->set(data.c_str());
+ if (_changeSignal != nullptr) {
+ _changeSignal->emit();
+ }
+}
+
+
+
/**
* Creates a text box for the string parameter.
*
@@ -130,18 +192,36 @@ Gtk::Widget *ParamString::get_widget(sigc::signal<void> *changeSignal)
return nullptr;
}
- Gtk::HBox *hbox = Gtk::manage(new Gtk::HBox(false, GUI_PARAM_WIDGETS_SPACING));
+ Gtk::Box *box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, GUI_PARAM_WIDGETS_SPACING));
+
Gtk::Label *label = Gtk::manage(new Gtk::Label(_text, Gtk::ALIGN_START));
label->show();
- hbox->pack_start(*label, false, false);
+ box->pack_start(*label, false, false);
+
+ if (_mode == MULTILINE) {
+ box->set_orientation(Gtk::ORIENTATION_VERTICAL);
- ParamStringEntry * textbox = new ParamStringEntry(this, changeSignal);
- textbox->show();
- hbox->pack_start(*textbox, true, true);
+ Gtk::ScrolledWindow *textarea = new Gtk::ScrolledWindow();
+ textarea->set_vexpand();
+ textarea->set_shadow_type(Gtk::SHADOW_IN);
+
+ ParamMultilineStringEntry *entry = new ParamMultilineStringEntry(this, changeSignal);
+ entry->show();
+
+ textarea->add(*entry);
+ textarea->show();
+
+ box->pack_start(*textarea, true, true);
+ } else {
+ Gtk::Widget *entry = new ParamStringEntry(this, changeSignal);
+ entry->show();
+
+ box->pack_start(*entry, true, true);
+ }
- hbox->show();
+ box->show();
- return dynamic_cast<Gtk::Widget *>(hbox);
+ return dynamic_cast<Gtk::Widget *>(box);
}
} /* namespace Extension */