//Inserts a character into the buffer. void CConsole::InsertChar(const int szChar, const wchar_t cooked) { static int iHistoryPos = -1; if (!m_bVisible) return; switch (szChar) { case SDLK_RETURN: iHistoryPos = -1; m_iMsgHistPos = 1; ProcessBuffer(m_szBuffer); FlushBuffer(); return; case SDLK_TAB: // Auto Complete return; case SDLK_BACKSPACE: if (IsEmpty() || IsBOB()) return; if (m_iBufferPos == m_iBufferLength) m_szBuffer[m_iBufferPos - 1] = '\0'; else { for (int j = m_iBufferPos-1; j < m_iBufferLength-1; j++) m_szBuffer[j] = m_szBuffer[j+1]; // move chars to left m_szBuffer[m_iBufferLength-1] = '\0'; } m_iBufferPos--; m_iBufferLength--; return; case SDLK_DELETE: if (IsEmpty() || IsEOB()) return; if (m_iBufferPos == m_iBufferLength-1) { m_szBuffer[m_iBufferPos] = '\0'; m_iBufferLength--; } else { if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL]) { // Make Ctrl-Delete delete up to end of line m_szBuffer[m_iBufferPos] = '\0'; m_iBufferLength = m_iBufferPos; } else { // Delete just one char and move the others left for(int j=m_iBufferPos; j<m_iBufferLength-1; j++) m_szBuffer[j] = m_szBuffer[j+1]; m_szBuffer[m_iBufferLength-1] = '\0'; m_iBufferLength--; } } return; case SDLK_HOME: if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL]) { CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory int linesShown = (int)m_fHeight/m_iFontHeight - 4; m_iMsgHistPos = clamp((int)m_deqMsgHistory.size() - linesShown, 1, (int)m_deqMsgHistory.size()); } else { m_iBufferPos = 0; } return; case SDLK_END: if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL]) { m_iMsgHistPos = 1; } else { m_iBufferPos = m_iBufferLength; } return; case SDLK_LEFT: if (m_iBufferPos) m_iBufferPos--; return; case SDLK_RIGHT: if (m_iBufferPos != m_iBufferLength) m_iBufferPos++; return; // BEGIN: Buffer History Lookup case SDLK_UP: if (m_deqBufHistory.size() && iHistoryPos != (int)m_deqBufHistory.size() - 1) { iHistoryPos++; SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str()); m_iBufferPos = m_iBufferLength; } return; case SDLK_DOWN: if (m_deqBufHistory.size()) { if (iHistoryPos > 0) { iHistoryPos--; SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str()); m_iBufferPos = m_iBufferLength; } else if (iHistoryPos == 0) { iHistoryPos--; FlushBuffer(); } } return; // END: Buffer History Lookup // BEGIN: Message History Lookup case SDLK_PAGEUP: { CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory if (m_iMsgHistPos != (int)m_deqMsgHistory.size()) m_iMsgHistPos++; return; } case SDLK_PAGEDOWN: if (m_iMsgHistPos != 1) m_iMsgHistPos--; return; // END: Message History Lookup default: //Insert a character if (IsFull()) return; if (cooked == 0) return; if (IsEOB()) //are we at the end of the buffer? m_szBuffer[m_iBufferPos] = cooked; //cat char onto end else { //we need to insert int i; for(i=m_iBufferLength; i>m_iBufferPos; i--) m_szBuffer[i] = m_szBuffer[i-1]; // move chars to right m_szBuffer[i] = cooked; } m_iBufferPos++; m_iBufferLength++; return; } }
//Inserts a character into the buffer. void CConsole::InsertChar(const int szChar, const wchar_t cooked ) { static int iHistoryPos = -1; if (!m_bVisible) return; switch (szChar){ case SDLK_RETURN: iHistoryPos = -1; m_iMsgHistPos = 1; ProcessBuffer(m_szBuffer); FlushBuffer(); return; case SDLK_TAB: // Auto Complete return; case SDLK_BACKSPACE: if (IsEmpty() || IsBOB()) return; if (m_iBufferPos == m_iBufferLength) m_szBuffer[m_iBufferPos - 1] = '\0'; else{ for(int j=m_iBufferPos-1; j<m_iBufferLength-1; j++) m_szBuffer[j] = m_szBuffer[j+1]; // move chars to left m_szBuffer[m_iBufferLength-1] = '\0'; } m_iBufferPos--; m_iBufferLength--; return; case SDLK_DELETE: if (IsEmpty() || IsEOB()) return; if (m_iBufferPos == m_iBufferLength-1) { m_szBuffer[m_iBufferPos] = '\0'; m_iBufferLength--; } else { if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL]) { // Make Ctrl-Delete delete up to end of line m_szBuffer[m_iBufferPos] = '\0'; m_iBufferLength = m_iBufferPos; } else { // Delete just one char and move the others left for(int j=m_iBufferPos; j<m_iBufferLength-1; j++) m_szBuffer[j] = m_szBuffer[j+1]; m_szBuffer[m_iBufferLength-1] = '\0'; m_iBufferLength--; } } return; case SDLK_HOME: if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL]) { CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory int linesShown = (int)m_fHeight/m_iFontHeight - 4; m_iMsgHistPos = clamp((int)m_deqMsgHistory.size() - linesShown, 1, (int)m_deqMsgHistory.size()); } else { m_iBufferPos = 0; } return; case SDLK_END: if (g_keys[SDLK_RCTRL] || g_keys[SDLK_LCTRL]) { m_iMsgHistPos = 1; } else { m_iBufferPos = m_iBufferLength; } return; case SDLK_LEFT: if (m_iBufferPos) m_iBufferPos--; return; case SDLK_RIGHT: if (m_iBufferPos != m_iBufferLength) m_iBufferPos++; return; // BEGIN: Buffer History Lookup case SDLK_UP: if ( m_deqBufHistory.size() ) { int oldHistoryPos = iHistoryPos; while( iHistoryPos != (int)m_deqBufHistory.size() - 1) { iHistoryPos++; std::wstring& histString = m_deqBufHistory.at(iHistoryPos); if((int)histString.length() >= m_iBufferPos) { bool bad = false; for(int i=0; i<m_iBufferPos; i++) { if(histString[i] != m_szBuffer[i]) { bad = true; break; } } if(!bad) { SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str()); return; } } } // if we got here, failed to find a string with the right prefix; // revert to the old position in case the user types more stuff iHistoryPos = oldHistoryPos; } return; case SDLK_DOWN: if ( m_deqBufHistory.size() && iHistoryPos > 0 ) { int oldHistoryPos = iHistoryPos; while( iHistoryPos != 0) { iHistoryPos--; std::wstring& histString = m_deqBufHistory.at(iHistoryPos); if((int)histString.length() >= m_iBufferPos) { bool bad = false; for(int i=0; i<m_iBufferPos; i++) { if(histString[i] != m_szBuffer[i]) { bad = true; break; } } if(!bad) { SetBuffer(m_deqBufHistory.at(iHistoryPos).c_str()); return; } } } // if we got here, failed to find a string with the right prefix; // revert to the old position in case the user types more stuff, // and also clear any complietion we might've added going up iHistoryPos = oldHistoryPos; m_szBuffer[m_iBufferPos] = 0; m_iBufferLength = m_iBufferPos; } return; // END: Buffer History Lookup // BEGIN: Message History Lookup case SDLK_PAGEUP: { CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory if (m_iMsgHistPos != (int)m_deqMsgHistory.size()) m_iMsgHistPos++; return; } case SDLK_PAGEDOWN: if (m_iMsgHistPos != 1) m_iMsgHistPos--; return; // END: Message History Lookup default: //Insert a character if (IsFull()) return; if (cooked == 0) return; if (IsEOB()) //are we at the end of the buffer? m_szBuffer[m_iBufferPos] = cooked; //cat char onto end else{ //we need to insert int i; for(i=m_iBufferLength; i>m_iBufferPos; i--) m_szBuffer[i] = m_szBuffer[i-1]; // move chars to right m_szBuffer[i] = cooked; } m_iBufferPos++; m_iBufferLength++; return; } }