|
| 1 | +// SuperTux |
| 2 | +// Copyright (C) 2021 mrkubax10 <[email protected]> |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +#include "gui/menu_string_array.hpp" |
| 18 | + |
| 19 | +#include <boost/format.hpp> |
| 20 | + |
| 21 | +#include "gui/menu_item.hpp" |
| 22 | +#include "util/gettext.hpp" |
| 23 | + |
| 24 | +StringArrayMenu::StringArrayMenu(std::vector<std::string>& items) : |
| 25 | + m_array_items(items), |
| 26 | + m_text(), |
| 27 | + m_selected_item(-1) |
| 28 | +{ |
| 29 | + reload(); |
| 30 | +} |
| 31 | + |
| 32 | +void |
| 33 | +StringArrayMenu::menu_action(MenuItem& item) |
| 34 | +{ |
| 35 | + int id = item.get_id(); |
| 36 | + if (id >= 0) |
| 37 | + { |
| 38 | + m_text = m_array_items[id]; |
| 39 | + m_selected_item = id; |
| 40 | + get_item_by_id(-2).set_text(str(boost::format(_("Selected item: %s")) % (m_selected_item >= 0 ? m_array_items[m_selected_item] : _("None")))); |
| 41 | + } |
| 42 | + else if (m_text.length() > 0 && id < -2) |
| 43 | + { |
| 44 | + if (id == -3) |
| 45 | + { |
| 46 | + m_array_items.push_back(m_text); |
| 47 | + } |
| 48 | + else if (id == -4 && m_selected_item >= 0) |
| 49 | + { |
| 50 | + m_array_items.insert(m_array_items.begin() + m_selected_item + 1, m_text); |
| 51 | + } |
| 52 | + else if (id == -5 && m_selected_item >= 0) |
| 53 | + { |
| 54 | + m_array_items[m_selected_item] = m_text; |
| 55 | + } |
| 56 | + else if (id == -6 && m_selected_item >= 0) |
| 57 | + { |
| 58 | + m_array_items.erase(m_array_items.begin() + m_selected_item); |
| 59 | + } |
| 60 | + m_text = ""; |
| 61 | + m_selected_item = -1; |
| 62 | + reload(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void |
| 67 | +StringArrayMenu::reload() |
| 68 | +{ |
| 69 | + clear(); |
| 70 | + add_label(_("Edit string array")); |
| 71 | + add_hl(); |
| 72 | + for (unsigned int i = 0; i < m_array_items.size(); i++) |
| 73 | + { |
| 74 | + add_entry(i, m_array_items.at(i)); |
| 75 | + } |
| 76 | + add_hl(); |
| 77 | + add_textfield(_("Text"), &m_text); |
| 78 | + add_entry(-2, str(boost::format(_("Selected item: %s")) % (m_selected_item >= 0 ? m_array_items[m_selected_item] : _("None")))); |
| 79 | + add_entry(-3, _("Add")); |
| 80 | + add_entry(-4, _("Insert")); |
| 81 | + add_entry(-5, _("Update")); |
| 82 | + add_entry(-6, _("Delete")); |
| 83 | + add_hl(); |
| 84 | + add_back(_("OK")); |
| 85 | +} |
| 86 | +/* EOF */ |
0 commit comments