The Battle for Wesnoth  1.15.2+dev
message.hpp
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 #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  /** Inherited from modal_dialog. */
114  virtual void pre_show(window& window) override;
115 
116  /** Inherited from modal_dialog. */
117  virtual void post_show(window& window) override;
118 
119 private:
120  /** The title for the dialog. */
121  std::string title_;
122 
123  /**
124  * The image which is shown in the dialog.
125  *
126  * This image can be an icon or portrait or any other image.
127  */
128  std::string image_;
129 
130  /** The message to show to the user. */
131  std::string message_;
132 
133  /**
134  * Does the window need to use click_dismiss when the dialog doesn't need a
135  * scrollbar.
136  */
138 
139  /** Whether to enable formatting markup for the dialog message. */
141 
142  /** Whether to enable formatting markup for the dialog title. */
144 
146  {
147  button_status();
148 
150  std::string caption;
152  int retval;
153  };
154 
155  /** Holds a pointer to the buttons. */
156  std::vector<button_status> buttons_;
157 
158  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
159  virtual const std::string& window_id() const override;
160 };
161 } // namespace dialogs
162 
163 /**
164  * Shows a message to the user.
165  *
166  * Normally the dialog won't have a button only when the text doesn't fit in
167  * the dialog and a scrollbar is used the button will be shown.
168  *
169  * @param title The title of the dialog.
170  * @param message The message to show in the dialog.
171  * @param button_caption The caption of the close button.
172  * @param auto_close When true the window will hide the ok button
173  * when the message doesn't need a scrollbar to
174  * show itself.
175  * @param message_use_markup Use markup for the message?
176  * @param title_use_markup Use markup for the title?
177  */
178 void show_message(const std::string& title,
179  const std::string& message,
180  const std::string& button_caption = "",
181  const bool auto_close = true,
182  const bool message_use_markup = false,
183  const bool title_use_markup = false);
184 
185 /**
186  * Shows a message to the user.
187  *
188  * @note this function is rather untested, and the API might change in the
189  * near future.
190  *
191  * @param title The title of the dialog.
192  * @param message The message to show in the dialog.
193  * @param button_style The style of the button(s) shown.
194  * @param message_use_markup Use markup for the message?
195  * @param title_use_markup Use markup for the title?
196  *
197  * @returns The retval of the dialog shown.
198  */
199 int show_message(const std::string& title,
200  const std::string& message,
202  bool message_use_markup = false,
203  bool title_use_markup = false);
204 
205 /**
206  * Shows an error message to the user.
207  *
208  * @param message The message to show in the dialog.
209  * @param message_use_markup Use markup for the message?
210  */
211 void show_error_message(const std::string& message,
212  bool message_use_markup = false);
213 
214 } // 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
Inherited from modal_dialog, implemented by REGISTER_DIALOG.
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
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
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:143
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
Inherited from modal_dialog.
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:128
Shows a yes and no button.
Definition: message.hpp:79
std::string message_
The message to show to the user.
Definition: message.hpp:131
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:57
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.
this module manages the cache of images.
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: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:62