The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
text.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_HPP_INCLUDED
16 #define GUI_WIDGETS_TEXT_HPP_INCLUDED
17 
18 //#include "gui/core/event/dispatcher.hpp"
19 #include "gui/widgets/control.hpp"
20 #include "../../text.hpp" // We want the file in src/
21 
22 #include <string>
23 
24 #include "utils/functional.hpp"
25 
26 namespace gui2
27 {
28 
29 /**
30  * Abstract base class for text items.
31  *
32  * All other text classes should inherit from this base class.
33  *
34  * The NOTIFY_MODIFIED event is send when the text is modified.
35  *
36  * @todo Validate whether the NOTIFY_MODIFIED is always fired properly. The
37  * current implementation is added for some quick testing so some cases might
38  * be forgotten.
39  *
40  * Common signal handlers:
41  * - connect_signal_pre_key_press
42  */
43 class ttext_ : public tcontrol
44 {
45 
46 public:
47  ttext_();
48 
49  /** See @ref tcontrol::set_active. */
50  virtual void set_active(const bool active) override;
51 
52  /** See @ref tcontrol::get_active. */
53  virtual bool get_active() const override;
54 
55  /** See @ref tcontrol::get_state. */
56  virtual unsigned get_state() const override;
57 
58  /***** ***** ***** ***** expose some functions ***** ***** ***** *****/
59 
60  void set_maximum_length(const size_t maximum_length);
61 
62  size_t get_length() const
63  {
64  return text_.get_length();
65  }
66 
67  /***** ***** ***** setters / getters for members ***** ****** *****/
68 
69  /**
70  * The set_value is virtual for the @ref tpassword_box class.
71  *
72  * That class overrides the set_value function to replace it with asterisk.
73  * There might be more generic way to do it when more classes are needed.
74  */
75  virtual void set_value(const std::string& text);
77  {
78  return text_.text();
79  }
80 
81  const std::string& text() const
82  {
83  return text_.text();
84  }
85 
86  /** Set the text_changed callback. */
88  std::function<void(ttext_* textbox, const std::string text)> cb)
89  {
91  }
92 
93 protected:
94  /**
95  * Moves the cursor to the end of the line.
96  *
97  * @param select Select the text from the original cursor
98  * position till the end of the line?
99  */
100  virtual void goto_end_of_line(const bool select = false) = 0;
101 
102  /**
103  * Moves the cursor to the end of all text.
104  *
105  * For a single line text this is the same as goto_end_of_line().
106  *
107  * @param select Select the text from the original cursor
108  * position till the end of the data?
109  */
110  void goto_end_of_data(const bool select = false)
111  {
112  set_cursor(text_.get_length(), select);
113  }
114 
115  /**
116  * Moves the cursor to the beginning of the line
117  *
118  * @param select Select the text from the original cursor
119  * position till the beginning of the line?
120  */
121  virtual void goto_start_of_line(const bool select = false) = 0;
122 
123  /**
124  * Moves the cursor to the beginning of the data.
125  *
126  * @param select Select the text from the original cursor
127  * position till the beginning of the data?
128  */
129  void goto_start_of_data(const bool select = false)
130  {
131  set_cursor(0, select);
132  }
133 
134  /** Selects all text. */
135  void select_all()
136  {
137  selection_start_ = 0;
138  goto_end_of_data(true);
139  }
140 
141  /**
142  * Moves the cursor at the wanted position.
143  *
144  * @param offset The wanted new cursor position.
145  * @param select Select the text from the original cursor
146  * position till the new position?
147  */
148  void set_cursor(const size_t offset, const bool select);
149 
150  /**
151  * Inserts a character at the cursor.
152  *
153  * This function is preferred over set_text since it's optimized for
154  * updating the internal bookkeeping.
155  *
156  * @param unicode The unicode value of the character to insert.
157  */
158  virtual void insert_char(const utf8::string& unicode);
159 
160  /**
161  * Deletes the character.
162  *
163  * @param before_cursor If true it deletes the character before the
164  * cursor (backspace) else the character after
165  * the cursor (delete).
166  */
167  virtual void delete_char(const bool before_cursor) = 0;
168 
169  /** Deletes the current selection. */
170  virtual void delete_selection() = 0;
171 
172  /** Copies the current selection. */
173  virtual void copy_selection(const bool mouse);
174 
175  /** Pastes the current selection. */
176  virtual void paste_selection(const bool mouse);
177 
178  /***** ***** ***** ***** expose some functions ***** ***** ***** *****/
179 
181  const unsigned line = 0) const
182  {
183  return text_.get_cursor_position(column, line);
184  }
185 
186  tpoint get_column_line(const tpoint& position) const
187  {
188  return text_.get_column_line(position);
189  }
190 
191  void set_font_size(const unsigned font_size)
192  {
193  text_.set_font_size(font_size);
194  }
195 
196  void set_font_style(const unsigned font_style)
197  {
198  text_.set_font_style(font_style);
199  }
200 
201  void set_maximum_width(const int width)
202  {
203  text_.set_maximum_width(width);
204  }
205 
206  void set_maximum_height(const int height, const bool multiline)
207  {
208  text_.set_maximum_height(height, multiline);
209  }
210 
211  void set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
212  {
213  text_.set_ellipse_mode(ellipse_mode);
214  }
215 
216  /***** ***** ***** setters / getters for members ***** ****** *****/
217 
218  size_t get_selection_start() const
219  {
220  return selection_start_;
221  }
222  void set_selection_start(const size_t selection_start);
223 
224  size_t get_selection_length() const
225  {
226  return selection_length_;
227  }
228  void set_selection_length(const int selection_length);
229 
230 
231 private:
232  /** Note the order of the states must be the same as defined in
233  * settings.hpp. */
234  enum tstate {
239  };
240 
241  void set_state(const tstate state);
242 
243  /**
244  * Current state of the widget.
245  *
246  * The state of the widget determines what to render and how the widget
247  * reacts to certain 'events'.
248  */
250 
251  /** The text entered in the widget. */
253 
254  /** Start of the selected text. */
256 
257  /**
258  * Length of the selected text.
259  *
260  * * positive selection_len_ means selection to the right.
261  * * negative selection_len_ means selection to the left.
262  * * selection_len_ == 0 means no selection.
263  */
265 
266  /****** handling of special keys first the pure virtuals *****/
267 
268  /**
269  * Every key can have several behaviors.
270  *
271  * Unmodified No modifier is pressed.
272  * Control The control key is pressed.
273  * Shift The shift key is pressed.
274  * Alt The alt key is pressed.
275  *
276  * If modifiers together do something else as the sum of the modifiers
277  * it's listed separately eg.
278  *
279  * Control Moves 10 steps at the time.
280  * Shift Selects the text.
281  * Control + Shift Inserts 42 in the text.
282  *
283  * There are some predefined actions for results.
284  * Unhandled The key/modifier is ignored and also reported
285  * unhandled.
286  * Ignored The key/modifier is ignored and it's
287  * _expected_ the inherited classes do the same.
288  * Implementation defined The key/modifier is ignored and it's expected
289  * the inherited classes will define some meaning
290  * to it.
291  */
292 
293  /**
294  * Up arrow key pressed.
295  *
296  * The behavior is implementation defined.
297  */
298  virtual void handle_key_up_arrow(SDLMod modifier, bool& handled) = 0;
299 
300  /**
301  * Down arrow key pressed.
302  *
303  * The behavior is implementation defined.
304  */
305  virtual void handle_key_down_arrow(SDLMod modifier, bool& handled) = 0;
306 
307  /**
308  * Clears the current line.
309  *
310  * Unmodified Clears the current line.
311  * Control Ignored.
312  * Shift Ignored.
313  * Alt Ignored.
314  */
315  virtual void handle_key_clear_line(SDLMod modifier, bool& handled) = 0;
316 
317  /**
318  * Left arrow key pressed.
319  *
320  * Unmodified Moves the cursor a character to the left.
321  * Control Like unmodified but a word instead of a letter
322  * at the time.
323  * Shift Selects the text while moving.
324  * Alt Ignored.
325  */
326  virtual void handle_key_left_arrow(SDLMod modifier, bool& handled);
327 
328  /**
329  * Right arrow key pressed.
330  *
331  * Unmodified Moves the cursor a character to the right.
332  * Control Like unmodified but a word instead of a letter
333  * at the time.
334  * Shift Selects the text while moving.
335  * Alt Ignored.
336  */
337  virtual void handle_key_right_arrow(SDLMod modifier, bool& handled);
338 
339  /**
340  * Home key pressed.
341  *
342  * Unmodified Moves the cursor a to the beginning of the
343  * line.
344  * Control Like unmodified but to the beginning of the
345  * data.
346  * Shift Selects the text while moving.
347  * Alt Ignored.
348  */
349  virtual void handle_key_home(SDLMod modifier, bool& handled);
350 
351  /**
352  * End key pressed.
353  *
354  * Unmodified Moves the cursor a to the end of the line.
355  * Control Like unmodified but to the end of the data.
356  * Shift Selects the text while moving.
357  * Alt Ignored.
358  */
359  virtual void handle_key_end(SDLMod modifier, bool& handled);
360 
361  /**
362  * Backspace key pressed.
363  *
364  * Unmodified Deletes the character before the cursor,
365  * ignored if at the beginning of the data.
366  * Control Ignored.
367  * Shift Ignored.
368  * Alt Ignored.
369  */
370  virtual void handle_key_backspace(SDLMod modifier, bool& handled);
371 
372  /**
373  * Delete key pressed.
374  *
375  * Unmodified If there is a selection that's deleted.
376  * Else if not at the end of the data the
377  * character after the cursor is deleted.
378  * Else the key is ignored.
379  * ignored if at the beginning of the data.
380  * Control Ignored.
381  * Shift Ignored.
382  * Alt Ignored.
383  */
384  virtual void handle_key_delete(SDLMod modifier, bool& handled);
385 
386  /**
387  * Page up key.
388  *
389  * Unmodified Unhandled.
390  * Control Ignored.
391  * Shift Ignored.
392  * Alt Ignored.
393  */
394  virtual void handle_key_page_up(SDLMod /*modifier*/, bool& /*handled*/)
395  {
396  }
397 
398  /**
399  * Page down key.
400  *
401  * Unmodified Unhandled.
402  * Control Ignored.
403  * Shift Ignored.
404  * Alt Ignored.
405  */
406  virtual void handle_key_page_down(SDLMod /*modifier*/, bool& /*handled*/)
407  {
408  }
409 
410 protected:
411  /**
412  * Default key handler if none of the above functions is called.
413  *
414  * Unmodified If invalid unicode it's ignored.
415  * Else if text selected the selected text is
416  * replaced with the unicode character send.
417  * Else the unicode character is inserted after
418  * the cursor.
419  * Control Ignored.
420  * Shift Ignored (already in the unicode value).
421  * Alt Ignored.
422  */
423  virtual void handle_key_default(bool& handled,
424  SDLKey key,
425  SDLMod modifier,
426  const utf8::string& unicode);
427 
428 private:
429  /**
430  * Text changed callback.
431  *
432  * This callback is called in key_press after the key_press event has been
433  * handled by the control. The parameters to the function are:
434  * - The widget invoking the callback
435  * - The new text of the textbox.
436  */
437  std::function<void(ttext_* textbox, const std::string text)>
439 
440  /***** ***** ***** signal handlers ***** ****** *****/
441 
443  bool& handled);
444 
446  bool& handled,
447  const SDLKey key,
448  SDLMod modifier,
449  const utf8::string& unicode);
450 
453 };
454 
455 } // namespace gui2
456 
457 #endif
void set_selection_length(const int selection_length)
Definition: text.cpp:192
virtual void delete_char(const bool before_cursor)=0
Deletes the character.
virtual void delete_selection()=0
Deletes the current selection.
font::ttext text_
The text entered in the widget.
Definition: text.hpp:252
#define SDLMod
Definition: compat.hpp:30
virtual void handle_key_left_arrow(SDLMod modifier, bool &handled)
Left arrow key pressed.
Definition: text.cpp:208
int selection_length_
Length of the selected text.
Definition: text.hpp:264
virtual void handle_key_end(SDLMod modifier, bool &handled)
End key pressed.
Definition: text.cpp:244
void set_font_size(const unsigned font_size)
Definition: text.hpp:191
void set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
Definition: text.hpp:211
const std::string & text() const
Definition: text.hpp:220
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: text.cpp:56
ttext & set_font_style(const unsigned font_style)
Definition: text.cpp:418
void set_font_style(const unsigned font_style)
Definition: text.hpp:196
virtual void paste_selection(const bool mouse)
Pastes the current selection.
Definition: text.cpp:168
void set_maximum_width(const int width)
Definition: text.hpp:201
tstate
Note the order of the states must be the same as defined in settings.hpp.
Definition: text.hpp:234
void signal_handler_lose_keyboard_focus(const event::tevent event)
Definition: text.cpp:443
void signal_handler_sdl_key_down(const event::tevent event, bool &handled, const SDLKey key, SDLMod modifier, const utf8::string &unicode)
Definition: text.cpp:306
ttext & set_font_size(const unsigned font_size)
Definition: text.cpp:406
gui2::tpoint get_column_line(const gui2::tpoint &position) const
Gets the column of line of the character at the position.
Definition: text.cpp:326
ttext & set_maximum_width(int width)
Definition: text.cpp:446
virtual void handle_key_right_arrow(SDLMod modifier, bool &handled)
Right arrow key pressed.
Definition: text.cpp:220
std::string get_value() const
Definition: text.hpp:76
size_t get_selection_start() const
Definition: text.hpp:218
void select_all()
Selects all text.
Definition: text.hpp:135
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
tpoint get_column_line(const tpoint &position) const
Definition: text.hpp:186
std::function< void(ttext_ *textbox, const std::string text)> text_changed_callback_
Text changed callback.
Definition: text.hpp:438
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
virtual void handle_key_backspace(SDLMod modifier, bool &handled)
Backspace key pressed.
Definition: text.cpp:256
GLintptr offset
Definition: glew.h:1650
void goto_end_of_data(const bool select=false)
Moves the cursor to the end of all text.
Definition: text.hpp:110
virtual void insert_char(const utf8::string &unicode)
Inserts a character at the cursor.
Definition: text.cpp:135
tstate state_
Current state of the widget.
Definition: text.hpp:249
size_t selection_start_
Start of the selected text.
Definition: text.hpp:255
void goto_start_of_data(const bool select=false)
Moves the cursor to the beginning of the data.
Definition: text.hpp:129
virtual void handle_key_clear_line(SDLMod modifier, bool &handled)=0
Clears the current line.
ttext & set_maximum_height(int height, bool multiline)
Definition: text.cpp:491
size_t get_length() const
Gets the length of the text in characters.
Definition: text.hpp:202
size_t get_length() const
Definition: text.hpp:62
gui2::tpoint get_cursor_position(const unsigned column, const unsigned line=0) const
Gets the location for the cursor.
Definition: text.cpp:235
virtual void handle_key_up_arrow(SDLMod modifier, bool &handled)=0
Every key can have several behaviors.
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
virtual void handle_key_delete(SDLMod modifier, bool &handled)
Delete key pressed.
Definition: text.cpp:269
virtual unsigned get_state() const override
See tcontrol::get_state.
Definition: text.cpp:68
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
virtual void handle_key_down_arrow(SDLMod modifier, bool &handled)=0
Down arrow key pressed.
virtual void goto_start_of_line(const bool select=false)=0
Moves the cursor to the beginning of the line.
virtual void handle_key_default(bool &handled, SDLKey key, SDLMod modifier, const utf8::string &unicode)
Default key handler if none of the above functions is called.
Definition: text.cpp:282
size_t get_selection_length() const
Definition: text.hpp:224
void signal_handler_receive_keyboard_focus(const event::tevent event)
Definition: text.cpp:436
Holds a 2D point.
Definition: point.hpp:24
virtual void handle_key_page_down(SDLMod, bool &)
Page down key.
Definition: text.hpp:406
virtual void handle_key_home(SDLMod modifier, bool &handled)
Home key pressed.
Definition: text.cpp:232
virtual void handle_key_page_up(SDLMod, bool &)
Page up key.
Definition: text.hpp:394
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
Base class for all visible items.
Definition: control.hpp:34
void set_selection_start(const size_t selection_start)
Definition: text.cpp:184
void signal_handler_middle_button_click(const event::tevent event, bool &handled)
Definition: text.cpp:296
gui2::tpoint get_cursor_position(const unsigned column, const unsigned line=0) const
Definition: text.hpp:180
cl_event event
Definition: glew.h:3070
void set_maximum_length(const size_t maximum_length)
Definition: text.cpp:73
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
virtual bool get_active() const override
See tcontrol::get_active.
Definition: text.cpp:63
const std::string & text() const
Definition: text.hpp:81
virtual void goto_end_of_line(const bool select=false)=0
Moves the cursor to the end of the line.
void set_state(const tstate state)
Definition: text.cpp:200
const int font_size
virtual void copy_selection(const bool mouse)
Copies the current selection.
Definition: text.cpp:148
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Text class.
Definition: text.hpp:66
Abstract base class for text items.
Definition: text.hpp:43
ttext & set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
Definition: text.cpp:510
std::string string
void set_cursor(const size_t offset, const bool select)
Moves the cursor at the wanted position.
Definition: text.cpp:108
void set_text_changed_callback(std::function< void(ttext_ *textbox, const std::string text)> cb)
Set the text_changed callback.
Definition: text.hpp:87
void set_maximum_height(const int height, const bool multiline)
Definition: text.hpp:206