#include "Display.h" #include #include #include #include using namespace std; /* *-------------------------------------------------------------------- * Method: * Purpose: * Arguments: * Returns: *-------------------------------------------------------------------- */ namespace MKBasic { /* *-------------------------------------------------------------------- * Method: * Purpose: * Arguments: * Returns: *-------------------------------------------------------------------- */ Display::Display() { InitScr(); } /* *-------------------------------------------------------------------- * Method: * Purpose: * Arguments: * Returns: *-------------------------------------------------------------------- */ Display::~Display() { } /* *-------------------------------------------------------------------- * Method: InitScr() * Purpose: Initialize screen. * Arguments: n/a * Returns: n/a *-------------------------------------------------------------------- */ void Display::InitScr() { mLastChar = 0; mScrLines = SCREENDIM_ROW; mScrColumns = SCREENDIM_COL; mShellConsoleWidth = GetConsoleWidth(); if (mScrColumns > mShellConsoleWidth) { mScrColumns = mShellConsoleWidth; } ClrScr(); } #if defined(WINDOWS) #include #include /* *-------------------------------------------------------------------- * Method: GetConsoleWidth() * Purpose: Obtain the width of shell console (the real one, not * the emulated one) on Windows. * Arguments: n/a * Returns: int - width of the shell console. *-------------------------------------------------------------------- */ int Display::GetConsoleWidth() { HANDLE hStdOut; CONSOLE_SCREEN_BUFFER_INFO csbi; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); if (hStdOut == INVALID_HANDLE_VALUE) return -1; if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return -2; return csbi.dwSize.X; } #endif #if defined(LINUX) #include /* *-------------------------------------------------------------------- * Method: GetConsoleWidth() * Purpose: Obtain the width of shell console (the real one, not * the emulated one) on Linux. * Arguments: n/a * Returns: int - width of the shell console. *-------------------------------------------------------------------- */ int Display::GetConsoleWidth() { unsigned int conwidth = SCREENDIM_COL; char *termtype = getenv("TERM"); static char termbuf[2048]; if (tgetent(termbuf, termtype) < 0) { cout << "WARNING: Could not access the termcap data base." << endl; cout << " Unable to determine console width." << endl; } else { conwidth = tgetnum("co"); } return conwidth; } #endif /* *-------------------------------------------------------------------- * Method: * Purpose: * Arguments: * Returns: *-------------------------------------------------------------------- */ void Display::ScrollUp() { for (unsigned int row=0; row= mScrLines) { ScrollUp(); mCursorCoord.row = mScrLines-1; } } else if (c == SCREENSPECCHARS_CR) { mLastChar = SCREENSPECCHARS_CR; mCursorCoord.col = 0; } else if (c == SCREENSPECCHARS_TB) { mLastChar = SCREENSPECCHARS_TB; mCursorCoord.col += TABSIZE; if (mCursorCoord.col >= mScrColumns) { mCursorCoord.col = mScrColumns-1; // must work on it some more } } else if (c == SCREENSPECCHARS_BS) { mLastChar = SCREENSPECCHARS_BS; if (mCursorCoord.col > 0) mCursorCoord.col--; } else if (c == SCREENSPECCHARS_BE) { mLastChar = SCREENSPECCHARS_BE; // no action } else { mScreen[mCursorCoord.row][mCursorCoord.col] = c; mLastChar = c; mCursorCoord.col++; if (mCursorCoord.col >= mScrColumns) { mCursorCoord.col = 0; mCursorCoord.row++; if (mCursorCoord.row >= mScrLines) { ScrollUp(); mCursorCoord.row = mScrLines-1; } } } } } /* *-------------------------------------------------------------------- * Method: ClrScr() * Purpose: Fill the screen with spaces. Set cursor in left-upper * corner. * Arguments: n/a * Returns: n/a *-------------------------------------------------------------------- */ void Display::ClrScr() { for (unsigned int col=0; col mScrColumns) line = line + "\n"; scr = scr + line; } cout << scr; } /* *-------------------------------------------------------------------- * Method: GetCursorCoord() * Purpose: Get cursor coordinates. * Arguments: n/a * Returns: pointer to cursor coordinates *-------------------------------------------------------------------- */ CursorCoord *Display::GetCursorCoord() { return &mCursorCoord; } /* *-------------------------------------------------------------------- * Method: * Purpose: * Arguments: * Returns: *-------------------------------------------------------------------- */ char Display::GetLastChar() { return mLastChar; } } // namespace MKBasic