From a896ab7e3e2cc764849d177d627a81d3d763cfc6 Mon Sep 17 00:00:00 2001 From: Ole Christian Eidheim Date: Mon, 6 Jun 2016 12:37:11 +0200 Subject: [PATCH] SelectionDialogs: improved search entry --- src/selectiondialog.cc | 24 +++++-------------- src/selectiondialog.h | 44 +++++++++++++++++++--------------- tests/stubs/selectiondialog.cc | 2 +- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/selectiondialog.cc b/src/selectiondialog.cc index d9f5784..51dae14 100644 --- a/src/selectiondialog.cc +++ b/src/selectiondialog.cc @@ -15,7 +15,7 @@ namespace sigc { #endif } -ListViewText::ListViewText(bool use_markup) : Gtk::TreeView(), use_markup(use_markup) { +SelectionDialogBase::ListViewText::ListViewText(bool use_markup) : Gtk::TreeView(), use_markup(use_markup) { list_store = Gtk::ListStore::create(column_record); set_model(list_store); append_column("", cell_renderer); @@ -33,12 +33,12 @@ ListViewText::ListViewText(bool use_markup) : Gtk::TreeView(), use_markup(use_ma set_rules_hint(true); } -void ListViewText::append(const std::string& value) { +void SelectionDialogBase::ListViewText::append(const std::string& value) { auto new_row=list_store->append(); new_row->set_value(column_record.text, value); } -void ListViewText::clear() { +void SelectionDialogBase::ListViewText::clear() { unset_model(); list_store.reset(); } @@ -261,21 +261,9 @@ bool SelectionDialog::on_key_press(GdkEventKey* key) { hide(); return false; } - else if(key->keyval==GDK_KEY_BackSpace) { - auto length=search_entry.get_text_length(); - if(length>0) - search_entry.delete_text(length-1, length); - return true; - } - else { - gunichar unicode=gdk_keyval_to_unicode(key->keyval); - if(unicode>=32 && unicode!=126) { - int length=search_entry.get_text_length(); - auto ustr=Glib::ustring(1, unicode); - search_entry.insert_text(ustr, ustr.bytes(), length); - return true; - } - } + else if(show_search_entry) + return search_entry.on_key_press_event(key); + hide(); return false; } diff --git a/src/selectiondialog.h b/src/selectiondialog.h index e648779..9b80b89 100644 --- a/src/selectiondialog.h +++ b/src/selectiondialog.h @@ -4,26 +4,32 @@ #include "gtkmm.h" #include -class ListViewText : public Gtk::TreeView { - class ColumnRecord : public Gtk::TreeModel::ColumnRecord { +class SelectionDialogBase { + class ListViewText : public Gtk::TreeView { + class ColumnRecord : public Gtk::TreeModel::ColumnRecord { + public: + ColumnRecord() { + add(text); + } + Gtk::TreeModelColumn text; + }; public: - ColumnRecord() { - add(text); - } - Gtk::TreeModelColumn text; + bool use_markup; + ListViewText(bool use_markup); + void append(const std::string& value); + void clear(); + private: + Glib::RefPtr list_store; + ColumnRecord column_record; + Gtk::CellRendererText cell_renderer; }; -public: - bool use_markup; - ListViewText(bool use_markup); - void append(const std::string& value); - void clear(); -private: - Glib::RefPtr list_store; - ColumnRecord column_record; - Gtk::CellRendererText cell_renderer; -}; - -class SelectionDialogBase { + + class SearchEntry : public Gtk::Entry { + public: + SearchEntry() : Gtk::Entry() {} + bool on_key_press_event(GdkEventKey *event) override { return Gtk::Entry::on_key_press_event(event); }; + }; + public: SelectionDialogBase(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup); ~SelectionDialogBase(); @@ -48,7 +54,7 @@ protected: Gtk::VBox vbox; Gtk::ScrolledWindow scrolled_window; ListViewText list_view_text; - Gtk::Entry search_entry; + SearchEntry search_entry; bool show_search_entry; std::string last_row; diff --git a/tests/stubs/selectiondialog.cc b/tests/stubs/selectiondialog.cc index f8b9d67..61866a0 100644 --- a/tests/stubs/selectiondialog.cc +++ b/tests/stubs/selectiondialog.cc @@ -1,6 +1,6 @@ #include "selectiondialog.h" -ListViewText::ListViewText(bool use_markup) {} +SelectionDialogBase::ListViewText::ListViewText(bool use_markup) {} SelectionDialogBase::SelectionDialogBase(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup): text_view(text_view), list_view_text(use_markup) {}