-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
data_manage.cpp
192 lines (152 loc) · 4.7 KB
/
data_manage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright (C) 2010 - 2014 by Jody Northup
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/data_manage.hpp"
#include "formula_string_utils.hpp"
#include "game_config.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/log.hpp"
#include "gui/dialogs/field.hpp"
#include "gui/dialogs/game_delete.hpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/minimap.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/window.hpp"
#include "language.hpp"
#include "preferences_display.hpp"
#include "savegame.hpp"
#include "utils/foreach.tpp"
#include <cctype>
#include <boost/bind.hpp>
namespace gui2
{
REGISTER_DIALOG(data_manage)
tdata_manage::tdata_manage()
: txtFilter_(register_text("txtFilter", true))
, filename_()
, games_()
, last_words_()
{
}
void tdata_manage::pre_show(CVideo& /*video*/, twindow& window)
{
assert(txtFilter_);
ttext_box* filter
= find_widget<ttext_box>(&window, "txtFilter", false, true);
window.keyboard_capture(filter);
filter->set_text_changed_callback(
boost::bind(&tdata_manage::filter_text_changed, this, _1, _2));
tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
window.keyboard_capture(&list);
#ifdef GUI2_EXPERIMENTAL_LISTBOX
connect_signal_notify_modified(list,
boost::bind(&tdata_manage::list_item_clicked,
this,
boost::ref(window)));
#else
list.set_callback_value_change(
dialog_callback<tdata_manage, &tdata_manage::list_item_clicked>);
#endif
{
cursor::setter cur(cursor::WAIT);
games_ = savegame::get_saves_list();
}
fill_game_list(window, games_);
connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "clear", false),
boost::bind(&tdata_manage::delete_button_callback,
this,
boost::ref(window)));
}
void tdata_manage::fill_game_list(twindow& window,
std::vector<savegame::save_info>& games)
{
tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
list.clear();
FOREACH(const AUTO & game, games)
{
std::map<std::string, string_map> data;
string_map item;
item["label"] = game.name();
data.insert(std::make_pair("filename", item));
item["label"] = game.format_time_summary();
data.insert(std::make_pair("date", item));
list.add_row(data);
}
}
void tdata_manage::list_item_clicked(twindow& /*window*/)
{
}
bool tdata_manage::filter_text_changed(ttext_* textbox, const std::string& text)
{
twindow& window = *textbox->get_window();
tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
const std::vector<std::string> words = utils::split(text, ' ');
if(words == last_words_)
return false;
last_words_ = words;
std::vector<bool> show_items(list.get_item_count(), true);
if(!text.empty()) {
for(unsigned int i = 0; i < list.get_item_count(); i++) {
tgrid* row = list.get_row_grid(i);
tgrid::iterator it = row->begin();
tlabel& filename_label
= find_widget<tlabel>(*it, "filename", false);
bool found = false;
FOREACH(const AUTO & word, words)
{
found = std::search(filename_label.label().str().begin(),
filename_label.label().str().end(),
word.begin(),
word.end(),
chars_equal_insensitive)
!= filename_label.label().str().end();
if(!found) {
// one word doesn't match, we don't reach words.end()
break;
}
}
show_items[i] = found;
}
}
list.set_row_shown(show_items);
return false;
}
void tdata_manage::delete_button_callback(twindow& window)
{
tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
const size_t index = size_t(list.get_selected_row());
if(index < games_.size()) {
// See if we should ask the user for deletion confirmation
if(preferences::ask_delete_saves()) {
if(!gui2::tgame_delete::execute(window.video())) {
return;
}
}
// Delete the file
savegame::delete_game(games_[index].name());
// Remove it from the list of saves
games_.erase(games_.begin() + index);
list.remove_row(index);
}
}
} // namespace gui2