The Battle for Wesnoth  1.19.2+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 public:
123  /** Inherited from text_box_base. */
124  void goto_end_of_line(const bool select = false) override
125  {
128  }
129 
130  /** Inherited from text_box_base. */
131  void goto_start_of_line(const bool select = false) override
132  {
135  }
136 
137  /** Inherited from text_box_base. */
138  void goto_end_of_data(const bool select = false) override
139  {
141  update_layout();
142  }
143 
144  /** Inherited from text_box_base. */
145  void goto_start_of_data(const bool select = false) override
146  {
148  update_layout();
149  }
150 
151 private:
152  /** Inherited from text_box_base. */
153  void paste_selection(const bool mouse) override
154  {
156  update_layout();
157  }
158 
159  /** Inherited from text_box_base. */
160  void delete_char(const bool before_cursor) override;
161 
162  /** Inherited from text_box_base. */
163  void delete_selection() override;
164 
165  void handle_mouse_selection(point mouse, const bool start_selection);
166 
167 private:
168  /** The history text for this widget. */
170 
171  /** The maximum length of the text input. */
172  std::size_t max_input_length_;
173 
174  /**
175  * The x offset in the widget where the text starts.
176  *
177  * This value is needed to translate a location in the widget to a location
178  * in the text.
179  */
180  unsigned text_x_offset_;
181 
182  /**
183  * The y offset in the widget where the text starts.
184  *
185  * Needed to determine whether a click is on the text.
186  */
187  unsigned text_y_offset_;
188 
189  /**
190  * The height of the text itself.
191  *
192  * Needed to determine whether a click is on the text.
193  */
194  unsigned text_height_;
195 
196  /** Updates text_x_offset_ and text_y_offset_. */
197  void update_offsets();
198 
199  /** Is the mouse in dragging mode, this affects selection in mouse move */
200  bool dragging_;
201 
202  /** Helper text to display (such as "Search") if the text box is empty. */
203  std::string hint_text_;
204 
205  /** Image (such as a magnifying glass) that accompanies the help text. */
206  std::string hint_image_;
207 
208  /** Line number of text */
209  unsigned line_num_;
210 
211  /** utility function to calculate and set line_num_ from offset */
213  unsigned get_line_num_from_offset(unsigned offset);
214 
215  /** Utility function to calculate the offset of the end of the line
216  */
217  unsigned get_line_end_offset(unsigned line_no);
218  /** Utility function to calculate the offset of the end of the line
219  */
220  unsigned get_line_start_offset(unsigned line_no);
221 
222  /** Update layout. To be called when text size changes */
223  void update_layout() {
224  set_label(get_value());
226  }
227 
228 
229  /**
230  * Inherited from text_box_base.
231  *
232  * Unmodified Handled.
233  * Control Ignored.
234  * Shift Ignored.
235  * Alt Ignored.
236  */
237  void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
238 
239  /**
240  * Inherited from text_box_base.
241  *
242  * Unmodified Handled.
243  * Control Ignored.
244  * Shift Ignored.
245  * Alt Ignored.
246  */
247  void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) override;
248 
249  /**
250  * Inherited from text_box_base.
251  *
252  * Unmodified Handled.
253  * Control Ignored.
254  * Shift Ignored.
255  * Alt Ignored.
256  */
257  void handle_key_left_arrow(SDL_Keymod modifier, bool& handled) override
258  {
259  text_box_base::handle_key_left_arrow(modifier, handled);
260  update_layout();
261  }
262 
263  /**
264  * Inherited from text_box_base.
265  *
266  * Unmodified Handled.
267  * Control Ignored.
268  * Shift Ignored.
269  * Alt Ignored.
270  */
271  void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override
272  {
273  text_box_base::handle_key_right_arrow(modifier, handled);
274  update_layout();
275  }
276 
277  /**
278  * Goes one item up in the history.
279  *
280  * @returns True if there's a history, false otherwise.
281  */
282  bool history_up();
283 
284  /**
285  * Goes one item down in the history.
286  *
287  * @returns True if there's a history, false otherwise.
288  */
289  bool history_down();
290 
291  /** Inherited from text_box_base. */
292  void handle_key_tab(SDL_Keymod modifier, bool& handled) override;
293 
294  /** Inherited from text_box_base. */
295  void handle_key_enter(SDL_Keymod modifier, bool& handled) override;
296 
297  /** Inherited from text_box_base. */
298  void handle_key_clear_line(SDL_Keymod modifier, bool& handled) override;
299 
300 public:
301  /** Static type getter that does not rely on the widget being constructed. */
302  static const std::string& type();
303 
304 private:
305 
306  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
307  virtual const std::string& get_control_type() const override;
308 
309  /***** ***** ***** signal handlers ***** ****** *****/
310 
312  bool& handled,
313  const point& coordinate);
314 
316  bool& handled);
317 
319  bool& handled);
320 
322  bool& handled);
323 };
324 
325 // }---------- DEFINITION ---------{
326 
328 {
329  explicit multiline_text_definition(const config& cfg);
330 
332  {
333  explicit resolution(const config& cfg);
334 
337  };
338 };
339 
340 // }---------- BUILDER -----------{
341 
342 namespace implementation
343 {
344 
346 {
347 public:
348  explicit builder_multiline_text(const config& cfg);
349 
351 
352  virtual std::unique_ptr<widget> build() const override;
353 
354  std::string history;
355 
356  std::size_t max_input_length;
357 
359  std::string hint_image;
360 
361  bool editable;
362  bool wrap;
363 };
364 
365 } // namespace implementation
366 
367 // }------------ END --------------
368 
369 } // 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:40
void push(const std::string &text)
Push string into the history.
Definition: text_box.cpp:47
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