file_dialog.cpp 60 KB

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