file_dialog.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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-2014 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_DRAW) {
  42. //RID ci = get_canvas_item();
  43. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  44. }
  45. }
  46. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  47. tree->set_select_mode(p_enable?Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  48. };
  49. Vector<String> FileDialog::get_selected_files() const {
  50. Vector<String> list;
  51. TreeItem* item = tree->get_root();
  52. while ( (item = tree->get_next_selected(item)) ) {
  53. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  54. };
  55. return list;
  56. };
  57. void FileDialog::update_dir() {
  58. dir->set_text(dir_access->get_current_dir());
  59. }
  60. void FileDialog::_dir_entered(String p_dir) {
  61. dir_access->change_dir(p_dir);
  62. file->set_text("");
  63. invalidate();
  64. update_dir();
  65. }
  66. void FileDialog::_file_entered(const String& p_file) {
  67. _action_pressed();
  68. }
  69. void FileDialog::_save_confirm_pressed() {
  70. String f=dir_access->get_current_dir().plus_file(file->get_text());
  71. emit_signal("file_selected",f);
  72. hide();
  73. }
  74. void FileDialog::_post_popup() {
  75. ConfirmationDialog::_post_popup();
  76. if (invalidated) {
  77. update_file_list();
  78. invalidated=false;
  79. }
  80. if (mode==MODE_SAVE_FILE)
  81. file->grab_focus();
  82. else
  83. tree->grab_focus();
  84. }
  85. void FileDialog::_action_pressed() {
  86. if (mode==MODE_OPEN_FILES) {
  87. TreeItem *ti=tree->get_next_selected(NULL);
  88. String fbase=dir_access->get_current_dir();
  89. DVector<String> files;
  90. while(ti) {
  91. files.push_back( fbase.plus_file(ti->get_text(0)) );
  92. ti=tree->get_next_selected(ti);
  93. }
  94. if (files.size()) {
  95. emit_signal("files_selected",files);
  96. hide();
  97. }
  98. return;
  99. }
  100. String f=dir_access->get_current_dir().plus_file(file->get_text());
  101. if (mode==MODE_OPEN_FILE && dir_access->file_exists(f)) {
  102. emit_signal("file_selected",f);
  103. hide();
  104. }
  105. if (mode==MODE_OPEN_DIR) {
  106. String path=dir_access->get_current_dir();
  107. /*if (tree->get_selected()) {
  108. Dictionary d = tree->get_selected()->get_metadata(0);
  109. if (d["dir"]) {
  110. path=path+"/"+String(d["name"]);
  111. }
  112. }*/
  113. path=path.replace("\\","/");
  114. emit_signal("dir_selected",path);
  115. hide();
  116. }
  117. if (mode==MODE_SAVE_FILE) {
  118. bool valid=false;
  119. if (filter->get_selected()==filter->get_item_count()-1) {
  120. valid=true; //match none
  121. } else if (filters.size()>1 && filter->get_selected()==0) {
  122. // match all filters
  123. for (int i=0;i<filters.size();i++) {
  124. String flt=filters[i].get_slice(";",0);
  125. for (int j=0;j<flt.get_slice_count(",");j++) {
  126. String str = flt.get_slice(",",j).strip_edges();
  127. if (f.match(str)) {
  128. valid=true;
  129. break;
  130. }
  131. }
  132. if (valid)
  133. break;
  134. }
  135. } else {
  136. int idx=filter->get_selected();
  137. if (filters.size()>1)
  138. idx--;
  139. if (idx>=0 && idx<filters.size()) {
  140. String flt=filters[idx].get_slice(";",0);
  141. int filterSliceCount=flt.get_slice_count(",");
  142. for (int j=0;j<filterSliceCount;j++) {
  143. String str = (flt.get_slice(",",j).strip_edges());
  144. if (f.match(str)) {
  145. valid=true;
  146. break;
  147. }
  148. }
  149. if (!valid && filterSliceCount>0) {
  150. String str = (flt.get_slice(",",0).strip_edges());
  151. f+=str.substr(1, str.length()-1);
  152. file->set_text(f.get_file());
  153. valid=true;
  154. }
  155. } else {
  156. valid=true;
  157. }
  158. }
  159. if (!valid) {
  160. exterr->popup_centered_minsize(Size2(250,80));
  161. return;
  162. }
  163. if (dir_access->file_exists(f)) {
  164. confirm_save->set_text("File Exists, Overwrite?");
  165. confirm_save->popup_centered(Size2(200,80));
  166. } else {
  167. emit_signal("file_selected",f);
  168. hide();
  169. }
  170. }
  171. }
  172. void FileDialog::_cancel_pressed() {
  173. file->set_text("");
  174. invalidate();
  175. hide();
  176. }
  177. void FileDialog::_tree_selected() {
  178. TreeItem *ti=tree->get_selected();
  179. if (!ti)
  180. return;
  181. Dictionary d=ti->get_metadata(0);
  182. if (!d["dir"]) {
  183. file->set_text(d["name"]);
  184. }
  185. }
  186. void FileDialog::_tree_dc_selected() {
  187. TreeItem *ti=tree->get_selected();
  188. if (!ti)
  189. return;
  190. Dictionary d=ti->get_metadata(0);
  191. if (d["dir"]) {
  192. dir_access->change_dir(d["name"]);
  193. if (mode==MODE_OPEN_FILE || mode==MODE_OPEN_FILES || mode==MODE_OPEN_DIR)
  194. file->set_text("");
  195. call_deferred("_update_file_list");
  196. call_deferred("_update_dir");
  197. } else {
  198. _action_pressed();
  199. }
  200. }
  201. void FileDialog::update_file_list() {
  202. tree->clear();
  203. dir_access->list_dir_begin();
  204. TreeItem *root = tree->create_item();
  205. Ref<Texture> folder = get_icon("folder");
  206. List<String> files;
  207. List<String> dirs;
  208. bool isdir;
  209. bool ishidden;
  210. bool show_hidden = show_hidden_files;
  211. String item;
  212. while ((item=dir_access->get_next(&isdir))!="") {
  213. ishidden = dir_access->current_is_hidden();
  214. if (show_hidden || !ishidden) {
  215. if (!isdir)
  216. files.push_back(item);
  217. else
  218. dirs.push_back(item);
  219. }
  220. }
  221. dirs.sort_custom<NoCaseComparator>();
  222. files.sort_custom<NoCaseComparator>();
  223. while(!dirs.empty()) {
  224. if (dirs.front()->get()!=".") {
  225. TreeItem *ti=tree->create_item(root);
  226. ti->set_text(0,dirs.front()->get()+"/");
  227. ti->set_icon(0,folder);
  228. Dictionary d;
  229. d["name"]=dirs.front()->get();
  230. d["dir"]=true;
  231. ti->set_metadata(0,d);
  232. }
  233. dirs.pop_front();
  234. }
  235. dirs.clear();
  236. List<String> patterns;
  237. // build filter
  238. if (filter->get_selected()==filter->get_item_count()-1) {
  239. // match all
  240. } else if (filters.size()>1 && filter->get_selected()==0) {
  241. // match all filters
  242. for (int i=0;i<filters.size();i++) {
  243. String f=filters[i].get_slice(";",0);
  244. for (int j=0;j<f.get_slice_count(",");j++) {
  245. patterns.push_back(f.get_slice(",",j).strip_edges());
  246. }
  247. }
  248. } else {
  249. int idx=filter->get_selected();
  250. if (filters.size()>1)
  251. idx--;
  252. if (idx>=0 && idx<filters.size()) {
  253. String f=filters[idx].get_slice(";",0);
  254. for (int j=0;j<f.get_slice_count(",");j++) {
  255. patterns.push_back(f.get_slice(",",j).strip_edges());
  256. }
  257. }
  258. }
  259. String base_dir = dir_access->get_current_dir();
  260. while(!files.empty()) {
  261. bool match=patterns.empty();
  262. for(List<String>::Element *E=patterns.front();E;E=E->next()) {
  263. if (files.front()->get().matchn(E->get())) {
  264. match=true;
  265. break;
  266. }
  267. }
  268. if (match) {
  269. TreeItem *ti=tree->create_item(root);
  270. ti->set_text(0,files.front()->get());
  271. if (get_icon_func) {
  272. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  273. ti->set_icon(0,icon);
  274. }
  275. if (mode==MODE_OPEN_DIR) {
  276. ti->set_custom_color(0,get_color("files_disabled"));
  277. ti->set_selectable(0,false);
  278. }
  279. Dictionary d;
  280. d["name"]=files.front()->get();
  281. d["dir"]=false;
  282. ti->set_metadata(0,d);
  283. if (file->get_text()==files.front()->get())
  284. ti->select(0);
  285. }
  286. files.pop_front();
  287. }
  288. if (tree->get_root() && tree->get_root()->get_children())
  289. tree->get_root()->get_children()->select(0);
  290. files.clear();
  291. }
  292. void FileDialog::_filter_selected(int) {
  293. update_file_list();
  294. }
  295. void FileDialog::update_filters() {
  296. filter->clear();
  297. if (filters.size()>1) {
  298. String all_filters;
  299. const int max_filters=5;
  300. for(int i=0;i<MIN( max_filters, filters.size()) ;i++) {
  301. String flt=filters[i].get_slice(";",0);
  302. if (i>0)
  303. all_filters+=",";
  304. all_filters+=flt;
  305. }
  306. if (max_filters<filters.size())
  307. all_filters+=", ...";
  308. filter->add_item("All Recognized ( "+all_filters+" )");
  309. }
  310. for(int i=0;i<filters.size();i++) {
  311. String flt=filters[i].get_slice(";",0).strip_edges();
  312. String desc=filters[i].get_slice(";",1).strip_edges();
  313. if (desc.length())
  314. filter->add_item(desc+" ( "+flt+" )");
  315. else
  316. filter->add_item("( "+flt+" )");
  317. }
  318. filter->add_item("All Files (*)");
  319. }
  320. void FileDialog::clear_filters() {
  321. filters.clear();
  322. update_filters();
  323. invalidate();
  324. }
  325. void FileDialog::add_filter(const String& p_filter) {
  326. filters.push_back(p_filter);
  327. update_filters();
  328. invalidate();
  329. }
  330. String FileDialog::get_current_dir() const {
  331. return dir->get_text();
  332. }
  333. String FileDialog::get_current_file() const {
  334. return file->get_text();
  335. }
  336. String FileDialog::get_current_path() const {
  337. return dir->get_text().plus_file(file->get_text());
  338. }
  339. void FileDialog::set_current_dir(const String& p_dir) {
  340. dir_access->change_dir(p_dir);
  341. update_dir();
  342. invalidate();
  343. }
  344. void FileDialog::set_current_file(const String& p_file) {
  345. file->set_text(p_file);
  346. update_dir();
  347. invalidate();
  348. int lp = p_file.find_last(".");
  349. if (lp!=-1) {
  350. file->select(0,lp);
  351. file->grab_focus();
  352. }
  353. }
  354. void FileDialog::set_current_path(const String& p_path) {
  355. if (!p_path.size())
  356. return;
  357. int pos=MAX( p_path.find_last("/"), p_path.find_last("\\") );
  358. if (pos==-1) {
  359. set_current_file(p_path);
  360. } else {
  361. String dir=p_path.substr(0,pos);
  362. String file=p_path.substr(pos+1,p_path.length());
  363. set_current_dir(dir);
  364. set_current_file(file);
  365. }
  366. }
  367. void FileDialog::set_mode(Mode p_mode) {
  368. mode=p_mode;
  369. switch(mode) {
  370. case MODE_OPEN_FILE: get_ok()->set_text("Open"); set_title("Open a File"); makedir->hide(); break;
  371. case MODE_OPEN_FILES: get_ok()->set_text("Open"); set_title("Open File(s)"); makedir->hide(); break;
  372. case MODE_SAVE_FILE: get_ok()->set_text("Save"); set_title("Save a File"); makedir->show(); break;
  373. case MODE_OPEN_DIR: get_ok()->set_text("Open"); set_title("Open a Directory"); makedir->show(); break;
  374. }
  375. if (mode==MODE_OPEN_FILES) {
  376. tree->set_select_mode(Tree::SELECT_MULTI);
  377. } else {
  378. tree->set_select_mode(Tree::SELECT_SINGLE);
  379. }
  380. }
  381. FileDialog::Mode FileDialog::get_mode() const {
  382. return mode;
  383. }
  384. void FileDialog::set_access(Access p_access) {
  385. ERR_FAIL_INDEX(p_access,3);
  386. if (access==p_access)
  387. return;
  388. memdelete( dir_access );
  389. switch(p_access) {
  390. case ACCESS_FILESYSTEM: {
  391. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  392. } break;
  393. case ACCESS_RESOURCES: {
  394. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  395. } break;
  396. case ACCESS_USERDATA: {
  397. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  398. } break;
  399. }
  400. access=p_access;
  401. _update_drives();
  402. invalidate();
  403. update_filters();
  404. update_dir();
  405. }
  406. void FileDialog::invalidate() {
  407. if (is_visible()) {
  408. update_file_list();
  409. invalidated=false;
  410. } else {
  411. invalidated=true;
  412. }
  413. }
  414. FileDialog::Access FileDialog::get_access() const{
  415. return access;
  416. }
  417. void FileDialog::_make_dir_confirm() {
  418. Error err = dir_access->make_dir( makedirname->get_text() );
  419. if (err==OK) {
  420. dir_access->change_dir(makedirname->get_text());
  421. invalidate();
  422. update_filters();
  423. update_dir();
  424. } else {
  425. mkdirerr->popup_centered_minsize(Size2(250,50));
  426. }
  427. }
  428. void FileDialog::_make_dir() {
  429. makedialog->popup_centered_minsize(Size2(250,80));
  430. makedirname->grab_focus();
  431. }
  432. void FileDialog::_select_drive(int p_idx) {
  433. String d = drives->get_item_text(p_idx);
  434. dir_access->change_dir(d);
  435. file->set_text("");
  436. invalidate();
  437. update_dir();
  438. }
  439. void FileDialog::_update_drives() {
  440. int dc = dir_access->get_drive_count();
  441. if (dc==0 || access!=ACCESS_FILESYSTEM) {
  442. drives->hide();
  443. } else {
  444. drives->clear();
  445. drives->show();
  446. int current=-1;
  447. String abspath = dir_access->get_current_dir();
  448. for(int i=0;i<dir_access->get_drive_count();i++) {
  449. String d = dir_access->get_drive(i);
  450. if (abspath.begins_with(d))
  451. current=i;
  452. drives->add_item(dir_access->get_drive(i));
  453. }
  454. if (current!=-1)
  455. drives->select(current);
  456. }
  457. }
  458. bool FileDialog::default_show_hidden_files=true;
  459. void FileDialog::_bind_methods() {
  460. ObjectTypeDB::bind_method(_MD("_tree_selected"),&FileDialog::_tree_selected);
  461. ObjectTypeDB::bind_method(_MD("_tree_db_selected"),&FileDialog::_tree_dc_selected);
  462. ObjectTypeDB::bind_method(_MD("_dir_entered"),&FileDialog::_dir_entered);
  463. ObjectTypeDB::bind_method(_MD("_file_entered"),&FileDialog::_file_entered);
  464. ObjectTypeDB::bind_method(_MD("_action_pressed"),&FileDialog::_action_pressed);
  465. ObjectTypeDB::bind_method(_MD("_cancel_pressed"),&FileDialog::_cancel_pressed);
  466. ObjectTypeDB::bind_method(_MD("_filter_selected"),&FileDialog::_filter_selected);
  467. ObjectTypeDB::bind_method(_MD("_save_confirm_pressed"),&FileDialog::_save_confirm_pressed);
  468. ObjectTypeDB::bind_method(_MD("clear_filters"),&FileDialog::clear_filters);
  469. ObjectTypeDB::bind_method(_MD("add_filter","filter"),&FileDialog::add_filter);
  470. ObjectTypeDB::bind_method(_MD("get_current_dir"),&FileDialog::get_current_dir);
  471. ObjectTypeDB::bind_method(_MD("get_current_file"),&FileDialog::get_current_file);
  472. ObjectTypeDB::bind_method(_MD("get_current_path"),&FileDialog::get_current_path);
  473. ObjectTypeDB::bind_method(_MD("set_current_dir","dir"),&FileDialog::set_current_dir);
  474. ObjectTypeDB::bind_method(_MD("set_current_file","file"),&FileDialog::set_current_file);
  475. ObjectTypeDB::bind_method(_MD("set_current_path","path"),&FileDialog::set_current_path);
  476. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&FileDialog::set_mode);
  477. ObjectTypeDB::bind_method(_MD("get_mode"),&FileDialog::get_mode);
  478. ObjectTypeDB::bind_method(_MD("get_vbox:VBoxContainer"),&FileDialog::get_vbox);
  479. ObjectTypeDB::bind_method(_MD("set_access","access"),&FileDialog::set_access);
  480. ObjectTypeDB::bind_method(_MD("get_access"),&FileDialog::get_access);
  481. ObjectTypeDB::bind_method(_MD("set_show_hidden_files"),&FileDialog::set_show_hidden_files);
  482. ObjectTypeDB::bind_method(_MD("is_showing_hidden_files"),&FileDialog::is_showing_hidden_files);
  483. ObjectTypeDB::bind_method(_MD("_select_drive"),&FileDialog::_select_drive);
  484. ObjectTypeDB::bind_method(_MD("_make_dir"),&FileDialog::_make_dir);
  485. ObjectTypeDB::bind_method(_MD("_make_dir_confirm"),&FileDialog::_make_dir_confirm);
  486. ObjectTypeDB::bind_method(_MD("_update_file_list"),&FileDialog::update_file_list);
  487. ObjectTypeDB::bind_method(_MD("_update_dir"),&FileDialog::update_dir);
  488. ObjectTypeDB::bind_method(_MD("invalidate"),&FileDialog::invalidate);
  489. ADD_SIGNAL(MethodInfo("file_selected",PropertyInfo( Variant::STRING,"path")));
  490. ADD_SIGNAL(MethodInfo("files_selected",PropertyInfo( Variant::STRING_ARRAY,"paths")));
  491. ADD_SIGNAL(MethodInfo("dir_selected",PropertyInfo( Variant::STRING,"dir")));
  492. BIND_CONSTANT( MODE_OPEN_FILE );
  493. BIND_CONSTANT( MODE_OPEN_FILES );
  494. BIND_CONSTANT( MODE_OPEN_DIR );
  495. BIND_CONSTANT( MODE_SAVE_FILE );
  496. BIND_CONSTANT( ACCESS_RESOURCES );
  497. BIND_CONSTANT( ACCESS_USERDATA );
  498. BIND_CONSTANT( ACCESS_FILESYSTEM );
  499. }
  500. void FileDialog::set_show_hidden_files(bool p_show) {
  501. show_hidden_files=p_show;
  502. invalidate();
  503. }
  504. bool FileDialog::is_showing_hidden_files() const {
  505. return show_hidden_files;
  506. }
  507. void FileDialog::set_default_show_hidden_files(bool p_show) {
  508. default_show_hidden_files=p_show;
  509. }
  510. FileDialog::FileDialog() {
  511. show_hidden_files=true;
  512. VBoxContainer *vbc = memnew( VBoxContainer );
  513. add_child(vbc);
  514. set_child_rect(vbc);
  515. mode=MODE_SAVE_FILE;
  516. set_title("Save a File");
  517. dir = memnew(LineEdit);
  518. HBoxContainer *pathhb = memnew( HBoxContainer );
  519. pathhb->add_child(dir);
  520. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  521. drives = memnew( OptionButton );
  522. pathhb->add_child(drives);
  523. drives->connect("item_selected",this,"_select_drive");
  524. makedir = memnew( Button );
  525. makedir->set_text("Create Folder");
  526. makedir->connect("pressed",this,"_make_dir");
  527. pathhb->add_child(makedir);
  528. vbc->add_margin_child("Path:",pathhb);
  529. tree = memnew(Tree);
  530. tree->set_hide_root(true);
  531. vbc->add_margin_child("Directories & Files:",tree,true);
  532. file = memnew(LineEdit);
  533. //add_child(file);
  534. vbc->add_margin_child("File:",file);
  535. filter = memnew( OptionButton );
  536. //add_child(filter);
  537. vbc->add_margin_child("Filter:",filter);
  538. filter->set_clip_text(true);//too many extensions overflow it
  539. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  540. access=ACCESS_RESOURCES;
  541. _update_drives();
  542. connect("confirmed", this,"_action_pressed");
  543. //cancel->connect("pressed", this,"_cancel_pressed");
  544. tree->connect("cell_selected", this,"_tree_selected",varray(),CONNECT_DEFERRED);
  545. tree->connect("item_activated", this,"_tree_db_selected",varray());
  546. dir->connect("text_entered", this,"_dir_entered");
  547. file->connect("text_entered", this,"_file_entered");
  548. filter->connect("item_selected", this,"_filter_selected");
  549. confirm_save = memnew( ConfirmationDialog );
  550. confirm_save->set_as_toplevel(true);
  551. add_child(confirm_save);
  552. confirm_save->connect("confirmed", this,"_save_confirm_pressed");
  553. makedialog = memnew( ConfirmationDialog );
  554. makedialog->set_title("Create Folder");
  555. VBoxContainer *makevb= memnew( VBoxContainer );
  556. makedialog->add_child(makevb);
  557. makedialog->set_child_rect(makevb);
  558. makedirname = memnew( LineEdit );
  559. makevb->add_margin_child("Name:",makedirname);
  560. add_child(makedialog);
  561. makedialog->register_text_enter(makedirname);
  562. makedialog->connect("confirmed",this,"_make_dir_confirm");
  563. mkdirerr = memnew( AcceptDialog );
  564. mkdirerr->set_text("Could not create folder.");
  565. add_child(mkdirerr);
  566. exterr = memnew( AcceptDialog );
  567. exterr->set_text("Must use a valid extension.");
  568. add_child(exterr);
  569. //update_file_list();
  570. update_filters();
  571. update_dir();
  572. set_hide_on_ok(false);
  573. vbox=vbc;
  574. invalidated=true;
  575. if (register_func)
  576. register_func(this);
  577. }
  578. FileDialog::~FileDialog() {
  579. if (unregister_func)
  580. unregister_func(this);
  581. memdelete(dir_access);
  582. }
  583. void LineEditFileChooser::_bind_methods() {
  584. ObjectTypeDB::bind_method(_MD("_browse"),&LineEditFileChooser::_browse);
  585. ObjectTypeDB::bind_method(_MD("_chosen"),&LineEditFileChooser::_chosen);
  586. ObjectTypeDB::bind_method(_MD("get_button:Button"),&LineEditFileChooser::get_button);
  587. ObjectTypeDB::bind_method(_MD("get_line_edit:LineEdit"),&LineEditFileChooser::get_line_edit);
  588. ObjectTypeDB::bind_method(_MD("get_file_dialog:FileDialog"),&LineEditFileChooser::get_file_dialog);
  589. }
  590. void LineEditFileChooser::_chosen(const String& p_text){
  591. line_edit->set_text(p_text);
  592. line_edit->emit_signal("text_entered",p_text);
  593. }
  594. void LineEditFileChooser::_browse() {
  595. dialog->popup_centered_ratio();
  596. }
  597. LineEditFileChooser::LineEditFileChooser() {
  598. line_edit = memnew( LineEdit );
  599. add_child(line_edit);
  600. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  601. button = memnew( Button );
  602. button->set_text(" .. ");
  603. add_child(button);
  604. button->connect("pressed",this,"_browse");
  605. dialog = memnew( FileDialog);
  606. add_child(dialog);
  607. dialog->connect("file_selected",this,"_chosen");
  608. dialog->connect("dir_selected",this,"_chosen");
  609. dialog->connect("files_selected",this,"_chosen");
  610. }