file_dialog.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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/config/project_settings.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/print_string.h"
  34. #include "scene/gui/check_box.h"
  35. #include "scene/gui/grid_container.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/option_button.h"
  38. #include "scene/theme/theme_db.h"
  39. FileDialog::GetIconFunc FileDialog::get_icon_func = nullptr;
  40. FileDialog::RegisterFunc FileDialog::register_func = nullptr;
  41. FileDialog::RegisterFunc FileDialog::unregister_func = nullptr;
  42. void FileDialog::popup_file_dialog() {
  43. popup_centered_clamped(Size2i(700, 500), 0.8f);
  44. _focus_file_text();
  45. }
  46. void FileDialog::_focus_file_text() {
  47. int lp = file->get_text().rfind_char('.');
  48. if (lp != -1) {
  49. file->select(0, lp);
  50. if (file->is_inside_tree() && !is_part_of_edited_scene()) {
  51. file->grab_focus();
  52. }
  53. }
  54. }
  55. void FileDialog::_native_popup() {
  56. // Show native dialog directly.
  57. String root;
  58. if (!root_prefix.is_empty()) {
  59. root = ProjectSettings::get_singleton()->globalize_path(root_prefix);
  60. } else if (access == ACCESS_RESOURCES) {
  61. root = ProjectSettings::get_singleton()->get_resource_path();
  62. } else if (access == ACCESS_USERDATA) {
  63. root = OS::get_singleton()->get_user_data_dir();
  64. }
  65. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_EXTRA)) {
  66. DisplayServer::get_singleton()->file_dialog_with_options_show(get_translated_title(), ProjectSettings::get_singleton()->globalize_path(full_dir), root, file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), processed_filters, _get_options(), callable_mp(this, &FileDialog::_native_dialog_cb_with_options));
  67. } else {
  68. DisplayServer::get_singleton()->file_dialog_show(get_translated_title(), ProjectSettings::get_singleton()->globalize_path(full_dir), file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), processed_filters, callable_mp(this, &FileDialog::_native_dialog_cb));
  69. }
  70. }
  71. bool FileDialog::_can_use_native_popup() {
  72. if (access == ACCESS_RESOURCES || access == ACCESS_USERDATA || options.size() > 0) {
  73. return DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_EXTRA);
  74. }
  75. return DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE);
  76. }
  77. void FileDialog::popup(const Rect2i &p_rect) {
  78. _update_option_controls();
  79. #ifdef TOOLS_ENABLED
  80. if (is_part_of_edited_scene()) {
  81. ConfirmationDialog::popup(p_rect);
  82. return;
  83. }
  84. #endif
  85. if (_can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  86. _native_popup();
  87. } else {
  88. ConfirmationDialog::popup(p_rect);
  89. }
  90. }
  91. void FileDialog::set_visible(bool p_visible) {
  92. if (p_visible) {
  93. _update_option_controls();
  94. }
  95. #ifdef TOOLS_ENABLED
  96. if (is_part_of_edited_scene()) {
  97. ConfirmationDialog::set_visible(p_visible);
  98. return;
  99. }
  100. #endif
  101. if (_can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  102. if (p_visible) {
  103. _native_popup();
  104. }
  105. } else {
  106. ConfirmationDialog::set_visible(p_visible);
  107. }
  108. }
  109. void FileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter) {
  110. _native_dialog_cb_with_options(p_ok, p_files, p_filter, Dictionary());
  111. }
  112. void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options) {
  113. if (!p_ok) {
  114. file->set_text("");
  115. emit_signal(SNAME("canceled"));
  116. return;
  117. }
  118. if (p_files.is_empty()) {
  119. return;
  120. }
  121. Vector<String> files = p_files;
  122. if (access != ACCESS_FILESYSTEM) {
  123. for (String &file_name : files) {
  124. file_name = ProjectSettings::get_singleton()->localize_path(file_name);
  125. }
  126. }
  127. selected_options = p_selected_options;
  128. String f = files[0];
  129. filter->select(p_filter);
  130. dir->set_text(f.get_base_dir());
  131. file->set_text(f.get_file());
  132. _change_dir(f.get_base_dir());
  133. if (mode == FILE_MODE_OPEN_FILES) {
  134. emit_signal(SNAME("files_selected"), files);
  135. } else {
  136. if (mode == FILE_MODE_SAVE_FILE) {
  137. if (p_filter != 0 && p_filter != filter->get_item_count() - 1) {
  138. bool valid = false;
  139. int idx = p_filter;
  140. if (filters.size() > 1) {
  141. idx--;
  142. }
  143. if (idx >= 0 && idx < filters.size()) {
  144. String flt = filters[idx].get_slice(";", 0);
  145. int filter_slice_count = flt.get_slice_count(",");
  146. for (int j = 0; j < filter_slice_count; j++) {
  147. String str = (flt.get_slice(",", j).strip_edges());
  148. if (f.match(str)) {
  149. valid = true;
  150. break;
  151. }
  152. }
  153. if (!valid && filter_slice_count > 0) {
  154. String str = (flt.get_slice(",", 0).strip_edges());
  155. f += str.substr(1, str.length() - 1);
  156. }
  157. }
  158. }
  159. emit_signal(SNAME("file_selected"), f);
  160. } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  161. emit_signal(SNAME("file_selected"), f);
  162. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  163. emit_signal(SNAME("dir_selected"), f);
  164. }
  165. }
  166. }
  167. VBoxContainer *FileDialog::get_vbox() {
  168. return vbox;
  169. }
  170. void FileDialog::_validate_property(PropertyInfo &p_property) const {
  171. if (p_property.name == "dialog_text") {
  172. // File dialogs have a custom layout, and dialog nodes can't have both a text and a layout.
  173. p_property.usage = PROPERTY_USAGE_NONE;
  174. }
  175. }
  176. void FileDialog::_notification(int p_what) {
  177. switch (p_what) {
  178. case NOTIFICATION_READY: {
  179. #ifdef TOOLS_ENABLED
  180. if (is_part_of_edited_scene()) {
  181. return;
  182. }
  183. #endif
  184. // Replace the built-in dialog with the native one if it started visible.
  185. if (is_visible() && _can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  186. ConfirmationDialog::set_visible(false);
  187. _native_popup();
  188. }
  189. } break;
  190. case NOTIFICATION_VISIBILITY_CHANGED: {
  191. if (!is_visible()) {
  192. set_process_shortcut_input(false);
  193. }
  194. invalidate(); // Put it here to preview in the editor.
  195. } break;
  196. case NOTIFICATION_THEME_CHANGED: {
  197. dir_up->set_button_icon(theme_cache.parent_folder);
  198. if (vbox->is_layout_rtl()) {
  199. dir_prev->set_button_icon(theme_cache.forward_folder);
  200. dir_next->set_button_icon(theme_cache.back_folder);
  201. } else {
  202. dir_prev->set_button_icon(theme_cache.back_folder);
  203. dir_next->set_button_icon(theme_cache.forward_folder);
  204. }
  205. refresh->set_button_icon(theme_cache.reload);
  206. show_hidden->set_button_icon(theme_cache.toggle_hidden);
  207. makedir->set_button_icon(theme_cache.create_folder);
  208. show_filename_filter_button->set_button_icon(theme_cache.toggle_filename_filter);
  209. dir_up->begin_bulk_theme_override();
  210. dir_up->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  211. dir_up->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  212. dir_up->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  213. dir_up->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  214. dir_up->end_bulk_theme_override();
  215. dir_prev->begin_bulk_theme_override();
  216. dir_prev->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  217. dir_prev->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  218. dir_prev->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  219. dir_prev->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  220. dir_prev->end_bulk_theme_override();
  221. dir_next->begin_bulk_theme_override();
  222. dir_next->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  223. dir_next->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  224. dir_next->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  225. dir_next->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  226. dir_next->end_bulk_theme_override();
  227. refresh->begin_bulk_theme_override();
  228. refresh->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  229. refresh->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  230. refresh->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  231. refresh->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  232. refresh->end_bulk_theme_override();
  233. show_hidden->begin_bulk_theme_override();
  234. show_hidden->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  235. show_hidden->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  236. show_hidden->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  237. show_hidden->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  238. show_hidden->end_bulk_theme_override();
  239. makedir->begin_bulk_theme_override();
  240. makedir->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  241. makedir->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  242. makedir->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  243. makedir->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  244. makedir->end_bulk_theme_override();
  245. show_filename_filter_button->begin_bulk_theme_override();
  246. show_filename_filter_button->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  247. show_filename_filter_button->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  248. show_filename_filter_button->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  249. show_filename_filter_button->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  250. show_filename_filter_button->end_bulk_theme_override();
  251. invalidate();
  252. } break;
  253. case NOTIFICATION_TRANSLATION_CHANGED: {
  254. update_filters();
  255. } break;
  256. }
  257. }
  258. void FileDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  259. ERR_FAIL_COND(p_event.is_null());
  260. Ref<InputEventKey> k = p_event;
  261. if (k.is_valid() && has_focus()) {
  262. if (k->is_pressed()) {
  263. bool handled = true;
  264. switch (k->get_keycode()) {
  265. case Key::H: {
  266. if (k->is_command_or_control_pressed()) {
  267. set_show_hidden_files(!show_hidden_files);
  268. } else {
  269. handled = false;
  270. }
  271. } break;
  272. case Key::F: {
  273. if (k->is_command_or_control_pressed()) {
  274. show_filename_filter_button->set_pressed(!show_filename_filter_button->is_pressed());
  275. } else {
  276. handled = false;
  277. }
  278. } break;
  279. case Key::F5: {
  280. invalidate();
  281. } break;
  282. case Key::BACKSPACE: {
  283. _dir_submitted("..");
  284. } break;
  285. #ifdef MACOS_ENABLED
  286. // Cmd + Shift + G (matches Finder's "Go To" shortcut).
  287. case Key::G: {
  288. if (k->is_command_or_control_pressed() && k->is_shift_pressed()) {
  289. dir->grab_focus();
  290. dir->select_all();
  291. } else {
  292. handled = false;
  293. }
  294. } break;
  295. #endif
  296. // Ctrl + L (matches most Windows/Linux file managers' "focus on path bar" shortcut,
  297. // plus macOS Safari's "focus on address bar" shortcut).
  298. case Key::L: {
  299. if (k->is_command_or_control_pressed()) {
  300. dir->grab_focus();
  301. dir->select_all();
  302. } else {
  303. handled = false;
  304. }
  305. } break;
  306. default: {
  307. handled = false;
  308. }
  309. }
  310. if (handled) {
  311. set_input_as_handled();
  312. }
  313. }
  314. }
  315. }
  316. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  317. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  318. }
  319. Vector<String> FileDialog::get_selected_files() const {
  320. Vector<String> list;
  321. TreeItem *item = tree->get_root();
  322. item = tree->get_next_selected(item);
  323. while (item) {
  324. list.push_back(dir_access->get_current_dir().path_join(item->get_text(0)));
  325. item = tree->get_next_selected(item);
  326. }
  327. return list;
  328. }
  329. void FileDialog::update_dir() {
  330. full_dir = dir_access->get_current_dir();
  331. if (root_prefix.is_empty()) {
  332. dir->set_text(dir_access->get_current_dir(false));
  333. } else {
  334. dir->set_text(dir_access->get_current_dir(false).trim_prefix(root_prefix).trim_prefix("/"));
  335. }
  336. if (drives->is_visible()) {
  337. if (dir_access->get_current_dir().is_network_share_path()) {
  338. _update_drives(false);
  339. drives->add_item(ETR("Network"));
  340. drives->set_item_disabled(-1, true);
  341. drives->select(drives->get_item_count() - 1);
  342. } else {
  343. drives->select(dir_access->get_current_drive());
  344. }
  345. }
  346. // Deselect any item, to make "Select Current Folder" button text by default.
  347. deselect_all();
  348. }
  349. void FileDialog::_dir_submitted(String p_dir) {
  350. _change_dir(root_prefix.path_join(p_dir));
  351. file->set_text("");
  352. _push_history();
  353. }
  354. void FileDialog::_file_submitted(const String &p_file) {
  355. _action_pressed();
  356. }
  357. void FileDialog::_save_confirm_pressed() {
  358. String f = dir_access->get_current_dir().path_join(file->get_text());
  359. emit_signal(SNAME("file_selected"), f);
  360. hide();
  361. }
  362. void FileDialog::_post_popup() {
  363. ConfirmationDialog::_post_popup();
  364. if (mode == FILE_MODE_SAVE_FILE) {
  365. file->grab_focus();
  366. } else {
  367. tree->grab_focus();
  368. }
  369. set_process_shortcut_input(true);
  370. // For open dir mode, deselect all items on file dialog open.
  371. if (mode == FILE_MODE_OPEN_DIR) {
  372. deselect_all();
  373. file_box->set_visible(false);
  374. } else {
  375. file_box->set_visible(true);
  376. }
  377. local_history.clear();
  378. local_history_pos = -1;
  379. _push_history();
  380. }
  381. void FileDialog::_push_history() {
  382. local_history.resize(local_history_pos + 1);
  383. String new_path = dir_access->get_current_dir();
  384. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  385. local_history.push_back(new_path);
  386. local_history_pos++;
  387. dir_prev->set_disabled(local_history_pos == 0);
  388. dir_next->set_disabled(true);
  389. }
  390. }
  391. void FileDialog::_action_pressed() {
  392. if (mode == FILE_MODE_OPEN_FILES) {
  393. TreeItem *ti = tree->get_next_selected(nullptr);
  394. String fbase = dir_access->get_current_dir();
  395. Vector<String> files;
  396. while (ti) {
  397. files.push_back(fbase.path_join(ti->get_text(0)));
  398. ti = tree->get_next_selected(ti);
  399. }
  400. if (files.size()) {
  401. emit_signal(SNAME("files_selected"), files);
  402. hide();
  403. }
  404. return;
  405. }
  406. String file_text = file->get_text();
  407. String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text);
  408. if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  409. emit_signal(SNAME("file_selected"), f);
  410. hide();
  411. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  412. String path = dir_access->get_current_dir();
  413. path = path.replace("\\", "/");
  414. TreeItem *item = tree->get_selected();
  415. if (item) {
  416. Dictionary d = item->get_metadata(0);
  417. if (d["dir"] && d["name"] != "..") {
  418. path = path.path_join(d["name"]);
  419. }
  420. }
  421. emit_signal(SNAME("dir_selected"), path);
  422. hide();
  423. }
  424. if (mode == FILE_MODE_SAVE_FILE) {
  425. bool valid = false;
  426. if (filter->get_selected() == filter->get_item_count() - 1) {
  427. valid = true; // match none
  428. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  429. // match all filters
  430. for (int i = 0; i < filters.size(); i++) {
  431. String flt = filters[i].get_slice(";", 0);
  432. for (int j = 0; j < flt.get_slice_count(","); j++) {
  433. String str = flt.get_slice(",", j).strip_edges();
  434. if (f.matchn(str)) {
  435. valid = true;
  436. break;
  437. }
  438. }
  439. if (valid) {
  440. break;
  441. }
  442. }
  443. } else {
  444. int idx = filter->get_selected();
  445. if (filters.size() > 1) {
  446. idx--;
  447. }
  448. if (idx >= 0 && idx < filters.size()) {
  449. String flt = filters[idx].get_slice(";", 0);
  450. int filter_slice_count = flt.get_slice_count(",");
  451. for (int j = 0; j < filter_slice_count; j++) {
  452. String str = (flt.get_slice(",", j).strip_edges());
  453. if (f.matchn(str)) {
  454. valid = true;
  455. break;
  456. }
  457. }
  458. if (!valid && filter_slice_count > 0) {
  459. String str = (flt.get_slice(",", 0).strip_edges());
  460. f += str.substr(1, str.length() - 1);
  461. file->set_text(f.get_file());
  462. valid = true;
  463. }
  464. } else {
  465. valid = true;
  466. }
  467. }
  468. String file_name = file_text.strip_edges().get_file();
  469. if (!valid || file_name.is_empty()) {
  470. exterr->popup_centered(Size2(250, 80));
  471. return;
  472. }
  473. if (dir_access->file_exists(f)) {
  474. confirm_save->set_text(vformat(atr(ETR("File \"%s\" already exists.\nDo you want to overwrite it?")), f));
  475. confirm_save->popup_centered(Size2(250, 80));
  476. } else {
  477. emit_signal(SNAME("file_selected"), f);
  478. hide();
  479. }
  480. }
  481. }
  482. void FileDialog::_cancel_pressed() {
  483. file->set_text("");
  484. invalidate();
  485. hide();
  486. }
  487. bool FileDialog::_is_open_should_be_disabled() {
  488. if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_SAVE_FILE) {
  489. return false;
  490. }
  491. TreeItem *ti = tree->get_next_selected(tree->get_root());
  492. while (ti) {
  493. TreeItem *prev_ti = ti;
  494. ti = tree->get_next_selected(tree->get_root());
  495. if (ti == prev_ti) {
  496. break;
  497. }
  498. }
  499. // We have something that we can't select?
  500. if (!ti) {
  501. return mode != FILE_MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  502. }
  503. Dictionary d = ti->get_metadata(0);
  504. // Opening a file, but selected a folder? Forbidden.
  505. return ((mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  506. (mode == FILE_MODE_OPEN_DIR && !d["dir"]);
  507. }
  508. void FileDialog::_go_up() {
  509. _change_dir("..");
  510. _push_history();
  511. }
  512. void FileDialog::_go_back() {
  513. if (local_history_pos <= 0) {
  514. return;
  515. }
  516. local_history_pos--;
  517. _change_dir(local_history[local_history_pos]);
  518. dir_prev->set_disabled(local_history_pos == 0);
  519. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  520. }
  521. void FileDialog::_go_forward() {
  522. if (local_history_pos >= local_history.size() - 1) {
  523. return;
  524. }
  525. local_history_pos++;
  526. _change_dir(local_history[local_history_pos]);
  527. dir_prev->set_disabled(local_history_pos == 0);
  528. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  529. }
  530. void FileDialog::deselect_all() {
  531. // Clear currently selected items in file manager.
  532. tree->deselect_all();
  533. // And change get_ok title.
  534. if (!tree->is_anything_selected()) {
  535. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  536. switch (mode) {
  537. case FILE_MODE_OPEN_FILE:
  538. case FILE_MODE_OPEN_FILES:
  539. set_ok_button_text(ETR("Open"));
  540. break;
  541. case FILE_MODE_OPEN_DIR:
  542. set_ok_button_text(ETR("Select Current Folder"));
  543. break;
  544. case FILE_MODE_OPEN_ANY:
  545. case FILE_MODE_SAVE_FILE:
  546. // FIXME: Implement, or refactor to avoid duplication with set_mode
  547. break;
  548. }
  549. }
  550. }
  551. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  552. _tree_selected();
  553. }
  554. void FileDialog::_tree_selected() {
  555. TreeItem *ti = tree->get_selected();
  556. if (!ti) {
  557. return;
  558. }
  559. Dictionary d = ti->get_metadata(0);
  560. if (!d["dir"]) {
  561. file->set_text(d["name"]);
  562. } else if (mode == FILE_MODE_OPEN_DIR) {
  563. set_ok_button_text(ETR("Select This Folder"));
  564. }
  565. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  566. }
  567. void FileDialog::_tree_item_activated() {
  568. TreeItem *ti = tree->get_selected();
  569. if (!ti) {
  570. return;
  571. }
  572. Dictionary d = ti->get_metadata(0);
  573. if (d["dir"]) {
  574. _change_dir(d["name"]);
  575. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  576. file->set_text("");
  577. }
  578. _push_history();
  579. } else {
  580. _action_pressed();
  581. }
  582. }
  583. void FileDialog::update_file_name() {
  584. int idx = filter->get_selected() - 1;
  585. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  586. if (idx == -1) {
  587. idx += 1;
  588. }
  589. String filter_str = filters[idx];
  590. String file_str = file->get_text();
  591. String base_name = file_str.get_basename();
  592. Vector<String> filter_substr = filter_str.split(";");
  593. if (filter_substr.size() >= 2) {
  594. file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower();
  595. } else {
  596. file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower();
  597. }
  598. file->set_text(file_str);
  599. }
  600. }
  601. void FileDialog::update_file_list() {
  602. tree->clear();
  603. // Scroll back to the top after opening a directory
  604. tree->get_vscroll_bar()->set_value(0);
  605. dir_access->list_dir_begin();
  606. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  607. message->hide();
  608. } else {
  609. message->set_text(ETR("You don't have permission to access contents of this folder."));
  610. message->show();
  611. }
  612. TreeItem *root = tree->create_item();
  613. List<String> files;
  614. List<String> dirs;
  615. bool is_hidden;
  616. String item = dir_access->get_next();
  617. while (!item.is_empty()) {
  618. if (item == "." || item == "..") {
  619. item = dir_access->get_next();
  620. continue;
  621. }
  622. is_hidden = dir_access->current_is_hidden();
  623. if (show_hidden_files || !is_hidden) {
  624. if (!dir_access->current_is_dir()) {
  625. files.push_back(item);
  626. } else {
  627. dirs.push_back(item);
  628. }
  629. }
  630. item = dir_access->get_next();
  631. }
  632. dirs.sort_custom<FileNoCaseComparator>();
  633. files.sort_custom<FileNoCaseComparator>();
  634. String filename_filter_lower = file_name_filter.to_lower();
  635. while (!dirs.is_empty()) {
  636. const String &dir_name = dirs.front()->get();
  637. if (filename_filter_lower.is_empty() || dir_name.to_lower().contains(filename_filter_lower)) {
  638. TreeItem *ti = tree->create_item(root);
  639. ti->set_text(0, dir_name);
  640. ti->set_icon(0, theme_cache.folder);
  641. ti->set_icon_modulate(0, theme_cache.folder_icon_color);
  642. Dictionary d;
  643. d["name"] = dir_name;
  644. d["dir"] = true;
  645. ti->set_metadata(0, d);
  646. }
  647. dirs.pop_front();
  648. }
  649. List<String> patterns;
  650. // build filter
  651. if (filter->get_selected() == filter->get_item_count() - 1) {
  652. // match all
  653. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  654. // match all filters
  655. for (int i = 0; i < filters.size(); i++) {
  656. String f = filters[i].get_slice(";", 0);
  657. for (int j = 0; j < f.get_slice_count(","); j++) {
  658. patterns.push_back(f.get_slice(",", j).strip_edges());
  659. }
  660. }
  661. } else {
  662. int idx = filter->get_selected();
  663. if (filters.size() > 1) {
  664. idx--;
  665. }
  666. if (idx >= 0 && idx < filters.size()) {
  667. String f = filters[idx].get_slice(";", 0);
  668. for (int j = 0; j < f.get_slice_count(","); j++) {
  669. patterns.push_back(f.get_slice(",", j).strip_edges());
  670. }
  671. }
  672. }
  673. String base_dir = dir_access->get_current_dir();
  674. while (!files.is_empty()) {
  675. bool match = patterns.is_empty();
  676. String match_str;
  677. for (const String &E : patterns) {
  678. if (files.front()->get().matchn(E)) {
  679. match_str = E;
  680. match = true;
  681. break;
  682. }
  683. }
  684. if (match && (filename_filter_lower.is_empty() || files.front()->get().to_lower().contains(filename_filter_lower))) {
  685. TreeItem *ti = tree->create_item(root);
  686. ti->set_text(0, files.front()->get());
  687. if (get_icon_func) {
  688. Ref<Texture2D> icon = get_icon_func(base_dir.path_join(files.front()->get()));
  689. ti->set_icon(0, icon);
  690. } else {
  691. ti->set_icon(0, theme_cache.file);
  692. }
  693. ti->set_icon_modulate(0, theme_cache.file_icon_color);
  694. if (mode == FILE_MODE_OPEN_DIR) {
  695. ti->set_custom_color(0, theme_cache.file_disabled_color);
  696. ti->set_selectable(0, false);
  697. }
  698. Dictionary d;
  699. d["name"] = files.front()->get();
  700. d["dir"] = false;
  701. ti->set_metadata(0, d);
  702. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  703. ti->select(0);
  704. }
  705. }
  706. files.pop_front();
  707. }
  708. if (mode != FILE_MODE_SAVE_FILE && mode != FILE_MODE_OPEN_DIR) {
  709. // Select the first file from list if nothing is selected.
  710. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  711. tree->get_root()->get_first_child()->select(0);
  712. }
  713. }
  714. }
  715. void FileDialog::_filter_selected(int) {
  716. update_file_name();
  717. update_file_list();
  718. }
  719. void FileDialog::_filename_filter_changed() {
  720. update_filename_filter();
  721. update_file_list();
  722. callable_mp(this, &FileDialog::_tree_select_first).call_deferred();
  723. }
  724. void FileDialog::_tree_select_first() {
  725. if (tree->get_root() && tree->get_root()->get_first_child()) {
  726. tree->get_root()->get_first_child()->select(0);
  727. }
  728. }
  729. void FileDialog::_filename_filter_selected() {
  730. TreeItem *item = tree->get_selected();
  731. if (item) {
  732. file->set_text(item->get_text(0));
  733. file->emit_signal(SceneStringName(text_submitted), file->get_text());
  734. }
  735. }
  736. void FileDialog::update_filters() {
  737. filter->clear();
  738. processed_filters.clear();
  739. if (filters.size() > 1) {
  740. String all_filters;
  741. String all_filters_full;
  742. const int max_filters = 5;
  743. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  744. String flt = filters[i].get_slicec(';', 0).strip_edges();
  745. if (i > 0) {
  746. all_filters += ", ";
  747. }
  748. all_filters += flt;
  749. }
  750. for (int i = 0; i < filters.size(); i++) {
  751. String flt = filters[i].get_slicec(';', 0).strip_edges();
  752. if (i > 0) {
  753. all_filters_full += ",";
  754. }
  755. all_filters_full += flt;
  756. }
  757. if (max_filters < filters.size()) {
  758. all_filters += ", ...";
  759. }
  760. String f = atr(ETR("All Recognized")) + " (" + all_filters + ")";
  761. filter->add_item(f);
  762. processed_filters.push_back(all_filters_full + ";" + f);
  763. }
  764. for (int i = 0; i < filters.size(); i++) {
  765. String flt = filters[i].get_slicec(';', 0).strip_edges();
  766. String desc = filters[i].get_slice(";", 1).strip_edges();
  767. if (desc.length()) {
  768. String f = atr(desc) + " (" + flt + ")";
  769. filter->add_item(f);
  770. processed_filters.push_back(flt + ";" + f);
  771. } else {
  772. String f = "(" + flt + ")";
  773. filter->add_item(f);
  774. processed_filters.push_back(flt + ";" + f);
  775. }
  776. }
  777. String f = atr(ETR("All Files")) + " (*.*)";
  778. filter->add_item(f);
  779. processed_filters.push_back("*.*;" + f);
  780. }
  781. void FileDialog::clear_filename_filter() {
  782. set_filename_filter("");
  783. update_filename_filter_gui();
  784. invalidate();
  785. }
  786. void FileDialog::update_filename_filter_gui() {
  787. filename_filter_box->set_visible(show_filename_filter);
  788. if (!show_filename_filter) {
  789. file_name_filter.clear();
  790. }
  791. if (filename_filter->get_text() == file_name_filter) {
  792. return;
  793. }
  794. filename_filter->set_text(file_name_filter);
  795. }
  796. void FileDialog::update_filename_filter() {
  797. if (filename_filter->get_text() == file_name_filter) {
  798. return;
  799. }
  800. set_filename_filter(filename_filter->get_text());
  801. }
  802. void FileDialog::clear_filters() {
  803. filters.clear();
  804. update_filters();
  805. invalidate();
  806. }
  807. void FileDialog::add_filter(const String &p_filter, const String &p_description) {
  808. ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
  809. if (p_description.is_empty()) {
  810. filters.push_back(p_filter);
  811. } else {
  812. filters.push_back(vformat("%s ; %s", p_filter, p_description));
  813. }
  814. update_filters();
  815. invalidate();
  816. }
  817. void FileDialog::set_filters(const Vector<String> &p_filters) {
  818. if (filters == p_filters) {
  819. return;
  820. }
  821. filters = p_filters;
  822. update_filters();
  823. invalidate();
  824. }
  825. void FileDialog::set_filename_filter(const String &p_filename_filter) {
  826. if (file_name_filter == p_filename_filter) {
  827. return;
  828. }
  829. file_name_filter = p_filename_filter;
  830. update_filename_filter_gui();
  831. emit_signal(SNAME("filename_filter_changed"), filter);
  832. invalidate();
  833. }
  834. Vector<String> FileDialog::get_filters() const {
  835. return filters;
  836. }
  837. String FileDialog::get_filename_filter() const {
  838. return file_name_filter;
  839. }
  840. String FileDialog::get_current_dir() const {
  841. return full_dir;
  842. }
  843. String FileDialog::get_current_file() const {
  844. return file->get_text();
  845. }
  846. String FileDialog::get_current_path() const {
  847. return full_dir.path_join(file->get_text());
  848. }
  849. void FileDialog::set_current_dir(const String &p_dir) {
  850. _change_dir(p_dir);
  851. _push_history();
  852. }
  853. void FileDialog::set_current_file(const String &p_file) {
  854. if (file->get_text() == p_file) {
  855. return;
  856. }
  857. file->set_text(p_file);
  858. update_dir();
  859. invalidate();
  860. _focus_file_text();
  861. }
  862. void FileDialog::set_current_path(const String &p_path) {
  863. if (!p_path.size()) {
  864. return;
  865. }
  866. int pos = MAX(p_path.rfind_char('/'), p_path.rfind_char('\\'));
  867. if (pos == -1) {
  868. set_current_file(p_path);
  869. } else {
  870. String path_dir = p_path.substr(0, pos);
  871. String path_file = p_path.substr(pos + 1, p_path.length());
  872. set_current_dir(path_dir);
  873. set_current_file(path_file);
  874. }
  875. }
  876. void FileDialog::set_root_subfolder(const String &p_root) {
  877. root_subfolder = p_root;
  878. ERR_FAIL_COND_MSG(!dir_access->dir_exists(p_root), "root_subfolder must be an existing sub-directory.");
  879. local_history.clear();
  880. local_history_pos = -1;
  881. dir_access->change_dir(root_subfolder);
  882. if (root_subfolder.is_empty()) {
  883. root_prefix = "";
  884. } else {
  885. root_prefix = dir_access->get_current_dir();
  886. }
  887. invalidate();
  888. update_dir();
  889. }
  890. String FileDialog::get_root_subfolder() const {
  891. return root_subfolder;
  892. }
  893. void FileDialog::set_mode_overrides_title(bool p_override) {
  894. mode_overrides_title = p_override;
  895. }
  896. bool FileDialog::is_mode_overriding_title() const {
  897. return mode_overrides_title;
  898. }
  899. void FileDialog::set_file_mode(FileMode p_mode) {
  900. ERR_FAIL_INDEX((int)p_mode, 5);
  901. if (mode == p_mode) {
  902. return;
  903. }
  904. mode = p_mode;
  905. switch (mode) {
  906. case FILE_MODE_OPEN_FILE:
  907. set_ok_button_text(ETR("Open"));
  908. if (mode_overrides_title) {
  909. set_title(ETR("Open a File"));
  910. }
  911. makedir->hide();
  912. break;
  913. case FILE_MODE_OPEN_FILES:
  914. set_ok_button_text(ETR("Open"));
  915. if (mode_overrides_title) {
  916. set_title(ETR("Open File(s)"));
  917. }
  918. makedir->hide();
  919. break;
  920. case FILE_MODE_OPEN_DIR:
  921. set_ok_button_text(ETR("Select Current Folder"));
  922. if (mode_overrides_title) {
  923. set_title(ETR("Open a Directory"));
  924. }
  925. makedir->show();
  926. break;
  927. case FILE_MODE_OPEN_ANY:
  928. set_ok_button_text(ETR("Open"));
  929. if (mode_overrides_title) {
  930. set_title(ETR("Open a File or Directory"));
  931. }
  932. makedir->show();
  933. break;
  934. case FILE_MODE_SAVE_FILE:
  935. set_ok_button_text(ETR("Save"));
  936. if (mode_overrides_title) {
  937. set_title(ETR("Save a File"));
  938. }
  939. makedir->show();
  940. break;
  941. }
  942. if (mode == FILE_MODE_OPEN_FILES) {
  943. tree->set_select_mode(Tree::SELECT_MULTI);
  944. } else {
  945. tree->set_select_mode(Tree::SELECT_SINGLE);
  946. }
  947. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  948. }
  949. FileDialog::FileMode FileDialog::get_file_mode() const {
  950. return mode;
  951. }
  952. void FileDialog::set_access(Access p_access) {
  953. ERR_FAIL_INDEX(p_access, 3);
  954. if (access == p_access) {
  955. return;
  956. }
  957. access = p_access;
  958. root_prefix = "";
  959. root_subfolder = "";
  960. switch (p_access) {
  961. case ACCESS_FILESYSTEM: {
  962. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  963. #ifdef ANDROID_ENABLED
  964. set_root_subfolder(OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DESKTOP));
  965. #endif
  966. } break;
  967. case ACCESS_RESOURCES: {
  968. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  969. } break;
  970. case ACCESS_USERDATA: {
  971. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  972. } break;
  973. }
  974. _update_drives();
  975. invalidate();
  976. update_filters();
  977. update_dir();
  978. }
  979. void FileDialog::invalidate() {
  980. if (!is_visible() || is_invalidating) {
  981. return;
  982. }
  983. is_invalidating = true;
  984. callable_mp(this, &FileDialog::_invalidate).call_deferred();
  985. }
  986. void FileDialog::_invalidate() {
  987. if (!is_invalidating) {
  988. return;
  989. }
  990. update_file_list();
  991. is_invalidating = false;
  992. }
  993. FileDialog::Access FileDialog::get_access() const {
  994. return access;
  995. }
  996. void FileDialog::_make_dir_confirm() {
  997. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  998. if (err == OK) {
  999. _change_dir(makedirname->get_text().strip_edges());
  1000. update_filters();
  1001. _push_history();
  1002. } else {
  1003. mkdirerr->popup_centered(Size2(250, 50));
  1004. }
  1005. makedirname->set_text(""); // reset label
  1006. }
  1007. void FileDialog::_make_dir() {
  1008. makedialog->popup_centered(Size2(250, 80));
  1009. makedirname->grab_focus();
  1010. }
  1011. void FileDialog::_select_drive(int p_idx) {
  1012. String d = drives->get_item_text(p_idx);
  1013. _change_dir(d);
  1014. file->set_text("");
  1015. _push_history();
  1016. }
  1017. void FileDialog::_change_dir(const String &p_new_dir) {
  1018. if (root_prefix.is_empty()) {
  1019. dir_access->change_dir(p_new_dir);
  1020. } else {
  1021. String old_dir = dir_access->get_current_dir();
  1022. dir_access->change_dir(p_new_dir);
  1023. if (!dir_access->get_current_dir(false).begins_with(root_prefix)) {
  1024. dir_access->change_dir(old_dir);
  1025. return;
  1026. }
  1027. }
  1028. invalidate();
  1029. update_dir();
  1030. }
  1031. void FileDialog::_update_drives(bool p_select) {
  1032. int dc = dir_access->get_drive_count();
  1033. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  1034. drives->hide();
  1035. } else {
  1036. drives->clear();
  1037. Node *dp = drives->get_parent();
  1038. if (dp) {
  1039. dp->remove_child(drives);
  1040. }
  1041. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  1042. dp->add_child(drives);
  1043. drives->show();
  1044. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  1045. drives->add_item(dir_access->get_drive(i));
  1046. }
  1047. if (p_select) {
  1048. drives->select(dir_access->get_current_drive());
  1049. }
  1050. }
  1051. }
  1052. bool FileDialog::default_show_hidden_files = false;
  1053. TypedArray<Dictionary> FileDialog::_get_options() const {
  1054. TypedArray<Dictionary> out;
  1055. for (const FileDialog::Option &opt : options) {
  1056. Dictionary dict;
  1057. dict["name"] = opt.name;
  1058. dict["values"] = opt.values;
  1059. dict["default"] = (int)selected_options.get(opt.name, opt.default_idx);
  1060. out.push_back(dict);
  1061. }
  1062. return out;
  1063. }
  1064. void FileDialog::_option_changed_checkbox_toggled(bool p_pressed, const String &p_name) {
  1065. if (selected_options.has(p_name)) {
  1066. selected_options[p_name] = p_pressed;
  1067. }
  1068. }
  1069. void FileDialog::_option_changed_item_selected(int p_idx, const String &p_name) {
  1070. if (selected_options.has(p_name)) {
  1071. selected_options[p_name] = p_idx;
  1072. }
  1073. }
  1074. void FileDialog::_update_option_controls() {
  1075. if (!options_dirty) {
  1076. return;
  1077. }
  1078. options_dirty = false;
  1079. while (grid_options->get_child_count() > 0) {
  1080. Node *child = grid_options->get_child(0);
  1081. grid_options->remove_child(child);
  1082. child->queue_free();
  1083. }
  1084. selected_options.clear();
  1085. for (const FileDialog::Option &opt : options) {
  1086. Label *lbl = memnew(Label);
  1087. lbl->set_text(opt.name);
  1088. grid_options->add_child(lbl);
  1089. if (opt.values.is_empty()) {
  1090. CheckBox *cb = memnew(CheckBox);
  1091. cb->set_pressed(opt.default_idx);
  1092. grid_options->add_child(cb);
  1093. cb->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::_option_changed_checkbox_toggled).bind(opt.name));
  1094. selected_options[opt.name] = (bool)opt.default_idx;
  1095. } else {
  1096. OptionButton *ob = memnew(OptionButton);
  1097. for (const String &val : opt.values) {
  1098. ob->add_item(val);
  1099. }
  1100. ob->select(opt.default_idx);
  1101. grid_options->add_child(ob);
  1102. ob->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_option_changed_item_selected).bind(opt.name));
  1103. selected_options[opt.name] = opt.default_idx;
  1104. }
  1105. }
  1106. }
  1107. Dictionary FileDialog::get_selected_options() const {
  1108. return selected_options;
  1109. }
  1110. String FileDialog::get_option_name(int p_option) const {
  1111. ERR_FAIL_INDEX_V(p_option, options.size(), String());
  1112. return options[p_option].name;
  1113. }
  1114. Vector<String> FileDialog::get_option_values(int p_option) const {
  1115. ERR_FAIL_INDEX_V(p_option, options.size(), Vector<String>());
  1116. return options[p_option].values;
  1117. }
  1118. int FileDialog::get_option_default(int p_option) const {
  1119. ERR_FAIL_INDEX_V(p_option, options.size(), -1);
  1120. return options[p_option].default_idx;
  1121. }
  1122. void FileDialog::set_option_name(int p_option, const String &p_name) {
  1123. if (p_option < 0) {
  1124. p_option += get_option_count();
  1125. }
  1126. ERR_FAIL_INDEX(p_option, options.size());
  1127. options.write[p_option].name = p_name;
  1128. options_dirty = true;
  1129. if (is_visible()) {
  1130. _update_option_controls();
  1131. }
  1132. }
  1133. void FileDialog::set_option_values(int p_option, const Vector<String> &p_values) {
  1134. if (p_option < 0) {
  1135. p_option += get_option_count();
  1136. }
  1137. ERR_FAIL_INDEX(p_option, options.size());
  1138. options.write[p_option].values = p_values;
  1139. if (p_values.is_empty()) {
  1140. options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, 1);
  1141. } else {
  1142. options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, options[p_option].values.size() - 1);
  1143. }
  1144. options_dirty = true;
  1145. if (is_visible()) {
  1146. _update_option_controls();
  1147. }
  1148. }
  1149. void FileDialog::set_option_default(int p_option, int p_index) {
  1150. if (p_option < 0) {
  1151. p_option += get_option_count();
  1152. }
  1153. ERR_FAIL_INDEX(p_option, options.size());
  1154. if (options[p_option].values.is_empty()) {
  1155. options.write[p_option].default_idx = CLAMP(p_index, 0, 1);
  1156. } else {
  1157. options.write[p_option].default_idx = CLAMP(p_index, 0, options[p_option].values.size() - 1);
  1158. }
  1159. options_dirty = true;
  1160. if (is_visible()) {
  1161. _update_option_controls();
  1162. }
  1163. }
  1164. void FileDialog::add_option(const String &p_name, const Vector<String> &p_values, int p_index) {
  1165. Option opt;
  1166. opt.name = p_name;
  1167. opt.values = p_values;
  1168. if (opt.values.is_empty()) {
  1169. opt.default_idx = CLAMP(p_index, 0, 1);
  1170. } else {
  1171. opt.default_idx = CLAMP(p_index, 0, opt.values.size() - 1);
  1172. }
  1173. options.push_back(opt);
  1174. options_dirty = true;
  1175. if (is_visible()) {
  1176. _update_option_controls();
  1177. }
  1178. }
  1179. void FileDialog::set_option_count(int p_count) {
  1180. ERR_FAIL_COND(p_count < 0);
  1181. if (options.size() == p_count) {
  1182. return;
  1183. }
  1184. options.resize(p_count);
  1185. options_dirty = true;
  1186. notify_property_list_changed();
  1187. if (is_visible()) {
  1188. _update_option_controls();
  1189. }
  1190. }
  1191. int FileDialog::get_option_count() const {
  1192. return options.size();
  1193. }
  1194. void FileDialog::_bind_methods() {
  1195. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  1196. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  1197. ClassDB::bind_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::add_filter, DEFVAL(""));
  1198. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  1199. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  1200. ClassDB::bind_method(D_METHOD("clear_filename_filter"), &FileDialog::clear_filename_filter);
  1201. ClassDB::bind_method(D_METHOD("set_filename_filter", "filter"), &FileDialog::set_filename_filter);
  1202. ClassDB::bind_method(D_METHOD("get_filename_filter"), &FileDialog::get_filename_filter);
  1203. ClassDB::bind_method(D_METHOD("get_option_name", "option"), &FileDialog::get_option_name);
  1204. ClassDB::bind_method(D_METHOD("get_option_values", "option"), &FileDialog::get_option_values);
  1205. ClassDB::bind_method(D_METHOD("get_option_default", "option"), &FileDialog::get_option_default);
  1206. ClassDB::bind_method(D_METHOD("set_option_name", "option", "name"), &FileDialog::set_option_name);
  1207. ClassDB::bind_method(D_METHOD("set_option_values", "option", "values"), &FileDialog::set_option_values);
  1208. ClassDB::bind_method(D_METHOD("set_option_default", "option", "default_value_index"), &FileDialog::set_option_default);
  1209. ClassDB::bind_method(D_METHOD("set_option_count", "count"), &FileDialog::set_option_count);
  1210. ClassDB::bind_method(D_METHOD("get_option_count"), &FileDialog::get_option_count);
  1211. ClassDB::bind_method(D_METHOD("add_option", "name", "values", "default_value_index"), &FileDialog::add_option);
  1212. ClassDB::bind_method(D_METHOD("get_selected_options"), &FileDialog::get_selected_options);
  1213. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  1214. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  1215. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  1216. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  1217. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  1218. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  1219. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  1220. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  1221. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  1222. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  1223. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  1224. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  1225. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  1226. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  1227. ClassDB::bind_method(D_METHOD("set_root_subfolder", "dir"), &FileDialog::set_root_subfolder);
  1228. ClassDB::bind_method(D_METHOD("get_root_subfolder"), &FileDialog::get_root_subfolder);
  1229. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  1230. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  1231. ClassDB::bind_method(D_METHOD("set_use_native_dialog", "native"), &FileDialog::set_use_native_dialog);
  1232. ClassDB::bind_method(D_METHOD("get_use_native_dialog"), &FileDialog::get_use_native_dialog);
  1233. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  1234. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  1235. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  1236. 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");
  1237. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User Data,File System"), "set_access", "get_access");
  1238. ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_subfolder"), "set_root_subfolder", "get_root_subfolder");
  1239. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  1240. ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename_filter"), "set_filename_filter", "get_filename_filter");
  1241. ADD_ARRAY_COUNT("Options", "option_count", "set_option_count", "get_option_count", "option_");
  1242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  1243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_native_dialog"), "set_use_native_dialog", "get_use_native_dialog");
  1244. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", PROPERTY_USAGE_NONE), "set_current_dir", "get_current_dir");
  1245. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE, "*", PROPERTY_USAGE_NONE), "set_current_file", "get_current_file");
  1246. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_path", "get_current_path");
  1247. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  1248. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  1249. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  1250. ADD_SIGNAL(MethodInfo("filename_filter_changed", PropertyInfo(Variant::STRING, "filter")));
  1251. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  1252. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  1253. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  1254. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  1255. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  1256. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  1257. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  1258. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  1259. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, parent_folder);
  1260. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, forward_folder);
  1261. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, back_folder);
  1262. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, reload);
  1263. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_hidden);
  1264. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder);
  1265. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_filename_filter);
  1266. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file);
  1267. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, create_folder);
  1268. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, folder_icon_color);
  1269. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_icon_color);
  1270. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_disabled_color);
  1271. // TODO: Define own colors?
  1272. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_normal_color, "font_color", "Button");
  1273. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_hover_color, "font_hover_color", "Button");
  1274. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_focus_color, "font_focus_color", "Button");
  1275. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_pressed_color, "font_pressed_color", "Button");
  1276. Option defaults;
  1277. base_property_helper.set_prefix("option_");
  1278. base_property_helper.set_array_length_getter(&FileDialog::get_option_count);
  1279. base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults.name, &FileDialog::set_option_name, &FileDialog::get_option_name);
  1280. base_property_helper.register_property(PropertyInfo(Variant::PACKED_STRING_ARRAY, "values"), defaults.values, &FileDialog::set_option_values, &FileDialog::get_option_values);
  1281. base_property_helper.register_property(PropertyInfo(Variant::INT, "default"), defaults.default_idx, &FileDialog::set_option_default, &FileDialog::get_option_default);
  1282. PropertyListHelper::register_base_helper(&base_property_helper);
  1283. }
  1284. void FileDialog::set_show_hidden_files(bool p_show) {
  1285. if (show_hidden_files == p_show) {
  1286. return;
  1287. }
  1288. show_hidden_files = p_show;
  1289. invalidate();
  1290. }
  1291. void FileDialog::set_show_filename_filter(bool p_show) {
  1292. if (p_show == show_filename_filter) {
  1293. return;
  1294. }
  1295. if (p_show) {
  1296. filename_filter->grab_focus();
  1297. } else {
  1298. if (filename_filter->has_focus()) {
  1299. tree->call_deferred("grab_focus");
  1300. }
  1301. }
  1302. show_filename_filter = p_show;
  1303. update_filename_filter_gui();
  1304. invalidate();
  1305. }
  1306. bool FileDialog::get_show_filename_filter() const {
  1307. return show_filename_filter;
  1308. }
  1309. bool FileDialog::is_showing_hidden_files() const {
  1310. return show_hidden_files;
  1311. }
  1312. void FileDialog::set_default_show_hidden_files(bool p_show) {
  1313. default_show_hidden_files = p_show;
  1314. }
  1315. void FileDialog::set_use_native_dialog(bool p_native) {
  1316. use_native_dialog = p_native;
  1317. #ifdef TOOLS_ENABLED
  1318. if (is_part_of_edited_scene()) {
  1319. return;
  1320. }
  1321. #endif
  1322. // Replace the built-in dialog with the native one if it's currently visible.
  1323. if (is_inside_tree() && is_visible() && _can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  1324. ConfirmationDialog::set_visible(false);
  1325. _native_popup();
  1326. }
  1327. }
  1328. bool FileDialog::get_use_native_dialog() const {
  1329. return use_native_dialog;
  1330. }
  1331. FileDialog::FileDialog() {
  1332. show_hidden_files = default_show_hidden_files;
  1333. vbox = memnew(VBoxContainer);
  1334. add_child(vbox, false, INTERNAL_MODE_FRONT);
  1335. mode = FILE_MODE_SAVE_FILE;
  1336. set_title(TTRC("Save a File"));
  1337. HBoxContainer *hbc = memnew(HBoxContainer);
  1338. dir_prev = memnew(Button);
  1339. dir_prev->set_theme_type_variation("FlatButton");
  1340. dir_prev->set_tooltip_text(ETR("Go to previous folder."));
  1341. dir_next = memnew(Button);
  1342. dir_next->set_theme_type_variation("FlatButton");
  1343. dir_next->set_tooltip_text(ETR("Go to next folder."));
  1344. dir_up = memnew(Button);
  1345. dir_up->set_theme_type_variation("FlatButton");
  1346. dir_up->set_tooltip_text(ETR("Go to parent folder."));
  1347. hbc->add_child(dir_prev);
  1348. hbc->add_child(dir_next);
  1349. hbc->add_child(dir_up);
  1350. dir_prev->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_back));
  1351. dir_next->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_forward));
  1352. dir_up->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_up));
  1353. hbc->add_child(memnew(Label(ETR("Path:"))));
  1354. drives_container = memnew(HBoxContainer);
  1355. hbc->add_child(drives_container);
  1356. drives = memnew(OptionButton);
  1357. drives->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_select_drive));
  1358. hbc->add_child(drives);
  1359. dir = memnew(LineEdit);
  1360. dir->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1361. hbc->add_child(dir);
  1362. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1363. refresh = memnew(Button);
  1364. refresh->set_theme_type_variation("FlatButton");
  1365. refresh->set_tooltip_text(ETR("Refresh files."));
  1366. refresh->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::update_file_list));
  1367. hbc->add_child(refresh);
  1368. show_hidden = memnew(Button);
  1369. show_hidden->set_theme_type_variation("FlatButton");
  1370. show_hidden->set_toggle_mode(true);
  1371. show_hidden->set_pressed(is_showing_hidden_files());
  1372. show_hidden->set_tooltip_text(ETR("Toggle the visibility of hidden files."));
  1373. show_hidden->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_hidden_files));
  1374. hbc->add_child(show_hidden);
  1375. show_filename_filter_button = memnew(Button);
  1376. show_filename_filter_button->set_theme_type_variation("FlatButton");
  1377. show_filename_filter_button->set_toggle_mode(true);
  1378. show_filename_filter_button->set_pressed(false);
  1379. show_filename_filter_button->set_tooltip_text(RTR("Toggle the visibility of the filter for file names."));
  1380. show_filename_filter_button->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_filename_filter));
  1381. hbc->add_child(show_filename_filter_button);
  1382. shortcuts_container = memnew(HBoxContainer);
  1383. hbc->add_child(shortcuts_container);
  1384. makedir = memnew(Button);
  1385. makedir->set_theme_type_variation("FlatButton");
  1386. makedir->set_tooltip_text(ETR("Create a new folder."));
  1387. makedir->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_make_dir));
  1388. hbc->add_child(makedir);
  1389. vbox->add_child(hbc);
  1390. tree = memnew(Tree);
  1391. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1392. tree->set_hide_root(true);
  1393. vbox->add_margin_child(ETR("Directories & Files:"), tree, true);
  1394. message = memnew(Label);
  1395. message->hide();
  1396. message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1397. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1398. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1399. tree->add_child(message);
  1400. filename_filter_box = memnew(HBoxContainer);
  1401. filename_filter_box->add_child(memnew(Label(RTR("Filter:"))));
  1402. filename_filter = memnew(LineEdit);
  1403. filename_filter->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1404. filename_filter->set_stretch_ratio(4);
  1405. filename_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1406. filename_filter->set_clear_button_enabled(true);
  1407. filename_filter_box->add_child(filename_filter);
  1408. filename_filter_box->set_visible(false);
  1409. vbox->add_child(filename_filter_box);
  1410. file_box = memnew(HBoxContainer);
  1411. file_box->add_child(memnew(Label(ETR("File:"))));
  1412. file = memnew(LineEdit);
  1413. file->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1414. file->set_stretch_ratio(4);
  1415. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1416. file_box->add_child(file);
  1417. filter = memnew(OptionButton);
  1418. filter->set_stretch_ratio(3);
  1419. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1420. filter->set_clip_text(true); // too many extensions overflows it
  1421. file_box->add_child(filter);
  1422. vbox->add_child(file_box);
  1423. grid_options = memnew(GridContainer);
  1424. grid_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  1425. grid_options->set_columns(2);
  1426. vbox->add_child(grid_options);
  1427. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1428. _update_drives();
  1429. connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_action_pressed));
  1430. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), CONNECT_DEFERRED);
  1431. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), CONNECT_DEFERRED);
  1432. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated));
  1433. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  1434. dir->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_dir_submitted));
  1435. filename_filter->connect(SceneStringName(text_changed), callable_mp(this, &FileDialog::_filename_filter_changed).unbind(1));
  1436. filename_filter->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_filename_filter_selected).unbind(1));
  1437. file->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_file_submitted));
  1438. filter->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_filter_selected));
  1439. confirm_save = memnew(ConfirmationDialog);
  1440. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  1441. confirm_save->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_save_confirm_pressed));
  1442. makedialog = memnew(ConfirmationDialog);
  1443. makedialog->set_title(ETR("Create Folder"));
  1444. VBoxContainer *makevb = memnew(VBoxContainer);
  1445. makedialog->add_child(makevb);
  1446. makedirname = memnew(LineEdit);
  1447. makedirname->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1448. makevb->add_margin_child(ETR("Name:"), makedirname);
  1449. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  1450. makedialog->register_text_enter(makedirname);
  1451. makedialog->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_make_dir_confirm));
  1452. mkdirerr = memnew(AcceptDialog);
  1453. mkdirerr->set_text(ETR("Could not create folder."));
  1454. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  1455. exterr = memnew(AcceptDialog);
  1456. exterr->set_text(ETR("Invalid extension, or empty filename."));
  1457. add_child(exterr, false, INTERNAL_MODE_FRONT);
  1458. update_filters();
  1459. update_filename_filter_gui();
  1460. update_dir();
  1461. set_hide_on_ok(false);
  1462. set_size(Size2(640, 360));
  1463. if (register_func) {
  1464. register_func(this);
  1465. }
  1466. property_helper.setup_for_instance(base_property_helper, this);
  1467. }
  1468. FileDialog::~FileDialog() {
  1469. if (unregister_func) {
  1470. unregister_func(this);
  1471. }
  1472. }