diff --git a/libs/gtkmm2ext/gtkmm2ext/string_completion.h b/libs/gtkmm2ext/gtkmm2ext/string_completion.h new file mode 100644 index 0000000000..5c0ed5b1a8 --- /dev/null +++ b/libs/gtkmm2ext/gtkmm2ext/string_completion.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2025 Paul Davis + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include +#include +#include + +namespace Gtkmm2ext { + +class StringCompletion : public Gtk::EntryCompletion +{ + public: + StringCompletion (); + StringCompletion (std::vector strVector, bool norepeat = true); + + virtual ~StringCompletion(); + + void add_string (Glib::ustring str, bool norepeat=true); + void clear_strings(); + void delete_string (Glib::ustring str); + void insert_vector (std::vector strVector, bool norepeat = true); + + static Glib::RefPtr create(); + static Glib::RefPtr create (std::vector strVector, bool norepeat = true); + + protected: + Glib::RefPtr m_refCompletionModel; + + class CompletionRecord : public Gtk::TreeModel::ColumnRecord + { + public: + CompletionRecord() + { + add(col_text); + } + Gtk::TreeModelColumn col_text; + }; + + CompletionRecord m_completionRecord; + bool _delete_string (const Gtk::TreeModel::iterator &iter, const Glib::ustring &str); + bool _string_exists (const Glib::ustring &str); + void init(); +}; + +} /* namespace */ diff --git a/libs/gtkmm2ext/string_completion.cc b/libs/gtkmm2ext/string_completion.cc new file mode 100644 index 0000000000..78ffc841dc --- /dev/null +++ b/libs/gtkmm2ext/string_completion.cc @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2025 Paul Davis + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "gtkmm2ext/string_completion.h" + +using namespace Gtkmm2ext; + +StringCompletion::StringCompletion() +{ + this->init(); +} + +StringCompletion::StringCompletion (std::vector < Glib::ustring > strVector, bool norepeat) +{ + this->init(); + this->insert_vector(strVector, norepeat); +} + +StringCompletion::~StringCompletion() +{ +} + +void +StringCompletion::init () +{ + m_refCompletionModel = Gtk::ListStore::create(m_completionRecord); + + this->set_model(m_refCompletionModel); + this->set_text_column (m_completionRecord.col_text); +} + +void +StringCompletion::add_string (Glib::ustring str, bool norepeat) +{ + if ((!norepeat) || (!this->_string_exists (str))) { + Gtk::TreeModel::Row row =*(m_refCompletionModel->append()); + row[m_completionRecord.col_text] = str; + } +} + +Glib::RefPtr +StringCompletion::create() +{ + return Glib::RefPtr (new StringCompletion()); +} + +void +StringCompletion::clear_strings() +{ + m_refCompletionModel->clear(); +} + +void +StringCompletion::delete_string(Glib::ustring str) +{ + m_refCompletionModel->foreach_iter(sigc::bind(sigc::mem_fun( *this, &StringCompletion::_delete_string), str)); +} + +bool +StringCompletion::_delete_string (const Gtk::TreeModel::iterator & iter, const Glib::ustring & str) +{ + if (Gtk::TreeModel::Row(*iter)[m_completionRecord.col_text] == str) { + m_refCompletionModel->erase(iter); + return true; + } + return false; +} + +bool +StringCompletion::_string_exists (const Glib::ustring & str) +{ + for (auto & i : m_refCompletionModel->children()) { + if (Gtk::TreeModel::Row(i)[m_completionRecord.col_text] == str) { + return true; + } + } + return false; +} + +Glib::RefPtr +StringCompletion::create (std::vector < Glib::ustring > strVector, bool norepeat) +{ + return Glib::RefPtr ( new StringCompletion(strVector) ); +} + +void +StringCompletion::insert_vector (std::vector strVector, bool norepeat) +{ + for (auto & s : strVector) { + this->add_string (s, norepeat); + } +} diff --git a/libs/gtkmm2ext/wscript b/libs/gtkmm2ext/wscript index d47ee0465a..21a4b72cf5 100644 --- a/libs/gtkmm2ext/wscript +++ b/libs/gtkmm2ext/wscript @@ -39,6 +39,7 @@ gtkmm2ext_sources = [ 'keyboard.cc', 'menu_elems.cc', 'persistent_tooltip.cc', + 'string_completion.cc', 'textviewer.cc', 'treeutils.cc', 'utils.cc',