16 #define GETTEXT_DOMAIN "wesnoth-lib"
29 #include <SDL2/SDL_timer.h>
32 #define WRN_DP LOG_STREAM(warn, log_display)
33 #define DBG_G LOG_STREAM(debug, lg::general())
37 textbox::textbox(
int width,
const std::string& text,
bool editable, std::size_t max_size,
int font_size,
double alpha,
double alpha_focus,
const bool auto_join)
38 :
scrollarea(auto_join), max_size_(max_size), font_size_(font_size), text_(
unicode_cast<std::u32string>(text)),
39 cursor_(text_.
size()), selstart_(-1), selend_(-1),
40 grabmouse_(false), text_pos_(0), editable_(editable),
41 show_cursor_(true), show_cursor_at_(0), text_image_(nullptr),
42 wrap_(false), line_height_(0), yscroll_(0), alpha_(alpha),
43 alpha_focus_(alpha_focus),
77 const std::string &ret = unicode_cast<std::string>(
text_);
84 text_ = unicode_cast<std::u32string>(
text);
106 const std::u32string& wtext = unicode_cast<std::u32string>(
text);
107 text_.insert(
text_.end(), wtext.begin(), wtext.end());
136 if (selstart < 0 || selend < 0 || std::size_t(selstart) >
text_.size() ||
137 std::size_t(selend) >
text_.size()) {
138 WRN_DP <<
"out-of-boundary selection";
151 if (cursor_pos < 0 || std::size_t(cursor_pos) >
text_.size()) {
152 WRN_DP <<
"out-of-boundary selection";
213 while(starty <= endy) {
214 const std::size_t right = starty == endy ? endx :
text_image_.
w();
215 if(right <= std::size_t(startx)) {
309 std::string visible_string;
310 std::u32string wrapped_text;
312 std::u32string::const_iterator backup_itor =
text.end();
314 std::u32string::const_iterator itor =
text.begin();
315 while(itor !=
text.end()) {
317 if(
char(*itor) ==
' ') {
320 visible_string.append(unicode_cast<std::string>(*itor));
322 if(
char(*itor) ==
'\n') {
323 backup_itor =
text.end();
330 if(backup_itor !=
text.end()) {
331 int backup = itor - backup_itor;
332 itor = backup_itor + 1;
336 wrapped_text.erase(wrapped_text.end()-backup, wrapped_text.end());
339 if (visible_string == std::string(
"").append(unicode_cast<std::string>(*itor))) {
343 backup_itor =
text.end();
344 wrapped_text.push_back(char32_t(
'\n'));
349 wrapped_text.push_back(*itor);
356 const std::string
s = unicode_cast<std::string>(wrapped_text);
402 const unsigned int copypaste_modifier =
404 KMOD_LGUI | KMOD_RGUI
416 if(event ==
nullptr) {
421 if(event->type == SDL_KEYDOWN) {
422 SDL_Keycode key =
event->key.keysym.sym;
447 bool changed =
false;
448 std::string str =
event.text.text;
449 std::u32string
s = unicode_cast<std::u32string>(str);
451 DBG_G <<
"Char: " << str;
471 bool changed =
false;
473 const SDL_Keysym& key =
reinterpret_cast<const SDL_KeyboardEvent&
>(event).keysym;
474 const SDL_Keymod modifiers = SDL_GetModState();
476 const int c = key.sym;
477 const int old_cursor =
cursor_;
485 if(
c == SDLK_RIGHT &&
cursor_ <
static_cast<int>(
text_.size()))
489 if(
c == SDLK_END || (
c == SDLK_e && (modifiers & KMOD_CTRL)))
492 if(
c == SDLK_HOME || (
c == SDLK_a && (modifiers & KMOD_CTRL)))
495 if((old_cursor !=
cursor_) && (modifiers & KMOD_SHIFT)) {
500 }
else if(
c == SDLK_LEFT ||
c == SDLK_RIGHT ||
c == SDLK_END ||
c == SDLK_HOME) {
505 if(
c == SDLK_BACKSPACE) {
515 if(
c == SDLK_u && (modifiers & KMOD_CTRL)) {
521 if(
c == SDLK_DELETE && !
text_.empty()) {
531 }
else if(
c == SDLK_BACKSPACE ||
c == SDLK_DELETE || (
c == SDLK_u && (modifiers & KMOD_CTRL))) {
537 if(!(
c == SDLK_UP ||
c == SDLK_DOWN ||
c == SDLK_LEFT ||
c == SDLK_RIGHT ||
538 c == SDLK_DELETE ||
c == SDLK_BACKSPACE ||
c == SDLK_END ||
c == SDLK_HOME ||
539 c == SDLK_PAGEUP ||
c == SDLK_PAGEDOWN)) {
540 if((event.key.keysym.mod & copypaste_modifier)
543 && !(event.key.keysym.mod & KMOD_ALT)
563 std::u32string
s = unicode_cast<std::u32string>(str);
581 const std::size_t beg = std::min<std::size_t>(std::size_t(
selstart_),std::size_t(
selend_));
582 const std::size_t end = std::max<std::size_t>(std::size_t(
selstart_),std::size_t(
selend_));
584 std::u32string ws(
text_.begin() + beg,
text_.begin() + end);
585 std::string
s = unicode_cast<std::string>(ws);
598 std::u32string ws(
text_.begin() + beg,
text_.begin() + end);
599 std::string
s = unicode_cast<std::string>(ws);
629 bool changed =
false;
632 const int old_selend =
selend_;
637 WRN_DP <<
"out-of-boundary selection";
643 if(!(mousebuttons & SDL_BUTTON(1))) {
649 const bool mouse_inside =
loc.contains(mousex, mousey);
661 bool clicked_inside = !
mouse_locked() && (
event.type == SDL_MOUSEBUTTONDOWN
662 && (mousebuttons & SDL_BUTTON(1))
669 const int y = mousey -
loc.
y;
673 for(
unsigned int i = 1;
i <
char_x_.size(); ++
i) {
693 if(!
grabmouse_ && (mousebuttons & SDL_BUTTON(1))) {
696 }
else if (! (mousebuttons & SDL_BUTTON(1))) {
705 if(!was_forwarded &&
focus(&event) ==
false) {
706 if (!
mouse_locked() && event.type == SDL_MOUSEMOTION &&
loc.contains(mousex, mousey))
712 const int old_cursor =
cursor_;
714 if (event.type == SDL_TEXTINPUT &&
listening_) {
716 }
else if (event.type == SDL_KEYDOWN) {
bool handle_text_input(const SDL_Event &event)
virtual void update_location(const SDL_Rect &rect) override
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)
virtual void set_inner_location(const SDL_Rect &) override
virtual void scroll(unsigned int pos) override
const std::string text() const
void set_cursor_pos(const int cursor_pos)
void set_selection(const int selstart, const int selend)
void pass_event_to_target(const SDL_Event &event)
void set_text(const std::string &text, const color_t &color=font::NORMAL_COLOR)
virtual void draw_contents() override
void draw_cursor(int pos) const
void set_font_size(int fs)
void set_editable(bool value)
void handle_event(const SDL_Event &event, bool was_forwarded)
bool handle_key_down(const SDL_Event &event)
std::vector< int > char_x_
bool requires_event_focus(const SDL_Event *event=nullptr) const override
virtual void layout() override
Called by draw_manager to validate layout.
void set_edit_target(textbox *target)
void append_text(const std::string &text, bool auto_scroll=false, const color_t &color=font::NORMAL_COLOR)
void update_text_cache(bool reset=false, const color_t &color=font::NORMAL_COLOR)
std::vector< int > char_y_
texture add_text_line(const std::u32string &text, const color_t &color=font::NORMAL_COLOR)
virtual void handle_text_changed(const std::u32string &)
Wrapper class to encapsulate creation and management of an SDL_Texture.
int w() const
The draw-space width of the texture, in pixels.
void reset()
Releases ownership of the managed texture and resets the ptr to null.
void set_src(const rect &r)
Set the source region of the texture used for drawing operations.
int h() const
The draw-space height of the texture, in pixels.
void set_alpha_mod(uint8_t alpha)
Alpha modifier.
void clear_src()
Clear the source region.
Drawing functions, for drawing things on the screen.
static std::string _(const char *str)
Standard logging facilities (interface).
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
void copy_to_clipboard(const std::string &text)
Copies text to the clipboard.
std::string copy_from_clipboard()
Copies text from the clipboard.
clip_setter reduce_clip(const SDL_Rect &clip)
Set the clipping area to the intersection of the current clipping area and the given rectangle.
void fill(const SDL_Rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
void focus_handler(const sdl_handler *ptr)
int get_max_height(unsigned size, font::family_class fclass, pango_text::FONT_STYLE style)
Returns the maximum glyph height of a font, in pixels.
std::pair< int, int > pango_line_size(const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style)
Determine the width and height of a line of text given a certain font size.
texture pango_render_text(const std::string &text, int size, const color_t &color, font::pango_text::FONT_STYLE style, bool use_markup, int max_width)
Returns a SDL texture containing the rendered text.
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
bool isnewline(const char c)
std::string::const_iterator iterator
Contains the SDL_Rect helper code.
Transitional API for porting SDL_ttf-based code to Pango.
rect src
Non-transparent portion of the surface to compose.
The basic class for representing 8-bit RGB or RGBA colour values.
An abstract description of a rectangle with integer coordinates.
static map_location::direction s
static lg::log_domain log_display("display")
ucs4_convert_impl::enableif< TD, typename TS::value_type >::type unicode_cast(const TS &source)