mirror of https://gitlab.com/cppit/jucipp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.9 KiB
116 lines
3.9 KiB
|
6 years ago
|
#include "entrybox.hpp"
|
||
|
11 years ago
|
|
||
|
8 years ago
|
std::unordered_map<std::string, std::vector<std::string>> EntryBox::entry_histories;
|
||
|
10 years ago
|
|
||
|
8 years ago
|
EntryBox::Entry::Entry(const std::string &content, std::function<void(const std::string &content)> on_activate_, unsigned width_chars) : Gtk::Entry(), on_activate(std::move(on_activate_)) {
|
||
|
10 years ago
|
set_max_length(0);
|
||
|
|
set_width_chars(width_chars);
|
||
|
11 years ago
|
set_text(content);
|
||
|
6 years ago
|
last_content = content;
|
||
|
|
selected_history = -1;
|
||
|
8 years ago
|
signal_activate().connect([this]() {
|
||
|
10 years ago
|
if(this->on_activate) {
|
||
|
8 years ago
|
auto &history = EntryBox::entry_histories[get_placeholder_text()];
|
||
|
|
auto text = get_text();
|
||
|
6 years ago
|
if(!text.empty() && (history.empty() || *history.begin() != text))
|
||
|
10 years ago
|
history.emplace(history.begin(), text);
|
||
|
6 years ago
|
selected_history = -1;
|
||
|
10 years ago
|
this->on_activate(text);
|
||
|
|
}
|
||
|
|
});
|
||
|
6 years ago
|
signal_changed().connect([this] {
|
||
|
|
if(!set_text_from_history) {
|
||
|
|
last_content = get_text();
|
||
|
|
selected_history = -1;
|
||
|
|
}
|
||
|
|
});
|
||
|
8 years ago
|
signal_key_press_event().connect([this](GdkEventKey *key) {
|
||
|
|
if(key->keyval == GDK_KEY_Up || key->keyval == GDK_KEY_KP_Up) {
|
||
|
|
auto &history = entry_histories[get_placeholder_text()];
|
||
|
|
if(history.size() > 0) {
|
||
|
7 years ago
|
selected_history++;
|
||
|
6 years ago
|
if(selected_history == 0 && get_text() == history[selected_history])
|
||
|
|
selected_history++;
|
||
|
|
if(selected_history >= static_cast<long>(history.size()))
|
||
|
8 years ago
|
selected_history = history.size() - 1;
|
||
|
6 years ago
|
set_text_from_history = true;
|
||
|
10 years ago
|
set_text(history[selected_history]);
|
||
|
6 years ago
|
set_text_from_history = false;
|
||
|
10 years ago
|
set_position(-1);
|
||
|
|
}
|
||
|
|
}
|
||
|
8 years ago
|
if(key->keyval == GDK_KEY_Down || key->keyval == GDK_KEY_KP_Down) {
|
||
|
|
auto &history = entry_histories[get_placeholder_text()];
|
||
|
|
if(history.size() > 0) {
|
||
|
6 years ago
|
selected_history--;
|
||
|
|
if(selected_history < -1)
|
||
|
|
selected_history = -1;
|
||
|
|
set_text_from_history = true;
|
||
|
|
if(selected_history == -1)
|
||
|
|
set_text(last_content);
|
||
|
|
else
|
||
|
|
set_text(history[selected_history]);
|
||
|
|
set_text_from_history = false;
|
||
|
10 years ago
|
set_position(-1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
11 years ago
|
});
|
||
|
|
}
|
||
|
|
|
||
|
8 years ago
|
EntryBox::Button::Button(const std::string &label, std::function<void()> on_activate_) : Gtk::Button(label), on_activate(std::move(on_activate_)) {
|
||
|
11 years ago
|
set_focus_on_click(false);
|
||
|
8 years ago
|
signal_clicked().connect([this]() {
|
||
|
11 years ago
|
if(this->on_activate)
|
||
|
|
this->on_activate();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
8 years ago
|
EntryBox::ToggleButton::ToggleButton(const std::string &label, std::function<void()> on_activate_) : Gtk::ToggleButton(label), on_activate(std::move(on_activate_)) {
|
||
|
11 years ago
|
set_focus_on_click(false);
|
||
|
8 years ago
|
signal_clicked().connect([this]() {
|
||
|
11 years ago
|
if(this->on_activate)
|
||
|
|
this->on_activate();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
8 years ago
|
EntryBox::Label::Label(std::function<void(int state, const std::string &message)> update_) : Gtk::Label(), update(std::move(update_)) {
|
||
|
|
if(this->update)
|
||
|
|
this->update(-1, "");
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
|
EntryBox::EntryBox() : Gtk::Box(Gtk::ORIENTATION_VERTICAL), upper_box(Gtk::ORIENTATION_HORIZONTAL), lower_box(Gtk::ORIENTATION_HORIZONTAL) {
|
||
|
6 years ago
|
get_style_context()->add_class("juci_entry");
|
||
|
11 years ago
|
pack_start(upper_box, Gtk::PACK_SHRINK);
|
||
|
|
pack_start(lower_box, Gtk::PACK_SHRINK);
|
||
|
10 years ago
|
this->set_focus_chain({&lower_box});
|
||
|
11 years ago
|
}
|
||
|
11 years ago
|
|
||
|
|
void EntryBox::clear() {
|
||
|
11 years ago
|
Gtk::Box::hide();
|
||
|
11 years ago
|
entries.clear();
|
||
|
|
buttons.clear();
|
||
|
11 years ago
|
toggle_buttons.clear();
|
||
|
|
labels.clear();
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
|
void EntryBox::show() {
|
||
|
8 years ago
|
std::vector<Gtk::Widget *> focus_chain;
|
||
|
|
for(auto &entry : entries) {
|
||
|
10 years ago
|
lower_box.pack_start(entry, Gtk::PACK_SHRINK);
|
||
|
11 years ago
|
focus_chain.emplace_back(&entry);
|
||
|
11 years ago
|
}
|
||
|
8 years ago
|
for(auto &button : buttons)
|
||
|
10 years ago
|
lower_box.pack_start(button, Gtk::PACK_SHRINK);
|
||
|
8 years ago
|
for(auto &toggle_button : toggle_buttons)
|
||
|
10 years ago
|
lower_box.pack_start(toggle_button, Gtk::PACK_SHRINK);
|
||
|
8 years ago
|
for(auto &label : labels)
|
||
|
10 years ago
|
upper_box.pack_start(label, Gtk::PACK_SHRINK);
|
||
|
|
lower_box.set_focus_chain(focus_chain);
|
||
|
11 years ago
|
show_all();
|
||
|
8 years ago
|
if(entries.size() > 0) {
|
||
|
11 years ago
|
entries.begin()->grab_focus();
|
||
|
|
entries.begin()->select_region(0, entries.begin()->get_text_length());
|
||
|
|
}
|
||
|
|
}
|