file_dialog.cpp 37 KB

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