diff options
author | michi_cc <michi_cc@openttd.org> | 2013-08-05 20:37:57 +0000 |
---|---|---|
committer | michi_cc <michi_cc@openttd.org> | 2013-08-05 20:37:57 +0000 |
commit | d15c1c5d4aacb74bbfd6d47700c5b2a2cec368ff (patch) | |
tree | 1cc8e74963f04a70fe7a7622e51dc2f31451a9f7 /src/textbuf.cpp | |
parent | 30867c487f17ca13744977c7873f4e1cf214fd33 (diff) | |
download | openttd-d15c1c5d4aacb74bbfd6d47700c5b2a2cec368ff.tar.xz |
(svn r25692) -Add: Replacement of a part of the edit box text with a new string.
Diffstat (limited to 'src/textbuf.cpp')
-rw-r--r-- | src/textbuf.cpp | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/src/textbuf.cpp b/src/textbuf.cpp index e99e89662..445ebe59d 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -153,13 +153,23 @@ bool Textbuf::InsertChar(WChar key) * @param str String to insert. * @param marked Replace the currently marked text with the new text. * @param caret Move the caret to this point in the insertion string. + * @param insert_location Position at which to insert the string. + * @param replacement_end Replace all characters from #insert_location up to this location with the new string. * @return True on successful change of Textbuf, or false otherwise. */ -bool Textbuf::InsertString(const char *str, bool marked, const char *caret) +bool Textbuf::InsertString(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end) { uint16 insertpos = (marked && this->marklength != 0) ? this->markpos : this->caretpos; + if (insert_location != NULL) { + insertpos = insert_location - this->buf; + if (insertpos > this->bytes) return NULL; - if (marked) this->DiscardMarkedText(str == NULL); + if (replacement_end != NULL) { + this->DeleteText(insertpos, replacement_end - this->buf, str == NULL); + } + } else { + if (marked) this->DiscardMarkedText(str == NULL); + } if (str == NULL) return false; @@ -220,37 +230,34 @@ bool Textbuf::InsertClipboard() } /** - * Discard any marked text. + * Delete a part of the text. + * @param from Start of the text to delete. + * @param to End of the text to delete. * @param update Set to true if the internal state should be updated. */ -void Textbuf::DiscardMarkedText(bool update) +void Textbuf::DeleteText(uint16 from, uint16 to, bool update) { - if (this->markend == 0) return; - - /* Count marked characters. */ uint c = 0; - const char *s = this->buf + this->markpos; - while (s < this->buf + this->markend) { + const char *s = this->buf + from; + while (s < this->buf + to) { Utf8Consume(&s); c++; } /* Strip marked characters from buffer. */ - memmove(this->buf + this->markpos, this->buf + this->markend, this->bytes - this->markend); - this->bytes -= this->markend - this->markpos; + memmove(this->buf + from, this->buf + to, this->bytes - to); + this->bytes -= to - from; this->chars -= c; /* Fixup caret if needed. */ - if (this->caretpos > this->markpos) { - if (this->caretpos <= this->markend) { - this->caretpos = this->markpos; + if (this->caretpos > from) { + if (this->caretpos <= to) { + this->caretpos = from; } else { - this->caretpos -= this->markend - this->markpos; + this->caretpos -= to - from; } } - this->markpos = this->markend = 0; - if (update) { this->UpdateStringIter(); this->UpdateCaretPosition(); @@ -258,6 +265,18 @@ void Textbuf::DiscardMarkedText(bool update) } } +/** + * Discard any marked text. + * @param update Set to true if the internal state should be updated. + */ +void Textbuf::DiscardMarkedText(bool update) +{ + if (this->markend == 0) return; + + this->DeleteText(this->markpos, this->markend, update); + this->markpos = this->markend = this->markxoffs = this->marklength = 0; +} + /** Update the character iter after the text has changed. */ void Textbuf::UpdateStringIter() { |