From 38466681b6ba29a6ed3d92c46ea7a2e37c83d0fb Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Fri, 25 Aug 2017 13:50:17 +1100 Subject: [PATCH] GUI2/Window: handle CLOSE_WINDOW event with a signal handler --- src/gui/core/event/handler.cpp | 31 ++++++++++++++++++++----------- src/gui/widgets/window.cpp | 7 +++++++ src/gui/widgets/window.hpp | 2 ++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/gui/core/event/handler.cpp b/src/gui/core/event/handler.cpp index bed4ef006f7b..5e16f4410f73 100644 --- a/src/gui/core/event/handler.cpp +++ b/src/gui/core/event/handler.cpp @@ -236,7 +236,6 @@ class sdl_event_handler : public events::sdl_handler */ void hat_motion(const SDL_Event& event); - /** * Handles a joystick button down event. * @@ -244,7 +243,6 @@ class sdl_event_handler : public events::sdl_handler */ void button_down(const SDL_Event& event); - /** * Fires a key down event. * @@ -297,6 +295,13 @@ class sdl_event_handler : public events::sdl_handler */ void keyboard(const ui_event event); + /** + * Fires a CLOSE_WINDOW event for the window with the given ID. + * + * @param window_id The ID of the window to close. + */ + void close_window(const unsigned window_id); + /** * The dispatchers. * @@ -383,15 +388,9 @@ void sdl_event_handler::handle_event(const SDL_Event& event) execute_timer(reinterpret_cast(event.user.data1)); break; - case CLOSE_WINDOW_EVENT: { - /** @todo Convert this to a proper new style event. */ - DBG_GUI_E << "Firing " << CLOSE_WINDOW << ".\n"; - - window* window = window::window_instance(event.user.code); - if(window) { - window->set_retval(window::AUTO_CLOSE); - } - } break; + case CLOSE_WINDOW_EVENT: + close_window(event.user.code); + break; case SDL_JOYBUTTONDOWN: button_down(event); @@ -788,6 +787,16 @@ void sdl_event_handler::keyboard(const ui_event event) } } +void sdl_event_handler::close_window(const unsigned window_id) +{ + DBG_GUI_E << "Firing " << CLOSE_WINDOW << ".\n"; + + window* window = window::window_instance(window_id); + if(window) { + window->fire(CLOSE_WINDOW, *window); + } +} + /***** manager class. *****/ manager::manager() diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index d6226e576f80..af197a119f6c 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -398,6 +398,8 @@ window::window(CVideo& video, const builder_window::window_resolution* definitio &window::signal_handler_request_placement, this, _2, _3), event::dispatcher::back_pre_child); + connect_signal(std::bind(&window::signal_handler_close_window, this)); + register_hotkey(hotkey::GLOBAL__HELPTIP, std::bind(gui2::helptip)); } @@ -1471,6 +1473,11 @@ void window::signal_handler_request_placement(const event::ui_event event, handled = true; } +void window::signal_handler_close_window() +{ + set_retval(AUTO_CLOSE); +} + // }---------- DEFINITION ---------{ /*WIKI diff --git a/src/gui/widgets/window.hpp b/src/gui/widgets/window.hpp index 45514158f028..2c2861274959 100644 --- a/src/gui/widgets/window.hpp +++ b/src/gui/widgets/window.hpp @@ -788,6 +788,8 @@ class window : public panel, public cursor::setter void signal_handler_request_placement(const event::ui_event event, bool& handled); + void signal_handler_close_window(); + std::function exit_hook_; std::function callback_next_draw_; };