Screen.h 24.7 KB
Newer Older
1
/*
2
3
    This file is part of Konsole, KDE's terminal.

4
5
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
    Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6

7
8
9
10
11
12
13
14
15
    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.
16

17
18
19
20
    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.
21
22
*/

23
24
#ifndef SCREEN_H
#define SCREEN_H
25

26
// Qt
27
28
29
30
31
#include <QRect>
#include <QSet>
#include <QVector>
#include <QBitArray>
#include <QVarLengthArray>
32

33
// Konsole
34
#include "Character.h"
35

36
37
38
39
40
41
42
43
#define MODE_Origin    0
#define MODE_Wrap      1
#define MODE_Insert    2
#define MODE_Screen    3
#define MODE_Cursor    4
#define MODE_NewLine   5
#define MODES_SCREEN   6

Kurt Hindenburg's avatar
Kurt Hindenburg committed
44
namespace Konsole {
45
class TerminalCharacterDecoder;
46
class TerminalDisplay;
47
48
class HistoryType;
class HistoryScroll;
49

50
51
52
/**
    \brief An image of characters with associated attributes.

53
    The terminal emulation ( Emulation ) receives a serial stream of
54
55
    characters from the program currently running in the terminal.
    From this stream it creates an image of characters which is ultimately
56
    rendered by the display widget ( TerminalDisplay ).  Some types of emulation
Jekyll Wu's avatar
Jekyll Wu committed
57
    may have more than one screen image.
58

Robert Knight's avatar
   
Robert Knight committed
59
    getImage() is used to retrieve the currently visible image
60
    which is then used by the display widget to draw the output from the
Jekyll Wu's avatar
Jekyll Wu committed
61
    terminal.
62
63

    The number of lines of output history which are kept in addition to the current
Jekyll Wu's avatar
Jekyll Wu committed
64
    screen image depends on the history scroll being used to store the output.
65
66
67
    The scroll is specified using setScroll()
    The output history can be retrieved using writeToStream()

Jekyll Wu's avatar
Jekyll Wu committed
68
    The screen image has a selection associated with it, specified using
69
    setSelectionStart() and setSelectionEnd().  The selected text can be retrieved
70
    using selectedText().  When getImage() is used to retrieve the visible image,
Jekyll Wu's avatar
Jekyll Wu committed
71
    characters which are part of the selection have their colors inverted.
72
*/
73
class Screen
74
75
{
public:
76
77
78
79
80
81
82
83
84
    /* PlainText: Return plain text (default)
     * ConvertToHtml: Specifies if returned text should have HTML tags.
     * PreserveLineBreaks: Specifies whether new line characters should be
     *      inserted into the returned text at the end of each terminal line.
     * TrimLeadingWhitespace: Specifies whether leading spaces should be
     *      trimmed in the returned text.
     * TrimTrailingWhitespace: Specifies whether trailing spaces should be
     *      trimmed in the returned text.
     */
85
86
87
88
89
90
91
92
93
    enum DecodingOption {
        PlainText = 0x0,
        ConvertToHtml = 0x1,
        PreserveLineBreaks = 0x2,
        TrimLeadingWhitespace = 0x4,
        TrimTrailingWhitespace = 0x8
    };
    Q_DECLARE_FLAGS(DecodingOptions, DecodingOption)

94
    /** Construct a new screen image of size @p lines by @p columns. */
95
96
    Screen(int lines, int columns);
    ~Screen();
97

Kurt Hindenburg's avatar
Kurt Hindenburg committed
98
99
    Screen(const Screen &) = delete;
    Screen &operator=(const Screen &) = delete;
100

Jekyll Wu's avatar
Jekyll Wu committed
101
    // VT100/2 Operations
102
    // Cursor Movement
103

Jekyll Wu's avatar
Jekyll Wu committed
104
105
    /**
     * Move the cursor up by @p n lines.  The cursor will stop at the
106
107
     * top margin.
     */
108
    void cursorUp(int n);
Jekyll Wu's avatar
Jekyll Wu committed
109
    /**
110
111
112
     * Move the cursor down by @p n lines.  The cursor will stop at the
     * bottom margin.
     */
113
    void cursorDown(int n);
Jekyll Wu's avatar
Jekyll Wu committed
114
    /**
115
116
117
     * Move the cursor to the left by @p n columns.
     * The cursor will stop at the first column.
     */
118
    void cursorLeft(int n);
Jekyll Wu's avatar
Jekyll Wu committed
119
    /**
120
121
122
     * Move the cursor to the right by @p n columns.
     * The cursor will stop at the right-most column.
     */
123
    void cursorRight(int n);
124
    /** Position the cursor on line @p y. */
125
    void setCursorY(int y);
126
    /** Position the cursor at column @p x. */
127
    void setCursorX(int x);
128
    /** Position the cursor at line @p y, column @p x. */
129
    void setCursorYX(int y, int x);
130
131
132
    /**
     * Sets the margins for scrolling the screen.
     *
133
134
     * @param top The top line of the new scrolling margin.
     * @param bot The bottom line of the new scrolling margin.
135
     */
136
    void setMargins(int top, int bot);
Jekyll Wu's avatar
Jekyll Wu committed
137
    /** Returns the top line of the scrolling region. */
138
    int topMargin() const;
139
    /** Returns the bottom line of the scrolling region. */
140
141
    int bottomMargin() const;

Jekyll Wu's avatar
Jekyll Wu committed
142
    /**
143
144
145
     * Resets the scrolling margins back to the top and bottom lines
     * of the screen.
     */
146
    void setDefaultMargins();
147

Jekyll Wu's avatar
Jekyll Wu committed
148
149
    /**
     * Moves the cursor down one line, if the MODE_NewLine mode
150
151
152
153
     * flag is enabled then the cursor is returned to the leftmost
     * column first.
     *
     * Equivalent to NextLine() if the MODE_NewLine flag is set
Jekyll Wu's avatar
Jekyll Wu committed
154
     * or index() otherwise.
155
     */
156
    void newLine();
157
158
    /**
     * Moves the cursor down one line and positions it at the beginning
159
     * of the line.  Equivalent to calling Return() followed by index()
160
     */
161
    void nextLine();
162

Jekyll Wu's avatar
Jekyll Wu committed
163
    /**
164
165
166
167
     * Move the cursor down one line.  If the cursor is on the bottom
     * line of the scrolling region (as returned by bottomMargin()) the
     * scrolling region is scrolled up by one line instead.
     */
168
    void index();
169
170
171
172
173
    /**
     * Move the cursor up one line.  If the cursor is on the top line
     * of the scrolling region (as returned by topMargin()) the scrolling
     * region is scrolled down by one line instead.
     */
174
    void reverseIndex();
175

Jekyll Wu's avatar
Jekyll Wu committed
176
177
178
    /**
     * Scroll the scrolling region of the screen up by @p n lines.
     * The scrolling region is initially the whole screen, but can be changed
179
     * using setMargins()
Jekyll Wu's avatar
Jekyll Wu committed
180
     */
181
    void scrollUp(int n);
182
183
184
185
186
    /**
     * Scroll the scrolling region of the screen down by @p n lines.
     * The scrolling region is initially the whole screen, but can be changed
     * using setMargins()
     */
187
    void scrollDown(int n);
Jekyll Wu's avatar
Jekyll Wu committed
188
189
    /**
     * Moves the cursor to the beginning of the current line.
190
191
     * Equivalent to setCursorX(0)
     */
192
    void toStartOfLine();
Jekyll Wu's avatar
Jekyll Wu committed
193
    /**
194
195
196
     * Moves the cursor one column to the left and erases the character
     * at the new cursor position.
     */
197
198
199
200
201
    void backspace();
    /** Moves the cursor @p n tab-stops to the right. */
    void tab(int n = 1);
    /** Moves the cursor @p n tab-stops to the left. */
    void backtab(int n);
202

203
    // Editing
204

Jekyll Wu's avatar
Jekyll Wu committed
205
206
    /**
     * Erase @p n characters beginning from the current cursor position.
207
208
     * This is equivalent to over-writing @p n characters starting with the current
     * cursor position with spaces.
Jekyll Wu's avatar
Jekyll Wu committed
209
     * If @p n is 0 then one character is erased.
210
     */
211
    void eraseChars(int n);
Jekyll Wu's avatar
Jekyll Wu committed
212
213
214
    /**
     * Delete @p n characters beginning from the current cursor position.
     * If @p n is 0 then one character is deleted.
215
     */
216
    void deleteChars(int n);
217
218
    /**
     * Insert @p n blank characters beginning from the current cursor position.
Jekyll Wu's avatar
Jekyll Wu committed
219
     * The position of the cursor is not altered.
220
221
     * If @p n is 0 then one character is inserted.
     */
222
    void insertChars(int n);
223
    /**
Kurt Hindenburg's avatar
Kurt Hindenburg committed
224
225
     * Repeat the preceding graphic character @p n times, including SPACE.
     * If @p n is 0 then the character is repeated once.
226
227
     */
    void repeatChars(int n);
Jekyll Wu's avatar
Jekyll Wu committed
228
    /**
229
230
231
232
     * Removes @p n lines beginning from the current cursor position.
     * The position of the cursor is not altered.
     * If @p n is 0 then one line is removed.
     */
233
    void deleteLines(int n);
234
235
236
237
238
    /**
     * Inserts @p lines beginning from the current cursor position.
     * The position of the cursor is not altered.
     * If @p n is 0 then one line is inserted.
     */
239
    void insertLines(int n);
240
    /** Clears all the tab stops. */
241
    void clearTabStops();
Jekyll Wu's avatar
Jekyll Wu committed
242
    /**  Sets or removes a tab stop at the cursor's current column. */
243
    void changeTabStop(bool set);
244

245
246
247
248
    /** Resets (clears) the specified screen @p m. */
    void resetMode(int m);
    /** Sets (enables) the specified screen @p m. */
    void setMode(int m);
Jekyll Wu's avatar
Jekyll Wu committed
249
    /**
250
     * Saves the state of the specified screen @p m.  It can be restored
251
252
     * using restoreMode()
     */
253
254
255
256
257
    void saveMode(int m);
    /** Restores the state of a screen @p m saved by calling saveMode() */
    void restoreMode(int m);
    /** Returns whether the specified screen @p me is enabled or not .*/
    bool getMode(int m) const;
258

Jekyll Wu's avatar
Jekyll Wu committed
259
260
261
262
    /**
     * Saves the current position and appearance (text color and style) of the cursor.
     * It can be restored by calling restoreCursor()
     */
263
    void saveCursor();
Albert Astals Cid's avatar
Albert Astals Cid committed
264
    /** Restores the position and appearance of the cursor.  See saveCursor() */
265
    void restoreCursor();
266

Jekyll Wu's avatar
Jekyll Wu committed
267
    /** Clear the whole screen, moving the current screen contents into the history first. */
268
    void clearEntireScreen();
Jekyll Wu's avatar
Jekyll Wu committed
269
270
    /**
     * Clear the area of the screen from the current cursor position to the end of
271
272
     * the screen.
     */
273
    void clearToEndOfScreen();
274
275
276
277
    /**
     * Clear the area of the screen from the current cursor position to the start
     * of the screen.
     */
278
    void clearToBeginOfScreen();
279
    /** Clears the whole of the line on which the cursor is currently positioned. */
280
    void clearEntireLine();
281
    /** Clears from the current cursor position to the end of the line. */
282
    void clearToEndOfLine();
283
    /** Clears from the current cursor position to the beginning of the line. */
284
    void clearToBeginOfLine();
285

286
    /** Fills the entire screen with the letter 'E' */
287
    void helpAlign();
288

Jekyll Wu's avatar
Jekyll Wu committed
289
290
    /**
     * Enables the given @p rendition flag.  Rendition flags control the appearance
291
292
293
     * of characters on the screen.
     *
     * @see Character::rendition
Jekyll Wu's avatar
Jekyll Wu committed
294
     */
295
    void setRendition(RenditionFlags rendition);
296
    /**
Albert Astals Cid's avatar
Albert Astals Cid committed
297
     * Disables the given @p rendition flag.  Rendition flags control the appearance
298
299
300
301
     * of characters on the screen.
     *
     * @see Character::rendition
     */
302
    void resetRendition(RenditionFlags rendition);
303

Jekyll Wu's avatar
Jekyll Wu committed
304
    /**
305
306
307
308
309
310
311
     * Sets the cursor's foreground color.
     * @param space The color space used by the @p color argument
     * @param color The new foreground color.  The meaning of this depends on
     * the color @p space used.
     *
     * @see CharacterColor
     */
312
    void setForeColor(int space, int color);
313
314
    /**
     * Sets the cursor's background color.
315
     * @param space The color space used by the @p color argument.
316
317
318
319
320
     * @param color The new background color.  The meaning of this depends on
     * the color @p space used.
     *
     * @see CharacterColor
     */
321
    void setBackColor(int space, int color);
Jekyll Wu's avatar
Jekyll Wu committed
322
323
    /**
     * Resets the cursor's color back to the default and sets the
324
325
     * character's rendition flags back to the default settings.
     */
326
    void setDefaultRendition();
327

328
    /** Returns the column which the cursor is positioned at. */
329
    int  getCursorX() const;
330
    /** Returns the line which the cursor is positioned on. */
331
    int  getCursorY() const;
332

333
334
335
336
337
338
339
340
341
342
343
344
345
346
    /**
     * Resets the state of the screen.  This resets the various screen modes
     * back to their default states.  The cursor style and colors are reset
     * (as if setDefaultRendition() had been called)
     *
     * <ul>
     * <li>Line wrapping is enabled.</li>
     * <li>Origin mode is disabled.</li>
     * <li>Insert mode is disabled.</li>
     * <li>Cursor mode is enabled.  TODO Document me</li>
     * <li>Screen mode is disabled. TODO Document me</li>
     * <li>New line mode is disabled.  TODO Document me</li>
     * </ul>
     *
Jekyll Wu's avatar
Jekyll Wu committed
347
     * If @p clearScreen is true then the screen contents are erased entirely,
348
349
     * otherwise they are unaltered.
     */
350
    void reset();
351

Jekyll Wu's avatar
Jekyll Wu committed
352
353
354
    /**
     * Displays a new character at the current cursor position.
     *
355
     * If the cursor is currently positioned at the right-edge of the screen and
Jekyll Wu's avatar
Jekyll Wu committed
356
     * line wrapping is enabled then the character is added at the start of a new
357
358
     * line below the current one.
     *
Jekyll Wu's avatar
Jekyll Wu committed
359
360
361
362
     * If the MODE_Insert screen mode is currently enabled then the character
     * is inserted at the current cursor position, otherwise it will replace the
     * character already at the current cursor position.
     */
363
    void displayCharacter(uint c);
364

Jekyll Wu's avatar
Jekyll Wu committed
365
366
    /**
     * Resizes the image to a new fixed size of @p new_lines by @p new_columns.
367
368
     * In the case that @p new_columns is smaller than the current number of columns,
     * existing lines are not truncated.  This prevents characters from being lost
369
     * if the terminal display is resized smaller and then larger again.
370
     *
Jekyll Wu's avatar
Jekyll Wu committed
371
     * The top and bottom margins are reset to the top and bottom of the new
372
373
     * screen size.  Tab stops are also reset and the current selection is
     * cleared.
374
     */
375
    void resizeImage(int new_lines, int new_columns);
376

377
    /**
Jekyll Wu's avatar
Jekyll Wu committed
378
     * Returns the current screen image.
379
380
     * The result is an array of Characters of size [getLines()][getColumns()] which
     * must be freed by the caller after use.
Robert Knight's avatar
   
Robert Knight committed
381
382
383
384
385
     *
     * @param dest Buffer to copy the characters into
     * @param size Size of @p dest in Characters
     * @param startLine Index of first line to copy
     * @param endLine Index of last line to copy
386
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
387
    void getImage(Character *dest, int size, int startLine, int endLine) const;
388

Jekyll Wu's avatar
Jekyll Wu committed
389
    /**
390
     * Returns the additional attributes associated with lines in the image.
Jekyll Wu's avatar
Jekyll Wu committed
391
     * The most important attribute is LINE_WRAPPED which specifies that the
Robert Knight's avatar
   
Robert Knight committed
392
     * line is wrapped,
393
394
     * other attributes control the size of characters in the line.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
395
    QVector<LineProperty> getLineProperties(int startLine, int endLine) const;
396

397
    /** Return the number of lines. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
398
399
    int getLines() const
    {
Jekyll Wu's avatar
Jekyll Wu committed
400
        return _lines;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
401
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
402

403
    /** Return the number of columns. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
404
405
    int getColumns() const
    {
Jekyll Wu's avatar
Jekyll Wu committed
406
        return _columns;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
407
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
408

409
    /** Return the number of lines in the history buffer. */
410
    int getHistLines() const;
Jekyll Wu's avatar
Jekyll Wu committed
411
412
413
    /**
     * Sets the type of storage used to keep lines in the history.
     * If @p copyPreviousScroll is true then the contents of the previous
414
415
     * history buffer are copied into the new scroll.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
416
    void setScroll(const HistoryType &, bool copyPreviousScroll = true);
417
    /** Returns the type of storage used to keep lines in the history. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
418
    const HistoryType &getScroll() const;
Jekyll Wu's avatar
Jekyll Wu committed
419
    /**
420
421
422
     * Returns true if this screen keeps lines that are scrolled off the screen
     * in a history buffer.
     */
423
    bool hasScroll() const;
424

Jekyll Wu's avatar
Jekyll Wu committed
425
    /**
426
427
     * Sets the start of the selection.
     *
428
     * @param x The column index of the first character in the selection.
429
     * @param y The line index of the first character in the selection.
430
     * @param blockSelectionMode True if the selection is in column mode.
431
     */
432
    void setSelectionStart(const int x, const int y, const bool blockSelectionMode);
433

434
    /**
435
     * Sets the end of the current selection.
436
     *
437
438
     * @param x The column index of the last character in the selection.
     * @param y The line index of the last character in the selection.
Jekyll Wu's avatar
Jekyll Wu committed
439
     */
440
    void setSelectionEnd(const int x, const int y);
441

442
443
444
445
    /**
     * Retrieves the start of the selection or the cursor position if there
     * is no selection.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
446
    void getSelectionStart(int &column, int &line) const;
447

448
449
450
451
    /**
     * Retrieves the end of the selection or the cursor position if there
     * is no selection.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
452
    void getSelectionEnd(int &column, int &line) const;
453

454
    /** Clears the current selection */
455
    void clearSelection();
456

Jekyll Wu's avatar
Jekyll Wu committed
457
    /**
458
      *  Returns true if the character at (@p x, @p y) is part of the
Jekyll Wu's avatar
Jekyll Wu committed
459
460
      *  current selection.
      */
461
    bool isSelected(const int x, const int y) const;
462

Jekyll Wu's avatar
Jekyll Wu committed
463
464
    /**
     * Convenience method.  Returns the currently selected text.
465
     * @param options See Screen::DecodingOptions
466
     */
467
    QString selectedText(const DecodingOptions options) const;
468

469
    /**
470
471
472
     * Convenience method.  Returns the text between two indices.
     * @param startIndex Specifies the starting text index
     * @param endIndex Specifies the ending text index
473
     * @param options See Screen::DecodingOptions
474
     */
475
    QString text(int startIndex, int endIndex, const DecodingOptions options) const;
476

477
478
479
    /**
     * Copies part of the output to a stream.
     *
480
     * @param decoder A decoder which converts terminal characters into text
481
482
     * @param fromLine The first line in the history to retrieve
     * @param toLine The last line in the history to retrieve
483
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
484
    void writeLinesToStream(TerminalCharacterDecoder *decoder, int fromLine, int toLine) const;
485

486
487
488
489
490
491
492
493
494
495
    /**
     * Checks if the text between from and to is inside the current
     * selection. If this is the case, the selection is cleared. The
     * from and to are coordinates in the current viewable window.
     * The loc(x,y) macro can be used to generate these values from a
     * column,line pair.
     *
     * @param from The start of the area to check.
     * @param to The end of the area to check
     */
496
497
    void checkSelection(int from, int to);

Jekyll Wu's avatar
Jekyll Wu committed
498
    /**
499
     * Sets or clears an attribute of the current line.
Jekyll Wu's avatar
Jekyll Wu committed
500
     *
501
502
503
504
     * @param property The attribute to set or clear
     * Possible properties are:
     * LINE_WRAPPED:     Specifies that the line is wrapped.
     * LINE_DOUBLEWIDTH: Specifies that the characters in the current line
505
     *                   should be double the normal width.
Jekyll Wu's avatar
Jekyll Wu committed
506
     * LINE_DOUBLEHEIGHT:Specifies that the characters in the current line
507
     *                   should be double the normal height.
508
     *                   Double-height lines are formed of two lines containing the same characters,
509
510
511
     *                   with both having the LINE_DOUBLEHEIGHT attribute.
     *                   This allows other parts of the code to work on the
     *                   assumption that all lines are the same height.
512
513
514
     *
     * @param enable true to apply the attribute to the current line or false to remove it
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
515
    void setLineProperty(LineProperty property, bool enable);
516

Jekyll Wu's avatar
Jekyll Wu committed
517
    /**
518
     * Returns the number of lines that the image has been scrolled up or down by,
Robert Knight's avatar
   
Robert Knight committed
519
520
     * since the last call to resetScrolledLines().
     *
521
     * a positive return value indicates that the image has been scrolled up,
Jekyll Wu's avatar
Jekyll Wu committed
522
     * a negative return value indicates that the image has been scrolled down.
523
524
     */
    int scrolledLines() const;
525
526
527
528

    /**
     * Returns the region of the image which was last scrolled.
     *
Jekyll Wu's avatar
Jekyll Wu committed
529
     * This is the area of the image from the top margin to the
530
531
532
533
     * bottom margin when the last scroll occurred.
     */
    QRect lastScrolledRegion() const;

Jekyll Wu's avatar
Jekyll Wu committed
534
    /**
535
536
537
538
539
     * Resets the count of the number of lines that the image has been scrolled up or down by,
     * see scrolledLines()
     */
    void resetScrolledLines();

540
541
542
543
544
545
546
    /**
     * Returns the number of lines of output which have been
     * dropped from the history since the last call
     * to resetDroppedLines()
     *
     * If the history is not unlimited then it will drop
     * the oldest lines of output if new lines are added when
Jekyll Wu's avatar
Jekyll Wu committed
547
     * it is full.
548
549
550
551
552
553
554
555
556
     */
    int droppedLines() const;

    /**
     * Resets the count of the number of lines dropped from
     * the history.
     */
    void resetDroppedLines();

Jekyll Wu's avatar
Jekyll Wu committed
557
    /**
558
559
560
      * Fills the buffer @p dest with @p count instances of the default (ie. blank)
      * Character style.
      */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
561
    static void fillWithDefaultChar(Character *dest, int count);
Robert Knight's avatar
   
Robert Knight committed
562

Kurt Hindenburg's avatar
Kurt Hindenburg committed
563
564
    void setCurrentTerminalDisplay(TerminalDisplay *display)
    {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
565
        _currentTerminalDisplay = display;
566
567
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
568
569
    TerminalDisplay *currentTerminalDisplay()
    {
570
571
        return _currentTerminalDisplay;
    }
572

573
    QSet<uint> usedExtendedChars() const
Kurt Hindenburg's avatar
Kurt Hindenburg committed
574
    {
575
        QSet<uint> result;
Jekyll Wu's avatar
Jekyll Wu committed
576
        for (int i = 0; i < _lines; ++i) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
577
            const ImageLine &il = _screenLines[i];
578
            for (int j = 0; j < il.length(); ++j) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
579
                if (il[j].rendition & RE_EXTENDED_CHAR) {
580
581
582
583
584
585
                    result << il[j].character;
                }
            }
        }
        return result;
    }
586

Jekyll Wu's avatar
Jekyll Wu committed
587
    static const Character DefaultChar;
588

Jekyll Wu's avatar
Jekyll Wu committed
589
590
private:
    //copies a line of text from the screen or history into a stream using a
591
592
    //specified character decoder.  Returns the number of lines actually copied,
    //which may be less than 'count' if (start+count) is more than the number of characters on
Jekyll Wu's avatar
Jekyll Wu committed
593
    //the line
594
    //
Jekyll Wu's avatar
Jekyll Wu committed
595
    //line - the line number to copy, from 0 (the earliest line in the history) up to
596
    //         history->getLines() + lines - 1
597
598
    //start - the first column on the line to copy
    //count - the number of characters on the line to copy
599
    //decoder - a decoder which converts terminal characters (an Character array) into text
600
    //appendNewLine - if true a new line character (\n) is appended to the end of the line
Kurt Hindenburg's avatar
Kurt Hindenburg committed
601
    int  copyLineToStream(int line, int start, int count, TerminalCharacterDecoder *decoder,
602
                          bool appendNewLine, const DecodingOptions options) const;
603

604
605
606
    //fills a section of the screen image with the character 'c'
    //the parameters are specified as offsets from the start of the screen image.
    //the loc(x,y) macro can be used to generate these values from a column,line pair.
607
    void clearImage(int loca, int loce, char c);
608
609
610
611

    //move screen image between 'sourceBegin' and 'sourceEnd' to 'dest'.
    //the parameters are specified as offsets from the start of the screen image.
    //the loc(x,y) macro can be used to generate these values from a column,line pair.
612
613
    //
    //NOTE: moveImage() can only move whole lines
614
    void moveImage(int dest, int sourceBegin, int sourceEnd);
615
616
617
618
    // scroll up 'n' lines in current region, clearing the bottom 'n' lines
    void scrollUp(int from, int n);
    // scroll down 'n' lines in current region, clearing the top 'n' lines
    void scrollDown(int from, int n);
619

620
    //when we handle scroll commands, we need to know which screenwindow will scroll
Kurt Hindenburg's avatar
Kurt Hindenburg committed
621
    TerminalDisplay *_currentTerminalDisplay;
622

623
624
625
626
    void addHistLine();

    void initTabStops();

627
    void updateEffectiveRendition();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
628
    void reverseRendition(Character &p) const;
629

630
    bool isSelectionValid() const;
631
632
    // copies text from 'startIndex' to 'endIndex' to a stream
    // startIndex and endIndex are positions generated using the loc(x,y) macro
Kurt Hindenburg's avatar
Kurt Hindenburg committed
633
    void writeToStream(TerminalCharacterDecoder *decoder, int startIndex, int endIndex,
634
                       const DecodingOptions options) const;
635
636
    // copies 'count' lines from the screen buffer into 'dest',
    // starting from 'startLine', where 0 is the first line in the screen buffer
Kurt Hindenburg's avatar
Kurt Hindenburg committed
637
    void copyFromScreen(Character *dest, int startLine, int count) const;
638
639
    // copies 'count' lines from the history buffer into 'dest',
    // starting from 'startLine', where 0 is the first line in the history
Kurt Hindenburg's avatar
Kurt Hindenburg committed
640
    void copyFromHistory(Character *dest, int startLine, int count) const;
Robert Knight's avatar
   
Robert Knight committed
641

642
    // screen image ----------------
Jekyll Wu's avatar
Jekyll Wu committed
643
644
    int _lines;
    int _columns;
645

646
    typedef QVector<Character> ImageLine;      // [0..columns]
Kurt Hindenburg's avatar
Kurt Hindenburg committed
647
    ImageLine *_screenLines;             // [lines]
648
    int _screenLinesSize;                // _screenLines.size()
649

650
    int _scrolledLines;
651
    QRect _lastScrolledRegion;
652

653
654
    int _droppedLines;

Jekyll Wu's avatar
Jekyll Wu committed
655
    QVarLengthArray<LineProperty, 64> _lineProperties;
656

657
    // history buffer ---------------
Kurt Hindenburg's avatar
Kurt Hindenburg committed
658
    HistoryScroll *_history;
659

660
    // cursor location
Jekyll Wu's avatar
Jekyll Wu committed
661
662
    int _cuX;
    int _cuY;
663
664

    // cursor color and rendition info
Jekyll Wu's avatar
Jekyll Wu committed
665
666
    CharacterColor _currentForeground;
    CharacterColor _currentBackground;
667
    RenditionFlags _currentRendition;
668
669

    // margins ----------------
670
671
    int _topMargin;
    int _bottomMargin;
672
673

    // states ----------------
Jekyll Wu's avatar
Jekyll Wu committed
674
675
    int _currentModes[MODES_SCREEN];
    int _savedModes[MODES_SCREEN];
676
677
678

    // ----------------------------

Jekyll Wu's avatar
Jekyll Wu committed
679
    QBitArray _tabStops;
680
681

    // selection -------------------
Jekyll Wu's avatar
Jekyll Wu committed
682
683
684
685
    int _selBegin; // The first location selected.
    int _selTopLeft;    // TopLeft Location.
    int _selBottomRight;    // Bottom Right Location.
    bool _blockSelectionMode;  // Column selection mode
686
687

    // effective colors and rendition ------------
Jekyll Wu's avatar
Jekyll Wu committed
688
689
    CharacterColor _effectiveForeground; // These are derived from
    CharacterColor _effectiveBackground; // the cu_* variables above
690
    RenditionFlags _effectiveRendition;  // to speed up operation
691

Kurt Hindenburg's avatar
Kurt Hindenburg committed
692
    class SavedState
693
694
    {
    public:
Kurt Hindenburg's avatar
Kurt Hindenburg committed
695
696
697
698
699
700
701
702
        SavedState() :
            cursorColumn(0),
            cursorLine(0),
            rendition(0),
            foreground(CharacterColor()),
            background(CharacterColor())
        {
        }
703
704
705

        int cursorColumn;
        int cursorLine;
706
        RenditionFlags rendition;
707
708
709
        CharacterColor foreground;
        CharacterColor background;
    };
Jekyll Wu's avatar
Jekyll Wu committed
710
    SavedState _savedState;
711

712
    // last position where we added a character
Jekyll Wu's avatar
Jekyll Wu committed
713
    int _lastPos;
714
715

    // used in REP (repeating char)
716
    quint32 _lastDrawnChar;
717
};
718

719
720
Q_DECLARE_OPERATORS_FOR_FLAGS(Screen::DecodingOptions)

Stephan Binner's avatar
Stephan Binner committed
721
}
722

723
724


725
#endif // SCREEN_H