1 | /* |
---|
2 | Copyright 2007-2008 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 | #ifndef SCREENWINDOW_H |
---|
21 | #define SCREENWINDOW_H |
---|
22 | |
---|
23 | // Qt |
---|
24 | #include <QObject> |
---|
25 | #include <QPoint> |
---|
26 | #include <QRect> |
---|
27 | |
---|
28 | // Konsole |
---|
29 | #include "Character.h" |
---|
30 | #include "KeyboardTranslator.h" |
---|
31 | |
---|
32 | namespace Konsole |
---|
33 | { |
---|
34 | |
---|
35 | class Screen; |
---|
36 | |
---|
37 | /** |
---|
38 | * Provides a window onto a section of a terminal screen. A terminal widget can then render |
---|
39 | * the contents of the window and use the window to change the terminal screen's selection |
---|
40 | * in response to mouse or keyboard input. |
---|
41 | * |
---|
42 | * A new ScreenWindow for a terminal session can be created by calling Emulation::createWindow() |
---|
43 | * |
---|
44 | * Use the scrollTo() method to scroll the window up and down on the screen. |
---|
45 | * Use the getImage() method to retrieve the character image which is currently visible in the window. |
---|
46 | * |
---|
47 | * setTrackOutput() controls whether the window moves to the bottom of the associated screen when new |
---|
48 | * lines are added to it. |
---|
49 | * |
---|
50 | * Whenever the output from the underlying screen is changed, the notifyOutputChanged() slot should |
---|
51 | * be called. This in turn will update the window's position and emit the outputChanged() signal |
---|
52 | * if necessary. |
---|
53 | */ |
---|
54 | class ScreenWindow : public QObject |
---|
55 | { |
---|
56 | Q_OBJECT |
---|
57 | |
---|
58 | public: |
---|
59 | /** |
---|
60 | * Constructs a new screen window with the given parent. |
---|
61 | * A screen must be specified by calling setScreen() before calling getImage() or getLineProperties(). |
---|
62 | * |
---|
63 | * You should not call this constructor directly, instead use the Emulation::createWindow() method |
---|
64 | * to create a window on the emulation which you wish to view. This allows the emulation |
---|
65 | * to notify the window when the associated screen has changed and synchronize selection updates |
---|
66 | * between all views on a session. |
---|
67 | */ |
---|
68 | ScreenWindow(QObject* parent = nullptr); |
---|
69 | ~ScreenWindow() override; |
---|
70 | |
---|
71 | /** Sets the screen which this window looks onto */ |
---|
72 | void setScreen(Screen* screen); |
---|
73 | /** Returns the screen which this window looks onto */ |
---|
74 | Screen* screen() const; |
---|
75 | |
---|
76 | /** |
---|
77 | * Returns the image of characters which are currently visible through this window |
---|
78 | * onto the screen. |
---|
79 | * |
---|
80 | * The returned buffer is managed by the ScreenWindow instance and does not need to be |
---|
81 | * deleted by the caller. |
---|
82 | */ |
---|
83 | Character* getImage(); |
---|
84 | |
---|
85 | /** |
---|
86 | * Returns the line attributes associated with the lines of characters which |
---|
87 | * are currently visible through this window |
---|
88 | */ |
---|
89 | QVector<LineProperty> getLineProperties(); |
---|
90 | |
---|
91 | /** |
---|
92 | * Returns the number of lines which the region of the window |
---|
93 | * specified by scrollRegion() has been scrolled by since the last call |
---|
94 | * to resetScrollCount(). scrollRegion() is in most cases the |
---|
95 | * whole window, but will be a smaller area in, for example, applications |
---|
96 | * which provide split-screen facilities. |
---|
97 | * |
---|
98 | * This is not guaranteed to be accurate, but allows views to optimize |
---|
99 | * rendering by reducing the amount of costly text rendering that |
---|
100 | * needs to be done when the output is scrolled. |
---|
101 | */ |
---|
102 | int scrollCount() const; |
---|
103 | |
---|
104 | /** |
---|
105 | * Resets the count of scrolled lines returned by scrollCount() |
---|
106 | */ |
---|
107 | void resetScrollCount(); |
---|
108 | |
---|
109 | /** |
---|
110 | * Returns the area of the window which was last scrolled, this is |
---|
111 | * usually the whole window area. |
---|
112 | * |
---|
113 | * Like scrollCount(), this is not guaranteed to be accurate, |
---|
114 | * but allows views to optimize rendering. |
---|
115 | */ |
---|
116 | QRect scrollRegion() const; |
---|
117 | |
---|
118 | /** |
---|
119 | * Sets the start of the selection to the given @p line and @p column within |
---|
120 | * the window. |
---|
121 | */ |
---|
122 | void setSelectionStart( int column , int line , bool columnMode ); |
---|
123 | /** |
---|
124 | * Sets the end of the selection to the given @p line and @p column within |
---|
125 | * the window. |
---|
126 | */ |
---|
127 | void setSelectionEnd( int column , int line ); |
---|
128 | /** |
---|
129 | * Retrieves the start of the selection within the window. |
---|
130 | */ |
---|
131 | void getSelectionStart( int& column , int& line ); |
---|
132 | /** |
---|
133 | * Retrieves the end of the selection within the window. |
---|
134 | */ |
---|
135 | void getSelectionEnd( int& column , int& line ); |
---|
136 | /** |
---|
137 | * Returns true if the character at @p line , @p column is part of the selection. |
---|
138 | */ |
---|
139 | bool isSelected( int column , int line ); |
---|
140 | /** |
---|
141 | * Clears the current selection |
---|
142 | */ |
---|
143 | void clearSelection(); |
---|
144 | |
---|
145 | /** Sets the number of lines in the window */ |
---|
146 | void setWindowLines(int lines); |
---|
147 | /** Returns the number of lines in the window */ |
---|
148 | int windowLines() const; |
---|
149 | /** Returns the number of columns in the window */ |
---|
150 | int windowColumns() const; |
---|
151 | |
---|
152 | /** Returns the total number of lines in the screen */ |
---|
153 | int lineCount() const; |
---|
154 | /** Returns the total number of columns in the screen */ |
---|
155 | int columnCount() const; |
---|
156 | |
---|
157 | /** Returns the index of the line which is currently at the top of this window */ |
---|
158 | int currentLine() const; |
---|
159 | |
---|
160 | /** |
---|
161 | * Returns the position of the cursor |
---|
162 | * within the window. |
---|
163 | */ |
---|
164 | QPoint cursorPosition() const; |
---|
165 | |
---|
166 | /** |
---|
167 | * Convenience method. Returns true if the window is currently at the bottom |
---|
168 | * of the screen. |
---|
169 | */ |
---|
170 | bool atEndOfOutput() const; |
---|
171 | |
---|
172 | /** Scrolls the window so that @p line is at the top of the window */ |
---|
173 | void scrollTo( int line ); |
---|
174 | |
---|
175 | /** Describes the units which scrollBy() moves the window by. */ |
---|
176 | enum RelativeScrollMode |
---|
177 | { |
---|
178 | /** Scroll the window down by a given number of lines. */ |
---|
179 | ScrollLines, |
---|
180 | /** |
---|
181 | * Scroll the window down by a given number of pages, where |
---|
182 | * one page is windowLines() lines |
---|
183 | */ |
---|
184 | ScrollPages |
---|
185 | }; |
---|
186 | |
---|
187 | /** |
---|
188 | * Scrolls the window relative to its current position on the screen. |
---|
189 | * |
---|
190 | * @param mode Specifies whether @p amount refers to the number of lines or the number |
---|
191 | * of pages to scroll. |
---|
192 | * @param amount The number of lines or pages ( depending on @p mode ) to scroll by. If |
---|
193 | * this number is positive, the view is scrolled down. If this number is negative, the view |
---|
194 | * is scrolled up. |
---|
195 | */ |
---|
196 | void scrollBy( RelativeScrollMode mode , int amount ); |
---|
197 | |
---|
198 | /** |
---|
199 | * Specifies whether the window should automatically move to the bottom |
---|
200 | * of the screen when new output is added. |
---|
201 | * |
---|
202 | * If this is set to true, the window will be moved to the bottom of the associated screen ( see |
---|
203 | * screen() ) when the notifyOutputChanged() method is called. |
---|
204 | */ |
---|
205 | void setTrackOutput(bool trackOutput); |
---|
206 | /** |
---|
207 | * Returns whether the window automatically moves to the bottom of the screen as |
---|
208 | * new output is added. See setTrackOutput() |
---|
209 | */ |
---|
210 | bool trackOutput() const; |
---|
211 | |
---|
212 | /** |
---|
213 | * Returns the text which is currently selected. |
---|
214 | * |
---|
215 | * @param preserveLineBreaks See Screen::selectedText() |
---|
216 | */ |
---|
217 | QString selectedText( bool preserveLineBreaks ) const; |
---|
218 | |
---|
219 | public slots: |
---|
220 | /** |
---|
221 | * Notifies the window that the contents of the associated terminal screen have changed. |
---|
222 | * This moves the window to the bottom of the screen if trackOutput() is true and causes |
---|
223 | * the outputChanged() signal to be emitted. |
---|
224 | */ |
---|
225 | void notifyOutputChanged(); |
---|
226 | |
---|
227 | void handleCommandFromKeyboard(KeyboardTranslator::Command command); |
---|
228 | |
---|
229 | signals: |
---|
230 | /** |
---|
231 | * Emitted when the contents of the associated terminal screen (see screen()) changes. |
---|
232 | */ |
---|
233 | void outputChanged(); |
---|
234 | |
---|
235 | /** |
---|
236 | * Emitted when the screen window is scrolled to a different position. |
---|
237 | * |
---|
238 | * @param line The line which is now at the top of the window. |
---|
239 | */ |
---|
240 | void scrolled(int line); |
---|
241 | |
---|
242 | /** Emitted when the selection is changed. */ |
---|
243 | void selectionChanged(); |
---|
244 | |
---|
245 | void scrollToEnd(); |
---|
246 | |
---|
247 | private: |
---|
248 | int endWindowLine() const; |
---|
249 | void fillUnusedArea(); |
---|
250 | |
---|
251 | Screen* _screen; // see setScreen() , screen() |
---|
252 | Character* _windowBuffer; |
---|
253 | int _windowBufferSize; |
---|
254 | bool _bufferNeedsUpdate; |
---|
255 | |
---|
256 | int _windowLines; |
---|
257 | int _currentLine; // see scrollTo() , currentLine() |
---|
258 | bool _trackOutput; // see setTrackOutput() , trackOutput() |
---|
259 | int _scrollCount; // count of lines which the window has been scrolled by since |
---|
260 | // the last call to resetScrollCount() |
---|
261 | }; |
---|
262 | |
---|
263 | } |
---|
264 | #endif // SCREENWINDOW_H |
---|