GUI2/Window: implement dialog exit hooks

These differ from post_show events, which are part of tdialog and show after the window has closed.
Nothing in post_show can stop the dialog closing. This new exit hook functionality allows setting of
functions that fire before a window attempts to close by any method, and can halt the order.

This is much simpler than attempting to hook into every closure source on a per-dialog basis.
This commit is contained in:
Charles Dang 2016-09-08 10:27:45 +11:00
parent 67f3113808
commit d858c6b739
2 changed files with 31 additions and 1 deletions

View file

@ -671,7 +671,7 @@ int twindow::show(const bool restore, const unsigned auto_close_timeout)
{
// Start our loop drawing will happen here as well.
bool mouse_button_state_initialised = false;
for(status_ = SHOWING; status_ != REQUEST_CLOSE;) {
for(status_ = SHOWING; status_ != CLOSED;) {
// process installed callback if valid, to allow e.g. network
// polling
events::pump();
@ -691,6 +691,10 @@ int twindow::show(const bool restore, const unsigned auto_close_timeout)
mouse_button_state_initialised = true;
}
if(status_ == REQUEST_CLOSE) {
status_ = exit_hook_(*this) ? CLOSED : SHOWING;
}
// Add a delay so we don't keep spinning if there's no event.
SDL_Delay(10);
}

View file

@ -465,6 +465,30 @@ public:
return tpoint(-1, -1);
}
}
/**
* Sets the window's exit hook.
*
* A window will only close if this function returns true.
*
* @param window The current window.
*/
void set_exit_hook(std::function<bool(twindow&)> func)
{
exit_hook_ = func;
}
void set_exit_hook_ok_only(std::function<bool(twindow&)> func)
{
exit_hook_ = [func](twindow& w)->bool {
if(w.get_retval() == OK) {
return func(w);
}
return true;
};
}
private:
/** Needed so we can change what's drawn on the screen. */
CVideo& video_;
@ -764,6 +788,8 @@ private:
void signal_handler_request_placement(const event::tevent event,
bool& handled);
std::function<bool(twindow&)> exit_hook_ = [](twindow&)->bool { return true; };
};
// }---------- DEFINITION ---------{