file_dialog.cpp 35 KB

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