From 742f654fa4cc196e5b602c5dae2fbcd1af9c0802 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Tue, 10 Jun 2025 12:27:33 -0600 Subject: [PATCH] StringCompletion: allow case-folded matching --- libs/gtkmm2ext/gtkmm2ext/string_completion.h | 2 ++ libs/gtkmm2ext/string_completion.cc | 28 +++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libs/gtkmm2ext/gtkmm2ext/string_completion.h b/libs/gtkmm2ext/gtkmm2ext/string_completion.h index 93d93ab53a..467f8bde7a 100644 --- a/libs/gtkmm2ext/gtkmm2ext/string_completion.h +++ b/libs/gtkmm2ext/gtkmm2ext/string_completion.h @@ -39,12 +39,14 @@ class LIBGTKMM2EXT_API StringCompletion : public Gtk::EntryCompletion void insert_vector (std::vector strVector, bool norepeat = true); void set_match_anywhere (); + void set_case_fold (bool); static Glib::RefPtr create(); static Glib::RefPtr create (std::vector strVector, bool norepeat = true); protected: Glib::RefPtr m_refCompletionModel; + bool case_fold; class CompletionRecord : public Gtk::TreeModel::ColumnRecord { diff --git a/libs/gtkmm2ext/string_completion.cc b/libs/gtkmm2ext/string_completion.cc index c43be57598..80d349ce91 100644 --- a/libs/gtkmm2ext/string_completion.cc +++ b/libs/gtkmm2ext/string_completion.cc @@ -16,19 +16,24 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include + +#include "pbd/convert.h" #include "gtkmm2ext/string_completion.h" using namespace Gtkmm2ext; StringCompletion::StringCompletion() + : case_fold (true) { - this->init(); + init(); } StringCompletion::StringCompletion (std::vector < Glib::ustring > strVector, bool norepeat) + : case_fold (true) { - this->init(); - this->insert_vector(strVector, norepeat); + init(); + insert_vector(strVector, norepeat); } StringCompletion::~StringCompletion() @@ -40,14 +45,14 @@ StringCompletion::init () { m_refCompletionModel = Gtk::ListStore::create(m_completionRecord); - this->set_model(m_refCompletionModel); - this->set_text_column (m_completionRecord.col_text); + set_model(m_refCompletionModel); + set_text_column (m_completionRecord.col_text); } void StringCompletion::add_string (Glib::ustring str, bool norepeat) { - if ((!norepeat) || (!this->_string_exists (str))) { + if ((!norepeat) || (!_string_exists (str))) { Gtk::TreeModel::Row row =*(m_refCompletionModel->append()); row[m_completionRecord.col_text] = str; } @@ -102,7 +107,7 @@ void StringCompletion::insert_vector (std::vector strVector, bool norepeat) { for (auto & s : strVector) { - this->add_string (s, norepeat); + add_string (s, norepeat); } } @@ -110,6 +115,9 @@ bool StringCompletion::match_anywhere (Glib::ustring const & str, Gtk::TreeModel::const_iterator const & iter) { Glib::ustring r = Gtk::TreeModel::Row (*iter)[m_completionRecord.col_text]; + if (case_fold) { + return PBD::downcase (r).find (PBD::downcase (str)) != Glib::ustring::npos; + } return r.find (str) != Glib::ustring::npos; } @@ -118,3 +126,9 @@ StringCompletion::set_match_anywhere () { set_match_func (sigc::mem_fun (this, &StringCompletion::match_anywhere)); } + +void +StringCompletion::set_case_fold (bool yn) +{ + case_fold = yn; +}