From 430929e75a4e6c81d7dd2746570f925bb45e674e Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sat, 27 Feb 2016 16:04:44 -0500 Subject: Add text selection and copying to console --- src/chat.cpp | 64 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) (limited to 'src/chat.cpp') diff --git a/src/chat.cpp b/src/chat.cpp index d8cf3efd9..809d4e422 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -390,6 +390,7 @@ ChatPrompt::ChatPrompt(std::wstring prompt, u32 history_limit): m_cols(0), m_view(0), m_cursor(0), + m_cursor_len(0), m_nick_completion_start(0), m_nick_completion_end(0) { @@ -426,11 +427,6 @@ void ChatPrompt::addToHistory(std::wstring line) m_history_index = m_history.size(); } -std::wstring ChatPrompt::getLine() -{ - return m_line; -} - void ChatPrompt::clear() { m_line.clear(); @@ -590,14 +586,12 @@ void ChatPrompt::cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope sco s32 length = m_line.size(); s32 increment = (dir == CURSOROP_DIR_RIGHT) ? 1 : -1; - if (scope == CURSOROP_SCOPE_CHARACTER) - { + switch (scope) { + case CURSOROP_SCOPE_CHARACTER: new_cursor += increment; - } - else if (scope == CURSOROP_SCOPE_WORD) - { - if (increment > 0) - { + break; + case CURSOROP_SCOPE_WORD: + if (dir == CURSOROP_DIR_RIGHT) { // skip one word to the right while (new_cursor < length && isspace(m_line[new_cursor])) new_cursor++; @@ -605,39 +599,47 @@ void ChatPrompt::cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope sco new_cursor++; while (new_cursor < length && isspace(m_line[new_cursor])) new_cursor++; - } - else - { + } else { // skip one word to the left while (new_cursor >= 1 && isspace(m_line[new_cursor - 1])) new_cursor--; while (new_cursor >= 1 && !isspace(m_line[new_cursor - 1])) new_cursor--; } - } - else if (scope == CURSOROP_SCOPE_LINE) - { + break; + case CURSOROP_SCOPE_LINE: new_cursor += increment * length; + break; + case CURSOROP_SCOPE_SELECTION: + break; } new_cursor = MYMAX(MYMIN(new_cursor, length), 0); - if (op == CURSOROP_MOVE) - { + switch (op) { + case CURSOROP_MOVE: m_cursor = new_cursor; - } - else if (op == CURSOROP_DELETE) - { - if (new_cursor < old_cursor) - { - m_line.erase(new_cursor, old_cursor - new_cursor); - m_cursor = new_cursor; + m_cursor_len = 0; + break; + case CURSOROP_DELETE: + if (m_cursor_len > 0) { // Delete selected text first + m_line.erase(m_cursor, m_cursor_len); + } else { + m_cursor = MYMIN(new_cursor, old_cursor); + m_line.erase(m_cursor, abs(new_cursor - old_cursor)); } - else if (new_cursor > old_cursor) - { - m_line.erase(old_cursor, new_cursor - old_cursor); - m_cursor = old_cursor; + m_cursor_len = 0; + break; + case CURSOROP_SELECT: + if (scope == CURSOROP_SCOPE_LINE) { + m_cursor = 0; + m_cursor_len = length; + } else { + m_cursor = MYMIN(new_cursor, old_cursor); + m_cursor_len += abs(new_cursor - old_cursor); + m_cursor_len = MYMIN(m_cursor_len, length - m_cursor); } + break; } clampView(); -- cgit v1.2.3