53 bool word = (keycode & WKC_CTRL) != 0;
55 keycode &= ~WKC_SPECIAL_KEYS;
56 if (keycode != WKC_BACKSPACE && keycode != WKC_DELETE)
return false;
58 bool backspace = keycode == WKC_BACKSPACE;
84 start = it.GetByteOffset();
97 this->
buf.erase(start, len);
99 if (backspace) this->
caretpos -=
static_cast<uint16_t
>(len);
131 if (this->
buf.size() + len < this->
max_bytes && this->chars + 1 <= this->max_chars) {
157bool 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)
160 if (insert_location.has_value()) {
161 insertpos =
static_cast<uint16_t
>(*insert_location);
162 if (insertpos >= this->
buf.size())
return false;
164 if (replacement_end.has_value()) {
165 this->
DeleteText(insertpos,
static_cast<uint16_t
>(*replacement_end), str.empty());
171 if (str.empty())
return false;
177 auto cur = view.begin();
178 const auto end = view.end();
184 if (this->
buf.size() + next.GetByteOffset() >= this->max_bytes)
break;
190 bytes =
static_cast<uint16_t
>(cur.GetByteOffset());
192 if (bytes == 0)
return false;
199 this->
markend = insertpos + bytes;
202 this->
buf.insert(insertpos, str.substr(0, bytes));
204 this->chars +=
chars;
206 assert(this->
buf.size() < this->max_bytes);
157bool 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) {
…}
226 if (!contents.has_value())
return false;
243 this->
buf.erase(from, to - from);
245 auto fixup = [&](uint16_t &pos) {
246 if (pos <= from)
return;
274 if (this->
markend == 0)
return;
292 this->char_iter->SetString(this->
buf);
293 size_t pos = this->char_iter->SetCurPosition(this->
caretpos);
316 this->
markxoffs = std::min(pos.left, end.left);
330 if (this->
caretpos == 0)
return false;
332 size_t pos = this->char_iter->Prev(what);
335 this->
caretpos =
static_cast<uint16_t
>(pos);
347 if (this->
caretpos >= this->
buf.size())
return false;
349 size_t pos = this->char_iter->Next(what);
352 this->
caretpos =
static_cast<uint16_t
>(pos);
367 case WKC_CTRL | WKC_LEFT: {
373 case WKC_CTRL | WKC_RIGHT: {
380 this->char_iter->SetCurPosition(this->
caretpos);
385 this->
caretpos =
static_cast<uint16_t
>(this->
buf.size());
386 this->char_iter->SetCurPosition(this->
caretpos);
422 size_t bytes = std::min<size_t>(this->
max_bytes - 1, text.size());
428 auto it = view.begin();
429 const auto end = view.end();
430 for (
size_t len = 1; len < this->
max_chars && it != end; ++len) ++it;
433 this->
buf.erase(it.GetByteOffset(), std::string::npos);
448 assert(this->
buf.size() < this->max_bytes);
451 this->
caretpos =
static_cast<uint16_t
>(this->
buf.size());
466 bool b = !!(_caret_timer & 0x20);
468 if (b != this->
caret) {
482 case WKC_RETURN:
case WKC_NUM_ENTER:
return HKPR_CONFIRM;
484 case (WKC_CTRL |
'V'):
485 case (WKC_SHIFT | WKC_INSERT):
489 case (WKC_CTRL |
'U'):
494 case WKC_BACKSPACE:
case WKC_DELETE:
495 case WKC_CTRL | WKC_BACKSPACE:
case WKC_CTRL | WKC_DELETE:
499 case WKC_LEFT:
case WKC_RIGHT:
case WKC_END:
case WKC_HOME:
500 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".
std::pair< char[4], size_t > EncodeUtf8(char32_t c)
Encode a character to UTF-8.
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.
@ FS_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.
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.
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
@ HKPR_CANCEL
Escape key pressed.
@ HKPR_EDITING
Textbuf content changed.
@ HKPR_CONFIRM
Return or enter key pressed.
@ HKPR_CURSOR
Non-text change, e.g. cursor position.
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
Handling of UTF-8 encoded data.
Window functions not directly related to making/drawing windows.