The Battle for Wesnoth  1.17.0-dev
message.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 #pragma once
16 
18 #include "gui/widgets/widget.hpp"
19 
20 namespace gui2
21 {
22 
23 class button;
24 
25 namespace dialogs
26 {
27 
28 /**
29  * Main class to show messages to the user.
30  *
31  * It can be used to show a message or ask a result from the user. For the
32  * most common usage cases there are helper functions defined.
33  */
34 class message : public modal_dialog
35 {
36  friend struct message_implementation;
37 
38 public:
39  message(const std::string& title,
40  const std::string& message,
41  const bool auto_close,
42  const bool message_use_markup,
43  const bool title_use_markup)
44  : title_(title)
45  , image_()
46  , message_(message)
47  , auto_close_(auto_close)
48  , message_use_markup_(message_use_markup)
49  , title_use_markup_(title_use_markup)
50  , buttons_(count)
51  {
52  }
53 
54  enum button_id {
55  left_1 = 0,
57  ok,
60  };
61 
62  /**
63  * Selects the style of the buttons to be shown.
64  *
65  * These values are not directly implemented in this class but are used
66  * by our helper functions.
67  */
68  enum button_style {
69  auto_close /**< Enables auto close. */
70  ,
71  ok_button /**< Shows an ok button. */
72  ,
73  close_button /**< Shows a close button. */
74  ,
75  ok_cancel_buttons /**< Shows an ok and cancel button. */
76  ,
77  cancel_button /**< Shows a cancel button. */
78  ,
79  yes_no_buttons /**< Shows a yes and no button. */
80  };
81 
83  const std::string& caption);
84 
86  const widget::visibility visible);
87 
88  void set_button_retval(const button_id button, const int retval);
89 
90  /***** ***** ***** setters / getters for members ***** ****** *****/
91 
92  void set_title(const std::string& title)
93  {
94  title_ = title;
95  }
96 
97  void set_image(const std::string& image)
98  {
99  image_ = image;
100  }
101 
102  void set_message(const std::string& message)
103  {
104  message_ = message;
105  }
106 
107  void set_auto_close(const bool auto_close)
108  {
110  }
111 
112 protected:
113  virtual void pre_show(window& window) override;
114 
115  virtual void post_show(window& window) override;
116 
117 private:
118  /** The title for the dialog. */
119  std::string title_;
120 
121  /**
122  * The image which is shown in the dialog.
123  *
124  * This image can be an icon or portrait or any other image.
125  */
126  std::string image_;
127 
128  /** The message to show to the user. */
129  std::string message_;
130 
131  /**
132  * Does the window need to use click_dismiss when the dialog doesn't need a
133  * scrollbar.
134  */
136 
137  /** Whether to enable formatting markup for the dialog message. */
139 
140  /** Whether to enable formatting markup for the dialog title. */
142 
144  {
145  button_status();
146 
148  std::string caption;
150  int retval;
151  };
152 
153  /** Holds a pointer to the buttons. */
154  std::vector<button_status> buttons_;
155 
156  virtual const std::string& window_id() const override;
157 };
158 } // namespace dialogs
159 
160 /**
161  * Shows a message to the user.
162  *
163  * Normally the dialog won't have a button only when the text doesn't fit in
164  * the dialog and a scrollbar is used the button will be shown.
165  *
166  * @param title The title of the dialog.
167  * @param message The message to show in the dialog.
168  * @param button_caption The caption of the close button.
169  * @param auto_close When true the window will hide the ok button
170  * when the message doesn't need a scrollbar to
171  * show itself.
172  * @param message_use_markup Use markup for the message?
173  * @param title_use_markup Use markup for the title?
174  */
175 void show_message(const std::string& title,
176  const std::string& message,
177  const std::string& button_caption = "",
178  const bool auto_close = true,
179  const bool message_use_markup = false,
180  const bool title_use_markup = false);
181 
182 /**
183  * Shows a message to the user.
184  *
185  * @note this function is rather untested, and the API might change in the
186  * near future.
187  *
188  * @param title The title of the dialog.
189  * @param message The message to show in the dialog.
190  * @param button_style The style of the button(s) shown.
191  * @param message_use_markup Use markup for the message?
192  * @param title_use_markup Use markup for the title?
193  *
194  * @returns The retval of the dialog shown.
195  */
196 int show_message(const std::string& title,
197  const std::string& message,
199  bool message_use_markup = false,
200  bool title_use_markup = false);
201 
202 /**
203  * Shows an error message to the user.
204  *
205  * @param message The message to show in the dialog.
206  * @param message_use_markup Use markup for the message?
207  */
208 void show_error_message(const std::string& message,
209  bool message_use_markup = false);
210 
211 } // 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_title(const std::string &title)
Definition: message.hpp:92
void set_button_caption(const button_id button, const std::string &caption)
Definition: message.cpp:114
virtual const std::string & window_id() const override
The id of the window to build.
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:154
bool message_use_markup_
Whether to enable formatting markup for the dialog message.
Definition: message.hpp:138
std::string title_
The title for the dialog.
Definition: message.hpp:119
button_style
Selects the style of the buttons to be shown.
Definition: message.hpp:68
bool title_use_markup_
Whether to enable formatting markup for the dialog title.
Definition: message.hpp:141
void set_image(const std::string &image)
Definition: message.hpp:97
Shows a close button.
Definition: message.hpp:73
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: message.cpp:106
Shows an ok button.
Definition: message.hpp:71
std::string image_
The image which is shown in the dialog.
Definition: message.hpp:126
Shows a yes and no button.
Definition: message.hpp:79
std::string message_
The message to show to the user.
Definition: message.hpp:129
Various uncategorised dialogs.
message(const std::string &title, const std::string &message, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Definition: message.hpp:39
Shows a cancel button.
Definition: message.hpp:77
visibility
Visibility settings done by the user.
Definition: widget.hpp:58
void set_auto_close(const bool auto_close)
Definition: message.hpp:107
void set_message(const std::string &message)
Definition: message.hpp:102
void set_button_retval(const button_id button, const int retval)
Definition: message.cpp:132
Abstract base class for all modal dialogs.
Functions to load and save images from/to disk.
Simple push button.
Definition: button.hpp:35
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:135
void set_button_visible(const button_id button, const widget::visibility visible)
Definition: message.cpp:123
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
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:64