The Battle for Wesnoth  1.19.5+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  std::size_t get_max_input_length() const
150  {
151  return max_input_length_;
152  }
153 
154  void set_hint_text(const std::string& text)
155  {
156  hint_text_ = text;
157  update_canvas();
158  }
159 
160  std::string get_hint_text() const
161  {
162  return hint_text_;
163  }
164 
165  void set_hint_image(const std::string& image)
166  {
167  hint_image_ = image;
168  update_canvas();
169  }
170 
171  std::string get_hint_image() const
172  {
173  return hint_image_;
174  }
175 
176  void set_hint_data(const std::string& text, const std::string& image)
177  {
178  hint_text_ = text;
179  hint_image_ = image;
180 
181  update_canvas();
182  }
183 
184  void clear()
185  {
186  set_value("");
187  }
188 
189 protected:
190  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
191 
192  /** See @ref widget::place. */
193  virtual void place(const point& origin, const point& size) override;
194 
195  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
196 
197  /** See @ref styled_widget::update_canvas. */
198  virtual void update_canvas() override;
199 
200  /** Inherited from text_box_base. */
201  void goto_end_of_line(const bool select = false) override
202  {
203  goto_end_of_data(select);
204  }
205 
206  /** Inherited from text_box_base. */
207  void goto_start_of_line(const bool select = false) override
208  {
209  goto_start_of_data(select);
210  }
211 
212  /** Inherited from text_box_base. */
213  void delete_char(const bool before_cursor) override;
214 
215  /** Inherited from text_box_base. */
216  void delete_selection() override;
217 
218  void handle_mouse_selection(point mouse, const bool start_selection);
219 
220 private:
221  /** The history text for this widget. */
223 
224  /** The maximum length of the text input. */
225  std::size_t max_input_length_;
226 
227  /**
228  * The x offset in the widget where the text starts.
229  *
230  * This value is needed to translate a location in the widget to a location
231  * in the text.
232  */
233  unsigned text_x_offset_;
234 
235  /**
236  * The y offset in the widget where the text starts.
237  *
238  * Needed to determine whether a click is on the text.
239  */
240  unsigned text_y_offset_;
241 
242  /**
243  * The height of the text itself.
244  *
245  * Needed to determine whether a click is on the text.
246  */
247  unsigned text_height_;
248 
249  /** Updates text_x_offset_ and text_y_offset_. */
250  void update_offsets();
251 
252  /** Is the mouse in dragging mode, this affects selection in mouse move */
253  bool dragging_;
254 
255  /** Helper text to display (such as "Search") if the text box is empty. */
256  std::string hint_text_;
257 
258  /** Image (such as a magnifying glass) that accompanies the help text. */
259  std::string hint_image_;
260 
261  /**
262  * Inherited from text_box_base.
263  *
264  * Unmodified Unhandled.
265  * Control Ignored.
266  * Shift Ignored.
267  * Alt Ignored.
268  */
269  void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override
270  {
271  }
272 
273  /**
274  * Inherited from text_box_base.
275  *
276  * Unmodified Unhandled.
277  * Control Ignored.
278  * Shift Ignored.
279  * Alt Ignored.
280  */
281  void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override
282  {
283  }
284 
285  /**
286  * Goes one item up in the history.
287  *
288  * @returns True if there's a history, false otherwise.
289  */
290  bool history_up();
291 
292  /**
293  * Goes one item down in the history.
294  *
295  * @returns True if there's a history, false otherwise.
296  */
297  bool history_down();
298 
299  /** Inherited from text_box_base. */
300  void handle_key_tab(SDL_Keymod modifier, bool& handled) override;
301 
302  /** Inherited from text_box_base. */
303  void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
304 
305 public:
306  /** Static type getter that does not rely on the widget being constructed. */
307  static const std::string& type();
308 
309 private:
310  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
311  virtual const std::string& get_control_type() const override;
312 
313  /***** ***** ***** signal handlers ***** ****** *****/
314 
316  bool& handled,
317  const point& coordinate);
318 
320  bool& handled);
321 
323  bool& handled);
324 
326  bool& handled);
327 };
328 
329 // }---------- DEFINITION ---------{
330 
332 {
333  explicit text_box_definition(const config& cfg);
334 
336  {
337  explicit resolution(const config& cfg);
338 
341  };
342 };
343 
344 // }---------- BUILDER -----------{
345 
346 namespace implementation
347 {
348 
350 {
351 public:
352  explicit builder_text_box(const config& cfg);
353 
355 
356  virtual std::unique_ptr<widget> build() const override;
357 
358  std::string history;
359 
360  std::size_t max_input_length;
361 
363  std::string hint_image;
364 
365  bool editable;
366 };
367 
368 } // namespace implementation
369 
370 // }------------ END --------------
371 
372 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
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
std::string get_hint_image() const
Definition: text_box.hpp:171
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:225
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:222
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void set_hint_text(const std::string &text)
Definition: text_box.hpp:154
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:259
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 set_hint_image(const std::string &image)
Definition: text_box.hpp:165
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:201
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:247
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:281
std::string hint_text_
Helper text to display (such as "Search") if the text box is empty.
Definition: text_box.hpp:256
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:207
void handle_key_up_arrow(SDL_Keymod, bool &) override
Inherited from text_box_base.
Definition: text_box.hpp:269
bool history_up()
Goes one item up in the history.
Definition: text_box.cpp:304
std::size_t get_max_input_length() const
Definition: text_box.hpp:149
std::string get_hint_text() const
Definition: text_box.hpp:160
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:384
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
Definition: text_box.hpp:253
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:176
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
Definition: text_box.cpp:374
unsigned text_y_offset_
The y offset in the widget where the text starts.
Definition: text_box.hpp:240
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:233
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:430
typed_formula< unsigned > text_y_offset
Definition: text_box.hpp:340
typed_formula< unsigned > text_x_offset
Definition: text_box.hpp:339
text_box_definition(const config &cfg)
Definition: text_box.cpp:395
Holds a 2D point.
Definition: point.hpp:25