WINDOW.CPP
上传用户:xr_qian
上传日期:2007-01-05
资源大小:443k
文件大小:14k
源码类别:

通讯/手机编程

开发平台:

DOS

  1. // ******************************************************************** //
  2. //                                                                      //
  3. //      WINDOW.CPP                                                      //
  4. //      Copyright (c) 1993, Michael Holmes and Bob Flanders             //
  5. //      C++ Communication Utilities                                     //
  6. //                                                                      //
  7. //      This file contains the definition and interface for             //
  8. //      the window class.                                               //
  9. //                                                                      //
  10. // ******************************************************************** //
  11. extern
  12. int    _wscroll;                            // screen scrolling flag
  13. enum    boxes                               // line drawing box types
  14.     {
  15.     none = -1,                              // no box
  16.     single_line,                            // single line box
  17.     double_line                             // double line box
  18.     };
  19. struct  box_characters                      // box drawing characters
  20.      {
  21.      char ul_char,                          // upper left corner
  22.           ur_char,                          // upper right corner
  23.           ll_char,                          // lower left corner
  24.           lr_char,                          // lower right corner
  25.           top_char,                         // horizontal line
  26.           side_char;                        // vertical line
  27.      } box_chars[2] =
  28.          {
  29.          { 'xda', 'xbf', 'xc0', 'xd9',  // single line box
  30.            'xc4', 'xb3'},
  31.          { 'xc9', 'xbb', 'xc8', 'xbc',  // double line box
  32.            'xcd', 'xba'}
  33.          };
  34. class Window
  35.     {
  36.     public:
  37.         Window(char ul_c, char ul_r,        // define window, upper left
  38.                char lr_c, char lr_r,        //   lower right,
  39.                char cn,   char cr);         //   normal & reverse colors
  40.         void Open(boxes box = none),        // open window
  41.              AtSay(int c, int r, char *s),  // display string at position
  42.              AtSayReverse(int c, int r,     // display string at position
  43.                char *s),                    //   in reverse video
  44.              Display(char),                 // display a character
  45.              Display(char *s),              // display a string
  46.              DisplayReverse(char *s),       // display string in rev video
  47.              Clear(void),                   // clear window
  48.              GotoXY(int c, int r),          // goto xy location
  49.              MakeCurrent(void),             // make window current
  50.              Close(void);                   // close window
  51.        ~Window();                           // destructor
  52.     private:
  53.         char  ul_col, ul_row,               // window upper left
  54.               lr_col, lr_row,               // ..and lower right
  55.               cursor_col, cursor_row,       // cursor column and row
  56.               cn_color, cr_color,           // norm and reverse colors
  57.              *old_data,                     // overlaid data
  58.               open_flag,                    // window open/close flag
  59.               scroll_flag;                  // scrolling enabled flag
  60.         boxes border_flag;                  // border type
  61.     };
  62. //
  63. //  Globals
  64. //
  65. int     max_lines = 25;                     // max lines on screen
  66. Window *last_window;                        // last window pointer
  67. /* ******************************************************************** *
  68.  *
  69.  *  Window -- define window instance
  70.  *
  71.  * ******************************************************************** */
  72. Window::Window(char ul_c, char ul_r,        // upper left corner
  73.                char lr_c, char lr_r,        // lower right corner
  74.                char cn,   char cr)          // normal and reverse colors
  75. {
  76. ul_col = ul_c;                              // save window coordinates
  77. ul_row = ul_r;                              // ..row and column
  78. lr_col = lr_c;                              // ..for upper left
  79. lr_row = lr_r;                              // ..and lower right
  80. cn_color = cn;                              // save user colors
  81. cr_color = cr;                              // ..for later
  82. cursor_col = cursor_row = 1;                // init cursor column and row
  83. open_flag = 0;                              // clear open flags
  84. old_data = new char[(((lr_c - ul_c) + 1)    // get work buffer
  85.             * ((lr_r - ul_r) + 1)) * 2];    // ..for old screen image
  86. }
  87. /* ******************************************************************** *
  88.  *
  89.  *  Open -- open a window
  90.  *
  91.  * ******************************************************************** */
  92. void    Window::Open(boxes box)             // border flag
  93. {
  94. int     i;                                  // loop control
  95. struct  box_characters *b;                  // box characters
  96. if (open_flag)                              // q. window already opened?
  97.     return;                                 // a. yes .. just return
  98. border_flag = box;                          // set border flag
  99. open_flag = 1;                              // show window opened
  100. gettext(ul_col, ul_row, lr_col, lr_row,     // capture old screen data
  101.             old_data);                      // ..to temp buffer
  102. window(ul_col, ul_row, lr_col, lr_row);     // make window active
  103. textcolor(FG(cn_color));                    // set up foreground
  104. textbackground(BG(cn_color));               // ..and background colors
  105. clrscr();                                   // clear window
  106. scroll_flag = _wscroll;                     // ..and save scroll setting
  107. if (box != none)                            // q. border requested?
  108.     {                                       // a. yes .. draw the box
  109.     b = &box_chars[box];                    // get line drawing group
  110.     _wscroll = 0;                           // disable scrolling
  111.     gotoxy(1, 1);                           // goto upper left corner
  112.     cprintf("%c", b->ul_char);              // put out first corner
  113.     for (i = 1; i < (lr_col - ul_col); i++) // build top of box..
  114.         cprintf("%c", b->top_char);         // ..with horizontals
  115.     cprintf("%c", b->ur_char);              // ..and upper right corner
  116.     gotoxy(1, (lr_row - ul_row) + 1);       // goto lower left corner
  117.     cprintf("%c", b->ll_char);              // put out bottom corner
  118.     for (i = 1; i < (lr_col - ul_col); i++) // build bottom of box
  119.         cprintf("%c", b->top_char);         // ..with horizontals
  120.     cprintf("%c", b->lr_char);              // ..and lower right corner
  121.     for (i = 2; i <= (lr_row - ul_row); i++)// put the sides on the box
  122.         {
  123.         gotoxy(1, i);                       // jump to left side of box
  124.         cprintf("%c", b->side_char);        // ..and draw a chunk
  125.         gotoxy((lr_col - ul_col) + 1, i);   // ..then jump to right side
  126.         cprintf("%c", b->side_char);        // ..of the box and draw
  127.         }
  128.     _wscroll = scroll_flag;                 // restore scrolling mode
  129.     }
  130. }
  131. /* ******************************************************************** *
  132.  *
  133.  *  AtSay -- display string at position
  134.  *
  135.  * ******************************************************************** */
  136. void    Window::AtSay(int c, int r,         // column and row to
  137.               char *s)                      // display string
  138. {
  139. GotoXY(c, r);                               // set up at the right place
  140. cprintf("%s", s);                           // display string in window
  141. cursor_col = wherex();                      // save cursor column..
  142. cursor_row = wherey();                      // ..and cursor row
  143. }
  144. /* ******************************************************************** *
  145.  *
  146.  *  AtSayReverse -- display string at position in reverse video
  147.  *
  148.  * ******************************************************************** */
  149. void    Window::AtSayReverse(int c, int r,  // column and row to
  150.               char *s)                      // display string
  151. {
  152. GotoXY(c, r);                               // set up at the right place
  153. textcolor(FG(cr_color));                    // set up foreground
  154. textbackground(BG(cr_color));               // ..and background colors
  155. cprintf("%s", s);                           // display string in window
  156. cursor_col = wherex();                      // save cursor column..
  157. cursor_row = wherey();                      // ..and cursor row
  158. textcolor(FG(cn_color));                    // then set colors back to
  159. textbackground(BG(cn_color));               // ..their normal settings
  160. }
  161. /* ******************************************************************** *
  162.  *
  163.  *  Display -- display a character in a window
  164.  *
  165.  * ******************************************************************** */
  166. void    Window::Display(char c)             // character to display
  167. {
  168. MakeCurrent();                              // make this window current
  169. cprintf("%c", c);                           // display string in window
  170. cursor_col = wherex();                      // save cursor column..
  171. cursor_row = wherey();                      // ..and cursor row
  172. }
  173. /* ******************************************************************** *
  174.  *
  175.  *  Display -- display string in window
  176.  *
  177.  * ******************************************************************** */
  178. void    Window::Display(char *s)            // string to display
  179. {
  180. MakeCurrent();                              // make this window current
  181. cprintf("%s", s);                           // display string in window
  182. cursor_col = wherex();                      // save cursor column..
  183. cursor_row = wherey();                      // ..and cursor row
  184. }
  185. /* ******************************************************************** *
  186.  *
  187.  *  DisplayReverse -- display string in reverse video
  188.  *
  189.  * ******************************************************************** */
  190. void    Window::DisplayReverse(char *s)     // string to display
  191. {
  192. MakeCurrent();                              // make this window current
  193. textcolor(FG(cr_color));                    // set up foreground
  194. textbackground(BG(cr_color));               // ..and background colors
  195. cprintf("%s", s);                           // display string in window
  196. cursor_col = wherex();                      // save cursor column..
  197. cursor_row = wherey();                      // ..and cursor row
  198. textcolor(FG(cn_color));                    // then set colors back to
  199. textbackground(BG(cn_color));               // ..their normal settings
  200. }
  201. /* ******************************************************************** *
  202.  *
  203.  *  Clear -- clear current window
  204.  *
  205.  * ******************************************************************** */
  206. void    Window::Clear(void)
  207. {
  208. MakeCurrent();                              // make this window current
  209. clrscr();                                   // ..then clear it
  210. cursor_col = wherex();                      // save cursor column..
  211. cursor_row = wherey();                      // ..and cursor row
  212. }
  213. /* ******************************************************************** *
  214.  *
  215.  *  GotoXY -- position cursor in window
  216.  *
  217.  * ******************************************************************** */
  218. void    Window::GotoXY(int c, int r)        // column and row
  219. {
  220. MakeCurrent();                              // make this window current
  221. gotoxy(c, r);                               // goto requested location
  222. cursor_col = wherex();                      // save cursor column..
  223. cursor_row = wherey();                      // ..and cursor row
  224. }
  225. /* ******************************************************************** *
  226.  *
  227.  *  Close -- close window and restore screen
  228.  *
  229.  * ******************************************************************** */
  230. void    Window::Close(void)
  231. {
  232. if (NOT open_flag)                          // q. window already closed?
  233.     return;                                 // a. yes .. just return
  234. open_flag = 0;                              // clear opened flag
  235. puttext(ul_col, ul_row, lr_col, lr_row,     // restore old screen data
  236.             old_data);                      // ..from temp buffer
  237. }
  238. /* ******************************************************************** *
  239.  *
  240.  *  ~Window -- destructor
  241.  *
  242.  * ******************************************************************** */
  243. Window::~Window()
  244. {
  245. if (open_flag)                              // q. window still open?
  246.     Close();                                // a. yes .. close window
  247. last_window = 0;                            // clear window pointer
  248. delete old_data;                            // de-allocate screen buffer
  249. window(1, 1, 80, max_lines);                // set whole screen as window
  250. }
  251. /* ******************************************************************** *
  252.  *
  253.  *  MakeCurrent -- make this window current
  254.  *
  255.  * ******************************************************************** */
  256. void    Window::MakeCurrent(void)
  257. {
  258. if (last_window != this)                    // q. same window?
  259.     {
  260.     last_window = this;                     // a. no .. use this window
  261.     _wscroll = scroll_flag;                 // ..and set up scroll flag
  262.     if (border_flag == none)                // q. any border?
  263.         window(ul_col, ul_row,              // a. no .. set up window
  264.                 lr_col, lr_row);            // ..using entire area
  265.      else
  266.         window(ul_col + 1, ul_row + 1,      // else .. set up the window
  267.                 lr_col - 1, lr_row - 1);    // ..allowing for the border
  268.     gotoxy(cursor_col, cursor_row);         // ..and re-place cursor
  269.     textcolor(FG(cn_color));                // ..and set up foreground
  270.     textbackground(BG(cn_color));           // ..and background colors
  271.     }
  272. }