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