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;
76 for (
const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
91 for (
const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
98 memmove(s, s + len, this->
bytes - (s - this->
buf) - len);
101 if (backspace) this->caretpos -= len;
160 bool Textbuf::InsertString(
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
163 if (insert_location !=
nullptr) {
164 insertpos = insert_location - this->
buf;
165 if (insertpos > this->
bytes)
return false;
167 if (replacement_end !=
nullptr) {
168 this->
DeleteText(insertpos, replacement_end - this->buf, str ==
nullptr);
174 if (str ==
nullptr)
return false;
178 for (
const char *ptr = str; (c = Utf8Consume(&ptr)) !=
'\0';) {
192 if (
bytes == 0)
return false;
199 memmove(this->
buf + insertpos +
bytes, this->
buf + insertpos, this->bytes - insertpos);
200 memcpy(this->
buf + insertpos, str,
bytes);
202 this->bytes +=
bytes;
207 this->
buf[this->bytes - 1] =
'\0';
226 if (!contents.has_value())
return false;
228 return this->
InsertString(contents.value().c_str(),
false);
240 const char *s = this->
buf + from;
241 while (s < this->
buf + to) {
247 memmove(this->
buf + from, this->
buf + to, this->
bytes - to);
248 this->
bytes -= to - from;
251 auto fixup = [&](uint16_t &pos) {
252 if (pos <= from)
return;
280 if (this->
markend == 0)
return;
298 this->char_iter->SetString(this->
buf);
299 size_t pos = this->char_iter->SetCurPosition(this->
caretpos);
322 this->
markxoffs = std::min(pos.left, end.left);
336 if (this->
caretpos == 0)
return false;
338 size_t pos = this->char_iter->Prev(what);
341 this->
caretpos =
static_cast<uint16_t
>(pos);
355 size_t pos = this->char_iter->Next(what);
358 this->
caretpos =
static_cast<uint16_t
>(pos);
373 case WKC_CTRL | WKC_LEFT: {
379 case WKC_CTRL | WKC_RIGHT: {
386 this->char_iter->SetCurPosition(this->
caretpos);
392 this->char_iter->SetCurPosition(this->
caretpos);
442 size_t bytes = std::min<size_t>(this->
max_bytes - 1, text.size());
443 memcpy(this->
buf, text.data(),
bytes);
465 const char *
buf = this->
buf;
470 while ((c = Utf8Consume(&
buf)) !=
'\0') {
492 bool b = !!(_caret_timer & 0x20);
494 if (b != this->
caret) {
508 case WKC_RETURN:
case WKC_NUM_ENTER:
return HKPR_CONFIRM;
510 case (WKC_CTRL |
'V'):
511 case (WKC_SHIFT | WKC_INSERT):
515 case (WKC_CTRL |
'U'):
520 case WKC_BACKSPACE:
case WKC_DELETE:
521 case WKC_CTRL | WKC_BACKSPACE:
case WKC_CTRL | WKC_DELETE:
525 case WKC_LEFT:
case WKC_RIGHT:
case WKC_END:
case WKC_HOME:
526 case WKC_CTRL | WKC_LEFT:
case WKC_CTRL | WKC_RIGHT:
Functions related to the allocation of memory.
T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
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).
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, const char *ch, 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.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
size_t Utf8Decode(char32_t *c, const char *s)
Decode and consume the next UTF-8 encoded character.
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
Functions related to low-level strings.
char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
int8_t Utf8CharLen(char32_t c)
Return the length of a UTF-8 encoded character.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
@ SVS_NONE
Allow nothing and replace nothing.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
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.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
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')
uint16_t chars
the current size of the string in characters (including terminating '\0')
bool InsertString(const char *str, bool marked, const char *caret=nullptr, const char *insert_location=nullptr, const char *replacement_end=nullptr)
Insert a string into the text buffer.
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')
void Assign(StringID string)
Render a string into the textbuffer.
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.
uint16_t bytes
the current size of the string in bytes (including terminating '\0')
const char * GetText() const
Get the current text.
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 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.
char *const buf
buffer in which text is saved
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...
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.
Window functions not directly related to making/drawing windows.