The Battle for Wesnoth  1.17.0-dev
textbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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(CVideo &video, 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 protected:
53  virtual void draw_contents();
54  virtual void update_location(const SDL_Rect& rect);
55  virtual void set_inner_location(const SDL_Rect& );
56  virtual void scroll(unsigned int pos);
57 
58 private:
59  virtual void handle_text_changed(const std::u32string&) {}
60 
61  std::size_t max_size_;
62 
64 
65  std::u32string text_;
66 
67  // mutable unsigned int firstOnScreen_;
68  int cursor_;
69  int selstart_;
70  int selend_;
71  bool grabmouse_;
72 
73  int text_pos_;
75  std::vector<int> char_x_, char_y_;
76 
77  bool editable_;
78 
80 
81  //records the time the cursor was shown at last
82  //the cursor should be inverted every 500 ms.
83  //this will be reset when keyboard input events occur
86 
87  bool wrap_;
88 
89  std::size_t line_height_, yscroll_;
90 
91  double alpha_;
92  double alpha_focus_;
93 
95 
96  /* This boolean is used to filter out any TextInput events that are received without
97  * the corresponding KeyPress events. This is needed to avoid a bug when creating a
98  * textbox using a hotkey.
99  * */
101 
102  void handle_event(const SDL_Event& event, bool was_forwarded);
103 
104  void handle_event(const SDL_Event& event);
105 
106  void pass_event_to_target(const SDL_Event& event);
107 
108  void draw_cursor(int pos) const;
109  void update_text_cache(bool reset = false, const color_t& color =font::NORMAL_COLOR);
110  surface add_text_line(const std::u32string& text, const color_t& color =font::NORMAL_COLOR);
111  bool is_selection();
112  void erase_selection();
113 
114  //make it so that only one textbox object can be receiving
115  //events at a time.
116  bool requires_event_focus(const SDL_Event *event=nullptr) const;
117 
118  bool show_scrollbar() const;
119  bool handle_text_input(const SDL_Event& event);
120  bool handle_key_down(const SDL_Event &event);
121 };
122 
123 }
bool is_selection()
Definition: textbox.cpp:398
double alpha_focus_
Definition: textbox.hpp:92
std::vector< int > char_x_
Definition: textbox.hpp:75
bool show_scrollbar() const
virtual void update_location(const SDL_Rect &rect)
Definition: textbox.cpp:59
void set_edit_target(textbox *target)
Definition: textbox.cpp:764
int cursor_pos_
Definition: textbox.hpp:74
textbox(CVideo &video, 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:34
const std::string text() const
Definition: textbox.cpp:74
void pass_event_to_target(const SDL_Event &event)
Definition: textbox.cpp:757
int font_size() const
Definition: textbox.cpp:274
Definition: video.hpp:32
void draw_cursor(int pos) const
Definition: textbox.cpp:177
bool grabmouse_
Definition: textbox.hpp:71
General purpose widgets.
int selstart_
Definition: textbox.hpp:69
virtual void draw_contents()
Definition: textbox.cpp:191
virtual ~textbox()
Definition: textbox.cpp:51
const int SIZE_PLUS
Definition: constants.cpp:29
surface text_image_
Definition: textbox.hpp:85
std::vector< int > char_y_
Definition: textbox.hpp:75
textbox * edit_target_
Definition: textbox.hpp:94
std::size_t max_size_
Definition: textbox.hpp:61
std::u32string text_
Definition: textbox.hpp:65
bool requires_event_focus(const SDL_Event *event=nullptr) const
Definition: textbox.cpp:424
void set_cursor_pos(const int cursor_pos)
Definition: textbox.cpp:162
void append_text(const std::string &text, bool auto_scroll=false, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:93
void set_wrap(bool val)
Definition: textbox.cpp:289
int show_cursor_at_
Definition: textbox.hpp:84
bool handle_text_input(const SDL_Event &event)
Definition: textbox.cpp:458
bool editable() const
Definition: textbox.cpp:269
void set_editable(bool value)
Definition: textbox.cpp:264
void erase_selection()
Definition: textbox.cpp:403
void scroll_to_bottom()
Definition: textbox.cpp:284
bool handle_key_down(const SDL_Event &event)
Definition: textbox.cpp:482
void update_text_cache(bool reset=false, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:374
void set_font_size(int fs)
Definition: textbox.cpp:279
bool listening_
Definition: textbox.hpp:100
const color_t NORMAL_COLOR
int text_pos_
Definition: textbox.hpp:73
bool show_cursor_
Definition: textbox.hpp:79
void handle_event(const SDL_Event &event, bool was_forwarded)
Definition: textbox.cpp:633
void set_selection(const int selstart, const int selend)
Definition: textbox.cpp:147
surface add_text_line(const std::u32string &text, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:304
void clear()
Definition: textbox.cpp:134
int width() const
Definition: widget.cpp:124
virtual void scroll(unsigned int pos)
Definition: textbox.cpp:298
std::size_t yscroll_
Definition: textbox.hpp:89
std::size_t line_height_
Definition: textbox.hpp:89
CVideo & video() const
Definition: widget.hpp:84
void set_text(const std::string &text, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:81
bool editable_
Definition: textbox.hpp:77
bool wrap_
Definition: textbox.hpp:87
virtual void set_inner_location(const SDL_Rect &)
Definition: textbox.cpp:66
int font_size_
Definition: textbox.hpp:63
virtual void handle_text_changed(const std::u32string &)
Definition: textbox.hpp:59
double alpha_
Definition: textbox.hpp:91