file_dialog.cpp 35 KB

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