- Client::List::iterator it = _stacking.begin(), end = _stacking.end();
- // insert the windows above this window
- for (; it != end; ++it) {
- if ((*it)->layer() < layer || (raise && (*it)->layer() == layer))
- break;
- wins.push_back((*it)->frame->window());
- }
- // insert our client
- wins.push_back(client->frame->window());
+ Client::List::iterator it = _stacking.begin();
+ Client::List::const_iterator end = _stacking.end();
+
+ // the stacking list is from highest to lowest
+ for (; it != end && (*it)->layer() > client->layer(); ++it);
+
+ /*
+ if our new position is the top, we want to stack under the _focuswindow
+ otherwise, we want to stack under the previous window in the stack.
+ */
+ wins[0] = (it == _stacking.begin() ? _focuswindow :
+ ((*(--Client::List::const_iterator(it)))->frame->window()));
+ wins[1] = client->frame->window();
+