From 72a45fae3c39323206e7d19913bb3ab17a691691 Mon Sep 17 00:00:00 2001 From: Mark Tiefenbruck Date: Sun, 11 May 2008 10:17:21 -0700 Subject: reuse some menu pointers instead of using delete/new --- src/FbCommands.cc | 25 ++++++++++++------------- src/FbCommands.hh | 7 ++++--- src/MenuCreator.cc | 41 ++++++++++++----------------------------- src/MenuCreator.hh | 4 ++-- src/Screen.cc | 15 ++++++--------- src/fluxbox.cc | 3 --- 6 files changed, 36 insertions(+), 59 deletions(-) diff --git a/src/FbCommands.cc b/src/FbCommands.cc index 91490d3..4eabae2 100644 --- a/src/FbCommands.cc +++ b/src/FbCommands.cc @@ -76,13 +76,8 @@ void showMenu(const BScreen &screen, FbTk::Menu &menu) { // special case for root menu if (&menu == &screen.rootMenu()) { Fluxbox* fb = Fluxbox::instance(); - if(fb->menuTimestampsChanged()) { - // we dont show the menu here because fluxbox - // will bring up the rootmenu after the timed - // reread of the menu + if(fb->menuTimestampsChanged()) fb->rereadMenu(); - return; - } } Window root_ret; // not used @@ -335,8 +330,8 @@ void ShowClientMenuCmd::execute() { m_list.push_back(static_cast(*it)); } - m_menu = new ClientMenu(*screen, m_list, 0); - ::showMenu(*screen, **m_menu); + m_menu.reset(new ClientMenu(*screen, m_list, 0)); + ::showMenu(*screen, *m_menu.get()); } REGISTER_COMMAND_WITH_ARGS(custommenu, FbCommands::ShowCustomMenuCmd, void); @@ -347,11 +342,15 @@ void ShowCustomMenuCmd::execute() { BScreen *screen = Fluxbox::instance()->mouseScreen(); if (screen == 0) return; - m_menu = MenuCreator::createFromFile(custom_menu_file, - screen->screenNumber()); - if (!m_menu.get()) - return; - ::showMenu(*screen, **m_menu); + + if (m_menu.get()) { + m_menu->removeAll(); + m_menu->setLabel(""); + } else + m_menu.reset(screen->createMenu("")); + + MenuCreator::createFromFile(custom_menu_file, *m_menu.get()); + ::showMenu(*screen, *m_menu.get()); } REGISTER_COMMAND(rootmenu, FbCommands::ShowRootMenuCmd, void); diff --git a/src/FbCommands.hh b/src/FbCommands.hh index f9c33af..3ad45c4 100644 --- a/src/FbCommands.hh +++ b/src/FbCommands.hh @@ -25,7 +25,8 @@ #define FBCOMMANDS_HH #include "FbTk/Command.hh" -#include "FbTk/RefCount.hh" + +#include #include "ClientMenu.hh" #include "ClientPattern.hh" @@ -124,7 +125,7 @@ private: const int m_option; const ClientPattern m_pat; std::list m_list; - FbTk::RefCount m_menu; + std::auto_ptr m_menu; }; class ShowCustomMenuCmd: public FbTk::Command { @@ -133,7 +134,7 @@ public: void execute(); private: std::string custom_menu_file; - FbTk::RefCount m_menu; + std::auto_ptr m_menu; }; class ShowRootMenuCmd: public FbTk::Command { diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index 3f184f7..25f99ca 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -255,13 +255,13 @@ void translateMenuItem(FbTk::Parser &parse, ParseItem &pitem, FbTk::StringConver if (FbTk::FileUtil::isRegularFile(thisfile.c_str()) && (filelist[file_index][0] != '.') && (thisfile[thisfile.length() - 1] != '~')) { - MenuCreator::createFromFile(thisfile, menu); + MenuCreator::createFromFile(thisfile, menu, false); } } } else { // inject this file into the current menu - MenuCreator::createFromFile(newfile, menu); + MenuCreator::createFromFile(newfile, menu, false); } safe_counter--; @@ -390,33 +390,8 @@ FbTk::Menu *MenuCreator::createMenu(const string &label, int screen_number) { return menu; } -FbTk::Menu *MenuCreator::createFromFile(const string &filename, int screen_number) { - string real_filename = FbTk::StringUtil::expandFilename(filename); - Fluxbox::instance()->saveMenuFilename(real_filename.c_str()); - - FbMenuParser parser(real_filename); - if (!parser.isLoaded()) - return 0; - - startFile(); - string label; - if (!getStart(parser, label, m_stringconvertor)) { - endFile(); - return 0; - } - - FbTk::Menu *menu = createMenu(label, screen_number); - if (menu != 0) - parseMenu(parser, *menu, m_stringconvertor); - - endFile(); - - return menu; -} - - bool MenuCreator::createFromFile(const string &filename, - FbTk::Menu &inject_into) { + FbTk::Menu &inject_into, bool begin) { string real_filename = FbTk::StringUtil::expandFilename(filename); FbMenuParser parser(real_filename); @@ -424,6 +399,14 @@ bool MenuCreator::createFromFile(const string &filename, 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 Fluxbox::instance()->saveMenuFilename(real_filename.c_str()); @@ -467,7 +450,7 @@ FbTk::Menu *MenuCreator::createMenuType(const string &type, int screen_num) { } else if (type == "workspacemenu") { return new WorkspaceMenu(*screen); } else if (type == "windowmenu") { - FbTk::Menu *menu = screen->createMenu(""); + FbTk::Menu *menu = createMenu("", screen_num); menu->disableTitle(); // not titlebar if (screen->windowMenuFilename().empty() || diff --git a/src/MenuCreator.hh b/src/MenuCreator.hh index 4494dc7..660d097 100644 --- a/src/MenuCreator.hh +++ b/src/MenuCreator.hh @@ -36,9 +36,9 @@ class FluxboxWindow; class MenuCreator { public: static FbTk::Menu *createMenu(const std::string &label, int screen_num); - static FbTk::Menu *createFromFile(const std::string &filename, int screen_num); static FbTk::Menu *createMenuType(const std::string &label, int screen_num); - static bool createFromFile(const std::string &filename, FbTk::Menu &inject_into); + static bool createFromFile(const std::string &filename, + FbTk::Menu &inject_into, bool begin = true); static bool createWindowMenuFromFile(const std::string &filename, FbTk::Menu &inject_into); 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 1708a69..8b00186 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -1498,21 +1498,18 @@ void BScreen::initMenus() { void BScreen::initMenu() { if (m_rootmenu.get()) { - while (m_rootmenu->numberOfItems()) - m_rootmenu->remove(0); + m_rootmenu->removeAll(); + m_rootmenu->setLabel(""); } else m_rootmenu.reset(createMenu("")); Fluxbox * const fb = Fluxbox::instance(); - if (!fb->getMenuFilename().empty()) { - m_rootmenu.reset(MenuCreator::createFromFile(fb->getMenuFilename(), - screenNumber())); - - } + if (!fb->getMenuFilename().empty()) + MenuCreator::createFromFile(fb->getMenuFilename(), *m_rootmenu); - if (m_rootmenu.get() == 0 || m_rootmenu->numberOfItems() == 0) { + if (m_rootmenu->numberOfItems() == 0) { _FB_USES_NLS; - m_rootmenu.reset(createMenu(_FB_XTEXT(Menu, DefaultRootMenu, "Fluxbox default menu", "Title of fallback root menu"))); + m_rootmenu->setLabel(_FB_XTEXT(Menu, DefaultRootMenu, "Fluxbox default menu", "Title of fallback root menu")); FbTk::RefCount > restart_fb(FbTk::CommandParser::instance().parse("restart")); FbTk::RefCount > exit_fb(FbTk::CommandParser::instance().parse("exit")); FbTk::RefCount > execute_xterm(FbTk::CommandParser::instance().parse("exec xterm")); diff --git a/src/fluxbox.cc b/src/fluxbox.cc index d765d3c..7d774cc 100644 --- a/src/fluxbox.cc +++ b/src/fluxbox.cc @@ -1490,9 +1490,6 @@ void Fluxbox::rereadMenu() { for_each(m_screen_list.begin(), m_screen_list.end(), mem_fun(&BScreen::rereadMenu)); - - FbCommands::ShowRootMenuCmd showcmd; - showcmd.execute(); } void Fluxbox::saveMenuFilename(const char *filename) { -- cgit v0.11.2