The Battle for Wesnoth  1.19.5+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"
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/image.hpp"
23 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "log.hpp"
26 
27 namespace gui2
28 {
29 namespace dialogs
30 {
31 
32 REGISTER_DIALOG(message)
33 
34 /**
35  * Helper to implement private functions without modifying the header.
36  *
37  * The class is a helper to avoid recompilation and only has static
38  * functions.
39  */
41 {
42  /**
43  * Initialiazes a button.
44  *
45  * @param window The window that contains the button.
46  * @param button_status The button status to modify.
47  * @param id The id of the button.
48  */
49  static void init_button(window& window,
50  message::button_status& button_status,
51  const std::string& id)
52  {
53  button_status.ptr = window.find_widget<button>(id, false, true);
54  button_status.ptr->set_visible(button_status.visible);
55 
56  if(!button_status.caption.empty()) {
57  button_status.ptr->set_label(button_status.caption);
58  }
59 
60  if(button_status.retval != retval::NONE) {
61  button_status.ptr->set_retval(button_status.retval);
62  }
63  }
64 };
65 
67 {
68  // ***** Validate the required buttons ***** ***** ***** *****
73 
74  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
75  styled_widget& title_widget = find_widget<label>("title");
76  if(!title_.empty()) {
77  title_widget.set_label(title_);
78  title_widget.set_use_markup(title_use_markup_);
79  } else {
81  }
82 
83  styled_widget& img_widget = find_widget<image>("image");
84  if(!image_.empty()) {
85  img_widget.set_label(image_);
86  } else {
88  }
89 
90  styled_widget& label = find_widget<styled_widget>("label");
93 
94  // The label might not always be a scroll_label but the capturing
95  // shouldn't hurt.
97 
98  // Override the user value, to make sure it's set properly.
100 }
101 
103 {
104  for(auto & button_status : buttons_)
105  {
106  button_status.ptr = nullptr;
107  }
108 }
109 
111  const std::string& caption)
112 {
113  buttons_[button].caption = caption;
114  if(buttons_[button].ptr) {
115  buttons_[button].ptr->set_label(caption);
116  }
117 }
118 
120  const widget::visibility visible)
121 {
122  buttons_[button].visible = visible;
123  if(buttons_[button].ptr) {
124  buttons_[button].ptr->set_visible(visible);
125  }
126 }
127 
129 {
130  buttons_[button].retval = retval;
131  if(buttons_[button].ptr) {
132  buttons_[button].ptr->set_retval(retval);
133  }
134 }
135 
137  : ptr(nullptr)
138  , caption()
139  , visible(widget::visibility::invisible)
140  , retval(gui2::retval::NONE) // Needs explicit namespace qualifier to avoid name clashes.
141 {
142 }
143 
144 } // namespace dialogs
145 
146 using namespace dialogs;
147 
148 void show_message(const std::string& title,
149  const std::string& msg,
150  const std::string& button_caption,
151  const bool auto_close,
152  const bool message_use_markup,
153  const bool title_use_markup)
154 {
155  message dlg(title, msg, auto_close, message_use_markup, title_use_markup);
156  dlg.set_button_caption(message::ok, button_caption);
157  dlg.show();
158 }
159 
160 int show_message(const std::string& title,
161  const std::string& msg,
162  const message::button_style button_style,
163  bool message_use_markup,
164  bool title_use_markup)
165 {
166  message dlg(title,
167  msg,
168  button_style == message::auto_close,
169  message_use_markup,
170  title_use_markup);
171 
172  switch(button_style) {
173  case message::auto_close:
174  break;
175  case message::ok_button:
177  dlg.set_button_caption(message::ok, _("OK"));
178  break;
181  break;
184  dlg.set_button_caption(message::ok, _("OK"));
185  [[fallthrough]];
188  break;
191  dlg.set_button_caption(message::ok, _("Yes"));
194  break;
195  }
196 
197  dlg.show();
198  return dlg.get_retval();
199 }
200 
201 void show_error_message(const std::string& msg,
202  bool message_use_markup)
203 {
204  LOG_STREAM(err, lg::general()) << msg;
205  (void) show_message(
206  _("Error"),
207  msg,
209  message_use_markup);
210 }
211 } // 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
virtual void post_show() override
Actions to be taken after the window has been shown.
Definition: message.cpp:102
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:110
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:119
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 pre_show() override
Actions to be taken before showing the window.
Definition: message.cpp:66
void set_button_retval(const button_id button, const int retval)
Definition: message.cpp:128
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:55
NOT_DANGLING T * find_widget(const std::string &id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: widget.hpp:742
void set_visible(const visibility visible)
Definition: widget.cpp:479
visibility
Visibility settings done by the user.
Definition: widget.hpp:65
@ 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:1207
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:412
static std::string _(const char *str)
Definition: gettext.hpp:93
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:198
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:281
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:201
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:148
retval
Default window/dialog return values.
Definition: retval.hpp:30
@ NONE
Default, unset return value.
Definition: retval.hpp:32
logger & err()
Definition: log.cpp:307
log_domain & general()
Definition: log.cpp:333
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:41
static void init_button(window &window, message::button_status &button_status, const std::string &id)
Initialiazes a button.
Definition: message.cpp:49