The Battle for Wesnoth  1.19.0-dev
wml_message.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
20 namespace gui2::dialogs
21 {
22 
23 /**
24  * Helper class for message options
25  */
27 public:
28  explicit wml_message_option(std::string label, std::string description = "", std::string image = "")
29  : label_(label)
31  , image_(image)
32  {}
33  std::string label() const {return label_;}
34  std::string description() const {return description_;}
35  std::string image() const {return image_;}
36 private:
37  std::string label_, description_, image_;
38 };
39 
40 /**
41  * Base class for the wml generated messages.
42  *
43  * We have a separate sub class for left and right images.
44  */
46 {
47 public:
49  const std::string& window_id,
50  const std::string& title,
51  const std::string& message,
52  const std::string& portrait,
53  const bool mirror)
55  , title_(title)
56  , image_("")
57  , message_(message)
58  , portrait_(portrait)
59  , mirror_(mirror)
60  , has_input_(false)
61  , input_caption_("")
62  , input_text_(nullptr)
64  , option_list_()
65  , chosen_option_(nullptr)
66  {
67  }
68 
69  /**
70  * Sets the input text variables.
71  *
72  * @param caption The caption for the label.
73  * @param [in,out] text The initial text, after showing the final
74  * text.
75  * @param maximum_length The maximum length of the text.
76  */
77  void set_input(const std::string& caption,
78  std::string* text,
79  const unsigned maximum_length);
80  /**
81  * Sets the option list
82  *
83  * @param option_list The list of options to display.
84  * @param [in,out] chosen_option Pointer to the index of the initially
85  * selected option; after showing, the
86  * chosen option.
87  */
88  void set_option_list(const std::vector<wml_message_option>& option_list,
89  int* chosen_option);
90 
91 private:
92  /** The title for the dialog. */
93  std::string title_;
94 
95  /**
96  * The image which is shown in the dialog.
97  *
98  * This image can be an icon or portrait or any other image.
99  */
100  std::string image_;
101 
102  /** The message to show to the user. */
103  std::string message_;
104  /** Filename of the portrait. */
105  std::string portrait_;
106 
107  /** Mirror the portrait? */
108  bool mirror_;
109 
110  /** Do we need to show an input box? */
112 
113  /** The caption to show for the input text. */
114  std::string input_caption_;
115 
116  /** The text input. */
117  std::string* input_text_;
118 
119  /** The maximum length of the input text. */
121 
122  /** The list of options the user can choose. */
123  std::vector<wml_message_option> option_list_;
124 
125  /** The chosen option. */
127 
128 protected:
129  virtual void pre_show(window& window) override;
130 
131 private:
132  virtual void post_show(window& window) override;
133 };
134 
135 /** Shows a dialog with the portrait on the left side. */
137 {
138 public:
139  wml_message_left(const std::string& title,
140  const std::string& message,
141  const std::string& portrait,
142  const bool mirror)
143  : wml_message_base(window_id(), title, message, portrait, mirror)
144  {
145  }
146 
147 private:
148  virtual const std::string& window_id() const override;
149 };
150 
151 /** Shows a dialog with the portrait on the right side. */
153 {
154 public:
155  wml_message_right(const std::string& title,
156  const std::string& message,
157  const std::string& portrait,
158  const bool mirror)
159  : wml_message_base(window_id(), title, message, portrait, mirror)
160  {
161  }
162 
163 private:
164  virtual const std::string& window_id() const override;
165 };
166 
167 /** Shows a dialog with two portraits, one on each side. */
169 {
170 public:
171  wml_message_double(const std::string& title,
172  const std::string& message,
173  const std::string& portrait,
174  const bool mirror,
175  const std::string& second_portrait,
176  const bool second_mirror)
177  : wml_message_left(title, message, portrait, mirror)
178  , second_portrait_(second_portrait)
179  , second_mirror_(second_mirror)
180  {
181  }
182 
183 private:
184  virtual const std::string& window_id() const override;
185 
186  virtual void pre_show(window& window) override;
187 
188  std::string second_portrait_;
189 
191 };
192 
193 /**
194  * Parameter pack for message list input options
195  */
197 {
198  /** A list of options to select in the dialog. */
199  std::vector<wml_message_option> option_list;
200  /**
201  * The initially chosen option.
202  * Will be set to the chosen option when the dialog closes.
203  */
204  mutable int chosen_option;
205 };
206 
207 /**
208  * Parameter pack for message text input options
209  */
211 {
212  /**
213  * The caption for the optional input text box.
214  * If empty, there is no input box.
215  */
216  std::string caption;
217  /**
218  * The initial text value.
219  * Will be set to the result.
220  */
221  mutable std::string text;
222  /** The maximum length of the text. */
223  unsigned maximum_length;
224  /** True when [text_input] appeared in [message] */
226 };
227 
228 /**
229  * Parameter pack for message portrait
230  */
232 {
233  /** Filename of the portrait. */
234  std::string portrait;
235  /** Does the portrait need to be mirrored? */
236  bool mirror;
237 };
238 
239 /**
240  * Helper function to show a portrait.
241  *
242  * @param title The title of the dialog.
243  * @param message The message to show.
244  * @param left Portrait to show on the left.
245  * @param right Portrait to show on the right.
246  * @param options Options to offer.
247  * @param input Info on text input.
248  */
249 int show_wml_message(const std::string& title,
250  const std::string& message,
251  const wml_message_portrait* left,
252  const wml_message_portrait* right,
254  const wml_message_input& input);
255 
256 } // namespace dialogs
Main class to show messages to the user.
Definition: message.hpp:36
Abstract base class for all modal dialogs.
virtual const std::string & window_id() const =0
The ID of the window to build.
Base class for the wml generated messages.
Definition: wml_message.hpp:46
std::string * input_text_
The text input.
std::string portrait_
Filename of the portrait.
std::string message_
The message to show to the user.
void set_input(const std::string &caption, std::string *text, const unsigned maximum_length)
Sets the input text variables.
Definition: wml_message.cpp:29
bool has_input_
Do we need to show an input box?
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
unsigned input_maximum_length_
The maximum length of the input text.
bool mirror_
Mirror the portrait?
std::string image_
The image which is shown in the dialog.
int * chosen_option_
The chosen option.
std::string input_caption_
The caption to show for the input text.
void set_option_list(const std::vector< wml_message_option > &option_list, int *chosen_option)
Sets the option list.
Definition: wml_message.cpp:41
std::string title_
The title for the dialog.
Definition: wml_message.hpp:93
wml_message_base(const std::string &window_id, const std::string &title, const std::string &message, const std::string &portrait, const bool mirror)
Definition: wml_message.hpp:48
std::vector< wml_message_option > option_list_
The list of options the user can choose.
virtual void pre_show(window &window) override
Definition: wml_message.cpp:57
Shows a dialog with two portraits, one on each side.
virtual const std::string & window_id() const override
The ID of the window to build.
wml_message_double(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror, const std::string &second_portrait, const bool second_mirror)
virtual void pre_show(window &window) override
Shows a dialog with the portrait on the left side.
virtual const std::string & window_id() const override
The ID of the window to build.
wml_message_left(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror)
Helper class for message options.
Definition: wml_message.hpp:26
wml_message_option(std::string label, std::string description="", std::string image="")
Definition: wml_message.hpp:28
std::string description() const
Definition: wml_message.hpp:34
Shows a dialog with the portrait on the right side.
virtual const std::string & window_id() const override
The ID of the window to build.
wml_message_right(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror)
A label displays text that can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
int show_wml_message(const std::string &title, const std::string &message, const wml_message_portrait *left, const wml_message_portrait *right, const wml_message_options &options, const wml_message_input &input)
Helper function to show a portrait.
Functions to load and save images from/to disk.
const config & options()
Definition: game.cpp:552
Parameter pack for message text input options.
unsigned maximum_length
The maximum length of the text.
std::string text
The initial text value.
bool text_input_was_specified
True when [text_input] appeared in [message].
std::string caption
The caption for the optional input text box.
Parameter pack for message list input options.
int chosen_option
The initially chosen option.
std::vector< wml_message_option > option_list
A list of options to select in the dialog.
Parameter pack for message portrait.
std::string portrait
Filename of the portrait.
bool mirror
Does the portrait need to be mirrored?