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;
75 for (
auto ss = s; ss < s + len; Utf8Consume(ss)) {
90 for (
auto ss = s; ss < s + len; Utf8Consume(ss)) {
97 this->
buf.erase(s - this->
buf.begin(), len);
99 if (backspace) this->
caretpos -= len;
134 pos = this->
buf.insert(pos, len,
'\0');
159bool Textbuf::InsertString(
const char *str,
bool marked,
const char *caret,
const char *insert_location,
const char *replacement_end)
162 if (insert_location !=
nullptr) {
163 insertpos = insert_location - this->
buf.data();
164 if (insertpos >= this->
buf.size())
return false;
166 if (replacement_end !=
nullptr) {
167 this->
DeleteText(insertpos, replacement_end - this->
buf.data(), str ==
nullptr);
173 if (str ==
nullptr)
return false;
175 uint16_t bytes = 0,
chars = 0;
177 for (
const char *ptr = str; (c = Utf8Consume(&ptr)) !=
'\0';) {
181 if (this->
buf.size() + bytes + len >= this->max_bytes)
break;
191 if (bytes == 0)
return false;
195 this->
markend = insertpos + bytes;
198 this->
buf.insert(insertpos, str, bytes);
202 assert(this->
buf.size() < this->max_bytes);
222 if (!contents.has_value())
return false;
224 return this->
InsertString(contents.value().c_str(),
false);
236 auto s = this->
buf.begin() + from;
237 auto end = this->
buf.begin() + to;
244 this->
buf.erase(from, to - from);
247 auto fixup = [&](uint16_t &pos) {
248 if (pos <= from)
return;
276 if (this->
markend == 0)
return;
288 return this->
buf.c_str();
294 this->char_iter->SetString(this->
buf.c_str());
295 size_t pos = this->char_iter->SetCurPosition(this->
caretpos);
318 this->
markxoffs = std::min(pos.left, end.left);
332 if (this->
caretpos == 0)
return false;
334 size_t pos = this->char_iter->Prev(what);
337 this->
caretpos =
static_cast<uint16_t
>(pos);
349 if (this->
caretpos >= this->
buf.size())
return false;
351 size_t pos = this->char_iter->Next(what);
354 this->
caretpos =
static_cast<uint16_t
>(pos);
369 case WKC_CTRL | WKC_LEFT: {
375 case WKC_CTRL | WKC_RIGHT: {
382 this->char_iter->SetCurPosition(this->
caretpos);
387 this->
caretpos =
static_cast<uint16_t
>(this->
buf.size());
388 this->char_iter->SetCurPosition(this->
caretpos);
424 size_t bytes = std::min<size_t>(this->
max_bytes - 1, text.size());
429 auto iter = this->
buf.begin();
430 for (
size_t len = 1; len < this->
max_chars && iter != this->
buf.end(); ++len) Utf8Consume(iter);
432 if (iter != this->
buf.end()) {
433 this->
buf.erase(iter, this->
buf.end());
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).
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.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
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.
int8_t Utf8CharLen(char32_t c)
Return the length of a UTF-8 encoded character.
char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
@ 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')
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')
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.
void Assign(const std::string_view text)
Copy a string into the textbuffer.
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.
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.
Window functions not directly related to making/drawing windows.