The Battle for Wesnoth  1.17.8+dev
message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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  // ***** Validate the required buttons ***** ***** ***** *****
75  window, buttons_[right_1], "right_side");
76 
77  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
78  styled_widget& title_widget = find_widget<label>(&window, "title", false);
79  if(!title_.empty()) {
80  title_widget.set_label(title_);
81  title_widget.set_use_markup(title_use_markup_);
82  } else {
84  }
85 
86  styled_widget& img_widget = find_widget<image>(&window, "image", false);
87  if(!image_.empty()) {
88  img_widget.set_label(image_);
89  } else {
91  }
92 
93  styled_widget& label = find_widget<styled_widget>(&window, "label", false);
94  label.set_label(message_);
96 
97  // The label might not always be a scroll_label but the capturing
98  // shouldn't hurt.
99  window.keyboard_capture(&label);
100 
101  // Override the user value, to make sure it's set properly.
102  window.set_click_dismiss(auto_close_);
103 }
104 
105 void message::post_show(window& /*window*/)
106 {
107  for(auto & button_status : buttons_)
108  {
109  button_status.ptr = nullptr;
110  }
111 }
112 
114  const std::string& caption)
115 {
116  buttons_[button].caption = caption;
117  if(buttons_[button].ptr) {
118  buttons_[button].ptr->set_label(caption);
119  }
120 }
121 
123  const widget::visibility visible)
124 {
125  buttons_[button].visible = visible;
126  if(buttons_[button].ptr) {
127  buttons_[button].ptr->set_visible(visible);
128  }
129 }
130 
132 {
133  buttons_[button].retval = retval;
134  if(buttons_[button].ptr) {
135  buttons_[button].ptr->set_retval(retval);
136  }
137 }
138 
140  : ptr(nullptr)
141  , caption()
142  , visible(widget::visibility::invisible)
143  , retval(gui2::retval::NONE) // Needs explicit namespace qualifier to avoid name clashes.
144 {
145 }
146 
147 } // namespace dialogs
148 
149 using namespace dialogs;
150 
151 void show_message(const std::string& title,
152  const std::string& msg,
153  const std::string& button_caption,
154  const bool auto_close,
155  const bool message_use_markup,
156  const bool title_use_markup)
157 {
158  message dlg(title, msg, auto_close, message_use_markup, title_use_markup);
159  dlg.set_button_caption(message::ok, button_caption);
160  dlg.show();
161 }
162 
163 int show_message(const std::string& title,
164  const std::string& msg,
166  bool message_use_markup,
167  bool title_use_markup)
168 {
169  message dlg(title,
170  msg,
171  button_style == message::auto_close,
172  message_use_markup,
173  title_use_markup);
174 
175  switch(button_style) {
176  case message::auto_close:
177  break;
178  case message::ok_button:
180  dlg.set_button_caption(message::ok, _("OK"));
181  break;
184  break;
187  dlg.set_button_caption(message::ok, _("OK"));
188  [[fallthrough]];
191  break;
194  dlg.set_button_caption(message::ok, _("Yes"));
197  break;
198  }
199 
200  dlg.show();
201  return dlg.get_retval();
202 }
203 
204 void show_error_message(const std::string& msg,
205  bool message_use_markup)
206 {
207  LOG_STREAM(err, lg::general()) << msg;
208  (void) show_message(
209  _("Error"),
210  msg,
212  message_use_markup);
213 }
214 } // namespace gui2
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
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:151
void set_button_caption(const button_id button, const std::string &caption)
Definition: message.cpp:113
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:77
std::vector< button_status > buttons_
Holds a pointer to the buttons.
Definition: message.hpp:156
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:140
Base class for all widgets.
Definition: widget.hpp:53
std::string title_
The title for the dialog.
Definition: message.hpp:121
button_style
Selects the style of the buttons to be shown.
Definition: message.hpp:70
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:143
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:238
bool show(const unsigned auto_close_time=0)
Shows the window.
Shows a close button.
Definition: message.hpp:75
Generic file dialog.
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:105
This file contains the settings handling of the widget library.
void set_visible(const visibility visible)
Definition: widget.cpp:456
Shows an ok button.
Definition: message.hpp:73
std::string image_
The image which is shown in the dialog.
Definition: message.hpp:128
Shows a yes and no button.
Definition: message.hpp:81
virtual void set_use_markup(bool use_markup)
std::string message_
The message to show to the user.
Definition: message.hpp:131
Various uncategorised dialogs.
log_domain & general()
Definition: log.cpp:185
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
logger & err()
Definition: log.cpp:159
Default, unset return value.
Definition: retval.hpp:32
Shows a cancel button.
Definition: message.hpp:79
The user set the widget invisible, that means:
visibility
Visibility settings done by the user.
Definition: widget.hpp:63
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:131
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:204
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:137
void set_button_visible(const button_id button, const widget::visibility visible)
Definition: message.cpp:122
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:71
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66