From 32cc4ba69b78b496524b11cd9f76328820ccaef1 Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Wed, 23 Feb 2011 21:45:50 +0100 Subject: code deduplication --- src/FbTk/MultLayers.cc | 23 +-------- src/FbTk/XLayer.cc | 137 +++++++++++++++++++++++-------------------------- src/FbTk/XLayer.hh | 2 + 3 files changed, 68 insertions(+), 94 deletions(-) diff --git a/src/FbTk/MultLayers.cc b/src/FbTk/MultLayers.cc index 6469a14..0f0748f 100644 --- a/src/FbTk/MultLayers.cc +++ b/src/FbTk/MultLayers.cc @@ -114,28 +114,7 @@ void MultLayers::restack() { if (!isUpdatable()) return; - int layernum=0, winnum=0, size = this->size(); - - Window *winlist = new Window[size]; - for (layernum=0; layernum < static_cast(m_layers.size()); layernum++) { - - XLayer::ItemList::iterator it = m_layers[layernum]->getItemList().begin(); - XLayer::ItemList::iterator it_end = m_layers[layernum]->getItemList().end(); - - // add all windows from each layeritem in each layer - for (; it != it_end; ++it) { - XLayerItem::Windows::const_iterator wit = (*it)->getWindows().begin(); - XLayerItem::Windows::const_iterator wit_end = (*it)->getWindows().end(); - for (; wit != wit_end; ++wit) { - if ((*wit)->window()) - winlist[winnum++] = (*wit)->window(); - } - } - } - - XRestackWindows(FbTk::App::instance()->display(), winlist, winnum); - - delete [] winlist; + XLayer::restack(m_layers); } int MultLayers::size() { diff --git a/src/FbTk/XLayer.cc b/src/FbTk/XLayer.cc index b1bc14c..21790cb 100644 --- a/src/FbTk/XLayer.cc +++ b/src/FbTk/XLayer.cc @@ -27,6 +27,7 @@ #include "MultLayers.hh" #include +#include using std::find; using namespace FbTk; @@ -36,85 +37,88 @@ using std::cerr; using std::endl; #endif // DEBUG -XLayer::XLayer(MultLayers &manager, int layernum): - m_manager(manager), m_layernum(layernum), m_needs_restack(false) { -} +namespace { -XLayer::~XLayer() { +int sum_windows(int nr, XLayerItem* item) { + return nr + item->numWindows(); +} +int count_windows(const FbTk::XLayer::ItemList& items) { + return std::accumulate(items.begin(), items.end(), 0, sum_windows); } -void XLayer::restack() { - if (!m_manager.isUpdatable()) - return; - int num_windows = countWindows(); +void extract_windows_to_stack(const XLayerItem::Windows& windows, std::vector& stack) { + XLayerItem::Windows::const_iterator i = windows.begin(); + XLayerItem::Windows::const_iterator end = windows.end(); + for (; i != end; ++i) { + Window w = (*i)->window(); + if (w) + stack.push_back(w); + } +} - // each LayerItem can contain several windows - iterator it = itemList().begin(); - iterator it_end = itemList().end(); - Window *winlist = new Window[num_windows]; - size_t j=0; +void extract_windows_to_stack(const FbTk::XLayer::ItemList& items, XLayerItem* temp_raised, std::vector& stack) { - // add all the windows from each item - for (; it != it_end; ++it) { - XLayerItem::Windows::const_iterator wit = (*it)->getWindows().begin(); - XLayerItem::Windows::const_iterator wit_end = (*it)->getWindows().end(); - for (; wit != wit_end; ++wit) { - if ((*wit)->window()) - winlist[j++] = (*wit)->window(); + if (temp_raised) { // add windows that go on top + extract_windows_to_stack(temp_raised->getWindows(), stack); + } + + FbTk::XLayer::ItemList::const_iterator it = items.begin(); + FbTk::XLayer::ItemList::const_iterator it_end = items.end(); + for (; it != it_end; ++it) { // add all the windows from each other item + if (*it == temp_raised) { + continue; } + extract_windows_to_stack((*it)->getWindows(), stack); } +} - XRestackWindows(FbTk::App::instance()->display(), winlist, j); +void restack(const FbTk::XLayer::ItemList& items, XLayerItem* temp_raised) { - delete [] winlist; + std::vector stack; + extract_windows_to_stack(items, temp_raised, stack); - m_needs_restack = false; + if (!stack.empty()) + XRestackWindows(FbTk::App::instance()->display(), &stack[0], stack.size()); } -void XLayer::restackAndTempRaise(XLayerItem &item) { - int num_windows = countWindows(); +} // end of anonymous namespace - // each LayerItem can contain several windows - iterator it = itemList().begin(); - iterator it_end = itemList().end(); - Window *winlist = new Window[num_windows]; - size_t j=0; - - // add windows that go on top - XLayerItem::Windows::const_iterator wit = item.getWindows().begin(); - XLayerItem::Windows::const_iterator wit_end = item.getWindows().end(); - for (; wit != wit_end; ++wit) { - if ((*wit)->window()) - winlist[j++] = (*wit)->window(); - } - // add all the windows from each other item - for (; it != it_end; ++it) { - if (*it == &item) - continue; - wit = (*it)->getWindows().begin(); - wit_end = (*it)->getWindows().end(); - for (; wit != wit_end; ++wit) { - if ((*wit)->window()) - winlist[j++] = (*wit)->window(); - } +void XLayer::restack(const std::vector& layers) { + + std::vector stack; + std::vector::const_iterator l; + for (l = layers.begin(); l != layers.end(); ++l) { + extract_windows_to_stack((*l)->getItemList(), 0, stack); } - XRestackWindows(FbTk::App::instance()->display(), winlist, j); + if (!stack.empty()) + XRestackWindows(FbTk::App::instance()->display(), &stack[0], stack.size()); +} - delete [] winlist; +XLayer::XLayer(MultLayers &manager, int layernum): + m_manager(manager), m_layernum(layernum), m_needs_restack(false) { } -int XLayer::countWindows() { - int num_windows = 0; - iterator it = itemList().begin(); - iterator it_end = itemList().end(); - for (; it != it_end; ++it) { - num_windows += (*it)->numWindows(); +XLayer::~XLayer() { + +} + +void XLayer::restack() { + if (m_manager.isUpdatable()) { + ::restack(itemList(), 0); + m_needs_restack = false; } - return num_windows; +} + +void XLayer::restackAndTempRaise(XLayerItem &item) { + ::restack(itemList(), &item); +} + +int XLayer::countWindows() { + return ::count_windows(itemList()); } @@ -132,28 +136,17 @@ void XLayer::stackBelowItem(XLayerItem &item, XLayerItem *above) { return; } - Window *winlist; - size_t winnum = 1, size = item.numWindows()+1; + std::vector stack; // We do have a window to stack below // so we put it on top, and fill the rest of the array with the ones to go below it. - winlist = new Window[size]; // assume that above's window exists - winlist[0] = above->getWindows().back()->window(); + stack.push_back(above->getWindows().back()->window()); // fill the rest of the array - XLayerItem::Windows::iterator it = item.getWindows().begin(); - XLayerItem::Windows::iterator it_end = item.getWindows().end(); - for (; it != it_end; ++it) { - if ((*it)->window()) - winlist[winnum++] = (*it)->window(); - } - - // stack the windows - XRestackWindows(FbTk::App::instance()->display(), winlist, winnum); - - delete [] winlist; + extract_windows_to_stack(item.getWindows(), stack); + XRestackWindows(FbTk::App::instance()->display(), &stack[0], stack.size()); } // We can't just use Restack here, because it won't do anything if they're diff --git a/src/FbTk/XLayer.hh b/src/FbTk/XLayer.hh index ba5406b..3b7e45f 100644 --- a/src/FbTk/XLayer.hh +++ b/src/FbTk/XLayer.hh @@ -68,6 +68,8 @@ public: void lowerLayer(XLayerItem &item); void moveToLayer(XLayerItem &item, int layernum); + static void restack(const std::vector& layers); + private: void restack(); void restackAndTempRaise(XLayerItem &item); -- cgit v0.11.2