The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
text_box.hpp
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 #ifndef GUI_WIDGETS_TEXT_BOX_HPP_INCLUDED
16 #define GUI_WIDGETS_TEXT_BOX_HPP_INCLUDED
17 
18 #include "gui/widgets/text.hpp"
19 
20 namespace gui2
21 {
22 
23 // ------------ WIDGET -----------{
24 
25 /**
26  * Class for text input history.
27  *
28  * The history of text items can be stored in the preferences. This class
29  * handles that. Every item needs an id by which the history is loaded and
30  * saved.
31  */
33 {
34 public:
35  /**
36  * Gets history that matches id.
37  *
38  * @param id The id of the history to look for.
39  * @param enabled The enabled state of the history.
40  *
41  * @returns The history object.
42  */
43  static ttext_history get_history(const std::string& id, const bool enabled);
44 
45  ttext_history() : history_(0), pos_(0), enabled_(false)
46  {
47  }
48 
49  /**
50  * Push string into the history.
51  *
52  * If the string is empty or the same as the last item in the history this
53  * function is a nop.
54  *
55  * @param text The text to push in the history.
56  */
57  void push(const std::string& text);
58 
59  /**
60  * One step up in the history.
61  *
62  * Pushes text to the history if at the end.
63  *
64  * @param text The text to push in the history.
65  *
66  * @returns The current value of the history.
67  */
68  std::string up(const std::string& text = "");
69 
70  /**
71  * One step down in the history.
72  *
73  * Pushes text to the history if at the end.
74  *
75  * @param text The text to push in the history.
76  *
77  * @returns The current value of the history.
78  */
79  std::string down(const std::string& text = "");
80 
81  /**
82  * Gets the current history value.
83  *
84  * @returns If enabled return the current history
85  * position, otherwise an empty string is
86  * returned.
87  */
88  std::string get_value() const;
89 
90  /***** ***** ***** setters / getters for members ***** ****** *****/
91 
92  void set_enabled(bool enabled = true)
93  {
94  enabled_ = enabled;
95  }
96  bool get_enabled() const
97  {
98  return enabled_;
99  }
100 
101 private:
102  ttext_history(std::vector<std::string>* history, const bool enabled)
103  : history_(history), pos_(history->size()), enabled_(enabled)
104  {
105  }
106 
107  /** The items in the history. */
108  std::vector<std::string>* history_;
109 
110  /** The current position in the history. */
111  unsigned pos_;
112 
113  /** Is the history enabled. */
114  bool enabled_;
115 };
116 
117 /** Class for a single line text area. */
118 class ttext_box : public ttext_
119 {
120 public:
121  ttext_box();
122 
123  /** Saves the text in the widget to the history. */
125  {
127  }
128 
129  /***** ***** ***** setters / getters for members ***** ****** *****/
130 
131  void set_history(const std::string& id)
132  {
134  }
135 
136  void set_max_input_length(const size_t length)
137  {
139  }
140 
141  void clear()
142  {
143  set_value("");
144  }
145 
146 protected:
147  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
148 
149  /** See @ref twidget::place. */
150  virtual void place(const tpoint& origin, const tpoint& size) override;
151 
152  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
153 
154  /** See @ref tcontrol::update_canvas. */
155  virtual void update_canvas() override;
156 
157  /** Inherited from ttext_. */
158  void goto_end_of_line(const bool select = false)
159  {
160  goto_end_of_data(select);
161  }
162 
163  /** Inherited from ttext_. */
164  void goto_start_of_line(const bool select = false)
165  {
166  goto_start_of_data(select);
167  }
168 
169  /** Inherited from ttext_. */
170  void delete_char(const bool before_cursor);
171 
172  /** Inherited from ttext_. */
173  void delete_selection();
174 
175  void handle_mouse_selection(tpoint mouse, const bool start_selection);
176 
177 private:
178  /** The history text for this widget. */
180 
181  /** The maximum length of the text input. */
183 
184  /**
185  * The x offset in the widget where the text starts.
186  *
187  * This value is needed to translate a location in the widget to a location
188  * in the text.
189  */
190  unsigned text_x_offset_;
191 
192  /**
193  * The y offset in the widget where the text starts.
194  *
195  * Needed to determine whether a click is on the text.
196  */
197  unsigned text_y_offset_;
198 
199  /**
200  * The height of the text itself.
201  *
202  * Needed to determine whether a click is on the text.
203  */
204  unsigned text_height_;
205 
206  /** Updates text_x_offset_ and text_y_offset_. */
207  void update_offsets();
208 
209  /** Is the mouse in dragging mode, this affects selection in mouse move */
210  bool dragging_;
211 
212  /**
213  * Inherited from ttext_.
214  *
215  * Unmodified Unhandled.
216  * Control Ignored.
217  * Shift Ignored.
218  * Alt Ignored.
219  */
220  void handle_key_up_arrow(SDLMod /*modifier*/, bool& /*handled*/)
221  {
222  }
223 
224  /**
225  * Inherited from ttext_.
226  *
227  * Unmodified Unhandled.
228  * Control Ignored.
229  * Shift Ignored.
230  * Alt Ignored.
231  */
232  void handle_key_down_arrow(SDLMod /*modifier*/, bool& /*handled*/)
233  {
234  }
235 
236  /**
237  * Goes one item up in the history.
238  *
239  * @returns True if there's a history, false otherwise.
240  */
241  bool history_up();
242 
243  /**
244  * Goes one item down in the history.
245  *
246  * @returns True if there's a history, false otherwise.
247  */
248  bool history_down();
249 
250  /** Inherited from ttext_. */
251  void handle_key_default(bool& handled,
252  SDLKey key,
253  SDLMod modifier,
254  const utf8::string& unicode);
255 
256  /** Inherited from ttext_. */
257  void handle_key_clear_line(SDLMod modifier, bool& handled);
258 
259  /** See @ref tcontrol::get_control_type. */
260  virtual const std::string& get_control_type() const override;
261 
262  /** Inherited from tcontrol. */
263  void load_config_extra();
264 
265  /***** ***** ***** signal handlers ***** ****** *****/
266 
268  bool& handled,
269  const tpoint& coordinate);
270 
272  bool& handled);
273 
275  bool& handled);
276 
278  bool& handled);
279 };
280 
281 // }---------- DEFINITION ---------{
282 
284 {
285  explicit ttext_box_definition(const config& cfg);
286 
288  {
289  explicit tresolution(const config& cfg);
290 
293  };
294 };
295 
296 // }---------- BUILDER -----------{
297 
298 namespace implementation
299 {
300 
302 {
303 public:
304  explicit tbuilder_text_box(const config& cfg);
305 
307 
308  twidget* build() const;
309 
311 
313 };
314 
315 } // namespace implementation
316 
317 // }------------ END --------------
318 
319 } // namespace gui2
320 
321 #endif
void handle_key_clear_line(SDLMod modifier, bool &handled)
Inherited from ttext_.
Definition: text_box.cpp:320
void set_enabled(bool enabled=true)
Definition: text_box.hpp:92
unsigned pos_
The current position in the history.
Definition: text_box.hpp:111
Class for text input history.
Definition: text_box.hpp:32
#define SDLMod
Definition: compat.hpp:30
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
Definition: text_box.hpp:210
GLenum GLsizei const GLuint GLboolean enabled
Definition: glew.h:2497
void handle_key_up_arrow(SDLMod, bool &)
Inherited from ttext_.
Definition: text_box.hpp:220
ttext_history history_
The history text for this widget.
Definition: text_box.hpp:179
unsigned text_y_offset_
The y offset in the widget where the text starts.
Definition: text_box.hpp:197
unsigned text_x_offset_
The x offset in the widget where the text starts.
Definition: text_box.hpp:190
virtual void place(const tpoint &origin, const tpoint &size) override
See twidget::place.
Definition: text_box.cpp:119
bool get_enabled() const
Definition: text_box.hpp:96
std::string up(const std::string &text="")
One step up in the history.
Definition: text_box.cpp:58
bool history_up()
Goes one item up in the history.
Definition: text_box.cpp:275
void load_config_extra()
Inherited from tcontrol.
Definition: text_box.cpp:327
void goto_start_of_line(const bool select=false)
Inherited from ttext_.
Definition: text_box.hpp:164
static ttext_history get_history(const std::string &id, const bool enabled)
Gets history that matches id.
Definition: text_box.cpp:38
void signal_handler_left_button_down(const event::tevent event, bool &handled)
Definition: text_box.cpp:362
Class for a single line text area.
Definition: text_box.hpp:118
unsigned text_height_
The height of the text itself.
Definition: text_box.hpp:204
std::string get_value() const
Definition: text.hpp:76
void update_offsets()
Updates text_x_offset_ and text_y_offset_.
Definition: text_box.cpp:244
virtual void set_value(const std::string &text)
The set_value is virtual for the tpassword_box class.
Definition: text.cpp:95
#define SDLKey
Definition: compat.hpp:29
virtual void update_canvas() override
See tcontrol::update_canvas.
Definition: text_box.cpp:132
Base class of a resolution, contains the common keys for a resolution.
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
std::string get_value() const
Gets the current history value.
Definition: text_box.cpp:89
void goto_end_of_data(const bool select=false)
Moves the cursor to the end of all text.
Definition: text.hpp:110
virtual twidget * build() const =0
void goto_start_of_data(const bool select=false)
Moves the cursor to the beginning of the data.
Definition: text.hpp:129
void push(const std::string &text)
Push string into the history.
Definition: text_box.cpp:45
void signal_handler_left_button_double_click(const event::tevent event, bool &handled)
Definition: text_box.cpp:389
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
void handle_key_default(bool &handled, SDLKey key, SDLMod modifier, const utf8::string &unicode)
Inherited from ttext_.
Definition: text_box.cpp:301
void goto_end_of_line(const bool select=false)
Inherited from ttext_.
Definition: text_box.hpp:158
ttext_box_definition(const config &cfg)
Definition: text_box.cpp:400
bool enabled_
Is the history enabled.
Definition: text_box.hpp:114
void save_to_history()
Saves the text in the widget to the history.
Definition: text_box.hpp:124
void handle_mouse_selection(tpoint mouse, const bool start_selection)
Definition: text_box.cpp:219
void delete_selection()
Inherited from ttext_.
Definition: text_box.cpp:199
std::string down(const std::string &text="")
One step down in the history.
Definition: text_box.cpp:76
Holds a 2D point.
Definition: point.hpp:24
void set_max_input_length(const size_t length)
Definition: text_box.hpp:136
void set_history(const std::string &id)
Definition: text_box.hpp:131
GLsizeiptr size
Definition: glew.h:1649
cl_event event
Definition: glew.h:3070
Base class for all widgets.
Definition: widget.hpp:49
void handle_key_down_arrow(SDLMod, bool &)
Inherited from ttext_.
Definition: text_box.hpp:232
void signal_handler_mouse_motion(const event::tevent event, bool &handled, const tpoint &coordinate)
Definition: text_box.cpp:349
std::vector< std::string > * history_
The items in the history.
Definition: text_box.hpp:108
void signal_handler_left_button_up(const event::tevent event, bool &handled)
Definition: text_box.cpp:379
void delete_char(const bool before_cursor)
Inherited from ttext_.
Definition: text_box.cpp:188
ttext_history(std::vector< std::string > *history, const bool enabled)
Definition: text_box.hpp:102
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
size_t max_input_length_
The maximum length of the text input.
Definition: text_box.hpp:182
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Abstract base class for text items.
Definition: text.hpp:43
Contains the implementation details for lexical_cast and shouldn't be used directly.
bool history_down()
Goes one item down in the history.
Definition: text_box.cpp:288
std::string string
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
Definition: text_box.cpp:343