Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 sapier
4 :
5 : This program is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as published by
7 : the Free Software Foundation; either version 2.1 of the License, or
8 : (at your option) any later version.
9 :
10 : This program is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General Public License along
16 : with this program; if not, write to the Free Software Foundation, Inc.,
17 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "guiFileSelectMenu.h"
21 : #include "util/string.h"
22 : #include <locale.h>
23 :
24 0 : GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
25 : gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
26 : std::string title, std::string formname) :
27 0 : GUIModalMenu(env, parent, id, menumgr)
28 : {
29 0 : m_title = narrow_to_wide(title);
30 0 : m_parent = parent;
31 0 : m_formname = formname;
32 0 : m_text_dst = 0;
33 0 : m_accepted = false;
34 0 : }
35 :
36 0 : GUIFileSelectMenu::~GUIFileSelectMenu()
37 : {
38 0 : removeChildren();
39 0 : }
40 :
41 0 : void GUIFileSelectMenu::removeChildren()
42 : {
43 0 : const core::list<gui::IGUIElement*> &children = getChildren();
44 0 : core::list<gui::IGUIElement*> children_copy;
45 0 : for (core::list<gui::IGUIElement*>::ConstIterator i = children.begin(); i
46 0 : != children.end(); i++)
47 : {
48 0 : children_copy.push_back(*i);
49 : }
50 0 : for (core::list<gui::IGUIElement*>::Iterator i = children_copy.begin(); i
51 0 : != children_copy.end(); i++)
52 : {
53 0 : (*i)->remove();
54 : }
55 0 : }
56 :
57 0 : void GUIFileSelectMenu::regenerateGui(v2u32 screensize)
58 : {
59 0 : removeChildren();
60 0 : m_fileOpenDialog = 0;
61 :
62 0 : core::dimension2du size(600,400);
63 0 : core::rect < s32 > rect(0,0,screensize.X,screensize.Y);
64 :
65 0 : DesiredRect = rect;
66 0 : recalculateAbsolutePosition(false);
67 :
68 : m_fileOpenDialog =
69 0 : Environment->addFileOpenDialog(m_title.c_str(),false,this,-1);
70 :
71 0 : core::position2di pos = core::position2di(screensize.X/2 - size.Width/2,screensize.Y/2 -size.Height/2);
72 0 : m_fileOpenDialog->setRelativePosition(pos);
73 0 : m_fileOpenDialog->setMinSize(size);
74 0 : }
75 :
76 0 : void GUIFileSelectMenu::drawMenu()
77 : {
78 0 : gui::IGUISkin* skin = Environment->getSkin();
79 0 : if (!skin)
80 0 : return;
81 :
82 0 : gui::IGUIElement::draw();
83 : }
84 :
85 0 : void GUIFileSelectMenu::acceptInput() {
86 0 : if ((m_text_dst != 0) && (this->m_formname != "")){
87 0 : StringMap fields;
88 :
89 0 : if (m_accepted)
90 0 : fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName());
91 : else
92 0 : fields[m_formname + "_canceled"] = m_formname;
93 :
94 0 : this->m_text_dst->gotText(fields);
95 : }
96 0 : }
97 :
98 0 : bool GUIFileSelectMenu::OnEvent(const SEvent& event)
99 : {
100 :
101 0 : if (event.EventType == irr::EET_GUI_EVENT) {
102 0 : switch (event.GUIEvent.EventType) {
103 : case gui::EGET_ELEMENT_CLOSED:
104 : case gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED:
105 0 : m_accepted=false;
106 0 : acceptInput();
107 0 : quitMenu();
108 0 : return true;
109 : break;
110 :
111 : case gui::EGET_DIRECTORY_SELECTED:
112 : case gui::EGET_FILE_SELECTED:
113 0 : m_accepted=true;
114 0 : acceptInput();
115 0 : quitMenu();
116 0 : return true;
117 : break;
118 :
119 : default:
120 : //ignore this event
121 0 : break;
122 : }
123 : }
124 0 : return Parent ? Parent->OnEvent(event) : false;
125 3 : }
|