~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to src/ui_basic/textinput.cc

  • Committer: The Widelands Bunnybot
  • Date: 2023-08-26 14:47:30 UTC
  • Revision ID: bunnybot@widelands.org-20230826144730-e95bd1g04nwirbl8
'src/ui_fsmenu/main.cc' was automatically formatted.

(by bunnybot)
20ed7405cfe0298d9903dc093e1e59575ea6367e

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include <algorithm>
22
22
 
 
23
#include "base/log.h"
23
24
#include "base/utf8.h"
24
25
#include "graphic/font_handler.h"
25
26
#include "graphic/graphic.h"
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"
816
819
                        }
817
820
 
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];
822
 
                                }
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();
825
826
                        }
826
827
 
827
828
                        ok();
829
830
 
830
831
                case SDLK_UP:
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;
835
 
                                }
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);
 
836
                                }
 
837
                                ++history_position_;
 
838
                                if (history_position_ >= history_->current_size()) {
 
839
                                        history_position_ = history_->current_size() - 1;
 
840
                                        return true;
 
841
                                }
 
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();
840
847
                                        changed();
845
852
 
846
853
                case SDLK_DOWN:
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) {
 
856
                                --history_position_;
 
857
                                if (history_position_ < 0) {
 
858
                                        history_position_ = -1;
851
859
                                }
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();
856
865
                                        changed();
1111
1120
        scrollbar.set_steps(textheight - owner.get_h() + 2 * get_style().background().margin());
1112
1121
}
1113
1122
 
 
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)) {
 
1126
                return;
 
1127
        }
 
1128
        entries_.emplace(entries_.begin(), new_entry);
 
1129
        changed_ = true;
 
1130
        if (entries_.size() > max_size_) {
 
1131
                entries_.pop_back();
 
1132
        }
 
1133
}
 
1134
 
 
1135
const std::string& EditBoxHistory::get_entry(int16_t position) const {
 
1136
        if (position < 0 || position >= static_cast<int>(entries_.size())) {
 
1137
                return tmp_;
 
1138
        }
 
1139
        return entries_.at(position);
 
1140
}
 
1141
 
 
1142
void EditBoxHistory::load(const std::string& filename) {
 
1143
        FileRead fr;
 
1144
        if (fr.try_open(*g_fs, filename)) {
 
1145
                entries_.clear();
 
1146
                try {
 
1147
                        char* line;
 
1148
                        while ((line = fr.read_line()) != nullptr) {
 
1149
                                add_entry(line);
 
1150
                        }
 
1151
                        // Only set it on success to allow next save() to try to fix problem
 
1152
                        changed_ = false;
 
1153
                } catch (const std::exception& e) {
 
1154
                        log_err(
 
1155
                           "Loading %s, line %" PRIuS ": %s", filename.c_str(), entries_.size() + 1, e.what());
 
1156
                }
 
1157
        }
 
1158
}
 
1159
 
 
1160
void EditBoxHistory::save(const std::string& filename) {
 
1161
        if (!changed_) {
 
1162
                return;
 
1163
        }
 
1164
        try {
 
1165
                FileWrite fw;
 
1166
                for (auto it = entries_.rbegin(); it != entries_.rend(); ++it) {
 
1167
                        fw.print_f("%s\n", it->c_str());
 
1168
                }
 
1169
                fw.write(*g_fs, filename);
 
1170
                changed_ = false;
 
1171
        } catch (const std::exception& e) {
 
1172
                log_err("Saving %s: %s", filename.c_str(), e.what());
 
1173
        }
 
1174
}
 
1175
 
1114
1176
}  // namespace UI