diff options
| author | Patrick Storz <eduard.braun2@gmx.de> | 2019-09-01 16:56:35 +0000 |
|---|---|---|
| committer | Patrick Storz <eduard.braun2@gmx.de> | 2019-09-01 16:56:35 +0000 |
| commit | c6067b75a239a1322598f2d85ef0ce7cc59df0d4 (patch) | |
| tree | f91f4514e7fc563d01a0cbe04a863ab0a488acfd /src/extension | |
| parent | Add appearance="colorbutton" to parameters of type color (diff) | |
| download | inkscape-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')
| -rw-r--r-- | src/extension/extension.cpp | 3 | ||||
| -rw-r--r-- | src/extension/prefdialog/parameter-notebook.cpp | 3 | ||||
| -rw-r--r-- | src/extension/prefdialog/parameter-string.cpp | 96 | ||||
| -rw-r--r-- | src/extension/prefdialog/parameter-string.h | 10 | ||||
| -rw-r--r-- | src/extension/prefdialog/widget-box.cpp | 3 |
5 files changed, 102 insertions, 13 deletions
diff --git a/src/extension/extension.cpp b/src/extension/extension.cpp index 7836a8479..b8c3242a5 100644 --- a/src/extension/extension.cpp +++ b/src/extension/extension.cpp @@ -722,7 +722,8 @@ public: void addWidget(Gtk::Widget *widg, gchar const *tooltip, int indent) { if (widg) { widg->set_margin_start(indent * InxParameter::GUI_INDENTATION); - this->pack_start(*widg, false, false, 0); + this->pack_start(*widg, false, true, 0); // fill=true does not have an effect here, but allows the + // child to choose to expand by setting hexpand/vexpand if (tooltip) { widg->set_tooltip_text(tooltip); } else { diff --git a/src/extension/prefdialog/parameter-notebook.cpp b/src/extension/prefdialog/parameter-notebook.cpp index a139bd2bd..a90935ca1 100644 --- a/src/extension/prefdialog/parameter-notebook.cpp +++ b/src/extension/prefdialog/parameter-notebook.cpp @@ -82,7 +82,8 @@ Gtk::Widget *ParamNotebook::ParamNotebookPage::get_widget(sigc::signal<void> *ch if (child_widget) { int indent = child->get_indent(); child_widget->set_margin_start(indent *GUI_INDENTATION); - vbox->pack_start(*child_widget, false, false, 0); + 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) { 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 */ diff --git a/src/extension/prefdialog/parameter-string.h b/src/extension/prefdialog/parameter-string.h index ef1d48821..3af83111d 100644 --- a/src/extension/prefdialog/parameter-string.h +++ b/src/extension/prefdialog/parameter-string.h @@ -20,6 +20,10 @@ namespace Extension { class ParamString : public InxParameter { public: + enum AppearanceMode { + DEFAULT, MULTILINE + }; + ParamString(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext); /** \brief Returns \c _value, with a \i const to protect it. */ @@ -37,8 +41,10 @@ private: /** \brief Internal value. */ Glib::ustring _value; - /** \brief Maximum length of the string in characters (zero meaning unlimited). - */ + /** appearance mode **/ + AppearanceMode _mode = DEFAULT; + + /** \brief Maximum length of the string in characters (zero meaning unlimited). */ int _max_length = 0; }; diff --git a/src/extension/prefdialog/widget-box.cpp b/src/extension/prefdialog/widget-box.cpp index 2c9d24f4a..9fd75e19a 100644 --- a/src/extension/prefdialog/widget-box.cpp +++ b/src/extension/prefdialog/widget-box.cpp @@ -96,7 +96,8 @@ Gtk::Widget *WidgetBox::get_widget(sigc::signal<void> *changeSignal) if (child_widget) { int indent = child->get_indent(); child_widget->set_margin_start(indent * GUI_INDENTATION); - box->pack_start(*child_widget, false, false, 0); + box->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) { |
