summaryrefslogtreecommitdiffstats
path: root/client/browser/qtermwidget/src/Screen.h
blob: a4ad199bdf0fa3b86afa0f7e3e13ec32de510813 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
    This file is part of Konsole, KDE's terminal.

    Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
    Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>

    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008

    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.
*/

#ifndef SCREEN_H
#define SCREEN_H

// Qt
#include <QtCore/QRect>
#include <QtCore/QTextStream>
#include <QtCore/QVarLengthArray>

// Konsole
#include "Character.h"
#include "History.h"

#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

namespace Konsole
{

/*!
*/
struct ScreenParm
{
  int mode[MODES_SCREEN];
};

class TerminalCharacterDecoder;

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

    The terminal emulation ( Emulation ) receives a serial stream of
    characters from the program currently running in the terminal.
    From this stream it creates an image of characters which is ultimately
    rendered by the display widget ( TerminalDisplay ).  Some types of emulation
    may have more than one screen image. 

    getImage() is used to retrieve the currently visible image
    which is then used by the display widget to draw the output from the
    terminal. 

    The number of lines of output history which are kept in addition to the current
    screen image depends on the history scroll being used to store the output.  
    The scroll is specified using setScroll()
    The output history can be retrieved using writeToStream()

    The screen image has a selection associated with it, specified using 
    setSelectionStart() and setSelectionEnd().  The selected text can be retrieved
    using selectedText().  When getImage() is used to retrieve the the visible image,
    characters which are part of the selection have their colours inverted.   
*/
class Screen
{
public:
    /** Construct a new screen image of size @p lines by @p columns. */
    Screen(int lines, int columns);
    ~Screen();

    // VT100/2 Operations 
    // Cursor Movement
    
    /** Move the cursor up by @p n lines. */
    void cursorUp    (int n);
    /** Move the cursor down by @p n lines. */
    void cursorDown  (int n);
    /** Move the cursor to the left by @p n columns. */
    void cursorLeft  (int n);
    /** Move the cursor to the right by @p n columns. */
    void cursorRight (int n);
    /** Position the cursor on line @p y. */
    void setCursorY  (int y);
    /** Position the cursor at column @p x. */
    void setCursorX  (int x);
    /** Position the cursor at line @p y, column @p x. */
    void setCursorYX (int y, int x);
    /**
     * Sets the margins for scrolling the screen.
     *
     * @param topLine The top line of the new scrolling margin. 
     * @param bottomLine The bottom line of the new scrolling margin. 
     */
    void setMargins  (int topLine , int bottomLine);
    /** Returns the top line of the scrolling region. */ 
    int topMargin() const;
    /** Returns the bottom line of the scrolling region. */
    int bottomMargin() const;

    /** 
     * Resets the scrolling margins back to the top and bottom lines
     * of the screen.
     */
    void setDefaultMargins();
    
    /** 
     * Moves the cursor down one line, if the MODE_NewLine mode 
     * flag is enabled then the cursor is returned to the leftmost
     * column first.
     *
     * Equivalent to NextLine() if the MODE_NewLine flag is set
     * or index() otherwise. 
     */
    void NewLine     ();
    /**
     * Moves the cursor down one line and positions it at the beginning
     * of the line.
     */
    void NextLine    ();

    /** 
     * 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.
     */
    void index       ();
    /**
     * 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.
     */
    void reverseIndex();
    
    /** 
     * Scroll the scrolling region of the screen up by @p n lines. 
     * The scrolling region is initially the whole screen, but can be changed 
     * using setMargins()
     */ 
    void scrollUp(int n);
    /**
     * 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()
     */
    void scrollDown(int n);
    
    /** 
     * Moves the cursor to the beginning of the current line. 
     * Equivalent to setCursorX(0)
     */
    void Return      ();
    /** 
     * Moves the cursor one column to the left and erases the character
     * at the new cursor position.
     */
    void BackSpace   ();
    /** 
     * Moves the cursor @p n tab-stops to the right.
     */
    void Tabulate    (int n = 1);
    /** 
     * Moves the cursor @p n tab-stops to the left. 
     */
    void backTabulate(int n);
    
    // Editing
    
    /** 
     * Erase @p n characters beginning from the current cursor position. 
     * This is equivalent to over-writing @p n characters starting with the current
     * cursor position with spaces.
     * If @p n is 0 then one character is erased. 
     */
    void eraseChars  (int n);
    /** 
     * Delete @p n characters beginning from the current cursor position. 
     * If @p n is 0 then one character is deleted. 
     */
    void deleteChars (int n);
    /**
     * Insert @p n blank characters beginning from the current cursor position.
     * The position of the cursor is not altered.  
     * If @p n is 0 then one character is inserted.
     */
    void insertChars (int n);
    /** 
     * 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.
     */
    void deleteLines (int n);
    /**
     * 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.
     */
    void insertLines (int n);
    /** Clears all the tab stops. */
    void clearTabStops();
    /**  Sets or removes a tab stop at the cursor's current column. */ 
    void changeTabStop(bool set);
   
    /** Resets (clears) the specified screen @p mode. */
    void resetMode   (int mode);
    /** Sets (enables) the specified screen @p mode. */
    void setMode     (int mode);
    /** 
     * Saves the state of the specified screen @p mode.  It can be restored
     * using restoreMode()
     */
    void saveMode    (int mode);
    /** Restores the state of a screen @p mode saved by calling saveMode() */
    void restoreMode (int mode);
    /** Returns whether the specified screen @p mode is enabled or not .*/
    bool getMode     (int mode) const;
   
    /** 
     * Saves the current position and appearence (text color and style) of the cursor. 
     * It can be restored by calling restoreCursor() 
     */ 
    void saveCursor  ();
    /** Restores the position and appearence of the cursor.  See saveCursor() */
    void restoreCursor();
   
    /** Clear the whole screen, moving the current screen contents into the history first. */ 
    void clearEntireScreen();
    /** 
     * Clear the area of the screen from the current cursor position to the end of 
     * the screen.
     */
    void clearToEndOfScreen();
    /**
     * Clear the area of the screen from the current cursor position to the start
     * of the screen.
     */
    void clearToBeginOfScreen();
    /** Clears the whole of the line on which the cursor is currently positioned. */
    void clearEntireLine();
    /** Clears from the current cursor position to the end of the line. */
    void clearToEndOfLine();
    /** Clears from the current cursor position to the beginning of the line. */
    void clearToBeginOfLine();
    
    /** Fills the entire screen with the letter 'E' */
    void helpAlign   ();
       
    /** 
     * Enables the given @p rendition flag.  Rendition flags control the appearence 
     * of characters on the screen.
     *
     * @see Character::rendition
     */  
    void setRendition  (int rendition);
    /**
     * Disables the given @p rendition flag.  Rendition flags control the appearence
     * of characters on the screen.
     *
     * @see Character::rendition
     */
    void resetRendition(int rendition);
    
    /** 
     * 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
     */
    void setForeColor  (int space, int color);
    /**
     * Sets the cursor's background color.
     * @param space The color space used by the @p color argumnet.
     * @param color The new background color.  The meaning of this depends on
     * the color @p space used.
     *
     * @see CharacterColor
     */
    void setBackColor  (int space, int color);
    /** 
     * Resets the cursor's color back to the default and sets the 
     * character's rendition flags back to the default settings.
     */
    void setDefaultRendition();
    
    /** Returns the column which the cursor is positioned at. */
    int  getCursorX() const;
    /** Returns the line which the cursor is positioned on. */
    int  getCursorY() const;
   
	/** TODO Document me */ 
	void clear();
    /** 
     * Sets the position of the cursor to the 'home' position at the top-left
     * corner of the screen (0,0) 
     */
    void home();
    /**
     * 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>
     *
     * If @p clearScreen is true then the screen contents are erased entirely, 
     * otherwise they are unaltered.
     */
    void reset(bool clearScreen = true);
   
    /** 
     * Displays a new character at the current cursor position. 
     * 
     * If the cursor is currently positioned at the right-edge of the screen and
     * line wrapping is enabled then the character is added at the start of a new 
     * line below the current one.
     *
     * 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.  
     */ 
    void ShowCharacter(unsigned short c);
    
    // Do composition with last shown character FIXME: Not implemented yet for KDE 4
    void compose(const QString& compose);
    
    /** 
     * Resizes the image to a new fixed size of @p new_lines by @p new_columns.  
     * 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
     * if the terminal display is resized smaller and then larger again.
     *
     * (note that in versions of Konsole prior to KDE 4, existing lines were
     *  truncated when making the screen image smaller)
     */
    void resizeImage(int new_lines, int new_columns);
    
    /**
     * Returns the current screen image.  
     * The result is an array of Characters of size [getLines()][getColumns()] which
     * must be freed by the caller after use.
     *
     * @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
     */
    void getImage( Character* dest , int size , int startLine , int endLine ) const;

    /** 
     * Returns the additional attributes associated with lines in the image.
     * The most important attribute is LINE_WRAPPED which specifies that the 
     * line is wrapped,
     * other attributes control the size of characters in the line.
     */
    QVector<LineProperty> getLineProperties( int startLine , int endLine ) const;
	

    /** Return the number of lines. */
    int  getLines()   { return lines; }
    /** Return the number of columns. */
    int  getColumns() { return columns; }
    /** Return the number of lines in the history buffer. */
    int  getHistLines ();
    /** 
     * Sets the type of storage used to keep lines in the history. 
     * If @p copyPreviousScroll is true then the contents of the previous 
     * history buffer are copied into the new scroll.
     */
    void setScroll(const HistoryType& , bool copyPreviousScroll = true);
    /** Returns the type of storage used to keep lines in the history. */
    const HistoryType& getScroll();
    /** 
     * Returns true if this screen keeps lines that are scrolled off the screen
     * in a history buffer.
     */
    bool hasScroll();

    /** 
     * Sets the start of the selection.
     *
     * @param column The column index of the first character in the selection.
     * @param line The line index of the first character in the selection.
     * @param columnmode True if the selection is in column mode.
     */
    void setSelectionStart(const int column, const int line, const bool columnmode);
    
    /**
     * Sets the end of the current selection.
     *
     * @param column The column index of the last character in the selection.
     * @param line The line index of the last character in the selection. 
     */ 
    void setSelectionEnd(const int column, const int line);
   
    /**
     * Retrieves the start of the selection or the cursor position if there
     * is no selection.
     */
    void getSelectionStart(int& column , int& line);
    
    /**
     * Retrieves the end of the selection or the cursor position if there
     * is no selection.
     */
    void getSelectionEnd(int& column , int& line);

    /** Clears the current selection */
    void clearSelection();

    void setBusySelecting(bool busy) { sel_busy = busy; }

    /** 
 	 * 	Returns true if the character at (@p column, @p line) is part of the
 	 *  current selection. 
 	 */ 
    bool isSelected(const int column,const int line) const;

    /** 
     * Convenience method.  Returns the currently selected text. 
     * @param preserveLineBreaks Specifies whether new line characters should 
     * be inserted into the returned text at the end of each terminal line.
     */
    QString selectedText(bool preserveLineBreaks);
	    
	/**
	 * Copies part of the output to a stream.
	 *
	 * @param decoder A decoder which coverts terminal characters into text
	 * @param from The first line in the history to retrieve
	 * @param to The last line in the history to retrieve
	 */
	void writeToStream(TerminalCharacterDecoder* decoder, int from, int to);

    /** 
     * Sets the selection to line @p no in the history and returns
     * the text of that line from the history buffer.
     */
    QString getHistoryLine(int no);

	/**
	 * Copies the selected characters, set using @see setSelBeginXY and @see setSelExtentXY
	 * into a stream.
	 *
	 * @param decoder A decoder which converts terminal characters into text.  
	 * PlainTextDecoder is the most commonly used decoder which coverts characters 
	 * into plain text with no formatting.
     * @param preserveLineBreaks Specifies whether new line characters should 
     * be inserted into the returned text at the end of each terminal line. 
	 */
	void writeSelectionToStream(TerminalCharacterDecoder* decoder , bool
                                preserveLineBreaks = true);

    /** TODO Document me */
    void checkSelection(int from, int to);

	/** 
	 * Sets or clears an attribute of the current line.
	 * 
	 * @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 should be double the normal width.
	 * LINE_DOUBLEHEIGHT:Specifies that the characters in the current line should be double the normal height.
     *                   Double-height lines are formed of two lines containing the same characters,
     *                   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.
	 *
	 * @param enable true to apply the attribute to the current line or false to remove it
	 */
	void setLineProperty(LineProperty property , bool enable);


    /** 
     * Returns the number of lines that the image has been scrolled up or down by,
     * since the last call to resetScrolledLines().
     *
     * a positive return value indicates that the image has been scrolled up,
     * a negative return value indicates that the image has been scrolled down. 
     */
    int scrolledLines() const;

    /**
     * Returns the region of the image which was last scrolled.
     *
     * This is the area of the image from the top margin to the 
     * bottom margin when the last scroll occurred.
     */
    QRect lastScrolledRegion() const;

    /** 
     * Resets the count of the number of lines that the image has been scrolled up or down by,
     * see scrolledLines()
     */
    void resetScrolledLines();

    /**
     * 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
     * it is full.  
     */
    int droppedLines() const;

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

	/** 
 	 * Fills the buffer @p dest with @p count instances of the default (ie. blank)
 	 * Character style.
 	 */
	static void fillWithDefaultChar(Character* dest, int count);

private: 

	//copies a line of text from the screen or history into a stream using a 
	//specified character decoder
	//line - the line number to copy, from 0 (the earliest line in the history) up to 
	//		 hist->getLines() + lines - 1
	//start - the first column on the line to copy
	//count - the number of characters on the line to copy
	//decoder - a decoder which coverts terminal characters (an Character array) into text
    //appendNewLine - if true a new line character (\n) is appended to the end of the line
	void copyLineToStream(int line, 
                          int start, 
                          int count, 
                          TerminalCharacterDecoder* decoder,
                          bool appendNewLine,
                          bool preserveLineBreaks);
	
    //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.
    void clearImage(int loca, int loce, char c);

    //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.
    void moveImage(int dest, int sourceBegin, int sourceEnd);
    
    void scrollUp(int from, int i);
    void scrollDown(int from, int i);

    void addHistLine();

    void initTabStops();

    void effectiveRendition();
    void reverseRendition(Character& p) const;

    bool isSelectionValid() const;

	// copies 'count' lines from the screen buffer into 'dest',
	// starting from 'startLine', where 0 is the first line in the screen buffer
	void copyFromScreen(Character* dest, int startLine, int count) const;
	// copies 'count' lines from the history buffer into 'dest',
	// starting from 'startLine', where 0 is the first line in the history
	void copyFromHistory(Character* dest, int startLine, int count) const;


    // screen image ----------------
    int lines;
    int columns;

    typedef QVector<Character> ImageLine;      // [0..columns]
    ImageLine*          screenLines;    // [lines]

    int _scrolledLines;
    QRect _lastScrolledRegion;

    int _droppedLines;

    QVarLengthArray<LineProperty,64> lineProperties;    
	
    // history buffer ---------------
    HistoryScroll *hist;
    
    // cursor location
    int cuX;
    int cuY;

    // cursor color and rendition info
    CharacterColor cu_fg;      // foreground
    CharacterColor cu_bg;      // background
    quint8 cu_re;      // rendition

    // margins ----------------
    int tmargin;      // top margin
    int bmargin;      // bottom margin

    // states ----------------
    ScreenParm currParm;

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

    bool* tabstops;

    // selection -------------------
    int sel_begin; // The first location selected.
    int sel_TL;    // TopLeft Location.
    int sel_BR;    // Bottom Right Location.
    bool sel_busy; // Busy making a selection.
    bool columnmode;  // Column selection mode

    // effective colors and rendition ------------
    CharacterColor ef_fg;      // These are derived from
    CharacterColor ef_bg;      // the cu_* variables above
    quint8 ef_re;      // to speed up operation

    //
    // save cursor, rendition & states ------------
    // 

    // cursor location
    int sa_cuX;
    int sa_cuY;

    // rendition info
    quint8 sa_cu_re;
    CharacterColor sa_cu_fg;
    CharacterColor sa_cu_bg;
    
    // last position where we added a character
    int lastPos;

    // modes
    ScreenParm saveParm;

    static Character defaultChar;
};

}

#endif // SCREEN_H