source: ogBrowser-Git/qtermwidget/src/Screen.h

qndtest
Last change on this file was 050d67a, checked in by adelcastillo <adelcastillo@…>, 15 years ago

Ahora el browser tiene consola en vez del output.
Pasado todo el sistema de compilacion a cmake.

git-svn-id: https://opengnsys.es/svn/trunk@408 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 22.4 KB
Line 
1/*
2    This file is part of Konsole, KDE's terminal.
3
4    Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
5    Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6
7    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
8
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.
13
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.
18
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
22    02110-1301  USA.
23*/
24
25#ifndef SCREEN_H
26#define SCREEN_H
27
28// Qt
29#include <QtCore/QRect>
30#include <QtCore/QTextStream>
31#include <QtCore/QVarLengthArray>
32
33// Konsole
34#include "Character.h"
35#include "History.h"
36
37#define MODE_Origin    0
38#define MODE_Wrap      1
39#define MODE_Insert    2
40#define MODE_Screen    3
41#define MODE_Cursor    4
42#define MODE_NewLine   5
43#define MODES_SCREEN   6
44
45namespace Konsole
46{
47
48/*!
49*/
50struct ScreenParm
51{
52  int mode[MODES_SCREEN];
53};
54
55class TerminalCharacterDecoder;
56
57/**
58    \brief An image of characters with associated attributes.
59
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.
65
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
68    terminal.
69
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()
74
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.   
79*/
80class Screen
81{
82public:
83    /** Construct a new screen image of size @p lines by @p columns. */
84    Screen(int lines, int columns);
85    ~Screen();
86
87    // VT100/2 Operations
88    // Cursor Movement
89   
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);
104    /**
105     * Sets the margins for scrolling the screen.
106     *
107     * @param topLine The top line of the new scrolling margin.
108     * @param bottomLine The bottom line of the new scrolling margin.
109     */
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;
115
116    /**
117     * Resets the scrolling margins back to the top and bottom lines
118     * of the screen.
119     */
120    void setDefaultMargins();
121   
122    /**
123     * Moves the cursor down one line, if the MODE_NewLine mode
124     * flag is enabled then the cursor is returned to the leftmost
125     * column first.
126     *
127     * Equivalent to NextLine() if the MODE_NewLine flag is set
128     * or index() otherwise.
129     */
130    void NewLine     ();
131    /**
132     * Moves the cursor down one line and positions it at the beginning
133     * of the line.
134     */
135    void NextLine    ();
136
137    /**
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.
141     */
142    void index       ();
143    /**
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.
147     */
148    void reverseIndex();
149   
150    /**
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
153     * using setMargins()
154     */ 
155    void scrollUp(int n);
156    /**
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
159     * using setMargins()
160     */
161    void scrollDown(int n);
162   
163    /**
164     * Moves the cursor to the beginning of the current line.
165     * Equivalent to setCursorX(0)
166     */
167    void Return      ();
168    /**
169     * Moves the cursor one column to the left and erases the character
170     * at the new cursor position.
171     */
172    void BackSpace   ();
173    /**
174     * Moves the cursor @p n tab-stops to the right.
175     */
176    void Tabulate    (int n = 1);
177    /**
178     * Moves the cursor @p n tab-stops to the left.
179     */
180    void backTabulate(int n);
181   
182    // Editing
183   
184    /**
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.
189     */
190    void eraseChars  (int n);
191    /**
192     * Delete @p n characters beginning from the current cursor position.
193     * If @p n is 0 then one character is deleted.
194     */
195    void deleteChars (int n);
196    /**
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.
200     */
201    void insertChars (int n);
202    /**
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.
206     */
207    void deleteLines (int n);
208    /**
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.
212     */
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);
218   
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);
223    /**
224     * Saves the state of the specified screen @p mode.  It can be restored
225     * using restoreMode()
226     */
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;
232   
233    /**
234     * Saves the current position and appearence (text color and style) of the cursor.
235     * It can be restored by calling restoreCursor()
236     */ 
237    void saveCursor  ();
238    /** Restores the position and appearence of the cursor.  See saveCursor() */
239    void restoreCursor();
240   
241    /** Clear the whole screen, moving the current screen contents into the history first. */ 
242    void clearEntireScreen();
243    /**
244     * Clear the area of the screen from the current cursor position to the end of
245     * the screen.
246     */
247    void clearToEndOfScreen();
248    /**
249     * Clear the area of the screen from the current cursor position to the start
250     * of the screen.
251     */
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();
259   
260    /** Fills the entire screen with the letter 'E' */
261    void helpAlign   ();
262       
263    /**
264     * Enables the given @p rendition flag.  Rendition flags control the appearence
265     * of characters on the screen.
266     *
267     * @see Character::rendition
268     */ 
269    void setRendition  (int rendition);
270    /**
271     * Disables the given @p rendition flag.  Rendition flags control the appearence
272     * of characters on the screen.
273     *
274     * @see Character::rendition
275     */
276    void resetRendition(int rendition);
277   
278    /**
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.
283     *
284     * @see CharacterColor
285     */
286    void setForeColor  (int space, int color);
287    /**
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.
292     *
293     * @see CharacterColor
294     */
295    void setBackColor  (int space, int color);
296    /**
297     * Resets the cursor's color back to the default and sets the
298     * character's rendition flags back to the default settings.
299     */
300    void setDefaultRendition();
301   
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;
306   
307        /** TODO Document me */ 
308        void clear();
309    /**
310     * Sets the position of the cursor to the 'home' position at the top-left
311     * corner of the screen (0,0)
312     */
313    void home();
314    /**
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)
318     *
319     * <ul>
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>
326     * </ul>
327     *
328     * If @p clearScreen is true then the screen contents are erased entirely,
329     * otherwise they are unaltered.
330     */
331    void reset(bool clearScreen = true);
332   
333    /**
334     * Displays a new character at the current cursor position.
335     *
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.
339     *
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. 
343     */ 
344    void ShowCharacter(unsigned short c);
345   
346    // Do composition with last shown character FIXME: Not implemented yet for KDE 4
347    void compose(const QString& compose);
348   
349    /**
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.
354     *
355     * (note that in versions of Konsole prior to KDE 4, existing lines were
356     *  truncated when making the screen image smaller)
357     */
358    void resizeImage(int new_lines, int new_columns);
359   
360    /**
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.
364     *
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
369     */
370    void getImage( Character* dest , int size , int startLine , int endLine ) const;
371
372    /**
373     * Returns the additional attributes associated with lines in the image.
374     * The most important attribute is LINE_WRAPPED which specifies that the
375     * line is wrapped,
376     * other attributes control the size of characters in the line.
377     */
378    QVector<LineProperty> getLineProperties( int startLine , int endLine ) const;
379       
380
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. */
386    int  getHistLines ();
387    /**
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.
391     */
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();
395    /**
396     * Returns true if this screen keeps lines that are scrolled off the screen
397     * in a history buffer.
398     */
399    bool hasScroll();
400
401    /**
402     * Sets the start of the selection.
403     *
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.
407     */
408    void setSelectionStart(const int column, const int line, const bool columnmode);
409   
410    /**
411     * Sets the end of the current selection.
412     *
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.
415     */ 
416    void setSelectionEnd(const int column, const int line);
417   
418    /**
419     * Retrieves the start of the selection or the cursor position if there
420     * is no selection.
421     */
422    void getSelectionStart(int& column , int& line);
423   
424    /**
425     * Retrieves the end of the selection or the cursor position if there
426     * is no selection.
427     */
428    void getSelectionEnd(int& column , int& line);
429
430    /** Clears the current selection */
431    void clearSelection();
432
433    void setBusySelecting(bool busy) { sel_busy = busy; }
434
435    /**
436         *      Returns true if the character at (@p column, @p line) is part of the
437         *  current selection.
438         */ 
439    bool isSelected(const int column,const int line) const;
440
441    /**
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.
445     */
446    QString selectedText(bool preserveLineBreaks);
447           
448        /**
449         * Copies part of the output to a stream.
450         *
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
454         */
455        void writeToStream(TerminalCharacterDecoder* decoder, int from, int to);
456
457    /**
458     * Sets the selection to line @p no in the history and returns
459     * the text of that line from the history buffer.
460     */
461    QString getHistoryLine(int no);
462
463        /**
464         * Copies the selected characters, set using @see setSelBeginXY and @see setSelExtentXY
465         * into a stream.
466         *
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.
472         */
473        void writeSelectionToStream(TerminalCharacterDecoder* decoder , bool
474                                preserveLineBreaks = true);
475
476    /** TODO Document me */
477    void checkSelection(int from, int to);
478
479        /**
480         * Sets or clears an attribute of the current line.
481         *
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.
490         *
491         * @param enable true to apply the attribute to the current line or false to remove it
492         */
493        void setLineProperty(LineProperty property , bool enable);
494
495
496    /**
497     * Returns the number of lines that the image has been scrolled up or down by,
498     * since the last call to resetScrolledLines().
499     *
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.
502     */
503    int scrolledLines() const;
504
505    /**
506     * Returns the region of the image which was last scrolled.
507     *
508     * This is the area of the image from the top margin to the
509     * bottom margin when the last scroll occurred.
510     */
511    QRect lastScrolledRegion() const;
512
513    /**
514     * Resets the count of the number of lines that the image has been scrolled up or down by,
515     * see scrolledLines()
516     */
517    void resetScrolledLines();
518
519    /**
520     * Returns the number of lines of output which have been
521     * dropped from the history since the last call
522     * to resetDroppedLines()
523     *
524     * If the history is not unlimited then it will drop
525     * the oldest lines of output if new lines are added when
526     * it is full. 
527     */
528    int droppedLines() const;
529
530    /**
531     * Resets the count of the number of lines dropped from
532     * the history.
533     */
534    void resetDroppedLines();
535
536        /**
537         * Fills the buffer @p dest with @p count instances of the default (ie. blank)
538         * Character style.
539         */
540        static void fillWithDefaultChar(Character* dest, int count);
541
542private: 
543
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, 
553                          int start, 
554                          int count, 
555                          TerminalCharacterDecoder* decoder,
556                          bool appendNewLine,
557                          bool preserveLineBreaks);
558       
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);
563
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);
568   
569    void scrollUp(int from, int i);
570    void scrollDown(int from, int i);
571
572    void addHistLine();
573
574    void initTabStops();
575
576    void effectiveRendition();
577    void reverseRendition(Character& p) const;
578
579    bool isSelectionValid() const;
580
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;
587
588
589    // screen image ----------------
590    int lines;
591    int columns;
592
593    typedef QVector<Character> ImageLine;      // [0..columns]
594    ImageLine*          screenLines;    // [lines]
595
596    int _scrolledLines;
597    QRect _lastScrolledRegion;
598
599    int _droppedLines;
600
601    QVarLengthArray<LineProperty,64> lineProperties;   
602       
603    // history buffer ---------------
604    HistoryScroll *hist;
605   
606    // cursor location
607    int cuX;
608    int cuY;
609
610    // cursor color and rendition info
611    CharacterColor cu_fg;      // foreground
612    CharacterColor cu_bg;      // background
613    quint8 cu_re;      // rendition
614
615    // margins ----------------
616    int tmargin;      // top margin
617    int bmargin;      // bottom margin
618
619    // states ----------------
620    ScreenParm currParm;
621
622    // ----------------------------
623
624    bool* tabstops;
625
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
632
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
637
638    //
639    // save cursor, rendition & states ------------
640    //
641
642    // cursor location
643    int sa_cuX;
644    int sa_cuY;
645
646    // rendition info
647    quint8 sa_cu_re;
648    CharacterColor sa_cu_fg;
649    CharacterColor sa_cu_bg;
650   
651    // last position where we added a character
652    int lastPos;
653
654    // modes
655    ScreenParm saveParm;
656
657    static Character defaultChar;
658};
659
660}
661
662#endif // SCREEN_H
Note: See TracBrowser for help on using the repository browser.