The Battle for Wesnoth  1.15.1+dev
quit_confirmation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2018 by the Battle for Wesnoth Project
3  <https://www.wesnoth.org/>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 class CVideo;
18 
19 #include <cassert>
20 #include <vector>
21 #include <string>
22 
23 #include "utils/functional.hpp"
24 
25 /**
26  * Implements a quit confirmation dialog.
27  *
28  * Any object of this type will prevent the game from quitting immediately.
29  * Instead, a confirmation dialog will pop up when attempting to close.
30  */
32 {
33 public:
34  explicit quit_confirmation(const std::function<bool()>& prompt = &quit_confirmation::default_prompt)
35  : prompt_(prompt) { blockers_.push_back(this); }
36 
37  ~quit_confirmation() { blockers_.pop_back(); }
38 
39  /**
40  * Shows the quit confirmation if needed.
41  *
42  * @throws CVideo::quit If the user chooses to quit or no prompt was
43  * displayed.
44  */
45  static bool quit();
46  static void quit_to_title();
47  static void quit_to_desktop();
48 
49  static bool show_prompt(const std::string& message);
50  static bool default_prompt();
51 
52 private:
53  // noncopyable
54  quit_confirmation(const quit_confirmation&) = delete;
55  const quit_confirmation& operator=(const quit_confirmation&) = delete;
56  static std::vector<quit_confirmation*> blockers_;
57  static bool open_;
58 
59  std::function<bool()> prompt_;
60 };
static void quit_to_title()
Definition: video.hpp:31
static bool show_prompt(const std::string &message)
Implements a quit confirmation dialog.
quit_confirmation(const std::function< bool()> &prompt=&quit_confirmation::default_prompt)
const quit_confirmation & operator=(const quit_confirmation &)=delete
static bool quit()
Shows the quit confirmation if needed.
static void quit_to_desktop()
static bool default_prompt()
std::function< bool()> prompt_
static std::vector< quit_confirmation * > blockers_