| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <span> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "event.h" |
| 25 |
#include "maths.h" |
| 26 |
#include "pstypes.h" |
| 27 |
#include "gr.h" |
| 28 |
#include "key.h" |
| 29 |
#include "mouse.h" |
| 30 |
#include "strutil.h" |
| 31 |
#include "ui.h" |
| 32 |
#include "window.h" |
| 33 |
#include "u_mem.h" |
| 34 |
#include "physfsx.h" |
| 35 |
#include "physfs_list.h" |
| 36 |
|
| 37 |
#include "d_range.h" |
| 38 |
#include <memory> |
| 39 |
|
| 40 |
namespace dcx { |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
static PHYSFSX_counted_list file_getdirlist(const std::span<const char, PATH_MAX> dir) |
| 45 |
{ |
| 46 |
ntstring<PATH_MAX - 1> path; |
| 47 |
auto dlen = path.copy_if(dir.data()); |
| 48 |
if ((!dlen && dir[0] != '\0') || !path.copy_if(dlen, "/")) |
| 49 |
return {}; |
| 50 |
++ dlen; |
| 51 |
PHYSFSX_uncounted_list list{PHYSFS_enumerateFiles(dir.data())}; |
| 52 |
if (!list) |
| 53 |
return {}; |
| 54 |
const auto predicate = [&](char *i) -> bool { |
| 55 |
if (path.copy_if(dlen, i) && PHYSFS_isDirectory(path)) |
| 56 |
return false; |
| 57 |
free(i); |
| 58 |
return true; |
| 59 |
}; |
| 60 |
const auto j = std::ranges::remove_if(list, predicate).begin(); |
| 61 |
*j = nullptr; |
| 62 |
auto NumDirs = j.get() - list.get(); |
| 63 |
qsort(list.get(), NumDirs, sizeof(char *), string_array_sort_func); |
| 64 |
if (dir[0]) |
| 65 |
{ |
| 66 |
// Put the 'go to parent directory' sequence '..' first |
| 67 |
++NumDirs; |
| 68 |
auto r = reinterpret_cast<char **>(realloc(list.get(), sizeof(char *)*(NumDirs + 1))); |
| 69 |
if (!r) |
| 70 |
return {}; |
| 71 |
list.release(); |
| 72 |
list = PHYSFSX_uncounted_list{r}; |
| 73 |
std::move_backward(r, r + NumDirs, r + NumDirs + 1); |
| 74 |
r[0] = strdup(".."); |
| 75 |
} |
| 76 |
return {std::move(list), static_cast<std::size_t>(NumDirs)}; |
| 77 |
} |
| 78 |
|
| 79 |
static PHYSFSX_counted_list file_getfilelist(const char *filespec, const char *dir) |
| 80 |
{ |
| 81 |
PHYSFSX_uncounted_list list{PHYSFS_enumerateFiles(dir)}; |
| 82 |
if (!list) |
| 83 |
return {}; |
| 84 |
|
| 85 |
if (*filespec == '*') |
| 86 |
filespec++; |
| 87 |
|
| 88 |
const auto predicate = [&](char *i) -> bool { |
| 89 |
auto ext = strrchr(i, '.'); |
| 90 |
if (ext && (!d_stricmp(ext, filespec))) |
| 91 |
return false; |
| 92 |
free(i); |
| 93 |
return true; |
| 94 |
}; |
| 95 |
const auto j = std::ranges::remove_if(list, predicate).begin(); |
| 96 |
*j = nullptr; |
| 97 |
auto NumFiles = j.get() - list.get(); |
| 98 |
qsort(list.get(), NumFiles, sizeof(char *), string_array_sort_func); |
| 99 |
return {std::move(list), static_cast<std::size_t>(NumFiles)}; |
| 100 |
} |
| 101 |
|
| 102 |
struct ui_file_browser : UI_DIALOG |
| 103 |
{ |
| 104 |
static constexpr auto spaces = " "; |
| 105 |
char *filename; |
| 106 |
const char *filespec; |
| 107 |
const char *message; |
| 108 |
PHYSFSX_counted_list filename_list; |
| 109 |
PHYSFSX_counted_list directory_list; |
| 110 |
std::unique_ptr<UI_GADGET_BUTTON> button1, button2, help_button; |
| 111 |
std::unique_ptr<UI_GADGET_LISTBOX> listbox1, listbox2; |
| 112 |
std::unique_ptr<UI_GADGET_INPUTBOX> user_file; |
| 113 |
std::array<char, PATH_MAX> view_dir; |
| 114 |
explicit ui_file_browser(short x, short y, short w, short h, enum dialog_flags flags, const std::array<char, PATH_MAX> &view_dir, PHYSFSX_counted_list &&filename, PHYSFSX_counted_list &&directory) : |
| 115 |
UI_DIALOG(x, y, w, h, flags), |
| 116 |
filename_list(std::move(filename)), |
| 117 |
directory_list(std::move(directory)), |
| 118 |
view_dir(view_dir) |
| 119 |
{ |
| 120 |
} |
| 121 |
virtual window_event_result callback_handler(const d_event &) override; |
| 122 |
}; |
| 123 |
|
| 124 |
window_event_result ui_file_browser::callback_handler(const d_event &event) |
| 125 |
{ |
| 126 |
window_event_result rval = window_event_result::ignored; |
| 127 |
|
| 128 |
if (event.type == event_type::ui_dialog_draw) |
| 129 |
{ |
| 130 |
ui_dputs_at(this, 10, 5, message); |
| 131 |
|
| 132 |
ui_dputs_at(this, 20, 32, "N&ame"); |
| 133 |
ui_dputs_at(this, 20, 86, "&Files"); |
| 134 |
ui_dputs_at(this, 210, 86, "&Dirs"); |
| 135 |
|
| 136 |
ui_dputs_at(this, 20, 60, spaces); |
| 137 |
ui_dputs_at(this, 20, 60, view_dir.data()); |
| 138 |
|
| 139 |
return window_event_result::handled; |
| 140 |
} |
| 141 |
|
| 142 |
if (GADGET_PRESSED(button2.get())) |
| 143 |
{ |
| 144 |
filename_list.reset(); |
| 145 |
return window_event_result::close; |
| 146 |
} |
| 147 |
|
| 148 |
if (GADGET_PRESSED(help_button.get())) |
| 149 |
{ |
| 150 |
ui_messagebox( -1, -1, 1, "Sorry, no help is available!", "Ok" ); |
| 151 |
rval = window_event_result::handled; |
| 152 |
} |
| 153 |
if (event.type == event_type::ui_listbox_moved) |
| 154 |
{ |
| 155 |
if (&ui_event_get_gadget(event) == listbox1.get() && listbox1->current_item >= 0 && filename_list[listbox1->current_item]) |
| 156 |
ui_inputbox_set_text(user_file.get(), filename_list[listbox1->current_item]); |
| 157 |
|
| 158 |
if (&ui_event_get_gadget(event) == listbox2.get() && listbox2->current_item >= 0 && directory_list[listbox2->current_item]) |
| 159 |
ui_inputbox_set_text(user_file.get(), directory_list[listbox2->current_item]); |
| 160 |
|
| 161 |
rval = window_event_result::handled; |
| 162 |
} |
| 163 |
if (GADGET_PRESSED(button1.get()) || GADGET_PRESSED(user_file.get()) || event.type == event_type::ui_listbox_selected) |
| 164 |
{ |
| 165 |
char *p; |
| 166 |
|
| 167 |
if (&ui_event_get_gadget(event) == listbox2.get()) |
| 168 |
strcpy(user_file->text.get(), directory_list[listbox2->current_item]); |
| 169 |
|
| 170 |
strncpy(filename, view_dir.data(), PATH_MAX); |
| 171 |
|
| 172 |
p = user_file->text.get(); |
| 173 |
while (!strncmp(p, "..", 2)) // shorten the path manually |
| 174 |
{ |
| 175 |
char *sep = strrchr(filename, '/'); |
| 176 |
if (sep) |
| 177 |
*sep = 0; |
| 178 |
else |
| 179 |
*filename = 0; // look directly in search paths |
| 180 |
|
| 181 |
p += 2; |
| 182 |
if (*p == '/') |
| 183 |
p++; |
| 184 |
} |
| 185 |
|
| 186 |
if (*filename && *p) |
| 187 |
strncat(filename, "/", PATH_MAX - strlen(filename)); |
| 188 |
strncat(filename, p, PATH_MAX - strlen(filename)); |
| 189 |
|
| 190 |
if (!PHYSFS_isDirectory(filename)) |
| 191 |
{ |
| 192 |
if (RAIIPHYSFS_File{PHYSFS_openRead(filename)}) |
| 193 |
{ |
| 194 |
// Looks like a valid filename that already exists! |
| 195 |
return window_event_result::close; |
| 196 |
} |
| 197 |
|
| 198 |
// File doesn't exist, but can we create it? |
| 199 |
if (RAIIPHYSFS_File TempFile{PHYSFS_openWrite(filename)}) |
| 200 |
{ |
| 201 |
TempFile.reset(); |
| 202 |
// Looks like a valid filename! |
| 203 |
PHYSFS_delete(filename); |
| 204 |
return window_event_result::close; |
| 205 |
} |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
if (auto &last = filename[strlen(filename) - 1]; last == '/') // user typed a separator on the end |
| 210 |
last = 0; |
| 211 |
|
| 212 |
strcpy(view_dir.data(), filename); |
| 213 |
filename_list = file_getfilelist(filespec, view_dir.data()); |
| 214 |
if (!filename_list) |
| 215 |
{ |
| 216 |
return window_event_result::close; |
| 217 |
} |
| 218 |
|
| 219 |
ui_inputbox_set_text(user_file.get(), filespec); |
| 220 |
directory_list = file_getdirlist(view_dir); |
| 221 |
if (!directory_list) |
| 222 |
{ |
| 223 |
filename_list.reset(); |
| 224 |
return window_event_result::close; |
| 225 |
} |
| 226 |
|
| 227 |
ui_listbox_change(this, listbox1.get(), filename_list.get_count(), filename_list.get()); |
| 228 |
ui_listbox_change(this, listbox2.get(), directory_list.get_count(), directory_list.get()); |
| 229 |
} |
| 230 |
|
| 231 |
rval = window_event_result::handled; |
| 232 |
} |
| 233 |
return rval; |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
int ui_get_filename(std::array<char, PATH_MAX> &filename, const char *const filespec, const char *const message) |
| 239 |
{ |
| 240 |
std::array<char, PATH_MAX>::iterator InputText; |
| 241 |
std::size_t InputLength; |
| 242 |
int rval = 0; |
| 243 |
std::array<char, PATH_MAX> view_dir; |
| 244 |
if (const auto p = strrchr(filename.data(), '/')) |
| 245 |
{ |
| 246 |
const auto count = std::distance(filename.begin(), p); |
| 247 |
if (count >= view_dir.size()) |
| 248 |
/* This should never happen. */ |
| 249 |
return 0; |
| 250 |
std::copy(std::begin(filename), p, std::begin(view_dir)); |
| 251 |
InputText = std::next(p); |
| 252 |
InputLength = std::distance(InputText, filename.end()); |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
view_dir.front() = 0; |
| 257 |
InputText = filename.begin(); |
| 258 |
InputLength = filename.size(); |
| 259 |
} |
| 260 |
|
| 261 |
auto dlg = ({ |
| 262 |
auto &&filename_list = file_getfilelist(filespec, view_dir.data()); |
| 263 |
if (!filename_list) |
| 264 |
{ |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
auto &&directory_list = file_getdirlist(view_dir); |
| 269 |
if (!directory_list) |
| 270 |
{ |
| 271 |
return 0; |
| 272 |
} |
| 273 |
window_create<ui_file_browser>(200, 100, 400, 370, static_cast<dialog_flags>(DF_DIALOG | DF_MODAL), view_dir, std::move(filename_list), std::move(directory_list)); |
| 274 |
}); |
| 275 |
|
| 276 |
dlg->user_file = ui_add_gadget_inputbox(*dlg, 60, 30, InputLength, 40, InputText); |
| 277 |
|
| 278 |
dlg->listbox1 = ui_add_gadget_listbox(*dlg, 20, 110, 125, 200, dlg->filename_list.get_count(), dlg->filename_list.get()); |
| 279 |
dlg->listbox2 = ui_add_gadget_listbox(*dlg, 210, 110, 100, 200, dlg->directory_list.get_count(), dlg->directory_list.get()); |
| 280 |
|
| 281 |
dlg->button1 = ui_add_gadget_button(*dlg, 20, 330, 60, 25, "Ok", nullptr); |
| 282 |
dlg->button2 = ui_add_gadget_button(*dlg, 100, 330, 60, 25, "Cancel", nullptr); |
| 283 |
dlg->help_button = ui_add_gadget_button(*dlg, 180, 330, 60, 25, "Help", nullptr); |
| 284 |
|
| 285 |
dlg->keyboard_focus_gadget = dlg->user_file.get(); |
| 286 |
|
| 287 |
dlg->button1->hotkey = KEY_CTRLED + KEY_ENTER; |
| 288 |
dlg->button2->hotkey = KEY_ESC; |
| 289 |
dlg->help_button->hotkey = KEY_F1; |
| 290 |
dlg->listbox1->hotkey = KEY_ALTED + KEY_F; |
| 291 |
dlg->listbox2->hotkey = KEY_ALTED + KEY_D; |
| 292 |
dlg->user_file->hotkey = KEY_ALTED + KEY_A; |
| 293 |
|
| 294 |
ui_gadget_calc_keys(*dlg); |
| 295 |
|
| 296 |
dlg->filename = filename.data(); |
| 297 |
dlg->filespec = filespec; |
| 298 |
dlg->message = message; |
| 299 |
|
| 300 |
event_process_all(); |
| 301 |
rval = static_cast<bool>(dlg->filename_list); |
| 302 |
|
| 303 |
//key_flush(); |
| 304 |
return rval; |
| 305 |
} |
| 306 |
|
| 307 |
int ui_get_file( char * filename, const char * Filespec ) |
| 308 |
{ |
| 309 |
int x; |
| 310 |
auto list = file_getfilelist(Filespec, ""); |
| 311 |
if (!list) return 0; |
| 312 |
x = MenuX(-1, -1, list.get_count(), list.get()); |
| 313 |
if (x > 0) |
| 314 |
strcpy(filename, list[x - 1]); |
| 315 |
return (x > 0); |
| 316 |
} |
| 317 |
|
| 318 |
} |