file_dialog.cpp 59 KB

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