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