www.pudn.com > commutil.zip > WINDOW.CPP


// ******************************************************************** // 
//                                                                      // 
//      WINDOW.CPP                                                      // 
//      Copyright (c) 1993, Michael Holmes and Bob Flanders             // 
//      C++ Communication Utilities                                     // 
//                                                                      // 
//      Chapter 7: Receiving a FAX                                      // 
//      Last changed in chapter 2                                       // 
//                                                                      // 
//      This file contains the definition and interface for             // 
//      the window class.                                               // 
//                                                                      // 
// ******************************************************************** // 
 
// 
//  MSC specific defines 
// 
 
#define BLACK           0                   // standard colors 
#define BLUE            1 
#define GREEN           2 
#define CYAN            3 
#define RED             4 
#define MAGENTA         5 
#define BROWN           6 
#define LIGHTGRAY       7 
#define DARKGRAY        8 
#define LIGHTBLUE       9 
#define LIGHTGREEN      10 
#define LIGHTCYAN       11 
#define LIGHTRED        12 
#define LIGHTMAGENTA    13 
#define YELLOW          14 
#define WHITE           15 
                                            // MSC text window defines 
#define gotoxy(x,y) _settextposition(y, x)  // cursor position in window 
#define clrscr()    _clearscreen(_GWINDOW)  // clear screen 
#define textcolor(x)      _settextcolor((short) x)  // set up text and 
#define textbackground(x) _setbkcolor(x)            // ..and bkgnd color 
#define window(ur,uc,lr,lc) _settextwindow(uc, ur, lc, lr) // window define 
 
// 
//  Globals 
// 
 
int     _wscroll = 1,                       // global text scroll flag 
        cprintf(char *msg, ...);            // message to format/display 
 
enum    boxes                               // line drawing box types 
    { 
    none = -1,                              // no box 
    single_line,                            // single line box 
    double_line                             // double line box 
    }; 
 
struct  box_characters                      // box drawing characters 
     { 
     char ul_char,                          // upper left corner 
          ur_char,                          // upper right corner 
          ll_char,                          // lower left corner 
          lr_char,                          // lower right corner 
          top_char,                         // horizontal line 
          side_char;                        // vertical line 
     } box_chars[2] = 
         { 
         { '\xda', '\xbf', '\xc0', '\xd9',  // single line box 
           '\xc4', '\xb3'}, 
         { '\xc9', '\xbb', '\xc8', '\xbc',  // double line box 
           '\xcd', '\xba'} 
         }; 
 
class Window 
    { 
    public: 
        Window(char ul_c, char ul_r,        // define window, upper left 
               char lr_c, char lr_r,        //   lower right, 
               char cn,   char cr);         //   normal & reverse colors 
        void Open(boxes box = none),        // open window 
             AtSay(int c, int r, char *s),  // display string at position 
             AtSayReverse(int c, int r,     // display string at position 
               char *s),                    //   in reverse video 
             Display(char),                 // display a character 
             Display(char *s),              // display a string 
             DisplayReverse(char *s),       // display string in rev video 
             Clear(void),                   // clear window 
             GotoXY(int c, int r),          // goto xy location 
             MakeCurrent(void),             // make window current 
             Close(void);                   // close window 
       ~Window();                           // destructor 
 
    private: 
        char  ul_col, ul_row,               // window upper left 
              lr_col, lr_row,               // ..and lower right 
              cursor_col, cursor_row,       // cursor column and row 
              cn_color, cr_color,           // norm and reverse colors 
             *old_data,                     // overlaid data 
              open_flag,                    // window open/close flag 
              scroll_flag;                  // scrolling enabled flag 
        boxes border_flag;                  // border type 
    }; 
 
 
 
// 
//  Globals 
// 
 
int     max_lines = 25;                     // max lines on screen 
 
Window *last_window;                        // last window pointer 
 
 
 
// 
//  Routine Definitions 
// 
 
int     wherex(void),                       // get current cursor 
        wherey(void);                       // ..x and y locations 
 
 
 
/* ******************************************************************** * 
 * 
 *  Window -- define window instance 
 * 
 * ******************************************************************** */ 
 
Window::Window(char ul_c, char ul_r,        // upper left corner 
               char lr_c, char lr_r,        // lower right corner 
               char cn,   char cr)          // normal and reverse colors 
{ 
 
ul_col = ul_c;                              // save window coordinates 
ul_row = ul_r;                              // ..row and column 
lr_col = lr_c;                              // ..for upper left 
lr_row = lr_r;                              // ..and lower right 
 
cn_color = cn;                              // save user colors 
cr_color = cr;                              // ..for later 
 
cursor_col = cursor_row = 1;                // init cursor column and row 
open_flag = 0;                              // clear open flags 
 
old_data = new char[(((lr_c - ul_c) + 1)    // get work buffer 
            * ((lr_r - ul_r) + 1)) * 2];    // ..for old screen image 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  Open -- open a window 
 * 
 * ******************************************************************** */ 
 
void    Window::Open(boxes box)             // border flag 
{ 
int     i;                                  // loop control 
struct  box_characters *b;                  // box characters 
 
 
if (open_flag)                              // q. window already opened? 
    return;                                 // a. yes .. just return 
 
border_flag = box;                          // set border flag 
open_flag = 1;                              // show window opened 
 
gettext(ul_col, ul_row, lr_col, lr_row,     // capture old screen data 
            old_data);                      // ..to temp buffer 
 
window(ul_col, ul_row, lr_col, lr_row);     // make window active 
 
textcolor(FG(cn_color));                    // set up foreground 
textbackground(BG(cn_color));               // ..and background colors 
 
clrscr();                                   // clear window 
scroll_flag = _wscroll;                     // ..and save scroll setting 
 
if (box != none)                            // q. border requested? 
    {                                       // a. yes .. draw the box 
    b = &box_chars[box];                    // get line drawing group 
    _wrapon(_GWRAPOFF);                     // disable text scrolling 
 
    gotoxy(1, 1);                           // goto upper left corner 
    cprintf("%c", b->ul_char);              // put out first corner 
 
    for (i = 1; i < (lr_col - ul_col); i++) // build top of box.. 
        cprintf("%c", b->top_char);         // ..with horizontals 
 
    cprintf("%c", b->ur_char);              // ..and upper right corner 
 
    gotoxy(1, (lr_row - ul_row) + 1);       // goto lower left corner 
    cprintf("%c", b->ll_char);              // put out bottom corner 
 
    for (i = 1; i < (lr_col - ul_col); i++) // build bottom of box 
        cprintf("%c", b->top_char);         // ..with horizontals 
 
    cprintf("%c", b->lr_char);              // ..and lower right corner 
 
    for (i = 2; i <= (lr_row - ul_row); i++)// put the sides on the box 
        { 
        gotoxy(1, i);                       // jump to left side of box 
        cprintf("%c", b->side_char);        // ..and draw a chunk 
 
        gotoxy((lr_col - ul_col) + 1, i);   // ..then jump to right side 
        cprintf("%c", b->side_char);        // ..of the box and draw 
        } 
 
    } 
 
if (scroll_flag)                            // q. user want scrolling? 
    _wrapon(_GWRAPON);                      // a. yes .. enable scrolling 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  AtSay -- display string at position 
 * 
 * ******************************************************************** */ 
 
void    Window::AtSay(int c, int r,         // column and row to 
              char *s)                      // display string 
{ 
 
GotoXY(c, r);                               // set up at the right place 
 
cprintf("%s", s);                           // display string in window 
 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  AtSayReverse -- display string at position in reverse video 
 * 
 * ******************************************************************** */ 
 
void    Window::AtSayReverse(int c, int r,  // column and row to 
              char *s)                      // display string 
{ 
 
GotoXY(c, r);                               // set up at the right place 
textcolor(FG(cr_color));                    // set up foreground 
textbackground(BG(cr_color));               // ..and background colors 
 
cprintf("%s", s);                           // display string in window 
 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
textcolor(FG(cn_color));                    // then set colors back to 
textbackground(BG(cn_color));               // ..their normal settings 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  Display -- display a character in a window 
 * 
 * ******************************************************************** */ 
 
void    Window::Display(char c)             // character to display 
{ 
 
MakeCurrent();                              // make this window current 
cprintf("%c", c);                           // display string in window 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  Display -- display string in window 
 * 
 * ******************************************************************** */ 
 
void    Window::Display(char *s)            // string to display 
{ 
 
MakeCurrent();                              // make this window current 
cprintf("%s", s);                           // display string in window 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  DisplayReverse -- display string in reverse video 
 * 
 * ******************************************************************** */ 
 
void    Window::DisplayReverse(char *s)     // string to display 
{ 
 
MakeCurrent();                              // make this window current 
textcolor(FG(cr_color));                    // set up foreground 
textbackground(BG(cr_color));               // ..and background colors 
 
cprintf("%s", s);                           // display string in window 
 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
textcolor(FG(cn_color));                    // then set colors back to 
textbackground(BG(cn_color));               // ..their normal settings 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  Clear -- clear current window 
 * 
 * ******************************************************************** */ 
 
void    Window::Clear(void) 
{ 
 
MakeCurrent();                              // make this window current 
clrscr();                                   // ..then clear it 
 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  GotoXY -- position cursor in window 
 * 
 * ******************************************************************** */ 
 
void    Window::GotoXY(int c, int r)        // column and row 
{ 
 
MakeCurrent();                              // make this window current 
gotoxy(c, r);                               // goto requested location 
cursor_col = wherex();                      // save cursor column.. 
cursor_row = wherey();                      // ..and cursor row 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  Close -- close window and restore screen 
 * 
 * ******************************************************************** */ 
 
void    Window::Close(void) 
{ 
 
if (NOT open_flag)                          // q. window already closed? 
    return;                                 // a. yes .. just return 
 
open_flag = 0;                              // clear opened flag 
 
puttext(ul_col, ul_row, lr_col, lr_row,     // restore old screen data 
            old_data);                      // ..from temp buffer 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  ~Window -- destructor 
 * 
 * ******************************************************************** */ 
 
Window::~Window() 
{ 
 
if (open_flag)                              // q. window still open? 
    Close();                                // a. yes .. close window 
 
last_window = 0;                            // clear window pointer 
delete old_data;                            // de-allocate screen buffer 
window(1, 1, 80, max_lines);                // set whole screen as window 
 
} 
 
 
 
/* ******************************************************************** * 
 * 
 *  MakeCurrent -- make this window current 
 * 
 * ******************************************************************** */ 
 
void    Window::MakeCurrent(void) 
{ 
 
if (last_window != this)                    // q. same window? 
    { 
    last_window = this;                     // a. no .. use this window 
 
    if (scroll_flag)                        // q. user want scrolling? 
        _wrapon(_GWRAPON);                  // a. yes .. enable scrolling 
     else 
        _wrapon(_GWRAPOFF);                 // else .. disable scrolling 
 
    if (border_flag == none)                // q. any border? 
        window(ul_col, ul_row,              // a. no .. set up window 
                lr_col, lr_row);            // ..using entire area 
     else 
        window(ul_col + 1, ul_row + 1,      // else .. set up the window 
                lr_col - 1, lr_row - 1);    // ..allowing for the border 
 
    gotoxy(cursor_col, cursor_row);         // ..and re-place cursor 
    textcolor(FG(cn_color));                // ..and set up foreground 
    textbackground(BG(cn_color));           // ..and background colors 
    } 
} 
 
 
 
/* ******************************************************************** * 
 *      wherex() -- determine column number                             * 
 * ******************************************************************** */ 
 
int     wherex(void) 
{ 
struct  rccoord pos;                        // row and col coordinates 
 
 
pos = _gettextposition();                   // get current cursor .. 
return(pos.col);                            // ..and return the column 
 
} 
 
 
 
/* ******************************************************************** * 
 *      wherey() -- determine row number                                * 
 * ******************************************************************** */ 
 
int     wherey(void) 
{ 
struct  rccoord pos;                        // row and col coordinates 
 
 
pos = _gettextposition();                   // get current cursor .. 
return(pos.row);                            // ..and return the row nbr 
 
} 
 
 
 
/* ******************************************************************** * 
 *      cprintf() -- handle outputing text to screen                    * 
 * ******************************************************************** */ 
 
int     cprintf(char *msg, ...)             // message to format/display 
{ 
char    buf[100];                           // string buffer 
va_list list;                               // variable list 
 
 
va_start(list, msg);                        // set up variable list 
vsprintf(buf, msg, list);                   // ..format buffer 
_outtext(buf);                              // ..then display message 
 
return(strlen(buf));                        // ..and rtn w/string length 
 
}