The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
32 
33 /**
34  * Helper to implement private functions without modifying the header.
35  *
36  * The class is a helper to avoid recompilation and only has static
37  * functions.
38  */
40 {
41  /**
42  * Initialiazes a button.
43  *
44  * @param window The window that contains the button.
45  * @param button_status The button status to modify.
46  * @param id The id of the button.
47  */
48  static void init_button(twindow& window,
49  tmessage::tbutton_status& button_status,
50  const std::string& id)
51  {
52  button_status.button = find_widget<tbutton>(&window, id, false, true);
53  button_status.button->set_visible(button_status.visible);
54 
55  if(!button_status.caption.empty()) {
56  button_status.button->set_label(button_status.caption);
57  }
58 
59  if(button_status.retval != twindow::NONE) {
60  button_status.button->set_retval(button_status.retval);
61  }
62  }
63 };
64 
66 {
67  // ***** Validate the required buttons ***** ***** ***** *****
72  window, buttons_[right_1], "right_side");
73 
74  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
75  tcontrol& title_widget = find_widget<tlabel>(&window, "title", false);
76  if(!title_.empty()) {
77  title_widget.set_label(title_);
78  } else {
80  }
81 
82  tcontrol& img_widget = find_widget<timage>(&window, "image", false);
83  if(!image_.empty()) {
84  img_widget.set_label(image_);
85  } else {
87  }
88 
89  tcontrol& label = find_widget<tcontrol>(&window, "label", false);
90  label.set_label(message_);
92 
93  // The label might not always be a scroll_label but the capturing
94  // shouldn't hurt.
95  window.keyboard_capture(&label);
96 
97  // Override the user value, to make sure it's set properly.
98  window.set_click_dismiss(auto_close_);
99 }
100 
101 void tmessage::post_show(twindow& /*window*/)
102 {
103  for(auto & button_status : buttons_)
104  {
105  button_status.button = nullptr;
106  }
107 }
108 
110  const std::string& caption)
111 {
112  buttons_[button].caption = caption;
113  if(buttons_[button].button) {
114  buttons_[button].button->set_label(caption);
115  }
116 }
117 
119  const twidget::tvisible::scoped_enum visible)
120 {
121  buttons_[button].visible = visible;
122  if(buttons_[button].button) {
123  buttons_[button].button->set_visible(visible);
124  }
125 }
126 
127 void tmessage::set_button_retval(const tbutton_id button, const int retval)
128 {
129  buttons_[button].retval = retval;
130  if(buttons_[button].button) {
131  buttons_[button].button->set_retval(retval);
132  }
133 }
134 
136  : button(nullptr)
137  , caption()
138  , visible(twidget::tvisible::invisible)
139  , retval(twindow::NONE)
140 {
141 }
142 
143 void show_message(CVideo& video,
144  const std::string& title,
145  const std::string& message,
146  const std::string& button_caption,
147  const bool auto_close,
148  const bool message_use_markup)
149 {
150  tmessage dlg(title, message, auto_close, message_use_markup);
151  dlg.set_button_caption(tmessage::ok, button_caption);
152  dlg.show(video);
153 }
154 
155 int show_message(CVideo& video,
156  const std::string& title,
157  const std::string& message,
158  const tmessage::tbutton_style button_style,
159  bool message_use_markup,
160  bool /*message_title_mode*/)
161 {
162  tmessage dlg(title,
163  message,
164  button_style == tmessage::auto_close,
165  message_use_markup);
166 
167  switch(button_style) {
169  break;
170  case tmessage::ok_button:
172  dlg.set_button_caption(tmessage::ok, _("OK"));
173  break;
176  break;
179  dlg.set_button_caption(tmessage::ok, _("OK"));
180  /* FALL DOWN */
184  break;
187  dlg.set_button_caption(tmessage::ok, _("Yes"));
191  break;
192  }
193 
194  dlg.show(video);
195  return dlg.get_retval();
196 }
197 
199  const std::string& message,
200  bool message_use_markup)
201 {
202  LOG_STREAM(err, lg::general()) << message << '\n';
203  show_message(video,
204  _("Error"),
205  message,
207  message_use_markup);
208 }
209 
210 } // namespace gui2
void show_error_message(CVideo &video, const std::string &message, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:198
int get_retval() const
Definition: dialog.hpp:161
static void init_button(twindow &window, tmessage::tbutton_status &button_status, const std::string &id)
Initialiazes a button.
Definition: message.cpp:48
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
Definition: video.hpp:58
void set_retval(const int retval)
Definition: button.hpp:62
std::vector< tbutton_status > buttons_
Holds a pointer to the buttons.
Definition: message.hpp:149
This file contains the window object, this object is a top level container which has the event manage...
Enables auto close.
Definition: message.hpp:65
REGISTER_DIALOG(label_settings)
Main class to show messages to the user.
Definition: message.hpp:32
std::string title_
The title for the dialog.
Definition: message.hpp:117
virtual void set_label(const t_string &label)
Definition: control.cpp:330
twidget::tvisible::scoped_enum visible
Definition: message.hpp:144
#define LOG_STREAM(level, domain)
Definition: log.hpp:188
Helper to implement private functions without modifying the header.
Definition: message.cpp:39
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
The user set the widget invisible, that means:
Definition: widget.hpp:103
virtual void set_use_markup(bool use_markup)
Definition: control.cpp:342
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
Shows a close button.
Definition: message.hpp:69
This file contains the settings handling of the widget library.
Dialog is closed with no return value, should be rare but eg a message popup can do it...
Definition: window.hpp:120
void set_button_retval(const tbutton_id button, const int retval)
Definition: message.cpp:127
void show_message(CVideo &video, const std::string &title, const std::string &message, const std::string &button_caption, const bool auto_close, const bool message_use_markup)
Shows a message to the user.
Definition: message.cpp:143
Shows an ok and cancel button.
Definition: message.hpp:71
tbutton_style
Selects the style of the buttons to be shown.
Definition: message.hpp:64
std::string message_
The message to show to the user.
Definition: message.hpp:127
bool message_use_markup_
Whether to enable formatting markup for the dialog message.
Definition: message.hpp:136
static size_t id
Ids for the timers.
Definition: timer.cpp:39
log_domain & general()
Definition: log.cpp:103
The user sets the widget visible, that means:
Definition: widget.hpp:79
Shows a cancel button.
Definition: message.hpp:73
logger & err()
Definition: log.cpp:79
bool auto_close_
Does the window need to use click_dismiss when the dialog doesn't need a scrollbar.
Definition: message.hpp:133
void pre_show(twindow &window)
Inherited from tdialog.
Definition: message.cpp:65
Base class for all visible items.
Definition: control.hpp:34
std::string image_
The image which is shown in the dialog.
Definition: message.hpp:124
void post_show(twindow &window)
Inherited from tdialog.
Definition: message.cpp:101
void set_button_visible(const tbutton_id button, const twidget::tvisible::scoped_enum visible)
Definition: message.cpp:118
Base class for all widgets.
Definition: widget.hpp:49
Standard logging facilities (interface).
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
Shows an ok button.
Definition: message.hpp:67
void set_visible(const tvisible::scoped_enum visible)
Definition: widget.cpp:445
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Shows a yes and no button.
Definition: message.hpp:75
void set_button_caption(const tbutton_id button, const std::string &caption)
Definition: message.cpp:109