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