Browse Source

Added history to the entry-fields (find/replace/run command) using up and down arrow keys.

merge-requests/365/head
eidheim 10 years ago
parent
commit
694ca529b4
  1. 35
      src/entrybox.cc
  2. 8
      src/entrybox.h
  3. 1
      src/window.cc

35
src/entrybox.cc

@ -4,12 +4,43 @@ namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
std::unordered_map<std::string, std::vector<std::string> > EntryBox::entry_histories;
EntryBox::Entry::Entry(const std::string& content, std::function<void(const std::string& content)> 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;
});
}

8
src/entrybox.h

@ -4,6 +4,9 @@
#include <list>
#include <functional>
#include "gtkmm.h"
#include <unordered_map>
#include <string>
#include <vector>
class EntryBox : public Gtk::Box {
public:
@ -11,6 +14,8 @@ public:
public:
Entry(const std::string& content="", std::function<void(const std::string& content)> on_activate=nullptr, unsigned length=50);
std::function<void(const std::string& content)> on_activate;
private:
size_t selected_history;
};
class Button : public Gtk::Button {
public:
@ -39,6 +44,9 @@ public:
std::list<Button> buttons;
std::list<ToggleButton> toggle_buttons;
std::list<Label> labels;
private:
static std::unordered_map<std::string, std::vector<std::string> > entry_histories;
};
#endif // JUCI_ENTRYBOX_H_

1
src/window.cc

@ -282,6 +282,7 @@ void Window::create_menu() {
entry_box.hide();
});
auto entry_it=entry_box.entries.begin();
entry_it->set_placeholder_text("Command");
entry_box.buttons.emplace_back("Run command", [this, entry_it](){
entry_it->activate();
});

Loading…
Cancel
Save