From f61f4d830eefff411889d9b3c1dfaa2ef95ee05d 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 | 5 +++-- src/Screen.cc | 37 ++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index 2c99d61..edbe313 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -234,8 +234,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 @@ -288,8 +288,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"); @@ -327,19 +329,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; @@ -347,31 +348,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 6c537de..2c90f3d 100644 --- a/src/MenuCreator.hh +++ b/src/MenuCreator.hh @@ -42,8 +42,9 @@ class FluxboxWindow; class MenuCreator { public: - static std::auto_ptr - createMenu(lua::state &l, int screen_number, FbTk::AutoReloadHelper *reloader = NULL); + static void + createMenu(FbMenu &inject_into, lua::state &l, int screen_number, + 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, diff --git a/src/Screen.cc b/src/Screen.cc index f384476..7b81469 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"), @@ -1392,26 +1392,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; @@ -1438,12 +1437,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