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