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