new Gtkmm2ext helper class for string completions on a text entry
This commit is contained in:
62
libs/gtkmm2ext/gtkmm2ext/string_completion.h
Normal file
62
libs/gtkmm2ext/gtkmm2ext/string_completion.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Paul Davis <paul@linuxaudiosystems.com>
|
||||
*
|
||||
* 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 <vector>
|
||||
#include <ytkmm/entry.h>
|
||||
#include <ytkmm/liststore.h>
|
||||
|
||||
namespace Gtkmm2ext {
|
||||
|
||||
class StringCompletion : public Gtk::EntryCompletion
|
||||
{
|
||||
public:
|
||||
StringCompletion ();
|
||||
StringCompletion (std::vector<Glib::ustring> 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<Glib::ustring> strVector, bool norepeat = true);
|
||||
|
||||
static Glib::RefPtr<StringCompletion> create();
|
||||
static Glib::RefPtr<StringCompletion> create (std::vector<Glib::ustring> strVector, bool norepeat = true);
|
||||
|
||||
protected:
|
||||
Glib::RefPtr<Gtk::ListStore> m_refCompletionModel;
|
||||
|
||||
class CompletionRecord : public Gtk::TreeModel::ColumnRecord
|
||||
{
|
||||
public:
|
||||
CompletionRecord()
|
||||
{
|
||||
add(col_text);
|
||||
}
|
||||
Gtk::TreeModelColumn<Glib::ustring> 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 */
|
||||
107
libs/gtkmm2ext/string_completion.cc
Normal file
107
libs/gtkmm2ext/string_completion.cc
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Paul Davis <paul@linuxaudiosystems.com>
|
||||
*
|
||||
* 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>
|
||||
StringCompletion::create()
|
||||
{
|
||||
return Glib::RefPtr<StringCompletion> (new StringCompletion());
|
||||
}
|
||||
|
||||
void
|
||||
StringCompletion::clear_strings()
|
||||
{
|
||||
m_refCompletionModel->clear();
|
||||
}
|
||||
|
||||
void
|
||||
StringCompletion::delete_string(Glib::ustring str)
|
||||
{
|
||||
m_refCompletionModel->foreach_iter(sigc::bind<const Glib::ustring& >(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>
|
||||
StringCompletion::create (std::vector < Glib::ustring > strVector, bool norepeat)
|
||||
{
|
||||
return Glib::RefPtr<StringCompletion> ( new StringCompletion(strVector) );
|
||||
}
|
||||
|
||||
void
|
||||
StringCompletion::insert_vector (std::vector<Glib::ustring> strVector, bool norepeat)
|
||||
{
|
||||
for (auto & s : strVector) {
|
||||
this->add_string (s, norepeat);
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,7 @@ gtkmm2ext_sources = [
|
||||
'keyboard.cc',
|
||||
'menu_elems.cc',
|
||||
'persistent_tooltip.cc',
|
||||
'string_completion.cc',
|
||||
'textviewer.cc',
|
||||
'treeutils.cc',
|
||||
'utils.cc',
|
||||
|
||||
Reference in New Issue
Block a user