The Battle for Wesnoth  1.15.12+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 #include "utils/ranges.hpp"
25 
27 {
28  if(!open_) {
29  open_ = true;
31  {
32  if(!blocker->prompt_()) {
33  open_ = false;
34  return false;
35  }
36  }
37  open_ = false;
38  }
39 
40  return true;
41 }
42 
44 {
45  if(quit()) { throw_quit_game_exception(); }
46 }
47 
49 {
50  if(quit()) { throw CVideo::quit(); }
51 }
52 
53 bool quit_confirmation::show_prompt(const std::string& message)
54 {
55  return gui2::show_message(_("Quit"), message,
57 }
58 
60 {
62  std::size_t humans_notme_cnt = 0;
63 
64  if(pmc != nullptr) {
65  for(const auto& t : pmc->get_teams()) {
66  if(t.is_network_human()) {
67  ++humans_notme_cnt;
68  }
69  }
70  }
71 
72  if(!(pmc == nullptr || humans_notme_cnt < 1 || pmc->is_linger_mode() || pmc->is_observer())) {
74  sq.show();
75  int retval = sq.get_retval();
76  if(retval == 1)
77  {
78  pmc->surrender(display::get_singleton()->viewing_team());
79  return true;
80  }
81  else if(retval == 2)
82  {
83  return true;
84  }
85  else
86  {
87  return false;
88  }
89  } else {
90  return show_prompt(_("Do you really want to quit?"));
91  }
92 }
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:91
void surrender(int side_number)
static void quit_to_title()
auto reversed_view(T &container)
Definition: ranges.hpp:27
static bool show_prompt(const std::string &message)
Type that can be thrown as an exception to quit to desktop.
Definition: video.hpp:245
static std::vector< quit_confirmation * > blockers_
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
static std::string _(const char *str)
Definition: gettext.hpp:92
bool show(const unsigned auto_close_time=0)
Shows the window.
void throw_quit_game_exception()
Implements a quit confirmation dialog.
std::vector< team > & get_teams()
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()
This shows the dialog to confirm surrender and/or quitting the game.
int get_retval() const
Returns the cached window exit code.
double t
Definition: astarsearch.cpp:64
static bool default_prompt()
retval
Default window/dialog return values.
Definition: retval.hpp:28