diff --git a/src/window.cpp b/src/window.cpp index 09d858bfb4..61eb078b91 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -119,6 +119,7 @@ WindowDesc::WindowDesc(const char * const file, const int line, WindowPosition d default_height_trad(def_height_trad) { if (_window_descs == nullptr) _window_descs = new std::vector(); + _window_descs->push_back(this); } @@ -1145,6 +1146,20 @@ Window *FindWindowByClass(WindowClass cls) return nullptr; } +/** + * Find any window by its token. + * @param token Window token + * @return Pointer to the found window, or \c nullptr if not available + */ +Window *FindWindowByToken(WindowToken token) +{ + for (Window *w : Window::IterateFromBack()) { + if (w->GetWindowToken() == token) return w; + } + + return nullptr; +} + /** * Get the main window, i.e. FindWindowById(WC_MAIN_WINDOW, 0). * If the main window is not available, this function will trigger an assert. @@ -1891,6 +1906,9 @@ void Window::InitNested(WindowNumber window_number) */ Window::Window(WindowDesc *desc) : window_desc(desc), scale(_gui_scale), mouse_capture_widget(-1) { + static uint64_t last_window_token = 0; + last_window_token++; + this->window_token = WindowToken(last_window_token); } /** diff --git a/src/window_func.h b/src/window_func.h index 7374c27c88..6eefe44382 100644 --- a/src/window_func.h +++ b/src/window_func.h @@ -18,6 +18,7 @@ Window *FindWindowById(WindowClass cls, WindowNumber number); Window *FindWindowByClass(WindowClass cls); +Window *FindWindowByToken(WindowToken token); Window *GetMainWindow(); void ChangeWindowOwner(Owner old_owner, Owner new_owner); diff --git a/src/window_gui.h b/src/window_gui.h index fba710786d..7f91286d63 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -255,6 +255,8 @@ struct Window : ZeroedMemoryAllocator { WindowClass window_class; ///< Window class private: + WindowToken window_token; + /** * Helper allocation function to disallow something. * Don't allow arrays; arrays of Windows are pointless as you need @@ -334,6 +336,8 @@ public: void ChangeWindowClass(WindowClass cls); + WindowToken GetWindowToken() const { return this->window_token; } + /** * Set the timeout flag of the window and initiate the timer. */ diff --git a/src/window_type.h b/src/window_type.h index 9a6716721a..5f0dced793 100644 --- a/src/window_type.h +++ b/src/window_type.h @@ -10,6 +10,8 @@ #ifndef WINDOW_TYPE_H #define WINDOW_TYPE_H +#include "core/strong_typedef_type.hpp" + /** %Window numbers. */ enum WindowNumberEnum { WN_GAME_OPTIONS_AI = 0, ///< AI settings. @@ -819,4 +821,6 @@ enum EventState { ES_NOT_HANDLED, ///< The passed event is not handled. }; +using WindowToken = StrongType::Typedef; + #endif /* WINDOW_TYPE_H */