file_dialog.cpp 53 KB

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