2
This file is part of Konsole, KDE's terminal.
4
Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
5
Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
7
Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29
#include <QtCore/QRect>
30
#include <QtCore/QTextStream>
31
#include <QtCore/QVarLengthArray>
34
#include "Character.h"
42
#define MODE_NewLine 5
43
#define MODES_SCREEN 6
52
int mode[MODES_SCREEN];
55
class TerminalCharacterDecoder;
58
\brief An image of characters with associated attributes.
60
The terminal emulation ( Emulation ) receives a serial stream of
61
characters from the program currently running in the terminal.
62
From this stream it creates an image of characters which is ultimately
63
rendered by the display widget ( TerminalDisplay ). Some types of emulation
64
may have more than one screen image.
66
getImage() is used to retrieve the currently visible image
67
which is then used by the display widget to draw the output from the
70
The number of lines of output history which are kept in addition to the current
71
screen image depends on the history scroll being used to store the output.
72
The scroll is specified using setScroll()
73
The output history can be retrieved using writeToStream()
75
The screen image has a selection associated with it, specified using
76
setSelectionStart() and setSelectionEnd(). The selected text can be retrieved
77
using selectedText(). When getImage() is used to retrieve the the visible image,
78
characters which are part of the selection have their colours inverted.
83
/** Construct a new screen image of size @p lines by @p columns. */
84
Screen(int lines, int columns);
90
/** Move the cursor up by @p n lines. */
91
void cursorUp (int n);
92
/** Move the cursor down by @p n lines. */
93
void cursorDown (int n);
94
/** Move the cursor to the left by @p n columns. */
95
void cursorLeft (int n);
96
/** Move the cursor to the right by @p n columns. */
97
void cursorRight (int n);
98
/** Position the cursor on line @p y. */
99
void setCursorY (int y);
100
/** Position the cursor at column @p x. */
101
void setCursorX (int x);
102
/** Position the cursor at line @p y, column @p x. */
103
void setCursorYX (int y, int x);
105
* Sets the margins for scrolling the screen.
107
* @param topLine The top line of the new scrolling margin.
108
* @param bottomLine The bottom line of the new scrolling margin.
110
void setMargins (int topLine , int bottomLine);
111
/** Returns the top line of the scrolling region. */
112
int topMargin() const;
113
/** Returns the bottom line of the scrolling region. */
114
int bottomMargin() const;
117
* Resets the scrolling margins back to the top and bottom lines
120
void setDefaultMargins();
123
* Moves the cursor down one line, if the MODE_NewLine mode
124
* flag is enabled then the cursor is returned to the leftmost
127
* Equivalent to NextLine() if the MODE_NewLine flag is set
128
* or index() otherwise.
132
* Moves the cursor down one line and positions it at the beginning
138
* Move the cursor down one line. If the cursor is on the bottom
139
* line of the scrolling region (as returned by bottomMargin()) the
140
* scrolling region is scrolled up by one line instead.
144
* Move the cursor up one line. If the cursor is on the top line
145
* of the scrolling region (as returned by topMargin()) the scrolling
146
* region is scrolled down by one line instead.
151
* Scroll the scrolling region of the screen up by @p n lines.
152
* The scrolling region is initially the whole screen, but can be changed
155
void scrollUp(int n);
157
* Scroll the scrolling region of the screen down by @p n lines.
158
* The scrolling region is initially the whole screen, but can be changed
161
void scrollDown(int n);
164
* Moves the cursor to the beginning of the current line.
165
* Equivalent to setCursorX(0)
169
* Moves the cursor one column to the left and erases the character
170
* at the new cursor position.
174
* Moves the cursor @p n tab-stops to the right.
176
void Tabulate (int n = 1);
178
* Moves the cursor @p n tab-stops to the left.
180
void backTabulate(int n);
185
* Erase @p n characters beginning from the current cursor position.
186
* This is equivalent to over-writing @p n characters starting with the current
187
* cursor position with spaces.
188
* If @p n is 0 then one character is erased.
190
void eraseChars (int n);
192
* Delete @p n characters beginning from the current cursor position.
193
* If @p n is 0 then one character is deleted.
195
void deleteChars (int n);
197
* Insert @p n blank characters beginning from the current cursor position.
198
* The position of the cursor is not altered.
199
* If @p n is 0 then one character is inserted.
201
void insertChars (int n);
203
* Removes @p n lines beginning from the current cursor position.
204
* The position of the cursor is not altered.
205
* If @p n is 0 then one line is removed.
207
void deleteLines (int n);
209
* Inserts @p lines beginning from the current cursor position.
210
* The position of the cursor is not altered.
211
* If @p n is 0 then one line is inserted.
213
void insertLines (int n);
214
/** Clears all the tab stops. */
215
void clearTabStops();
216
/** Sets or removes a tab stop at the cursor's current column. */
217
void changeTabStop(bool set);
219
/** Resets (clears) the specified screen @p mode. */
220
void resetMode (int mode);
221
/** Sets (enables) the specified screen @p mode. */
222
void setMode (int mode);
224
* Saves the state of the specified screen @p mode. It can be restored
225
* using restoreMode()
227
void saveMode (int mode);
228
/** Restores the state of a screen @p mode saved by calling saveMode() */
229
void restoreMode (int mode);
230
/** Returns whether the specified screen @p mode is enabled or not .*/
231
bool getMode (int mode) const;
234
* Saves the current position and appearence (text color and style) of the cursor.
235
* It can be restored by calling restoreCursor()
238
/** Restores the position and appearence of the cursor. See saveCursor() */
239
void restoreCursor();
241
/** Clear the whole screen, moving the current screen contents into the history first. */
242
void clearEntireScreen();
244
* Clear the area of the screen from the current cursor position to the end of
247
void clearToEndOfScreen();
249
* Clear the area of the screen from the current cursor position to the start
252
void clearToBeginOfScreen();
253
/** Clears the whole of the line on which the cursor is currently positioned. */
254
void clearEntireLine();
255
/** Clears from the current cursor position to the end of the line. */
256
void clearToEndOfLine();
257
/** Clears from the current cursor position to the beginning of the line. */
258
void clearToBeginOfLine();
260
/** Fills the entire screen with the letter 'E' */
264
* Enables the given @p rendition flag. Rendition flags control the appearence
265
* of characters on the screen.
267
* @see Character::rendition
269
void setRendition (int rendition);
271
* Disables the given @p rendition flag. Rendition flags control the appearence
272
* of characters on the screen.
274
* @see Character::rendition
276
void resetRendition(int rendition);
279
* Sets the cursor's foreground color.
280
* @param space The color space used by the @p color argument
281
* @param color The new foreground color. The meaning of this depends on
282
* the color @p space used.
284
* @see CharacterColor
286
void setForeColor (int space, int color);
288
* Sets the cursor's background color.
289
* @param space The color space used by the @p color argumnet.
290
* @param color The new background color. The meaning of this depends on
291
* the color @p space used.
293
* @see CharacterColor
295
void setBackColor (int space, int color);
297
* Resets the cursor's color back to the default and sets the
298
* character's rendition flags back to the default settings.
300
void setDefaultRendition();
302
/** Returns the column which the cursor is positioned at. */
303
int getCursorX() const;
304
/** Returns the line which the cursor is positioned on. */
305
int getCursorY() const;
307
/** TODO Document me */
310
* Sets the position of the cursor to the 'home' position at the top-left
311
* corner of the screen (0,0)
315
* Resets the state of the screen. This resets the various screen modes
316
* back to their default states. The cursor style and colors are reset
317
* (as if setDefaultRendition() had been called)
320
* <li>Line wrapping is enabled.</li>
321
* <li>Origin mode is disabled.</li>
322
* <li>Insert mode is disabled.</li>
323
* <li>Cursor mode is enabled. TODO Document me</li>
324
* <li>Screen mode is disabled. TODO Document me</li>
325
* <li>New line mode is disabled. TODO Document me</li>
328
* If @p clearScreen is true then the screen contents are erased entirely,
329
* otherwise they are unaltered.
331
void reset(bool clearScreen = true);
334
* Displays a new character at the current cursor position.
336
* If the cursor is currently positioned at the right-edge of the screen and
337
* line wrapping is enabled then the character is added at the start of a new
338
* line below the current one.
340
* If the MODE_Insert screen mode is currently enabled then the character
341
* is inserted at the current cursor position, otherwise it will replace the
342
* character already at the current cursor position.
344
void ShowCharacter(unsigned short c);
346
// Do composition with last shown character FIXME: Not implemented yet for KDE 4
347
void compose(const QString& compose);
350
* Resizes the image to a new fixed size of @p new_lines by @p new_columns.
351
* In the case that @p new_columns is smaller than the current number of columns,
352
* existing lines are not truncated. This prevents characters from being lost
353
* if the terminal display is resized smaller and then larger again.
355
* (note that in versions of Konsole prior to KDE 4, existing lines were
356
* truncated when making the screen image smaller)
358
void resizeImage(int new_lines, int new_columns);
361
* Returns the current screen image.
362
* The result is an array of Characters of size [getLines()][getColumns()] which
363
* must be freed by the caller after use.
365
* @param dest Buffer to copy the characters into
366
* @param size Size of @p dest in Characters
367
* @param startLine Index of first line to copy
368
* @param endLine Index of last line to copy
370
void getImage( Character* dest , int size , int startLine , int endLine ) const;
373
* Returns the additional attributes associated with lines in the image.
374
* The most important attribute is LINE_WRAPPED which specifies that the
376
* other attributes control the size of characters in the line.
378
QVector<LineProperty> getLineProperties( int startLine , int endLine ) const;
381
/** Return the number of lines. */
382
int getLines() { return lines; }
383
/** Return the number of columns. */
384
int getColumns() { return columns; }
385
/** Return the number of lines in the history buffer. */
388
* Sets the type of storage used to keep lines in the history.
389
* If @p copyPreviousScroll is true then the contents of the previous
390
* history buffer are copied into the new scroll.
392
void setScroll(const HistoryType& , bool copyPreviousScroll = true);
393
/** Returns the type of storage used to keep lines in the history. */
394
const HistoryType& getScroll();
396
* Returns true if this screen keeps lines that are scrolled off the screen
397
* in a history buffer.
402
* Sets the start of the selection.
404
* @param column The column index of the first character in the selection.
405
* @param line The line index of the first character in the selection.
406
* @param columnmode True if the selection is in column mode.
408
void setSelectionStart(const int column, const int line, const bool columnmode);
411
* Sets the end of the current selection.
413
* @param column The column index of the last character in the selection.
414
* @param line The line index of the last character in the selection.
416
void setSelectionEnd(const int column, const int line);
419
* Retrieves the start of the selection or the cursor position if there
422
void getSelectionStart(int& column , int& line);
425
* Retrieves the end of the selection or the cursor position if there
428
void getSelectionEnd(int& column , int& line);
430
/** Clears the current selection */
431
void clearSelection();
433
void setBusySelecting(bool busy) { sel_busy = busy; }
436
* Returns true if the character at (@p column, @p line) is part of the
439
bool isSelected(const int column,const int line) const;
442
* Convenience method. Returns the currently selected text.
443
* @param preserveLineBreaks Specifies whether new line characters should
444
* be inserted into the returned text at the end of each terminal line.
446
QString selectedText(bool preserveLineBreaks);
449
* Copies part of the output to a stream.
451
* @param decoder A decoder which coverts terminal characters into text
452
* @param from The first line in the history to retrieve
453
* @param to The last line in the history to retrieve
455
void writeToStream(TerminalCharacterDecoder* decoder, int from, int to);
458
* Sets the selection to line @p no in the history and returns
459
* the text of that line from the history buffer.
461
QString getHistoryLine(int no);
464
* Copies the selected characters, set using @see setSelBeginXY and @see setSelExtentXY
467
* @param decoder A decoder which converts terminal characters into text.
468
* PlainTextDecoder is the most commonly used decoder which coverts characters
469
* into plain text with no formatting.
470
* @param preserveLineBreaks Specifies whether new line characters should
471
* be inserted into the returned text at the end of each terminal line.
473
void writeSelectionToStream(TerminalCharacterDecoder* decoder , bool
474
preserveLineBreaks = true);
476
/** TODO Document me */
477
void checkSelection(int from, int to);
480
* Sets or clears an attribute of the current line.
482
* @param property The attribute to set or clear
483
* Possible properties are:
484
* LINE_WRAPPED: Specifies that the line is wrapped.
485
* LINE_DOUBLEWIDTH: Specifies that the characters in the current line should be double the normal width.
486
* LINE_DOUBLEHEIGHT:Specifies that the characters in the current line should be double the normal height.
487
* Double-height lines are formed of two lines containing the same characters,
488
* with both having the LINE_DOUBLEHEIGHT attribute. This allows other parts of the
489
* code to work on the assumption that all lines are the same height.
491
* @param enable true to apply the attribute to the current line or false to remove it
493
void setLineProperty(LineProperty property , bool enable);
497
* Returns the number of lines that the image has been scrolled up or down by,
498
* since the last call to resetScrolledLines().
500
* a positive return value indicates that the image has been scrolled up,
501
* a negative return value indicates that the image has been scrolled down.
503
int scrolledLines() const;
506
* Returns the region of the image which was last scrolled.
508
* This is the area of the image from the top margin to the
509
* bottom margin when the last scroll occurred.
511
QRect lastScrolledRegion() const;
514
* Resets the count of the number of lines that the image has been scrolled up or down by,
515
* see scrolledLines()
517
void resetScrolledLines();
520
* Returns the number of lines of output which have been
521
* dropped from the history since the last call
522
* to resetDroppedLines()
524
* If the history is not unlimited then it will drop
525
* the oldest lines of output if new lines are added when
528
int droppedLines() const;
531
* Resets the count of the number of lines dropped from
534
void resetDroppedLines();
537
* Fills the buffer @p dest with @p count instances of the default (ie. blank)
540
static void fillWithDefaultChar(Character* dest, int count);
544
//copies a line of text from the screen or history into a stream using a
545
//specified character decoder
546
//line - the line number to copy, from 0 (the earliest line in the history) up to
547
// hist->getLines() + lines - 1
548
//start - the first column on the line to copy
549
//count - the number of characters on the line to copy
550
//decoder - a decoder which coverts terminal characters (an Character array) into text
551
//appendNewLine - if true a new line character (\n) is appended to the end of the line
552
void copyLineToStream(int line,
555
TerminalCharacterDecoder* decoder,
557
bool preserveLineBreaks);
559
//fills a section of the screen image with the character 'c'
560
//the parameters are specified as offsets from the start of the screen image.
561
//the loc(x,y) macro can be used to generate these values from a column,line pair.
562
void clearImage(int loca, int loce, char c);
564
//move screen image between 'sourceBegin' and 'sourceEnd' to 'dest'.
565
//the parameters are specified as offsets from the start of the screen image.
566
//the loc(x,y) macro can be used to generate these values from a column,line pair.
567
void moveImage(int dest, int sourceBegin, int sourceEnd);
569
void scrollUp(int from, int i);
570
void scrollDown(int from, int i);
576
void effectiveRendition();
577
void reverseRendition(Character& p) const;
579
bool isSelectionValid() const;
581
// copies 'count' lines from the screen buffer into 'dest',
582
// starting from 'startLine', where 0 is the first line in the screen buffer
583
void copyFromScreen(Character* dest, int startLine, int count) const;
584
// copies 'count' lines from the history buffer into 'dest',
585
// starting from 'startLine', where 0 is the first line in the history
586
void copyFromHistory(Character* dest, int startLine, int count) const;
589
// screen image ----------------
593
typedef QVector<Character> ImageLine; // [0..columns]
594
ImageLine* screenLines; // [lines]
597
QRect _lastScrolledRegion;
601
QVarLengthArray<LineProperty,64> lineProperties;
603
// history buffer ---------------
610
// cursor color and rendition info
611
CharacterColor cu_fg; // foreground
612
CharacterColor cu_bg; // background
613
quint8 cu_re; // rendition
615
// margins ----------------
616
int tmargin; // top margin
617
int bmargin; // bottom margin
619
// states ----------------
622
// ----------------------------
626
// selection -------------------
627
int sel_begin; // The first location selected.
628
int sel_TL; // TopLeft Location.
629
int sel_BR; // Bottom Right Location.
630
bool sel_busy; // Busy making a selection.
631
bool columnmode; // Column selection mode
633
// effective colors and rendition ------------
634
CharacterColor ef_fg; // These are derived from
635
CharacterColor ef_bg; // the cu_* variables above
636
quint8 ef_re; // to speed up operation
639
// save cursor, rendition & states ------------
648
CharacterColor sa_cu_fg;
649
CharacterColor sa_cu_bg;
651
// last position where we added a character
657
static Character defaultChar;