From 80a4b28ed7327008153d14f14574f7dc47827bb1 Mon Sep 17 00:00:00 2001 From: Azeem Bande-Ali Date: Sun, 18 May 2025 22:34:01 -0400 Subject: [PATCH 1/2] Support custom size for string params for extensions Effect-style extensions can control the content of the dialog but have limited control on the sizing. This commit allows extensions to control the starting width/height for the string entry. -1 for either will leave the respective dimension to its default. --- src/extension/prefdialog/parameter-string.cpp | 14 ++++++++++++++ src/extension/prefdialog/parameter-string.h | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/src/extension/prefdialog/parameter-string.cpp b/src/extension/prefdialog/parameter-string.cpp index 07f7cc461c..aedc880d87 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); + } } /** @@ -213,6 +223,10 @@ Gtk::Widget *ParamString::get_widget(sigc::signal *changeSignal) UI::pack_start(*box, *entry, true, true); } + if (_height >= 0 || _width >= 0) { + box->set_size_request(std::max(-1, _width), std::max(-1, _height)); + } + return box; } diff --git a/src/extension/prefdialog/parameter-string.h b/src/extension/prefdialog/parameter-string.h index 19441e7c40..8cabb464df 100644 --- a/src/extension/prefdialog/parameter-string.h +++ b/src/extension/prefdialog/parameter-string.h @@ -45,6 +45,11 @@ 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; }; } // namespace Inkscape::Extension -- GitLab From abd39d703d8b4fbafe2626895aeae3021045f719 Mon Sep 17 00:00:00 2001 From: Azeem Bande-Ali Date: Mon, 2 Jun 2025 00:24:22 -0400 Subject: [PATCH 2/2] Interpret height and width as rows and columns --- src/extension/prefdialog/parameter-string.cpp | 45 +++++++++++++++++-- src/extension/prefdialog/parameter-string.h | 8 ++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/extension/prefdialog/parameter-string.cpp b/src/extension/prefdialog/parameter-string.cpp index aedc880d87..98c3c3d01a 100644 --- a/src/extension/prefdialog/parameter-string.cpp +++ b/src/extension/prefdialog/parameter-string.cpp @@ -217,17 +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); } - if (_height >= 0 || _width >= 0) { - box->set_size_request(std::max(-1, _width), std::max(-1, _height)); + 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); - return box; + 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 8cabb464df..2e7b2f8d64 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 { @@ -50,6 +54,10 @@ private: 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 -- GitLab