The Battle for Wesnoth  1.19.1+dev
message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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/window.hpp"
26 #include "log.hpp"
27 
28 namespace gui2
29 {
30 namespace dialogs
31 {
32 
33 REGISTER_DIALOG(message)
34 
35 /**
36  * Helper to implement private functions without modifying the header.
37  *
38  * The class is a helper to avoid recompilation and only has static
39  * functions.
40  */
42 {
43  /**
44  * Initialiazes a button.
45  *
46  * @param window The window that contains the button.
47  * @param button_status The button status to modify.
48  * @param id The id of the button.
49  */
50  static void init_button(window& window,
51  message::button_status& button_status,
52  const std::string& id)
53  {
54  button_status.ptr = find_widget<button>(&window, id, false, true);
55  button_status.ptr->set_visible(button_status.visible);
56 
57  if(!button_status.caption.empty()) {
58  button_status.ptr->set_label(button_status.caption);
59  }
60 
61  if(button_status.retval != retval::NONE) {
62  button_status.ptr->set_retval(button_status.retval);
63  }
64  }
65 };
66 
68 {
69  // ***** Validate the required buttons ***** ***** ***** *****
74  window, buttons_[right_1], "right_side");
75 
76  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
77  styled_widget& title_widget = find_widget<label>(&window, "title", false);
78  if(!title_.empty()) {
79  title_widget.set_label(title_);
80  title_widget.set_use_markup(title_use_markup_);
81  } else {
83  }
84 
85  styled_widget& img_widget = find_widget<image>(&window, "image", false);
86  if(!image_.empty()) {
87  img_widget.set_label(image_);
88  } else {
90  }
91 
92  styled_widget& label = find_widget<styled_widget>(&window, "label", false);
95 
96  // The label might not always be a scroll_label but the capturing
97  // shouldn't hurt.
99 
100  // Override the user value, to make sure it's set properly.
102 }
103 
104 void message::post_show(window& /*window*/)
105 {
106  for(auto & button_status : buttons_)
107  {
108  button_status.ptr = nullptr;
109  }
110 }
111 
113  const std::string& caption)
114 {
115  buttons_[button].caption = caption;
116  if(buttons_[button].ptr) {
117  buttons_[button].ptr->set_label(caption);
118  }
119 }
120 
122  const widget::visibility visible)
123 {
124  buttons_[button].visible = visible;
125  if(buttons_[button].ptr) {
126  buttons_[button].ptr->set_visible(visible);
127  }
128 }
129 
131 {
132  buttons_[button].retval = retval;
133  if(buttons_[button].ptr) {
134  buttons_[button].ptr->set_retval(retval);
135  }
136 }
137 
139  : ptr(nullptr)
140  , caption()
141  , visible(widget::visibility::invisible)
142  , retval(gui2::retval::NONE) // Needs explicit namespace qualifier to avoid name clashes.
143 {
144 }
145 
146 } // namespace dialogs
147 
148 using namespace dialogs;
149 
150 void show_message(const std::string& title,
151  const std::string& msg,
152  const std::string& button_caption,
153  const bool auto_close,
154  const bool message_use_markup,
155  const bool title_use_markup)
156 {
157  message dlg(title, msg, auto_close, message_use_markup, title_use_markup);
158  dlg.set_button_caption(message::ok, button_caption);
159  dlg.show();
160 }
161 
162 int show_message(const std::string& title,
163  const std::string& msg,
164  const message::button_style button_style,
165  bool message_use_markup,
166  bool title_use_markup)
167 {
168  message dlg(title,
169  msg,
170  button_style == message::auto_close,
171  message_use_markup,
172  title_use_markup);
173 
174  switch(button_style) {
175  case message::auto_close:
176  break;
177  case message::ok_button:
179  dlg.set_button_caption(message::ok, _("OK"));
180  break;
183  break;
186  dlg.set_button_caption(message::ok, _("OK"));
187  [[fallthrough]];
190  break;
193  dlg.set_button_caption(message::ok, _("Yes"));
196  break;
197  }
198 
199  dlg.show();
200  return dlg.get_retval();
201 }
202 
203 void show_error_message(const std::string& msg,
204  bool message_use_markup)
205 {
206  LOG_STREAM(err, lg::general()) << msg;
207  (void) show_message(
208  _("Error"),
209  msg,
211  message_use_markup);
212 }
213 } // namespace gui2
Simple push button.
Definition: button.hpp:36
void set_retval(const int retval)
Definition: button.hpp:67
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:112
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: message.cpp:67
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:121
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:104
void set_button_retval(const button_id button, const int retval)
Definition: message.cpp:130
bool show(const unsigned auto_close_time=0)
Shows the window.
int get_retval() const
Returns the cached window exit code.
virtual void set_label(const t_string &text)
virtual void set_use_markup(bool use_markup)
Base class for all widgets.
Definition: widget.hpp:53
void set_visible(const visibility visible)
Definition: widget.cpp:469
visibility
Visibility settings done by the user.
Definition: widget.hpp:63
@ 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:61
void keyboard_capture(widget *widget)
Definition: window.cpp:1221
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:414
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:277
Various uncategorised dialogs.
REGISTER_DIALOG(editor_edit_unit)
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:203
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:150
retval
Default window/dialog return values.
Definition: retval.hpp:30
@ NONE
Default, unset return value.
Definition: retval.hpp:32
logger & err()
Definition: log.cpp:302
log_domain & general()
Definition: log.cpp:328
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
Helper to implement private functions without modifying the header.
Definition: message.cpp:42
static void init_button(window &window, message::button_status &button_status, const std::string &id)
Initialiazes a button.
Definition: message.cpp:50