file_dialog.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*************************************************************************/
  2. /* file_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "file_dialog.h"
  30. #include "scene/gui/label.h"
  31. #include "print_string.h"
  32. #include "os/keyboard.h"
  33. FileDialog::GetIconFunc FileDialog::get_icon_func=NULL;
  34. FileDialog::GetIconFunc FileDialog::get_large_icon_func=NULL;
  35. FileDialog::RegisterFunc FileDialog::register_func=NULL;
  36. FileDialog::RegisterFunc FileDialog::unregister_func=NULL;
  37. VBoxContainer *FileDialog::get_vbox() {
  38. return vbox;
  39. }
  40. void FileDialog::_notification(int p_what) {
  41. if (p_what==NOTIFICATION_ENTER_TREE) {
  42. refresh->set_icon(get_icon("reload"));
  43. }
  44. if (p_what==NOTIFICATION_DRAW) {
  45. //RID ci = get_canvas_item();
  46. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  47. }
  48. if (p_what==NOTIFICATION_POPUP_HIDE) {
  49. set_process_unhandled_input(false);
  50. }
  51. }
  52. void FileDialog::_unhandled_input(const InputEvent& p_event) {
  53. if (p_event.type==InputEvent::KEY && is_window_modal_on_top()) {
  54. const InputEventKey &k=p_event.key;
  55. if (k.pressed) {
  56. bool handled=true;
  57. switch (k.scancode) {
  58. case KEY_H: {
  59. if (k.mod.command) {
  60. set_show_hidden_files(!show_hidden_files);
  61. } else {
  62. handled=false;
  63. }
  64. } break;
  65. default: { handled=false; }
  66. }
  67. if (handled)
  68. accept_event();
  69. }
  70. }
  71. }
  72. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  73. tree->set_select_mode(p_enable?Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  74. };
  75. Vector<String> FileDialog::get_selected_files() const {
  76. Vector<String> list;
  77. TreeItem* item = tree->get_root();
  78. while ( (item = tree->get_next_selected(item)) ) {
  79. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  80. };
  81. return list;
  82. };
  83. void FileDialog::update_dir() {
  84. dir->set_text(dir_access->get_current_dir());
  85. }
  86. void FileDialog::_dir_entered(String p_dir) {
  87. dir_access->change_dir(p_dir);
  88. file->set_text("");
  89. invalidate();
  90. update_dir();
  91. }
  92. void FileDialog::_file_entered(const String& p_file) {
  93. _action_pressed();
  94. }
  95. void FileDialog::_save_confirm_pressed() {
  96. String f=dir_access->get_current_dir().plus_file(file->get_text());
  97. emit_signal("file_selected",f);
  98. hide();
  99. }
  100. void FileDialog::_post_popup() {
  101. ConfirmationDialog::_post_popup();
  102. if (invalidated) {
  103. update_file_list();
  104. invalidated=false;
  105. }
  106. if (mode==MODE_SAVE_FILE)
  107. file->grab_focus();
  108. else
  109. tree->grab_focus();
  110. set_process_unhandled_input(true);
  111. }
  112. void FileDialog::_action_pressed() {
  113. if (mode==MODE_OPEN_FILES) {
  114. TreeItem *ti=tree->get_next_selected(NULL);
  115. String fbase=dir_access->get_current_dir();
  116. DVector<String> files;
  117. while(ti) {
  118. files.push_back( fbase.plus_file(ti->get_text(0)) );
  119. ti=tree->get_next_selected(ti);
  120. }
  121. if (files.size()) {
  122. emit_signal("files_selected",files);
  123. hide();
  124. }
  125. return;
  126. }
  127. String f=dir_access->get_current_dir().plus_file(file->get_text());
  128. if (mode==MODE_OPEN_FILE && dir_access->file_exists(f)) {
  129. emit_signal("file_selected",f);
  130. hide();
  131. }
  132. if (mode==MODE_OPEN_DIR) {
  133. String path=dir_access->get_current_dir();
  134. /*if (tree->get_selected()) {
  135. Dictionary d = tree->get_selected()->get_metadata(0);
  136. if (d["dir"]) {
  137. path=path+"/"+String(d["name"]);
  138. }
  139. }*/
  140. path=path.replace("\\","/");
  141. emit_signal("dir_selected",path);
  142. hide();
  143. }
  144. if (mode==MODE_SAVE_FILE) {
  145. bool valid=false;
  146. if (filter->get_selected()==filter->get_item_count()-1) {
  147. valid=true; //match none
  148. } else if (filters.size()>1 && filter->get_selected()==0) {
  149. // match all filters
  150. for (int i=0;i<filters.size();i++) {
  151. String flt=filters[i].get_slice(";",0);
  152. for (int j=0;j<flt.get_slice_count(",");j++) {
  153. String str = flt.get_slice(",",j).strip_edges();
  154. if (f.match(str)) {
  155. valid=true;
  156. break;
  157. }
  158. }
  159. if (valid)
  160. break;
  161. }
  162. } else {
  163. int idx=filter->get_selected();
  164. if (filters.size()>1)
  165. idx--;
  166. if (idx>=0 && idx<filters.size()) {
  167. String flt=filters[idx].get_slice(";",0);
  168. int filterSliceCount=flt.get_slice_count(",");
  169. for (int j=0;j<filterSliceCount;j++) {
  170. String str = (flt.get_slice(",",j).strip_edges());
  171. if (f.match(str)) {
  172. valid=true;
  173. break;
  174. }
  175. }
  176. if (!valid && filterSliceCount>0) {
  177. String str = (flt.get_slice(",",0).strip_edges());
  178. f+=str.substr(1, str.length()-1);
  179. file->set_text(f.get_file());
  180. valid=true;
  181. }
  182. } else {
  183. valid=true;
  184. }
  185. }
  186. if (!valid) {
  187. exterr->popup_centered_minsize(Size2(250,80));
  188. return;
  189. }
  190. if (dir_access->file_exists(f)) {
  191. confirm_save->set_text("File Exists, Overwrite?");
  192. confirm_save->popup_centered(Size2(200,80));
  193. } else {
  194. emit_signal("file_selected",f);
  195. hide();
  196. }
  197. }
  198. }
  199. void FileDialog::_cancel_pressed() {
  200. file->set_text("");
  201. invalidate();
  202. hide();
  203. }
  204. void FileDialog::_tree_selected() {
  205. TreeItem *ti=tree->get_selected();
  206. if (!ti)
  207. return;
  208. Dictionary d=ti->get_metadata(0);
  209. if (!d["dir"]) {
  210. file->set_text(d["name"]);
  211. }
  212. }
  213. void FileDialog::_tree_dc_selected() {
  214. TreeItem *ti=tree->get_selected();
  215. if (!ti)
  216. return;
  217. Dictionary d=ti->get_metadata(0);
  218. if (d["dir"]) {
  219. dir_access->change_dir(d["name"]);
  220. if (mode==MODE_OPEN_FILE || mode==MODE_OPEN_FILES || mode==MODE_OPEN_DIR)
  221. file->set_text("");
  222. call_deferred("_update_file_list");
  223. call_deferred("_update_dir");
  224. } else {
  225. _action_pressed();
  226. }
  227. }
  228. void FileDialog::update_file_list() {
  229. tree->clear();
  230. dir_access->list_dir_begin();
  231. TreeItem *root = tree->create_item();
  232. Ref<Texture> folder = get_icon("folder");
  233. List<String> files;
  234. List<String> dirs;
  235. bool isdir;
  236. bool ishidden;
  237. bool show_hidden = show_hidden_files;
  238. String item;
  239. while ((item=dir_access->get_next(&isdir))!="") {
  240. ishidden = dir_access->current_is_hidden();
  241. if (show_hidden || !ishidden) {
  242. if (!isdir)
  243. files.push_back(item);
  244. else
  245. dirs.push_back(item);
  246. }
  247. }
  248. dirs.sort_custom<NoCaseComparator>();
  249. files.sort_custom<NoCaseComparator>();
  250. while(!dirs.empty()) {
  251. if (dirs.front()->get()!=".") {
  252. TreeItem *ti=tree->create_item(root);
  253. ti->set_text(0,dirs.front()->get()+"/");
  254. ti->set_icon(0,folder);
  255. Dictionary d;
  256. d["name"]=dirs.front()->get();
  257. d["dir"]=true;
  258. ti->set_metadata(0,d);
  259. }
  260. dirs.pop_front();
  261. }
  262. dirs.clear();
  263. List<String> patterns;
  264. // build filter
  265. if (filter->get_selected()==filter->get_item_count()-1) {
  266. // match all
  267. } else if (filters.size()>1 && filter->get_selected()==0) {
  268. // match all filters
  269. for (int i=0;i<filters.size();i++) {
  270. String f=filters[i].get_slice(";",0);
  271. for (int j=0;j<f.get_slice_count(",");j++) {
  272. patterns.push_back(f.get_slice(",",j).strip_edges());
  273. }
  274. }
  275. } else {
  276. int idx=filter->get_selected();
  277. if (filters.size()>1)
  278. idx--;
  279. if (idx>=0 && idx<filters.size()) {
  280. String f=filters[idx].get_slice(";",0);
  281. for (int j=0;j<f.get_slice_count(",");j++) {
  282. patterns.push_back(f.get_slice(",",j).strip_edges());
  283. }
  284. }
  285. }
  286. String base_dir = dir_access->get_current_dir();
  287. while(!files.empty()) {
  288. bool match=patterns.empty();
  289. for(List<String>::Element *E=patterns.front();E;E=E->next()) {
  290. if (files.front()->get().matchn(E->get())) {
  291. match=true;
  292. break;
  293. }
  294. }
  295. if (match) {
  296. TreeItem *ti=tree->create_item(root);
  297. ti->set_text(0,files.front()->get());
  298. if (get_icon_func) {
  299. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  300. ti->set_icon(0,icon);
  301. }
  302. if (mode==MODE_OPEN_DIR) {
  303. ti->set_custom_color(0,get_color("files_disabled"));
  304. ti->set_selectable(0,false);
  305. }
  306. Dictionary d;
  307. d["name"]=files.front()->get();
  308. d["dir"]=false;
  309. ti->set_metadata(0,d);
  310. if (file->get_text()==files.front()->get())
  311. ti->select(0);
  312. }
  313. files.pop_front();
  314. }
  315. if (tree->get_root() && tree->get_root()->get_children())
  316. tree->get_root()->get_children()->select(0);
  317. files.clear();
  318. }
  319. void FileDialog::_filter_selected(int) {
  320. update_file_list();
  321. }
  322. void FileDialog::update_filters() {
  323. filter->clear();
  324. if (filters.size()>1) {
  325. String all_filters;
  326. const int max_filters=5;
  327. for(int i=0;i<MIN( max_filters, filters.size()) ;i++) {
  328. String flt=filters[i].get_slice(";",0);
  329. if (i>0)
  330. all_filters+=",";
  331. all_filters+=flt;
  332. }
  333. if (max_filters<filters.size())
  334. all_filters+=", ...";
  335. filter->add_item("All Recognized ( "+all_filters+" )");
  336. }
  337. for(int i=0;i<filters.size();i++) {
  338. String flt=filters[i].get_slice(";",0).strip_edges();
  339. String desc=filters[i].get_slice(";",1).strip_edges();
  340. if (desc.length())
  341. filter->add_item(desc+" ( "+flt+" )");
  342. else
  343. filter->add_item("( "+flt+" )");
  344. }
  345. filter->add_item("All Files (*)");
  346. }
  347. void FileDialog::clear_filters() {
  348. filters.clear();
  349. update_filters();
  350. invalidate();
  351. }
  352. void FileDialog::add_filter(const String& p_filter) {
  353. filters.push_back(p_filter);
  354. update_filters();
  355. invalidate();
  356. }
  357. String FileDialog::get_current_dir() const {
  358. return dir->get_text();
  359. }
  360. String FileDialog::get_current_file() const {
  361. return file->get_text();
  362. }
  363. String FileDialog::get_current_path() const {
  364. return dir->get_text().plus_file(file->get_text());
  365. }
  366. void FileDialog::set_current_dir(const String& p_dir) {
  367. dir_access->change_dir(p_dir);
  368. update_dir();
  369. invalidate();
  370. }
  371. void FileDialog::set_current_file(const String& p_file) {
  372. file->set_text(p_file);
  373. update_dir();
  374. invalidate();
  375. int lp = p_file.find_last(".");
  376. if (lp!=-1) {
  377. file->select(0,lp);
  378. file->grab_focus();
  379. }
  380. }
  381. void FileDialog::set_current_path(const String& p_path) {
  382. if (!p_path.size())
  383. return;
  384. int pos=MAX( p_path.find_last("/"), p_path.find_last("\\") );
  385. if (pos==-1) {
  386. set_current_file(p_path);
  387. } else {
  388. String dir=p_path.substr(0,pos);
  389. String file=p_path.substr(pos+1,p_path.length());
  390. set_current_dir(dir);
  391. set_current_file(file);
  392. }
  393. }
  394. void FileDialog::set_mode(Mode p_mode) {
  395. mode=p_mode;
  396. switch(mode) {
  397. case MODE_OPEN_FILE: get_ok()->set_text("Open"); set_title("Open a File"); makedir->hide(); break;
  398. case MODE_OPEN_FILES: get_ok()->set_text("Open"); set_title("Open File(s)"); makedir->hide(); break;
  399. case MODE_SAVE_FILE: get_ok()->set_text("Save"); set_title("Save a File"); makedir->show(); break;
  400. case MODE_OPEN_DIR: get_ok()->set_text("Open"); set_title("Open a Directory"); makedir->show(); break;
  401. }
  402. if (mode==MODE_OPEN_FILES) {
  403. tree->set_select_mode(Tree::SELECT_MULTI);
  404. } else {
  405. tree->set_select_mode(Tree::SELECT_SINGLE);
  406. }
  407. }
  408. FileDialog::Mode FileDialog::get_mode() const {
  409. return mode;
  410. }
  411. void FileDialog::set_access(Access p_access) {
  412. ERR_FAIL_INDEX(p_access,3);
  413. if (access==p_access)
  414. return;
  415. memdelete( dir_access );
  416. switch(p_access) {
  417. case ACCESS_FILESYSTEM: {
  418. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  419. } break;
  420. case ACCESS_RESOURCES: {
  421. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  422. } break;
  423. case ACCESS_USERDATA: {
  424. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  425. } break;
  426. }
  427. access=p_access;
  428. _update_drives();
  429. invalidate();
  430. update_filters();
  431. update_dir();
  432. }
  433. void FileDialog::invalidate() {
  434. if (is_visible()) {
  435. update_file_list();
  436. invalidated=false;
  437. } else {
  438. invalidated=true;
  439. }
  440. }
  441. FileDialog::Access FileDialog::get_access() const{
  442. return access;
  443. }
  444. void FileDialog::_make_dir_confirm() {
  445. Error err = dir_access->make_dir( makedirname->get_text() );
  446. if (err==OK) {
  447. dir_access->change_dir(makedirname->get_text());
  448. invalidate();
  449. update_filters();
  450. update_dir();
  451. } else {
  452. mkdirerr->popup_centered_minsize(Size2(250,50));
  453. }
  454. }
  455. void FileDialog::_make_dir() {
  456. makedialog->popup_centered_minsize(Size2(250,80));
  457. makedirname->grab_focus();
  458. }
  459. void FileDialog::_select_drive(int p_idx) {
  460. String d = drives->get_item_text(p_idx);
  461. dir_access->change_dir(d);
  462. file->set_text("");
  463. invalidate();
  464. update_dir();
  465. }
  466. void FileDialog::_update_drives() {
  467. int dc = dir_access->get_drive_count();
  468. if (dc==0 || access!=ACCESS_FILESYSTEM) {
  469. drives->hide();
  470. } else {
  471. drives->clear();
  472. drives->show();
  473. for(int i=0;i<dir_access->get_drive_count();i++) {
  474. String d = dir_access->get_drive(i);
  475. drives->add_item(dir_access->get_drive(i));
  476. }
  477. drives->select(dir_access->get_current_drive());
  478. }
  479. }
  480. bool FileDialog::default_show_hidden_files=false;
  481. void FileDialog::_bind_methods() {
  482. ObjectTypeDB::bind_method(_MD("_unhandled_input"),&FileDialog::_unhandled_input);
  483. ObjectTypeDB::bind_method(_MD("_tree_selected"),&FileDialog::_tree_selected);
  484. ObjectTypeDB::bind_method(_MD("_tree_db_selected"),&FileDialog::_tree_dc_selected);
  485. ObjectTypeDB::bind_method(_MD("_dir_entered"),&FileDialog::_dir_entered);
  486. ObjectTypeDB::bind_method(_MD("_file_entered"),&FileDialog::_file_entered);
  487. ObjectTypeDB::bind_method(_MD("_action_pressed"),&FileDialog::_action_pressed);
  488. ObjectTypeDB::bind_method(_MD("_cancel_pressed"),&FileDialog::_cancel_pressed);
  489. ObjectTypeDB::bind_method(_MD("_filter_selected"),&FileDialog::_filter_selected);
  490. ObjectTypeDB::bind_method(_MD("_save_confirm_pressed"),&FileDialog::_save_confirm_pressed);
  491. ObjectTypeDB::bind_method(_MD("clear_filters"),&FileDialog::clear_filters);
  492. ObjectTypeDB::bind_method(_MD("add_filter","filter"),&FileDialog::add_filter);
  493. ObjectTypeDB::bind_method(_MD("get_current_dir"),&FileDialog::get_current_dir);
  494. ObjectTypeDB::bind_method(_MD("get_current_file"),&FileDialog::get_current_file);
  495. ObjectTypeDB::bind_method(_MD("get_current_path"),&FileDialog::get_current_path);
  496. ObjectTypeDB::bind_method(_MD("set_current_dir","dir"),&FileDialog::set_current_dir);
  497. ObjectTypeDB::bind_method(_MD("set_current_file","file"),&FileDialog::set_current_file);
  498. ObjectTypeDB::bind_method(_MD("set_current_path","path"),&FileDialog::set_current_path);
  499. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&FileDialog::set_mode);
  500. ObjectTypeDB::bind_method(_MD("get_mode"),&FileDialog::get_mode);
  501. ObjectTypeDB::bind_method(_MD("get_vbox:VBoxContainer"),&FileDialog::get_vbox);
  502. ObjectTypeDB::bind_method(_MD("set_access","access"),&FileDialog::set_access);
  503. ObjectTypeDB::bind_method(_MD("get_access"),&FileDialog::get_access);
  504. ObjectTypeDB::bind_method(_MD("set_show_hidden_files","show"),&FileDialog::set_show_hidden_files);
  505. ObjectTypeDB::bind_method(_MD("is_showing_hidden_files"),&FileDialog::is_showing_hidden_files);
  506. ObjectTypeDB::bind_method(_MD("_select_drive"),&FileDialog::_select_drive);
  507. ObjectTypeDB::bind_method(_MD("_make_dir"),&FileDialog::_make_dir);
  508. ObjectTypeDB::bind_method(_MD("_make_dir_confirm"),&FileDialog::_make_dir_confirm);
  509. ObjectTypeDB::bind_method(_MD("_update_file_list"),&FileDialog::update_file_list);
  510. ObjectTypeDB::bind_method(_MD("_update_dir"),&FileDialog::update_dir);
  511. ObjectTypeDB::bind_method(_MD("invalidate"),&FileDialog::invalidate);
  512. ADD_SIGNAL(MethodInfo("file_selected",PropertyInfo( Variant::STRING,"path")));
  513. ADD_SIGNAL(MethodInfo("files_selected",PropertyInfo( Variant::STRING_ARRAY,"paths")));
  514. ADD_SIGNAL(MethodInfo("dir_selected",PropertyInfo( Variant::STRING,"dir")));
  515. BIND_CONSTANT( MODE_OPEN_FILE );
  516. BIND_CONSTANT( MODE_OPEN_FILES );
  517. BIND_CONSTANT( MODE_OPEN_DIR );
  518. BIND_CONSTANT( MODE_SAVE_FILE );
  519. BIND_CONSTANT( ACCESS_RESOURCES );
  520. BIND_CONSTANT( ACCESS_USERDATA );
  521. BIND_CONSTANT( ACCESS_FILESYSTEM );
  522. }
  523. void FileDialog::set_show_hidden_files(bool p_show) {
  524. show_hidden_files=p_show;
  525. invalidate();
  526. }
  527. bool FileDialog::is_showing_hidden_files() const {
  528. return show_hidden_files;
  529. }
  530. void FileDialog::set_default_show_hidden_files(bool p_show) {
  531. default_show_hidden_files=p_show;
  532. }
  533. FileDialog::FileDialog() {
  534. show_hidden_files=default_show_hidden_files;
  535. VBoxContainer *vbc = memnew( VBoxContainer );
  536. add_child(vbc);
  537. set_child_rect(vbc);
  538. mode=MODE_SAVE_FILE;
  539. set_title("Save a File");
  540. dir = memnew(LineEdit);
  541. HBoxContainer *pathhb = memnew( HBoxContainer );
  542. pathhb->add_child(dir);
  543. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  544. refresh = memnew( ToolButton );
  545. refresh->connect("pressed",this,"_update_file_list");
  546. pathhb->add_child(refresh);
  547. drives = memnew( OptionButton );
  548. pathhb->add_child(drives);
  549. drives->connect("item_selected",this,"_select_drive");
  550. makedir = memnew( Button );
  551. makedir->set_text("Create Folder");
  552. makedir->connect("pressed",this,"_make_dir");
  553. pathhb->add_child(makedir);
  554. vbc->add_margin_child("Path:",pathhb);
  555. tree = memnew(Tree);
  556. tree->set_hide_root(true);
  557. vbc->add_margin_child("Directories & Files:",tree,true);
  558. file = memnew(LineEdit);
  559. //add_child(file);
  560. vbc->add_margin_child("File:",file);
  561. filter = memnew( OptionButton );
  562. //add_child(filter);
  563. vbc->add_margin_child("Filter:",filter);
  564. filter->set_clip_text(true);//too many extensions overflow it
  565. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  566. access=ACCESS_RESOURCES;
  567. _update_drives();
  568. connect("confirmed", this,"_action_pressed");
  569. //cancel->connect("pressed", this,"_cancel_pressed");
  570. tree->connect("cell_selected", this,"_tree_selected",varray(),CONNECT_DEFERRED);
  571. tree->connect("item_activated", this,"_tree_db_selected",varray());
  572. dir->connect("text_entered", this,"_dir_entered");
  573. file->connect("text_entered", this,"_file_entered");
  574. filter->connect("item_selected", this,"_filter_selected");
  575. confirm_save = memnew( ConfirmationDialog );
  576. confirm_save->set_as_toplevel(true);
  577. add_child(confirm_save);
  578. confirm_save->connect("confirmed", this,"_save_confirm_pressed");
  579. makedialog = memnew( ConfirmationDialog );
  580. makedialog->set_title("Create Folder");
  581. VBoxContainer *makevb= memnew( VBoxContainer );
  582. makedialog->add_child(makevb);
  583. makedialog->set_child_rect(makevb);
  584. makedirname = memnew( LineEdit );
  585. makevb->add_margin_child("Name:",makedirname);
  586. add_child(makedialog);
  587. makedialog->register_text_enter(makedirname);
  588. makedialog->connect("confirmed",this,"_make_dir_confirm");
  589. mkdirerr = memnew( AcceptDialog );
  590. mkdirerr->set_text("Could not create folder.");
  591. add_child(mkdirerr);
  592. exterr = memnew( AcceptDialog );
  593. exterr->set_text("Must use a valid extension.");
  594. add_child(exterr);
  595. //update_file_list();
  596. update_filters();
  597. update_dir();
  598. set_hide_on_ok(false);
  599. vbox=vbc;
  600. invalidated=true;
  601. if (register_func)
  602. register_func(this);
  603. }
  604. FileDialog::~FileDialog() {
  605. if (unregister_func)
  606. unregister_func(this);
  607. memdelete(dir_access);
  608. }
  609. void LineEditFileChooser::_bind_methods() {
  610. ObjectTypeDB::bind_method(_MD("_browse"),&LineEditFileChooser::_browse);
  611. ObjectTypeDB::bind_method(_MD("_chosen"),&LineEditFileChooser::_chosen);
  612. ObjectTypeDB::bind_method(_MD("get_button:Button"),&LineEditFileChooser::get_button);
  613. ObjectTypeDB::bind_method(_MD("get_line_edit:LineEdit"),&LineEditFileChooser::get_line_edit);
  614. ObjectTypeDB::bind_method(_MD("get_file_dialog:FileDialog"),&LineEditFileChooser::get_file_dialog);
  615. }
  616. void LineEditFileChooser::_chosen(const String& p_text){
  617. line_edit->set_text(p_text);
  618. line_edit->emit_signal("text_entered",p_text);
  619. }
  620. void LineEditFileChooser::_browse() {
  621. dialog->popup_centered_ratio();
  622. }
  623. LineEditFileChooser::LineEditFileChooser() {
  624. line_edit = memnew( LineEdit );
  625. add_child(line_edit);
  626. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  627. button = memnew( Button );
  628. button->set_text(" .. ");
  629. add_child(button);
  630. button->connect("pressed",this,"_browse");
  631. dialog = memnew( FileDialog);
  632. add_child(dialog);
  633. dialog->connect("file_selected",this,"_chosen");
  634. dialog->connect("dir_selected",this,"_chosen");
  635. dialog->connect("files_selected",this,"_chosen");
  636. }