• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ScreenWindow.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301 USA.
18 */
19 
20 // Own
21 #include "ScreenWindow.h"
22 
23 // Konsole
24 #include "Screen.h"
25 
26 using namespace Konsole;
27 
28 ScreenWindow::ScreenWindow(Screen* screen, QObject* parent)
29  : QObject(parent)
30  , _windowBuffer(0)
31  , _windowBufferSize(0)
32  , _bufferNeedsUpdate(true)
33  , _windowLines(1)
34  , _currentLine(0)
35  , _currentResultLine(-1)
36  , _trackOutput(true)
37  , _scrollCount(0)
38 {
39  setScreen(screen);
40 }
41 
42 ScreenWindow::~ScreenWindow()
43 {
44  delete[] _windowBuffer;
45 }
46 void ScreenWindow::setScreen(Screen* screen)
47 {
48  Q_ASSERT(screen);
49 
50  _screen = screen;
51 }
52 
53 Screen* ScreenWindow::screen() const
54 {
55  return _screen;
56 }
57 
58 Character* ScreenWindow::getImage()
59 {
60  // reallocate internal buffer if the window size has changed
61  int size = windowLines() * windowColumns();
62  if (_windowBuffer == 0 || _windowBufferSize != size) {
63  delete[] _windowBuffer;
64  _windowBufferSize = size;
65  _windowBuffer = new Character[size];
66  _bufferNeedsUpdate = true;
67  }
68 
69  if (!_bufferNeedsUpdate)
70  return _windowBuffer;
71 
72  _screen->getImage(_windowBuffer, size,
73  currentLine(), endWindowLine());
74 
75  // this window may look beyond the end of the screen, in which
76  // case there will be an unused area which needs to be filled
77  // with blank characters
78  fillUnusedArea();
79 
80  _bufferNeedsUpdate = false;
81  return _windowBuffer;
82 }
83 
84 void ScreenWindow::fillUnusedArea()
85 {
86  int screenEndLine = _screen->getHistLines() + _screen->getLines() - 1;
87  int windowEndLine = currentLine() + windowLines() - 1;
88 
89  int unusedLines = windowEndLine - screenEndLine;
90 
91  // stop when unusedLines is negative; there is an issue w/ charsToFill
92  // being greater than an int can hold
93  if (unusedLines <= 0)
94  return;
95 
96  int charsToFill = unusedLines * windowColumns();
97 
98  Screen::fillWithDefaultChar(_windowBuffer + _windowBufferSize - charsToFill, charsToFill);
99 }
100 
101 // return the index of the line at the end of this window, or if this window
102 // goes beyond the end of the screen, the index of the line at the end
103 // of the screen.
104 //
105 // when passing a line number to a Screen method, the line number should
106 // never be more than endWindowLine()
107 //
108 int ScreenWindow::endWindowLine() const
109 {
110  return qMin(currentLine() + windowLines() - 1,
111  lineCount() - 1);
112 }
113 QVector<LineProperty> ScreenWindow::getLineProperties()
114 {
115  QVector<LineProperty> result = _screen->getLineProperties(currentLine(), endWindowLine());
116 
117  if (result.count() != windowLines())
118  result.resize(windowLines());
119 
120  return result;
121 }
122 
123 QString ScreenWindow::selectedText(bool preserveLineBreaks, bool trimTrailingSpaces) const
124 {
125  return _screen->selectedText(preserveLineBreaks, trimTrailingSpaces);
126 }
127 
128 void ScreenWindow::getSelectionStart(int& column , int& line)
129 {
130  _screen->getSelectionStart(column, line);
131  line -= currentLine();
132 }
133 void ScreenWindow::getSelectionEnd(int& column , int& line)
134 {
135  _screen->getSelectionEnd(column, line);
136  line -= currentLine();
137 }
138 void ScreenWindow::setSelectionStart(int column , int line , bool columnMode)
139 {
140  _screen->setSelectionStart(column , line + currentLine() , columnMode);
141 
142  _bufferNeedsUpdate = true;
143  emit selectionChanged();
144 }
145 
146 void ScreenWindow::setSelectionEnd(int column , int line)
147 {
148  _screen->setSelectionEnd(column , line + currentLine());
149 
150  _bufferNeedsUpdate = true;
151  emit selectionChanged();
152 }
153 
154 void ScreenWindow::setSelectionByLineRange(int start, int end)
155 {
156  clearSelection();
157 
158  _screen->setSelectionStart(0 , start , false);
159  _screen->setSelectionEnd(windowColumns() , end);
160 
161  _bufferNeedsUpdate = true;
162  emit selectionChanged();
163 }
164 
165 bool ScreenWindow::isSelected(int column , int line)
166 {
167  return _screen->isSelected(column , qMin(line + currentLine(), endWindowLine()));
168 }
169 
170 void ScreenWindow::clearSelection()
171 {
172  _screen->clearSelection();
173 
174  emit selectionChanged();
175 }
176 
177 void ScreenWindow::setWindowLines(int lines)
178 {
179  Q_ASSERT(lines > 0);
180  _windowLines = lines;
181 }
182 int ScreenWindow::windowLines() const
183 {
184  return _windowLines;
185 }
186 
187 int ScreenWindow::windowColumns() const
188 {
189  return _screen->getColumns();
190 }
191 
192 int ScreenWindow::lineCount() const
193 {
194  return _screen->getHistLines() + _screen->getLines();
195 }
196 
197 int ScreenWindow::columnCount() const
198 {
199  return _screen->getColumns();
200 }
201 
202 QPoint ScreenWindow::cursorPosition() const
203 {
204  QPoint position;
205 
206  position.setX(_screen->getCursorX());
207  position.setY(_screen->getCursorY());
208 
209  return position;
210 }
211 
212 int ScreenWindow::currentLine() const
213 {
214  return qBound(0, _currentLine, lineCount() - windowLines());
215 }
216 
217 int ScreenWindow::currentResultLine() const
218 {
219  return _currentResultLine;
220 }
221 
222 void ScreenWindow::setCurrentResultLine(int line)
223 {
224  if (_currentResultLine == line) {
225  return;
226  }
227 
228  _currentResultLine = line;
229  emit currentResultLineChanged();
230 }
231 
232 void ScreenWindow::scrollBy(RelativeScrollMode mode, int amount, bool fullPage)
233 {
234  if (mode == ScrollLines) {
235  scrollTo(currentLine() + amount);
236  } else if (mode == ScrollPages) {
237  if (fullPage)
238  scrollTo(currentLine() + amount * (windowLines()));
239  else
240  scrollTo(currentLine() + amount * (windowLines() / 2));
241  }
242 }
243 
244 bool ScreenWindow::atEndOfOutput() const
245 {
246  return currentLine() == (lineCount() - windowLines());
247 }
248 
249 void ScreenWindow::scrollTo(int line)
250 {
251  int maxCurrentLineNumber = lineCount() - windowLines();
252  line = qBound(0, line, maxCurrentLineNumber);
253 
254  const int delta = line - _currentLine;
255  _currentLine = line;
256 
257  // keep track of number of lines scrolled by,
258  // this can be reset by calling resetScrollCount()
259  _scrollCount += delta;
260 
261  _bufferNeedsUpdate = true;
262 
263  emit scrolled(_currentLine);
264 }
265 
266 void ScreenWindow::setTrackOutput(bool trackOutput)
267 {
268  _trackOutput = trackOutput;
269 }
270 
271 bool ScreenWindow::trackOutput() const
272 {
273  return _trackOutput;
274 }
275 
276 int ScreenWindow::scrollCount() const
277 {
278  return _scrollCount;
279 }
280 
281 void ScreenWindow::resetScrollCount()
282 {
283  _scrollCount = 0;
284 }
285 
286 QRect ScreenWindow::scrollRegion() const
287 {
288  bool equalToScreenSize = windowLines() == _screen->getLines();
289 
290  if (atEndOfOutput() && equalToScreenSize)
291  return _screen->lastScrolledRegion();
292  else
293  return QRect(0, 0, windowColumns(), windowLines());
294 }
295 
296 void ScreenWindow::notifyOutputChanged()
297 {
298  // move window to the bottom of the screen and update scroll count
299  // if this window is currently tracking the bottom of the screen
300  if (_trackOutput) {
301  _scrollCount -= _screen->scrolledLines();
302  _currentLine = qMax(0, _screen->getHistLines() - (windowLines() - _screen->getLines()));
303  } else {
304  // if the history is not unlimited then it may
305  // have run out of space and dropped the oldest
306  // lines of output - in this case the screen
307  // window's current line number will need to
308  // be adjusted - otherwise the output will scroll
309  _currentLine = qMax(0, _currentLine -
310  _screen->droppedLines());
311 
312  // ensure that the screen window's current position does
313  // not go beyond the bottom of the screen
314  _currentLine = qMin(_currentLine , _screen->getHistLines());
315  }
316 
317  _bufferNeedsUpdate = true;
318 
319  emit outputChanged();
320 }
321 
322 #include "ScreenWindow.moc"
Konsole::Screen::droppedLines
int droppedLines() const
Returns the number of lines of output which have been dropped from the history since the last call to...
Definition: Screen.cpp:741
Konsole::ScreenWindow::scrollRegion
QRect scrollRegion() const
Returns the area of the window which was last scrolled, this is usually the whole window area...
Definition: ScreenWindow.cpp:286
Konsole::Screen::lastScrolledRegion
QRect lastScrolledRegion() const
Returns the region of the image which was last scrolled.
Definition: Screen.cpp:761
Konsole::Screen::selectedText
QString selectedText(bool preserveLineBreaks, bool trimTrailingSpaces=false) const
Convenience method.
Definition: Screen.cpp:1110
Konsole::ScreenWindow::currentResultLine
int currentResultLine() const
Definition: ScreenWindow.cpp:217
Konsole::ScreenWindow::setSelectionEnd
void setSelectionEnd(int column, int line)
Sets the end of the selection to the given line and column within the window.
Definition: ScreenWindow.cpp:146
Konsole::Screen::getLines
int getLines() const
Return the number of lines.
Definition: Screen.h:382
Konsole::ScreenWindow::notifyOutputChanged
void notifyOutputChanged()
Notifies the window that the contents of the associated terminal screen have changed.
Definition: ScreenWindow.cpp:296
Konsole::ScreenWindow::clearSelection
void clearSelection()
Clears the current selection.
Definition: ScreenWindow.cpp:170
Konsole::ScreenWindow::getLineProperties
QVector< LineProperty > getLineProperties()
Returns the line attributes associated with the lines of characters which are currently visible throu...
Definition: ScreenWindow.cpp:113
Konsole::ScreenWindow::currentLine
int currentLine() const
Returns the index of the line which is currently at the top of this window.
Definition: ScreenWindow.cpp:212
Konsole::Screen::getImage
void getImage(Character *dest, int size, int startLine, int endLine) const
Returns the current screen image.
Definition: Screen.cpp:450
Screen.h
Konsole::ScreenWindow::trackOutput
bool trackOutput() const
Returns whether the window automatically moves to the bottom of the screen as new output is added...
Definition: ScreenWindow.cpp:271
QPoint
Konsole::Screen::getColumns
int getColumns() const
Return the number of columns.
Definition: Screen.h:386
Konsole::ScreenWindow::columnCount
int columnCount() const
Returns the total number of columns in the screen.
Definition: ScreenWindow.cpp:197
Konsole::Screen::fillWithDefaultChar
static void fillWithDefaultChar(Character *dest, int count)
Fills the buffer dest with count instances of the default (ie.
Definition: Screen.cpp:1383
Konsole::ScreenWindow::scrollBy
void scrollBy(RelativeScrollMode mode, int amount, bool fullPage)
Scrolls the window relative to its current position on the screen.
Definition: ScreenWindow.cpp:232
Konsole::ScreenWindow::setSelectionByLineRange
void setSelectionByLineRange(int start, int end)
Sets the selection as the range specified by line start and line end in the whole history...
Definition: ScreenWindow.cpp:154
Konsole::ScreenWindow::cursorPosition
QPoint cursorPosition() const
Returns the position of the cursor within the window.
Definition: ScreenWindow.cpp:202
Konsole::ScreenWindow::selectedText
QString selectedText(bool preserveLineBreaks, bool trimTrailingSpaces=false) const
Returns the text which is currently selected.
Definition: ScreenWindow.cpp:123
Konsole::ScreenWindow::scrollCount
int scrollCount() const
Returns the number of lines which the region of the window specified by scrollRegion() has been scrol...
Definition: ScreenWindow.cpp:276
Konsole::Character
A single character in the terminal which consists of a unicode character value, foreground and backgr...
Definition: Character.h:77
QRect
Konsole::ScreenWindow::atEndOfOutput
bool atEndOfOutput() const
Convenience method.
Definition: ScreenWindow.cpp:244
Konsole::ScreenWindow::outputChanged
void outputChanged()
Emitted when the contents of the associated terminal screen (see screen()) changes.
QVector::resize
void resize(int size)
QObject
Konsole::Screen
An image of characters with associated attributes.
Definition: Screen.h:74
Konsole::Screen::scrolledLines
int scrolledLines() const
Returns the number of lines that the image has been scrolled up or down by, since the last call to re...
Definition: Screen.cpp:737
Konsole::ScreenWindow::scrollTo
void scrollTo(int line)
Scrolls the window so that line is at the top of the window.
Definition: ScreenWindow.cpp:249
Konsole::ScreenWindow::windowColumns
int windowColumns() const
Returns the number of columns in the window.
Definition: ScreenWindow.cpp:187
QString
Konsole::ScreenWindow::~ScreenWindow
virtual ~ScreenWindow()
Definition: ScreenWindow.cpp:42
Konsole::Screen::clearSelection
void clearSelection()
Clears the current selection.
Definition: Screen.cpp:1029
Konsole::ScreenWindow::getSelectionEnd
void getSelectionEnd(int &column, int &line)
Retrieves the end of the selection within the window.
Definition: ScreenWindow.cpp:133
Konsole::ScreenWindow::isSelected
bool isSelected(int column, int line)
Returns true if the character at line , column is part of the selection.
Definition: ScreenWindow.cpp:165
Konsole::ScreenWindow::getSelectionStart
void getSelectionStart(int &column, int &line)
Retrieves the start of the selection within the window.
Definition: ScreenWindow.cpp:128
Konsole::Screen::getSelectionStart
void getSelectionStart(int &column, int &line) const
Retrieves the start of the selection or the cursor position if there is no selection.
Definition: Screen.cpp:1036
Konsole::ScreenWindow::getImage
Character * getImage()
Returns the image of characters which are currently visible through this window onto the screen...
Definition: ScreenWindow.cpp:58
Konsole::ScreenWindow::setCurrentResultLine
void setCurrentResultLine(int line)
What line the next search will start from.
Definition: ScreenWindow.cpp:222
Konsole::Screen::isSelected
bool isSelected(const int column, const int line) const
Returns true if the character at (column, line) is part of the current selection. ...
Definition: Screen.cpp:1098
QVector< LineProperty >
Konsole::Screen::getSelectionEnd
void getSelectionEnd(int &column, int &line) const
Retrieves the end of the selection or the cursor position if there is no selection.
Definition: Screen.cpp:1046
Konsole::ScreenWindow::resetScrollCount
void resetScrollCount()
Resets the count of scrolled lines returned by scrollCount()
Definition: ScreenWindow.cpp:281
Konsole::Screen::getCursorX
int getCursorX() const
Returns the column which the cursor is positioned at.
Definition: Screen.cpp:830
Konsole::ScreenWindow::screen
Screen * screen() const
Returns the screen which this window looks onto.
Definition: ScreenWindow.cpp:53
Konsole::ScreenWindow::setSelectionStart
void setSelectionStart(int column, int line, bool columnMode)
Sets the start of the selection to the given line and column within the window.
Definition: ScreenWindow.cpp:138
Konsole::Screen::getCursorY
int getCursorY() const
Returns the line which the cursor is positioned on.
Definition: Screen.cpp:835
QVector::count
int count(const T &value) const
Konsole::Screen::setSelectionEnd
void setSelectionEnd(const int column, const int line)
Sets the end of the current selection.
Definition: Screen.cpp:1067
QPoint::setX
void setX(int x)
QPoint::setY
void setY(int y)
Konsole::ScreenWindow::selectionChanged
void selectionChanged()
Emitted when the selection is changed.
Konsole::ScreenWindow::currentResultLineChanged
void currentResultLineChanged()
Konsole::ScreenWindow::setWindowLines
void setWindowLines(int lines)
Sets the number of lines in the window.
Definition: ScreenWindow.cpp:177
Konsole::ScreenWindow::scrolled
void scrolled(int line)
Emitted when the screen window is scrolled to a different position.
Konsole::ScreenWindow::RelativeScrollMode
RelativeScrollMode
Describes the units which scrollBy() moves the window by.
Definition: ScreenWindow.h:193
Konsole::Screen::setSelectionStart
void setSelectionStart(const int column, const int line, const bool blockSelectionMode)
Sets the start of the selection.
Definition: Screen.cpp:1056
Konsole::Screen::getHistLines
int getHistLines() const
Return the number of lines in the history buffer.
Definition: Screen.cpp:1348
Konsole::ScreenWindow::setScreen
void setScreen(Screen *screen)
Sets the screen which this window looks onto.
Definition: ScreenWindow.cpp:46
ScreenWindow.h
Konsole::ScreenWindow::lineCount
int lineCount() const
Returns the total number of lines in the screen.
Definition: ScreenWindow.cpp:192
Konsole::ScreenWindow::setTrackOutput
void setTrackOutput(bool trackOutput)
Specifies whether the window should automatically move to the bottom of the screen when new output is...
Definition: ScreenWindow.cpp:266
Konsole::ScreenWindow::ScrollPages
Scroll the window down by a given number of pages, where one page is windowLines() lines...
Definition: ScreenWindow.h:200
Konsole::Screen::getLineProperties
QVector< LineProperty > getLineProperties(int startLine, int endLine) const
Returns the additional attributes associated with lines in the image.
Definition: Screen.cpp:485
Konsole::ScreenWindow::windowLines
int windowLines() const
Returns the number of lines in the window.
Definition: ScreenWindow.cpp:182
Konsole::ScreenWindow::ScreenWindow
ScreenWindow(Screen *screen, QObject *parent=0)
Constructs a new screen window with the given parent.
Definition: ScreenWindow.cpp:28
Konsole::ScreenWindow::ScrollLines
Scroll the window down by a given number of lines.
Definition: ScreenWindow.h:195
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal