show confirmation dialog when quitting

when quitting (for example by clicking on the red cross in the corner) the
game now asks for a confirmation, if there is currently a (re-)play or
the editor running.
This commit is contained in:
gfgtdf 2015-08-31 21:31:51 +02:00
parent 8db92e9af3
commit 0abde389dc
7 changed files with 58 additions and 2 deletions

View file

@ -1104,6 +1104,7 @@ set(libwesnoth-game_STAT_SRC
preferences.cpp preferences.cpp
preferences_display.cpp preferences_display.cpp
race.cpp race.cpp
quit_confirmation.cpp
reports.cpp reports.cpp
show_dialog.cpp show_dialog.cpp
simple_rng.cpp simple_rng.cpp

View file

@ -117,6 +117,7 @@ libwesnoth_sources = Split("""
pathutils.cpp pathutils.cpp
preferences.cpp preferences.cpp
preferences_display.cpp preferences_display.cpp
quit_confirmation.cpp
race.cpp race.cpp
reports.cpp reports.cpp
show_dialog.cpp show_dialog.cpp

View file

@ -42,6 +42,7 @@
#include "hotkey/hotkey_command.hpp" #include "hotkey/hotkey_command.hpp"
#include "joystick.hpp" #include "joystick.hpp"
#include "key.hpp" #include "key.hpp"
#include "quit_confirmation.hpp"
class CVideo; class CVideo;
class display; class display;
@ -53,7 +54,7 @@ namespace hotkey { class command_executor; }
namespace soundsource { class manager; } namespace soundsource { class manager; }
class controller_base : public events::sdl_handler class controller_base : public events::sdl_handler, quit_confirmation
{ {
public: public:
controller_base(const config& game_config, CVideo& video); controller_base(const config& game_config, CVideo& video);

View file

@ -229,6 +229,8 @@ display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::
, do_reverse_memcpy_workaround_(false) , do_reverse_memcpy_workaround_(false)
#endif #endif
{ {
//The following assertion fails when starting a campaign
//assert(singleton_ == NULL);
singleton_ = this; singleton_ = this;
resources::fake_units = fake_unit_man_.get(); resources::fake_units = fake_unit_man_.get();

View file

@ -19,6 +19,7 @@
#include "events.hpp" #include "events.hpp"
#include "log.hpp" #include "log.hpp"
#include "sound.hpp" #include "sound.hpp"
#include "quit_confirmation.hpp"
#include "video.hpp" #include "video.hpp"
#if defined _WIN32 #if defined _WIN32
#include "desktop/windows_tray_notification.hpp" #include "desktop/windows_tray_notification.hpp"
@ -415,7 +416,7 @@ void pump()
#endif #endif
case SDL_QUIT: { case SDL_QUIT: {
throw CVideo::quit(); quit_confirmation::quit();
} }
} }

35
src/quit_confirmation.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "quit_confirmation.hpp"
#include "gettext.hpp"
#include "display.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/window.hpp"
#include "resources.hpp"
int quit_confirmation::count_ = 0;
bool quit_confirmation::open_ = false;
void quit_confirmation::quit()
{
if(count_ != 0 && display::get_singleton() && !open_)
{
quit(display::get_singleton()->video());
}
else
{
throw CVideo::quit();
}
}
void quit_confirmation::quit(CVideo& video)
{
assert(!open_);
open_ = true;
const int res = gui2::show_message(video, _("Quit"),
_("Do you really want to quit?"), gui2::tmessage::yes_no_buttons);
open_ = false;
if(res != gui2::twindow::CANCEL) {
throw CVideo::quit();
} else {
return;
}
}

15
src/quit_confirmation.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
class CVideo;
class quit_confirmation
{
public:
quit_confirmation() { ++count_; }
quit_confirmation(const quit_confirmation&) { ++count_; }
~quit_confirmation() { --count_; }
static void quit();
static void quit(CVideo& video );
private:
static int count_;
static bool open_;
};