MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / DeleteChar

Method DeleteChar

src/textbuf.cpp:51–107  ·  view source on GitHub ↗

* Delete a character from a textbuffer, either with 'Delete' or 'Backspace' * The character is delete from the position the caret is at * @param keycode Type of deletion, either WKC_BACKSPACE or WKC_DELETE * @return Return true on successful change of Textbuf, or false otherwise */

Source from the content-addressed store, hash-verified

49 * @return Return true on successful change of Textbuf, or false otherwise
50 */
51bool Textbuf::DeleteChar(uint16_t keycode)
52{
53 bool word = (keycode & WKC_CTRL) != 0;
54
55 keycode &= ~WKC_SPECIAL_KEYS;
56 if (keycode != WKC_BACKSPACE && keycode != WKC_DELETE) return false;
57
58 bool backspace = keycode == WKC_BACKSPACE;
59
60 if (!CanDelChar(backspace)) return false;
61
62 size_t start;
63 size_t len;
64 if (word) {
65 /* Delete a complete word. */
66 if (backspace) {
67 /* Delete whitespace and word in front of the caret. */
68 start = this->char_iter->Prev(StringIterator::ITER_WORD);
69 len = this->caretpos - start;
70 } else {
71 /* Delete word and following whitespace following the caret. */
72 start = this->caretpos;
73 len = this->char_iter->Next(StringIterator::ITER_WORD) - start;
74 }
75 /* Update character count. */
76 this->chars -= static_cast<uint16_t>(Utf8StringLength(std::string_view(this->buf).substr(start, len)));
77 } else {
78 /* Delete a single character. */
79 if (backspace) {
80 /* Delete the last code point in front of the caret. */
81 Utf8View view(this->buf);
82 auto it = view.GetIterAtByte(this->caretpos);
83 --it;
84 start = it.GetByteOffset();
85 len = this->caretpos - start;
86 this->chars--;
87 } else {
88 /* Delete the complete character following the caret. */
89 start = this->caretpos;
90 len = this->char_iter->Next(StringIterator::ITER_CHARACTER) - start;
91 /* Update character count. */
92 this->chars -= static_cast<uint16_t>(Utf8StringLength(std::string_view(this->buf).substr(start, len)));
93 }
94 }
95
96 /* Move the remaining characters over the marker */
97 this->buf.erase(start, len);
98
99 if (backspace) this->caretpos -= static_cast<uint16_t>(len);
100
101 this->UpdateStringIter();
102 this->UpdateWidth();
103 this->UpdateCaretPosition();
104 this->UpdateMarkedText();
105
106 return true;
107}
108

Callers 2

HandleKeyPressMethod · 0.95
OnClickMethod · 0.80

Calls 11

UpdateStringIterMethod · 0.95
UpdateWidthMethod · 0.95
UpdateCaretPositionMethod · 0.95
UpdateMarkedTextMethod · 0.95
Utf8StringLengthFunction · 0.85
substrMethod · 0.80
GetIterAtByteMethod · 0.80
GetByteOffsetMethod · 0.80
PrevMethod · 0.45
NextMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected