file_dialog.cpp 28 KB

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