2012-06-04 15:29:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file textbuf.cpp Textbuffer handling. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2012-11-14 22:50:11 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-06-04 15:29:37 +00:00
|
|
|
#include "textbuf_type.h"
|
|
|
|
#include "string_func.h"
|
2012-11-14 22:50:11 +00:00
|
|
|
#include "strings_func.h"
|
2012-06-04 15:29:37 +00:00
|
|
|
#include "gfx_type.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "window_func.h"
|
2012-11-14 22:50:17 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2012-06-04 15:29:37 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Try to retrieve the current clipboard contents.
|
2012-06-04 15:29:37 +00:00
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @note OS-specific function.
|
|
|
|
* @return True if some text could be retrieved.
|
2012-06-04 15:29:37 +00:00
|
|
|
*/
|
|
|
|
bool GetClipboardContents(char *buffer, size_t buff_len);
|
|
|
|
|
|
|
|
int _caret_timer;
|
|
|
|
|
|
|
|
|
2012-09-10 18:42:34 +00:00
|
|
|
/**
|
|
|
|
* Checks if it is possible to delete a character.
|
|
|
|
* @param backspace if set, delete the character before the caret,
|
|
|
|
* otherwise, delete the character after it.
|
|
|
|
* @return true if a character can be deleted in the given direction.
|
|
|
|
*/
|
|
|
|
bool Textbuf::CanDelChar(bool backspace)
|
|
|
|
{
|
|
|
|
return backspace ? this->caretpos != 0 : this->caretpos < this->bytes - 1;
|
|
|
|
}
|
|
|
|
|
2012-09-10 18:47:13 +00:00
|
|
|
/**
|
2013-08-05 20:35:35 +00:00
|
|
|
* 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
|
2012-09-10 18:47:13 +00:00
|
|
|
*/
|
2013-08-05 20:35:35 +00:00
|
|
|
bool Textbuf::DeleteChar(uint16 keycode)
|
2012-09-10 18:47:13 +00:00
|
|
|
{
|
2013-08-05 20:35:35 +00:00
|
|
|
bool word = (keycode & WKC_CTRL) != 0;
|
2012-09-10 18:47:13 +00:00
|
|
|
|
2013-08-05 20:35:35 +00:00
|
|
|
keycode &= ~WKC_SPECIAL_KEYS;
|
|
|
|
if (keycode != WKC_BACKSPACE && keycode != WKC_DELETE) return false;
|
2012-09-10 18:47:13 +00:00
|
|
|
|
2013-08-05 20:35:35 +00:00
|
|
|
bool backspace = keycode == WKC_BACKSPACE;
|
2012-09-10 18:47:13 +00:00
|
|
|
|
2013-08-05 20:35:35 +00:00
|
|
|
if (!CanDelChar(backspace)) return false;
|
2012-09-10 18:42:34 +00:00
|
|
|
|
2012-06-04 15:30:29 +00:00
|
|
|
char *s = this->buf + this->caretpos;
|
2013-08-05 20:35:35 +00:00
|
|
|
uint16 len = 0;
|
|
|
|
|
|
|
|
if (word) {
|
|
|
|
/* Delete a complete word. */
|
|
|
|
if (backspace) {
|
|
|
|
/* Delete whitespace and word in front of the caret. */
|
|
|
|
len = this->caretpos - (uint16)this->char_iter->Prev(StringIterator::ITER_WORD);
|
|
|
|
s -= len;
|
|
|
|
} else {
|
|
|
|
/* Delete word and following whitespace following the caret. */
|
|
|
|
len = (uint16)this->char_iter->Next(StringIterator::ITER_WORD) - this->caretpos;
|
|
|
|
}
|
|
|
|
/* Update character count. */
|
|
|
|
for (const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
|
|
|
|
this->chars--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Delete a single character. */
|
|
|
|
if (backspace) {
|
|
|
|
/* Delete the last code point in front of the caret. */
|
|
|
|
s = Utf8PrevChar(s);
|
|
|
|
WChar c;
|
|
|
|
len = (uint16)Utf8Decode(&c, s);
|
|
|
|
this->chars--;
|
|
|
|
} else {
|
|
|
|
/* Delete the complete character following the caret. */
|
|
|
|
len = (uint16)this->char_iter->Next(StringIterator::ITER_CHARACTER) - this->caretpos;
|
|
|
|
/* Update character count. */
|
|
|
|
for (const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
|
|
|
|
this->chars--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 15:29:37 +00:00
|
|
|
|
|
|
|
/* Move the remaining characters over the marker */
|
2012-06-04 15:30:29 +00:00
|
|
|
memmove(s, s + len, this->bytes - (s - this->buf) - len);
|
|
|
|
this->bytes -= len;
|
2013-08-05 20:35:23 +00:00
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
if (backspace) this->caretpos -= len;
|
|
|
|
|
|
|
|
this->UpdateStringIter();
|
2013-08-05 20:35:23 +00:00
|
|
|
this->UpdateWidth();
|
2013-08-05 20:35:27 +00:00
|
|
|
this->UpdateCaretPosition();
|
2013-08-05 20:37:22 +00:00
|
|
|
this->UpdateMarkedText();
|
2012-09-10 18:47:13 +00:00
|
|
|
|
2013-08-05 20:35:35 +00:00
|
|
|
return true;
|
2012-06-04 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete every character in the textbuffer
|
|
|
|
*/
|
2012-06-04 15:30:29 +00:00
|
|
|
void Textbuf::DeleteAll()
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
2012-06-04 15:30:29 +00:00
|
|
|
memset(this->buf, 0, this->max_bytes);
|
|
|
|
this->bytes = this->chars = 1;
|
|
|
|
this->pixels = this->caretpos = this->caretxoffs = 0;
|
2013-08-05 20:37:22 +00:00
|
|
|
this->markpos = this->markend = this->markxoffs = this->marklength = 0;
|
2013-08-05 20:35:27 +00:00
|
|
|
this->UpdateStringIter();
|
2012-06-04 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a character to a textbuffer. If maxwidth of the Textbuf is zero,
|
|
|
|
* we don't care about the visual-length but only about the physical
|
|
|
|
* length of the string
|
|
|
|
* @param key Character to be inserted
|
|
|
|
* @return Return true on successful change of Textbuf, or false otherwise
|
|
|
|
*/
|
2012-06-04 15:30:29 +00:00
|
|
|
bool Textbuf::InsertChar(WChar key)
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
|
|
|
uint16 len = (uint16)Utf8CharLen(key);
|
2012-06-04 15:30:29 +00:00
|
|
|
if (this->bytes + len <= this->max_bytes && this->chars + 1 <= this->max_chars) {
|
|
|
|
memmove(this->buf + this->caretpos + len, this->buf + this->caretpos, this->bytes - this->caretpos);
|
|
|
|
Utf8Encode(this->buf + this->caretpos, key);
|
|
|
|
this->chars++;
|
2013-08-05 20:35:27 +00:00
|
|
|
this->bytes += len;
|
|
|
|
this->caretpos += len;
|
2012-06-04 15:30:29 +00:00
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
this->UpdateStringIter();
|
|
|
|
this->UpdateWidth();
|
2013-08-05 20:35:23 +00:00
|
|
|
this->UpdateCaretPosition();
|
2013-08-05 20:37:22 +00:00
|
|
|
this->UpdateMarkedText();
|
2012-06-04 15:29:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-05 20:37:06 +00:00
|
|
|
* Insert a string into the text buffer. If maxwidth of the Textbuf is zero,
|
|
|
|
* we don't care about the visual-length but only about the physical
|
|
|
|
* length of the string.
|
|
|
|
* @param str String to insert.
|
2013-08-05 20:37:25 +00:00
|
|
|
* @param marked Replace the currently marked text with the new text.
|
|
|
|
* @param caret Move the caret to this point in the insertion string.
|
2013-08-05 20:37:57 +00:00
|
|
|
* @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.
|
2013-08-05 20:37:06 +00:00
|
|
|
* @return True on successful change of Textbuf, or false otherwise.
|
2012-06-04 15:29:37 +00:00
|
|
|
*/
|
2013-08-05 20:37:57 +00:00
|
|
|
bool Textbuf::InsertString(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
2013-08-05 20:37:25 +00:00
|
|
|
uint16 insertpos = (marked && this->marklength != 0) ? this->markpos : this->caretpos;
|
2013-08-05 20:37:57 +00:00
|
|
|
if (insert_location != NULL) {
|
|
|
|
insertpos = insert_location - this->buf;
|
2013-08-10 12:47:11 +00:00
|
|
|
if (insertpos > this->bytes) return false;
|
2013-08-05 20:37:25 +00:00
|
|
|
|
2013-08-05 20:37:57 +00:00
|
|
|
if (replacement_end != NULL) {
|
|
|
|
this->DeleteText(insertpos, replacement_end - this->buf, str == NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (marked) this->DiscardMarkedText(str == NULL);
|
|
|
|
}
|
2013-08-05 20:37:25 +00:00
|
|
|
|
|
|
|
if (str == NULL) return false;
|
2012-06-04 15:29:37 +00:00
|
|
|
|
2013-08-05 20:35:23 +00:00
|
|
|
uint16 bytes = 0, chars = 0;
|
2012-06-04 15:29:37 +00:00
|
|
|
WChar c;
|
2013-08-05 20:37:06 +00:00
|
|
|
for (const char *ptr = str; (c = Utf8Consume(&ptr)) != '\0';) {
|
2013-03-17 13:04:48 +00:00
|
|
|
if (!IsValidChar(c, this->afilter)) break;
|
2012-06-04 15:29:37 +00:00
|
|
|
|
|
|
|
byte len = Utf8CharLen(c);
|
2012-06-04 15:30:29 +00:00
|
|
|
if (this->bytes + bytes + len > this->max_bytes) break;
|
|
|
|
if (this->chars + chars + 1 > this->max_chars) break;
|
2012-06-04 15:29:37 +00:00
|
|
|
|
|
|
|
bytes += len;
|
|
|
|
chars++;
|
2013-08-05 20:37:25 +00:00
|
|
|
|
|
|
|
/* Move caret if needed. */
|
|
|
|
if (ptr == caret) this->caretpos = insertpos + bytes;
|
2012-06-04 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes == 0) return false;
|
|
|
|
|
2013-08-05 20:37:25 +00:00
|
|
|
if (marked) {
|
|
|
|
this->markpos = insertpos;
|
|
|
|
this->markend = insertpos + bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(this->buf + insertpos + bytes, this->buf + insertpos, this->bytes - insertpos);
|
|
|
|
memcpy(this->buf + insertpos, str, bytes);
|
2012-06-04 15:29:37 +00:00
|
|
|
|
2012-06-04 15:30:29 +00:00
|
|
|
this->bytes += bytes;
|
|
|
|
this->chars += chars;
|
2013-08-05 20:37:25 +00:00
|
|
|
if (!marked && caret == NULL) this->caretpos += bytes;
|
2012-06-04 15:30:29 +00:00
|
|
|
assert(this->bytes <= this->max_bytes);
|
|
|
|
assert(this->chars <= this->max_chars);
|
|
|
|
this->buf[this->bytes - 1] = '\0'; // terminating zero
|
2012-06-04 15:29:37 +00:00
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
this->UpdateStringIter();
|
2013-08-05 20:35:23 +00:00
|
|
|
this->UpdateWidth();
|
|
|
|
this->UpdateCaretPosition();
|
2013-08-05 20:37:22 +00:00
|
|
|
this->UpdateMarkedText();
|
2013-08-05 20:35:23 +00:00
|
|
|
|
2012-06-04 15:29:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:06 +00:00
|
|
|
/**
|
|
|
|
* Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
|
|
|
|
* and append this up to the maximum length (either absolute or screenlength). If maxlength
|
|
|
|
* is zero, we don't care about the screenlength but only about the physical length of the string
|
|
|
|
* @return true on successful change of Textbuf, or false otherwise
|
|
|
|
*/
|
|
|
|
bool Textbuf::InsertClipboard()
|
|
|
|
{
|
|
|
|
char utf8_buf[512];
|
|
|
|
|
|
|
|
if (!GetClipboardContents(utf8_buf, lengthof(utf8_buf))) return false;
|
|
|
|
|
2013-08-05 20:37:25 +00:00
|
|
|
return this->InsertString(utf8_buf, false);
|
2013-08-05 20:37:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:22 +00:00
|
|
|
/**
|
2013-08-05 20:37:57 +00:00
|
|
|
* Delete a part of the text.
|
|
|
|
* @param from Start of the text to delete.
|
|
|
|
* @param to End of the text to delete.
|
2013-08-05 20:37:22 +00:00
|
|
|
* @param update Set to true if the internal state should be updated.
|
|
|
|
*/
|
2013-08-05 20:37:57 +00:00
|
|
|
void Textbuf::DeleteText(uint16 from, uint16 to, bool update)
|
2013-08-05 20:37:22 +00:00
|
|
|
{
|
|
|
|
uint c = 0;
|
2013-08-05 20:37:57 +00:00
|
|
|
const char *s = this->buf + from;
|
|
|
|
while (s < this->buf + to) {
|
2013-08-05 20:37:22 +00:00
|
|
|
Utf8Consume(&s);
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip marked characters from buffer. */
|
2013-08-05 20:37:57 +00:00
|
|
|
memmove(this->buf + from, this->buf + to, this->bytes - to);
|
|
|
|
this->bytes -= to - from;
|
2013-08-05 20:37:22 +00:00
|
|
|
this->chars -= c;
|
|
|
|
|
|
|
|
/* Fixup caret if needed. */
|
2013-08-05 20:37:57 +00:00
|
|
|
if (this->caretpos > from) {
|
|
|
|
if (this->caretpos <= to) {
|
|
|
|
this->caretpos = from;
|
2013-08-05 20:37:22 +00:00
|
|
|
} else {
|
2013-08-05 20:37:57 +00:00
|
|
|
this->caretpos -= to - from;
|
2013-08-05 20:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
this->UpdateStringIter();
|
|
|
|
this->UpdateCaretPosition();
|
|
|
|
this->UpdateMarkedText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:57 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
/** Update the character iter after the text has changed. */
|
|
|
|
void Textbuf::UpdateStringIter()
|
|
|
|
{
|
|
|
|
this->char_iter->SetString(this->buf);
|
2013-08-05 20:35:31 +00:00
|
|
|
size_t pos = this->char_iter->SetCurPosition(this->caretpos);
|
|
|
|
this->caretpos = pos == StringIterator::END ? 0 : (uint16)pos;
|
2013-08-05 20:35:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:35:23 +00:00
|
|
|
/** Update pixel width of the text. */
|
|
|
|
void Textbuf::UpdateWidth()
|
|
|
|
{
|
|
|
|
this->pixels = GetStringBoundingBox(this->buf, FS_NORMAL).width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update pixel position of the caret. */
|
|
|
|
void Textbuf::UpdateCaretPosition()
|
|
|
|
{
|
|
|
|
this->caretxoffs = this->chars > 1 ? GetCharPosInString(this->buf, this->buf + this->caretpos, FS_NORMAL).x : 0;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:22 +00:00
|
|
|
/** Update pixel positions of the marked text area. */
|
|
|
|
void Textbuf::UpdateMarkedText()
|
|
|
|
{
|
|
|
|
if (this->markend != 0) {
|
|
|
|
this->markxoffs = GetCharPosInString(this->buf, this->buf + this->markpos, FS_NORMAL).x;
|
|
|
|
this->marklength = GetCharPosInString(this->buf, this->buf + this->markend, FS_NORMAL).x - this->markxoffs;
|
|
|
|
} else {
|
|
|
|
this->markxoffs = this->marklength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 15:29:37 +00:00
|
|
|
/**
|
|
|
|
* Handle text navigation with arrow keys left/right.
|
2013-01-08 22:46:42 +00:00
|
|
|
* This defines where the caret will blink and the next character interaction will occur
|
2013-03-17 13:05:18 +00:00
|
|
|
* @param keycode Direction in which navigation occurs (WKC_CTRL |) WKC_LEFT, (WKC_CTRL |) WKC_RIGHT, WKC_END, WKC_HOME
|
2012-06-04 15:29:37 +00:00
|
|
|
* @return Return true on successful change of Textbuf, or false otherwise
|
|
|
|
*/
|
2013-03-17 13:05:18 +00:00
|
|
|
bool Textbuf::MovePos(uint16 keycode)
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
2013-03-17 13:05:18 +00:00
|
|
|
switch (keycode) {
|
2012-06-04 15:29:37 +00:00
|
|
|
case WKC_LEFT:
|
2012-09-10 18:45:29 +00:00
|
|
|
case WKC_CTRL | WKC_LEFT: {
|
2013-08-05 20:35:31 +00:00
|
|
|
if (this->caretpos == 0) break;
|
|
|
|
|
|
|
|
size_t pos = this->char_iter->Prev(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
|
|
|
|
if (pos == StringIterator::END) return true;
|
|
|
|
|
|
|
|
this->caretpos = (uint16)pos;
|
|
|
|
this->UpdateCaretPosition();
|
2012-09-10 18:45:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-04 15:29:37 +00:00
|
|
|
case WKC_RIGHT:
|
2012-09-10 18:45:29 +00:00
|
|
|
case WKC_CTRL | WKC_RIGHT: {
|
2013-08-05 20:35:31 +00:00
|
|
|
if (this->caretpos >= this->bytes - 1) break;
|
|
|
|
|
|
|
|
size_t pos = this->char_iter->Next(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
|
|
|
|
if (pos == StringIterator::END) return true;
|
|
|
|
|
|
|
|
this->caretpos = (uint16)pos;
|
|
|
|
this->UpdateCaretPosition();
|
2012-09-10 18:45:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-04 15:29:37 +00:00
|
|
|
case WKC_HOME:
|
2012-06-04 15:30:29 +00:00
|
|
|
this->caretpos = 0;
|
2013-08-05 20:35:31 +00:00
|
|
|
this->char_iter->SetCurPosition(this->caretpos);
|
2013-08-05 20:35:23 +00:00
|
|
|
this->UpdateCaretPosition();
|
2012-06-04 15:29:37 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case WKC_END:
|
2012-06-04 15:30:29 +00:00
|
|
|
this->caretpos = this->bytes - 1;
|
2013-08-05 20:35:31 +00:00
|
|
|
this->char_iter->SetCurPosition(this->caretpos);
|
2013-08-05 20:35:23 +00:00
|
|
|
this->UpdateCaretPosition();
|
2012-06-04 15:29:37 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the textbuffer by supplying it the buffer to write into
|
|
|
|
* and the maximum length of this buffer
|
|
|
|
* @param buf the buffer that will be holding the data for input
|
|
|
|
* @param max_bytes maximum size in bytes, including terminating '\0'
|
|
|
|
* @param max_chars maximum size in chars, including terminating '\0'
|
|
|
|
*/
|
2012-11-14 22:50:17 +00:00
|
|
|
Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars)
|
|
|
|
: buf(MallocT<char>(max_bytes))
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
|
|
|
assert(max_bytes != 0);
|
|
|
|
assert(max_chars != 0);
|
|
|
|
|
2013-08-05 20:35:27 +00:00
|
|
|
this->char_iter = StringIterator::Create();
|
|
|
|
|
2013-03-17 13:04:10 +00:00
|
|
|
this->afilter = CS_ALPHANUMERAL;
|
2012-06-04 15:30:29 +00:00
|
|
|
this->max_bytes = max_bytes;
|
2012-11-14 22:50:17 +00:00
|
|
|
this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars;
|
2012-06-04 15:30:29 +00:00
|
|
|
this->caret = true;
|
2012-11-14 22:50:17 +00:00
|
|
|
this->DeleteAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Textbuf::~Textbuf()
|
|
|
|
{
|
2013-08-05 20:35:27 +00:00
|
|
|
delete this->char_iter;
|
2012-11-14 22:50:17 +00:00
|
|
|
free(this->buf);
|
2012-06-04 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 22:50:11 +00:00
|
|
|
/**
|
|
|
|
* Render a string into the textbuffer.
|
|
|
|
* @param string String
|
|
|
|
*/
|
|
|
|
void Textbuf::Assign(StringID string)
|
|
|
|
{
|
|
|
|
GetString(this->buf, string, &this->buf[this->max_bytes - 1]);
|
|
|
|
this->UpdateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a string into the textbuffer.
|
|
|
|
* @param text Source.
|
|
|
|
*/
|
|
|
|
void Textbuf::Assign(const char *text)
|
|
|
|
{
|
|
|
|
ttd_strlcpy(this->buf, text, this->max_bytes);
|
|
|
|
this->UpdateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a formatted string into the textbuffer.
|
|
|
|
*/
|
|
|
|
void Textbuf::Print(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
vsnprintf(this->buf, this->max_bytes, format, va);
|
|
|
|
va_end(va);
|
|
|
|
this->UpdateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-04 15:29:37 +00:00
|
|
|
/**
|
|
|
|
* Update Textbuf type with its actual physical character and screenlength
|
|
|
|
* Get the count of characters in the string as well as the width in pixels.
|
|
|
|
* Useful when copying in a larger amount of text at once
|
|
|
|
*/
|
2012-06-04 15:30:29 +00:00
|
|
|
void Textbuf::UpdateSize()
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
2012-06-04 15:30:29 +00:00
|
|
|
const char *buf = this->buf;
|
2012-06-04 15:29:37 +00:00
|
|
|
|
2012-06-04 15:30:29 +00:00
|
|
|
this->chars = this->bytes = 1; // terminating zero
|
2012-06-04 15:29:37 +00:00
|
|
|
|
|
|
|
WChar c;
|
|
|
|
while ((c = Utf8Consume(&buf)) != '\0') {
|
2012-06-04 15:30:29 +00:00
|
|
|
this->bytes += Utf8CharLen(c);
|
|
|
|
this->chars++;
|
2012-06-04 15:29:37 +00:00
|
|
|
}
|
2012-06-04 15:30:29 +00:00
|
|
|
assert(this->bytes <= this->max_bytes);
|
|
|
|
assert(this->chars <= this->max_chars);
|
2012-06-04 15:29:37 +00:00
|
|
|
|
2012-06-04 15:30:29 +00:00
|
|
|
this->caretpos = this->bytes - 1;
|
2013-08-05 20:35:27 +00:00
|
|
|
this->UpdateStringIter();
|
2013-08-05 20:35:23 +00:00
|
|
|
this->UpdateWidth();
|
2013-08-05 20:37:22 +00:00
|
|
|
this->UpdateMarkedText();
|
2013-08-05 20:35:23 +00:00
|
|
|
|
|
|
|
this->UpdateCaretPosition();
|
2012-06-04 15:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the flashing of the caret.
|
|
|
|
* @return True if the caret state changes.
|
|
|
|
*/
|
2012-06-04 15:30:29 +00:00
|
|
|
bool Textbuf::HandleCaret()
|
2012-06-04 15:29:37 +00:00
|
|
|
{
|
|
|
|
/* caret changed? */
|
|
|
|
bool b = !!(_caret_timer & 0x20);
|
|
|
|
|
2012-06-04 15:30:29 +00:00
|
|
|
if (b != this->caret) {
|
|
|
|
this->caret = b;
|
2012-06-04 15:29:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-17 13:05:45 +00:00
|
|
|
|
2013-08-05 20:36:32 +00:00
|
|
|
HandleKeyPressResult Textbuf::HandleKeyPress(WChar key, uint16 keycode)
|
2013-03-17 13:05:45 +00:00
|
|
|
{
|
|
|
|
bool edited = false;
|
|
|
|
|
|
|
|
switch (keycode) {
|
|
|
|
case WKC_ESC: return HKPR_CANCEL;
|
|
|
|
|
|
|
|
case WKC_RETURN: case WKC_NUM_ENTER: return HKPR_CONFIRM;
|
|
|
|
|
|
|
|
#ifdef WITH_COCOA
|
|
|
|
case (WKC_META | 'V'):
|
|
|
|
#endif
|
|
|
|
case (WKC_CTRL | 'V'):
|
|
|
|
edited = this->InsertClipboard();
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef WITH_COCOA
|
|
|
|
case (WKC_META | 'U'):
|
|
|
|
#endif
|
|
|
|
case (WKC_CTRL | 'U'):
|
|
|
|
this->DeleteAll();
|
|
|
|
edited = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_BACKSPACE: case WKC_DELETE:
|
|
|
|
case WKC_CTRL | WKC_BACKSPACE: case WKC_CTRL | WKC_DELETE:
|
|
|
|
edited = this->DeleteChar(keycode);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
|
|
|
|
case WKC_CTRL | WKC_LEFT: case WKC_CTRL | WKC_RIGHT:
|
|
|
|
this->MovePos(keycode);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (IsValidChar(key, this->afilter)) {
|
|
|
|
edited = this->InsertChar(key);
|
|
|
|
} else {
|
|
|
|
return HKPR_NOT_HANDLED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return edited ? HKPR_EDITING : HKPR_CURSOR;
|
|
|
|
}
|