The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
textbox.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef TEXTBOX_HPP_INCLUDED
16 #define TEXTBOX_HPP_INCLUDED
17 
19 #include "font.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, 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 SDL_Color& color =font::NORMAL_COLOR);
33  void append_text(const std::string& text,bool auto_scroll = false, const SDL_Color& 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 
50 
51 protected:
52  virtual void draw_contents();
53  virtual void update_location(SDL_Rect const &rect);
54  virtual void set_inner_location(SDL_Rect const &);
55  virtual void scroll(unsigned int pos);
56 
57 private:
58  virtual void handle_text_changed(const ucs4::string&) {}
59 
60  size_t max_size_;
61 
63 
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 
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, CVideo &video) const;
108  void update_text_cache(bool reset = false, const SDL_Color& color =font::NORMAL_COLOR);
109  surface add_text_line(const ucs4::string& text, const SDL_Color& 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 }
123 
124 #endif
bool is_selection()
Definition: textbox.cpp:386
double alpha_focus_
Definition: textbox.hpp:91
int font_size() const
Definition: textbox.cpp:262
std::vector< int > char_x_
Definition: textbox.hpp:74
std::vector< char_t > string
virtual void handle_text_changed(const ucs4::string &)
Definition: textbox.hpp:58
void set_edit_target(textbox *target)
Definition: textbox.cpp:719
int cursor_pos_
Definition: textbox.hpp:73
void draw_cursor(int pos, CVideo &video) const
Definition: textbox.cpp:170
int pos
Definition: formula.cpp:800
void pass_event_to_target(const SDL_Event &event)
Definition: textbox.cpp:712
Definition: video.hpp:58
bool grabmouse_
Definition: textbox.hpp:70
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
int selstart_
Definition: textbox.hpp:68
GLuint const GLfloat * val
Definition: glew.h:2614
void set_text(const std::string &text, const SDL_Color &color=font::NORMAL_COLOR)
Definition: textbox.cpp:76
bool editable() const
Definition: textbox.cpp:257
virtual void draw_contents()
Definition: textbox.cpp:183
virtual ~textbox()
Definition: textbox.cpp:50
const std::string text() const
Definition: textbox.cpp:69
textbox(CVideo &video, int width, const std::string &text="", bool editable=true, 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 int SIZE_PLUS
Definition: font.hpp:65
surface text_image_
Definition: textbox.hpp:84
std::vector< int > char_y_
Definition: textbox.hpp:74
void update_text_cache(bool reset=false, const SDL_Color &color=font::NORMAL_COLOR)
Definition: textbox.cpp:362
const SDL_Color NORMAL_COLOR
Definition: font.cpp:564
textbox * edit_target_
Definition: textbox.hpp:93
void set_cursor_pos(const int cursor_pos)
Definition: textbox.cpp:155
size_t max_size_
Definition: textbox.hpp:60
void set_wrap(bool val)
Definition: textbox.cpp:277
int show_cursor_at_
Definition: textbox.hpp:83
bool handle_text_input(const SDL_Event &event)
Definition: textbox.cpp:446
GLsizei const GLfloat * value
Definition: glew.h:1817
void set_editable(bool value)
Definition: textbox.cpp:252
void erase_selection()
Definition: textbox.cpp:391
void scroll_to_bottom()
Definition: textbox.cpp:272
bool handle_key_down(const SDL_Event &event)
Definition: textbox.cpp:470
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1488
void set_font_size(int fs)
Definition: textbox.cpp:267
bool listening_
Definition: textbox.hpp:99
size_t line_height_
Definition: textbox.hpp:88
void append_text(const std::string &text, bool auto_scroll=false, const SDL_Color &color=font::NORMAL_COLOR)
Definition: textbox.cpp:88
GLuint color
Definition: glew.h:5801
int text_pos_
Definition: textbox.hpp:72
GLboolean reset
Definition: glew.h:3799
bool show_cursor_
Definition: textbox.hpp:78
void handle_event(const SDL_Event &event, bool was_forwarded)
Definition: textbox.cpp:601
surface add_text_line(const ucs4::string &text, const SDL_Color &color=font::NORMAL_COLOR)
Definition: textbox.cpp:292
void set_selection(const int selstart, const int selend)
Definition: textbox.cpp:140
virtual void update_location(SDL_Rect const &rect)
Definition: textbox.cpp:54
ucs4::string text_
Definition: textbox.hpp:64
void clear()
Definition: textbox.cpp:127
virtual void scroll(unsigned int pos)
Definition: textbox.cpp:286
size_t yscroll_
Definition: textbox.hpp:88
virtual void set_inner_location(SDL_Rect const &)
Definition: textbox.cpp:61
cl_event event
Definition: glew.h:3070
bool editable_
Definition: textbox.hpp:76
bool requires_event_focus(const SDL_Event *event=nullptr) const
Definition: textbox.cpp:412
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
bool wrap_
Definition: textbox.hpp:86
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool show_scrollbar() const
GLenum target
Definition: glew.h:5190
int font_size_
Definition: textbox.hpp:62
double alpha_
Definition: textbox.hpp:90