/
filechooser.cpp
278 lines (243 loc) · 8.31 KB
/
filechooser.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
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.
*/
#include "global.hpp"
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "display.hpp"
#include "gettext.hpp"
#include "gui/dialogs/folder_create.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "filechooser.hpp"
#include "widgets/file_menu.hpp"
namespace dialogs
{
int show_file_chooser_dialog(CVideo& video, std::string &filename,
std::string const &title, bool show_directory_buttons,
const std::string& type_a_head,
int xloc, int yloc) {
file_dialog d(video, filename, title, "", show_directory_buttons);
if (!type_a_head.empty())
d.select_file(type_a_head);
if(d.show(xloc, yloc) >= 0) {
filename = d.get_choice();
}
return d.result();
}
int show_file_chooser_dialog_save(CVideo& video, std::string &filename,
std::string const &title,
const std::string& default_file_name,
bool show_directory_buttons,
const std::string& type_a_head,
int xloc, int yloc) {
file_dialog d(video, filename, title, default_file_name, show_directory_buttons);
d.set_autocomplete(false);
if (!type_a_head.empty())
d.select_file(type_a_head);
if(d.show(xloc, yloc) >= 0) {
filename = d.get_choice();
}
return d.result();
}
file_dialog::file_dialog(CVideo& video, const std::string& file_path,
const std::string& title, const std::string& default_file_name,
bool show_directory_buttons) :
gui::dialog(video, title, file_path, gui::OK_CANCEL),
show_directory_buttons_(show_directory_buttons),
files_list_(NULL),
last_selection_(-1),
last_textbox_text_(),
chosen_file_(".."),
autocomplete_(true)
{
files_list_ = new gui::file_menu(video, file_path);
const unsigned file_list_height = (display::get_singleton()->h() / 2);
const unsigned file_list_width = std::min<unsigned>(files_list_->width(), (display::get_singleton()->w() / 4));
files_list_->set_measurements(file_list_width, file_list_height);
files_list_->set_max_height(file_list_height);
set_menu(files_list_);
default_file_name_ = default_file_name;
get_message().set_text(format_dirname(files_list_->get_directory()));
set_textbox(_("File: "), format_filename(file_path), 100);
if (show_directory_buttons_)
{
add_button( new gui::dialog_button(video, _("Delete File"),
gui::button::TYPE_PRESS, gui::DELETE_ITEM), dialog::BUTTON_EXTRA);
add_button( new gui::dialog_button(video, _("New Folder"),
gui::button::TYPE_PRESS, gui::CREATE_ITEM), dialog::BUTTON_EXTRA_LEFT);
}
}
gui::dialog::dimension_measurements file_dialog::layout(int xloc, int yloc)
{
gui::dialog::dimension_measurements dim = dialog::layout(xloc, yloc);
//shift the menu up
unsigned y_shift = dim.menu_y - std::min<int>(dim.label_y, dim.textbox.y);
int y_max = dim.menu_y + get_menu().height();
dim.menu_y -= y_shift;
//shift the extra buttons up
if (show_directory_buttons_)
{
std::map<gui::dialog_button *, std::pair<int,int> >::iterator i;
for(i = dim.buttons.begin(); i != dim.buttons.end(); ++i)
{
const int btn_h = i->first->height();
int& btn_y = i->second.second;
y_max = std::max<int>(y_max, btn_y + btn_h);
btn_y -= y_shift;
}
}
//shift the textbox down
const int textbox_bottom_y = dim.textbox.y + get_textbox().height();
const int label_bottom_y = dim.label_y + get_textbox().get_label()->height();
y_shift = y_max - std::max<int>(textbox_bottom_y, label_bottom_y);
dim.textbox.y += y_shift;
dim.label_y += y_shift;
set_layout(dim);
return dim;
}
std::string file_dialog::unformat_filename(const std::string& filename) const
{
return files_list_->add_path(files_list_->get_directory(), filename);
}
std::string file_dialog::format_filename(const std::string& filename) const
{
if(files_list_->is_directory(filename)) {
return default_file_name_;
}
std::string::size_type last_delim = filename.find_last_of(gui::file_menu::path_delim);
if(last_delim != std::string::npos) {
return filename.substr(last_delim + 1);
}
return filename;
}
void file_dialog::select_file(const std::string& file)
{
chosen_file_ = file;
get_textbox().set_text(format_filename(chosen_file_));
files_list_->select_file(file);
}
std::string file_dialog::format_dirname(const std::string& dirname) const
{
int menu_font_size = font::SIZE_NORMAL;
std::string tmp = files_list_->strip_last_delim(dirname);
// If the text get out of bounds, make it shorter;
// Take the prefix of the dir (ie. /home/ or c:/) put three dot's behind it
// and shorten the rest of the dir:
// /home/.../rest_of_the_dir
// Note that this is a dirty hack and fundamental changes in the widget subdir
// needs to be made...
if(font::line_width(tmp, menu_font_size) <= 390) {
return tmp;
}
static const int filler_width = font::line_width("...", menu_font_size);
// Find the first part of the dir
std::string dir_prefix = "/";
std::string::size_type pos_first = 0;
if((pos_first = tmp.find_first_of("\\/", 1)) != std::string::npos)
{
dir_prefix = tmp.substr(0, pos_first) + "/...";
tmp = tmp.substr(pos_first);
}
static const int prefix_width = font::line_width(dir_prefix, menu_font_size);
// Try to cut off text at the '/' or '\' tokens
while(font::line_width(tmp, menu_font_size) + filler_width + prefix_width > 390 && tmp.length() != 0)
{
std::string::size_type pos;
if((pos = tmp.find_first_of("\\/", 1)) != std::string::npos)
tmp = tmp.substr(pos, tmp.length());
else
tmp = tmp.substr(1, tmp.length());
}
return dir_prefix + tmp;
}
void file_dialog::set_save_text(const std::string& filename)
{
const std::string fn = format_filename(filename);
const size_t filename_dot = fn.find_last_of('.');
get_textbox().set_text(fn);
get_textbox().set_selection(0, filename_dot != std::string::npos ? filename_dot : fn.length());
get_textbox().set_cursor_pos(0);
}
void file_dialog::action(gui::dialog_process_info &dp_info)
{
if(result() == gui::CLOSE_DIALOG)
return;
//handle "delete item" requests
if(result() == gui::DELETE_ITEM)
{
if(!chosen_file_.empty())
{
if(files_list_->delete_chosen_file() == -1) {
gui2::show_transient_error_message(get_video()
, _("Deletion of the file failed."));
dp_info.clear_buttons();
} else {
dp_info.first_time = true;
}
}
set_result(gui::CONTINUE_DIALOG);
}
//handle "create item" requests
else if(result() == gui::CREATE_ITEM)
{
std::string new_dir_name = "";
if(gui2::tfolder_create::execute(new_dir_name, get_video()))
{
if( !files_list_->make_directory(new_dir_name) ) {
gui2::show_transient_error_message(get_video()
, _("Creation of the directory failed."));
} else {
dp_info.first_time = true;
}
}
dp_info.clear_buttons();
set_result(gui::CONTINUE_DIALOG);
}
//update the chosen file
if((dp_info.selection != last_selection_
|| dp_info.first_time
|| dp_info.double_clicked)
&& (!files_list_->type_a_head()
|| dp_info.new_left_button))
{
files_list_->reset_type_a_head();
chosen_file_ = files_list_->get_choice();
set_save_text(chosen_file_);
last_selection_ = (dp_info.double_clicked) ? -1 : dp_info.selection;
last_textbox_text_ = textbox_text();
}
else if(textbox_text() != last_textbox_text_)
{
chosen_file_ = unformat_filename(textbox_text());
last_textbox_text_ = textbox_text();
// Do type-a-head search in listbox
if (autocomplete_) {
files_list_->select_file(textbox_text());
}
}
if(result() >=0) {
//if a directory has been chosen, enter it
if(files_list_->is_directory(chosen_file_))
{
files_list_->change_directory(chosen_file_);
get_message().set_text(format_dirname(files_list_->get_directory()));
//reset the chosen file
chosen_file_ = "..";
get_textbox().set_text(format_filename(chosen_file_));
set_result(gui::CONTINUE_DIALOG);
}
//if a file has been chosen, return button index "Ok"
else
{
set_result(0);
}
}
}
} //end namespace dialogs