OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
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:
@ HKPR_EDITING
Textbuf content changed.
bool InsertChar(char32_t key)
Insert a character to a textbuffer.
IterType
Type of the iterator.
@ HKPR_CONFIRM
Return or enter key pressed.
uint16_t markend
the end position of the marked area in the buffer, in bytes
@ TD_LTR
Text is written left-to-right by default.
int8_t Utf8CharLen(char32_t c)
Return the length of a UTF-8 encoded character.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
static const size_t END
Sentinel to indicate end-of-iteration.
void Assign(StringID string)
Render a string into the textbuffer.
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.
uint16_t max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
uint16_t pixels
the current size of the string in pixels
char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
uint16_t caretpos
the current position of the caret in the buffer, in bytes
uint16_t markpos
the start position of the marked area in the buffer, in bytes
bool MovePos(uint16_t keycode)
Handle text navigation with arrow keys left/right.
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.
@ ITER_CHARACTER
Iterate over characters (or more exactly grapheme clusters).
char *const buf
buffer in which text is saved
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...
bool HandleCaret()
Handle the flashing of the caret.
uint16_t markxoffs
the start position of the marked area in pixels
Class for iterating over different kind of parts of a string.
@ FS_NORMAL
Index of the normal font in the font tables.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
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).
uint16_t bytes
the current size of the string in bytes (including terminating '\0')
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
const char * GetText() const
Get the current text.
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
void UpdateMarkedText()
Update pixel positions of the marked text area.
bool caret
is the caret ("_") visible or not
void UpdateCaretPosition()
Update pixel position of the caret.
CharSetFilter afilter
Allowed characters.
bool DeleteChar(uint16_t keycode)
Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete fro...
@ ITER_WORD
Iterate over words.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
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...
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
uint16_t max_chars
the maximum size of the buffer in characters (including terminating '\0')
void DeleteText(uint16_t from, uint16_t to, bool update)
Delete a part of the text.
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
uint16_t marklength
the length of the marked area in pixels
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
@ HKPR_CANCEL
Escape key pressed.
bool MoveNext(StringIterator::IterType what)
Move to next character position.
void UpdateStringIter()
Update the character iter after the text has changed.
@ SVS_NONE
Allow nothing and replace nothing.
uint16_t chars
the current size of the string in characters (including terminating '\0')
bool MovePrev(StringIterator::IterType what)
Move to previous character position.
T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
size_t Utf8Decode(char32_t *c, const char *s)
Decode and consume the next UTF-8 encoded character.
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
void UpdateWidth()
Update pixel width of the text.
uint16_t caretxoffs
the current position of the caret in pixels
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
void DeleteAll()
Delete every character in the textbuffer.
TextDirection _current_text_dir
Text direction of the currently selected language.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
@ HKPR_CURSOR
Non-text change, e.g. cursor position.
void DiscardMarkedText(bool update=true)
Discard any marked text.