Skip to content

Commit

Permalink
GUI2/Window: handle CLOSE_WINDOW event with a signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Aug 25, 2017
1 parent ca5ac9b commit a4dab46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/gui/core/event/handler.cpp
Expand Up @@ -236,15 +236,13 @@ class sdl_event_handler : public events::sdl_handler
*/
void hat_motion(const SDL_Event& event);


/**
* Handles a joystick button down event.
*
* @param event The SDL joystick button event triggered.
*/
void button_down(const SDL_Event& event);


/**
* Fires a key down event.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -383,15 +388,9 @@ void sdl_event_handler::handle_event(const SDL_Event& event)
execute_timer(reinterpret_cast<size_t>(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);
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions src/gui/widgets/window.cpp
Expand Up @@ -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<event::CLOSE_WINDOW>(std::bind(&window::signal_handler_close_window, this));

register_hotkey(hotkey::GLOBAL__HELPTIP, std::bind(gui2::helptip));
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/gui/widgets/window.hpp
Expand Up @@ -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<bool(window&)> exit_hook_;
std::function<void()> callback_next_draw_;
};
Expand Down

0 comments on commit a4dab46

Please sign in to comment.