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