file_dialog.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /**************************************************************************/
  2. /* file_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "file_dialog.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/string/print_string.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/theme/theme_db.h"
  35. FileDialog::GetIconFunc FileDialog::get_icon_func = nullptr;
  36. FileDialog::RegisterFunc FileDialog::register_func = nullptr;
  37. FileDialog::RegisterFunc FileDialog::unregister_func = nullptr;
  38. void FileDialog::popup_file_dialog() {
  39. popup_centered_clamped(Size2i(700, 500), 0.8f);
  40. _focus_file_text();
  41. }
  42. void FileDialog::_focus_file_text() {
  43. int lp = file->get_text().rfind(".");
  44. if (lp != -1) {
  45. file->select(0, lp);
  46. if (file->is_inside_tree() && !get_tree()->is_node_being_edited(file)) {
  47. file->grab_focus();
  48. }
  49. }
  50. }
  51. void FileDialog::popup(const Rect2i &p_rect) {
  52. #ifdef TOOLS_ENABLED
  53. if (is_part_of_edited_scene()) {
  54. ConfirmationDialog::popup(p_rect);
  55. }
  56. #endif
  57. if (access == ACCESS_FILESYSTEM && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG) && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  58. DisplayServer::get_singleton()->file_dialog_show(get_title(), dir->get_text(), file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), filters, callable_mp(this, &FileDialog::_native_dialog_cb));
  59. } else {
  60. ConfirmationDialog::popup(p_rect);
  61. }
  62. }
  63. void FileDialog::set_visible(bool p_visible) {
  64. #ifdef TOOLS_ENABLED
  65. if (is_part_of_edited_scene()) {
  66. ConfirmationDialog::set_visible(p_visible);
  67. return;
  68. }
  69. #endif
  70. if (access == ACCESS_FILESYSTEM && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG) && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  71. if (p_visible) {
  72. DisplayServer::get_singleton()->file_dialog_show(get_title(), dir->get_text(), file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), filters, callable_mp(this, &FileDialog::_native_dialog_cb));
  73. }
  74. } else {
  75. ConfirmationDialog::set_visible(p_visible);
  76. }
  77. }
  78. void FileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter) {
  79. if (p_ok) {
  80. if (p_files.size() > 0) {
  81. String f = p_files[0];
  82. if (mode == FILE_MODE_OPEN_FILES) {
  83. emit_signal(SNAME("files_selected"), p_files);
  84. } else {
  85. if (mode == FILE_MODE_SAVE_FILE) {
  86. emit_signal(SNAME("file_selected"), f);
  87. } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  88. emit_signal(SNAME("file_selected"), f);
  89. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  90. emit_signal(SNAME("dir_selected"), f);
  91. }
  92. }
  93. file->set_text(f);
  94. dir->set_text(f.get_base_dir());
  95. _filter_selected(p_filter);
  96. }
  97. } else {
  98. file->set_text("");
  99. emit_signal(SNAME("canceled"));
  100. }
  101. }
  102. VBoxContainer *FileDialog::get_vbox() {
  103. return vbox;
  104. }
  105. void FileDialog::_validate_property(PropertyInfo &p_property) const {
  106. if (p_property.name == "dialog_text") {
  107. // File dialogs have a custom layout, and dialog nodes can't have both a text and a layout.
  108. p_property.usage = PROPERTY_USAGE_NONE;
  109. }
  110. }
  111. void FileDialog::_notification(int p_what) {
  112. switch (p_what) {
  113. case NOTIFICATION_VISIBILITY_CHANGED: {
  114. if (!is_visible()) {
  115. set_process_shortcut_input(false);
  116. }
  117. invalidate(); // Put it here to preview in the editor.
  118. } break;
  119. case NOTIFICATION_THEME_CHANGED: {
  120. dir_up->set_icon(theme_cache.parent_folder);
  121. if (vbox->is_layout_rtl()) {
  122. dir_prev->set_icon(theme_cache.forward_folder);
  123. dir_next->set_icon(theme_cache.back_folder);
  124. } else {
  125. dir_prev->set_icon(theme_cache.back_folder);
  126. dir_next->set_icon(theme_cache.forward_folder);
  127. }
  128. refresh->set_icon(theme_cache.reload);
  129. show_hidden->set_icon(theme_cache.toggle_hidden);
  130. dir_up->begin_bulk_theme_override();
  131. dir_up->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  132. dir_up->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  133. dir_up->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  134. dir_up->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  135. dir_up->end_bulk_theme_override();
  136. dir_prev->begin_bulk_theme_override();
  137. dir_prev->add_theme_color_override("icon_color_normal", theme_cache.icon_normal_color);
  138. dir_prev->add_theme_color_override("icon_color_hover", theme_cache.icon_hover_color);
  139. dir_prev->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  140. dir_prev->add_theme_color_override("icon_color_pressed", theme_cache.icon_pressed_color);
  141. dir_prev->end_bulk_theme_override();
  142. dir_next->begin_bulk_theme_override();
  143. dir_next->add_theme_color_override("icon_color_normal", theme_cache.icon_normal_color);
  144. dir_next->add_theme_color_override("icon_color_hover", theme_cache.icon_hover_color);
  145. dir_next->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  146. dir_next->add_theme_color_override("icon_color_pressed", theme_cache.icon_pressed_color);
  147. dir_next->end_bulk_theme_override();
  148. refresh->begin_bulk_theme_override();
  149. refresh->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  150. refresh->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  151. refresh->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  152. refresh->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  153. refresh->end_bulk_theme_override();
  154. show_hidden->begin_bulk_theme_override();
  155. show_hidden->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  156. show_hidden->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  157. show_hidden->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  158. show_hidden->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  159. show_hidden->end_bulk_theme_override();
  160. invalidate();
  161. } break;
  162. case NOTIFICATION_TRANSLATION_CHANGED: {
  163. update_filters();
  164. } break;
  165. }
  166. }
  167. void FileDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  168. ERR_FAIL_COND(p_event.is_null());
  169. Ref<InputEventKey> k = p_event;
  170. if (k.is_valid() && has_focus()) {
  171. if (k->is_pressed()) {
  172. bool handled = true;
  173. switch (k->get_keycode()) {
  174. case Key::H: {
  175. if (k->is_command_or_control_pressed()) {
  176. set_show_hidden_files(!show_hidden_files);
  177. } else {
  178. handled = false;
  179. }
  180. } break;
  181. case Key::F5: {
  182. invalidate();
  183. } break;
  184. case Key::BACKSPACE: {
  185. _dir_submitted("..");
  186. } break;
  187. default: {
  188. handled = false;
  189. }
  190. }
  191. if (handled) {
  192. set_input_as_handled();
  193. }
  194. }
  195. }
  196. }
  197. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  198. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  199. }
  200. Vector<String> FileDialog::get_selected_files() const {
  201. Vector<String> list;
  202. TreeItem *item = tree->get_root();
  203. item = tree->get_next_selected(item);
  204. while (item) {
  205. list.push_back(dir_access->get_current_dir().path_join(item->get_text(0)));
  206. item = tree->get_next_selected(item);
  207. }
  208. return list;
  209. }
  210. void FileDialog::update_dir() {
  211. if (root_prefix.is_empty()) {
  212. dir->set_text(dir_access->get_current_dir(false));
  213. } else {
  214. dir->set_text(dir_access->get_current_dir(false).trim_prefix(root_prefix).trim_prefix("/"));
  215. }
  216. if (drives->is_visible()) {
  217. if (dir_access->get_current_dir().is_network_share_path()) {
  218. _update_drives(false);
  219. drives->add_item(RTR("Network"));
  220. drives->set_item_disabled(-1, true);
  221. drives->select(drives->get_item_count() - 1);
  222. } else {
  223. drives->select(dir_access->get_current_drive());
  224. }
  225. }
  226. // Deselect any item, to make "Select Current Folder" button text by default.
  227. deselect_all();
  228. }
  229. void FileDialog::_dir_submitted(String p_dir) {
  230. _change_dir(root_prefix.path_join(p_dir));
  231. file->set_text("");
  232. _push_history();
  233. }
  234. void FileDialog::_file_submitted(const String &p_file) {
  235. _action_pressed();
  236. }
  237. void FileDialog::_save_confirm_pressed() {
  238. String f = dir_access->get_current_dir().path_join(file->get_text());
  239. emit_signal(SNAME("file_selected"), f);
  240. hide();
  241. }
  242. void FileDialog::_post_popup() {
  243. ConfirmationDialog::_post_popup();
  244. if (mode == FILE_MODE_SAVE_FILE) {
  245. file->grab_focus();
  246. } else {
  247. tree->grab_focus();
  248. }
  249. set_process_shortcut_input(true);
  250. // For open dir mode, deselect all items on file dialog open.
  251. if (mode == FILE_MODE_OPEN_DIR) {
  252. deselect_all();
  253. file_box->set_visible(false);
  254. } else {
  255. file_box->set_visible(true);
  256. }
  257. local_history.clear();
  258. local_history_pos = -1;
  259. _push_history();
  260. }
  261. void FileDialog::_push_history() {
  262. local_history.resize(local_history_pos + 1);
  263. String new_path = dir_access->get_current_dir();
  264. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  265. local_history.push_back(new_path);
  266. local_history_pos++;
  267. dir_prev->set_disabled(local_history_pos == 0);
  268. dir_next->set_disabled(true);
  269. }
  270. }
  271. void FileDialog::_action_pressed() {
  272. if (mode == FILE_MODE_OPEN_FILES) {
  273. TreeItem *ti = tree->get_next_selected(nullptr);
  274. String fbase = dir_access->get_current_dir();
  275. Vector<String> files;
  276. while (ti) {
  277. files.push_back(fbase.path_join(ti->get_text(0)));
  278. ti = tree->get_next_selected(ti);
  279. }
  280. if (files.size()) {
  281. emit_signal(SNAME("files_selected"), files);
  282. hide();
  283. }
  284. return;
  285. }
  286. String file_text = file->get_text();
  287. String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text);
  288. if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  289. emit_signal(SNAME("file_selected"), f);
  290. hide();
  291. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  292. String path = dir_access->get_current_dir();
  293. path = path.replace("\\", "/");
  294. TreeItem *item = tree->get_selected();
  295. if (item) {
  296. Dictionary d = item->get_metadata(0);
  297. if (d["dir"] && d["name"] != "..") {
  298. path = path.path_join(d["name"]);
  299. }
  300. }
  301. emit_signal(SNAME("dir_selected"), path);
  302. hide();
  303. }
  304. if (mode == FILE_MODE_SAVE_FILE) {
  305. bool valid = false;
  306. if (filter->get_selected() == filter->get_item_count() - 1) {
  307. valid = true; // match none
  308. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  309. // match all filters
  310. for (int i = 0; i < filters.size(); i++) {
  311. String flt = filters[i].get_slice(";", 0);
  312. for (int j = 0; j < flt.get_slice_count(","); j++) {
  313. String str = flt.get_slice(",", j).strip_edges();
  314. if (f.match(str)) {
  315. valid = true;
  316. break;
  317. }
  318. }
  319. if (valid) {
  320. break;
  321. }
  322. }
  323. } else {
  324. int idx = filter->get_selected();
  325. if (filters.size() > 1) {
  326. idx--;
  327. }
  328. if (idx >= 0 && idx < filters.size()) {
  329. String flt = filters[idx].get_slice(";", 0);
  330. int filterSliceCount = flt.get_slice_count(",");
  331. for (int j = 0; j < filterSliceCount; j++) {
  332. String str = (flt.get_slice(",", j).strip_edges());
  333. if (f.match(str)) {
  334. valid = true;
  335. break;
  336. }
  337. }
  338. if (!valid && filterSliceCount > 0) {
  339. String str = (flt.get_slice(",", 0).strip_edges());
  340. f += str.substr(1, str.length() - 1);
  341. file->set_text(f.get_file());
  342. valid = true;
  343. }
  344. } else {
  345. valid = true;
  346. }
  347. }
  348. String file_name = file_text.strip_edges().get_file();
  349. if (!valid || file_name.is_empty()) {
  350. exterr->popup_centered(Size2(250, 80));
  351. return;
  352. }
  353. if (dir_access->file_exists(f)) {
  354. confirm_save->set_text(vformat(RTR("File \"%s\" already exists.\nDo you want to overwrite it?"), f));
  355. confirm_save->popup_centered(Size2(250, 80));
  356. } else {
  357. emit_signal(SNAME("file_selected"), f);
  358. hide();
  359. }
  360. }
  361. }
  362. void FileDialog::_cancel_pressed() {
  363. file->set_text("");
  364. invalidate();
  365. hide();
  366. }
  367. bool FileDialog::_is_open_should_be_disabled() {
  368. if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_SAVE_FILE) {
  369. return false;
  370. }
  371. TreeItem *ti = tree->get_next_selected(tree->get_root());
  372. while (ti) {
  373. TreeItem *prev_ti = ti;
  374. ti = tree->get_next_selected(tree->get_root());
  375. if (ti == prev_ti) {
  376. break;
  377. }
  378. }
  379. // We have something that we can't select?
  380. if (!ti) {
  381. return mode != FILE_MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  382. }
  383. Dictionary d = ti->get_metadata(0);
  384. // Opening a file, but selected a folder? Forbidden.
  385. return ((mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  386. (mode == FILE_MODE_OPEN_DIR && !d["dir"]);
  387. }
  388. void FileDialog::_go_up() {
  389. _change_dir("..");
  390. _push_history();
  391. }
  392. void FileDialog::_go_back() {
  393. if (local_history_pos <= 0) {
  394. return;
  395. }
  396. local_history_pos--;
  397. _change_dir(local_history[local_history_pos]);
  398. dir_prev->set_disabled(local_history_pos == 0);
  399. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  400. }
  401. void FileDialog::_go_forward() {
  402. if (local_history_pos >= local_history.size() - 1) {
  403. return;
  404. }
  405. local_history_pos++;
  406. _change_dir(local_history[local_history_pos]);
  407. dir_prev->set_disabled(local_history_pos == 0);
  408. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  409. }
  410. void FileDialog::deselect_all() {
  411. // Clear currently selected items in file manager.
  412. tree->deselect_all();
  413. // And change get_ok title.
  414. if (!tree->is_anything_selected()) {
  415. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  416. switch (mode) {
  417. case FILE_MODE_OPEN_FILE:
  418. case FILE_MODE_OPEN_FILES:
  419. set_ok_button_text(RTR("Open"));
  420. break;
  421. case FILE_MODE_OPEN_DIR:
  422. set_ok_button_text(RTR("Select Current Folder"));
  423. break;
  424. case FILE_MODE_OPEN_ANY:
  425. case FILE_MODE_SAVE_FILE:
  426. // FIXME: Implement, or refactor to avoid duplication with set_mode
  427. break;
  428. }
  429. }
  430. }
  431. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  432. _tree_selected();
  433. }
  434. void FileDialog::_tree_selected() {
  435. TreeItem *ti = tree->get_selected();
  436. if (!ti) {
  437. return;
  438. }
  439. Dictionary d = ti->get_metadata(0);
  440. if (!d["dir"]) {
  441. file->set_text(d["name"]);
  442. } else if (mode == FILE_MODE_OPEN_DIR) {
  443. set_ok_button_text(RTR("Select This Folder"));
  444. }
  445. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  446. }
  447. void FileDialog::_tree_item_activated() {
  448. TreeItem *ti = tree->get_selected();
  449. if (!ti) {
  450. return;
  451. }
  452. Dictionary d = ti->get_metadata(0);
  453. if (d["dir"]) {
  454. _change_dir(d["name"]);
  455. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  456. file->set_text("");
  457. }
  458. _push_history();
  459. } else {
  460. _action_pressed();
  461. }
  462. }
  463. void FileDialog::update_file_name() {
  464. int idx = filter->get_selected() - 1;
  465. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  466. if (idx == -1) {
  467. idx += 1;
  468. }
  469. String filter_str = filters[idx];
  470. String file_str = file->get_text();
  471. String base_name = file_str.get_basename();
  472. Vector<String> filter_substr = filter_str.split(";");
  473. if (filter_substr.size() >= 2) {
  474. file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower();
  475. } else {
  476. file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower();
  477. }
  478. file->set_text(file_str);
  479. }
  480. }
  481. void FileDialog::update_file_list() {
  482. tree->clear();
  483. // Scroll back to the top after opening a directory
  484. tree->get_vscroll_bar()->set_value(0);
  485. dir_access->list_dir_begin();
  486. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  487. message->hide();
  488. } else {
  489. message->set_text(RTR("You don't have permission to access contents of this folder."));
  490. message->show();
  491. }
  492. TreeItem *root = tree->create_item();
  493. List<String> files;
  494. List<String> dirs;
  495. bool is_hidden;
  496. String item = dir_access->get_next();
  497. while (!item.is_empty()) {
  498. if (item == "." || item == "..") {
  499. item = dir_access->get_next();
  500. continue;
  501. }
  502. is_hidden = dir_access->current_is_hidden();
  503. if (show_hidden_files || !is_hidden) {
  504. if (!dir_access->current_is_dir()) {
  505. files.push_back(item);
  506. } else {
  507. dirs.push_back(item);
  508. }
  509. }
  510. item = dir_access->get_next();
  511. }
  512. dirs.sort_custom<NaturalNoCaseComparator>();
  513. files.sort_custom<NaturalNoCaseComparator>();
  514. while (!dirs.is_empty()) {
  515. String &dir_name = dirs.front()->get();
  516. TreeItem *ti = tree->create_item(root);
  517. ti->set_text(0, dir_name);
  518. ti->set_icon(0, theme_cache.folder);
  519. ti->set_icon_modulate(0, theme_cache.folder_icon_color);
  520. Dictionary d;
  521. d["name"] = dir_name;
  522. d["dir"] = true;
  523. ti->set_metadata(0, d);
  524. dirs.pop_front();
  525. }
  526. List<String> patterns;
  527. // build filter
  528. if (filter->get_selected() == filter->get_item_count() - 1) {
  529. // match all
  530. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  531. // match all filters
  532. for (int i = 0; i < filters.size(); i++) {
  533. String f = filters[i].get_slice(";", 0);
  534. for (int j = 0; j < f.get_slice_count(","); j++) {
  535. patterns.push_back(f.get_slice(",", j).strip_edges());
  536. }
  537. }
  538. } else {
  539. int idx = filter->get_selected();
  540. if (filters.size() > 1) {
  541. idx--;
  542. }
  543. if (idx >= 0 && idx < filters.size()) {
  544. String f = filters[idx].get_slice(";", 0);
  545. for (int j = 0; j < f.get_slice_count(","); j++) {
  546. patterns.push_back(f.get_slice(",", j).strip_edges());
  547. }
  548. }
  549. }
  550. String base_dir = dir_access->get_current_dir();
  551. while (!files.is_empty()) {
  552. bool match = patterns.is_empty();
  553. String match_str;
  554. for (const String &E : patterns) {
  555. if (files.front()->get().matchn(E)) {
  556. match_str = E;
  557. match = true;
  558. break;
  559. }
  560. }
  561. if (match) {
  562. TreeItem *ti = tree->create_item(root);
  563. ti->set_text(0, files.front()->get());
  564. if (get_icon_func) {
  565. Ref<Texture2D> icon = get_icon_func(base_dir.path_join(files.front()->get()));
  566. ti->set_icon(0, icon);
  567. } else {
  568. ti->set_icon(0, theme_cache.file);
  569. }
  570. ti->set_icon_modulate(0, theme_cache.file_icon_color);
  571. if (mode == FILE_MODE_OPEN_DIR) {
  572. ti->set_custom_color(0, theme_cache.file_disabled_color);
  573. ti->set_selectable(0, false);
  574. }
  575. Dictionary d;
  576. d["name"] = files.front()->get();
  577. d["dir"] = false;
  578. ti->set_metadata(0, d);
  579. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  580. ti->select(0);
  581. }
  582. }
  583. files.pop_front();
  584. }
  585. if (mode != FILE_MODE_SAVE_FILE && mode != FILE_MODE_OPEN_DIR) {
  586. // Select the first file from list if nothing is selected.
  587. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  588. tree->get_root()->get_first_child()->select(0);
  589. }
  590. }
  591. }
  592. void FileDialog::_filter_selected(int) {
  593. update_file_name();
  594. update_file_list();
  595. }
  596. void FileDialog::update_filters() {
  597. filter->clear();
  598. if (filters.size() > 1) {
  599. String all_filters;
  600. const int max_filters = 5;
  601. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  602. String flt = filters[i].get_slice(";", 0).strip_edges();
  603. if (i > 0) {
  604. all_filters += ", ";
  605. }
  606. all_filters += flt;
  607. }
  608. if (max_filters < filters.size()) {
  609. all_filters += ", ...";
  610. }
  611. filter->add_item(RTR("All Recognized") + " (" + all_filters + ")");
  612. }
  613. for (int i = 0; i < filters.size(); i++) {
  614. String flt = filters[i].get_slice(";", 0).strip_edges();
  615. String desc = filters[i].get_slice(";", 1).strip_edges();
  616. if (desc.length()) {
  617. filter->add_item(String(tr(desc)) + " (" + flt + ")");
  618. } else {
  619. filter->add_item("(" + flt + ")");
  620. }
  621. }
  622. filter->add_item(RTR("All Files") + " (*)");
  623. }
  624. void FileDialog::clear_filters() {
  625. filters.clear();
  626. update_filters();
  627. invalidate();
  628. }
  629. void FileDialog::add_filter(const String &p_filter, const String &p_description) {
  630. ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
  631. if (p_description.is_empty()) {
  632. filters.push_back(p_filter);
  633. } else {
  634. filters.push_back(vformat("%s ; %s", p_filter, p_description));
  635. }
  636. update_filters();
  637. invalidate();
  638. }
  639. void FileDialog::set_filters(const Vector<String> &p_filters) {
  640. if (filters == p_filters) {
  641. return;
  642. }
  643. filters = p_filters;
  644. update_filters();
  645. invalidate();
  646. }
  647. Vector<String> FileDialog::get_filters() const {
  648. return filters;
  649. }
  650. String FileDialog::get_current_dir() const {
  651. return dir->get_text();
  652. }
  653. String FileDialog::get_current_file() const {
  654. return file->get_text();
  655. }
  656. String FileDialog::get_current_path() const {
  657. return dir->get_text().path_join(file->get_text());
  658. }
  659. void FileDialog::set_current_dir(const String &p_dir) {
  660. _change_dir(p_dir);
  661. _push_history();
  662. }
  663. void FileDialog::set_current_file(const String &p_file) {
  664. if (file->get_text() == p_file) {
  665. return;
  666. }
  667. file->set_text(p_file);
  668. update_dir();
  669. invalidate();
  670. _focus_file_text();
  671. }
  672. void FileDialog::set_current_path(const String &p_path) {
  673. if (!p_path.size()) {
  674. return;
  675. }
  676. int pos = MAX(p_path.rfind("/"), p_path.rfind("\\"));
  677. if (pos == -1) {
  678. set_current_file(p_path);
  679. } else {
  680. String path_dir = p_path.substr(0, pos);
  681. String path_file = p_path.substr(pos + 1, p_path.length());
  682. set_current_dir(path_dir);
  683. set_current_file(path_file);
  684. }
  685. }
  686. void FileDialog::set_root_subfolder(const String &p_root) {
  687. root_subfolder = p_root;
  688. ERR_FAIL_COND_MSG(!dir_access->dir_exists(p_root), "root_subfolder must be an existing sub-directory.");
  689. local_history.clear();
  690. local_history_pos = -1;
  691. dir_access->change_dir(root_subfolder);
  692. if (root_subfolder.is_empty()) {
  693. root_prefix = "";
  694. } else {
  695. root_prefix = dir_access->get_current_dir();
  696. }
  697. invalidate();
  698. update_dir();
  699. }
  700. String FileDialog::get_root_subfolder() const {
  701. return root_subfolder;
  702. }
  703. void FileDialog::set_mode_overrides_title(bool p_override) {
  704. mode_overrides_title = p_override;
  705. }
  706. bool FileDialog::is_mode_overriding_title() const {
  707. return mode_overrides_title;
  708. }
  709. void FileDialog::set_file_mode(FileMode p_mode) {
  710. ERR_FAIL_INDEX((int)p_mode, 5);
  711. if (mode == p_mode) {
  712. return;
  713. }
  714. mode = p_mode;
  715. switch (mode) {
  716. case FILE_MODE_OPEN_FILE:
  717. set_ok_button_text(RTR("Open"));
  718. if (mode_overrides_title) {
  719. set_title(TTRC("Open a File"));
  720. }
  721. makedir->hide();
  722. break;
  723. case FILE_MODE_OPEN_FILES:
  724. set_ok_button_text(RTR("Open"));
  725. if (mode_overrides_title) {
  726. set_title(TTRC("Open File(s)"));
  727. }
  728. makedir->hide();
  729. break;
  730. case FILE_MODE_OPEN_DIR:
  731. set_ok_button_text(RTR("Select Current Folder"));
  732. if (mode_overrides_title) {
  733. set_title(TTRC("Open a Directory"));
  734. }
  735. makedir->show();
  736. break;
  737. case FILE_MODE_OPEN_ANY:
  738. set_ok_button_text(RTR("Open"));
  739. if (mode_overrides_title) {
  740. set_title(TTRC("Open a File or Directory"));
  741. }
  742. makedir->show();
  743. break;
  744. case FILE_MODE_SAVE_FILE:
  745. set_ok_button_text(RTR("Save"));
  746. if (mode_overrides_title) {
  747. set_title(TTRC("Save a File"));
  748. }
  749. makedir->show();
  750. break;
  751. }
  752. if (mode == FILE_MODE_OPEN_FILES) {
  753. tree->set_select_mode(Tree::SELECT_MULTI);
  754. } else {
  755. tree->set_select_mode(Tree::SELECT_SINGLE);
  756. }
  757. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  758. }
  759. FileDialog::FileMode FileDialog::get_file_mode() const {
  760. return mode;
  761. }
  762. void FileDialog::set_access(Access p_access) {
  763. ERR_FAIL_INDEX(p_access, 3);
  764. if (access == p_access) {
  765. return;
  766. }
  767. switch (p_access) {
  768. case ACCESS_FILESYSTEM: {
  769. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  770. } break;
  771. case ACCESS_RESOURCES: {
  772. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  773. } break;
  774. case ACCESS_USERDATA: {
  775. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  776. } break;
  777. }
  778. access = p_access;
  779. root_prefix = "";
  780. root_subfolder = "";
  781. _update_drives();
  782. invalidate();
  783. update_filters();
  784. update_dir();
  785. }
  786. void FileDialog::invalidate() {
  787. if (!is_visible() || is_invalidating) {
  788. return;
  789. }
  790. is_invalidating = true;
  791. callable_mp(this, &FileDialog::_invalidate).call_deferred();
  792. }
  793. void FileDialog::_invalidate() {
  794. if (!is_invalidating) {
  795. return;
  796. }
  797. update_file_list();
  798. is_invalidating = false;
  799. }
  800. FileDialog::Access FileDialog::get_access() const {
  801. return access;
  802. }
  803. void FileDialog::_make_dir_confirm() {
  804. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  805. if (err == OK) {
  806. _change_dir(makedirname->get_text().strip_edges());
  807. update_filters();
  808. _push_history();
  809. } else {
  810. mkdirerr->popup_centered(Size2(250, 50));
  811. }
  812. makedirname->set_text(""); // reset label
  813. }
  814. void FileDialog::_make_dir() {
  815. makedialog->popup_centered(Size2(250, 80));
  816. makedirname->grab_focus();
  817. }
  818. void FileDialog::_select_drive(int p_idx) {
  819. String d = drives->get_item_text(p_idx);
  820. _change_dir(d);
  821. file->set_text("");
  822. _push_history();
  823. }
  824. void FileDialog::_change_dir(const String &p_new_dir) {
  825. if (root_prefix.is_empty()) {
  826. dir_access->change_dir(p_new_dir);
  827. } else {
  828. String old_dir = dir_access->get_current_dir();
  829. dir_access->change_dir(p_new_dir);
  830. if (!dir_access->get_current_dir(false).begins_with(root_prefix)) {
  831. dir_access->change_dir(old_dir);
  832. return;
  833. }
  834. }
  835. invalidate();
  836. update_dir();
  837. }
  838. void FileDialog::_update_drives(bool p_select) {
  839. int dc = dir_access->get_drive_count();
  840. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  841. drives->hide();
  842. } else {
  843. drives->clear();
  844. Node *dp = drives->get_parent();
  845. if (dp) {
  846. dp->remove_child(drives);
  847. }
  848. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  849. dp->add_child(drives);
  850. drives->show();
  851. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  852. drives->add_item(dir_access->get_drive(i));
  853. }
  854. if (p_select) {
  855. drives->select(dir_access->get_current_drive());
  856. }
  857. }
  858. }
  859. bool FileDialog::default_show_hidden_files = false;
  860. void FileDialog::_bind_methods() {
  861. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  862. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  863. ClassDB::bind_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::add_filter, DEFVAL(""));
  864. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  865. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  866. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  867. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  868. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  869. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  870. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  871. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  872. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  873. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  874. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  875. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  876. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  877. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  878. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  879. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  880. ClassDB::bind_method(D_METHOD("set_root_subfolder", "dir"), &FileDialog::set_root_subfolder);
  881. ClassDB::bind_method(D_METHOD("get_root_subfolder"), &FileDialog::get_root_subfolder);
  882. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  883. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  884. ClassDB::bind_method(D_METHOD("set_use_native_dialog", "native"), &FileDialog::set_use_native_dialog);
  885. ClassDB::bind_method(D_METHOD("get_use_native_dialog"), &FileDialog::get_use_native_dialog);
  886. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  887. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  888. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  889. ADD_PROPERTY(PropertyInfo(Variant::INT, "file_mode", PROPERTY_HINT_ENUM, "Open File,Open Files,Open Folder,Open Any,Save"), "set_file_mode", "get_file_mode");
  890. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User Data,File System"), "set_access", "get_access");
  891. ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_subfolder"), "set_root_subfolder", "get_root_subfolder");
  892. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  893. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  894. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_native_dialog"), "set_use_native_dialog", "get_use_native_dialog");
  895. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", PROPERTY_USAGE_NONE), "set_current_dir", "get_current_dir");
  896. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE, "*", PROPERTY_USAGE_NONE), "set_current_file", "get_current_file");
  897. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_path", "get_current_path");
  898. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  899. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  900. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  901. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  902. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  903. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  904. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  905. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  906. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  907. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  908. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  909. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, parent_folder);
  910. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, forward_folder);
  911. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, back_folder);
  912. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, reload);
  913. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_hidden);
  914. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder);
  915. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file);
  916. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, folder_icon_color);
  917. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_icon_color);
  918. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_disabled_color);
  919. // TODO: Define own colors?
  920. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_normal_color, "font_color", "Button");
  921. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_hover_color, "font_hover_color", "Button");
  922. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_focus_color, "font_focus_color", "Button");
  923. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_pressed_color, "font_pressed_color", "Button");
  924. }
  925. void FileDialog::set_show_hidden_files(bool p_show) {
  926. if (show_hidden_files == p_show) {
  927. return;
  928. }
  929. show_hidden_files = p_show;
  930. invalidate();
  931. }
  932. bool FileDialog::is_showing_hidden_files() const {
  933. return show_hidden_files;
  934. }
  935. void FileDialog::set_default_show_hidden_files(bool p_show) {
  936. default_show_hidden_files = p_show;
  937. }
  938. void FileDialog::set_use_native_dialog(bool p_native) {
  939. use_native_dialog = p_native;
  940. }
  941. bool FileDialog::get_use_native_dialog() const {
  942. return use_native_dialog;
  943. }
  944. FileDialog::FileDialog() {
  945. show_hidden_files = default_show_hidden_files;
  946. vbox = memnew(VBoxContainer);
  947. add_child(vbox, false, INTERNAL_MODE_FRONT);
  948. mode = FILE_MODE_SAVE_FILE;
  949. set_title(TTRC("Save a File"));
  950. HBoxContainer *hbc = memnew(HBoxContainer);
  951. dir_prev = memnew(Button);
  952. dir_prev->set_theme_type_variation("FlatButton");
  953. dir_prev->set_tooltip_text(RTR("Go to previous folder."));
  954. dir_next = memnew(Button);
  955. dir_next->set_theme_type_variation("FlatButton");
  956. dir_next->set_tooltip_text(RTR("Go to next folder."));
  957. dir_up = memnew(Button);
  958. dir_up->set_theme_type_variation("FlatButton");
  959. dir_up->set_tooltip_text(RTR("Go to parent folder."));
  960. hbc->add_child(dir_prev);
  961. hbc->add_child(dir_next);
  962. hbc->add_child(dir_up);
  963. dir_prev->connect("pressed", callable_mp(this, &FileDialog::_go_back));
  964. dir_next->connect("pressed", callable_mp(this, &FileDialog::_go_forward));
  965. dir_up->connect("pressed", callable_mp(this, &FileDialog::_go_up));
  966. hbc->add_child(memnew(Label(RTR("Path:"))));
  967. drives_container = memnew(HBoxContainer);
  968. hbc->add_child(drives_container);
  969. drives = memnew(OptionButton);
  970. drives->connect("item_selected", callable_mp(this, &FileDialog::_select_drive));
  971. hbc->add_child(drives);
  972. dir = memnew(LineEdit);
  973. dir->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  974. hbc->add_child(dir);
  975. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  976. refresh = memnew(Button);
  977. refresh->set_theme_type_variation("FlatButton");
  978. refresh->set_tooltip_text(RTR("Refresh files."));
  979. refresh->connect("pressed", callable_mp(this, &FileDialog::update_file_list));
  980. hbc->add_child(refresh);
  981. show_hidden = memnew(Button);
  982. show_hidden->set_theme_type_variation("FlatButton");
  983. show_hidden->set_toggle_mode(true);
  984. show_hidden->set_pressed(is_showing_hidden_files());
  985. show_hidden->set_tooltip_text(RTR("Toggle the visibility of hidden files."));
  986. show_hidden->connect("toggled", callable_mp(this, &FileDialog::set_show_hidden_files));
  987. hbc->add_child(show_hidden);
  988. shortcuts_container = memnew(HBoxContainer);
  989. hbc->add_child(shortcuts_container);
  990. makedir = memnew(Button);
  991. makedir->set_text(RTR("Create Folder"));
  992. makedir->connect("pressed", callable_mp(this, &FileDialog::_make_dir));
  993. hbc->add_child(makedir);
  994. vbox->add_child(hbc);
  995. tree = memnew(Tree);
  996. tree->set_hide_root(true);
  997. vbox->add_margin_child(RTR("Directories & Files:"), tree, true);
  998. message = memnew(Label);
  999. message->hide();
  1000. message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1001. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1002. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1003. tree->add_child(message);
  1004. file_box = memnew(HBoxContainer);
  1005. file_box->add_child(memnew(Label(RTR("File:"))));
  1006. file = memnew(LineEdit);
  1007. file->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1008. file->set_stretch_ratio(4);
  1009. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1010. file_box->add_child(file);
  1011. filter = memnew(OptionButton);
  1012. filter->set_stretch_ratio(3);
  1013. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1014. filter->set_clip_text(true); // too many extensions overflows it
  1015. file_box->add_child(filter);
  1016. vbox->add_child(file_box);
  1017. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1018. _update_drives();
  1019. connect("confirmed", callable_mp(this, &FileDialog::_action_pressed));
  1020. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), CONNECT_DEFERRED);
  1021. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), CONNECT_DEFERRED);
  1022. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated));
  1023. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  1024. dir->connect("text_submitted", callable_mp(this, &FileDialog::_dir_submitted));
  1025. file->connect("text_submitted", callable_mp(this, &FileDialog::_file_submitted));
  1026. filter->connect("item_selected", callable_mp(this, &FileDialog::_filter_selected));
  1027. confirm_save = memnew(ConfirmationDialog);
  1028. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  1029. confirm_save->connect("confirmed", callable_mp(this, &FileDialog::_save_confirm_pressed));
  1030. makedialog = memnew(ConfirmationDialog);
  1031. makedialog->set_title(RTR("Create Folder"));
  1032. VBoxContainer *makevb = memnew(VBoxContainer);
  1033. makedialog->add_child(makevb);
  1034. makedirname = memnew(LineEdit);
  1035. makedirname->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1036. makevb->add_margin_child(RTR("Name:"), makedirname);
  1037. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  1038. makedialog->register_text_enter(makedirname);
  1039. makedialog->connect("confirmed", callable_mp(this, &FileDialog::_make_dir_confirm));
  1040. mkdirerr = memnew(AcceptDialog);
  1041. mkdirerr->set_text(RTR("Could not create folder."));
  1042. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  1043. exterr = memnew(AcceptDialog);
  1044. exterr->set_text(RTR("Invalid extension, or empty filename."));
  1045. add_child(exterr, false, INTERNAL_MODE_FRONT);
  1046. update_filters();
  1047. update_dir();
  1048. set_hide_on_ok(false);
  1049. if (register_func) {
  1050. register_func(this);
  1051. }
  1052. }
  1053. FileDialog::~FileDialog() {
  1054. if (unregister_func) {
  1055. unregister_func(this);
  1056. }
  1057. }