The Battle for Wesnoth  1.15.1+dev
message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/dialogs/message.hpp"
18 
19 #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/settings.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  set_restore(true);
70 
71  // ***** Validate the required buttons ***** ***** ***** *****
76  window, buttons_[right_1], "right_side");
77 
78  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
79  styled_widget& title_widget = find_widget<label>(&window, "title", false);
80  if(!title_.empty()) {
81  title_widget.set_label(title_);
82  title_widget.set_use_markup(title_use_markup_);
83  } else {
85  }
86 
87  styled_widget& img_widget = find_widget<image>(&window, "image", false);
88  if(!image_.empty()) {
89  img_widget.set_label(image_);
90  } else {
92  }
93 
94  styled_widget& label = find_widget<styled_widget>(&window, "label", false);
95  label.set_label(message_);
97 
98  // The label might not always be a scroll_label but the capturing
99  // shouldn't hurt.
100  window.keyboard_capture(&label);
101 
102  // Override the user value, to make sure it's set properly.
103  window.set_click_dismiss(auto_close_);
104 }
105 
106 void message::post_show(window& /*window*/)
107 {
108  for(auto & button_status : buttons_)
109  {
110  button_status.ptr = nullptr;
111  }
112 }
113 
115  const std::string& caption)
116 {
117  buttons_[button].caption = caption;
118  if(buttons_[button].ptr) {
119  buttons_[button].ptr->set_label(caption);
120  }
121 }
122 
124  const widget::visibility visible)
125 {
126  buttons_[button].visible = visible;
127  if(buttons_[button].ptr) {
128  buttons_[button].ptr->set_visible(visible);
129  }
130 }
131 
133 {
134  buttons_[button].retval = retval;
135  if(buttons_[button].ptr) {
136  buttons_[button].ptr->set_retval(retval);
137  }
138 }
139 
141  : ptr(nullptr)
142  , caption()
143  , visible(widget::visibility::invisible)
144  , retval(gui2::retval::NONE) // Needs explicit namespace qualifier to avoid name clashes.
145 {
146 }
147 
148 } // namespace dialogs
149 
150 using namespace dialogs;
151 
152 void show_message(const std::string& title,
153  const std::string& msg,
154  const std::string& button_caption,
155  const bool auto_close,
156  const bool message_use_markup,
157  const bool title_use_markup)
158 {
159  message dlg(title, msg, auto_close, message_use_markup, title_use_markup);
160  dlg.set_button_caption(message::ok, button_caption);
161  dlg.show();
162 }
163 
164 int show_message(const std::string& title,
165  const std::string& msg,
167  bool message_use_markup,
168  bool title_use_markup)
169 {
170  message dlg(title,
171  msg,
172  button_style == message::auto_close,
173  message_use_markup,
174  title_use_markup);
175 
176  switch(button_style) {
177  case message::auto_close:
178  break;
179  case message::ok_button:
181  dlg.set_button_caption(message::ok, _("OK"));
182  break;
185  break;
188  dlg.set_button_caption(message::ok, _("OK"));
189  FALLTHROUGH;
192  break;
195  dlg.set_button_caption(message::ok, _("Yes"));
198  break;
199  }
200 
201  dlg.show();
202  return dlg.get_retval();
203 }
204 
205 void show_error_message(const std::string& msg,
206  bool message_use_markup)
207 {
208  LOG_STREAM(err, lg::general()) << msg << '\n';
209  (void) show_message(
210  _("Error"),
211  msg,
213  message_use_markup);
214 }
215 } // namespace gui2
Helper to implement private functions without modifying the header.
Definition: message.cpp:41
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:152
void set_button_caption(const button_id button, const std::string &caption)
Definition: message.cpp:114
void set_retval(const int retval)
Definition: button.hpp:65
Main class to show messages to the user.
Definition: message.hpp:34
Shows an ok and cancel button.
Definition: message.hpp:75
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:47
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:68
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
bool title_use_markup_
Whether to enable formatting markup for the dialog title.
Definition: message.hpp:143
Label showing a text.
Definition: label.hpp:32
#define LOG_STREAM(level, domain)
Definition: log.hpp:189
bool show(const unsigned auto_close_time=0)
Shows the window.
Shows a close button.
Definition: message.hpp:73
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void set_label(const t_string &label)
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
virtual void post_show(window &window) override
Inherited from modal_dialog.
Definition: message.cpp:106
This file contains the settings handling of the widget library.
void set_visible(const visibility visible)
Definition: widget.cpp:473
Shows an ok button.
Definition: message.hpp:71
std::string image_
The image which is shown in the dialog.
Definition: message.hpp:128
Shows a yes and no button.
Definition: message.hpp:79
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:104
logger & err()
Definition: log.cpp:78
Default, unset return value.
Definition: retval.hpp:31
Shows a cancel button.
Definition: message.hpp:77
The user set the widget invisible, that means:
visibility
Visibility settings done by the user.
Definition: widget.hpp:57
Base class for all visible items.
static void init_button(window &window, message::button_status &button_status, const std::string &id)
Initialiazes a button.
Definition: message.cpp:50
The user sets the widget visible, that means:
void set_button_retval(const button_id button, const int retval)
Definition: message.cpp:132
Simple push button.
Definition: button.hpp:35
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:205
retval
Default window/dialog return values.
Definition: retval.hpp:28
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:123
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: message.cpp:67
Enables auto close.
Definition: message.hpp:69
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
void set_restore(const bool restore)