diff --git a/src/viewport.cpp b/src/viewport.cpp index 303e027600..3e4c2d273f 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -266,35 +266,36 @@ void InitializeWindowViewport(Window *w, int x, int y, static Point _vp_move_offs; -static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height) +static void DoSetViewportPosition(Window::IteratorToFront it, int left, int top, int width, int height) { - for (const Window *w : Window::IterateFromBack(w)) { + for (; !it.IsEnd(); ++it) { + const Window *w = *it; if (left + width > w->left && w->left + w->width > left && top + height > w->top && w->top + w->height > top) { if (left < w->left) { - DoSetViewportPosition(w, left, top, w->left - left, height); - DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height); + DoSetViewportPosition(it, left, top, w->left - left, height); + DoSetViewportPosition(it, left + (w->left - left), top, width - (w->left - left), height); return; } if (left + width > w->left + w->width) { - DoSetViewportPosition(w, left, top, (w->left + w->width - left), height); - DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height); + DoSetViewportPosition(it, left, top, (w->left + w->width - left), height); + DoSetViewportPosition(it, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height); return; } if (top < w->top) { - DoSetViewportPosition(w, left, top, width, (w->top - top)); - DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top)); + DoSetViewportPosition(it, left, top, width, (w->top - top)); + DoSetViewportPosition(it, left, top + (w->top - top), width, height - (w->top - top)); return; } if (top + height > w->top + w->height) { - DoSetViewportPosition(w, left, top, width, (w->top + w->height - top)); - DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top)); + DoSetViewportPosition(it, left, top, width, (w->top + w->height - top)); + DoSetViewportPosition(it, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top)); return; } @@ -380,7 +381,11 @@ static void SetViewportPosition(Window *w, int x, int y) i = top + height - _screen.height; if (i >= 0) height -= i; - if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height); + if (height > 0) { + Window::IteratorToFront it(w); + ++it; + DoSetViewportPosition(it, left, top, width, height); + } } } diff --git a/src/window.cpp b/src/window.cpp index d6901bd97a..9b51eafd8a 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -896,7 +896,10 @@ static bool MayBeShown(const Window *w) */ static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom) { - for (const Window *v : Window::IterateFromBack(w->z_front)) { + Window::IteratorToFront it(w); + ++it; + for (; !it.IsEnd(); ++it) { + const Window *v = *it; if (MayBeShown(v) && right > v->left && bottom > v->top && @@ -2530,7 +2533,10 @@ static bool MaybeBringWindowToFront(Window *w) w_height = w->unshaded_size.height; } - for (Window *u : Window::IterateFromBack(w->z_front)) { + Window::IteratorToFront it(w); + ++it; + for (; !it.IsEnd(); ++it) { + Window *u = *it; /* A modal child will prevent the activation of the parent window */ if (u->parent == w && (u->window_desc->flags & WDF_MODAL)) { u->SetWhiteBorder(); diff --git a/src/window_gui.h b/src/window_gui.h index cf4ebcf73b..7f7ab2667e 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -833,39 +833,28 @@ public: Window * operator*() const { return this->w; } WindowIterator & operator++() { this->Next(); this->Validate(); return *this; } + bool IsEnd() const { return this->w == nullptr; } + private: Window *w; void Validate() { while (this->w != nullptr && this->w->window_class == WC_INVALID) this->Next(); } void Next() { if (this->w != nullptr) this->w = Tfront ? this->w->z_back : this->w->z_front; } }; + using IteratorToFront = WindowIterator; //!< Iterate in Z order towards front. + using IteratorToBack = WindowIterator; //!< Iterate in Z order towards back. /** * Iterable ensemble of all valid Windows * @tparam Tfront Wether we iterate from front */ template - struct Iterate { - Iterate(const Window *from) : from(from) {} - WindowIterator begin() { return WindowIterator(this->from); } + struct AllWindows { + AllWindows() {} + WindowIterator begin() { return WindowIterator(Tfront ? _z_front_window : _z_back_window); } WindowIterator end() { return WindowIterator(nullptr); } - bool empty() { return this->begin() == this->end(); } - private: - const Window *from; }; - - /** - * Returns an iterable ensemble of all valid Window from back to front - * @param from index of the first Window to consider - * @return an iterable ensemble of all valid Window - */ - static Iterate IterateFromBack(const Window *from = _z_back_window) { return Iterate(from); } - - /** - * Returns an iterable ensemble of all valid Window from front to back - * @param from index of the first Window to consider - * @return an iterable ensemble of all valid Window - */ - static Iterate IterateFromFront(const Window *from = _z_front_window) { return Iterate(from); } + using IterateFromBack = AllWindows; //!< Iterate all windows in Z order from back to front. + using IterateFromFront = AllWindows; //!< Iterate all windows in Z order from front to back. }; /**