diff --git a/src/Screen.cpp b/src/Screen.cpp index 90671829..94841a9a 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -1,1389 +1,1377 @@ /* This file is part of Konsole, an X terminal. Copyright 2007-2008 by Robert Knight Copyright 1997,1998 by Lars Doelle This program 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; either version 2 of the License, or (at your option) any later version. This program 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 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ // Own #include "Screen.h" // Qt #include // Konsole #include "konsole_wcwidth.h" #include "TerminalCharacterDecoder.h" #include "History.h" #include "ExtendedCharTable.h" using namespace Konsole; //Macro to convert x,y position on screen to position within an image. // //Originally the image was stored as one large contiguous block of //memory, so a position within the image could be represented as an //offset from the beginning of the block. For efficiency reasons this //is no longer the case. //Many internal parts of this class still use this representation for parameters and so on, //notably moveImage() and clearImage(). //This macro converts from an X,Y position into an image offset. #ifndef loc #define loc(X,Y) ((Y)*_columns+(X)) #endif const Character Screen::DefaultChar = Character(' ', CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR), CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR), DEFAULT_RENDITION, false); Screen::Screen(int lines, int columns): _lines(lines), _columns(columns), _screenLines(new ImageLine[_lines + 1]), _screenLinesSize(_lines), _scrolledLines(0), _droppedLines(0), _history(new HistoryScrollNone()), _cuX(0), _cuY(0), _currentRendition(DEFAULT_RENDITION), _topMargin(0), _bottomMargin(0), _selBegin(0), _selTopLeft(0), _selBottomRight(0), _blockSelectionMode(false), _effectiveForeground(CharacterColor()), _effectiveBackground(CharacterColor()), _effectiveRendition(DEFAULT_RENDITION), _lastPos(-1) { _lineProperties.resize(_lines + 1); for (int i = 0; i < _lines + 1; i++) _lineProperties[i] = LINE_DEFAULT; initTabStops(); clearSelection(); reset(); } Screen::~Screen() { delete[] _screenLines; delete _history; } void Screen::cursorUp(int n) //=CUU { if (n == 0) n = 1; // Default const int stop = _cuY < _topMargin ? 0 : _topMargin; _cuX = qMin(_columns - 1, _cuX); // nowrap! _cuY = qMax(stop, _cuY - n); } void Screen::cursorDown(int n) //=CUD { if (n == 0) n = 1; // Default const int stop = _cuY > _bottomMargin ? _lines - 1 : _bottomMargin; _cuX = qMin(_columns - 1, _cuX); // nowrap! _cuY = qMin(stop, _cuY + n); } void Screen::cursorLeft(int n) //=CUB { if (n == 0) n = 1; // Default _cuX = qMin(_columns - 1, _cuX); // nowrap! _cuX = qMax(0, _cuX - n); } void Screen::cursorRight(int n) //=CUF { if (n == 0) n = 1; // Default _cuX = qMin(_columns - 1, _cuX + n); } void Screen::setMargins(int top, int bot) //=STBM { if (top == 0) top = 1; // Default if (bot == 0) bot = _lines; // Default top = top - 1; // Adjust to internal lineno bot = bot - 1; // Adjust to internal lineno if (!(0 <= top && top < bot && bot < _lines)) { //Debug()<<" setRegion("< 0) _cuY -= 1; } void Screen::nextLine() //=NEL { toStartOfLine(); index(); } void Screen::eraseChars(int n) { if (n == 0) n = 1; // Default const int p = qMax(0, qMin(_cuX + n - 1, _columns - 1)); clearImage(loc(_cuX, _cuY), loc(p, _cuY), ' '); } void Screen::deleteChars(int n) { Q_ASSERT(n >= 0); // always delete at least one char if (n == 0) n = 1; // if cursor is beyond the end of the line there is nothing to do if (_cuX >= _screenLines[_cuY].count()) return; if (_cuX + n > _screenLines[_cuY].count()) n = _screenLines[_cuY].count() - _cuX; Q_ASSERT(n >= 0); Q_ASSERT(_cuX + n <= _screenLines[_cuY].count()); _screenLines[_cuY].remove(_cuX, n); // Append space(s) with current attributes Character spaceWithCurrentAttrs(' ', _effectiveForeground, _effectiveBackground, _effectiveRendition, false); for (int i = 0; i < n; i++) _screenLines[_cuY].append(spaceWithCurrentAttrs); } void Screen::insertChars(int n) { if (n == 0) n = 1; // Default if (_screenLines[_cuY].size() < _cuX) _screenLines[_cuY].resize(_cuX); _screenLines[_cuY].insert(_cuX, n, Character(' ')); if (_screenLines[_cuY].count() > _columns) _screenLines[_cuY].resize(_columns); } void Screen::deleteLines(int n) { if (n == 0) n = 1; // Default scrollUp(_cuY, n); } void Screen::insertLines(int n) { if (n == 0) n = 1; // Default scrollDown(_cuY, n); } void Screen::setMode(int m) { _currentModes[m] = 1; switch (m) { case MODE_Origin : _cuX = 0; _cuY = _topMargin; break; //FIXME: home } } void Screen::resetMode(int m) { _currentModes[m] = 0; switch (m) { case MODE_Origin : _cuX = 0; _cuY = 0; break; //FIXME: home } } void Screen::saveMode(int m) { _savedModes[m] = _currentModes[m]; } void Screen::restoreMode(int m) { _currentModes[m] = _savedModes[m]; } bool Screen::getMode(int m) const { return _currentModes[m] != 0; } void Screen::saveCursor() { _savedState.cursorColumn = _cuX; _savedState.cursorLine = _cuY; _savedState.rendition = _currentRendition; _savedState.foreground = _currentForeground; _savedState.background = _currentBackground; } void Screen::restoreCursor() { _cuX = qMin(_savedState.cursorColumn, _columns - 1); _cuY = qMin(_savedState.cursorLine, _lines - 1); _currentRendition = _savedState.rendition; _currentForeground = _savedState.foreground; _currentBackground = _savedState.background; updateEffectiveRendition(); } void Screen::resizeImage(int new_lines, int new_columns) { if ((new_lines == _lines) && (new_columns == _columns)) return; if (_cuY > new_lines - 1) { // attempt to preserve focus and _lines _bottomMargin = _lines - 1; //FIXME: margin lost for (int i = 0; i < _cuY - (new_lines - 1); i++) { addHistLine(); scrollUp(0, 1); } } // create new screen _lines and copy from old to new auto newScreenLines = new ImageLine[new_lines + 1]; for (int i = 0; i < qMin(_lines, new_lines + 1) ; i++) newScreenLines[i] = _screenLines[i]; for (int i = _lines; (i > 0) && (i < new_lines + 1); i++) newScreenLines[i].resize(new_columns); _lineProperties.resize(new_lines + 1); for (int i = _lines; (i > 0) && (i < new_lines + 1); i++) _lineProperties[i] = LINE_DEFAULT; clearSelection(); delete[] _screenLines; _screenLines = newScreenLines; _screenLinesSize = new_lines; _lines = new_lines; _columns = new_columns; _cuX = qMin(_cuX, _columns - 1); _cuY = qMin(_cuY, _lines - 1); // FIXME: try to keep values, evtl. _topMargin = 0; _bottomMargin = _lines - 1; initTabStops(); clearSelection(); } void Screen::setDefaultMargins() { _topMargin = 0; _bottomMargin = _lines - 1; } /* Clarifying rendition here and in the display. The rendition attributes are attribute -------------- RE_UNDERLINE RE_BLINK RE_BOLD RE_REVERSE RE_TRANSPARENT RE_FAINT RE_STRIKEOUT RE_CONCEAL RE_OVERLINE Depending on settings, bold may be rendered as a heavier font in addition to a different color. */ void Screen::reverseRendition(Character& p) const { CharacterColor f = p.foregroundColor; CharacterColor b = p.backgroundColor; p.foregroundColor = b; p.backgroundColor = f; //p->r &= ~RE_TRANSPARENT; } void Screen::updateEffectiveRendition() { _effectiveRendition = _currentRendition; if ((_currentRendition & RE_REVERSE) != 0) { _effectiveForeground = _currentBackground; _effectiveBackground = _currentForeground; } else { _effectiveForeground = _currentForeground; _effectiveBackground = _currentBackground; } if ((_currentRendition & RE_BOLD) != 0) { if ((_currentRendition & RE_FAINT) == 0) _effectiveForeground.setIntensive(); } else { if ((_currentRendition & RE_FAINT) != 0) _effectiveForeground.setFaint(); } } void Screen::copyFromHistory(Character* dest, int startLine, int count) const { Q_ASSERT(startLine >= 0 && count > 0 && startLine + count <= _history->getLines()); for (int line = startLine; line < startLine + count; line++) { const int length = qMin(_columns, _history->getLineLen(line)); const int destLineOffset = (line - startLine) * _columns; _history->getCells(line, 0, length, dest + destLineOffset); for (int column = length; column < _columns; column++) dest[destLineOffset + column] = Screen::DefaultChar; // invert selected text if (_selBegin != -1) { for (int column = 0; column < _columns; column++) { if (isSelected(column, line)) { reverseRendition(dest[destLineOffset + column]); } } } } } void Screen::copyFromScreen(Character* dest , int startLine , int count) const { Q_ASSERT(startLine >= 0 && count > 0 && startLine + count <= _lines); for (int line = startLine; line < (startLine + count) ; line++) { int srcLineStartIndex = line * _columns; int destLineStartIndex = (line - startLine) * _columns; for (int column = 0; column < _columns; column++) { int srcIndex = srcLineStartIndex + column; int destIndex = destLineStartIndex + column; dest[destIndex] = _screenLines[srcIndex / _columns].value(srcIndex % _columns, Screen::DefaultChar); // invert selected text if (_selBegin != -1 && isSelected(column, line + _history->getLines())) reverseRendition(dest[destIndex]); } } } void Screen::getImage(Character* dest, int size, int startLine, int endLine) const { Q_ASSERT(startLine >= 0); Q_ASSERT(endLine >= startLine && endLine < _history->getLines() + _lines); const int mergedLines = endLine - startLine + 1; Q_ASSERT(size >= mergedLines * _columns); Q_UNUSED(size); const int linesInHistoryBuffer = qBound(0, _history->getLines() - startLine, mergedLines); const int linesInScreenBuffer = mergedLines - linesInHistoryBuffer; // copy _lines from history buffer if (linesInHistoryBuffer > 0) copyFromHistory(dest, startLine, linesInHistoryBuffer); // copy _lines from screen buffer if (linesInScreenBuffer > 0) copyFromScreen(dest + linesInHistoryBuffer * _columns, startLine + linesInHistoryBuffer - _history->getLines(), linesInScreenBuffer); // invert display when in screen mode if (getMode(MODE_Screen)) { for (int i = 0; i < mergedLines * _columns; i++) reverseRendition(dest[i]); // for reverse display } // mark the character at the current cursor position int cursorIndex = loc(_cuX, _cuY + linesInHistoryBuffer); if (getMode(MODE_Cursor) && cursorIndex < _columns * mergedLines) dest[cursorIndex].rendition |= RE_CURSOR; } QVector Screen::getLineProperties(int startLine , int endLine) const { Q_ASSERT(startLine >= 0); Q_ASSERT(endLine >= startLine && endLine < _history->getLines() + _lines); const int mergedLines = endLine - startLine + 1; const int linesInHistory = qBound(0, _history->getLines() - startLine, mergedLines); const int linesInScreen = mergedLines - linesInHistory; QVector result(mergedLines); int index = 0; // copy properties for _lines in history for (int line = startLine; line < startLine + linesInHistory; line++) { //TODO Support for line properties other than wrapped _lines if (_history->isWrappedLine(line)) { result[index] = (LineProperty)(result[index] | LINE_WRAPPED); } index++; } // copy properties for _lines in screen buffer const int firstScreenLine = startLine + linesInHistory - _history->getLines(); for (int line = firstScreenLine; line < firstScreenLine + linesInScreen; line++) { result[index] = _lineProperties[line]; index++; } return result; } void Screen::reset() { // Clear screen, but preserve the current line scrollUp(0, _cuY); _cuY = 0; _currentModes[MODE_Origin] = 0; _savedModes[MODE_Origin] = 0; setMode(MODE_Wrap); saveMode(MODE_Wrap); // wrap at end of margin resetMode(MODE_Insert); saveMode(MODE_Insert); // overstroke setMode(MODE_Cursor); // cursor visible resetMode(MODE_Screen); // screen not inverse resetMode(MODE_NewLine); _topMargin = 0; _bottomMargin = _lines - 1; // Other terminal emulators reset the entire scroll history during a reset // setScroll(getScroll(), false); setDefaultRendition(); saveCursor(); } -void Screen::clear() -{ - clearEntireScreen(); - home(); -} - void Screen::backspace() { _cuX = qMin(_columns - 1, _cuX); // nowrap! _cuX = qMax(0, _cuX - 1); if (_screenLines[_cuY].size() < _cuX + 1) _screenLines[_cuY].resize(_cuX + 1); } void Screen::tab(int n) { // note that TAB is a format effector (does not write ' '); if (n == 0) n = 1; while ((n > 0) && (_cuX < _columns - 1)) { cursorRight(1); while ((_cuX < _columns - 1) && !_tabStops[_cuX]) cursorRight(1); n--; } } void Screen::backtab(int n) { // note that TAB is a format effector (does not write ' '); if (n == 0) n = 1; while ((n > 0) && (_cuX > 0)) { cursorLeft(1); while ((_cuX > 0) && !_tabStops[_cuX]) { cursorLeft(1); } n--; } } void Screen::clearTabStops() { for (int i = 0; i < _columns; i++) _tabStops[i] = false; } void Screen::changeTabStop(bool set) { if (_cuX >= _columns) return; _tabStops[_cuX] = set; } void Screen::initTabStops() { _tabStops.resize(_columns); // The 1st tabstop has to be one longer than the other. // i.e. the kids start counting from 0 instead of 1. // Other programs might behave correctly. Be aware. for (int i = 0; i < _columns; i++) _tabStops[i] = (i % 8 == 0 && i != 0); } void Screen::newLine() { if (getMode(MODE_NewLine)) toStartOfLine(); index(); } void Screen::checkSelection(int from, int to) { if (_selBegin == -1) return; const int scr_TL = loc(0, _history->getLines()); //Clear entire selection if it overlaps region [from, to] if ((_selBottomRight >= (from + scr_TL)) && (_selTopLeft <= (to + scr_TL))) clearSelection(); } void Screen::displayCharacter(unsigned short c) { // Note that VT100 does wrapping BEFORE putting the character. // This has impact on the assumption of valid cursor positions. // We indicate the fact that a newline has to be triggered by // putting the cursor one right to the last column of the screen. int w = konsole_wcwidth(c); if (w < 0) { // Non-printable character return; } else if (w == 0) { const QChar::Category category = QChar(c).category(); if (category != QChar::Mark_NonSpacing && category != QChar::Letter_Other && !QChar::isLowSurrogate(c)) { return; } // Find previous "real character" to try to combine with int charToCombineWithX = qMin(_cuX, _screenLines[_cuY].length()); int charToCombineWithY = _cuY; do { if (charToCombineWithX > 0) { charToCombineWithX--; } else if (charToCombineWithY > 0) { // Try previous line charToCombineWithY--; charToCombineWithX = _screenLines[charToCombineWithY].length() - 1; } else { // Give up return; } // Failsafe if (charToCombineWithX < 0) { return; } } while(!_screenLines[charToCombineWithY][charToCombineWithX].isRealCharacter); Character& currentChar = _screenLines[charToCombineWithY][charToCombineWithX]; if ((currentChar.rendition & RE_EXTENDED_CHAR) == 0) { const ushort chars[2] = { currentChar.character, c }; currentChar.rendition |= RE_EXTENDED_CHAR; currentChar.character = ExtendedCharTable::instance.createExtendedChar(chars, 2); } else { ushort extendedCharLength; const ushort* oldChars = ExtendedCharTable::instance.lookupExtendedChar(currentChar.character, extendedCharLength); Q_ASSERT(oldChars); if (((oldChars) != nullptr) && extendedCharLength < 3) { Q_ASSERT(extendedCharLength > 1); Q_ASSERT(extendedCharLength < 65535); auto chars = new ushort[extendedCharLength + 1]; memcpy(chars, oldChars, sizeof(ushort) * extendedCharLength); chars[extendedCharLength] = c; currentChar.character = ExtendedCharTable::instance.createExtendedChar(chars, extendedCharLength + 1); delete[] chars; } } return; } if (_cuX + w > _columns) { if (getMode(MODE_Wrap)) { _lineProperties[_cuY] = (LineProperty)(_lineProperties[_cuY] | LINE_WRAPPED); nextLine(); } else { _cuX = _columns - w; } } // ensure current line vector has enough elements if (_screenLines[_cuY].size() < _cuX + w) { _screenLines[_cuY].resize(_cuX + w); } if (getMode(MODE_Insert)) insertChars(w); _lastPos = loc(_cuX, _cuY); // check if selection is still valid. checkSelection(_lastPos, _lastPos); Character& currentChar = _screenLines[_cuY][_cuX]; currentChar.character = c; currentChar.foregroundColor = _effectiveForeground; currentChar.backgroundColor = _effectiveBackground; currentChar.rendition = _effectiveRendition; currentChar.isRealCharacter = true; int i = 0; const int newCursorX = _cuX + w--; while (w != 0) { i++; if (_screenLines[_cuY].size() < _cuX + i + 1) _screenLines[_cuY].resize(_cuX + i + 1); Character& ch = _screenLines[_cuY][_cuX + i]; ch.character = 0; ch.foregroundColor = _effectiveForeground; ch.backgroundColor = _effectiveBackground; ch.rendition = _effectiveRendition; ch.isRealCharacter = false; w--; } _cuX = newCursorX; } int Screen::scrolledLines() const { return _scrolledLines; } int Screen::droppedLines() const { return _droppedLines; } void Screen::resetDroppedLines() { _droppedLines = 0; } void Screen::resetScrolledLines() { _scrolledLines = 0; } void Screen::scrollUp(int n) { if (n == 0) n = 1; // Default if (_topMargin == 0) addHistLine(); // history.history scrollUp(_topMargin, n); } QRect Screen::lastScrolledRegion() const { return _lastScrolledRegion; } void Screen::scrollUp(int from, int n) { if (n <= 0 || from + n > _bottomMargin) return; _scrolledLines -= n; _lastScrolledRegion = QRect(0, _topMargin, _columns - 1, (_bottomMargin - _topMargin)); //FIXME: make sure `topMargin', `bottomMargin', `from', `n' is in bounds. moveImage(loc(0, from), loc(0, from + n), loc(_columns - 1, _bottomMargin)); clearImage(loc(0, _bottomMargin - n + 1), loc(_columns - 1, _bottomMargin), ' '); } void Screen::scrollDown(int n) { if (n == 0) n = 1; // Default scrollDown(_topMargin, n); } void Screen::scrollDown(int from, int n) { _scrolledLines += n; //FIXME: make sure `topMargin', `bottomMargin', `from', `n' is in bounds. if (n <= 0) return; if (from > _bottomMargin) return; if (from + n > _bottomMargin) n = _bottomMargin - from; moveImage(loc(0, from + n), loc(0, from), loc(_columns - 1, _bottomMargin - n)); clearImage(loc(0, from), loc(_columns - 1, from + n - 1), ' '); } void Screen::setCursorYX(int y, int x) { setCursorY(y); setCursorX(x); } void Screen::setCursorX(int x) { if (x == 0) x = 1; // Default x -= 1; // Adjust _cuX = qMax(0, qMin(_columns - 1, x)); } void Screen::setCursorY(int y) { if (y == 0) y = 1; // Default y -= 1; // Adjust _cuY = qMax(0, qMin(_lines - 1, y + (getMode(MODE_Origin) ? _topMargin : 0))); } -void Screen::home() -{ - _cuX = 0; - _cuY = 0; -} - void Screen::toStartOfLine() { _cuX = 0; } int Screen::getCursorX() const { return _cuX; } int Screen::getCursorY() const { return _cuY; } void Screen::clearImage(int loca, int loce, char c) { const int scr_TL = loc(0, _history->getLines()); //FIXME: check positions //Clear entire selection if it overlaps region to be moved... if ((_selBottomRight > (loca + scr_TL)) && (_selTopLeft < (loce + scr_TL))) { clearSelection(); } const int topLine = loca / _columns; const int bottomLine = loce / _columns; Character clearCh(c, _currentForeground, _currentBackground, DEFAULT_RENDITION, false); //if the character being used to clear the area is the same as the //default character, the affected _lines can simply be shrunk. const bool isDefaultCh = (clearCh == Screen::DefaultChar); for (int y = topLine; y <= bottomLine; y++) { _lineProperties[y] = 0; const int endCol = (y == bottomLine) ? loce % _columns : _columns - 1; const int startCol = (y == topLine) ? loca % _columns : 0; QVector& line = _screenLines[y]; if (isDefaultCh && endCol == _columns - 1) { line.resize(startCol); } else { if (line.size() < endCol + 1) line.resize(endCol + 1); Character* data = line.data(); for (int i = startCol; i <= endCol; i++) data[i] = clearCh; } } } void Screen::moveImage(int dest, int sourceBegin, int sourceEnd) { Q_ASSERT(sourceBegin <= sourceEnd); const int lines = (sourceEnd - sourceBegin) / _columns; //move screen image and line properties: //the source and destination areas of the image may overlap, //so it matters that we do the copy in the right order - //forwards if dest < sourceBegin or backwards otherwise. //(search the web for 'memmove implementation' for details) if (dest < sourceBegin) { for (int i = 0; i <= lines; i++) { _screenLines[(dest / _columns) + i ] = _screenLines[(sourceBegin / _columns) + i ]; _lineProperties[(dest / _columns) + i] = _lineProperties[(sourceBegin / _columns) + i]; } } else { for (int i = lines; i >= 0; i--) { _screenLines[(dest / _columns) + i ] = _screenLines[(sourceBegin / _columns) + i ]; _lineProperties[(dest / _columns) + i] = _lineProperties[(sourceBegin / _columns) + i]; } } if (_lastPos != -1) { const int diff = dest - sourceBegin; // Scroll by this amount _lastPos += diff; if ((_lastPos < 0) || (_lastPos >= (lines * _columns))) _lastPos = -1; } // Adjust selection to follow scroll. if (_selBegin != -1) { const bool beginIsTL = (_selBegin == _selTopLeft); const int diff = dest - sourceBegin; // Scroll by this amount const int scr_TL = loc(0, _history->getLines()); const int srca = sourceBegin + scr_TL; // Translate index from screen to global const int srce = sourceEnd + scr_TL; // Translate index from screen to global const int desta = srca + diff; const int deste = srce + diff; if ((_selTopLeft >= srca) && (_selTopLeft <= srce)) _selTopLeft += diff; else if ((_selTopLeft >= desta) && (_selTopLeft <= deste)) _selBottomRight = -1; // Clear selection (see below) if ((_selBottomRight >= srca) && (_selBottomRight <= srce)) _selBottomRight += diff; else if ((_selBottomRight >= desta) && (_selBottomRight <= deste)) _selBottomRight = -1; // Clear selection (see below) if (_selBottomRight < 0) { clearSelection(); } else { if (_selTopLeft < 0) _selTopLeft = 0; } if (beginIsTL) _selBegin = _selTopLeft; else _selBegin = _selBottomRight; } } void Screen::clearToEndOfScreen() { clearImage(loc(_cuX, _cuY), loc(_columns - 1, _lines - 1), ' '); } void Screen::clearToBeginOfScreen() { clearImage(loc(0, 0), loc(_cuX, _cuY), ' '); } void Screen::clearEntireScreen() { // Add entire screen to history for (int i = 0; i < (_lines - 1); i++) { addHistLine(); scrollUp(0, 1); } clearImage(loc(0, 0), loc(_columns - 1, _lines - 1), ' '); } /*! fill screen with 'E' This is to aid screen alignment */ void Screen::helpAlign() { clearImage(loc(0, 0), loc(_columns - 1, _lines - 1), 'E'); } void Screen::clearToEndOfLine() { clearImage(loc(_cuX, _cuY), loc(_columns - 1, _cuY), ' '); } void Screen::clearToBeginOfLine() { clearImage(loc(0, _cuY), loc(_cuX, _cuY), ' '); } void Screen::clearEntireLine() { clearImage(loc(0, _cuY), loc(_columns - 1, _cuY), ' '); } void Screen::setRendition(RenditionFlags rendention) { _currentRendition |= rendention; updateEffectiveRendition(); } void Screen::resetRendition(RenditionFlags rendention) { _currentRendition &= ~rendention; updateEffectiveRendition(); } void Screen::setDefaultRendition() { setForeColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR); setBackColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR); _currentRendition = DEFAULT_RENDITION; updateEffectiveRendition(); } void Screen::setForeColor(int space, int color) { _currentForeground = CharacterColor(space, color); if (_currentForeground.isValid()) updateEffectiveRendition(); else setForeColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR); } void Screen::setBackColor(int space, int color) { _currentBackground = CharacterColor(space, color); if (_currentBackground.isValid()) updateEffectiveRendition(); else setBackColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR); } void Screen::clearSelection() { _selBottomRight = -1; _selTopLeft = -1; _selBegin = -1; } void Screen::getSelectionStart(int& column , int& line) const { if (_selTopLeft != -1) { column = _selTopLeft % _columns; line = _selTopLeft / _columns; } else { column = _cuX + getHistLines(); line = _cuY + getHistLines(); } } void Screen::getSelectionEnd(int& column , int& line) const { if (_selBottomRight != -1) { column = _selBottomRight % _columns; line = _selBottomRight / _columns; } else { column = _cuX + getHistLines(); line = _cuY + getHistLines(); } } void Screen::setSelectionStart(const int x, const int y, const bool blockSelectionMode) { _selBegin = loc(x, y); /* FIXME, HACK to correct for x too far to the right... */ if (x == _columns) _selBegin--; _selBottomRight = _selBegin; _selTopLeft = _selBegin; _blockSelectionMode = blockSelectionMode; } void Screen::setSelectionEnd(const int x, const int y) { if (_selBegin == -1) return; int endPos = loc(x, y); if (endPos < _selBegin) { _selTopLeft = endPos; _selBottomRight = _selBegin; } else { /* FIXME, HACK to correct for x too far to the right... */ if (x == _columns) endPos--; _selTopLeft = _selBegin; _selBottomRight = endPos; } // Normalize the selection in column mode if (_blockSelectionMode) { const int topRow = _selTopLeft / _columns; const int topColumn = _selTopLeft % _columns; const int bottomRow = _selBottomRight / _columns; const int bottomColumn = _selBottomRight % _columns; _selTopLeft = loc(qMin(topColumn, bottomColumn), topRow); _selBottomRight = loc(qMax(topColumn, bottomColumn), bottomRow); } } bool Screen::isSelected(const int x, const int y) const { bool columnInSelection = true; if (_blockSelectionMode) { columnInSelection = x >= (_selTopLeft % _columns) && x <= (_selBottomRight % _columns); } const int pos = loc(x, y); return pos >= _selTopLeft && pos <= _selBottomRight && columnInSelection; } QString Screen::selectedText(bool preserveLineBreaks, bool trimTrailingSpaces, bool html) const { if (!isSelectionValid()) return QString(); return text(_selTopLeft, _selBottomRight, preserveLineBreaks, trimTrailingSpaces, html); } QString Screen::text(int startIndex, int endIndex, bool preserveLineBreaks, bool trimTrailingSpaces, bool html) const { QString result; QTextStream stream(&result, QIODevice::ReadWrite); HTMLDecoder htmlDecoder; PlainTextDecoder plainTextDecoder; TerminalCharacterDecoder *decoder; if(html) { decoder = &htmlDecoder; } else { decoder = &plainTextDecoder; } decoder->begin(&stream); writeToStream(decoder, startIndex, endIndex, preserveLineBreaks, trimTrailingSpaces); decoder->end(); return result; } bool Screen::isSelectionValid() const { return _selTopLeft >= 0 && _selBottomRight >= 0; } void Screen::writeSelectionToStream(TerminalCharacterDecoder* decoder , bool preserveLineBreaks, bool trimTrailingSpaces) const { if (!isSelectionValid()) return; writeToStream(decoder, _selTopLeft, _selBottomRight, preserveLineBreaks, trimTrailingSpaces); } void Screen::writeToStream(TerminalCharacterDecoder* decoder, int startIndex, int endIndex, bool preserveLineBreaks, bool trimTrailingSpaces) const { const int top = startIndex / _columns; const int left = startIndex % _columns; const int bottom = endIndex / _columns; const int right = endIndex % _columns; Q_ASSERT(top >= 0 && left >= 0 && bottom >= 0 && right >= 0); for (int y = top; y <= bottom; y++) { int start = 0; if (y == top || _blockSelectionMode) start = left; int count = -1; if (y == bottom || _blockSelectionMode) count = right - start + 1; const bool appendNewLine = (y != bottom); int copied = copyLineToStream(y, start, count, decoder, appendNewLine, preserveLineBreaks, trimTrailingSpaces); // if the selection goes beyond the end of the last line then // append a new line character. // // this makes it possible to 'select' a trailing new line character after // the text on a line. if (y == bottom && copied < count && !trimTrailingSpaces) { Character newLineChar('\n'); decoder->decodeLine(&newLineChar, 1, 0); } } } int Screen::copyLineToStream(int line , int start, int count, TerminalCharacterDecoder* decoder, bool appendNewLine, bool preserveLineBreaks, bool trimTrailingSpaces) const { //buffer to hold characters for decoding //the buffer is static to avoid initializing every //element on each call to copyLineToStream //(which is unnecessary since all elements will be overwritten anyway) static const int MAX_CHARS = 1024; static Character characterBuffer[MAX_CHARS]; Q_ASSERT(count < MAX_CHARS); LineProperty currentLineProperties = 0; //determine if the line is in the history buffer or the screen image if (line < _history->getLines()) { const int lineLength = _history->getLineLen(line); // ensure that start position is before end of line start = qMin(start, qMax(0, lineLength - 1)); // retrieve line from history buffer. It is assumed // that the history buffer does not store trailing white space // at the end of the line, so it does not need to be trimmed here if (count == -1) { count = lineLength - start; } else { count = qMin(start + count, lineLength) - start; } // safety checks Q_ASSERT(start >= 0); Q_ASSERT(count >= 0); Q_ASSERT((start + count) <= _history->getLineLen(line)); _history->getCells(line, start, count, characterBuffer); if (_history->isWrappedLine(line)) currentLineProperties |= LINE_WRAPPED; } else { if (count == -1) count = _columns - start; Q_ASSERT(count >= 0); int screenLine = line - _history->getLines(); // FIXME: This can be triggered when clearing history // while having the searchbar open and selecting next/prev Q_ASSERT(screenLine <= _screenLinesSize); screenLine = qMin(screenLine, _screenLinesSize); Character* data = _screenLines[screenLine].data(); int length = _screenLines[screenLine].count(); // Don't remove end spaces in lines that wrap if (trimTrailingSpaces && ((_lineProperties[screenLine] & LINE_WRAPPED) == 0)) { // ignore trailing white space at the end of the line for (int i = length-1; i >= 0; i--) { if (QChar(data[i].character).isSpace()) length--; else break; } } //retrieve line from screen image for (int i = start; i < qMin(start + count, length); i++) { characterBuffer[i - start] = data[i]; } // count cannot be any greater than length count = qBound(0, count, length - start); Q_ASSERT(screenLine < _lineProperties.count()); currentLineProperties |= _lineProperties[screenLine]; } if (appendNewLine && (count + 1 < MAX_CHARS)) { if ((currentLineProperties & LINE_WRAPPED) != 0) { // do nothing extra when this line is wrapped. } else { // When users ask not to preserve the linebreaks, they usually mean: // `treat LINEBREAK as SPACE, thus joining multiple _lines into // single line in the same way as 'J' does in VIM.` characterBuffer[count] = preserveLineBreaks ? Character('\n') : Character(' '); count++; } } //decode line and write to text stream decoder->decodeLine((Character*) characterBuffer , count, currentLineProperties); return count; } void Screen::writeLinesToStream(TerminalCharacterDecoder* decoder, int fromLine, int toLine) const { writeToStream(decoder, loc(0, fromLine), loc(_columns - 1, toLine)); } void Screen::addHistLine() { // add line to history buffer // we have to take care about scrolling, too... if (hasScroll()) { const int oldHistLines = _history->getLines(); _history->addCellsVector(_screenLines[0]); _history->addLine((_lineProperties[0] & LINE_WRAPPED) != 0); const int newHistLines = _history->getLines(); const bool beginIsTL = (_selBegin == _selTopLeft); // If the history is full, increment the count // of dropped _lines if (newHistLines == oldHistLines) _droppedLines++; // Adjust selection for the new point of reference if (newHistLines > oldHistLines) { if (_selBegin != -1) { _selTopLeft += _columns; _selBottomRight += _columns; } } if (_selBegin != -1) { // Scroll selection in history up const int top_BR = loc(0, 1 + newHistLines); if (_selTopLeft < top_BR) _selTopLeft -= _columns; if (_selBottomRight < top_BR) _selBottomRight -= _columns; if (_selBottomRight < 0) { clearSelection(); } else { if (_selTopLeft < 0) _selTopLeft = 0; } if (beginIsTL) _selBegin = _selTopLeft; else _selBegin = _selBottomRight; } } } int Screen::getHistLines() const { return _history->getLines(); } void Screen::setScroll(const HistoryType& t , bool copyPreviousScroll) { clearSelection(); if (copyPreviousScroll) { _history = t.scroll(_history); } else { HistoryScroll* oldScroll = _history; _history = t.scroll(0); delete oldScroll; } } bool Screen::hasScroll() const { return _history->hasScroll(); } const HistoryType& Screen::getScroll() const { return _history->getType(); } void Screen::setLineProperty(LineProperty property , bool enable) { if (enable) _lineProperties[_cuY] = (LineProperty)(_lineProperties[_cuY] | property); else _lineProperties[_cuY] = (LineProperty)(_lineProperties[_cuY] & ~property); } void Screen::fillWithDefaultChar(Character* dest, int count) { for (int i = 0; i < count; i++) dest[i] = Screen::DefaultChar; } diff --git a/src/Screen.h b/src/Screen.h index cd8d5213..e7aebcf6 100644 --- a/src/Screen.h +++ b/src/Screen.h @@ -1,729 +1,720 @@ /* This file is part of Konsole, KDE's terminal. Copyright 2007-2008 by Robert Knight Copyright 1997,1998 by Lars Doelle This program 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; either version 2 of the License, or (at your option) any later version. This program 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 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef SCREEN_H #define SCREEN_H // Qt #include #include #include #include #include // Konsole #include "Character.h" #define MODE_Origin 0 #define MODE_Wrap 1 #define MODE_Insert 2 #define MODE_Screen 3 #define MODE_Cursor 4 #define MODE_NewLine 5 #define MODES_SCREEN 6 namespace Konsole { class TerminalCharacterDecoder; class TerminalDisplay; class HistoryType; class HistoryScroll; /** \brief An image of characters with associated attributes. The terminal emulation ( Emulation ) receives a serial stream of characters from the program currently running in the terminal. From this stream it creates an image of characters which is ultimately rendered by the display widget ( TerminalDisplay ). Some types of emulation may have more than one screen image. getImage() is used to retrieve the currently visible image which is then used by the display widget to draw the output from the terminal. The number of lines of output history which are kept in addition to the current screen image depends on the history scroll being used to store the output. The scroll is specified using setScroll() The output history can be retrieved using writeToStream() The screen image has a selection associated with it, specified using setSelectionStart() and setSelectionEnd(). The selected text can be retrieved using selectedText(). When getImage() is used to retrieve the visible image, characters which are part of the selection have their colors inverted. */ class Screen { public: /** Construct a new screen image of size @p lines by @p columns. */ Screen(int lines, int columns); ~Screen(); Screen(const Screen &) = delete; Screen &operator=(const Screen &) = delete; // VT100/2 Operations // Cursor Movement /** * Move the cursor up by @p n lines. The cursor will stop at the * top margin. */ void cursorUp(int n); /** * Move the cursor down by @p n lines. The cursor will stop at the * bottom margin. */ void cursorDown(int n); /** * Move the cursor to the left by @p n columns. * The cursor will stop at the first column. */ void cursorLeft(int n); /** * Move the cursor to the right by @p n columns. * The cursor will stop at the right-most column. */ void cursorRight(int n); /** Position the cursor on line @p y. */ void setCursorY(int y); /** Position the cursor at column @p x. */ void setCursorX(int x); /** Position the cursor at line @p y, column @p x. */ void setCursorYX(int y, int x); /** * Sets the margins for scrolling the screen. * * @param topLine The top line of the new scrolling margin. * @param bottomLine The bottom line of the new scrolling margin. */ void setMargins(int topLine, int bottomLine); /** Returns the top line of the scrolling region. */ int topMargin() const; /** Returns the bottom line of the scrolling region. */ int bottomMargin() const; /** * Resets the scrolling margins back to the top and bottom lines * of the screen. */ void setDefaultMargins(); /** * Moves the cursor down one line, if the MODE_NewLine mode * flag is enabled then the cursor is returned to the leftmost * column first. * * Equivalent to NextLine() if the MODE_NewLine flag is set * or index() otherwise. */ void newLine(); /** * Moves the cursor down one line and positions it at the beginning * of the line. Equivalent to calling Return() followed by index() */ void nextLine(); /** * Move the cursor down one line. If the cursor is on the bottom * line of the scrolling region (as returned by bottomMargin()) the * scrolling region is scrolled up by one line instead. */ void index(); /** * Move the cursor up one line. If the cursor is on the top line * of the scrolling region (as returned by topMargin()) the scrolling * region is scrolled down by one line instead. */ void reverseIndex(); /** * Scroll the scrolling region of the screen up by @p n lines. * The scrolling region is initially the whole screen, but can be changed * using setMargins() */ void scrollUp(int n); /** * Scroll the scrolling region of the screen down by @p n lines. * The scrolling region is initially the whole screen, but can be changed * using setMargins() */ void scrollDown(int n); /** * Moves the cursor to the beginning of the current line. * Equivalent to setCursorX(0) */ void toStartOfLine(); /** * Moves the cursor one column to the left and erases the character * at the new cursor position. */ void backspace(); /** Moves the cursor @p n tab-stops to the right. */ void tab(int n = 1); /** Moves the cursor @p n tab-stops to the left. */ void backtab(int n); // Editing /** * Erase @p n characters beginning from the current cursor position. * This is equivalent to over-writing @p n characters starting with the current * cursor position with spaces. * If @p n is 0 then one character is erased. */ void eraseChars(int n); /** * Delete @p n characters beginning from the current cursor position. * If @p n is 0 then one character is deleted. */ void deleteChars(int n); /** * Insert @p n blank characters beginning from the current cursor position. * The position of the cursor is not altered. * If @p n is 0 then one character is inserted. */ void insertChars(int n); /** * Removes @p n lines beginning from the current cursor position. * The position of the cursor is not altered. * If @p n is 0 then one line is removed. */ void deleteLines(int n); /** * Inserts @p lines beginning from the current cursor position. * The position of the cursor is not altered. * If @p n is 0 then one line is inserted. */ void insertLines(int n); /** Clears all the tab stops. */ void clearTabStops(); /** Sets or removes a tab stop at the cursor's current column. */ void changeTabStop(bool set); /** Resets (clears) the specified screen @p mode. */ void resetMode(int mode); /** Sets (enables) the specified screen @p mode. */ void setMode(int mode); /** * Saves the state of the specified screen @p mode. It can be restored * using restoreMode() */ void saveMode(int mode); /** Restores the state of a screen @p mode saved by calling saveMode() */ void restoreMode(int mode); /** Returns whether the specified screen @p mode is enabled or not .*/ bool getMode(int mode) const; /** * Saves the current position and appearance (text color and style) of the cursor. * It can be restored by calling restoreCursor() */ void saveCursor(); /** Restores the position and appearance of the cursor. See saveCursor() */ void restoreCursor(); /** Clear the whole screen, moving the current screen contents into the history first. */ void clearEntireScreen(); /** * Clear the area of the screen from the current cursor position to the end of * the screen. */ void clearToEndOfScreen(); /** * Clear the area of the screen from the current cursor position to the start * of the screen. */ void clearToBeginOfScreen(); /** Clears the whole of the line on which the cursor is currently positioned. */ void clearEntireLine(); /** Clears from the current cursor position to the end of the line. */ void clearToEndOfLine(); /** Clears from the current cursor position to the beginning of the line. */ void clearToBeginOfLine(); /** Fills the entire screen with the letter 'E' */ void helpAlign(); /** * Enables the given @p rendition flag. Rendition flags control the appearance * of characters on the screen. * * @see Character::rendition */ void setRendition(RenditionFlags rendition); /** * Disables the given @p rendition flag. Rendition flags control the appearance * of characters on the screen. * * @see Character::rendition */ void resetRendition(RenditionFlags rendition); /** * Sets the cursor's foreground color. * @param space The color space used by the @p color argument * @param color The new foreground color. The meaning of this depends on * the color @p space used. * * @see CharacterColor */ void setForeColor(int space, int color); /** * Sets the cursor's background color. * @param space The color space used by the @p color argument. * @param color The new background color. The meaning of this depends on * the color @p space used. * * @see CharacterColor */ void setBackColor(int space, int color); /** * Resets the cursor's color back to the default and sets the * character's rendition flags back to the default settings. */ void setDefaultRendition(); /** Returns the column which the cursor is positioned at. */ int getCursorX() const; /** Returns the line which the cursor is positioned on. */ int getCursorY() const; - /** Clear the entire screen and move the cursor to the home position. - * Equivalent to calling clearEntireScreen() followed by home(). - */ - void clear(); - /** - * Sets the position of the cursor to the 'home' position at the top-left - * corner of the screen (0,0) - */ - void home(); /** * Resets the state of the screen. This resets the various screen modes * back to their default states. The cursor style and colors are reset * (as if setDefaultRendition() had been called) * *
    *
  • Line wrapping is enabled.
  • *
  • Origin mode is disabled.
  • *
  • Insert mode is disabled.
  • *
  • Cursor mode is enabled. TODO Document me
  • *
  • Screen mode is disabled. TODO Document me
  • *
  • New line mode is disabled. TODO Document me
  • *
* * If @p clearScreen is true then the screen contents are erased entirely, * otherwise they are unaltered. */ void reset(); /** * Displays a new character at the current cursor position. * * If the cursor is currently positioned at the right-edge of the screen and * line wrapping is enabled then the character is added at the start of a new * line below the current one. * * If the MODE_Insert screen mode is currently enabled then the character * is inserted at the current cursor position, otherwise it will replace the * character already at the current cursor position. */ void displayCharacter(unsigned short c); /** * Resizes the image to a new fixed size of @p new_lines by @p new_columns. * In the case that @p new_columns is smaller than the current number of columns, * existing lines are not truncated. This prevents characters from being lost * if the terminal display is resized smaller and then larger again. * * The top and bottom margins are reset to the top and bottom of the new * screen size. Tab stops are also reset and the current selection is * cleared. */ void resizeImage(int new_lines, int new_columns); /** * Returns the current screen image. * The result is an array of Characters of size [getLines()][getColumns()] which * must be freed by the caller after use. * * @param dest Buffer to copy the characters into * @param size Size of @p dest in Characters * @param startLine Index of first line to copy * @param endLine Index of last line to copy */ void getImage(Character *dest, int size, int startLine, int endLine) const; /** * Returns the additional attributes associated with lines in the image. * The most important attribute is LINE_WRAPPED which specifies that the * line is wrapped, * other attributes control the size of characters in the line. */ QVector getLineProperties(int startLine, int endLine) const; /** Return the number of lines. */ int getLines() const { return _lines; } /** Return the number of columns. */ int getColumns() const { return _columns; } /** Return the number of lines in the history buffer. */ int getHistLines() const; /** * Sets the type of storage used to keep lines in the history. * If @p copyPreviousScroll is true then the contents of the previous * history buffer are copied into the new scroll. */ void setScroll(const HistoryType &, bool copyPreviousScroll = true); /** Returns the type of storage used to keep lines in the history. */ const HistoryType &getScroll() const; /** * Returns true if this screen keeps lines that are scrolled off the screen * in a history buffer. */ bool hasScroll() const; /** * Sets the start of the selection. * * @param column The column index of the first character in the selection. * @param line The line index of the first character in the selection. * @param blockSelectionMode True if the selection is in column mode. */ void setSelectionStart(const int column, const int line, const bool blockSelectionMode); /** * Sets the end of the current selection. * * @param column The column index of the last character in the selection. * @param line The line index of the last character in the selection. */ void setSelectionEnd(const int column, const int line); /** * Retrieves the start of the selection or the cursor position if there * is no selection. */ void getSelectionStart(int &column, int &line) const; /** * Retrieves the end of the selection or the cursor position if there * is no selection. */ void getSelectionEnd(int &column, int &line) const; /** Clears the current selection */ void clearSelection(); /** * Returns true if the character at (@p column, @p line) is part of the * current selection. */ bool isSelected(const int column, const int line) const; /** * Convenience method. Returns the currently selected text. * @param preserveLineBreaks Specifies whether new line characters should * be inserted into the returned text at the end of each terminal line. * @param trimTrailingSpaces Specifies whether trailing spaces should be * trimmed in the returned text. * @param html Specifies if returned text should have HTML tags. */ QString selectedText(bool preserveLineBreaks, bool trimTrailingSpaces = false, bool html = false) const; /** * Convenience method. Returns the text between two indices. * @param startIndex Specifies the starting text index * @param endIndex Specifies the ending text index * @param preserveLineBreaks Specifies whether new line characters should * be inserted into the returned text at the end of each terminal line. * @param trimTrailingSpaces Specifies whether trailing spaces should be * trimmed in the returned text. * @param html Specifies if returned text should have HTML tags. */ QString text(int startIndex, int endIndex, bool preserveLineBreaks, bool trimTrailingSpaces = false, bool html = false) const; /** * Copies part of the output to a stream. * * @param decoder A decoder which converts terminal characters into text * @param fromLine The first line in the history to retrieve * @param toLine The last line in the history to retrieve */ void writeLinesToStream(TerminalCharacterDecoder *decoder, int fromLine, int toLine) const; /** * Copies the selected characters, set using @see setSelBeginXY and @see setSelExtentXY * into a stream. * * @param decoder A decoder which converts terminal characters into text. * PlainTextDecoder is the most commonly used decoder which converts characters * into plain text with no formatting. * @param preserveLineBreaks Specifies whether new line characters should * be inserted into the returned text at the end of each terminal line. * @param trimTrailingSpaces Specifies whether trailing spaces should be * trimmed in the returned text. */ void writeSelectionToStream(TerminalCharacterDecoder *decoder, bool preserveLineBreaks = true, bool trimTrailingSpaces = false) const; /** * Checks if the text between from and to is inside the current * selection. If this is the case, the selection is cleared. The * from and to are coordinates in the current viewable window. * The loc(x,y) macro can be used to generate these values from a * column,line pair. * * @param from The start of the area to check. * @param to The end of the area to check */ void checkSelection(int from, int to); /** * Sets or clears an attribute of the current line. * * @param property The attribute to set or clear * Possible properties are: * LINE_WRAPPED: Specifies that the line is wrapped. * LINE_DOUBLEWIDTH: Specifies that the characters in the current line * should be double the normal width. * LINE_DOUBLEHEIGHT:Specifies that the characters in the current line * should be double the normal height. * Double-height lines are formed of two lines containing the same characters, * with both having the LINE_DOUBLEHEIGHT attribute. * This allows other parts of the code to work on the * assumption that all lines are the same height. * * @param enable true to apply the attribute to the current line or false to remove it */ void setLineProperty(LineProperty property, bool enable); /** * Returns the number of lines that the image has been scrolled up or down by, * since the last call to resetScrolledLines(). * * a positive return value indicates that the image has been scrolled up, * a negative return value indicates that the image has been scrolled down. */ int scrolledLines() const; /** * Returns the region of the image which was last scrolled. * * This is the area of the image from the top margin to the * bottom margin when the last scroll occurred. */ QRect lastScrolledRegion() const; /** * Resets the count of the number of lines that the image has been scrolled up or down by, * see scrolledLines() */ void resetScrolledLines(); /** * Returns the number of lines of output which have been * dropped from the history since the last call * to resetDroppedLines() * * If the history is not unlimited then it will drop * the oldest lines of output if new lines are added when * it is full. */ int droppedLines() const; /** * Resets the count of the number of lines dropped from * the history. */ void resetDroppedLines(); /** * Fills the buffer @p dest with @p count instances of the default (ie. blank) * Character style. */ static void fillWithDefaultChar(Character *dest, int count); void setCurrentTerminalDisplay(TerminalDisplay *display) { _currentTerminalDisplay = display; } TerminalDisplay *currentTerminalDisplay() { return _currentTerminalDisplay; } QSet usedExtendedChars() const { QSet result; for (int i = 0; i < _lines; ++i) { const ImageLine &il = _screenLines[i]; for (int j = 0; j < il.length(); ++j) { if (il[j].rendition & RE_EXTENDED_CHAR) { result << il[j].character; } } } return result; } static const Character DefaultChar; private: //copies a line of text from the screen or history into a stream using a //specified character decoder. Returns the number of lines actually copied, //which may be less than 'count' if (start+count) is more than the number of characters on //the line // //line - the line number to copy, from 0 (the earliest line in the history) up to // history->getLines() + lines - 1 //start - the first column on the line to copy //count - the number of characters on the line to copy //decoder - a decoder which converts terminal characters (an Character array) into text //appendNewLine - if true a new line character (\n) is appended to the end of the line int copyLineToStream(int line, int start, int count, TerminalCharacterDecoder *decoder, bool appendNewLine, bool preserveLineBreaks, bool trimTrailingSpaces) const; //fills a section of the screen image with the character 'c' //the parameters are specified as offsets from the start of the screen image. //the loc(x,y) macro can be used to generate these values from a column,line pair. void clearImage(int loca, int loce, char c); //move screen image between 'sourceBegin' and 'sourceEnd' to 'dest'. //the parameters are specified as offsets from the start of the screen image. //the loc(x,y) macro can be used to generate these values from a column,line pair. // //NOTE: moveImage() can only move whole lines void moveImage(int dest, int sourceBegin, int sourceEnd); // scroll up 'i' lines in current region, clearing the bottom 'i' lines void scrollUp(int from, int i); // scroll down 'i' lines in current region, clearing the top 'i' lines void scrollDown(int from, int i); //when we handle scroll commands, we need to know which screenwindow will scroll TerminalDisplay *_currentTerminalDisplay; void addHistLine(); void initTabStops(); void updateEffectiveRendition(); void reverseRendition(Character &p) const; bool isSelectionValid() const; // copies text from 'startIndex' to 'endIndex' to a stream // startIndex and endIndex are positions generated using the loc(x,y) macro void writeToStream(TerminalCharacterDecoder *decoder, int startIndex, int endIndex, bool preserveLineBreaks = true, bool trimTrailingSpaces = false) const; // copies 'count' lines from the screen buffer into 'dest', // starting from 'startLine', where 0 is the first line in the screen buffer void copyFromScreen(Character *dest, int startLine, int count) const; // copies 'count' lines from the history buffer into 'dest', // starting from 'startLine', where 0 is the first line in the history void copyFromHistory(Character *dest, int startLine, int count) const; // screen image ---------------- int _lines; int _columns; typedef QVector ImageLine; // [0..columns] ImageLine *_screenLines; // [lines] int _screenLinesSize; // _screenLines.size() int _scrolledLines; QRect _lastScrolledRegion; int _droppedLines; QVarLengthArray _lineProperties; // history buffer --------------- HistoryScroll *_history; // cursor location int _cuX; int _cuY; // cursor color and rendition info CharacterColor _currentForeground; CharacterColor _currentBackground; RenditionFlags _currentRendition; // margins ---------------- int _topMargin; int _bottomMargin; // states ---------------- int _currentModes[MODES_SCREEN]; int _savedModes[MODES_SCREEN]; // ---------------------------- QBitArray _tabStops; // selection ------------------- int _selBegin; // The first location selected. int _selTopLeft; // TopLeft Location. int _selBottomRight; // Bottom Right Location. bool _blockSelectionMode; // Column selection mode // effective colors and rendition ------------ CharacterColor _effectiveForeground; // These are derived from CharacterColor _effectiveBackground; // the cu_* variables above RenditionFlags _effectiveRendition; // to speed up operation class SavedState { public: SavedState() : cursorColumn(0), cursorLine(0), rendition(0), foreground(CharacterColor()), background(CharacterColor()) { } int cursorColumn; int cursorLine; RenditionFlags rendition; CharacterColor foreground; CharacterColor background; }; SavedState _savedState; // last position where we added a character int _lastPos; }; } #endif // SCREEN_H