The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wml_message.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
20 #include "gui/widgets/button.hpp"
21 #include "gui/widgets/label.hpp"
22 #ifdef GUI2_EXPERIMENTAL_LISTBOX
23 #include "gui/widgets/list.hpp"
24 #else
25 #include "gui/widgets/listbox.hpp"
26 #endif
27 #include "gui/widgets/settings.hpp"
28 #include "gui/widgets/text_box.hpp"
29 #include "gui/widgets/window.hpp"
30 
31 namespace gui2
32 {
33 
35  std::string* text,
36  const unsigned maximum_length)
37 {
38  assert(text);
39 
40  has_input_ = true;
41  input_caption_ = caption;
42  input_text_ = text;
43  input_maximum_length_ = maximum_length;
44 }
45 
46 void twml_message_::set_option_list(const std::vector<twml_message_option>& option_list,
47  int* chosen_option)
48 {
49  assert(!option_list.empty());
50  assert(chosen_option);
51 
52  option_list_ = option_list;
53  chosen_option_ = chosen_option;
54 }
55 
56 /**
57  * @todo This function enables the wml markup for all items, but the interface
58  * is a bit hacky. Especially the fiddling in the internals of the listbox is
59  * ugly. There needs to be a clean interface to set whether a widget has a
60  * markup and what kind of markup. These fixes will be post 1.6.
61  */
63 {
64  set_restore(true);
65 
66  window.canvas(1).set_variable("portrait_image", variant(portrait_));
67  window.canvas(1).set_variable("portrait_mirror", variant(mirror_));
68 
69  // Set the markup
70  tlabel& title = find_widget<tlabel>(&window, "title", false);
71  title.set_label(title_);
72  title.set_use_markup(true);
73  title.set_can_wrap(true);
74 
75  tcontrol& message = find_widget<tcontrol>(&window, "message", false);
76  message.set_label(message_);
77  message.set_use_markup(true);
78  // The message label might not always be a scroll_label but the capturing
79  // shouldn't hurt.
80  window.keyboard_capture(&message);
81 
82  // Find the input box related fields.
83  tlabel& caption = find_widget<tlabel>(&window, "input_caption", false);
84  ttext_box& input = find_widget<ttext_box>(&window, "input", true);
85 
86  if(has_input_) {
87  caption.set_label(input_caption_);
88  caption.set_use_markup(true);
89  input.set_value(*input_text_);
90  input.set_maximum_length(input_maximum_length_);
91  window.keyboard_capture(&input);
92  window.set_click_dismiss(false);
93  window.set_escape_disabled(true);
94  } else {
96  input.set_visible(twidget::tvisible::invisible);
97  }
98 
99  // Find the option list related fields.
100  tlistbox& options = find_widget<tlistbox>(&window, "input_list", true);
101 
102  if(!option_list_.empty()) {
103  std::map<std::string, string_map> data;
104  for(const twml_message_option& item : option_list_) {
105  // Add the data.
106  data["icon"]["label"] = item.image();
107  data["label"]["label"] = item.label();
108  data["label"]["use_markup"] = "true";
109  data["description"]["label"] = item.description();
110  data["description"]["use_markup"] = "true";
111  options.add_row(data);
112  }
113 
114  // Avoid negative and 0 since item 0 is already selected.
115  if(*chosen_option_ > 0 && static_cast<size_t>(*chosen_option_)
116  < option_list_.size()) {
117 
118  options.select_row(*chosen_option_);
119  }
120 
121  if(!has_input_) {
122  window.keyboard_capture(&options);
123  window.set_click_dismiss(false);
124  window.set_escape_disabled(true);
125  } else {
126  window.add_to_keyboard_chain(&options);
127  // click_dismiss has been disabled due to the input.
128  }
129  } else {
131  }
132  window.set_click_dismiss(!has_input_ && option_list_.empty());
133 }
134 
136 {
137  if(has_input_) {
138  *input_text_
139  = find_widget<ttext_box>(&window, "input", true).get_value();
140  }
141 
142  if(!option_list_.empty()) {
143  *chosen_option_ = find_widget<tlistbox>(&window, "input_list", true)
144  .get_selected_row();
145  }
146 }
147 
148 REGISTER_DIALOG(wml_message_left)
149 
150 REGISTER_DIALOG(wml_message_right)
151 
152 int show_wml_message(const bool left_side,
153  CVideo& video,
154  const std::string& title,
155  const std::string& message,
156  const std::string& portrait,
157  const bool mirror,
158  const bool has_input,
159  const std::string& input_caption,
160  std::string* input_text,
161  const unsigned maximum_length,
162  const std::vector<twml_message_option>& option_list,
163  int* chosen_option)
164 {
166  if(left_side) {
167  dlg.reset(new twml_message_left(title, message, portrait, mirror));
168  } else {
169  dlg.reset(new twml_message_right(title, message, portrait, mirror));
170  }
171  assert(dlg.get());
172 
173  if(has_input) {
174  dlg->set_input(input_caption, input_text, maximum_length);
175  }
176 
177  if(!option_list.empty()) {
178  dlg->set_option_list(option_list, chosen_option);
179  }
180 
181  dlg->show(video);
182  return dlg->get_retval();
183 }
184 
185 } // namespace gui2
void post_show(twindow &window)
Inherited from tdialog.
void set_can_wrap(const bool wrap)
Definition: label.hpp:60
Definition: video.hpp:58
GLenum GLenum GLenum input
Definition: glew.h:10668
bool has_input_
Do we need to show an input box?
This file contains the window object, this object is a top level container which has the event manage...
bool select_row(const unsigned row, const bool select=true)
Selectes a row.
Definition: listbox.cpp:228
REGISTER_DIALOG(label_settings)
virtual void set_label(const t_string &label)
Definition: control.cpp:330
STL namespace.
Class for a single line text area.
Definition: text_box.hpp:118
Label showing a text.
Definition: label.hpp:29
std::string portrait_
Filename of the portrait.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
int * chosen_option_
The chosen option.
The user set the widget invisible, that means:
Definition: widget.hpp:103
virtual void set_use_markup(bool use_markup)
Definition: control.cpp:342
const config & options()
This file contains the settings handling of the widget library.
void add_row(const string_map &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:74
void set_restore(const bool restore)
Definition: dialog.hpp:171
std::vector< twml_message_option > option_list_
The list of options the user can choose.
Helper class for message options.
Definition: wml_message.hpp:26
void set_input(const std::string &caption, std::string *text, const unsigned maximum_length)
Sets the input text variables.
Definition: wml_message.cpp:34
unsigned input_maximum_length_
The maximum length of the input text.
int show_wml_message(const bool left_side, CVideo &video, const std::string &title, const std::string &message, const std::string &portrait, const bool mirror, const bool has_input, const std::string &input_caption, std::string *input_text, const unsigned maximum_length, const std::vector< twml_message_option > &option_list, int *chosen_option)
Helper function to show a portrait.
The listbox class.
Definition: listbox.hpp:39
Shows a dialog with the portrait on the left side.
Shows a dialog with the portrait on the right side.
std::vector< tcanvas > & canvas()
Definition: control.hpp:282
std::string * input_text_
The text input.
bool mirror_
Mirror the portrait?
std::string input_caption_
The caption to show for the input text.
Base class for all visible items.
Definition: control.hpp:34
std::string title_
The title for the dialog.
Definition: wml_message.hpp:90
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
std::string message_
The message to show to the user.
void set_option_list(const std::vector< twml_message_option > &option_list, int *chosen_option)
Sets the option list.
Definition: wml_message.cpp:46
void pre_show(twindow &window)
Inherited from tdialog.
Definition: wml_message.cpp:62
void set_visible(const tvisible::scoped_enum visible)
Definition: widget.cpp:445
GLsizei const GLcharARB ** string
Definition: glew.h:4503