guiPathSelectMenu.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Minetest
  3. Copyright (C) 2013 sapier
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "guiPathSelectMenu.h"
  17. GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
  18. gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
  19. const std::string &title, const std::string &formname,
  20. bool is_file_select) :
  21. GUIModalMenu(env, parent, id, menumgr),
  22. m_title(utf8_to_wide(title)),
  23. m_formname(formname),
  24. m_file_select_dialog(is_file_select)
  25. {
  26. }
  27. GUIFileSelectMenu::~GUIFileSelectMenu()
  28. {
  29. removeChildren();
  30. setlocale(LC_NUMERIC, "C");
  31. }
  32. void GUIFileSelectMenu::regenerateGui(v2u32 screensize)
  33. {
  34. removeChildren();
  35. m_fileOpenDialog = 0;
  36. core::dimension2du size(600, 400);
  37. core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
  38. DesiredRect = rect;
  39. recalculateAbsolutePosition(false);
  40. m_fileOpenDialog =
  41. Environment->addFileOpenDialog(m_title.c_str(), false, this, -1);
  42. core::position2di pos = core::position2di(screensize.X / 2 - size.Width / 2,
  43. screensize.Y / 2 - size.Height / 2);
  44. m_fileOpenDialog->setRelativePosition(pos);
  45. m_fileOpenDialog->setMinSize(size);
  46. }
  47. void GUIFileSelectMenu::drawMenu()
  48. {
  49. gui::IGUISkin *skin = Environment->getSkin();
  50. if (!skin)
  51. return;
  52. gui::IGUIElement::draw();
  53. }
  54. void GUIFileSelectMenu::acceptInput()
  55. {
  56. if ((m_text_dst != 0) && (this->m_formname != "")) {
  57. StringMap fields;
  58. if (m_accepted) {
  59. std::string path;
  60. if (!m_file_select_dialog) {
  61. core::string<fschar_t> string =
  62. m_fileOpenDialog->getDirectoryName();
  63. path = std::string(string.c_str());
  64. } else {
  65. path = wide_to_utf8(m_fileOpenDialog->getFileName());
  66. }
  67. fields[m_formname + "_accepted"] = path;
  68. } else {
  69. fields[m_formname + "_canceled"] = m_formname;
  70. }
  71. this->m_text_dst->gotText(fields);
  72. }
  73. quitMenu();
  74. }
  75. bool GUIFileSelectMenu::OnEvent(const SEvent &event)
  76. {
  77. if (event.EventType == irr::EET_GUI_EVENT) {
  78. switch (event.GUIEvent.EventType) {
  79. case gui::EGET_ELEMENT_CLOSED:
  80. case gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED:
  81. m_accepted = false;
  82. acceptInput();
  83. return true;
  84. case gui::EGET_DIRECTORY_SELECTED:
  85. m_accepted = !m_file_select_dialog;
  86. acceptInput();
  87. return true;
  88. case gui::EGET_FILE_SELECTED:
  89. m_accepted = m_file_select_dialog;
  90. acceptInput();
  91. return true;
  92. default:
  93. // ignore this event
  94. break;
  95. }
  96. }
  97. return Parent ? Parent->OnEvent(event) : false;
  98. }