The Battle for Wesnoth  1.17.17+dev
message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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);
96 
97  // The label might not always be a scroll_label but the capturing
98  // shouldn't hurt.
100 
101  // Override the user value, to make sure it's set properly.
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,
165  const message::button_style button_style,
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
Simple push button.
Definition: button.hpp:37
void set_retval(const int retval)
Definition: button.hpp:66
Main class to show messages to the user.
Definition: message.hpp:36
bool message_use_markup_
Whether to enable formatting markup for the dialog message.
Definition: message.hpp:140
std::string title_
The title for the dialog.
Definition: message.hpp:121
std::string message_
The message to show to the user.
Definition: message.hpp:131
button_style
Selects the style of the buttons to be shown.
Definition: message.hpp:70
@ yes_no_buttons
Shows a yes and no button.
Definition: message.hpp:81
@ close_button
Shows a close button.
Definition: message.hpp:75
@ ok_button
Shows an ok button.
Definition: message.hpp:73
@ ok_cancel_buttons
Shows an ok and cancel button.
Definition: message.hpp:77
@ auto_close
Enables auto close.
Definition: message.hpp:71
@ cancel_button
Shows a cancel button.
Definition: message.hpp:79
void set_button_caption(const button_id button, const std::string &caption)
Definition: message.cpp:113
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: message.cpp:68
std::vector< button_status > buttons_
Holds a pointer to the buttons.
Definition: message.hpp:156
std::string image_
The image which is shown in the dialog.
Definition: message.hpp:128
bool title_use_markup_
Whether to enable formatting markup for the dialog title.
Definition: message.hpp:143
void set_button_visible(const button_id button, const widget::visibility visible)
Definition: message.cpp:122
bool auto_close_
Does the window need to use click_dismiss when the dialog doesn't need a scrollbar.
Definition: message.hpp:137
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: message.cpp:105
void set_button_retval(const button_id button, const int retval)
Definition: message.cpp:131
bool show(const unsigned auto_close_time=0)
Shows the window.
int get_retval() const
Returns the cached window exit code.
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:58
Base class for all visible items.
virtual void set_label(const t_string &label)
virtual void set_use_markup(bool use_markup)
Base class for all widgets.
Definition: widget.hpp:54
void set_visible(const visibility visible)
Definition: widget.cpp:456
visibility
Visibility settings done by the user.
Definition: widget.hpp:64
@ visible
The user sets the widget visible, that means:
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void keyboard_capture(widget *widget)
Definition: window.cpp:1130
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:375
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
#define LOG_STREAM(level, domain)
Definition: log.hpp:243
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
Various uncategorised dialogs.
Generic file dialog.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:204
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
retval
Default window/dialog return values.
Definition: retval.hpp:30
@ NONE
Default, unset return value.
Definition: retval.hpp:32
logger & err()
Definition: log.cpp:220
log_domain & general()
Definition: log.cpp:246
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
This file contains the settings handling of the widget library.
Helper to implement private functions without modifying the header.
Definition: message.cpp:43
static void init_button(window &window, message::button_status &button_status, const std::string &id)
Initialiazes a button.
Definition: message.cpp:51