ansiterm.h
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:27k
- /*
- * ansiterm.h
- *
- * ANSI text terminal emulator interactor for GUI.
- *
- * Portable Windows Library
- *
- * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
- *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is Portable Windows Library.
- *
- * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
- *
- * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
- * All Rights Reserved.
- *
- * Contributor(s): ______________________________________.
- *
- * $Log: ansiterm.h,v $
- * Revision 1.23 1999/03/09 08:01:47 robertj
- * Changed comments for doc++ support (more to come).
- *
- * Revision 1.22 1999/02/16 08:08:27 robertj
- * MSVC 6.0 compatibility changes.
- *
- * Revision 1.21 1998/12/12 00:41:59 robertj
- * Added capability to get text lines out of scroll back buffer.
- *
- * Revision 1.20 1998/09/23 06:19:16 robertj
- * Added open source copyright license.
- *
- * Revision 1.19 1996/05/15 10:14:36 robertj
- * Rewrite of terminal to make descendant only require single character output function.
- *
- * Revision 1.18 1996/04/30 12:33:29 robertj
- * Changed "inPixels" boolean to enum for three coordinate systems.
- *
- * Revision 1.17 1996/04/29 12:18:18 robertj
- * Added local echo capability to terminal.
- *
- * Revision 1.16 1995/11/20 11:02:29 robertj
- * Fixed bugs in non-adjustable rows and columns mode.
- *
- * Revision 1.15 1995/08/24 12:58:29 robertj
- * Fixed bug in font selection.
- * Implemented copy/paste.
- *
- * Revision 1.14 1995/07/31 12:15:39 robertj
- * Removed PContainer from PChannel ancestor.
- *
- * Revision 1.13 1995/07/02 01:16:30 robertj
- * Removed OnDoubleClick() and added BOOL to OnMouseDown() for double click.
- *
- * Revision 1.12 1995/06/17 11:07:58 robertj
- * Changed OnKeyDown to return BOOL indicating OnKeyInput is to be used.
- * Documentation update.
- *
- * Revision 1.11 1995/06/04 08:41:41 robertj
- * Some minor speedups.
- *
- * Revision 1.10 1995/03/14 12:40:56 robertj
- * Updated documentation to use HTML codes.
- *
- * Revision 1.9 1994/11/24 11:48:23 robertj
- * Documentation.
- *
- * Revision 1.8 1994/10/23 03:49:45 robertj
- * Added functions to set terminal options.
- * Implemented scrolling to ne PScrollable.
- * Improved terminal channel class.
- *
- * Revision 1.7 1994/08/23 11:32:52 robertj
- * Oops
- *
- * Revision 1.6 1994/08/22 00:46:48 robertj
- * Added pragma fro GNU C++ compiler.
- *
- * Revision 1.5 1994/07/27 06:03:00 robertj
- * Synchronisation
- *
- * Revision 1.4 1994/07/25 03:33:13 robertj
- * Changed variables to int for better platform independence.
- *
- * Revision 1.3 1994/07/17 10:46:06 robertj
- * Major redesign to use list instead of array to allow for bigger buffer.
- *
- * Revision 1.2 1994/06/25 11:55:15 robertj
- * Unix version synchronisation.
- *
- * Revision 1.1 1994/04/01 14:27:01 robertj
- * Initial revision
- *
- */
- #ifndef _ANSITERM_H
- #define _ANSITERM_H
- #ifdef __GNUC__
- #pragma interface
- #endif
- /** This class is an interactor that emulates a serial terminal. This is an
- abstract class that has specific terminal emulators descended from it eg
- the ref{PAnsiTerminal} class.
- A descendent class should implement the ref{Write()} function or
- ref{ProcessCharacter()} function to output characters to the terminal
- lines implemented by PTerminal. The descendent may also wish to implement
- the ref{PInteractor::OnKeyDown()} and ref{Reset()} functions for any
- special keys supported by the terminal or initialisation required when the
- terminal is reset.
- */
- class PTerminal : public PScrollable
- {
- PCLASSINFO(PTerminal, PScrollable);
- public:
- /**@name Construction */
- //@{
- /**Create a terminal interactor, contained within a scroller window.
- This always creates a Reset() terminal. */
- PTerminal(
- PScroller * parent,
- /**ref{PScroller} class that the terminal class interactor must be
- placed in. The PScroller contains the scroll bars etc and communicates
- with the ref{PScrollable} to do scrolling functions.
- */
- ostream * kbd = NULL
- /**Stream to send all keyboard input and other output, eg cursor
- position generated by the PTerminal.
- */
- );
- //@}
- /**@name Overrides from class PInteractor */
- //@{
- /**Set the font to be used by default by this interactor. A canvas created
- on this interactor will initially have this font selected.
-
- This also determines the coordinate system to be used for laying out
- child interactors via the ref{SetPosition()} and
- ref{SetDimensions()} functions. The coordinates used are 1/4 average
- font width and 1/8 the font height.
-
- If the #toChildren# parameter is TRUE then the font is
- propagated down to all child interactors and grandchild interactors etc.
- */
- virtual void SetFont(
- const PFont & newFont, /// New font specification fo rthe interactor.
- BOOL toChildren = TRUE
- /// Flag to recursively change all child interactors.
- );
- //@}
- /**@name New functions for class */
- //@{
- /**Reset the terminal. This clears the terminal virtual screen, wipes the
- scoll back buffer, resets internal terminal options (eg wrap at end of
- line) to default states and clears any modes the terminal is in.
- */
- virtual void Reset();
- /**Write characters to the terminal. This must be defined by the
- descendent class of the PTerminal. It should do all interpretation of
- the input data stream for the terminal protocol. This usually involves
- a state machine of some description.
- */
- virtual void Write(
- const char * buf,
- /// Pointer to the buffer of characters to place onto the terminal.
- PINDEX len
- /// Length of the buffer of characters.
- );
- /**Write a string to the terminal. This executes the Write() function
- that takes a buffer pointer and length.
- */
- void Write(
- const PString & str /// String to write to the terminal.
- ) { Write(str, str.GetLength()); }
- /**Determine if there is a selection active in the terminal.
- @return
- TRUE if there is a selection active, FALSE if no selection has been
- made.
- */
- virtual BOOL HasSelection() const;
- /**Get the lines of text that was selected in the terminal window.
- @return
- A string containing the lines of text in the selected region. The line
- separators are always n.
- */
- virtual PString GetSelection() const;
- /** Set the selected lines in the terminal window. */
- virtual void SetSelection(
- PINDEX startRow, /// Starting row of the selected block
- PINDEX startColumn, /// Starting column of the selected block
- PINDEX finishRow, /// Finishing row of the selected block
- PINDEX finishColumn /// Finishing column of the selected block
- );
- /**Set the output stream to receive all keyboard input or protocol
- responses from terminal emulation.
- */
- void SetKeyboardStream(
- ostream * out
- /// Stream to output keyboard data entered into the terminal emulator.
- );
- /**Get the text in the specified row. The trailing blanks are trimmed but
- leading blanks are included.
- @return
- String for the terminal row text.
- */
- PString GetRow(PINDEX idx) const;
- /**Get the maximum number of rows that are saved "scroll back" buffer of
- the terminal.
- @return
- Max number of rows in the buffer.
- */
- PINDEX GetTotalRows() const { return rowData.GetSize(); }
- /**Get the maximum number of rows that are saved "scroll back" buffer of
- the terminal.
- @return
- Max number of rows in the buffer.
- */
- PINDEX GetMaxSavedRows() const { return maxSavedRows; }
- /**Set the number of rows in the terminal. This will resize the interactor
- to fit the specified number of rows.
- */
- void SetMaxSavedRows(
- PINDEX newRows /// New number of rows
- );
- /**Get the number of rows in terminal.
- @return
- Number of rows visible in the window
- */
- PINDEX GetRows() const { return rows; }
- /* Set the number of rows in the terminal. This will resize the interactor
- to fit the specified number of rows.
- */
- void SetRows(
- PINDEX newRows /// New number of rows
- );
- /**Get the number of columns in terminal.
- @return
- Number of columns visible in the window
- */
- PINDEX GetColumns() const { return columns; }
- /**Set the number of columns in the terminal. This will resize the
- interactor to fit the specified number of rows.
- */
- void SetColumns(
- PINDEX newColumns /// New number of columns
- );
- /**Get the variable row/columns flag for the terminal. This flag controls
- if the rows/columns of the terminal emulator are fixed or change
- whenever the size of the interactor changes.
- @return
- Current value of the flag.
- */
- BOOL GetVariableRowColumns() const { return variableRowColumns; }
- /**Set the variable row/columns flag for the terminal. This flag controls
- if the rows/columns of the terminal emulator are fixed or change
- whenever the size of the interactor changes.
- */
- void SetVariableRowColumns(
- BOOL b /// New value of the flag
- );
- /**Get the block cursor flag for the terminal. This flag controls if
- the cursor (caret) of the terminal emulator is a full block or a single
- horizontal line.
- @return
- Current value of the flag.
- */
- BOOL GetBlockCursor() const { return blockCursor; }
- /**Set the block cursor flag for the terminal. This flag controls if the
- cursor (caret) of the terminal emulator is a full block or a single
- horizontal line.
- */
- void SetBlockCursor(
- BOOL b /// New value of the flag
- );
- /**Get the wrap at end of line flag for the terminal. This flag controls
- if the terminal emulator wraps around if text is output when the cursor
- is at the end of the line.
- @return
- Current value of the flag.
- */
- BOOL GetWrapEOL() const { return wrapEOL; }
- /**Set the wrap at end of line flag for the terminal. This flag controls
- if the terminal emulator wraps around if text is output when the
- cursor is at the end of the line.
- */
- void SetWrapEOL(
- BOOL b /// New value of the flag
- ) { wrapEOL = b; }
- /**Get the output line feed on return flag for the terminal. This flag
- controls if the terminal emulator does a line feed every time a
- carriage return is received.
- @return
- Current value of the flag.
- */
- BOOL GetLineFeedOnReturn() const { return lineFeedOnReturn; }
- /**Set the output return on line feed flag for the terminal. This flag
- controls if the terminal emulator does a line feed every time a
- carriage return is received.
- */
- void SetLineFeedOnReturn(
- BOOL b /// New value of the flag
- ) { lineFeedOnReturn = b; }
- /**Get the output return on line feed flag for the terminal. This flag
- controls if the terminal emulator does a carriage return every time a
- line feed is received.
- @return
- Current value of the flag.
- */
- BOOL GetReturnOnLineFeed() const { return returnOnLineFeed; }
- /**Set the output return on line feed flag for the terminal. This flag
- controls if the terminal emulator does a carriage return every time a
- line feed is received.
- */
- void SetReturnOnLineFeed(
- BOOL b /// New value of the flag
- ) { returnOnLineFeed = b; }
- /**Get the local echo flag for the terminal. This flag controls if the
- terminal emulator displays every character typed within it into its own
- input stream.
- @return
- Current value of the flag.
- */
- BOOL GetLocalEcho() const { return localEcho; }
- /**Set the local echo flag for the terminal. This flag controls if the
- terminal emulator displays every character typed within it into its own
- input stream.
- */
- void SetLocalEcho(
- BOOL b /// New value of the flag
- ) { localEcho = b; }
- //@}
- protected:
- /**@name Overrides from class PInteractor */
- //@{
- /**Set the dimensions of the interactor. The new size is specified
- in layout coordinates. The user may override this function if checks for
- maximum or minimum size are required. Note that this sets the dimension
- of the interior of the interactor, excluding any external decoration
- (eg title bars).
- */
- virtual void _SetDimensions(
- PDIMENSION width, /// New width of interactor
- PDIMENSION height, /// New height of interactor
- CoordinateSystem coords /// Coordinate system to use.
- );
- /**The system calls this whenever a mouse button is depressed in the
- interactors drawable area (ie not in title bars etc).
- */
- virtual void OnMouseDown(
- PKeyCode button,
- /**Key code that specifies which mouse button and which modifiers (shift,
- control etc) were active at the time.
- */
- const PPoint & where,
- /**The position of the mouse pointer provided in pixels with its origin
- at the top left of the interactor.
- */
- BOOL doubleClick
- /// Is TRUE if the mouse down is the second click of a double click.
- );
- /**This function is called whenever a drag track is in progress. This is
- begun with a call to the ref{StartMouseTrack()} function, which is
- typically called from within the ref{OnMouseDown()} function.
- The #lastTrack# parameter can be used to for special action
- at the end of the track operation.
- The mouse pointer is automatically grabbed (via ref{GrabMouse()})
- and released (via ref{ReleaseMouse()}). The canvas provided is
- also automatically created and deleted.
- */
- virtual void OnMouseTrack(
- PCanvas * canvas,
- /// The canvas available while the drag operation is tracking.
- const PPoint & where,
- /**The position of the mouse pointer provided in pixels with its origin
- at the top left of the interactor.
- */
- BOOL lastTrack
- /// This is TRUE if the position is the last track of the drag operation.
- );
- /**The system calls this whenever a keyboard key was pressed and this
- interactor had the focus. The string is the translated ANSI
- representation of the key combination.
- */
- virtual void OnKeyInput(
- const PString & str /// Translated key code.
- );
- /**The system calls this whenever it requires that the interactors
- drawable area needs to be updated and redrawn.
- */
- virtual void OnRedraw(
- PCanvas & canvas /// Canvas for drawing into the interactor
- );
- //@}
- // Sub-structures
- struct Attribute {
- unsigned inverse:1;
- unsigned bold:1;
- unsigned underline:1;
- unsigned blink:1;
- unsigned fgColor:4;
- unsigned bkColor:4;
- unsigned selected:1;
- }; // Current display attribute for terminal
- class Row : public PObject
- {
- PCLASSINFO(Row, PObject);
- public:
- Row(Attribute attribute);
- void Draw(PTerminal * term,
- PCanvas & canvas, PINDEX line, PINDEX lastCol);
- void Scroll(PTerminal * term, PCanvas & canvas,
- PINDEX line, PINDEX left, PINDEX right, int columns);
- void SetCell(PINDEX column, char ch, Attribute attribute)
- { ascii[column] = ch; attrib[column] = attribute; }
- void AdjustSelection(PINDEX start, PINDEX finish, BOOL select);
- PString GetText() const;
- PString GetSelection() const;
- enum { MaxColumns = 250 };
- private:
- Attribute attrib[MaxColumns];
- char ascii[MaxColumns];
- };
- friend class Row;
- /**@name New functions for class */
- //@{
- /**Process a character using the state machine for the terminal type.
- */
- virtual void ProcessCharacter(
- PDrawCanvas & canvas, /// Canvas used for updating the interactor
- char c /// Character to process
- );
- /**Get the Row object in which the current cursor is located.
- @return
- Reference to the Row object that the cursor is in.
- */
- Row & GetCursorRow();
- /**Adjust the caret/cursor position. This changes the internal location
- of the cursor clipped to the current scrolling region and window
- bounds. The ref{PCaret} is also updated.
- */
- void AdjustCaretPosition(
- int dLine, /// Amount to move the cursor vertically.
- int dCol /// Amount to move the cursor horizontally.
- );
- /**Move the caret/cursor position assuring that internal updates are made
- beforehand. This changes the internal location of the cursor clipped
- to the current scrolling region and window bounds. The ref{PCaret} is
- also updated.
- */
- void MoveCaretPosition(
- PDrawCanvas & canvas, /// Canvas used for updating the interactor
- int dLine, /// Amount to move the cursor vertically.
- int dCol /// Amount to move the cursor horizontally.
- );
- /**Scroll the section of the terminal erasing any part that is exposed
- by the scroll to spaces in the current attribute.
- */
- void ScrollLines(
- PDrawCanvas & canvas, /// Canvas used for updating the interactor.
- PINDEX top, /// The top line of the scrolled region.
- PINDEX bottom, /// The bottom line of the scrolled region.
- int lines /// The number of lines to scroll.
- );
- /**Adjust the cells selected in the terminal window.
- */
- void AdjustSelection(
- PCanvas & canvas, /// Canvas for updating the screen.
- PINDEX start, /// Start point of selection.
- PINDEX finish, /// End point of selection.
- BOOL select /// Whther region is being selected or deselected.
- );
- //@}
- // Member variables
- PLIST(RowList, Row);
- /**List of Row objects that constitute the terminal screen and the scroll
- back buffer.
- */
- RowList rowData;
- /// Maximum number of lines to save in the scroll back buffer.
- PINDEX maxSavedRows;
- /// Width of a cell in the terminal screen in pixels.
- PDIMENSION cellWidth;
- /// Height of a cell in the terminal screen in pixels.
- PDIMENSION cellHeight;
- /// Number of rows in the terminal screen.
- PINDEX rows;
- /// Number of columns in the terminal screen.
- PINDEX columns;
- /// Number of columns visible in the terminal screen.
- PINDEX columnsVisible;
- /// Number of rows visible in the terminal screen.
- PINDEX rowsVisible;
- /// Position of the top of the scroll region.
- PINDEX scrollTop;
- /// Position of the bottom of the scroll region.
- PINDEX scrollBottom;
- /// Row of the current cursor position.
- PINDEX cursorRow;
- /// Column of the current cursor position.
- PINDEX cursorColumn;
- /// Current attribute for new characters in terminal.
- Attribute attribute;
- /**The variable row/columns flag for the terminal. This flag controls if
- the rows/columns of the terminal emulator are fixed or change whenever
- the size of the interactor changes.
- */
- BOOL variableRowColumns;
- /**The block cursor flag for the terminal. This flag controls if the
- cursor (caret) of the terminal emulator is a full block or a single
- horizontal line.
- */
- BOOL blockCursor;
- /**The wrap at end of line flag for the terminal. This flag controls if
- the terminal emulator wraps around if text is output when the cursor is
- at the end of the line.
- */
- BOOL wrapEOL;
- /**The output line feed on return flag for the terminal. This flag controls
- if the terminal emulator does a line feed every time a carriage return
- is received.
- */
- BOOL lineFeedOnReturn;
- /**The output return on line feed flag for the terminal. This flag controls
- if the terminal emulator does a carriage return every time a line feed
- is received.
- */
- BOOL returnOnLineFeed;
- /**The local echo flag for the terminal. This flag controls if the
- terminal emulator displays every character typed within it into its own
- input stream.
- */
- BOOL localEcho;
- /**The stream to which data received by the terminals emulation of the
- keyboard is sent.
- */
- ostream * keyboard;
- /**Column for start of delayed screen output text. Characters may be output
- into internal structures but not displayed in the interactor until
- later. This algorithm is designed to improve performance by avoiding
- outputing each individual character through the display canvas.
- */
- PINDEX updateColumn;
- /**Starting position and current position for mouse selection of text in
- the terminal window.
- */
- PINDEX anchor, selection;
- /// Indicate that the selection is in columns, not whole lines.
- BOOL columnModeSelection;
- };
- /**An interactor that emulates an ANSI terminal. This is a descendent of the
- ref{PTerminal} class. This largely resembles the DEC VT100 in its
- operation but has some extra ANSI codes implemented.
- */
- class PAnsiTerminal : public PTerminal
- {
- PCLASSINFO(PAnsiTerminal, PTerminal);
- public:
- /**@name Construction */
- //@{
- /**Create an ANSI terminal interactor, contained within a scroller window.
- This always creates a Reset() terminal.
- */
- PAnsiTerminal(
- PScroller * parent,
- /**ref{PScroller} class that the terminal class interactor must be
- placed in. The PScroller contains the scroll bars etc and communicates
- with the ref{PScrollable} to do scrolling functions.
- */
- ostream * kbd = NULL
- /**Stream to send all keyboard input and other output, eg cursor position
- generated by the PTerminal.
- */
- );
- //@}
- /**@name Overrides from class PTerminal */
- //@{
- virtual void Reset();
- /* Reset the terminal emulation. This clears the terminal virtual screen,
- wipes the scoll back buffer, resets internal terminal options (eg wrap
- at end of line) to default states and clears any modes the terminal is
- in.
- */
- //@}
- protected:
- /**@name Overrides from class PInteractor */
- //@{
- /**The system calls this whenever a key has been pressed with the
- interactor having the focus. This provides a raw code for the key
- before any translation to ASCII.
- */
- virtual BOOL OnKeyDown(
- PKeyCode key, /// The key code that was pressed.
- unsigned repeat /// The repeat count if the key is auto-repeating.
- );
- //@}
- /**@name Overrides from class PTerminal */
- //@{
- /** Process incoming stream of data. */
- void ProcessCharacter(
- PDrawCanvas & canvas, /// Canvas to draw new characters
- char ch /// Character to process.
- );
- //@}
- // Member variables
- enum { MaxParameters = 10 };
- BOOL inEscapeSequence;
- BOOL inParameter;
- PINDEX parameter[MaxParameters];
- PINDEX numParameters;
- PINDEX savedCursorRow;
- PINDEX savedCursorColumn;
- };
- /**This class is a standard ref{PChannel} which may output to a
- ref{PTerminal} emulation interactor. As PChannel is descended from
- iostream all the standard << and >> operators may be used to output to a
- window.
- */
- class PTerminalChannel : public PChannel
- {
- PCLASSINFO(PTerminalChannel, PChannel);
- public:
- /**@name Construction */
- //@{
- /**Create a terminal interactor channel with no interactor. This is
- provided so that instances of this class (eg as member fields) can be
- declared before the ref{PTerminal} is created.
- */
- PTerminalChannel();
- /**Create a terminal interactor channel connected to a terminal emulator
- interactor.
- */
- PTerminalChannel(
- PTerminal & term
- /**The ref{PTerminal} descendent that implements the terminal emulation
- interactor window.
- */
- );
- ~PTerminalChannel();
- // Close the terminal channel when destroyed.
- //@}
- /**@name Overrides from class PChannel */
- //@{
- /**Determine if the channel is currently open and may be read from or
- written to.
- @return
- TRUE if the channel is open.
- */
- virtual BOOL IsOpen() const;
- /**Get the name given to the channel that is currently open. This returns
- the class name of the ref{PTerminal} descendent that is attached to
- the channel.
- @return
- Name of the channel.
- */
- virtual PString GetName() const;
-
- /**Low level read from the channel. This reads whatever is available as
- entered via the keyboard when the ref{PTerminal} had the focus.
- @return
- TRUE if the function successfully read at least one character of data
- from the channel.
- */
- virtual BOOL Read(
- void * buf, /// The buffer into which to put read characters.
- PINDEX len /// The maximum number of characters to read.
- );
- /**Low level write to the channel. This passes data through directly to
- the Write() function of ref{PTerminal}.
- @return
- TRUE if all the data specified was successfully written to the channel.
- */
- virtual BOOL Write(
- const void * buf, /// The buffer from which to write characters.
- PINDEX len /// The maximum number of characters to write.
- );
- /**Close the channel. This simply disconnects the current ref{PTerminal}
- from the channel and leaves it disconnected. Any reading or writing
- after this function has been executed and before a call to Open() has
- been made will assert.
- @return
- TRUE if the channel closed without error.
- */
- virtual BOOL Close();
- //@}
- /**@name New functions for class */
- //@{
- /**Open the ref{PTerminalChannel} using a ref{PTerminal} descendent.
- This "connects" the channel to an interactor. This will override any
- previous connection made.
- */
- BOOL Open(
- PTerminal & term
- /**The ref{PTerminal} descendent that implements the terminal emulation
- interactor window.
- */
- );
- //@}
- private:
- PTerminal * terminal;
- PStringStream readBuf;
- };
- #endif
- // End Of File ///////////////////////////////////////////////////////////////