/* ui/menu.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "filesystem/filesystem.h" #include "ui/label.h" #include "ui/button.h" #include "ui/menu.h" #include "ui/ui.h" namespace ui { Menu::Menu(Window *parent, const char *label) : Window(parent) { set_label(label); set_border(false); set_background(false); menu_background = new Bitmap(this); menu_container = new Container(this); hide(); } Menu::~Menu() { // menu_container and menu_background are deleted by Widget::~Widget() } void Menu::load() { std::string filename("menus/"); filename.append(label()); filesystem::IniFile ini; ini.open(filename); if (!ini.is_open()) { con_error << "Could not open " << ini.name() << std::endl; return; } std::string strval; Button *button = 0; Label *label = 0; while (ini.getline()) { if (ini.got_section()) { if (ini.got_section("menu")) { continue; } else if (ini.got_section("button")) { button = add_button(); } else if (ini.got_section("label")) { label = add_label(); } else if (ini.got_section()) { ini.unknown_section(); } } else if (ini.got_key()) { if (ini.in_section("menu")) { if (ini.got_key_string("background", strval)) { menu_background->set_texture(strval); } else { ini.unkown_key(); } } else if (ini.in_section("button")) { if (ini.got_key_string("text", strval)) { aux::strip_quotes(strval); button->set_text(strval); } else if (ini.got_key_string("command", strval)) { for (size_t i =0; i <= strval.size(); i++) { if (strval[i] == ',') strval[i] = ';'; } aux::strip_quotes(strval); button->set_command(strval); } else if (ini.got_key_string("align", strval)) { aux::to_label(strval); if (strval.compare("left") == 0) { button->set_alignment(AlignLeft | AlignVCenter); } else if (strval.compare("center") == 0) { button->set_alignment(AlignCenter); } else if (strval.compare("right") == 0) { button->set_alignment(AlignRight | AlignVCenter); } else { ini.unknown_value(); } } else { ini.unkown_key(); } } else if (ini.in_section("label")) { if (ini.got_key_string("text", strval)) { label->set_text(strval); } else if (ini.got_key_string("align", strval)) { aux::to_label(strval); if (strval.compare("left") == 0) { label->set_alignment(AlignLeft | AlignHCenter); } else if (strval.compare("center") == 0) { label->set_alignment(AlignCenter); } else if (strval.compare("right") == 0) { label->set_alignment(AlignRight | AlignHCenter); } else { ini.unknown_value(); } } else { ini.unkown_key(); } } } } con_debug << " " << ini.name() << " " << children().size() << " widgets" << std::endl; ini.close(); } void Menu::set_background_texture(const char *texture) { menu_background->set_texture(texture); } Label *Menu::add_label(char const * text) { Label *label = new Label(menu_container, text); label->set_alignment(AlignCenter); label->set_border(false); label->set_font(ui::root()->font_large()); return label; } Button *Menu::add_button(char const *text, char const *command) { return new Button(menu_container, text, command); } void Menu::resize() { set_size(parent()->size()); menu_background->set_size(size()); menu_container->set_location(UI::elementsize.width() * 0.25, (height() - menu_container->height()) / 2.0f); } }