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