GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ScreenWindow.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007, 2013 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 "unix/ScreenWindow.h"
24 
25 // Qt
26 #include <QtCore>
27 
28 // Konsole
29 #include "unix/Screen.h"
30 
32  : QObject(parent)
33  , _windowBuffer(0)
34  , _windowBufferSize(0)
35  , _bufferNeedsUpdate(true)
36  , _windowLines(1)
37  , _currentLine(0)
38  , _trackOutput(true)
39  , _scrollCount(0)
40 {
41 }
42 
44 {
45  delete[] _windowBuffer;
46 }
48 {
49  Q_ASSERT( screen );
50 
51  _screen = screen;
52 }
53 
55 {
56  return _screen;
57 }
58 
60 {
61  // reallocate internal buffer if the window size has changed
62  int size = windowLines() * windowColumns();
63  if (_windowBuffer == 0 || _windowBufferSize != size)
64  {
65  delete[] _windowBuffer;
68  _bufferNeedsUpdate = true;
69  }
70 
71  if (!_bufferNeedsUpdate)
72  return _windowBuffer;
73 
76 
77  // this window may look beyond the end of the screen, in which
78  // case there will be an unused area which needs to be filled
79  // with blank characters
81 
82  _bufferNeedsUpdate = false;
83  return _windowBuffer;
84 }
85 
87 {
88  int screenEndLine = _screen->getHistLines() + _screen->getLines() - 1;
89  int windowEndLine = currentLine() + windowLines() - 1;
90 
91  int unusedLines = windowEndLine - screenEndLine;
92  int charsToFill = unusedLines * windowColumns();
93 
94  Screen::fillWithDefaultChar(_windowBuffer + _windowBufferSize - charsToFill,charsToFill);
95 }
96 
97 // return the index of the line at the end of this window, or if this window
98 // goes beyond the end of the screen, the index of the line at the end
99 // of the screen.
100 //
101 // when passing a line number to a Screen method, the line number should
102 // never be more than endWindowLine()
103 //
105 {
106  return qMin(currentLine() + windowLines() - 1,
107  lineCount() - 1);
108 }
109 QVector<LineProperty> ScreenWindow::getLineProperties()
110 {
111  QVector<LineProperty> result = _screen->getLineProperties(currentLine(),endWindowLine());
112 
113  if (result.count() != windowLines())
114  result.resize(windowLines());
115 
116  return result;
117 }
118 
119 QString ScreenWindow::selectedText( bool preserveLineBreaks ) const
120 {
121  return _screen->selectedText( preserveLineBreaks );
122 }
123 
125 {
126  _screen->getSelectionStart(column,line);
127  line -= currentLine();
128 }
130 {
131  _screen->getSelectionEnd(column,line);
132  line -= currentLine();
133 }
134 void ScreenWindow::setSelectionStart( int column , int line , bool columnMode )
135 {
136  _screen->setSelectionStart( column , qMin(line + currentLine(),endWindowLine()) , columnMode);
137 
138  _bufferNeedsUpdate = true;
139  emit selectionChanged();
140 }
141 
143 {
144  _screen->setSelectionEnd( column , qMin(line + currentLine(),endWindowLine()) );
145 
146  _bufferNeedsUpdate = true;
147  emit selectionChanged();
148 }
149 
151 {
152  return _screen->isSelected( column , qMin(line + currentLine(),endWindowLine()) );
153 }
154 
156 {
158 
159  emit selectionChanged();
160 }
161 
163 {
164  Q_ASSERT(lines > 0);
165  _windowLines = lines;
166 }
168 {
169  return _windowLines;
170 }
171 
173 {
174  return _screen->getColumns();
175 }
176 
178 {
179  return _screen->getHistLines() + _screen->getLines();
180 }
181 
183 {
184  return _screen->getColumns();
185 }
186 
188 {
189  QPoint position;
190 
191  position.setX( _screen->getCursorX() );
192  position.setY( _screen->getCursorY() );
193 
194  return position;
195 }
196 
198 {
199  return qBound(0,_currentLine,lineCount()-windowLines());
200 }
201 
203 {
204  if ( mode == ScrollLines )
205  {
206  scrollTo( currentLine() + amount );
207  }
208  else if ( mode == ScrollPages )
209  {
210  scrollTo( currentLine() + amount * ( windowLines() / 2 ) );
211  }
212 }
213 
215 {
216  return currentLine() == (lineCount()-windowLines());
217 }
218 
220 {
221  int maxCurrentLineNumber = lineCount() - windowLines();
222  line = qBound(0,line,maxCurrentLineNumber);
223 
224  const int delta = line - _currentLine;
225  _currentLine = line;
226 
227  // keep track of number of lines scrolled by,
228  // this can be reset by calling resetScrollCount()
229  _scrollCount += delta;
230 
231  _bufferNeedsUpdate = true;
232 
233  emit scrolled(_currentLine);
234 }
235 
236 void ScreenWindow::setTrackOutput(bool trackOutput)
237 {
239 }
240 
242 {
243  return _trackOutput;
244 }
245 
247 {
248  return _scrollCount;
249 }
250 
252 {
253  _scrollCount = 0;
254 }
255 
257 {
258  bool equalToScreenSize = windowLines() == _screen->getLines();
259 
260  if ( atEndOfOutput() && equalToScreenSize )
261  return _screen->lastScrolledRegion();
262  else
263  return QRect(0,0,windowColumns(),windowLines());
264 }
265 
267 {
268  // move window to the bottom of the screen and update scroll count
269  // if this window is currently tracking the bottom of the screen
270  if ( _trackOutput )
271  {
274  }
275  else
276  {
277  // if the history is not unlimited then it may
278  // have run out of space and dropped the oldest
279  // lines of output - in this case the screen
280  // window's current line number will need to
281  // be adjusted - otherwise the output will scroll
282  _currentLine = qMax(0,_currentLine -
283  _screen->droppedLines());
284 
285  // ensure that the screen window's current position does
286  // not go beyond the bottom of the screen
287  _currentLine = qMin( _currentLine , _screen->getHistLines() );
288  }
289 
290  _bufferNeedsUpdate = true;
291 
292  emit outputChanged();
293 }
294 
void clearSelection()
Clears the current selection.
Definition: Screen.cpp:1194
OCTAVE_EXPORT octave_value_list column
Definition: sparse.cc:123
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:834
int columnCount() const
Returns the total number of columns in the screen.
void setScreen(Screen *screen)
Sets the screen which this window looks onto.
QString selectedText(bool preserveLineBreaks) const
Returns the text which is currently selected.
void setWindowLines(int lines)
Sets the number of lines in the window.
A single character in the terminal which consists of a unicode character value, foreground and backgr...
Definition: Character.h:55
int getCursorY() const
Returns the line which the cursor is positioned on.
Definition: Screen.cpp:945
Screen * screen() const
Returns the screen which this window looks onto.
An image of characters with associated attributes.
Definition: Screen.h:75
int getLines()
Return the number of lines.
Definition: Screen.h:377
virtual ~ScreenWindow()
int getColumns()
Return the number of columns.
Definition: Screen.h:379
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
void selectionChanged()
Emitted when the selection is changed.
int endWindowLine() const
QVector< LineProperty > getLineProperties(int startLine, int endLine) const
Returns the additional attributes associated with lines in the image.
Definition: Screen.cpp:602
bool _bufferNeedsUpdate
Definition: ScreenWindow.h:243
Screen * _screen
Definition: ScreenWindow.h:240
Character * _windowBuffer
Definition: ScreenWindow.h:241
int windowColumns() const
Returns the number of columns in the window.
QPoint cursorPosition() const
Returns the position of the cursor within the window.
void getSelectionStart(int &column, int &line)
Retrieves the start of the selection or the cursor position if there is no selection.
Definition: Screen.cpp:1201
int lineCount() const
Returns the total number of lines in the screen.
void fillUnusedArea()
void setSelectionStart(const int column, const int line, const bool columnmode)
Sets the start of the selection.
Definition: Screen.cpp:1227
bool isSelected(int column, int line)
Returns true if the character at line , column is part of the selection.
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:1261
void getSelectionEnd(int &column, int &line)
Retrieves the end of the selection within the window.
int windowLines() const
Returns the number of lines in the window.
int getCursorX() const
Returns the column which the cursor is positioned at.
Definition: Screen.cpp:940
bool atEndOfOutput() const
Convenience method.
void setSelectionStart(int column, int line, bool columnMode)
Sets the start of the selection to the given line and column within the window.
With real return the complex result
Definition: data.cc:3375
void getImage(Character *dest, int size, int startLine, int endLine) const
Returns the current screen image.
Definition: Screen.cpp:564
void getSelectionEnd(int &column, int &line)
Retrieves the end of the selection or the cursor position if there is no selection.
Definition: Screen.cpp:1214
void setTrackOutput(bool trackOutput)
Specifies whether the window should automatically move to the bottom of the screen when new output is...
Character * getImage()
Returns the image of characters which are currently visible through this window onto the screen...
void resetScrollCount()
Resets the count of scrolled lines returned by scrollCount()
ScreenWindow(QObject *parent=0)
Constructs a new screen window with the given parent.
QVector< LineProperty > getLineProperties()
Returns the line attributes associated with the lines of characters which are currently visible throu...
void scrollTo(int line)
Scrolls the window so that line is at the top of the window.
void notifyOutputChanged()
Notifies the window that the contents of the associated terminal screen have changed.
void clearSelection()
Clears the current selection.
static void fillWithDefaultChar(Character *dest, int count)
Fills the buffer dest with count instances of the default (ie.
Definition: Screen.cpp:1562
QRect lastScrolledRegion() const
Returns the region of the image which was last scrolled.
Definition: Screen.cpp:867
QRect scrollRegion() const
Returns the area of the window which was last scrolled, this is usually the whole window area...
void scrolled(int line)
Emitted when the screen window is scrolled to a different position.
int scrollCount() const
Returns the number of lines which the region of the window specified by scrollRegion() has been scrol...
int currentLine() const
Returns the index of the line which is currently at the top of this window.
int _windowBufferSize
Definition: ScreenWindow.h:242
void setSelectionEnd(const int column, const int line)
Sets the end of the current selection.
Definition: Screen.cpp:1240
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable or any other valid Octave code The number of return their size
Definition: input.cc:871
void setSelectionEnd(int column, int line)
Sets the end of the selection to the given line and column within the window.
int getHistLines()
Return the number of lines in the history buffer.
Definition: Screen.cpp:1522
bool trackOutput() const
Returns whether the window automatically moves to the bottom of the screen as new output is added...
void scrollBy(RelativeScrollMode mode, int amount)
Scrolls the window relative to its current position on the screen.
QString selectedText(bool preserveLineBreaks)
Convenience method.
Definition: Screen.cpp:1281
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:838
void outputChanged()
Emitted when the contents of the associated terminal screen ( see screen() ) changes.
void getSelectionStart(int &column, int &line)
Retrieves the start of the selection within the window.