52 bool word = (keycode & WKC_CTRL) != 0;
54 keycode &= ~WKC_SPECIAL_KEYS;
55 if (keycode != WKC_BACKSPACE && keycode != WKC_DELETE)
return false;
57 bool backspace = keycode == WKC_BACKSPACE;
83 start = it.GetByteOffset();
96 this->
buf.erase(start, len);
98 if (backspace) this->
caretpos -=
static_cast<uint16_t
>(len);
130 if (this->
buf.size() + len < this->
max_bytes && this->chars + 1 <= this->max_chars) {
156bool Textbuf::InsertString(std::string_view str,
bool marked, std::optional<size_t>
caret, std::optional<size_t> insert_location, std::optional<size_t> replacement_end)
159 if (insert_location.has_value()) {
160 insertpos =
static_cast<uint16_t
>(*insert_location);
161 if (insertpos >= this->
buf.size())
return false;
163 if (replacement_end.has_value()) {
164 this->
DeleteText(insertpos,
static_cast<uint16_t
>(*replacement_end), str.empty());
170 if (str.empty())
return false;
176 auto cur = view.begin();
177 const auto end = view.end();
183 if (this->
buf.size() + next.GetByteOffset() >= this->max_bytes)
break;
189 bytes =
static_cast<uint16_t
>(cur.GetByteOffset());
191 if (bytes == 0)
return false;
194 if (
caret.has_value()) this->
caretpos = insertpos +
static_cast<uint16_t
>(*caret);
198 this->
markend = insertpos + bytes;
201 this->
buf.insert(insertpos, str.substr(0, bytes));
203 this->chars +=
chars;
205 assert(this->
buf.size() < this->max_bytes);
225 if (!contents.has_value())
return false;
242 this->
buf.erase(from, to - from);
244 auto fixup = [&](uint16_t &pos) {
245 if (pos <= from)
return;
273 if (this->
markend == 0)
return;
291 this->char_iter->SetString(this->
buf);
292 size_t pos = this->char_iter->SetCurPosition(this->
caretpos);
315 this->
markxoffs = std::min(pos.left, end.left);
329 if (this->
caretpos == 0)
return false;
331 size_t pos = this->char_iter->Prev(what);
334 this->
caretpos =
static_cast<uint16_t
>(pos);
346 if (this->
caretpos >= this->
buf.size())
return false;
348 size_t pos = this->char_iter->Next(what);
351 this->
caretpos =
static_cast<uint16_t
>(pos);
366 case WKC_CTRL | WKC_LEFT: {
372 case WKC_CTRL | WKC_RIGHT: {
379 this->char_iter->SetCurPosition(this->
caretpos);
384 this->
caretpos =
static_cast<uint16_t
>(this->
buf.size());
385 this->char_iter->SetCurPosition(this->
caretpos);
421 size_t bytes = std::min<size_t>(this->
max_bytes - 1, text.size());
427 auto it = view.begin();
428 const auto end = view.end();
429 for (
size_t len = 1; len < this->
max_chars && it != end; ++len) ++it;
432 this->
buf.erase(it.GetByteOffset(), std::string::npos);
447 assert(this->
buf.size() < this->max_bytes);
450 this->
caretpos =
static_cast<uint16_t
>(this->
buf.size());
465 bool b = !!(_caret_timer & 0x20);
467 if (b != this->
caret) {
486 case (WKC_CTRL |
'V'):
487 case (WKC_SHIFT | WKC_INSERT):
491 case (WKC_CTRL |
'U'):
496 case WKC_BACKSPACE:
case WKC_DELETE:
497 case WKC_CTRL | WKC_BACKSPACE:
case WKC_CTRL | WKC_DELETE:
501 case WKC_LEFT:
case WKC_RIGHT:
case WKC_END:
case WKC_HOME:
502 case WKC_CTRL | WKC_LEFT:
case WKC_CTRL | WKC_RIGHT:
Class for iterating over different kind of parts of a string.
static const size_t END
Sentinel to indicate end-of-iteration.
IterType
Type of the iterator.
@ ITER_WORD
Iterate over words.
@ ITER_CHARACTER
Iterate over characters (or more exactly grapheme clusters).
Constant span of UTF-8 encoded data.
iterator GetIterAtByte(size_t offset) const
Create iterator pointing at codepoint, which occupies the byte position "offset".
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Functions related to the gfx engine.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
Functions related to laying out the texts.
Types related to the graphics and/or input devices.
@ Normal
Index of the normal font in the font tables.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Functions related to low-level strings.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
TextDirection _current_text_dir
Text direction of the currently selected language.
Functions related to OTTD's strings.
@ TD_LTR
Text is written left-to-right by default.
value_type width
The width of the rectangle.
uint16_t pixels
the current size of the string in pixels
uint16_t caretpos
the current position of the caret in the buffer, in bytes
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
bool MovePos(uint16_t keycode)
Handle text navigation with arrow keys left/right.
uint16_t markxoffs
the start position of the marked area in pixels
void UpdateMarkedText()
Update pixel positions of the marked text area.
void UpdateWidth()
Update pixel width of the text.
bool DeleteChar(uint16_t keycode)
Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete fro...
void DeleteAll()
Delete every character in the textbuffer.
uint16_t max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
std::string_view GetText() const
Get the current text.
uint16_t chars
the current size of the string in characters (including terminating '\0')
bool MovePrev(StringIterator::IterType what)
Move to previous character position.
uint16_t max_chars
the maximum size of the buffer in characters (including terminating '\0')
uint16_t markend
the end position of the marked area in the buffer, in bytes
void DeleteText(uint16_t from, uint16_t to, bool update)
Delete a part of the text.
bool MoveNext(StringIterator::IterType what)
Move to next character position.
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
bool InsertString(std::string_view str, bool marked, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Insert a string into the text buffer.
uint16_t caretxoffs
the current position of the caret in pixels
uint16_t marklength
the length of the marked area in pixels
uint16_t markpos
the start position of the marked area in the buffer, in bytes
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
void Assign(std::string_view text)
Copy a string into the textbuffer.
void DiscardMarkedText(bool update=true)
Discard any marked text.
bool caret
is the caret ("_") visible or not
CharSetFilter afilter
Allowed characters.
bool InsertChar(char32_t key)
Insert a character to a textbuffer.
void UpdateStringIter()
Update the character iter after the text has changed.
bool HandleCaret()
Handle the flashing of the caret.
Textbuf(uint16_t max_bytes, uint16_t max_chars=UINT16_MAX)
Initialize the textbuffer by supplying it the buffer to write into and the maximum length of this buf...
std::string buf
buffer in which text is saved
void UpdateCaretPosition()
Update pixel position of the caret.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
Stuff related to text buffers.
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
@ Confirm
Return or enter key pressed.
@ Cursor
Non-text change, e.g. cursor position.
@ Editing
Textbuf content changed.
@ NotHandled
Key does not affect editboxes.
@ Cancel
Escape key pressed.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
std::pair< char[4], size_t > EncodeUtf8(char32_t c)
Encode a character to UTF-8.
Handling of UTF-8 encoded data.