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