The Battle for Wesnoth  1.19.7+dev
wml_message.cpp
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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/widgets/label.hpp"
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/text_box.hpp"
23 #include "gui/widgets/window.hpp"
24 
25 namespace gui2::dialogs
26 {
27 
29  const std::string& caption,
30  std::string* text,
31  const unsigned maximum_length)
32 {
33  assert(text);
34 
35  has_input_ = true;
36  input_caption_ = caption;
37  input_text_ = text;
38  input_maximum_length_ = maximum_length;
39 }
40 
42  const std::vector<wml_message_option>& option_list,
43  int* chosen_option)
44 {
45  assert(!option_list.empty());
46  assert(chosen_option);
47 
48  option_list_ = option_list;
49  chosen_option_ = chosen_option;
50 }
51 
52 /**
53  * @todo This function enables the wml markup for all items, but the interface
54  * is a bit hacky. Especially the fiddling in the internals of the listbox is
55  * ugly. There needs to be a clean interface to set whether a widget has a
56  * markup and what kind of markup. These fixes will be post 1.6.
57  */
59 {
60  get_canvas(1).set_variable("portrait_image", wfl::variant(portrait_));
61  get_canvas(1).set_variable("portrait_mirror", wfl::variant(mirror_));
62 
63  // Set the markup
64  label& title = find_widget<label>("title");
65  title.set_label(title_);
66  title.set_use_markup(true);
67  title.set_can_wrap(true);
68 
69  styled_widget& message = find_widget<styled_widget>("message");
71  message.set_use_markup(true);
72  // The message label might not always be a scroll_label but the capturing
73  // shouldn't hurt.
75 
76  // Find the input box related fields.
77  label& caption = find_widget<label>("input_caption");
78  text_box& input = find_widget<text_box>("input", true);
79 
80  if(has_input_) {
81  caption.set_label(input_caption_);
82  caption.set_use_markup(true);
83  input.set_value(*input_text_);
85  keyboard_capture(&input);
86  set_click_dismiss(false);
87  set_escape_disabled(true);
88  } else {
91  }
92 
93  // Find the option list related fields.
94  listbox& options = find_widget<listbox>("input_list", true);
95 
96  if(!option_list_.empty()) {
98  for(const wml_message_option& item : option_list_) {
99  // Add the data.
100  data["icon"]["label"] = item.image();
101  data["label"]["label"] = item.label();
102  data["label"]["use_markup"] = "true";
103  data["description"]["label"] = item.description();
104  data["description"]["use_markup"] = "true";
105  options.add_row(data);
106  }
107 
108  // Avoid negative and 0 since item 0 is already selected.
109  if(*chosen_option_ > 0 && static_cast<std::size_t>(*chosen_option_)
110  < option_list_.size()) {
111 
112  options.select_row(*chosen_option_);
113  }
114 
115  if(!has_input_) {
116  keyboard_capture(&options);
117  set_click_dismiss(false);
118  set_escape_disabled(true);
119  } else {
120  add_to_keyboard_chain(&options);
121  // click_dismiss has been disabled due to the input.
122  }
123  } else {
125  }
127 }
128 
130 {
131  if(has_input_) {
132  *input_text_
133  = find_widget<text_box>("input", true).get_value();
134  }
135 
136  if(!option_list_.empty()) {
137  *chosen_option_ = find_widget<listbox>("input_list", true)
138  .get_selected_row();
139  }
140 }
141 
143 {
145  get_canvas(1).set_variable("second_portrait_image", wfl::variant(second_portrait_));
146  get_canvas(1).set_variable("second_portrait_mirror", wfl::variant(second_mirror_));
147 }
148 
150 
152 
154 
155 int show_wml_message(const std::string& title,
156  const std::string& message,
157  const wml_message_portrait* left,
158  const wml_message_portrait* right,
159  const wml_message_options& options,
160  const wml_message_input& input)
161 {
162  std::shared_ptr<wml_message_base> dlg;
163  if(left && !right) {
164  dlg.reset(new wml_message_left(title, message, left->portrait, left->mirror));
165  } else if(!left && right) {
166  dlg.reset(new wml_message_right(title, message, right->portrait, right->mirror));
167  } else if(right && left) {
168  dlg.reset(new wml_message_double(title, message, left->portrait, left->mirror, right->portrait, right->mirror));
169  }
170  assert(dlg.get());
171 
172  if(input.text_input_was_specified) {
173  dlg->set_input(input.caption, &input.text, input.maximum_length);
174  }
175 
176  if(!options.option_list.empty()) {
177  dlg->set_option_list(options.option_list, &options.chosen_option);
178  }
179 
180  dlg->show();
181  return dlg->get_retval();
182 }
183 
184 } // namespace dialogs
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:154
Main class to show messages to the user.
Definition: message.hpp:36
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:28
virtual void pre_show() override
Definition: wml_message.cpp:58
bool has_input_
Do we need to show an input box?
unsigned input_maximum_length_
The maximum length of the input text.
bool mirror_
Mirror the portrait?
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
std::vector< wml_message_option > option_list_
The list of options the user can choose.
virtual void post_show() override
Actions to be taken after the window has been shown.
Shows a dialog with two portraits, one on each side.
virtual void pre_show() override
Shows a dialog with the portrait on the left side.
Helper class for message options.
Definition: wml_message.hpp:26
Shows a dialog with the portrait on the right side.
void set_can_wrap(const bool wrap)
Definition: label.hpp:94
The listbox class.
Definition: listbox.hpp:41
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:92
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:280
virtual void set_label(const t_string &text)
canvas & get_canvas(const unsigned index)
virtual void set_use_markup(bool use_markup)
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
void set_maximum_length(const std::size_t maximum_length)
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
void set_visible(const visibility visible)
Definition: widget.cpp:479
@ invisible
The user set the widget invisible, that means:
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
void add_to_keyboard_chain(widget *widget)
Adds the widget to the keyboard chain.
Definition: window.cpp:1213
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:335
void set_click_dismiss(const bool click_dismiss)
Definition: window.hpp:411
This file contains the window object, this object is a top level container which has the event manage...
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.
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::string_view data
Definition: picture.cpp:178
Parameter pack for message text input options.
Parameter pack for message list input options.
Parameter pack for message portrait.