]> git.lizzy.rs Git - minetest.git/blob - src/guiFileSelectMenu.cpp
Fix invalid usage of texture->getSize() where actually texture->getOriginalSize(...
[minetest.git] / src / guiFileSelectMenu.cpp
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 GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
25                                 gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
26                                 std::string title, std::string formname) :
27 GUIModalMenu(env, parent, id, menumgr)
28 {
29         m_title = narrow_to_wide(title);
30         m_parent = parent;
31         m_formname = formname;
32         m_text_dst = 0;
33         m_accepted = false;
34         m_previous_locale = setlocale(LC_ALL,0);
35 }
36
37 GUIFileSelectMenu::~GUIFileSelectMenu()
38 {
39         removeChildren();
40         setlocale(LC_ALL,m_previous_locale.c_str());
41 }
42
43 void GUIFileSelectMenu::removeChildren()
44 {
45         const core::list<gui::IGUIElement*> &children = getChildren();
46         core::list<gui::IGUIElement*> children_copy;
47         for (core::list<gui::IGUIElement*>::ConstIterator i = children.begin(); i
48                  != children.end(); i++)
49         {
50                 children_copy.push_back(*i);
51         }
52         for (core::list<gui::IGUIElement*>::Iterator i = children_copy.begin(); i
53                  != children_copy.end(); i++)
54         {
55                 (*i)->remove();
56         }
57 }
58
59 void GUIFileSelectMenu::regenerateGui(v2u32 screensize)
60 {
61         removeChildren();
62         m_fileOpenDialog = 0;
63
64         core::dimension2du size(600,400);
65         core::rect < s32 > rect(0,0,screensize.X,screensize.Y);
66
67         DesiredRect = rect;
68         recalculateAbsolutePosition(false);
69
70         m_fileOpenDialog =
71                         Environment->addFileOpenDialog(m_title.c_str(),false,this,-1);
72
73         core::position2di pos = core::position2di(screensize.X/2 - size.Width/2,screensize.Y/2 -size.Height/2);
74         m_fileOpenDialog->setRelativePosition(pos);
75         m_fileOpenDialog->setMinSize(size);
76 }
77
78 void GUIFileSelectMenu::drawMenu()
79 {
80         gui::IGUISkin* skin = Environment->getSkin();
81         if (!skin)
82                 return;
83
84         gui::IGUIElement::draw();
85 }
86
87 void GUIFileSelectMenu::acceptInput() {
88         if ((m_text_dst != 0) && (this->m_formname != "")){
89                 std::map<std::string, std::string> fields;
90
91                 if (m_accepted)
92                         fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName());
93                 else
94                         fields[m_formname + "_canceled"] = m_formname;
95
96                 this->m_text_dst->gotText(fields);
97         }
98 }
99
100 bool GUIFileSelectMenu::OnEvent(const SEvent& event)
101 {
102
103         if (event.EventType == irr::EET_GUI_EVENT) {
104                 switch (event.GUIEvent.EventType) {
105                         case gui::EGET_ELEMENT_CLOSED:
106                         case gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED:
107                                 m_accepted=false;
108                                 acceptInput();
109                                 quitMenu();
110                                 return true;
111                                 break;
112
113                         case gui::EGET_DIRECTORY_SELECTED:
114                         case gui::EGET_FILE_SELECTED:
115                                 m_accepted=true;
116                                 acceptInput();
117                                 quitMenu();
118                                 return true;
119                                 break;
120
121                         default:
122                                 //ignore this event
123                                 break;
124                 }
125         }
126         return Parent ? Parent->OnEvent(event) : false;
127 }