From c56da3e3ddce80f4e33a55a122611db8debcc8a5 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 be52e47..f5907c9 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" @@ -326,9 +327,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 edbe313..70075d1 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -235,11 +235,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); @@ -290,7 +290,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 @@ -330,7 +330,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); @@ -353,7 +353,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) { @@ -369,7 +369,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); } @@ -387,33 +387,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, m_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, m_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 2c90f3d..8e5cad1 100644 --- a/src/MenuCreator.hh +++ b/src/MenuCreator.hh @@ -43,14 +43,12 @@ class FluxboxWindow; class MenuCreator { public: static 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); static FbMenu *createMenu(const std::string &label, int screen_num); static FbMenu *createMenuType(const std::string &label, int screen_num); - static bool createFromFile(const std::string &filename, + static void createFromFile(const std::string &filename, FbTk::Menu &inject_into, - FbTk::AutoReloadHelper *reloader = NULL, - bool begin = true); + FbTk::AutoReloadHelper *reloader = NULL); static 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 7b81469..ca12cb3 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -1410,7 +1410,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; @@ -1446,7 +1446,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