[050d67a] | 1 | /* |
---|
| 2 | Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> |
---|
| 3 | |
---|
| 4 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
| 10 | |
---|
| 11 | This program is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with this program; if not, write to the Free Software |
---|
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 19 | 02110-1301 USA. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | // Own |
---|
| 23 | #include "ScreenWindow.h" |
---|
| 24 | |
---|
| 25 | // Qt |
---|
| 26 | #include <QtCore> |
---|
| 27 | |
---|
| 28 | // Konsole |
---|
| 29 | #include "Screen.h" |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | using namespace Konsole; |
---|
| 33 | |
---|
| 34 | ScreenWindow::ScreenWindow(QObject* parent) |
---|
| 35 | : QObject(parent) |
---|
| 36 | , _windowBuffer(0) |
---|
| 37 | , _windowBufferSize(0) |
---|
| 38 | , _bufferNeedsUpdate(true) |
---|
| 39 | , _windowLines(1) |
---|
| 40 | , _currentLine(0) |
---|
| 41 | , _trackOutput(true) |
---|
| 42 | , _scrollCount(0) |
---|
| 43 | { |
---|
| 44 | } |
---|
| 45 | ScreenWindow::~ScreenWindow() |
---|
| 46 | { |
---|
| 47 | delete[] _windowBuffer; |
---|
| 48 | } |
---|
| 49 | void ScreenWindow::setScreen(Screen* screen) |
---|
| 50 | { |
---|
| 51 | Q_ASSERT( screen ); |
---|
| 52 | |
---|
| 53 | _screen = screen; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | Screen* ScreenWindow::screen() const |
---|
| 57 | { |
---|
| 58 | return _screen; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | Character* ScreenWindow::getImage() |
---|
| 62 | { |
---|
| 63 | // reallocate internal buffer if the window size has changed |
---|
| 64 | int size = windowLines() * windowColumns(); |
---|
| 65 | if (_windowBuffer == 0 || _windowBufferSize != size) |
---|
| 66 | { |
---|
| 67 | delete[] _windowBuffer; |
---|
| 68 | _windowBufferSize = size; |
---|
| 69 | _windowBuffer = new Character[size]; |
---|
| 70 | _bufferNeedsUpdate = true; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if (!_bufferNeedsUpdate) |
---|
| 74 | return _windowBuffer; |
---|
| 75 | |
---|
| 76 | _screen->getImage(_windowBuffer,size, |
---|
| 77 | currentLine(),endWindowLine()); |
---|
| 78 | |
---|
| 79 | // this window may look beyond the end of the screen, in which |
---|
| 80 | // case there will be an unused area which needs to be filled |
---|
| 81 | // with blank characters |
---|
| 82 | fillUnusedArea(); |
---|
| 83 | |
---|
| 84 | _bufferNeedsUpdate = false; |
---|
| 85 | return _windowBuffer; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void ScreenWindow::fillUnusedArea() |
---|
| 89 | { |
---|
| 90 | int screenEndLine = _screen->getHistLines() + _screen->getLines() - 1; |
---|
| 91 | int windowEndLine = currentLine() + windowLines() - 1; |
---|
| 92 | |
---|
| 93 | int unusedLines = windowEndLine - screenEndLine; |
---|
| 94 | int charsToFill = unusedLines * windowColumns(); |
---|
| 95 | |
---|
| 96 | Screen::fillWithDefaultChar(_windowBuffer + _windowBufferSize - charsToFill,charsToFill); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | // return the index of the line at the end of this window, or if this window |
---|
| 100 | // goes beyond the end of the screen, the index of the line at the end |
---|
| 101 | // of the screen. |
---|
| 102 | // |
---|
| 103 | // when passing a line number to a Screen method, the line number should |
---|
| 104 | // never be more than endWindowLine() |
---|
| 105 | // |
---|
| 106 | int ScreenWindow::endWindowLine() const |
---|
| 107 | { |
---|
| 108 | return qMin(currentLine() + windowLines() - 1, |
---|
| 109 | lineCount() - 1); |
---|
| 110 | } |
---|
| 111 | QVector<LineProperty> ScreenWindow::getLineProperties() |
---|
| 112 | { |
---|
| 113 | QVector<LineProperty> result = _screen->getLineProperties(currentLine(),endWindowLine()); |
---|
| 114 | |
---|
| 115 | if (result.count() != windowLines()) |
---|
| 116 | result.resize(windowLines()); |
---|
| 117 | |
---|
| 118 | return result; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | QString ScreenWindow::selectedText( bool preserveLineBreaks ) const |
---|
| 122 | { |
---|
| 123 | return _screen->selectedText( preserveLineBreaks ); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void ScreenWindow::getSelectionStart( int& column , int& line ) |
---|
| 127 | { |
---|
| 128 | _screen->getSelectionStart(column,line); |
---|
| 129 | line -= currentLine(); |
---|
| 130 | } |
---|
| 131 | void ScreenWindow::getSelectionEnd( int& column , int& line ) |
---|
| 132 | { |
---|
| 133 | _screen->getSelectionEnd(column,line); |
---|
| 134 | line -= currentLine(); |
---|
| 135 | } |
---|
| 136 | void ScreenWindow::setSelectionStart( int column , int line , bool columnMode ) |
---|
| 137 | { |
---|
| 138 | _screen->setSelectionStart( column , qMin(line + currentLine(),endWindowLine()) , columnMode); |
---|
| 139 | |
---|
| 140 | _bufferNeedsUpdate = true; |
---|
| 141 | emit selectionChanged(); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | void ScreenWindow::setSelectionEnd( int column , int line ) |
---|
| 145 | { |
---|
| 146 | _screen->setSelectionEnd( column , qMin(line + currentLine(),endWindowLine()) ); |
---|
| 147 | |
---|
| 148 | _bufferNeedsUpdate = true; |
---|
| 149 | emit selectionChanged(); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | bool ScreenWindow::isSelected( int column , int line ) |
---|
| 153 | { |
---|
| 154 | return _screen->isSelected( column , qMin(line + currentLine(),endWindowLine()) ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void ScreenWindow::clearSelection() |
---|
| 158 | { |
---|
| 159 | _screen->clearSelection(); |
---|
| 160 | |
---|
| 161 | emit selectionChanged(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | void ScreenWindow::setWindowLines(int lines) |
---|
| 165 | { |
---|
| 166 | Q_ASSERT(lines > 0); |
---|
| 167 | _windowLines = lines; |
---|
| 168 | } |
---|
| 169 | int ScreenWindow::windowLines() const |
---|
| 170 | { |
---|
| 171 | return _windowLines; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | int ScreenWindow::windowColumns() const |
---|
| 175 | { |
---|
| 176 | return _screen->getColumns(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | int ScreenWindow::lineCount() const |
---|
| 180 | { |
---|
| 181 | return _screen->getHistLines() + _screen->getLines(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | int ScreenWindow::columnCount() const |
---|
| 185 | { |
---|
| 186 | return _screen->getColumns(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | QPoint ScreenWindow::cursorPosition() const |
---|
| 190 | { |
---|
| 191 | QPoint position; |
---|
| 192 | |
---|
| 193 | position.setX( _screen->getCursorX() ); |
---|
| 194 | position.setY( _screen->getCursorY() ); |
---|
| 195 | |
---|
| 196 | return position; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | int ScreenWindow::currentLine() const |
---|
| 200 | { |
---|
| 201 | return qBound(0,_currentLine,lineCount()-windowLines()); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | void ScreenWindow::scrollBy( RelativeScrollMode mode , int amount ) |
---|
| 205 | { |
---|
| 206 | if ( mode == ScrollLines ) |
---|
| 207 | { |
---|
| 208 | scrollTo( currentLine() + amount ); |
---|
| 209 | } |
---|
| 210 | else if ( mode == ScrollPages ) |
---|
| 211 | { |
---|
| 212 | scrollTo( currentLine() + amount * ( windowLines() / 2 ) ); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | bool ScreenWindow::atEndOfOutput() const |
---|
| 217 | { |
---|
| 218 | return currentLine() == (lineCount()-windowLines()); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | void ScreenWindow::scrollTo( int line ) |
---|
| 222 | { |
---|
| 223 | int maxCurrentLineNumber = lineCount() - windowLines(); |
---|
| 224 | line = qBound(0,line,maxCurrentLineNumber); |
---|
| 225 | |
---|
| 226 | const int delta = line - _currentLine; |
---|
| 227 | _currentLine = line; |
---|
| 228 | |
---|
| 229 | // keep track of number of lines scrolled by, |
---|
| 230 | // this can be reset by calling resetScrollCount() |
---|
| 231 | _scrollCount += delta; |
---|
| 232 | |
---|
| 233 | _bufferNeedsUpdate = true; |
---|
| 234 | |
---|
| 235 | emit scrolled(_currentLine); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void ScreenWindow::setTrackOutput(bool trackOutput) |
---|
| 239 | { |
---|
| 240 | _trackOutput = trackOutput; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | bool ScreenWindow::trackOutput() const |
---|
| 244 | { |
---|
| 245 | return _trackOutput; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | int ScreenWindow::scrollCount() const |
---|
| 249 | { |
---|
| 250 | return _scrollCount; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | void ScreenWindow::resetScrollCount() |
---|
| 254 | { |
---|
| 255 | _scrollCount = 0; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | QRect ScreenWindow::scrollRegion() const |
---|
| 259 | { |
---|
| 260 | bool equalToScreenSize = windowLines() == _screen->getLines(); |
---|
| 261 | |
---|
| 262 | if ( atEndOfOutput() && equalToScreenSize ) |
---|
| 263 | return _screen->lastScrolledRegion(); |
---|
| 264 | else |
---|
| 265 | return QRect(0,0,windowColumns(),windowLines()); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | void ScreenWindow::notifyOutputChanged() |
---|
| 269 | { |
---|
| 270 | // move window to the bottom of the screen and update scroll count |
---|
| 271 | // if this window is currently tracking the bottom of the screen |
---|
| 272 | if ( _trackOutput ) |
---|
| 273 | { |
---|
| 274 | _scrollCount -= _screen->scrolledLines(); |
---|
| 275 | _currentLine = qMax(0,_screen->getHistLines() - (windowLines()-_screen->getLines())); |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | // if the history is not unlimited then it may |
---|
| 280 | // have run out of space and dropped the oldest |
---|
| 281 | // lines of output - in this case the screen |
---|
| 282 | // window's current line number will need to |
---|
| 283 | // be adjusted - otherwise the output will scroll |
---|
| 284 | _currentLine = qMax(0,_currentLine - |
---|
| 285 | _screen->droppedLines()); |
---|
| 286 | |
---|
| 287 | // ensure that the screen window's current position does |
---|
| 288 | // not go beyond the bottom of the screen |
---|
| 289 | _currentLine = qMin( _currentLine , _screen->getHistLines() ); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | _bufferNeedsUpdate = true; |
---|
| 293 | |
---|
| 294 | emit outputChanged(); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | //#include "moc_ScreenWindow.cpp" |
---|