The Battle for Wesnoth  1.15.2+dev
quit_confirmation.cpp
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 #include "quit_confirmation.hpp"
16 #include "game_end_exceptions.hpp"
17 #include "gettext.hpp"
18 #include "video.hpp"
19 #include "resources.hpp"
20 #include "playmp_controller.hpp"
22 #include "gui/dialogs/message.hpp"
23 #include "gui/widgets/retval.hpp"
24 
25 #include <boost/range/adaptor/reversed.hpp>
26 
27 std::vector<quit_confirmation*> quit_confirmation::blockers_ = std::vector<quit_confirmation*>();
28 bool quit_confirmation::open_ = false;
29 
31 {
32  if(!open_) {
33  open_ = true;
35  {
36  if(!blocker->prompt_()) {
37  open_ = false;
38  return false;
39  }
40  }
41  open_ = false;
42  }
43 
44  return true;
45 }
46 
48 {
49  if(quit()) { throw_quit_game_exception(); }
50 }
51 
53 {
54  if(quit()) { throw CVideo::quit(); }
55 }
56 
57 bool quit_confirmation::show_prompt(const std::string& message)
58 {
59  return gui2::show_message(_("Quit"), message,
61 }
62 
64 {
66  std::size_t humans_notme_cnt = 0;
67 
68  if(pmc != nullptr) {
69  for(const auto& t : pmc->get_teams_const()) {
70  if(t.is_network_human()) {
71  ++humans_notme_cnt;
72  }
73  }
74  }
75 
76  if(!(pmc == nullptr || humans_notme_cnt < 1 || pmc->is_linger_mode() || pmc->is_observer())) {
78  sq.show();
79  int retval = sq.get_retval();
80  if(retval == 1)
81  {
82  pmc->surrender(display::get_singleton()->viewing_team());
83  return true;
84  }
85  else if(retval == 2)
86  {
87  return true;
88  }
89  else
90  {
91  return false;
92  }
93  } else {
94  return show_prompt(_("Do you really want to quit?"));
95  }
96 }
play_controller * controller
Definition: resources.cpp:21
Dialog was closed with the CANCEL button.
Definition: retval.hpp:37
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:152
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:88
void surrender(int side_number)
static void quit_to_title()
static bool show_prompt(const std::string &message)
Type that can be thrown as an exception to quit to desktop.
Definition: video.hpp:228
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
bool show(const unsigned auto_close_time=0)
Shows the window.
-file util.hpp
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
void throw_quit_game_exception()
Implements a quit confirmation dialog.
Shows a yes and no button.
Definition: message.hpp:79
bool is_observer() const
static bool quit()
Shows the quit confirmation if needed.
static void quit_to_desktop()
double t
Definition: astarsearch.cpp:64
static bool default_prompt()
retval
Default window/dialog return values.
Definition: retval.hpp:28
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:193
const std::vector< team > & get_teams_const() const
static std::vector< quit_confirmation * > blockers_