• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

ScreenWindow.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "ScreenWindow.h"
00022 
00023 // Qt
00024 #include <KDebug>
00025 
00026 // Konsole
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     // reallocate internal buffer if the window size has changed
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     // this window may look beyond the end of the screen, in which 
00077     // case there will be an unused area which needs to be filled
00078     // with blank characters
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 // return the index of the line at the end of this window, or if this window 
00097 // goes beyond the end of the screen, the index of the line at the end
00098 // of the screen.
00099 //
00100 // when passing a line number to a Screen method, the line number should
00101 // never be more than endWindowLine()
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     // keep track of number of lines scrolled by,
00227     // this can be reset by calling resetScrollCount()
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     // move window to the bottom of the screen and update scroll count
00268     // if this window is currently tracking the bottom of the screen
00269     if ( _trackOutput )
00270     { 
00271         _scrollCount -= _screen->scrolledLines();
00272         _currentLine = qMax(0,_screen->getHistLines() - (windowLines()-_screen->getLines()));
00273     }
00274     else
00275     {
00276         // if the history is not unlimited then it may 
00277         // have run out of space and dropped the oldest
00278         // lines of output - in this case the screen
00279         // window's current line number will need to 
00280         // be adjusted - otherwise the output will scroll
00281         _currentLine = qMax(0,_currentLine - 
00282                               _screen->droppedLines());
00283 
00284         // ensure that the screen window's current position does
00285         // not go beyond the bottom of the screen
00286         _currentLine = qMin( _currentLine , _screen->getHistLines() );
00287     }
00288 
00289     _bufferNeedsUpdate = true;
00290 
00291     emit outputChanged(); 
00292 }
00293 
00294 #include "ScreenWindow.moc"

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal