From dee1ec29d0db48b8a721e8152a980f1379e332af Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 1 Jul 2011 23:30:54 +0200 Subject: Convert windowmenu to lua also, i've restored the autoreloading capability of menus. --- src/MenuCreator.cc | 34 ++++++++++++++++------------------ src/MenuCreator.hh | 6 +++--- src/Screen.cc | 37 ++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index cc891cc..14035c0 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -301,8 +301,8 @@ string getField(lua::state &l, int pos, const char *field, FbTk::StringConvertor return conv ? conv->recode(val) : val; } -std::auto_ptr -createMenu_(lua::state &l, int screen_number, FbTk::StringConvertor &conv, +void +createMenu_(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::StringConvertor &conv, FbTk::AutoReloadHelper *reloader); void @@ -355,8 +355,10 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv, } else if (str_key == "config") { menu.insert(str_label, &screen->configMenu()); } else if(str_key == "menu") { + std::auto_ptr t(MenuCreator::createMenu("", screen_number)); l.pushvalue(-1); - menu.insert(str_label, createMenu_(l, screen_number, *conv, reloader).release()); + createMenu_(*t, l, screen_number, *conv, reloader); + menu.insert(str_label, t.release()); } else { // items that have a parameter const string &str_cmd = getField(l, -1, "param"); @@ -394,19 +396,18 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv, } } -std::auto_ptr -createMenu_(lua::state &l, int screen_number, FbTk::StringConvertor &conv, +void +createMenu_(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::StringConvertor &conv, FbTk::AutoReloadHelper *reloader) { lua::stack_sentry s(l, -1); l.checkstack(1); - std::auto_ptr menu( MenuCreator::createMenu(getField(l, -1, "label", &conv), - screen_number) ); + inject_into.setLabel(getField(l, -1, "label", &conv)); for(int i = 1; l.rawgeti(-1, i), !l.isnil(-1); ++i) { try { - insertMenuItem(l, *menu, conv, reloader); + insertMenuItem(l, inject_into, conv, reloader); } catch(std::runtime_error &e) { cerr << e.what() << endl; @@ -414,31 +415,28 @@ createMenu_(lua::state &l, int screen_number, FbTk::StringConvertor &conv, } l.pop(); l.pop(); - return menu; } } // end of anonymous namespace -std::auto_ptr -MenuCreator::createMenu(lua::state &l, int screen_number, FbTk::AutoReloadHelper *reloader) { +void +MenuCreator::createMenu(FbMenu &inject_into, lua::state &l, int screen_number, FbTk::AutoReloadHelper *reloader) { lua::stack_sentry s(l, -1); - l.checkstack(1); if(l.type(-1) != lua::TTABLE) { cerr << _FB_CONSOLETEXT(Menu, MenuNotTable, "Warning: Menu is not a lua table", "Menu is not a lua table") << endl; - return std::auto_ptr(); + return; } std::auto_ptr conv(new FbTk::StringConvertor(FbTk::StringConvertor::ToFbString)); // if menu specifies an encoding, create a convertor for it - l.rawgetfield(-1, "encoding"); { - if(l.isstring(-1)) - conv->setSource(l.tostring(-1)); - } l.pop(); + const std::string &enc = getField(l, -1, "encoding"); + if(!enc.empty()) + conv->setSource(enc); - return createMenu_(l, screen_number, *conv, reloader); + createMenu_(inject_into, l, screen_number, *conv, reloader); } diff --git a/src/MenuCreator.hh b/src/MenuCreator.hh index 4329639..0d13529 100644 --- a/src/MenuCreator.hh +++ b/src/MenuCreator.hh @@ -41,9 +41,9 @@ class FbMenu; class FluxboxWindow; namespace MenuCreator { - - std::auto_ptr - createMenu(lua::state &l, int screen_number, FbTk::AutoReloadHelper *reloader = NULL); + void + createMenu(FbMenu &inject_into, lua::state &l, int screen_number, + 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, diff --git a/src/Screen.cc b/src/Screen.cc index 64512eb..43fdafe 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -259,7 +259,7 @@ BScreen::ScreenResource::ScreenResource(FbTk::ResourceManager_base &rm, click_raises(rm, true, scrname+".clickRaises"), default_deco(rm, "NORMAL", scrname+".defaultDeco"), tab_placement(rm, FbWinFrame::TOPLEFT, scrname+".tab.placement"), - windowmenufile(rm, Fluxbox::instance()->getDefaultDataFilename("windowmenu"), scrname+".windowMenu"), + windowmenufile(rm, Fluxbox::instance()->getDefaultDataFilename("windowmenu2"), scrname+".windowMenu"), typing_delay(rm, 0, scrname+".noFocusWhileTypingDelay"), workspaces(rm, 4, scrname+".workspaces"), edge_snap_threshold(rm, 10, scrname+".edgeSnapThreshold"), @@ -1384,26 +1384,25 @@ void BScreen::reassociateWindow(FluxboxWindow *w, unsigned int wkspc_id, } void BScreen::initMenus() { - lua::state &l = Fluxbox::instance()->lua(); m_workspacemenu.reset(MenuCreator::createMenuType("workspacemenu", screenNumber())); - l.loadfile(FbTk::StringUtil::expandFilename(Fluxbox::instance()->getMenuFilename()).c_str()); - l.call(0, 0); - l.getglobal("menu"); - m_rootmenu = MenuCreator::createMenu(l, 0); -// m_rootmenu->reloadHelper()->setMainFile(Fluxbox::instance()->getMenuFilename()); + + m_rootmenu->reloadHelper()->setMainFile(Fluxbox::instance()->getMenuFilename()); m_windowmenu->reloadHelper()->setMainFile(windowMenuFilename()); } void BScreen::rereadMenu() { - m_rootmenu->removeAll(); - m_rootmenu->setLabel(FbTk::BiDiString("")); - Fluxbox * const fb = Fluxbox::instance(); - if (!fb->getMenuFilename().empty()) - MenuCreator::createFromFile(fb->getMenuFilename(), *m_rootmenu, - m_rootmenu->reloadHelper()); + lua::state &l = fb->lua(); + l.checkstack(1); + lua::stack_sentry s(l); + + // XXX try/catch + 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()); if (m_rootmenu->numberOfItems() == 0) { _FB_USES_NLS; @@ -1430,12 +1429,16 @@ const std::string BScreen::windowMenuFilename() const { } void BScreen::rereadWindowMenu() { + lua::state &l = Fluxbox::instance()->lua(); + l.checkstack(1); + lua::stack_sentry s(l); - m_windowmenu->removeAll(); - if (!windowMenuFilename().empty()) - MenuCreator::createFromFile(windowMenuFilename(), *m_windowmenu, - m_windowmenu->reloadHelper()); + // XXX try/catch + m_windowmenu->removeAll(); + l.loadfile(FbTk::StringUtil::expandFilename(windowMenuFilename()).c_str()); + l.call(0, 1); + MenuCreator::createMenu(*m_windowmenu, l, screenNumber(), m_windowmenu->reloadHelper()); } void BScreen::addConfigMenu(const FbTk::FbString &label, FbTk::Menu &menu) { -- cgit v0.11.2