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