Changeset - r19588:62f8d8203be3
[Not reviewed]
master
0 2 0
zuu - 12 years ago 2012-09-10 18:42:34
zuu@openttd.org
(svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
2 files changed with 27 insertions and 9 deletions:
0 comments (0 inline, 0 general)
src/textbuf.cpp
Show inline comments
 
@@ -24,17 +24,33 @@
 
 */
 
bool GetClipboardContents(char *buffer, size_t buff_len);
 

	
 
int _caret_timer;
 

	
 

	
 
/* Delete a character at the caret position in a text buf.
 
 * If backspace is set, delete the character before the caret,
 
 * else delete the character after it. */
 
/**
 
 * 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;
 
}
 

	
 
/**
 
 * Delete a character at the caret position in a text buf.
 
 * @param backspace if set, delete the character before the caret,
 
 * else delete the character after it.
 
 * @warning You should ensure Textbuf::CanDelChar returns true before calling this function.
 
 */
 
void Textbuf::DelChar(bool backspace)
 
{
 
	assert(this->CanDelChar(backspace));
 

	
 
	WChar c;
 
	char *s = this->buf + this->caretpos;
 

	
 
	if (backspace) s = Utf8PrevChar(s);
 

	
 
	uint16 len = (uint16)Utf8Decode(&c, s);
 
@@ -57,18 +73,19 @@ void Textbuf::DelChar(bool backspace)
 
 * The character is delete from the position the caret is at
 
 * @param delmode Type of deletion, either WKC_BACKSPACE or WKC_DELETE
 
 * @return Return true on successful change of Textbuf, or false otherwise
 
 */
 
bool Textbuf::DeleteChar(int delmode)
 
{
 
	if (delmode == WKC_BACKSPACE && this->caretpos != 0) {
 
		this->DelChar(true);
 
		return true;
 
	} else if (delmode == WKC_DELETE && this->caretpos < this->bytes - 1) {
 
		this->DelChar(false);
 
		return true;
 
	if (delmode == WKC_BACKSPACE || delmode == WKC_DELETE) {
 
		bool backspace = delmode == WKC_BACKSPACE;
 
		if (CanDelChar(backspace)) {
 
			this->DelChar(backspace);
 
			return true;
 
		}
 
		return false;
 
	}
 

	
 
	return false;
 
}
 

	
 
/**
src/textbuf_type.h
Show inline comments
 
@@ -36,12 +36,13 @@ struct Textbuf {
 
	bool MovePos(int navmode);
 

	
 
	bool HandleCaret();
 
	void UpdateSize();
 

	
 
private:
 
	bool CanDelChar(bool backspace);
 
	void DelChar(bool backspace);
 
	bool CanMoveCaretLeft();
 
	WChar MoveCaretLeft();
 
	bool CanMoveCaretRight();
 
	WChar MoveCaretRight();
 

	
0 comments (0 inline, 0 general)