16 #define GETTEXT_DOMAIN "wesnoth-lib"
30 #define WRN_DP LOG_STREAM(warn, log_display)
31 #define DBG_G LOG_STREAM(debug, lg::general())
35 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)
36 :
scrollarea(auto_join), max_size_(max_size), font_size_(font_size), text_(
unicode_cast<std::u32string>(text)),
37 cursor_(text_.
size()), selstart_(-1), selend_(-1),
38 grabmouse_(false), text_pos_(0), editable_(editable),
39 show_cursor_(true), show_cursor_at_(0), text_image_(nullptr),
40 wrap_(false), line_height_(0), yscroll_(0), alpha_(alpha),
41 alpha_focus_(alpha_focus),
75 const std::string &ret = unicode_cast<std::string>(
text_);
82 text_ = unicode_cast<std::u32string>(
text);
104 const std::u32string& wtext = unicode_cast<std::u32string>(
text);
105 text_.insert(
text_.end(), wtext.begin(), wtext.end());
134 if (selstart < 0 || selend < 0 || std::size_t(selstart) >
text_.size() ||
135 std::size_t(selend) >
text_.size()) {
136 WRN_DP <<
"out-of-boundary selection";
149 if (cursor_pos < 0 || std::size_t(cursor_pos) >
text_.size()) {
150 WRN_DP <<
"out-of-boundary selection";
198 SDL_Rect dest{loc.x, loc.y,
src.w,
src.h};
211 while(starty <= endy) {
212 const std::size_t right = starty == endy ? endx :
text_image_.
w();
213 if(right <= std::size_t(startx)) {
217 rect r(loc.x + startx
218 , loc.y + starty -
src.y
307 std::string visible_string;
308 std::u32string wrapped_text;
310 std::u32string::const_iterator backup_itor =
text.end();
312 std::u32string::const_iterator itor =
text.begin();
313 while(itor !=
text.end()) {
315 if(
char(*itor) ==
' ') {
318 visible_string.append(unicode_cast<std::string>(*itor));
320 if(
char(*itor) ==
'\n') {
321 backup_itor =
text.end();
328 if(backup_itor !=
text.end()) {
329 int backup = itor - backup_itor;
330 itor = backup_itor + 1;
334 wrapped_text.erase(wrapped_text.end()-backup, wrapped_text.end());
337 if (visible_string == std::string(
"").append(unicode_cast<std::string>(*itor))) {
341 backup_itor =
text.end();
342 wrapped_text.push_back(char32_t(
'\n'));
347 wrapped_text.push_back(*itor);
354 const std::string
s = unicode_cast<std::string>(wrapped_text);
400 const unsigned int copypaste_modifier =
402 KMOD_LGUI | KMOD_RGUI
414 if(event ==
nullptr) {
419 if(event->type == SDL_KEYDOWN) {
420 SDL_Keycode key =
event->key.keysym.sym;
445 bool changed =
false;
446 std::string str =
event.text.text;
447 std::u32string
s = unicode_cast<std::u32string>(str);
449 DBG_G <<
"Char: " << str;
469 bool changed =
false;
471 const SDL_Keysym& key =
reinterpret_cast<const SDL_KeyboardEvent&
>(event).keysym;
472 const SDL_Keymod modifiers = SDL_GetModState();
474 const int c = key.sym;
475 const int old_cursor =
cursor_;
483 if(
c == SDLK_RIGHT &&
cursor_ <
static_cast<int>(
text_.size()))
487 if(
c == SDLK_END || (
c == SDLK_e && (modifiers & KMOD_CTRL)))
490 if(
c == SDLK_HOME || (
c == SDLK_a && (modifiers & KMOD_CTRL)))
493 if((old_cursor !=
cursor_) && (modifiers & KMOD_SHIFT)) {
498 }
else if(
c == SDLK_LEFT ||
c == SDLK_RIGHT ||
c == SDLK_END ||
c == SDLK_HOME) {
503 if(
c == SDLK_BACKSPACE) {
513 if(
c == SDLK_u && (modifiers & KMOD_CTRL)) {
519 if(
c == SDLK_DELETE && !
text_.empty()) {
529 }
else if(
c == SDLK_BACKSPACE ||
c == SDLK_DELETE || (
c == SDLK_u && (modifiers & KMOD_CTRL))) {
535 if(!(
c == SDLK_UP ||
c == SDLK_DOWN ||
c == SDLK_LEFT ||
c == SDLK_RIGHT ||
536 c == SDLK_DELETE ||
c == SDLK_BACKSPACE ||
c == SDLK_END ||
c == SDLK_HOME ||
537 c == SDLK_PAGEUP ||
c == SDLK_PAGEDOWN)) {
538 if((event.key.keysym.mod & copypaste_modifier)
541 && !(event.key.keysym.mod & KMOD_ALT)
561 std::u32string
s = unicode_cast<std::u32string>(str);
579 const std::size_t beg = std::min<std::size_t>(std::size_t(
selstart_),std::size_t(
selend_));
580 const std::size_t end = std::max<std::size_t>(std::size_t(
selstart_),std::size_t(
selend_));
582 std::u32string ws(
text_.begin() + beg,
text_.begin() + end);
583 std::string
s = unicode_cast<std::string>(ws);
596 std::u32string ws(
text_.begin() + beg,
text_.begin() + end);
597 std::string
s = unicode_cast<std::string>(ws);
627 bool changed =
false;
630 const int old_selend =
selend_;
635 WRN_DP <<
"out-of-boundary selection";
641 if(!(mousebuttons & SDL_BUTTON(1))) {
647 const bool mouse_inside = loc.
contains(mousex, mousey);
659 bool clicked_inside = !
mouse_locked() && (
event.type == SDL_MOUSEBUTTONDOWN
660 && (mousebuttons & SDL_BUTTON(1))
667 const int y = mousey - loc.y;
671 for(
unsigned int i = 1;
i <
char_x_.size(); ++
i) {
691 if(!
grabmouse_ && (mousebuttons & SDL_BUTTON(1))) {
694 }
else if (! (mousebuttons & SDL_BUTTON(1))) {
703 if(!was_forwarded &&
focus(&event) ==
false) {
710 const int old_cursor =
cursor_;
712 if (event.type == SDL_TEXTINPUT &&
listening_) {
714 }
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.
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.
int pango_line_width(const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style=font::pango_text::STYLE_NORMAL)
Determine the width 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(const std::string &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.
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
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)