1 /*
2 guiFileSelectMenu.cpp
3 Copyright (C) 2013 sapier
4 */
5 
6 /*
7 This file is part of Freeminer.
8 
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Freeminer  is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Freeminer.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "guiFileSelectMenu.h"
24 #include "util/string.h"
25 #include <locale.h>
26 
GUIFileSelectMenu(gui::IGUIEnvironment * env,gui::IGUIElement * parent,s32 id,IMenuManager * menumgr,std::string title,std::string formname)27 GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
28 				gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
29 				std::string title, std::string formname) :
30 GUIModalMenu(env, parent, id, menumgr)
31 {
32 	m_title = narrow_to_wide(title);
33 	m_parent = parent;
34 	m_formname = formname;
35 	m_text_dst = 0;
36 	m_accepted = false;
37 }
38 
~GUIFileSelectMenu()39 GUIFileSelectMenu::~GUIFileSelectMenu()
40 {
41 	removeChildren();
42 }
43 
removeChildren()44 void GUIFileSelectMenu::removeChildren()
45 {
46 	const core::list<gui::IGUIElement*> &children = getChildren();
47 	core::list<gui::IGUIElement*> children_copy;
48 	for (core::list<gui::IGUIElement*>::ConstIterator i = children.begin(); i
49 		 != children.end(); i++)
50 	{
51 		children_copy.push_back(*i);
52 	}
53 	for (core::list<gui::IGUIElement*>::Iterator i = children_copy.begin(); i
54 		 != children_copy.end(); i++)
55 	{
56 		(*i)->remove();
57 	}
58 }
59 
regenerateGui(v2u32 screensize)60 void GUIFileSelectMenu::regenerateGui(v2u32 screensize)
61 {
62 	removeChildren();
63 	m_fileOpenDialog = 0;
64 
65 	core::dimension2du size(600,400);
66 	core::rect < s32 > rect(0,0,screensize.X,screensize.Y);
67 
68 	DesiredRect = rect;
69 	recalculateAbsolutePosition(false);
70 
71 	m_fileOpenDialog =
72 			Environment->addFileOpenDialog(m_title.c_str(),false,this,-1);
73 
74 	core::position2di pos = core::position2di(screensize.X/2 - size.Width/2,screensize.Y/2 -size.Height/2);
75 	m_fileOpenDialog->setRelativePosition(pos);
76 	m_fileOpenDialog->setMinSize(size);
77 }
78 
drawMenu()79 void GUIFileSelectMenu::drawMenu()
80 {
81 	gui::IGUISkin* skin = Environment->getSkin();
82 	if (!skin)
83 		return;
84 
85 	gui::IGUIElement::draw();
86 }
87 
acceptInput()88 void GUIFileSelectMenu::acceptInput() {
89 	if ((m_text_dst != 0) && (this->m_formname != "")){
90 		std::map<std::string, std::string> fields;
91 
92 		if (m_accepted)
93 			fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName());
94 		else
95 			fields[m_formname + "_canceled"] = m_formname;
96 
97 		this->m_text_dst->gotText(fields);
98 	}
99 }
100 
OnEvent(const SEvent & event)101 bool GUIFileSelectMenu::OnEvent(const SEvent& event)
102 {
103 
104 	if (event.EventType == irr::EET_GUI_EVENT) {
105 		switch (event.GUIEvent.EventType) {
106 			case gui::EGET_ELEMENT_CLOSED:
107 			case gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED:
108 				m_accepted=false;
109 				acceptInput();
110 				quitMenu();
111 				return true;
112 				break;
113 
114 			case gui::EGET_DIRECTORY_SELECTED:
115 			case gui::EGET_FILE_SELECTED:
116 				m_accepted=true;
117 				acceptInput();
118 				quitMenu();
119 				return true;
120 				break;
121 
122 			default:
123 				//ignore this event
124 				break;
125 		}
126 	}
127 	return Parent ? Parent->OnEvent(event) : false;
128 }
129