[45157b3] | 1 | /* |
---|
[64efc22] | 2 | Copyright 2007-2008 by Robert Knight <robertknight@gmail.com> |
---|
[45157b3] | 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 |
---|
[64efc22] | 24 | #include <QObject> |
---|
| 25 | #include <QPoint> |
---|
| 26 | #include <QRect> |
---|
[45157b3] | 27 | |
---|
| 28 | // Konsole |
---|
| 29 | #include "Character.h" |
---|
[64efc22] | 30 | #include "KeyboardTranslator.h" |
---|
[45157b3] | 31 | |
---|
| 32 | namespace Konsole |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | class Screen; |
---|
| 36 | |
---|
| 37 | /** |
---|
[64efc22] | 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() |
---|
[45157b3] | 43 | * |
---|
| 44 | * Use the scrollTo() method to scroll the window up and down on the screen. |
---|
[64efc22] | 45 | * Use the getImage() method to retrieve the character image which is currently visible in the window. |
---|
[45157b3] | 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: |
---|
[64efc22] | 59 | /** |
---|
[45157b3] | 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 | */ |
---|
[64efc22] | 68 | ScreenWindow(QObject* parent = nullptr); |
---|
| 69 | ~ScreenWindow() override; |
---|
[45157b3] | 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 | |
---|
[64efc22] | 76 | /** |
---|
[45157b3] | 77 | * Returns the image of characters which are currently visible through this window |
---|
| 78 | * onto the screen. |
---|
| 79 | * |
---|
[64efc22] | 80 | * The returned buffer is managed by the ScreenWindow instance and does not need to be |
---|
[45157b3] | 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 |
---|
[64efc22] | 93 | * specified by scrollRegion() has been scrolled by since the last call |
---|
| 94 | * to resetScrollCount(). scrollRegion() is in most cases the |
---|
[45157b3] | 95 | * whole window, but will be a smaller area in, for example, applications |
---|
| 96 | * which provide split-screen facilities. |
---|
| 97 | * |
---|
[64efc22] | 98 | * This is not guaranteed to be accurate, but allows views to optimize |
---|
[45157b3] | 99 | * rendering by reducing the amount of costly text rendering that |
---|
[64efc22] | 100 | * needs to be done when the output is scrolled. |
---|
[45157b3] | 101 | */ |
---|
| 102 | int scrollCount() const; |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Resets the count of scrolled lines returned by scrollCount() |
---|
| 106 | */ |
---|
| 107 | void resetScrollCount(); |
---|
| 108 | |
---|
| 109 | /** |
---|
[64efc22] | 110 | * Returns the area of the window which was last scrolled, this is |
---|
[45157b3] | 111 | * usually the whole window area. |
---|
| 112 | * |
---|
| 113 | * Like scrollCount(), this is not guaranteed to be accurate, |
---|
[64efc22] | 114 | * but allows views to optimize rendering. |
---|
[45157b3] | 115 | */ |
---|
| 116 | QRect scrollRegion() const; |
---|
| 117 | |
---|
[64efc22] | 118 | /** |
---|
| 119 | * Sets the start of the selection to the given @p line and @p column within |
---|
[45157b3] | 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 | */ |
---|
[64efc22] | 127 | void setSelectionEnd( int column , int line ); |
---|
[45157b3] | 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 ); |
---|
[64efc22] | 140 | /** |
---|
[45157b3] | 141 | * Clears the current selection |
---|
| 142 | */ |
---|
| 143 | void clearSelection(); |
---|
| 144 | |
---|
[64efc22] | 145 | /** Sets the number of lines in the window */ |
---|
| 146 | void setWindowLines(int lines); |
---|
[45157b3] | 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; |
---|
[64efc22] | 151 | |
---|
[45157b3] | 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 | |
---|
[64efc22] | 160 | /** |
---|
| 161 | * Returns the position of the cursor |
---|
[45157b3] | 162 | * within the window. |
---|
| 163 | */ |
---|
| 164 | QPoint cursorPosition() const; |
---|
| 165 | |
---|
[64efc22] | 166 | /** |
---|
[45157b3] | 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 | |
---|
[64efc22] | 175 | /** Describes the units which scrollBy() moves the window by. */ |
---|
[45157b3] | 176 | enum RelativeScrollMode |
---|
| 177 | { |
---|
[64efc22] | 178 | /** Scroll the window down by a given number of lines. */ |
---|
[45157b3] | 179 | ScrollLines, |
---|
[64efc22] | 180 | /** |
---|
| 181 | * Scroll the window down by a given number of pages, where |
---|
| 182 | * one page is windowLines() lines |
---|
| 183 | */ |
---|
[45157b3] | 184 | ScrollPages |
---|
| 185 | }; |
---|
| 186 | |
---|
[64efc22] | 187 | /** |
---|
[45157b3] | 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 |
---|
[64efc22] | 191 | * of pages to scroll. |
---|
[45157b3] | 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 | |
---|
[64efc22] | 198 | /** |
---|
[45157b3] | 199 | * Specifies whether the window should automatically move to the bottom |
---|
| 200 | * of the screen when new output is added. |
---|
| 201 | * |
---|
[64efc22] | 202 | * If this is set to true, the window will be moved to the bottom of the associated screen ( see |
---|
[45157b3] | 203 | * screen() ) when the notifyOutputChanged() method is called. |
---|
| 204 | */ |
---|
| 205 | void setTrackOutput(bool trackOutput); |
---|
[64efc22] | 206 | /** |
---|
[45157b3] | 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: |
---|
[64efc22] | 220 | /** |
---|
[45157b3] | 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 | |
---|
[64efc22] | 227 | void handleCommandFromKeyboard(KeyboardTranslator::Command command); |
---|
| 228 | |
---|
[45157b3] | 229 | signals: |
---|
| 230 | /** |
---|
[64efc22] | 231 | * Emitted when the contents of the associated terminal screen (see screen()) changes. |
---|
[45157b3] | 232 | */ |
---|
| 233 | void outputChanged(); |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * Emitted when the screen window is scrolled to a different position. |
---|
[64efc22] | 237 | * |
---|
[45157b3] | 238 | * @param line The line which is now at the top of the window. |
---|
| 239 | */ |
---|
| 240 | void scrolled(int line); |
---|
| 241 | |
---|
[64efc22] | 242 | /** Emitted when the selection is changed. */ |
---|
[45157b3] | 243 | void selectionChanged(); |
---|
| 244 | |
---|
[64efc22] | 245 | void scrollToEnd(); |
---|
| 246 | |
---|
[45157b3] | 247 | private: |
---|
[64efc22] | 248 | int endWindowLine() const; |
---|
| 249 | void fillUnusedArea(); |
---|
[45157b3] | 250 | |
---|
| 251 | Screen* _screen; // see setScreen() , screen() |
---|
[64efc22] | 252 | Character* _windowBuffer; |
---|
| 253 | int _windowBufferSize; |
---|
| 254 | bool _bufferNeedsUpdate; |
---|
[45157b3] | 255 | |
---|
[64efc22] | 256 | int _windowLines; |
---|
[45157b3] | 257 | int _currentLine; // see scrollTo() , currentLine() |
---|
[64efc22] | 258 | bool _trackOutput; // see setTrackOutput() , trackOutput() |
---|
[45157b3] | 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 |
---|