From 82883442e7a9faf234d0a26ebc373bc4026118a1 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sat, 2 Jul 2011 10:19:48 +0200 Subject: CustomMenu command now shows lua menus --- src/FbCommands.cc | 16 ++++++++++++++-- src/MenuCreator.cc | 43 ++++++++++++++----------------------------- src/MenuCreator.hh | 8 +++----- src/Screen.cc | 4 ++-- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/FbCommands.cc b/src/FbCommands.cc index b0289b8..02a614a 100644 --- a/src/FbCommands.cc +++ b/src/FbCommands.cc @@ -30,6 +30,7 @@ #include "Keys.hh" #include "MenuCreator.hh" +#include "FbTk/I18n.hh" #include "FbTk/Theme.hh" #include "FbTk/Menu.hh" #include "FbTk/CommandParser.hh" @@ -338,9 +339,20 @@ void ShowCustomMenuCmd::execute() { } void ShowCustomMenuCmd::reload() { + _FB_USES_NLS; + m_menu->removeAll(); - m_menu->setLabel(FbTk::BiDiString("")); - MenuCreator::createFromFile(custom_menu_file, *m_menu.get(), m_menu->reloadHelper()); + try { + MenuCreator::createFromFile(custom_menu_file, *m_menu, m_menu->reloadHelper()); + } + catch(std::runtime_error &e) { + fprintf(stderr, _FB_CONSOLETEXT(FbCommands, CantLoadMenu, + "Failed to load menu file '%s': %s", + "Error message when loading of custom menu fails. " + "One %s for filename, one for exception text.").c_str(), + custom_menu_file.c_str(), e.what()); + fputs("\n", stderr); + } } REGISTER_COMMAND(rootmenu, FbCommands::ShowRootMenuCmd, void); diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index 14035c0..b357dce 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -302,11 +302,11 @@ string getField(lua::state &l, int pos, const char *field, FbTk::StringConvertor } void -createMenu_(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::StringConvertor &conv, +createMenu_(FbTk::Menu &inject_into, lua::state &l, FbTk::StringConvertor &conv, FbTk::AutoReloadHelper *reloader); void -insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv, +insertMenuItem(lua::state &l, FbTk::Menu &menu, FbTk::StringConvertor &parent_conv, FbTk::AutoReloadHelper *reloader) { lua::stack_sentry s(l, -1); l.checkstack(1); @@ -357,7 +357,7 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv, } else if(str_key == "menu") { std::auto_ptr t(MenuCreator::createMenu("", screen_number)); l.pushvalue(-1); - createMenu_(*t, l, screen_number, *conv, reloader); + createMenu_(*t, l, *conv, reloader); menu.insert(str_label, t.release()); } else { // items that have a parameter @@ -397,7 +397,7 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv, } void -createMenu_(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::StringConvertor &conv, +createMenu_(FbTk::Menu &inject_into, lua::state &l, FbTk::StringConvertor &conv, FbTk::AutoReloadHelper *reloader) { lua::stack_sentry s(l, -1); @@ -420,7 +420,7 @@ createMenu_(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::StringC } // end of anonymous namespace void -MenuCreator::createMenu(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::AutoReloadHelper *reloader) { +MenuCreator::createMenu(FbTk::Menu &inject_into, lua::state &l, FbTk::AutoReloadHelper *reloader) { lua::stack_sentry s(l, -1); if(l.type(-1) != lua::TTABLE) { @@ -436,7 +436,7 @@ MenuCreator::createMenu(FbMenu &inject_into, lua::state &l, int screen_number, F if(!enc.empty()) conv->setSource(enc); - createMenu_(inject_into, l, screen_number, *conv, reloader); + createMenu_(inject_into, l, *conv, reloader); } @@ -454,33 +454,18 @@ FbMenu *MenuCreator::createMenu(const string &label, int screen_number) { return menu; } -bool MenuCreator::createFromFile(const string &filename, +void MenuCreator::createFromFile(const string &filename, FbTk::Menu &inject_into, - AutoReloadHelper *reloader, bool begin) { + AutoReloadHelper *reloader) { string real_filename = FbTk::StringUtil::expandFilename(filename); - FbMenuParser parser(real_filename); - if (!parser.isLoaded()) - return false; - - startFile(); - if (begin) { - string label; - if (!getStart(parser, label, s_stringconvertor)) { - endFile(); - return false; - } - inject_into.setLabel(label); - } - - // save menu filename, so we can check if it changes - if (reloader) - reloader->addFile(real_filename); - - parseMenu(parser, inject_into, s_stringconvertor, reloader); - endFile(); + lua::state &l = Fluxbox::instance()->lua(); + l.checkstack(1); + lua::stack_sentry s(l); - return true; + l.loadfile(real_filename.c_str()); + l.call(0, 1); + createMenu(inject_into, l, reloader); } FbMenu *MenuCreator::createMenuType(const string &type, int screen_num) { diff --git a/src/MenuCreator.hh b/src/MenuCreator.hh index 0d13529..60f931d 100644 --- a/src/MenuCreator.hh +++ b/src/MenuCreator.hh @@ -42,14 +42,12 @@ class FluxboxWindow; namespace MenuCreator { void - createMenu(FbMenu &inject_into, lua::state &l, int screen_number, - FbTk::AutoReloadHelper *reloader = NULL); + createMenu(FbTk::Menu &inject_into, lua::state &l, FbTk::AutoReloadHelper *reloader = NULL); FbMenu *createMenu(const std::string &label, int screen_num); FbMenu *createMenuType(const std::string &label, int screen_num); - bool createFromFile(const std::string &filename, + void createFromFile(const std::string &filename, FbTk::Menu &inject_into, - FbTk::AutoReloadHelper *reloader = NULL, - bool begin = true); + FbTk::AutoReloadHelper *reloader = NULL); bool createWindowMenuItem(const std::string &type, const std::string &label, FbTk::Menu &inject_into); } diff --git a/src/Screen.cc b/src/Screen.cc index 43fdafe..73ccb4c 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -1402,7 +1402,7 @@ void BScreen::rereadMenu() { m_rootmenu->removeAll(); l.loadfile(FbTk::StringUtil::expandFilename(fb->getMenuFilename()).c_str()); l.call(0, 1); - MenuCreator::createMenu(*m_rootmenu, l, screenNumber(), m_rootmenu->reloadHelper()); + MenuCreator::createMenu(*m_rootmenu, l, m_rootmenu->reloadHelper()); if (m_rootmenu->numberOfItems() == 0) { _FB_USES_NLS; @@ -1438,7 +1438,7 @@ void BScreen::rereadWindowMenu() { m_windowmenu->removeAll(); l.loadfile(FbTk::StringUtil::expandFilename(windowMenuFilename()).c_str()); l.call(0, 1); - MenuCreator::createMenu(*m_windowmenu, l, screenNumber(), m_windowmenu->reloadHelper()); + MenuCreator::createMenu(*m_windowmenu, l, m_windowmenu->reloadHelper()); } void BScreen::addConfigMenu(const FbTk::FbString &label, FbTk::Menu &menu) { -- cgit v0.11.2