From 694ca529b406a226b5910473b04103528654bb3c Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 30 Aug 2015 09:46:55 +0200 Subject: [PATCH] Added history to the entry-fields (find/replace/run command) using up and down arrow keys. --- src/entrybox.cc | 35 +++++++++++++++++++++++++++++++++-- src/entrybox.h | 8 ++++++++ src/window.cc | 1 + 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/entrybox.cc b/src/entrybox.cc index f207e56..e6f5350 100644 --- a/src/entrybox.cc +++ b/src/entrybox.cc @@ -4,12 +4,43 @@ namespace sigc { SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE } +std::unordered_map > EntryBox::entry_histories; + EntryBox::Entry::Entry(const std::string& content, std::function on_activate, unsigned length) : Gtk::Entry(), on_activate(on_activate) { set_max_length(length); set_text(content); + selected_history=0; signal_activate().connect([this](){ - if(this->on_activate) - this->on_activate(get_text()); + if(this->on_activate) { + auto &history=EntryBox::entry_histories[get_placeholder_text()]; + auto text=get_text(); + if(history.size()==0 || (history.size()>0 && *history.begin()!=text)) + history.emplace(history.begin(), text); + selected_history=0; + this->on_activate(text); + } + }); + signal_key_press_event().connect([this](GdkEventKey* key){ + if(key->keyval==GDK_KEY_Up) { + auto &history=entry_histories[get_placeholder_text()]; + if(history.size()>0) { + selected_history++; + if(selected_history>=history.size()) + selected_history=history.size()-1; + set_text(history[selected_history]); + set_position(-1); + } + } + if(key->keyval==GDK_KEY_Down) { + auto &history=entry_histories[get_placeholder_text()]; + if(history.size()>0) { + if(selected_history!=0) + selected_history--; + set_text(history[selected_history]); + set_position(-1); + } + } + return false; }); } diff --git a/src/entrybox.h b/src/entrybox.h index 6ff40c1..e7bdebd 100644 --- a/src/entrybox.h +++ b/src/entrybox.h @@ -4,6 +4,9 @@ #include #include #include "gtkmm.h" +#include +#include +#include class EntryBox : public Gtk::Box { public: @@ -11,6 +14,8 @@ public: public: Entry(const std::string& content="", std::function on_activate=nullptr, unsigned length=50); std::function on_activate; + private: + size_t selected_history; }; class Button : public Gtk::Button { public: @@ -39,6 +44,9 @@ public: std::list