The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
textbox.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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"
21
22#include "scrollarea.hpp"
23
24namespace gui {
25
26class textbox : public scrollarea
27{
28public:
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
55protected:
56 // scrollarea overrides
57 virtual void draw_contents() override;
58 virtual void update_location(const rect& rect) override;
59 virtual void set_inner_location(const rect& ) override;
60 virtual void scroll(unsigned int pos) override;
61
62private:
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_;
76
79 std::vector<int> char_x_, char_y_;
80
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_;
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:447
double alpha_focus_
Definition textbox.hpp:96
bool show_scrollbar() const
int show_cursor_at_
Definition textbox.hpp:88
virtual void set_inner_location(const rect &) override
Definition textbox.cpp:70
virtual void scroll(unsigned int pos) override
Definition textbox.cpp:289
void set_wrap(bool val)
Definition textbox.cpp:280
void scroll_to_bottom()
Definition textbox.cpp:275
const std::string text() const
Definition textbox.cpp:77
void erase_selection()
Definition textbox.cpp:392
void set_cursor_pos(const int cursor_pos)
Definition textbox.cpp:148
void set_selection(const int selstart, const int selend)
Definition textbox.cpp:133
std::size_t line_height_
Definition textbox.hpp:93
bool editable() const
Definition textbox.cpp:260
void pass_event_to_target(const SDL_Event &event)
Definition textbox.cpp:744
bool show_cursor_
Definition textbox.hpp:83
void set_text(const std::string &text, const color_t &color=font::NORMAL_COLOR)
Definition textbox.cpp:84
virtual void draw_contents() override
Definition textbox.cpp:184
virtual ~textbox()
Definition textbox.cpp:55
void draw_cursor(int pos) const
Definition textbox.cpp:163
void set_font_size(int fs)
Definition textbox.cpp:270
void set_editable(bool value)
Definition textbox.cpp:255
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:120
void handle_event(const SDL_Event &event, bool was_forwarded)
Definition textbox.cpp:621
bool is_selection()
Definition textbox.cpp:387
bool handle_key_down(const SDL_Event &event)
Definition textbox.cpp:471
std::vector< int > char_x_
Definition textbox.hpp:79
bool requires_event_focus(const SDL_Event *event=nullptr) const override
Definition textbox.cpp:413
bool listening_
Definition textbox.hpp:104
int font_size() const
Definition textbox.cpp:265
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:177
void set_edit_target(textbox *target)
Definition textbox.cpp:751
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:96
void update_text_cache(bool reset=false, const color_t &color=font::NORMAL_COLOR)
Definition textbox.cpp:363
std::vector< int > char_y_
Definition textbox.hpp:79
std::size_t max_size_
Definition textbox.hpp:65
virtual void update_location(const rect &rect) override
Definition textbox.cpp:63
texture add_text_line(const std::u32string &text, const color_t &color=font::NORMAL_COLOR)
Definition textbox.cpp:295
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.
const std::size_t max_size
Definition sound.cpp:145
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
An abstract description of a rectangle with integer coordinates.
Definition rect.hpp:49