The Battle for Wesnoth  1.19.2+dev
text_box.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
21 {
22 namespace implementation
23 {
24 struct builder_text_box;
25 }
26 
27 // ------------ WIDGET -----------{
28 
29 /**
30  * Class for text input history.
31  *
32  * The history of text items can be stored in the preferences. This class
33  * handles that. Every item needs an id by which the history is loaded and
34  * saved.
35  */
37 {
38 public:
39  /**
40  * Gets history that matches id.
41  *
42  * @param id The id of the history to look for.
43  * @param enabled The enabled state of the history.
44  *
45  * @returns The history object.
46  */
47  static text_history get_history(const std::string& id, const bool enabled);
48 
49  text_history() : history_(0), pos_(0), enabled_(false)
50  {
51  }
52 
53  /**
54  * Push string into the history.
55  *
56  * If the string is empty or the same as the last item in the history this
57  * function is a nop.
58  *
59  * @param text The text to push in the history.
60  */
61  void push(const std::string& text);
62 
63  /**
64  * One step up in the history.
65  *
66  * Pushes text to the history if at the end.
67  *
68  * @param text The text to push in the history.
69  *
70  * @returns The current value of the history.
71  */
72  std::string up(const std::string& text = "");
73 
74  /**
75  * One step down in the history.
76  *
77  * Pushes text to the history if at the end.
78  *
79  * @param text The text to push in the history.
80  *
81  * @returns The current value of the history.
82  */
83  std::string down(const std::string& text = "");
84 
85  /**
86  * Gets the current history value.
87  *
88  * @returns If enabled return the current history
89  * position, otherwise an empty string is
90  * returned.
91  */
92  std::string get_value() const;
93 
94  /***** ***** ***** setters / getters for members ***** ****** *****/
95 
96  void set_enabled(bool enabled = true)
97  {
98  enabled_ = enabled;
99  }
100  bool get_enabled() const
101  {
102  return enabled_;
103  }
104 
105 private:
106  text_history(std::vector<std::string>* history, const bool enabled)
107  : history_(history), pos_(history->size()), enabled_(enabled)
108  {
109  }
110 
111  /** The items in the history. */
112  std::vector<std::string>* history_;
113 
114  /** The current position in the history. */
115  unsigned pos_;
116 
117  /** Is the history enabled. */
118  bool enabled_;
119 };
120 
121 /**
122  * A widget that allows the user to input text in single line
123  */
124 class text_box : public text_box_base
125 {
127 
128 public:
129  explicit text_box(const implementation::builder_styled_widget& builder);
130 
131  /** Saves the text in the widget to the history. */
133  {
135  }
136 
137  /***** ***** ***** setters / getters for members ***** ****** *****/
138 
139  void set_history(const std::string& id)
140  {
142  }
143 
144  void set_max_input_length(const std::size_t length)
145  {
146  max_input_length_ = length;
147  }
148 
149  void set_hint_data(const std::string& text, const std::string& image)
150  {
151  hint_text_ = text;
152  hint_image_ = image;
153 
154  update_canvas();
155  }
156 
157  void clear()
158  {
159  set_value("");
160  }
161 
162 protected:
163  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
164 
165  /** See @ref widget::place. */
166  virtual void place(const point& origin, const point& size) override;
167 
168  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
169 
170  /** See @ref styled_widget::update_canvas. */
171  virtual void update_canvas() override;
172 
173  /** Inherited from text_box_base. */
174  void goto_end_of_line(const bool select = false) override
175  {
176  goto_end_of_data(select);
177  }
178 
179  /** Inherited from text_box_base. */
180  void goto_start_of_line(const bool select = false) override
181  {
182  goto_start_of_data(select);
183  }
184 
185  /** Inherited from text_box_base. */
186  void delete_char(const bool before_cursor) override;
187 
188  /** Inherited from text_box_base. */
189  void delete_selection() override;
190 
191  void handle_mouse_selection(point mouse, const bool start_selection);
192 
193 private:
194  /** The history text for this widget. */
196 
197  /** The maximum length of the text input. */
198  std::size_t max_input_length_;
199 
200  /**
201  * The x offset in the widget where the text starts.
202  *
203  * This value is needed to translate a location in the widget to a location
204  * in the text.
205  */
206  unsigned text_x_offset_;
207 
208  /**
209  * The y offset in the widget where the text starts.
210  *
211  * Needed to determine whether a click is on the text.
212  */
213  unsigned text_y_offset_;
214 
215  /**
216  * The height of the text itself.
217  *
218  * Needed to determine whether a click is on the text.
219  */
220  unsigned text_height_;
221 
222  /** Updates text_x_offset_ and text_y_offset_. */
223  void update_offsets();
224 
225  /** Is the mouse in dragging mode, this affects selection in mouse move */
226  bool dragging_;
227 
228  /** Helper text to display (such as "Search") if the text box is empty. */
229  std::string hint_text_;
230 
231  /** Image (such as a magnifying glass) that accompanies the help text. */
232  std::string hint_image_;
233 
234  /**
235  * Inherited from text_box_base.
236  *
237  * Unmodified Unhandled.
238  * Control Ignored.
239  * Shift Ignored.
240  * Alt Ignored.
241  */
242  void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override
243  {
244  }
245 
246  /**
247  * Inherited from text_box_base.
248  *
249  * Unmodified Unhandled.
250  * Control Ignored.
251  * Shift Ignored.
252  * Alt Ignored.
253  */
254  void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override
255  {
256  }
257 
258  /**
259  * Goes one item up in the history.
260  *
261  * @returns True if there's a history, false otherwise.
262  */
263  bool history_up();
264 
265  /**
266  * Goes one item down in the history.
267  *
268  * @returns True if there's a history, false otherwise.
269  */
270  bool history_down();
271 
272  /** Inherited from text_box_base. */
273  void handle_key_tab(SDL_Keymod modifier, bool& handled) override;
274 
275  /** Inherited from text_box_base. */
276  void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
277 
278 public:
279  /** Static type getter that does not rely on the widget being constructed. */
280  static const std::string& type();
281 
282 private:
283  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
284  virtual const std::string& get_control_type() const override;
285 
286  /***** ***** ***** signal handlers ***** ****** *****/
287 
289  bool& handled,
290  const point& coordinate);
291 
293  bool& handled);
294 
296  bool& handled);
297 
299  bool& handled);
300 };
301 
302 // }---------- DEFINITION ---------{
303 
305 {
306  explicit text_box_definition(const config& cfg);
307 
309  {
310  explicit resolution(const config& cfg);
311 
314  };
315 };
316 
317 // }---------- BUILDER -----------{
318 
319 namespace implementation
320 {
321 
323 {
324 public:
325  explicit builder_text_box(const config& cfg);
326 
328 
329  virtual std::unique_ptr<widget> build() const override;
330 
331  std::string history;
332 
333  std::size_t max_input_length;
334 
336  std::string hint_image;
337 
338  bool editable;
339 };
340 
341 } // namespace implementation
342 
343 // }------------ END --------------
344 
345 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Abstract base class for text items.
std::string get_value() const
const std::string & text() const
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void goto_end_of_data(const bool select=false)
Moves the cursor to the end of all text.
virtual void goto_start_of_data(const bool select=false)
Moves the cursor to the beginning of the data.
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: text_box.cpp:129
std::size_t max_input_length_
The maximum length of the text input.
Definition: text_box.hpp:198
void handle_key_clear_line(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
Definition: text_box.cpp:341
text_history history_
The history text for this widget.
Definition: text_box.hpp:195
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
bool history_down()
Goes one item down in the history.
Definition: text_box.cpp:317
std::string hint_image_
Image (such as a magnifying glass) that accompanies the help text.
Definition: text_box.hpp:232
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void signal_handler_mouse_motion(const event::ui_event event, bool &handled, const point &coordinate)
Definition: text_box.cpp:348
void delete_char(const bool before_cursor) override
Inherited from text_box_base.
Definition: text_box.cpp:223
void goto_end_of_line(const bool select=false) override
Inherited from text_box_base.
Definition: text_box.hpp:174
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
Definition: text_box.cpp:361
unsigned text_height_
The height of the text itself.
Definition: text_box.hpp:220
void set_max_input_length(const std::size_t length)
Definition: text_box.hpp:144
void handle_key_down_arrow(SDL_Keymod, bool &) override
Inherited from text_box_base.
Definition: text_box.hpp:254
std::string hint_text_
Helper text to display (such as "Search") if the text box is empty.
Definition: text_box.hpp:229
void set_history(const std::string &id)
Definition: text_box.hpp:139
void goto_start_of_line(const bool select=false) override
Inherited from text_box_base.
Definition: text_box.hpp:180
void handle_key_up_arrow(SDL_Keymod, bool &) override
Inherited from text_box_base.
Definition: text_box.hpp:242
bool history_up()
Goes one item up in the history.
Definition: text_box.cpp:304
void handle_key_tab(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
Definition: text_box.cpp:330
void handle_mouse_selection(point mouse, const bool start_selection)
Definition: text_box.cpp:254
void signal_handler_left_button_double_click(const event::ui_event event, bool &handled)
Definition: text_box.cpp:388
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
Definition: text_box.hpp:226
void update_offsets()
Updates text_x_offset_ and text_y_offset_.
Definition: text_box.cpp:278
text_box(const implementation::builder_styled_widget &builder)
Definition: text_box.cpp:100
void save_to_history()
Saves the text in the widget to the history.
Definition: text_box.hpp:132
void delete_selection() override
Inherited from text_box_base.
Definition: text_box.cpp:234
void set_hint_data(const std::string &text, const std::string &image)
Definition: text_box.hpp:149
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: text_box.cpp:378
unsigned text_y_offset_
The y offset in the widget where the text starts.
Definition: text_box.hpp:213
virtual void update_canvas() override
See styled_widget::update_canvas.
Definition: text_box.cpp:142
unsigned text_x_offset_
The x offset in the widget where the text starts.
Definition: text_box.hpp:206
Class for text input history.
Definition: text_box.hpp:37
text_history(std::vector< std::string > *history, const bool enabled)
Definition: text_box.hpp:106
static text_history get_history(const std::string &id, const bool enabled)
Gets history that matches id.
Definition: text_box.cpp:40
unsigned pos_
The current position in the history.
Definition: text_box.hpp:115
std::string get_value() const
Gets the current history value.
Definition: text_box.cpp:91
void set_enabled(bool enabled=true)
Definition: text_box.hpp:96
std::string down(const std::string &text="")
One step down in the history.
Definition: text_box.cpp:78
bool enabled_
Is the history enabled.
Definition: text_box.hpp:118
std::vector< std::string > * history_
The items in the history.
Definition: text_box.hpp:112
void push(const std::string &text)
Push string into the history.
Definition: text_box.cpp:47
std::string up(const std::string &text="")
One step up in the history.
Definition: text_box.cpp:60
bool get_enabled() const
Definition: text_box.hpp:100
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
Functions to load and save images from/to disk.
Contains the implementation details for lexical_cast and shouldn't be used directly.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
virtual std::unique_ptr< widget > build() const=0
virtual std::unique_ptr< widget > build() const override
Definition: text_box.cpp:434
typed_formula< unsigned > text_y_offset
Definition: text_box.hpp:313
typed_formula< unsigned > text_x_offset
Definition: text_box.hpp:312
text_box_definition(const config &cfg)
Definition: text_box.cpp:399
Holds a 2D point.
Definition: point.hpp:25