# Independent Terminal Type Class

Topics
Overview | Coords Class
Terminal Class | MSDOS Port

Overview

The Terminal class is a terminal interface designed to be portable between DOS and UNIX systems. On UNIX systems the curses library is used to create independent terminal type code. On MSDOS and Windows 95 systems the ANSI.SYS driver is used to simulate some of the basic functions in the curses library.

The curses library is a comprehensive API that allows an application to perform detailed terminal operations. A terminal is the logical input (keyboard) and output (screen) device through which character-based applications interact with the user. There are several types of terminals. Each type has different capabilities with respect to its display modes plus its input and output operations. On UNIX systems various terminal types can be emulated on the console or during telnet sessions.

Using the curses library a program you can manipulate text anywhere on the screen and use different display modes such as highlighting and accept input from the user without waiting for the user to press the RETURN key. Console based programs do not have the capability to manipulate text on the screen and rely on buffered input.

Coords Class

The Coords class is provided for use within an application to maintain screen coordinates used to position text on the screen.

    class Coords
    {
    public:
      Coords() { xpos = ypos = 0; }
      Coords(int x, int y) { xpos = x; ypos = y; }
      ~Coords() { }
      Coords(const Coords &ob) { xpos = ob.xpos ; ypos = ob.ypos; }
      Coords &operator=(const Coords &ob) {
        xpos = ob.xpos ; ypos = ob.ypos; return *this;
      }

    public:
      int XPos() { return xpos; } // Current x coordinate
      int YPos() { return ypos; } // Current y coordinate
      int XNext() { int x = xpos + 1; return x; }
      int YNext() { int y = ypos + 1; return y; }
      int XPrev() { int x = xpos - 1; return x; }
      int YPrev() { int y = ypos - 1; return y; }
      int XOffset(int x) { xpos = xpos + x; return xpos; }
      int YOffset(int y) { ypos = ypos + y; return ypos; }
      void SetXY(int x, int y) { xpos = x; ypos = y; }
      void SetX(int x) { xpos = x; }
      void SetY(int y) { ypos = y; }

    private:
      int xpos;
      int ypos;
    };

Terminal Class

The Terminal class is used to create terminal based user interfaces. A global pointer to a Terminal class object, named terminal is provided to be used within an application to perform all the necessary operations for the life of the program.

The Terminal class is designed work under both DOS and UNIX systems. The operating system is selected by defining one of the macros at compile time: __DOS__ or __UNIX__. NOTE: on UNIX systems the following linker flags must be used when compiling a curses based program:

LFLAGS = -lcurses -ltermcap

Class Constructors and Destructor

Terminal::Terminal() - Default class constructor.

Terminal::~Terminal()

- Class destructor that does nothing. The application is responsible for closing the terminal session.

Standard dialogs

int Terminal::YesNo(int x = -1, int y = -1) - Public member function used to prompt the user for a yes or no input. If the x and y coordinates are not given the cursor will remain at its current location on the screen. Returns true if the user answers yes.

int Terminal::YesNo(const char *s, int x = -1, int y = -1)

- Public member function used to prompt the user for a yes or no input, with a specified prompt. If the x and y coordinates are not given the cursor will remain at its current location on the screen. Returns true if the user answers yes.

int Terminal::GetYesNo()

- Public member function used to wait for a yes or no input from the user with no prompt. Returns true if the user answers yes.

void Terminal::AnyKey(int x = -1, int y = -1)

- Public member function used to prompt the user to press any key before continuing. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::AnyKey(const char *s, int x = -1, int y = -1)

- Public member function used to prompt the user to press any key before continuing, with a specified prompt. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

Terminal setup and screen operations

void Terminal::init() - Public member function used to initialize the curses library in raw mode with no echo. A simulated terminal initialization is performed under MSDOS.

void Terminal::finish()

- Public member function used to end a terminal session. This function must be called before the program terminates.

char *Terminal::GetTerm()

- public member function that returns the current TERM environment setting.

int Terminal::MaxLines()

- Public member function that returns the maximum number of lines currently being used on the screen.

int Terminal::MaxLines() const

- Public member function that returns the maximum number of lines currently being used on the screen.

int Terminal::MaxCols()

- Public member function that returns the maximum number of columns currently being used on the screen.

int Terminal::MaxCols() const

- Public member function that returns the maximum number of columns currently being used on the screen.

void Terminal::SetMaxLines(int lines)

- Public member function used to set the maximum number of lines that can be used on the screen. This number cannot exceed the maximum number of lines set by the global LINES variable defined in the curses library.

void Terminal::SetMaxCols(int cols)

- Public member function used to set the maximum number of columns that can be used on the screen. This number cannot exceed the maximum number of columns set by the global COLS variable defined in the curses library.

void Terminal::MoveCursor(int x, int y) const

- Public member function used to move the cursor to the specified position.

void Terminal::MoveCursor(int x, int y)

- Public member function used to move the cursor to the specified position.

void Terminal::ClearScreen() const

- Public member function used to clear the screen.

void Terminal::ClearScreen()

- Public member function used to clear the screen.

void Terminal::ClearLine(int x = -1, int y = -1) const

- Public member function used to clear a single line. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::ClearLine(int x = -1, int y = -1)

- Public member function used to clear a single line. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

int Terminal::Center(const char *s) const

- Public member function used to center a line of text on the screen.

int Terminal::Center(char *s)

- Public member function used to center a line of text on the screen.

int Terminal::Right(const char *s) const

- Public member function used to right justify a line of text on the screen.

int Terminal::Right(char *s)

- Public member function used to right justify a line of text on the screen.

int Terminal::ScreenCenter(const int offset) const

- Public member function that returns the center coordinate of the screen starting at the specified offset.

int Terminal::ScreenCenter(int offset)

- Public member function that returns the center coordinate of the screen starting at the specified offset.

Output functions

void Terminal::Write(const char c, int x = -1, int y = -1) const - Public member function used to write a single character to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(char c, int x = -1, int y = -1)

- Public member function used to write a single character to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(const unsigned char c, int x = -1, int y = -1) const

- Public member function used to write a single character to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(unsigned char c, int x = -1, int y = -1)

- Public member function used to write a single character to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(const char *s, int x = -1, int y = -1) const

- Public member function used to write a character string to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(char *s, int x = -1, int y = -1)

- Public member function used to write a character string to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(const long val, int x = -1, int y = -1) const

- Public member function used to write a long integer value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(long val, int x = -1, int y = -1)

- Public member function used to write a long integer value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(const int val, int x = -1, int y = -1) const

- Public member function used to write an integer value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(int val, int x = -1, int y = -1)

- Public member function used to write an integer value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(double val, int x = -1, int y = -1)

- Public member function used to write a double precision floating point value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(const double val, int x = -1, int y = -1) const

- Public member function used to write a double precision floating point value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(float val, int x = -1, int y = -1)

- Public member function used to write a single precision floating point value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::Write(const float val, int x = -1, int y = -1) const

- Public member function used to write a single precision floating point value to the screen. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::StatusLine(const char *s) const

- Public member function used to write a status line of specified text on the last line of the screen.

void Terminal::StatusLine(char *s)

- Public member function used to write a status line of specified text on the last line of the screen.

void Terminal::StandOut(const char *s, int x = -1, int y = -1) const

- Public member function used to write a line of text in standout mode. NOTE: Standout mode does yield the same results on every terminal type. In order to maintain portability you should avoid using these functions in code that needs to maintain a universal appearance when executed across multiple platforms.

void Terminal::StandOut(char *s, int x = -1, int y = -1)

- Public member function used to write a line of text in standout mode. NOTE: Standout mode does yield the same results on every terminal type. In order to maintain portability you should avoid using these functions in code that needs to maintain a universal appearance when executed across multiple platforms.

Input functions

int Terminal::KBWait() const - Public member function used to allow the program to continue processing until the user interrupts by pressing any key.

unsigned Terminal::char GetChar()

- Public member function used to get a single character from the keyboard (unbuffered) without waiting for the RETURN key. This function will not accept any input if any characters are waiting to be put back due to a call to the Terminal::PutBack() function.

void Terminal::GetString(char *string, int x = -1, int y = -1)

- Public member function used to allow the user to enter a single string. It was added to echo each character without having to turn the echo on. This will allow the user to edit the entry using the backspace or other defined keys. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::GetPassword(char *string, int x = -1, int y = -1)

- Public member function used to allow the user to enter a password string and echo an asterisk for each character typed. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

void Terminal::PutBack(char c)

- Public member function used to put back a keyboard character before another one is allowed to be entered with a call to the Termainl::GetChar() function.

int Terminal::GetInt(int x = -1, int y = -1)

- Public member function used to allow the user to enter an integer value. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

long Terminal::GetLong(int x = -1, int y = -1)

- Public member function used to allow the user to enter a long integer value. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

double Terminal::GetFloat(int x = -1, int y = -1)

- Public member function used to allow the user to enter a double precision floating point value. If the x and y coordinates are not given the cursor will remain at its current location on the screen.

MSDOS Port

The DOS port of the curses library is enabled by defining the __DOS__ macro at compile time. Several standalone functions are used to port this code from UNIX to DOS, Windows 3.11, and Windows 95. These functions are used to simulate the basic functionality of the vast curses library. All of these functions rely on the ANSI.SYS driver installed in the config.sys file:

DEVICEHIGH=C:\WINDOWS\COMMAND\ANSI.SYS

(in Windows 95)

DEVICEHIGH=C:\DOS\ANSI.SYS

(in DOS/Windows 3.11)

The maximum number of lines and columns set by the global variables LINES and COLS are always set to 25 and 80 respectively. Several definitions are used for further compatibility with the curses library:

    #ifndef TRUE
    #define	TRUE	1
    #endif
    #ifndef FALSE
    #define	FALSE	0
    #endif
    #ifndef ERR
    #define	ERR	-1
    #endif
    #ifndef OK
    #define	OK	0
    #endif

The following standalone functions are used to simulate the basic functionality of the curses library:

int addch(const chtype c)

- Standalone function used to add a single character at the current cursor position.

int addstr(const char *s)

- Standalone function used to add a text string at the current cursor position.

int beep()

- Standalone function used to alert the user of an error condition.

int clear()

- Standalone function used to clear the screen.

int endwin()

- Standalone function used to close the terminal session before the program terminates.

int move(int y, int x)

- Standalone function used to move the cursor to a specified position on the screen.

int refresh()

- Standalone function used to refresh the screen after a move or add operation.

int standend()

- Standalone function used to turn off stand out mode.

int standout()

- Standalone function used to turn on stand out mode.

The following code is supplied for PC enhancements, used to emulate the effects of the video attribute functions in the curses library. All of the escape codes are compatible with vt100 terminals.

void UnderlineText()

- Standalone function used to underline text displayed on the screen.

void BoldText()

- Standalone function used to highlight text displayed on the screen in bold letters.

void ReverseVideo()

- Standalone function used to highlight text displayed on the screen in reverse video.

void NormalText()

- Standalone function used to reset the text mode to normal after a bold, underline, or reverse video operation.

End Of Document