Konsole
ScreenWindow.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ScreenWindow.h"
00022
00023
00024 #include <KDebug>
00025
00026
00027 #include "Screen.h"
00028
00029 using namespace Konsole;
00030
00031 ScreenWindow::ScreenWindow(QObject* parent)
00032 : QObject(parent)
00033 , _windowBuffer(0)
00034 , _windowBufferSize(0)
00035 , _bufferNeedsUpdate(true)
00036 , _windowLines(1)
00037 , _currentLine(0)
00038 , _trackOutput(true)
00039 , _scrollCount(0)
00040 {
00041 }
00042 ScreenWindow::~ScreenWindow()
00043 {
00044 delete[] _windowBuffer;
00045 }
00046 void ScreenWindow::setScreen(Screen* screen)
00047 {
00048 Q_ASSERT( screen );
00049
00050 _screen = screen;
00051 }
00052
00053 Screen* ScreenWindow::screen() const
00054 {
00055 return _screen;
00056 }
00057
00058 Character* ScreenWindow::getImage()
00059 {
00060
00061 int size = windowLines() * windowColumns();
00062 if (_windowBuffer == 0 || _windowBufferSize != size)
00063 {
00064 delete[] _windowBuffer;
00065 _windowBufferSize = size;
00066 _windowBuffer = new Character[size];
00067 _bufferNeedsUpdate = true;
00068 }
00069
00070 if (!_bufferNeedsUpdate)
00071 return _windowBuffer;
00072
00073 _screen->getImage(_windowBuffer,size,
00074 currentLine(),endWindowLine());
00075
00076
00077
00078
00079 fillUnusedArea();
00080
00081 _bufferNeedsUpdate = false;
00082 return _windowBuffer;
00083 }
00084
00085 void ScreenWindow::fillUnusedArea()
00086 {
00087 int screenEndLine = _screen->getHistLines() + _screen->getLines() - 1;
00088 int windowEndLine = currentLine() + windowLines() - 1;
00089
00090 int unusedLines = windowEndLine - screenEndLine;
00091 int charsToFill = unusedLines * windowColumns();
00092
00093 Screen::fillWithDefaultChar(_windowBuffer + _windowBufferSize - charsToFill,charsToFill);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 int ScreenWindow::endWindowLine() const
00104 {
00105 return qMin(currentLine() + windowLines() - 1,
00106 lineCount() - 1);
00107 }
00108 QVector<LineProperty> ScreenWindow::getLineProperties()
00109 {
00110 QVector<LineProperty> result = _screen->getLineProperties(currentLine(),endWindowLine());
00111
00112 if (result.count() != windowLines())
00113 result.resize(windowLines());
00114
00115 return result;
00116 }
00117
00118 QString ScreenWindow::selectedText( bool preserveLineBreaks ) const
00119 {
00120 return _screen->selectedText( preserveLineBreaks );
00121 }
00122
00123 void ScreenWindow::getSelectionStart( int& column , int& line )
00124 {
00125 _screen->getSelectionStart(column,line);
00126 line -= currentLine();
00127 }
00128 void ScreenWindow::getSelectionEnd( int& column , int& line )
00129 {
00130 _screen->getSelectionEnd(column,line);
00131 line -= currentLine();
00132 }
00133 void ScreenWindow::setSelectionStart( int column , int line , bool columnMode )
00134 {
00135 _screen->setSelectionStart( column , qMin(line + currentLine(),endWindowLine()) , columnMode);
00136
00137 _bufferNeedsUpdate = true;
00138 emit selectionChanged();
00139 }
00140
00141 void ScreenWindow::setSelectionEnd( int column , int line )
00142 {
00143 _screen->setSelectionEnd( column , qMin(line + currentLine(),endWindowLine()) );
00144
00145 _bufferNeedsUpdate = true;
00146 emit selectionChanged();
00147 }
00148
00149 bool ScreenWindow::isSelected( int column , int line )
00150 {
00151 return _screen->isSelected( column , qMin(line + currentLine(),endWindowLine()) );
00152 }
00153
00154 void ScreenWindow::clearSelection()
00155 {
00156 _screen->clearSelection();
00157
00158 emit selectionChanged();
00159 }
00160
00161 void ScreenWindow::setWindowLines(int lines)
00162 {
00163 Q_ASSERT(lines > 0);
00164 _windowLines = lines;
00165 }
00166 int ScreenWindow::windowLines() const
00167 {
00168 return _windowLines;
00169 }
00170
00171 int ScreenWindow::windowColumns() const
00172 {
00173 return _screen->getColumns();
00174 }
00175
00176 int ScreenWindow::lineCount() const
00177 {
00178 return _screen->getHistLines() + _screen->getLines();
00179 }
00180
00181 int ScreenWindow::columnCount() const
00182 {
00183 return _screen->getColumns();
00184 }
00185
00186 QPoint ScreenWindow::cursorPosition() const
00187 {
00188 QPoint position;
00189
00190 position.setX( _screen->getCursorX() );
00191 position.setY( _screen->getCursorY() );
00192
00193 return position;
00194 }
00195
00196 int ScreenWindow::currentLine() const
00197 {
00198 return qBound(0,_currentLine,lineCount()-windowLines());
00199 }
00200
00201 void ScreenWindow::scrollBy( RelativeScrollMode mode , int amount )
00202 {
00203 if ( mode == ScrollLines )
00204 {
00205 scrollTo( currentLine() + amount );
00206 }
00207 else if ( mode == ScrollPages )
00208 {
00209 scrollTo( currentLine() + amount * ( windowLines() / 2 ) );
00210 }
00211 }
00212
00213 bool ScreenWindow::atEndOfOutput() const
00214 {
00215 return currentLine() == (lineCount()-windowLines());
00216 }
00217
00218 void ScreenWindow::scrollTo( int line )
00219 {
00220 int maxCurrentLineNumber = lineCount() - windowLines();
00221 line = qBound(0,line,maxCurrentLineNumber);
00222
00223 const int delta = line - _currentLine;
00224 _currentLine = line;
00225
00226
00227
00228 _scrollCount += delta;
00229
00230 _bufferNeedsUpdate = true;
00231
00232 emit scrolled(_currentLine);
00233 }
00234
00235 void ScreenWindow::setTrackOutput(bool trackOutput)
00236 {
00237 _trackOutput = trackOutput;
00238 }
00239
00240 bool ScreenWindow::trackOutput() const
00241 {
00242 return _trackOutput;
00243 }
00244
00245 int ScreenWindow::scrollCount() const
00246 {
00247 return _scrollCount;
00248 }
00249
00250 void ScreenWindow::resetScrollCount()
00251 {
00252 _scrollCount = 0;
00253 }
00254
00255 QRect ScreenWindow::scrollRegion() const
00256 {
00257 bool equalToScreenSize = windowLines() == _screen->getLines();
00258
00259 if ( atEndOfOutput() && equalToScreenSize )
00260 return _screen->lastScrolledRegion();
00261 else
00262 return QRect(0,0,windowColumns(),windowLines());
00263 }
00264
00265 void ScreenWindow::notifyOutputChanged()
00266 {
00267
00268
00269 if ( _trackOutput )
00270 {
00271 _scrollCount -= _screen->scrolledLines();
00272 _currentLine = qMax(0,_screen->getHistLines() - (windowLines()-_screen->getLines()));
00273 }
00274 else
00275 {
00276
00277
00278
00279
00280
00281 _currentLine = qMax(0,_currentLine -
00282 _screen->droppedLines());
00283
00284
00285
00286 _currentLine = qMin( _currentLine , _screen->getHistLines() );
00287 }
00288
00289 _bufferNeedsUpdate = true;
00290
00291 emit outputChanged();
00292 }
00293
00294 #include "ScreenWindow.moc"