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