file_dialog.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*************************************************************************/
  2. /* file_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "file_dialog.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/print_string.h"
  33. #include "scene/gui/label.h"
  34. FileDialog::GetIconFunc FileDialog::get_icon_func = NULL;
  35. FileDialog::GetIconFunc FileDialog::get_large_icon_func = NULL;
  36. FileDialog::RegisterFunc FileDialog::register_func = NULL;
  37. FileDialog::RegisterFunc FileDialog::unregister_func = NULL;
  38. VBoxContainer *FileDialog::get_vbox() {
  39. return vbox;
  40. }
  41. void FileDialog::_notification(int p_what) {
  42. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  43. if (p_what == NOTIFICATION_ENTER_TREE) {
  44. dir_up->set_icon(get_icon("parent_folder"));
  45. refresh->set_icon(get_icon("reload"));
  46. show_hidden->set_icon(get_icon("toggle_hidden"));
  47. }
  48. Color font_color = get_color("font_color", "ToolButton");
  49. Color font_color_hover = get_color("font_color_hover", "ToolButton");
  50. Color font_color_pressed = get_color("font_color_pressed", "ToolButton");
  51. dir_up->add_color_override("icon_color_normal", font_color);
  52. dir_up->add_color_override("icon_color_hover", font_color_hover);
  53. dir_up->add_color_override("icon_color_pressed", font_color_pressed);
  54. refresh->add_color_override("icon_color_normal", font_color);
  55. refresh->add_color_override("icon_color_hover", font_color_hover);
  56. refresh->add_color_override("icon_color_pressed", font_color_pressed);
  57. show_hidden->add_color_override("icon_color_normal", font_color);
  58. show_hidden->add_color_override("icon_color_hover", font_color_hover);
  59. show_hidden->add_color_override("icon_color_pressed", font_color_pressed);
  60. } else if (p_what == NOTIFICATION_POPUP_HIDE) {
  61. set_process_unhandled_input(false);
  62. }
  63. }
  64. void FileDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
  65. Ref<InputEventKey> k = p_event;
  66. if (k.is_valid() && is_window_modal_on_top()) {
  67. if (k->is_pressed()) {
  68. bool handled = true;
  69. switch (k->get_scancode()) {
  70. case KEY_H: {
  71. if (k->get_command()) {
  72. set_show_hidden_files(!show_hidden_files);
  73. } else {
  74. handled = false;
  75. }
  76. } break;
  77. case KEY_F5: {
  78. invalidate();
  79. } break;
  80. case KEY_BACKSPACE: {
  81. _dir_entered("..");
  82. } break;
  83. default: {
  84. handled = false;
  85. }
  86. }
  87. if (handled)
  88. accept_event();
  89. }
  90. }
  91. }
  92. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  93. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  94. };
  95. Vector<String> FileDialog::get_selected_files() const {
  96. Vector<String> list;
  97. TreeItem *item = tree->get_root();
  98. while ((item = tree->get_next_selected(item))) {
  99. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  100. };
  101. return list;
  102. };
  103. void FileDialog::update_dir() {
  104. dir->set_text(dir_access->get_current_dir());
  105. if (drives->is_visible()) {
  106. drives->select(dir_access->get_current_drive());
  107. }
  108. // Deselect any item, to make "Select Current Folder" button text by default.
  109. deselect_items();
  110. }
  111. void FileDialog::_dir_entered(String p_dir) {
  112. dir_access->change_dir(p_dir);
  113. file->set_text("");
  114. invalidate();
  115. update_dir();
  116. }
  117. void FileDialog::_file_entered(const String &p_file) {
  118. _action_pressed();
  119. }
  120. void FileDialog::_save_confirm_pressed() {
  121. String f = dir_access->get_current_dir().plus_file(file->get_text());
  122. emit_signal("file_selected", f);
  123. hide();
  124. }
  125. void FileDialog::_post_popup() {
  126. ConfirmationDialog::_post_popup();
  127. if (invalidated) {
  128. update_file_list();
  129. invalidated = false;
  130. }
  131. if (mode == MODE_SAVE_FILE)
  132. file->grab_focus();
  133. else
  134. tree->grab_focus();
  135. set_process_unhandled_input(true);
  136. // For open dir mode, deselect all items on file dialog open.
  137. if (mode == MODE_OPEN_DIR)
  138. deselect_items();
  139. }
  140. void FileDialog::_action_pressed() {
  141. if (mode == MODE_OPEN_FILES) {
  142. TreeItem *ti = tree->get_next_selected(NULL);
  143. String fbase = dir_access->get_current_dir();
  144. PoolVector<String> files;
  145. while (ti) {
  146. files.push_back(fbase.plus_file(ti->get_text(0)));
  147. ti = tree->get_next_selected(ti);
  148. }
  149. if (files.size()) {
  150. emit_signal("files_selected", files);
  151. hide();
  152. }
  153. return;
  154. }
  155. String f = dir_access->get_current_dir().plus_file(file->get_text());
  156. if ((mode == MODE_OPEN_ANY || mode == MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  157. emit_signal("file_selected", f);
  158. hide();
  159. } else if (mode == MODE_OPEN_ANY || mode == MODE_OPEN_DIR) {
  160. String path = dir_access->get_current_dir();
  161. path = path.replace("\\", "/");
  162. TreeItem *item = tree->get_selected();
  163. if (item) {
  164. Dictionary d = item->get_metadata(0);
  165. if (d["dir"] && d["name"] != "..") {
  166. path = path.plus_file(d["name"]);
  167. }
  168. }
  169. emit_signal("dir_selected", path);
  170. hide();
  171. }
  172. if (mode == MODE_SAVE_FILE) {
  173. bool valid = false;
  174. if (filter->get_selected() == filter->get_item_count() - 1) {
  175. valid = true; // match none
  176. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  177. // match all filters
  178. for (int i = 0; i < filters.size(); i++) {
  179. String flt = filters[i].get_slice(";", 0);
  180. for (int j = 0; j < flt.get_slice_count(","); j++) {
  181. String str = flt.get_slice(",", j).strip_edges();
  182. if (f.match(str)) {
  183. valid = true;
  184. break;
  185. }
  186. }
  187. if (valid)
  188. break;
  189. }
  190. } else {
  191. int idx = filter->get_selected();
  192. if (filters.size() > 1)
  193. idx--;
  194. if (idx >= 0 && idx < filters.size()) {
  195. String flt = filters[idx].get_slice(";", 0);
  196. int filterSliceCount = flt.get_slice_count(",");
  197. for (int j = 0; j < filterSliceCount; j++) {
  198. String str = (flt.get_slice(",", j).strip_edges());
  199. if (f.match(str)) {
  200. valid = true;
  201. break;
  202. }
  203. }
  204. if (!valid && filterSliceCount > 0) {
  205. String str = (flt.get_slice(",", 0).strip_edges());
  206. f += str.substr(1, str.length() - 1);
  207. file->set_text(f.get_file());
  208. valid = true;
  209. }
  210. } else {
  211. valid = true;
  212. }
  213. }
  214. if (!valid) {
  215. exterr->popup_centered_minsize(Size2(250, 80));
  216. return;
  217. }
  218. if (dir_access->file_exists(f)) {
  219. confirm_save->set_text(RTR("File Exists, Overwrite?"));
  220. confirm_save->popup_centered(Size2(200, 80));
  221. } else {
  222. emit_signal("file_selected", f);
  223. hide();
  224. }
  225. }
  226. }
  227. void FileDialog::_cancel_pressed() {
  228. file->set_text("");
  229. invalidate();
  230. hide();
  231. }
  232. bool FileDialog::_is_open_should_be_disabled() {
  233. if (mode == MODE_OPEN_ANY || mode == MODE_SAVE_FILE)
  234. return false;
  235. TreeItem *ti = tree->get_next_selected(tree->get_root());
  236. while (ti) {
  237. TreeItem *prev_ti = ti;
  238. ti = tree->get_next_selected(tree->get_root());
  239. if (ti == prev_ti)
  240. break;
  241. }
  242. // We have something that we can't select?
  243. if (!ti)
  244. return mode != MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  245. Dictionary d = ti->get_metadata(0);
  246. // Opening a file, but selected a folder? Forbidden.
  247. return ((mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  248. (mode == MODE_OPEN_DIR && !d["dir"]);
  249. }
  250. void FileDialog::_go_up() {
  251. dir_access->change_dir("..");
  252. update_file_list();
  253. update_dir();
  254. }
  255. void FileDialog::deselect_items() {
  256. // Clear currently selected items in file manager.
  257. tree->deselect_all();
  258. // And change get_ok title.
  259. if (!tree->is_anything_selected()) {
  260. get_ok()->set_disabled(_is_open_should_be_disabled());
  261. switch (mode) {
  262. case MODE_OPEN_FILE:
  263. case MODE_OPEN_FILES:
  264. get_ok()->set_text(RTR("Open"));
  265. break;
  266. case MODE_OPEN_DIR:
  267. get_ok()->set_text(RTR("Select Current Folder"));
  268. break;
  269. case MODE_OPEN_ANY:
  270. case MODE_SAVE_FILE:
  271. // FIXME: Implement, or refactor to avoid duplication with set_mode
  272. break;
  273. }
  274. }
  275. }
  276. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  277. _tree_selected();
  278. }
  279. void FileDialog::_tree_selected() {
  280. TreeItem *ti = tree->get_selected();
  281. if (!ti)
  282. return;
  283. Dictionary d = ti->get_metadata(0);
  284. if (!d["dir"]) {
  285. file->set_text(d["name"]);
  286. } else if (mode == MODE_OPEN_DIR) {
  287. get_ok()->set_text(RTR("Select This Folder"));
  288. }
  289. get_ok()->set_disabled(_is_open_should_be_disabled());
  290. }
  291. void FileDialog::_tree_item_activated() {
  292. TreeItem *ti = tree->get_selected();
  293. if (!ti)
  294. return;
  295. Dictionary d = ti->get_metadata(0);
  296. if (d["dir"]) {
  297. dir_access->change_dir(d["name"]);
  298. if (mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES || mode == MODE_OPEN_DIR || mode == MODE_OPEN_ANY)
  299. file->set_text("");
  300. call_deferred("_update_file_list");
  301. call_deferred("_update_dir");
  302. } else {
  303. _action_pressed();
  304. }
  305. }
  306. void FileDialog::update_file_name() {
  307. int idx = filter->get_selected() - 1;
  308. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  309. if (idx == -1) idx += 1;
  310. String filter_str = filters[idx];
  311. String file_str = file->get_text();
  312. String base_name = file_str.get_basename();
  313. file_str = base_name + "." + filter_str.strip_edges().to_lower();
  314. file->set_text(file_str);
  315. }
  316. }
  317. void FileDialog::update_file_list() {
  318. tree->clear();
  319. // Scroll back to the top after opening a directory
  320. tree->get_vscroll_bar()->set_value(0);
  321. dir_access->list_dir_begin();
  322. TreeItem *root = tree->create_item();
  323. Ref<Texture> folder = get_icon("folder");
  324. const Color folder_color = get_color("folder_icon_modulate");
  325. List<String> files;
  326. List<String> dirs;
  327. bool is_hidden;
  328. String item;
  329. while ((item = dir_access->get_next()) != "") {
  330. if (item == "." || item == "..")
  331. continue;
  332. is_hidden = dir_access->current_is_hidden();
  333. if (show_hidden_files || !is_hidden) {
  334. if (!dir_access->current_is_dir())
  335. files.push_back(item);
  336. else
  337. dirs.push_back(item);
  338. }
  339. }
  340. dirs.sort_custom<NaturalNoCaseComparator>();
  341. files.sort_custom<NaturalNoCaseComparator>();
  342. while (!dirs.empty()) {
  343. String &dir_name = dirs.front()->get();
  344. TreeItem *ti = tree->create_item(root);
  345. ti->set_text(0, dir_name);
  346. ti->set_icon(0, folder);
  347. ti->set_icon_modulate(0, folder_color);
  348. Dictionary d;
  349. d["name"] = dir_name;
  350. d["dir"] = true;
  351. ti->set_metadata(0, d);
  352. dirs.pop_front();
  353. }
  354. List<String> patterns;
  355. // build filter
  356. if (filter->get_selected() == filter->get_item_count() - 1) {
  357. // match all
  358. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  359. // match all filters
  360. for (int i = 0; i < filters.size(); i++) {
  361. String f = filters[i].get_slice(";", 0);
  362. for (int j = 0; j < f.get_slice_count(","); j++) {
  363. patterns.push_back(f.get_slice(",", j).strip_edges());
  364. }
  365. }
  366. } else {
  367. int idx = filter->get_selected();
  368. if (filters.size() > 1)
  369. idx--;
  370. if (idx >= 0 && idx < filters.size()) {
  371. String f = filters[idx].get_slice(";", 0);
  372. for (int j = 0; j < f.get_slice_count(","); j++) {
  373. patterns.push_back(f.get_slice(",", j).strip_edges());
  374. }
  375. }
  376. }
  377. String base_dir = dir_access->get_current_dir();
  378. while (!files.empty()) {
  379. bool match = patterns.empty();
  380. String match_str;
  381. for (List<String>::Element *E = patterns.front(); E; E = E->next()) {
  382. if (files.front()->get().matchn(E->get())) {
  383. match_str = E->get();
  384. match = true;
  385. break;
  386. }
  387. }
  388. if (match) {
  389. TreeItem *ti = tree->create_item(root);
  390. ti->set_text(0, files.front()->get());
  391. if (get_icon_func) {
  392. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  393. ti->set_icon(0, icon);
  394. }
  395. if (mode == MODE_OPEN_DIR) {
  396. ti->set_custom_color(0, get_color("files_disabled"));
  397. ti->set_selectable(0, false);
  398. }
  399. Dictionary d;
  400. d["name"] = files.front()->get();
  401. d["dir"] = false;
  402. ti->set_metadata(0, d);
  403. if (file->get_text() == files.front()->get() || match_str == files.front()->get())
  404. ti->select(0);
  405. }
  406. files.pop_front();
  407. }
  408. if (tree->get_root() && tree->get_root()->get_children() && tree->get_selected() == NULL)
  409. tree->get_root()->get_children()->select(0);
  410. }
  411. void FileDialog::_filter_selected(int) {
  412. update_file_name();
  413. update_file_list();
  414. }
  415. void FileDialog::update_filters() {
  416. filter->clear();
  417. if (filters.size() > 1) {
  418. String all_filters;
  419. const int max_filters = 5;
  420. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  421. String flt = filters[i].get_slice(";", 0);
  422. if (i > 0)
  423. all_filters += ",";
  424. all_filters += flt;
  425. }
  426. if (max_filters < filters.size())
  427. all_filters += ", ...";
  428. filter->add_item(RTR("All Recognized") + " ( " + all_filters + " )");
  429. }
  430. for (int i = 0; i < filters.size(); i++) {
  431. String flt = filters[i].get_slice(";", 0).strip_edges();
  432. String desc = filters[i].get_slice(";", 1).strip_edges();
  433. if (desc.length())
  434. filter->add_item(String(tr(desc)) + " ( " + flt + " )");
  435. else
  436. filter->add_item("( " + flt + " )");
  437. }
  438. filter->add_item(RTR("All Files (*)"));
  439. }
  440. void FileDialog::clear_filters() {
  441. filters.clear();
  442. update_filters();
  443. invalidate();
  444. }
  445. void FileDialog::add_filter(const String &p_filter) {
  446. filters.push_back(p_filter);
  447. update_filters();
  448. invalidate();
  449. }
  450. void FileDialog::set_filters(const Vector<String> &p_filters) {
  451. filters = p_filters;
  452. update_filters();
  453. invalidate();
  454. }
  455. Vector<String> FileDialog::get_filters() const {
  456. return filters;
  457. }
  458. String FileDialog::get_current_dir() const {
  459. return dir->get_text();
  460. }
  461. String FileDialog::get_current_file() const {
  462. return file->get_text();
  463. }
  464. String FileDialog::get_current_path() const {
  465. return dir->get_text().plus_file(file->get_text());
  466. }
  467. void FileDialog::set_current_dir(const String &p_dir) {
  468. dir_access->change_dir(p_dir);
  469. update_dir();
  470. invalidate();
  471. }
  472. void FileDialog::set_current_file(const String &p_file) {
  473. file->set_text(p_file);
  474. update_dir();
  475. invalidate();
  476. int lp = p_file.find_last(".");
  477. if (lp != -1) {
  478. file->select(0, lp);
  479. if (file->is_inside_tree() && !get_tree()->is_node_being_edited(file))
  480. file->grab_focus();
  481. }
  482. }
  483. void FileDialog::set_current_path(const String &p_path) {
  484. if (!p_path.size())
  485. return;
  486. int pos = MAX(p_path.find_last("/"), p_path.find_last("\\"));
  487. if (pos == -1) {
  488. set_current_file(p_path);
  489. } else {
  490. String dir = p_path.substr(0, pos);
  491. String file = p_path.substr(pos + 1, p_path.length());
  492. set_current_dir(dir);
  493. set_current_file(file);
  494. }
  495. }
  496. void FileDialog::set_mode_overrides_title(bool p_override) {
  497. mode_overrides_title = p_override;
  498. }
  499. bool FileDialog::is_mode_overriding_title() const {
  500. return mode_overrides_title;
  501. }
  502. void FileDialog::set_mode(Mode p_mode) {
  503. ERR_FAIL_INDEX((int)p_mode, 5);
  504. mode = p_mode;
  505. switch (mode) {
  506. case MODE_OPEN_FILE:
  507. get_ok()->set_text(RTR("Open"));
  508. if (mode_overrides_title)
  509. set_title(RTR("Open a File"));
  510. makedir->hide();
  511. break;
  512. case MODE_OPEN_FILES:
  513. get_ok()->set_text(RTR("Open"));
  514. if (mode_overrides_title)
  515. set_title(RTR("Open File(s)"));
  516. makedir->hide();
  517. break;
  518. case MODE_OPEN_DIR:
  519. get_ok()->set_text(RTR("Select Current Folder"));
  520. if (mode_overrides_title)
  521. set_title(RTR("Open a Directory"));
  522. makedir->show();
  523. break;
  524. case MODE_OPEN_ANY:
  525. get_ok()->set_text(RTR("Open"));
  526. if (mode_overrides_title)
  527. set_title(RTR("Open a File or Directory"));
  528. makedir->show();
  529. break;
  530. case MODE_SAVE_FILE:
  531. get_ok()->set_text(RTR("Save"));
  532. if (mode_overrides_title)
  533. set_title(RTR("Save a File"));
  534. makedir->show();
  535. break;
  536. }
  537. if (mode == MODE_OPEN_FILES) {
  538. tree->set_select_mode(Tree::SELECT_MULTI);
  539. } else {
  540. tree->set_select_mode(Tree::SELECT_SINGLE);
  541. }
  542. }
  543. FileDialog::Mode FileDialog::get_mode() const {
  544. return mode;
  545. }
  546. void FileDialog::set_access(Access p_access) {
  547. ERR_FAIL_INDEX(p_access, 3);
  548. if (access == p_access)
  549. return;
  550. memdelete(dir_access);
  551. switch (p_access) {
  552. case ACCESS_FILESYSTEM: {
  553. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  554. } break;
  555. case ACCESS_RESOURCES: {
  556. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  557. } break;
  558. case ACCESS_USERDATA: {
  559. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  560. } break;
  561. }
  562. access = p_access;
  563. _update_drives();
  564. invalidate();
  565. update_filters();
  566. update_dir();
  567. }
  568. void FileDialog::invalidate() {
  569. if (is_visible_in_tree()) {
  570. update_file_list();
  571. invalidated = false;
  572. } else {
  573. invalidated = true;
  574. }
  575. }
  576. FileDialog::Access FileDialog::get_access() const {
  577. return access;
  578. }
  579. void FileDialog::_make_dir_confirm() {
  580. Error err = dir_access->make_dir(makedirname->get_text());
  581. if (err == OK) {
  582. dir_access->change_dir(makedirname->get_text());
  583. invalidate();
  584. update_filters();
  585. update_dir();
  586. } else {
  587. mkdirerr->popup_centered_minsize(Size2(250, 50));
  588. }
  589. makedirname->set_text(""); // reset label
  590. }
  591. void FileDialog::_make_dir() {
  592. makedialog->popup_centered_minsize(Size2(250, 80));
  593. makedirname->grab_focus();
  594. }
  595. void FileDialog::_select_drive(int p_idx) {
  596. String d = drives->get_item_text(p_idx);
  597. dir_access->change_dir(d);
  598. file->set_text("");
  599. invalidate();
  600. update_dir();
  601. }
  602. void FileDialog::_update_drives() {
  603. int dc = dir_access->get_drive_count();
  604. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  605. drives->hide();
  606. } else {
  607. drives->clear();
  608. drives->show();
  609. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  610. drives->add_item(dir_access->get_drive(i));
  611. }
  612. drives->select(dir_access->get_current_drive());
  613. }
  614. }
  615. bool FileDialog::default_show_hidden_files = false;
  616. void FileDialog::_bind_methods() {
  617. ClassDB::bind_method(D_METHOD("_unhandled_input"), &FileDialog::_unhandled_input);
  618. ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileDialog::_tree_multi_selected);
  619. ClassDB::bind_method(D_METHOD("_tree_selected"), &FileDialog::_tree_selected);
  620. ClassDB::bind_method(D_METHOD("_tree_item_activated"), &FileDialog::_tree_item_activated);
  621. ClassDB::bind_method(D_METHOD("_dir_entered"), &FileDialog::_dir_entered);
  622. ClassDB::bind_method(D_METHOD("_file_entered"), &FileDialog::_file_entered);
  623. ClassDB::bind_method(D_METHOD("_action_pressed"), &FileDialog::_action_pressed);
  624. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  625. ClassDB::bind_method(D_METHOD("_filter_selected"), &FileDialog::_filter_selected);
  626. ClassDB::bind_method(D_METHOD("_save_confirm_pressed"), &FileDialog::_save_confirm_pressed);
  627. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  628. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &FileDialog::add_filter);
  629. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  630. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  631. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  632. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  633. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  634. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  635. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  636. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  637. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  638. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  639. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &FileDialog::set_mode);
  640. ClassDB::bind_method(D_METHOD("get_mode"), &FileDialog::get_mode);
  641. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  642. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  643. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  644. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  645. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  646. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  647. ClassDB::bind_method(D_METHOD("_select_drive"), &FileDialog::_select_drive);
  648. ClassDB::bind_method(D_METHOD("_make_dir"), &FileDialog::_make_dir);
  649. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileDialog::_make_dir_confirm);
  650. ClassDB::bind_method(D_METHOD("_update_file_name"), &FileDialog::update_file_name);
  651. ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list);
  652. ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir);
  653. ClassDB::bind_method(D_METHOD("_go_up"), &FileDialog::_go_up);
  654. ClassDB::bind_method(D_METHOD("deselect_items"), &FileDialog::deselect_items);
  655. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  656. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  657. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Open File,Open Files,Open Folder,Open Any,Save"), "set_mode", "get_mode");
  658. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
  659. ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  660. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  661. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir"), "set_current_dir", "get_current_dir");
  662. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file"), "set_current_file", "get_current_file");
  663. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path"), "set_current_path", "get_current_path");
  664. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  665. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::POOL_STRING_ARRAY, "paths")));
  666. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  667. BIND_ENUM_CONSTANT(MODE_OPEN_FILE);
  668. BIND_ENUM_CONSTANT(MODE_OPEN_FILES);
  669. BIND_ENUM_CONSTANT(MODE_OPEN_DIR);
  670. BIND_ENUM_CONSTANT(MODE_OPEN_ANY);
  671. BIND_ENUM_CONSTANT(MODE_SAVE_FILE);
  672. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  673. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  674. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  675. }
  676. void FileDialog::set_show_hidden_files(bool p_show) {
  677. show_hidden_files = p_show;
  678. invalidate();
  679. }
  680. bool FileDialog::is_showing_hidden_files() const {
  681. return show_hidden_files;
  682. }
  683. void FileDialog::set_default_show_hidden_files(bool p_show) {
  684. default_show_hidden_files = p_show;
  685. }
  686. FileDialog::FileDialog() {
  687. show_hidden_files = default_show_hidden_files;
  688. mode_overrides_title = true;
  689. VBoxContainer *vbc = memnew(VBoxContainer);
  690. add_child(vbc);
  691. mode = MODE_SAVE_FILE;
  692. set_title(RTR("Save a File"));
  693. HBoxContainer *hbc = memnew(HBoxContainer);
  694. dir_up = memnew(ToolButton);
  695. dir_up->set_tooltip(RTR("Go to parent folder."));
  696. hbc->add_child(dir_up);
  697. dir_up->connect("pressed", this, "_go_up");
  698. hbc->add_child(memnew(Label(RTR("Path:"))));
  699. dir = memnew(LineEdit);
  700. hbc->add_child(dir);
  701. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  702. refresh = memnew(ToolButton);
  703. refresh->set_tooltip(RTR("Refresh files."));
  704. refresh->connect("pressed", this, "_update_file_list");
  705. hbc->add_child(refresh);
  706. show_hidden = memnew(ToolButton);
  707. show_hidden->set_toggle_mode(true);
  708. show_hidden->set_pressed(is_showing_hidden_files());
  709. show_hidden->set_tooltip(RTR("Toggle the visibility of hidden files."));
  710. show_hidden->connect("toggled", this, "set_show_hidden_files");
  711. hbc->add_child(show_hidden);
  712. drives = memnew(OptionButton);
  713. hbc->add_child(drives);
  714. drives->connect("item_selected", this, "_select_drive");
  715. makedir = memnew(Button);
  716. makedir->set_text(RTR("Create Folder"));
  717. makedir->connect("pressed", this, "_make_dir");
  718. hbc->add_child(makedir);
  719. vbc->add_child(hbc);
  720. tree = memnew(Tree);
  721. tree->set_hide_root(true);
  722. vbc->add_margin_child(RTR("Directories & Files:"), tree, true);
  723. hbc = memnew(HBoxContainer);
  724. hbc->add_child(memnew(Label(RTR("File:"))));
  725. file = memnew(LineEdit);
  726. file->set_stretch_ratio(4);
  727. file->set_h_size_flags(SIZE_EXPAND_FILL);
  728. hbc->add_child(file);
  729. filter = memnew(OptionButton);
  730. filter->set_stretch_ratio(3);
  731. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  732. filter->set_clip_text(true); // too many extensions overflows it
  733. hbc->add_child(filter);
  734. vbc->add_child(hbc);
  735. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  736. access = ACCESS_RESOURCES;
  737. _update_drives();
  738. connect("confirmed", this, "_action_pressed");
  739. tree->connect("multi_selected", this, "_tree_multi_selected", varray(), CONNECT_DEFERRED);
  740. tree->connect("cell_selected", this, "_tree_selected", varray(), CONNECT_DEFERRED);
  741. tree->connect("item_activated", this, "_tree_item_activated", varray());
  742. tree->connect("nothing_selected", this, "deselect_items");
  743. dir->connect("text_entered", this, "_dir_entered");
  744. file->connect("text_entered", this, "_file_entered");
  745. filter->connect("item_selected", this, "_filter_selected");
  746. confirm_save = memnew(ConfirmationDialog);
  747. confirm_save->set_as_toplevel(true);
  748. add_child(confirm_save);
  749. confirm_save->connect("confirmed", this, "_save_confirm_pressed");
  750. makedialog = memnew(ConfirmationDialog);
  751. makedialog->set_title(RTR("Create Folder"));
  752. VBoxContainer *makevb = memnew(VBoxContainer);
  753. makedialog->add_child(makevb);
  754. makedirname = memnew(LineEdit);
  755. makevb->add_margin_child(RTR("Name:"), makedirname);
  756. add_child(makedialog);
  757. makedialog->register_text_enter(makedirname);
  758. makedialog->connect("confirmed", this, "_make_dir_confirm");
  759. mkdirerr = memnew(AcceptDialog);
  760. mkdirerr->set_text(RTR("Could not create folder."));
  761. add_child(mkdirerr);
  762. exterr = memnew(AcceptDialog);
  763. exterr->set_text(RTR("Must use a valid extension."));
  764. add_child(exterr);
  765. update_filters();
  766. update_dir();
  767. set_hide_on_ok(false);
  768. vbox = vbc;
  769. invalidated = true;
  770. if (register_func)
  771. register_func(this);
  772. }
  773. FileDialog::~FileDialog() {
  774. if (unregister_func)
  775. unregister_func(this);
  776. memdelete(dir_access);
  777. }
  778. void LineEditFileChooser::_bind_methods() {
  779. ClassDB::bind_method(D_METHOD("_browse"), &LineEditFileChooser::_browse);
  780. ClassDB::bind_method(D_METHOD("_chosen"), &LineEditFileChooser::_chosen);
  781. ClassDB::bind_method(D_METHOD("get_button"), &LineEditFileChooser::get_button);
  782. ClassDB::bind_method(D_METHOD("get_line_edit"), &LineEditFileChooser::get_line_edit);
  783. ClassDB::bind_method(D_METHOD("get_file_dialog"), &LineEditFileChooser::get_file_dialog);
  784. }
  785. void LineEditFileChooser::_chosen(const String &p_text) {
  786. line_edit->set_text(p_text);
  787. line_edit->emit_signal("text_entered", p_text);
  788. }
  789. void LineEditFileChooser::_browse() {
  790. dialog->popup_centered_ratio();
  791. }
  792. LineEditFileChooser::LineEditFileChooser() {
  793. line_edit = memnew(LineEdit);
  794. add_child(line_edit);
  795. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  796. button = memnew(Button);
  797. button->set_text(" .. ");
  798. add_child(button);
  799. button->connect("pressed", this, "_browse");
  800. dialog = memnew(FileDialog);
  801. add_child(dialog);
  802. dialog->connect("file_selected", this, "_chosen");
  803. dialog->connect("dir_selected", this, "_chosen");
  804. dialog->connect("files_selected", this, "_chosen");
  805. }