2
0

file_dialog.cpp 20 KB

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