The Battle for Wesnoth  1.17.0-dev
message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/dialogs/message.hpp"
19 
20 #include "gettext.hpp"
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/image.hpp"
24 #include "gui/widgets/label.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "log.hpp"
28 
29 namespace gui2
30 {
31 namespace dialogs
32 {
33 
34 REGISTER_DIALOG(message)
35 
36 /**
37  * Helper to implement private functions without modifying the header.
38  *
39  * The class is a helper to avoid recompilation and only has static
40  * functions.
41  */
43 {
44  /**
45  * Initialiazes a button.
46  *
47  * @param window The window that contains the button.
48  * @param button_status The button status to modify.
49  * @param id The id of the button.
50  */
51  static void init_button(window& window,
52  message::button_status& button_status,
53  const std::string& id)
54  {
55  button_status.ptr = find_widget<button>(&window, id, false, true);
56  button_status.ptr->set_visible(button_status.visible);
57 
58  if(!button_status.caption.empty()) {
59  button_status.ptr->set_label(button_status.caption);
60  }
61 
62  if(button_status.retval != retval::NONE) {
63  button_status.ptr->set_retval(button_status.retval);
64  }
65  }
66 };
67 
69 {
70  set_restore(true);
71 
72  // ***** Validate the required buttons ***** ***** ***** *****
77  window, buttons_[right_1], "right_side");
78 
79  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
80  styled_widget& title_widget = find_widget<label>(&window, "title", false);
81  if(!title_.empty()) {
82  title_widget.set_label(title_);
83  title_widget.set_use_markup(title_use_markup_);
84  } else {
86  }
87 
88  styled_widget& img_widget = find_widget<image>(&window, "image", false);
89  if(!image_.empty()) {
90  img_widget.set_label(image_);
91  } else {
93  }
94 
95  styled_widget& label = find_widget<styled_widget>(&window, "label", false);
96  label.set_label(message_);
98 
99  // The label might not always be a scroll_label but the capturing
100  // shouldn't hurt.
101  window.keyboard_capture(&label);
102 
103  // Override the user value, to make sure it's set properly.
104  window.set_click_dismiss(auto_close_);
105 }
106 
107 void message::post_show(window& /*window*/)
108 {
109  for(auto & button_status : buttons_)
110  {
111  button_status.ptr = nullptr;
112  }
113 }
114 
116  const std::string& caption)
117 {
118  buttons_[button].caption = caption;
119  if(buttons_[button].ptr) {
120  buttons_[button].ptr->set_label(caption);
121  }
122 }
123 
125  const widget::visibility visible)
126 {
127  buttons_[button].visible = visible;
128  if(buttons_[button].ptr) {
129  buttons_[button].ptr->set_visible(visible);
130  }
131 }
132 
134 {
135  buttons_[button].retval = retval;
136  if(buttons_[button].ptr) {
137  buttons_[button].ptr->set_retval(retval);
138  }
139 }
140 
142  : ptr(nullptr)
143  , caption()
144  , visible(widget::visibility::invisible)
145  , retval(gui2::retval::NONE) // Needs explicit namespace qualifier to avoid name clashes.
146 {
147 }
148 
149 } // namespace dialogs
150 
151 using namespace dialogs;
152 
153 void show_message(const std::string& title,
154  const std::string& msg,
155  const std::string& button_caption,
156  const bool auto_close,
157  const bool message_use_markup,
158  const bool title_use_markup)
159 {
160  message dlg(title, msg, auto_close, message_use_markup, title_use_markup);
161  dlg.set_button_caption(message::ok, button_caption);
162  dlg.show();
163 }
164 
165 int show_message(const std::string& title,
166  const std::string& msg,
168  bool message_use_markup,
169  bool title_use_markup)
170 {
171  message dlg(title,
172  msg,
173  button_style == message::auto_close,
174  message_use_markup,
175  title_use_markup);
176 
177  switch(button_style) {
178  case message::auto_close:
179  break;
180  case message::ok_button:
182  dlg.set_button_caption(message::ok, _("OK"));
183  break;
186  break;
189  dlg.set_button_caption(message::ok, _("OK"));
190  [[fallthrough]];
193  break;
196  dlg.set_button_caption(message::ok, _("Yes"));
199  break;
200  }
201 
202  dlg.show();
203  return dlg.get_retval();
204 }
205 
206 void show_error_message(const std::string& msg,
207  bool message_use_markup)
208 {
209  LOG_STREAM(err, lg::general()) << msg << '\n';
210  (void) show_message(
211  _("Error"),
212  msg,
214  message_use_markup);
215 }
216 } // namespace gui2
Helper to implement private functions without modifying the header.
Definition: message.cpp:42
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:153
void set_button_caption(const button_id button, const std::string &caption)
Definition: message.cpp:115
void set_retval(const int retval)
Definition: button.hpp:66
Main class to show messages to the user.
Definition: message.hpp:35
Shows an ok and cancel button.
Definition: message.hpp:76
std::vector< button_status > buttons_
Holds a pointer to the buttons.
Definition: message.hpp:155
This file contains the window object, this object is a top level container which has the event manage...
bool message_use_markup_
Whether to enable formatting markup for the dialog message.
Definition: message.hpp:139
Base class for all widgets.
Definition: widget.hpp:49
std::string title_
The title for the dialog.
Definition: message.hpp:120
button_style
Selects the style of the buttons to be shown.
Definition: message.hpp:69
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
bool title_use_markup_
Whether to enable formatting markup for the dialog title.
Definition: message.hpp:142
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:57
static std::string _(const char *str)
Definition: gettext.hpp:93
#define LOG_STREAM(level, domain)
Definition: log.hpp:221
bool show(const unsigned auto_close_time=0)
Shows the window.
Shows a close button.
Definition: message.hpp:74
Generic file dialog.
Definition: field-fwd.hpp:23
virtual void set_label(const t_string &label)
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: message.cpp:107
This file contains the settings handling of the widget library.
void set_visible(const visibility visible)
Definition: widget.cpp:476
Shows an ok button.
Definition: message.hpp:72
std::string image_
The image which is shown in the dialog.
Definition: message.hpp:127
Shows a yes and no button.
Definition: message.hpp:80
virtual void set_use_markup(bool use_markup)
std::string message_
The message to show to the user.
Definition: message.hpp:130
Various uncategorised dialogs.
log_domain & general()
Definition: log.cpp:103
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
logger & err()
Definition: log.cpp:77
Default, unset return value.
Definition: retval.hpp:32
Shows a cancel button.
Definition: message.hpp:78
The user set the widget invisible, that means:
visibility
Visibility settings done by the user.
Definition: widget.hpp:59
Base class for all visible items.
int get_retval() const
Returns the cached window exit code.
static void init_button(window &window, message::button_status &button_status, const std::string &id)
Initialiazes a button.
Definition: message.cpp:51
The user sets the widget visible, that means:
void set_button_retval(const button_id button, const int retval)
Definition: message.cpp:133
Simple push button.
Definition: button.hpp:36
Standard logging facilities (interface).
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:206
retval
Default window/dialog return values.
Definition: retval.hpp:29
bool auto_close_
Does the window need to use click_dismiss when the dialog doesn&#39;t need a scrollbar.
Definition: message.hpp:136
void set_button_visible(const button_id button, const widget::visibility visible)
Definition: message.cpp:124
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: message.cpp:68
Enables auto close.
Definition: message.hpp:70
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
void set_restore(const bool restore)