The Battle for Wesnoth  1.15.2+dev
textbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
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 "font/constants.hpp"
19 #include "font/standard_colors.hpp"
20 
21 #include "scrollarea.hpp"
22 
23 namespace gui {
24 
25 class textbox : public scrollarea
26 {
27 public:
28  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);
29  virtual ~textbox();
30 
31  const std::string text() const;
32  void set_text(const std::string& text, const color_t& color =font::NORMAL_COLOR);
33  void append_text(const std::string& text,bool auto_scroll = false, const color_t& color =font::NORMAL_COLOR);
34  void clear();
35 
36  void set_selection(const int selstart, const int selend);
37  void set_cursor_pos(const int cursor_pos);
38 
39  void set_editable(bool value);
40  bool editable() const;
41 
42  int font_size() const;
43  void set_font_size(int fs);
44 
45  void scroll_to_bottom();
46 
47  void set_wrap(bool val);
48 
49  void set_edit_target(textbox* target);
50 
51 protected:
52  virtual void draw_contents();
53  virtual void update_location(const SDL_Rect& rect);
54  virtual void set_inner_location(const SDL_Rect& );
55  virtual void scroll(unsigned int pos);
56 
57 private:
58  virtual void handle_text_changed(const std::u32string&) {}
59 
60  std::size_t max_size_;
61 
63 
64  std::u32string text_;
65 
66  // mutable unsigned int firstOnScreen_;
67  int cursor_;
68  int selstart_;
69  int selend_;
70  bool grabmouse_;
71 
72  int text_pos_;
74  std::vector<int> char_x_, char_y_;
75 
76  bool editable_;
77 
79 
80  //records the time the cursor was shown at last
81  //the cursor should be inverted every 500 ms.
82  //this will be reset when keyboard input events occur
85 
86  bool wrap_;
87 
88  std::size_t line_height_, yscroll_;
89 
90  double alpha_;
91  double alpha_focus_;
92 
94 
95  /* This boolean is used to filter out any TextInput events that are received without
96  * the corresponding KeyPress events. This is needed to avoid a bug when creating a
97  * textbox using a hotkey.
98  * */
99  bool listening_;
100 
101  void handle_event(const SDL_Event& event, bool was_forwarded);
102 
103  void handle_event(const SDL_Event& event);
104 
105  void pass_event_to_target(const SDL_Event& event);
106 
107  void draw_cursor(int pos) const;
108  void update_text_cache(bool reset = false, const color_t& color =font::NORMAL_COLOR);
109  surface add_text_line(const std::u32string& text, const color_t& color =font::NORMAL_COLOR);
110  bool is_selection();
111  void erase_selection();
112 
113  //make it so that only one textbox object can be receiving
114  //events at a time.
115  bool requires_event_focus(const SDL_Event *event=nullptr) const;
116 
117  bool show_scrollbar() const;
118  bool handle_text_input(const SDL_Event& event);
119  bool handle_key_down(const SDL_Event &event);
120 };
121 
122 }
bool is_selection()
Definition: textbox.cpp:397
double alpha_focus_
Definition: textbox.hpp:91
std::vector< int > char_x_
Definition: textbox.hpp:74
bool show_scrollbar() const
virtual void update_location(const SDL_Rect &rect)
Definition: textbox.cpp:58
void set_edit_target(textbox *target)
Definition: textbox.cpp:763
int cursor_pos_
Definition: textbox.hpp:73
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:33
const std::string text() const
Definition: textbox.cpp:73
void pass_event_to_target(const SDL_Event &event)
Definition: textbox.cpp:756
int font_size() const
Definition: textbox.cpp:273
Definition: video.hpp:31
void draw_cursor(int pos) const
Definition: textbox.cpp:176
bool grabmouse_
Definition: textbox.hpp:70
General purpose widgets.
int selstart_
Definition: textbox.hpp:68
virtual void draw_contents()
Definition: textbox.cpp:190
virtual ~textbox()
Definition: textbox.cpp:50
const int SIZE_PLUS
Definition: constants.cpp:26
surface text_image_
Definition: textbox.hpp:84
std::vector< int > char_y_
Definition: textbox.hpp:74
textbox * edit_target_
Definition: textbox.hpp:93
std::size_t max_size_
Definition: textbox.hpp:60
std::u32string text_
Definition: textbox.hpp:64
bool requires_event_focus(const SDL_Event *event=nullptr) const
Definition: textbox.cpp:423
void set_cursor_pos(const int cursor_pos)
Definition: textbox.cpp:161
void append_text(const std::string &text, bool auto_scroll=false, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:92
void set_wrap(bool val)
Definition: textbox.cpp:288
int show_cursor_at_
Definition: textbox.hpp:83
bool handle_text_input(const SDL_Event &event)
Definition: textbox.cpp:457
bool editable() const
Definition: textbox.cpp:268
void set_editable(bool value)
Definition: textbox.cpp:263
void erase_selection()
Definition: textbox.cpp:402
void scroll_to_bottom()
Definition: textbox.cpp:283
bool handle_key_down(const SDL_Event &event)
Definition: textbox.cpp:481
void update_text_cache(bool reset=false, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:373
void set_font_size(int fs)
Definition: textbox.cpp:278
bool listening_
Definition: textbox.hpp:99
const color_t NORMAL_COLOR
int text_pos_
Definition: textbox.hpp:72
bool show_cursor_
Definition: textbox.hpp:78
void handle_event(const SDL_Event &event, bool was_forwarded)
Definition: textbox.cpp:632
void set_selection(const int selstart, const int selend)
Definition: textbox.cpp:146
surface add_text_line(const std::u32string &text, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:303
void clear()
Definition: textbox.cpp:133
int width() const
Definition: widget.cpp:124
virtual void scroll(unsigned int pos)
Definition: textbox.cpp:297
std::size_t yscroll_
Definition: textbox.hpp:88
std::size_t line_height_
Definition: textbox.hpp:88
CVideo & video() const
Definition: widget.hpp:83
void set_text(const std::string &text, const color_t &color=font::NORMAL_COLOR)
Definition: textbox.cpp:80
bool editable_
Definition: textbox.hpp:76
bool wrap_
Definition: textbox.hpp:86
virtual void set_inner_location(const SDL_Rect &)
Definition: textbox.cpp:65
int font_size_
Definition: textbox.hpp:62
virtual void handle_text_changed(const std::u32string &)
Definition: textbox.hpp:58
double alpha_
Definition: textbox.hpp:90