The Battle for Wesnoth  1.19.7+dev
textbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
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 #include "font/constants.hpp"
20 #include "font/standard_colors.hpp"
21 
22 #include "scrollarea.hpp"
23 
24 namespace gui {
25 
26 class textbox : public scrollarea
27 {
28 public:
29  textbox(int width, const std::string& text="", bool editable=true, std::size_t max_size = 256, int font_size = font::SIZE_PLUS, double alpha = 0.4, double alpha_focus = 0.2, const bool auto_join = true);
30  virtual ~textbox();
31 
32  const std::string text() const;
33  void set_text(const std::string& text, const color_t& color =font::NORMAL_COLOR);
34  void append_text(const std::string& text,bool auto_scroll = false, const color_t& color =font::NORMAL_COLOR);
35  void clear();
36 
37  void set_selection(const int selstart, const int selend);
38  void set_cursor_pos(const int cursor_pos);
39 
40  void set_editable(bool value);
41  bool editable() const;
42 
43  int font_size() const;
44  void set_font_size(int fs);
45 
46  void scroll_to_bottom();
47 
48  void set_wrap(bool val);
49 
50  void set_edit_target(textbox* target);
51 
52  /** Called by draw_manager to validate layout. */
53  virtual void layout() override;
54 
55 protected:
56  // scrollarea overrides
57  virtual void draw_contents() override;
58  virtual void update_location(const SDL_Rect& rect) override;
59  virtual void set_inner_location(const SDL_Rect& ) override;
60  virtual void scroll(unsigned int pos) override;
61 
62 private:
63  virtual void handle_text_changed(const std::u32string&) {}
64 
65  std::size_t max_size_;
66 
68 
69  std::u32string text_;
70 
71  // mutable unsigned int firstOnScreen_;
72  int cursor_;
73  int selstart_;
74  int selend_;
75  bool grabmouse_;
76 
77  int text_pos_;
79  std::vector<int> char_x_, char_y_;
80 
81  bool editable_;
82 
84 
85  //records the time the cursor was shown at last
86  //the cursor should be inverted every 500 ms.
87  //this will be reset when keyboard input events occur
90 
91  bool wrap_;
92 
93  std::size_t line_height_, yscroll_;
94 
95  double alpha_;
96  double alpha_focus_;
97 
99 
100  /* This boolean is used to filter out any TextInput events that are received without
101  * the corresponding KeyPress events. This is needed to avoid a bug when creating a
102  * textbox using a hotkey.
103  * */
105 
106  void handle_event(const SDL_Event& event, bool was_forwarded);
107 
108  void handle_event(const SDL_Event& event) override;
109 
110  void pass_event_to_target(const SDL_Event& event);
111 
112  void draw_cursor(int pos) const;
113  void update_text_cache(bool reset = false, const color_t& color =font::NORMAL_COLOR);
114  texture add_text_line(const std::u32string& text, const color_t& color =font::NORMAL_COLOR);
115  bool is_selection();
116  void erase_selection();
117 
118  //make it so that only one textbox object can be receiving
119  //events at a time.
120  bool requires_event_focus(const SDL_Event *event=nullptr) const override;
121 
122  bool show_scrollbar() const;
123  bool handle_text_input(const SDL_Event& event);
124  bool handle_key_down(const SDL_Event &event);
125 };
126 
127 }
bool handle_text_input(const SDL_Event &event)
Definition: textbox.cpp:443
double alpha_focus_
Definition: textbox.hpp:96
bool show_scrollbar() const
virtual void update_location(const SDL_Rect &rect) override
Definition: textbox.cpp:59
textbox(int width, const std::string &text="", bool editable=true, std::size_t max_size=256, int font_size=font::SIZE_PLUS, double alpha=0.4, double alpha_focus=0.2, const bool auto_join=true)
Definition: textbox.cpp:35
int font_size_
Definition: textbox.hpp:67
int show_cursor_at_
Definition: textbox.hpp:88
virtual void set_inner_location(const SDL_Rect &) override
Definition: textbox.cpp:66
virtual void scroll(unsigned int pos) override
Definition: textbox.cpp:285
void set_wrap(bool val)
Definition: textbox.cpp:276
void scroll_to_bottom()
Definition: textbox.cpp:271
const std::string text() const
Definition: textbox.cpp:73
void erase_selection()
Definition: textbox.cpp:388
void set_cursor_pos(const int cursor_pos)
Definition: textbox.cpp:144
void set_selection(const int selstart, const int selend)
Definition: textbox.cpp:129
std::size_t line_height_
Definition: textbox.hpp:93
bool editable() const
Definition: textbox.cpp:256
void pass_event_to_target(const SDL_Event &event)
Definition: textbox.cpp:734
bool show_cursor_
Definition: textbox.hpp:83
void set_text(const std::string &text, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:80
virtual void draw_contents() override
Definition: textbox.cpp:180
virtual ~textbox()
Definition: textbox.cpp:51
void draw_cursor(int pos) const
Definition: textbox.cpp:159
void set_font_size(int fs)
Definition: textbox.cpp:266
void set_editable(bool value)
Definition: textbox.cpp:251
bool grabmouse_
Definition: textbox.hpp:75
bool editable_
Definition: textbox.hpp:81
textbox * edit_target_
Definition: textbox.hpp:98
std::size_t yscroll_
Definition: textbox.hpp:93
void clear()
Definition: textbox.cpp:116
void handle_event(const SDL_Event &event, bool was_forwarded)
Definition: textbox.cpp:618
bool wrap_
Definition: textbox.hpp:91
bool is_selection()
Definition: textbox.cpp:383
int selstart_
Definition: textbox.hpp:73
bool handle_key_down(const SDL_Event &event)
Definition: textbox.cpp:467
std::vector< int > char_x_
Definition: textbox.hpp:79
bool requires_event_focus(const SDL_Event *event=nullptr) const override
Definition: textbox.cpp:409
bool listening_
Definition: textbox.hpp:104
int font_size() const
Definition: textbox.cpp:261
std::u32string text_
Definition: textbox.hpp:69
double alpha_
Definition: textbox.hpp:95
virtual void layout() override
Called by draw_manager to validate layout.
Definition: textbox.cpp:173
void set_edit_target(textbox *target)
Definition: textbox.cpp:741
texture text_image_
Definition: textbox.hpp:89
int cursor_pos_
Definition: textbox.hpp:78
void append_text(const std::string &text, bool auto_scroll=false, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:92
void update_text_cache(bool reset=false, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:359
int text_pos_
Definition: textbox.hpp:77
std::vector< int > char_y_
Definition: textbox.hpp:79
std::size_t max_size_
Definition: textbox.hpp:65
texture add_text_line(const std::u32string &text, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:291
virtual void handle_text_changed(const std::u32string &)
Definition: textbox.hpp:63
int width() const
Definition: widget.cpp:113
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
const int SIZE_PLUS
Definition: constants.cpp:29
const color_t NORMAL_COLOR
General purpose widgets.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47