The Battle for Wesnoth  1.19.0-dev
multiline_text.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2024
3  by babaissarkar(Subhraman Sarkar) <suvrax@gmail.com>
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  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 #pragma once
16 
18 #include "gui/widgets/text_box.hpp"
19 #include "gui/widgets/window.hpp"
20 
21 namespace gui2
22 {
23 namespace implementation
24 {
25 struct builder_multiline_text;
26 }
27 
28 // ------------ WIDGET -----------{
29 
31 {
33 
34 public:
36 
37  /** See @ref widget::can_wrap. */
38  bool can_wrap() const override
39  {
40  return false;
41  }
42 
43  /** Saves the text in the widget to the history. */
45  {
47  }
48 
49  /***** ***** ***** setters / getters for members ***** ****** *****/
50 
51  void set_history(const std::string& id)
52  {
54  }
55 
56  void set_max_input_length(const std::size_t length)
57  {
58  max_input_length_ = length;
59  }
60 
61  void set_hint_data(const std::string& text, const std::string& image)
62  {
63  hint_text_ = text;
65 
66  update_canvas();
67  }
68 
69  void clear()
70  {
71  set_value("");
72  }
73 
74  unsigned get_line_no()
75  {
77  return line_num_;
78  }
79 
81  {
83  }
84 
86  {
88  }
89 
91  {
93  }
94 
95 protected:
96  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
97 
98  /** See @ref widget::place. */
99  virtual void place(const point& origin, const point& size) override;
100 
101  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
102 
103  /** See @ref styled_widget::update_canvas. */
104  virtual void update_canvas() override;
105 
106  /** Inherited from text_box_base. */
107  void insert_char(const std::string& unicode) override
108  {
110  update_layout();
111  }
112 
113  /** Inherited from text_box_base */
114  void set_cursor(const std::size_t offset, const bool select) override
115  {
116  text_box_base::set_cursor(offset, select);
118  // Whenever cursor moves, this tells scroll_text to update the scrollbars
119  update_layout();
120  }
121 
122  /** Inherited from text_box_base. */
123  void goto_end_of_line(const bool select = false) override
124  {
127  }
128 
129  /** Inherited from text_box_base. */
130  void goto_start_of_line(const bool select = false) override
131  {
134  }
135 
136  /** Inherited from text_box_base. */
137  void goto_end_of_data(const bool select = false) override
138  {
140  update_layout();
141  }
142 
143  /** Inherited from text_box_base. */
144  void goto_start_of_data(const bool select = false) override
145  {
147  update_layout();
148  }
149 
150  /** Inherited from text_box_base. */
151  void paste_selection(const bool mouse) override
152  {
154  update_layout();
155  }
156 
157  /** Inherited from text_box_base. */
158  void delete_char(const bool before_cursor) override;
159 
160  /** Inherited from text_box_base. */
161  void delete_selection() override;
162 
163  void handle_mouse_selection(point mouse, const bool start_selection);
164 
165 private:
166  /** The history text for this widget. */
168 
169  /** The maximum length of the text input. */
170  std::size_t max_input_length_;
171 
172  /**
173  * The x offset in the widget where the text starts.
174  *
175  * This value is needed to translate a location in the widget to a location
176  * in the text.
177  */
178  unsigned text_x_offset_;
179 
180  /**
181  * The y offset in the widget where the text starts.
182  *
183  * Needed to determine whether a click is on the text.
184  */
185  unsigned text_y_offset_;
186 
187  /**
188  * The height of the text itself.
189  *
190  * Needed to determine whether a click is on the text.
191  */
192  unsigned text_height_;
193 
194  /** Updates text_x_offset_ and text_y_offset_. */
195  void update_offsets();
196 
197  /** Is the mouse in dragging mode, this affects selection in mouse move */
198  bool dragging_;
199 
200  /** Helper text to display (such as "Search") if the text box is empty. */
201  std::string hint_text_;
202 
203  /** Image (such as a magnifying glass) that accompanies the help text. */
204  std::string hint_image_;
205 
206  /** Line number of text */
207  unsigned line_num_;
208 
209  /** utility function to calculate and set line_num_ from offset */
211  unsigned get_line_num_from_offset(unsigned offset);
212 
213  /** Utility function to calculate the offset of the end of the line
214  */
215  unsigned get_line_end_offset(unsigned line_no);
216  /** Utility function to calculate the offset of the end of the line
217  */
218  unsigned get_line_start_offset(unsigned line_no);
219 
220  /** Update layout. To be called when text size changes */
221  void update_layout() {
222  set_label(get_value());
224  }
225 
226 
227  /**
228  * Inherited from text_box_base.
229  *
230  * Unmodified Handled.
231  * Control Ignored.
232  * Shift Ignored.
233  * Alt Ignored.
234  */
235  void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
236 
237  /**
238  * Inherited from text_box_base.
239  *
240  * Unmodified Handled.
241  * Control Ignored.
242  * Shift Ignored.
243  * Alt Ignored.
244  */
245  void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
246 
247  /**
248  * Inherited from text_box_base.
249  *
250  * Unmodified Handled.
251  * Control Ignored.
252  * Shift Ignored.
253  * Alt Ignored.
254  */
255  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override
256  {
257  text_box_base::handle_key_left_arrow(modifier, handled);
258  update_layout();
259  }
260 
261  /**
262  * Inherited from text_box_base.
263  *
264  * Unmodified Handled.
265  * Control Ignored.
266  * Shift Ignored.
267  * Alt Ignored.
268  */
269  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override
270  {
271  text_box_base::handle_key_right_arrow(modifier, handled);
272  update_layout();
273  }
274 
275  /**
276  * Goes one item up in the history.
277  *
278  * @returns True if there's a history, false otherwise.
279  */
280  bool history_up();
281 
282  /**
283  * Goes one item down in the history.
284  *
285  * @returns True if there's a history, false otherwise.
286  */
287  bool history_down();
288 
289  /** Inherited from text_box_base. */
290  void handle_key_tab(SDL_Keymod modifier, bool& handled) override;
291 
292  /** Inherited from text_box_base. */
293  void handle_key_enter(SDL_Keymod modifier, bool& handled) override;
294 
295  /** Inherited from text_box_base. */
296  void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
297 
298 public:
299  /** Static type getter that does not rely on the widget being constructed. */
300  static const std::string& type();
301 
302 private:
303 
304  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
305  virtual const std::string& get_control_type() const override;
306 
307  /***** ***** ***** signal handlers ***** ****** *****/
308 
310  bool& handled,
311  const point& coordinate);
312 
314  bool& handled);
315 
317  bool& handled);
318 
320  bool& handled);
321 };
322 
323 // }---------- DEFINITION ---------{
324 
326 {
327  explicit multiline_text_definition(const config& cfg);
328 
330  {
331  explicit resolution(const config& cfg);
332 
335  };
336 };
337 
338 // }---------- BUILDER -----------{
339 
340 namespace implementation
341 {
342 
344 {
345 public:
346  explicit builder_multiline_text(const config& cfg);
347 
349 
350  virtual std::unique_ptr<widget> build() const override;
351 
352  std::string history;
353 
354  std::size_t max_input_length;
355 
357  std::string hint_image;
358 
359  bool editable;
360  bool wrap;
361 };
362 
363 } // namespace implementation
364 
365 // }------------ END --------------
366 
367 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
void handle_key_down_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
void paste_selection(const bool mouse) override
Inherited from text_box_base.
bool dragging_
Is the mouse in dragging mode, this affects selection in mouse move.
unsigned line_num_
Line number of text.
unsigned text_y_offset_
The y offset in the widget where the text starts.
unsigned text_x_offset_
The x offset in the widget where the text starts.
virtual void place(const point &origin, const point &size) override
See widget::place.
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
void set_history(const std::string &id)
void set_max_input_length(const std::size_t length)
bool history_up()
Goes one item up in the history.
void delete_selection() override
Inherited from text_box_base.
unsigned text_height_
The height of the text itself.
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
bool can_wrap() const override
See widget::can_wrap.
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
void signal_handler_mouse_motion(const event::ui_event event, bool &handled, const point &coordinate)
void update_offsets()
Updates text_x_offset_ and text_y_offset_.
void handle_key_enter(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void goto_start_of_data(const bool select=false) override
Inherited from text_box_base.
bool history_down()
Goes one item down in the history.
unsigned get_line_end_offset(unsigned line_no)
Utility function to calculate the offset of the end of the line.
void handle_mouse_selection(point mouse, const bool start_selection)
std::string hint_text_
Helper text to display (such as "Search") if the text box is empty.
unsigned get_line_num_from_offset(unsigned offset)
void save_to_history()
Saves the text in the widget to the history.
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
void delete_char(const bool before_cursor) override
Inherited from text_box_base.
virtual void update_canvas() override
See styled_widget::update_canvas.
std::string hint_image_
Image (such as a magnifying glass) that accompanies the help text.
void goto_end_of_line(const bool select=false) override
Inherited from text_box_base.
void handle_key_tab(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
unsigned get_line_start_offset(unsigned line_no)
Utility function to calculate the offset of the end of the line.
void handle_key_clear_line(SDL_Keymod modifier, bool &handled) override
Inherited from text_box_base.
void goto_start_of_line(const bool select=false) override
Inherited from text_box_base.
std::size_t max_input_length_
The maximum length of the text input.
void signal_handler_left_button_double_click(const event::ui_event event, bool &handled)
void update_layout()
Update layout.
multiline_text(const implementation::builder_styled_widget &builder)
void set_cursor(const std::size_t offset, const bool select) override
Inherited from text_box_base.
void handle_key_up_arrow(SDL_Keymod, bool &handled) override
Inherited from text_box_base.
void insert_char(const std::string &unicode) override
Inherited from text_box_base.
void set_hint_data(const std::string &text, const std::string &image)
void goto_end_of_data(const bool select=false) override
Inherited from text_box_base.
text_history history_
The history text for this widget.
void set_line_num_from_offset()
utility function to calculate and set line_num_ from offset
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
virtual void set_label(const t_string &text)
Abstract base class for text items.
std::size_t get_length() const
Wrapper function, returns length of the text in pango column offsets.
virtual void paste_selection(const bool mouse)
Pastes the current selection.
virtual void insert_char(const std::string &unicode)
Inserts a character at the cursor.
virtual void handle_key_left_arrow(SDL_Keymod modifier, bool &handled)
Left arrow key pressed.
std::string get_value() const
const std::string & text() const
point get_cursor_position(const unsigned column, const unsigned line=0) const
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void handle_key_right_arrow(SDL_Keymod modifier, bool &handled)
Right arrow key pressed.
virtual void set_cursor(const std::size_t offset, const bool select)
Moves the cursor at the wanted position.
virtual void goto_end_of_data(const bool select=false)
Moves the cursor to the end of all text.
std::size_t get_selection_start() const
virtual void goto_start_of_data(const bool select=false)
Moves the cursor to the beginning of the data.
Class for text input history.
Definition: text_box.hpp:37
static text_history get_history(const std::string &id, const bool enabled)
Gets history that matches id.
Definition: text_box.cpp:38
void push(const std::string &text)
Push string into the history.
Definition: text_box.cpp:45
window * get_window()
Get the parent window.
Definition: widget.cpp:117
void invalidate_layout()
Updates the size of the window.
Definition: window.cpp:773
This file contains the window object, this object is a top level container which has the event manage...
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 override
virtual std::unique_ptr< widget > build() const=0
multiline_text_definition(const config &cfg)
Holds a 2D point.
Definition: point.hpp:25