27
28
#include "graphic/style_manager.h"
28
29
#include "graphic/text_layout.h"
29
30
#include "graphic/wordwrap.h"
31
#include "io/fileread.h"
32
#include "io/filewrite.h"
30
33
#include "ui_basic/mouse_constants.h"
31
34
#include "ui_basic/scrollbar.h"
32
35
#include "wlapplication_options.h"
818
821
// Save history if active and text is not empty
819
if (history_active_ && !d_->text.empty()) {
820
for (unsigned i = kHistorySize - 1; i > 0; --i) {
821
history_[i] = history_[i - 1];
823
history_[0] = d_->text;
822
if (history_ != nullptr && !d_->text.empty()) {
823
history_->add_entry(d_->text);
824
824
history_position_ = -1;
825
history_->clear_tmp();
831
832
// Load entry from history if active and text is not empty
832
if (history_active_) {
833
if (history_position_ > static_cast<int>(kHistorySize) - 2) {
834
history_position_ = kHistorySize - 2;
836
if (!history_[++history_position_].empty()) {
837
d_->text = history_[history_position_];
833
if (history_ != nullptr) {
834
if (history_position_ < 0) {
835
history_->set_tmp(d_->text);
838
if (history_position_ >= history_->current_size()) {
839
history_position_ = history_->current_size() - 1;
842
const std::string& hist_prev = history_->get_entry(history_position_);
843
if (!hist_prev.empty()) {
844
d_->text = hist_prev;
838
845
set_caret_pos(d_->text.size());
839
846
d_->reset_selection();
847
854
// Load entry from history if active and text is not equivalent to the current one
848
if (history_active_) {
849
if (history_position_ < 1) {
850
history_position_ = 1;
855
if (history_ != nullptr) {
857
if (history_position_ < 0) {
858
history_position_ = -1;
852
if (history_[--history_position_] != d_->text) {
853
d_->text = history_[history_position_];
860
const std::string& hist_next = history_->get_entry(history_position_);
861
if (hist_next != d_->text) {
862
d_->text = hist_next;
854
863
set_caret_pos(d_->text.size());
855
864
d_->reset_selection();
1111
1120
scrollbar.set_steps(textheight - owner.get_h() + 2 * get_style().background().margin());
1123
void EditBoxHistory::add_entry(const std::string& new_entry) {
1124
// Avoid duplicates next to each other
1125
if (!entries_.empty() && new_entry == entries_.at(0)) {
1128
entries_.emplace(entries_.begin(), new_entry);
1130
if (entries_.size() > max_size_) {
1131
entries_.pop_back();
1135
const std::string& EditBoxHistory::get_entry(int16_t position) const {
1136
if (position < 0 || position >= static_cast<int>(entries_.size())) {
1139
return entries_.at(position);
1142
void EditBoxHistory::load(const std::string& filename) {
1144
if (fr.try_open(*g_fs, filename)) {
1148
while ((line = fr.read_line()) != nullptr) {
1151
// Only set it on success to allow next save() to try to fix problem
1153
} catch (const std::exception& e) {
1155
"Loading %s, line %" PRIuS ": %s", filename.c_str(), entries_.size() + 1, e.what());
1160
void EditBoxHistory::save(const std::string& filename) {
1166
for (auto it = entries_.rbegin(); it != entries_.rend(); ++it) {
1167
fw.print_f("%s\n", it->c_str());
1169
fw.write(*g_fs, filename);
1171
} catch (const std::exception& e) {
1172
log_err("Saving %s: %s", filename.c_str(), e.what());
1114
1176
} // namespace UI