diff --git a/src/extension/prefdialog/parameter-string.cpp b/src/extension/prefdialog/parameter-string.cpp index 07f7cc461cd8e542d986ae55a23115ffb33fa6e2..98c3c3d01ab670988764ebcc81353972b1ff9d98 100644 --- a/src/extension/prefdialog/parameter-string.cpp +++ b/src/extension/prefdialog/parameter-string.cpp @@ -65,6 +65,16 @@ ParamString::ParamString(Inkscape::XML::Node *xml, Inkscape::Extension::Extensio _appearance, _name, _extension->get_id()); } } + + // parse width/height + char const *width = xml->attribute("width"); + if (width) { + _width = strtoul(width, nullptr, 0); + } + char const *height = xml->attribute("height"); + if (height) { + _height = strtoul(height, nullptr, 0); + } } /** @@ -207,13 +217,56 @@ Gtk::Widget *ParamString::get_widget(sigc::signal *changeSignal) entry->set_vexpand(); textarea->set_child(*entry); + set_multiline_size(*box); UI::pack_start(*box, *textarea, true, true); } else { Gtk::Widget *entry = Gtk::make_managed(this, changeSignal); + set_singleline_size(*box); UI::pack_start(*box, *entry, true, true); } return box; } +/** + * Sets the height in number of rows and width in columns + * by converting them to appropriate pixel values. + */ +void ParamString::set_multiline_size(Gtk::Box& box) +{ + set_size_helper(box, true); +} + +/** + * Sets the width of the widget in columns + * by converting it to appropriate pixel values + */ +void ParamString::set_singleline_size(Gtk::Box& box) +{ + set_size_helper(box, false); +} + +void ParamString::set_size_helper(Gtk::Box& box, bool set_height) +{ + auto context = box.get_pango_context(); + if (!context) { + return; + } + auto font_description = context->get_font_description(); + Pango::FontMetrics metrics = context->get_metrics(font_description); + + int total_height = -1; + if (set_height) + { + int row_height = PANGO_PIXELS(metrics.get_height()); + total_height = row_height * _height; + } + + // Only approximate but best we can do + int row_width = PANGO_PIXELS(metrics.get_approximate_digit_width()); + int total_width = row_width * _width; + + box.set_size_request(std::max(-1, total_width), std::max(-1, total_height)); +} + } // namespace Inkscape::Extension diff --git a/src/extension/prefdialog/parameter-string.h b/src/extension/prefdialog/parameter-string.h index 19441e7c409aed75c763c696e70cd4db10b47208..2e7b2f8d645c0221c9f94b7bb63abdd23ff9a3c5 100644 --- a/src/extension/prefdialog/parameter-string.h +++ b/src/extension/prefdialog/parameter-string.h @@ -14,6 +14,10 @@ #include +namespace Gtk { +class Box; +} + namespace Inkscape::Extension { class ParamString : public InxParameter { @@ -45,6 +49,15 @@ private: /** \brief Maximum length of the string in characters (zero meaning unlimited). */ int _max_length = 0; + + /** \brief Starting width of the widget (-1 is unset) */ + int _width = -1; + /** \brief Starting height of the widget (-1 is unset) */ + int _height = -1; + + void set_multiline_size(Gtk::Box& box); + void set_singleline_size(Gtk::Box& box); + void set_size_helper(Gtk::Box& box, bool setHeight); }; } // namespace Inkscape::Extension