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

通讯/手机编程

开发平台:

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. //
  15. //  MSC specific defines
  16. //
  17. #define BLACK           0                   // standard colors
  18. #define BLUE            1
  19. #define GREEN           2
  20. #define CYAN            3
  21. #define RED             4
  22. #define MAGENTA         5
  23. #define BROWN           6
  24. #define LIGHTGRAY       7
  25. #define DARKGRAY        8
  26. #define LIGHTBLUE       9
  27. #define LIGHTGREEN      10
  28. #define LIGHTCYAN       11
  29. #define LIGHTRED        12
  30. #define LIGHTMAGENTA    13
  31. #define YELLOW          14
  32. #define WHITE           15
  33.                                             // MSC text window defines
  34. #define gotoxy(x,y) _settextposition(y, x)  // cursor position in window
  35. #define clrscr()    _clearscreen(_GWINDOW)  // clear screen
  36. #define textcolor(x)      _settextcolor((short) x)  // set up text and
  37. #define textbackground(x) _setbkcolor(x)            // ..and bkgnd color
  38. #define window(ur,uc,lr,lc) _settextwindow(uc, ur, lc, lr) // window define
  39. //
  40. //  Globals
  41. //
  42. int     _wscroll = 1,                       // global text scroll flag
  43.         cprintf(char *msg, ...);            // message to format/display
  44. enum    boxes                               // line drawing box types
  45.     {
  46.     none = -1,                              // no box
  47.     single_line,                            // single line box
  48.     double_line                             // double line box
  49.     };
  50. struct  box_characters                      // box drawing characters
  51.      {
  52.      char ul_char,                          // upper left corner
  53.           ur_char,                          // upper right corner
  54.           ll_char,                          // lower left corner
  55.           lr_char,                          // lower right corner
  56.           top_char,                         // horizontal line
  57.           side_char;                        // vertical line
  58.      } box_chars[2] =
  59.          {
  60.          { 'xda', 'xbf', 'xc0', 'xd9',  // single line box
  61.            'xc4', 'xb3'},
  62.          { 'xc9', 'xbb', 'xc8', 'xbc',  // double line box
  63.            'xcd', 'xba'}
  64.          };
  65. class Window
  66.     {
  67.     public:
  68.         Window(char ul_c, char ul_r,        // define window, upper left
  69.                char lr_c, char lr_r,        //   lower right,
  70.                char cn,   char cr);         //   normal & reverse colors
  71.         void Open(boxes box = none),        // open window
  72.              AtSay(int c, int r, char *s),  // display string at position
  73.              AtSayReverse(int c, int r,     // display string at position
  74.                char *s),                    //   in reverse video
  75.              Display(char),                 // display a character
  76.              Display(char *s),              // display a string
  77.              DisplayReverse(char *s),       // display string in rev video
  78.              Clear(void),                   // clear window
  79.              GotoXY(int c, int r),          // goto xy location
  80.              MakeCurrent(void),             // make window current
  81.              Close(void);                   // close window
  82.        ~Window();                           // destructor
  83.     private:
  84.         char  ul_col, ul_row,               // window upper left
  85.               lr_col, lr_row,               // ..and lower right
  86.               cursor_col, cursor_row,       // cursor column and row
  87.               cn_color, cr_color,           // norm and reverse colors
  88.              *old_data,                     // overlaid data
  89.               open_flag,                    // window open/close flag
  90.               scroll_flag;                  // scrolling enabled flag
  91.         boxes border_flag;                  // border type
  92.     };
  93. //
  94. //  Globals
  95. //
  96. int     max_lines = 25;                     // max lines on screen
  97. Window *last_window;                        // last window pointer
  98. //
  99. //  Routine Definitions
  100. //
  101. int     wherex(void),                       // get current cursor
  102.         wherey(void);                       // ..x and y locations
  103. /* ******************************************************************** *
  104.  *
  105.  *  Window -- define window instance
  106.  *
  107.  * ******************************************************************** */
  108. Window::Window(char ul_c, char ul_r,        // upper left corner
  109.                char lr_c, char lr_r,        // lower right corner
  110.                char cn,   char cr)          // normal and reverse colors
  111. {
  112. ul_col = ul_c;                              // save window coordinates
  113. ul_row = ul_r;                              // ..row and column
  114. lr_col = lr_c;                              // ..for upper left
  115. lr_row = lr_r;                              // ..and lower right
  116. cn_color = cn;                              // save user colors
  117. cr_color = cr;                              // ..for later
  118. cursor_col = cursor_row = 1;                // init cursor column and row
  119. open_flag = 0;                              // clear open flags
  120. old_data = new char[(((lr_c - ul_c) + 1)    // get work buffer
  121.             * ((lr_r - ul_r) + 1)) * 2];    // ..for old screen image
  122. }
  123. /* ******************************************************************** *
  124.  *
  125.  *  Open -- open a window
  126.  *
  127.  * ******************************************************************** */
  128. void    Window::Open(boxes box)             // border flag
  129. {
  130. int     i;                                  // loop control
  131. struct  box_characters *b;                  // box characters
  132. if (open_flag)                              // q. window already opened?
  133.     return;                                 // a. yes .. just return
  134. border_flag = box;                          // set border flag
  135. open_flag = 1;                              // show window opened
  136. gettext(ul_col, ul_row, lr_col, lr_row,     // capture old screen data
  137.             old_data);                      // ..to temp buffer
  138. window(ul_col, ul_row, lr_col, lr_row);     // make window active
  139. textcolor(FG(cn_color));                    // set up foreground
  140. textbackground(BG(cn_color));               // ..and background colors
  141. clrscr();                                   // clear window
  142. scroll_flag = _wscroll;                     // ..and save scroll setting
  143. if (box != none)                            // q. border requested?
  144.     {                                       // a. yes .. draw the box
  145.     b = &box_chars[box];                    // get line drawing group
  146.     _wrapon(_GWRAPOFF);                     // disable text scrolling
  147.     gotoxy(1, 1);                           // goto upper left corner
  148.     cprintf("%c", b->ul_char);              // put out first corner
  149.     for (i = 1; i < (lr_col - ul_col); i++) // build top of box..
  150.         cprintf("%c", b->top_char);         // ..with horizontals
  151.     cprintf("%c", b->ur_char);              // ..and upper right corner
  152.     gotoxy(1, (lr_row - ul_row) + 1);       // goto lower left corner
  153.     cprintf("%c", b->ll_char);              // put out bottom corner
  154.     for (i = 1; i < (lr_col - ul_col); i++) // build bottom of box
  155.         cprintf("%c", b->top_char);         // ..with horizontals
  156.     cprintf("%c", b->lr_char);              // ..and lower right corner
  157.     for (i = 2; i <= (lr_row - ul_row); i++)// put the sides on the box
  158.         {
  159.         gotoxy(1, i);                       // jump to left side of box
  160.         cprintf("%c", b->side_char);        // ..and draw a chunk
  161.         gotoxy((lr_col - ul_col) + 1, i);   // ..then jump to right side
  162.         cprintf("%c", b->side_char);        // ..of the box and draw
  163.         }
  164.     }
  165. if (scroll_flag)                            // q. user want scrolling?
  166.     _wrapon(_GWRAPON);                      // a. yes .. enable scrolling
  167. }
  168. /* ******************************************************************** *
  169.  *
  170.  *  AtSay -- display string at position
  171.  *
  172.  * ******************************************************************** */
  173. void    Window::AtSay(int c, int r,         // column and row to
  174.               char *s)                      // display string
  175. {
  176. GotoXY(c, r);                               // set up at the right place
  177. cprintf("%s", s);                           // display string in window
  178. cursor_col = wherex();                      // save cursor column..
  179. cursor_row = wherey();                      // ..and cursor row
  180. }
  181. /* ******************************************************************** *
  182.  *
  183.  *  AtSayReverse -- display string at position in reverse video
  184.  *
  185.  * ******************************************************************** */
  186. void    Window::AtSayReverse(int c, int r,  // column and row to
  187.               char *s)                      // display string
  188. {
  189. GotoXY(c, r);                               // set up at the right place
  190. textcolor(FG(cr_color));                    // set up foreground
  191. textbackground(BG(cr_color));               // ..and background colors
  192. cprintf("%s", s);                           // display string in window
  193. cursor_col = wherex();                      // save cursor column..
  194. cursor_row = wherey();                      // ..and cursor row
  195. textcolor(FG(cn_color));                    // then set colors back to
  196. textbackground(BG(cn_color));               // ..their normal settings
  197. }
  198. /* ******************************************************************** *
  199.  *
  200.  *  Display -- display a character in a window
  201.  *
  202.  * ******************************************************************** */
  203. void    Window::Display(char c)             // character to display
  204. {
  205. MakeCurrent();                              // make this window current
  206. cprintf("%c", c);                           // display string in window
  207. cursor_col = wherex();                      // save cursor column..
  208. cursor_row = wherey();                      // ..and cursor row
  209. }
  210. /* ******************************************************************** *
  211.  *
  212.  *  Display -- display string in window
  213.  *
  214.  * ******************************************************************** */
  215. void    Window::Display(char *s)            // string to display
  216. {
  217. MakeCurrent();                              // make this window current
  218. cprintf("%s", s);                           // display string in window
  219. cursor_col = wherex();                      // save cursor column..
  220. cursor_row = wherey();                      // ..and cursor row
  221. }
  222. /* ******************************************************************** *
  223.  *
  224.  *  DisplayReverse -- display string in reverse video
  225.  *
  226.  * ******************************************************************** */
  227. void    Window::DisplayReverse(char *s)     // string to display
  228. {
  229. MakeCurrent();                              // make this window current
  230. textcolor(FG(cr_color));                    // set up foreground
  231. textbackground(BG(cr_color));               // ..and background colors
  232. cprintf("%s", s);                           // display string in window
  233. cursor_col = wherex();                      // save cursor column..
  234. cursor_row = wherey();                      // ..and cursor row
  235. textcolor(FG(cn_color));                    // then set colors back to
  236. textbackground(BG(cn_color));               // ..their normal settings
  237. }
  238. /* ******************************************************************** *
  239.  *
  240.  *  Clear -- clear current window
  241.  *
  242.  * ******************************************************************** */
  243. void    Window::Clear(void)
  244. {
  245. MakeCurrent();                              // make this window current
  246. clrscr();                                   // ..then clear it
  247. cursor_col = wherex();                      // save cursor column..
  248. cursor_row = wherey();                      // ..and cursor row
  249. }
  250. /* ******************************************************************** *
  251.  *
  252.  *  GotoXY -- position cursor in window
  253.  *
  254.  * ******************************************************************** */
  255. void    Window::GotoXY(int c, int r)        // column and row
  256. {
  257. MakeCurrent();                              // make this window current
  258. gotoxy(c, r);                               // goto requested location
  259. cursor_col = wherex();                      // save cursor column..
  260. cursor_row = wherey();                      // ..and cursor row
  261. }
  262. /* ******************************************************************** *
  263.  *
  264.  *  Close -- close window and restore screen
  265.  *
  266.  * ******************************************************************** */
  267. void    Window::Close(void)
  268. {
  269. if (NOT open_flag)                          // q. window already closed?
  270.     return;                                 // a. yes .. just return
  271. open_flag = 0;                              // clear opened flag
  272. puttext(ul_col, ul_row, lr_col, lr_row,     // restore old screen data
  273.             old_data);                      // ..from temp buffer
  274. }
  275. /* ******************************************************************** *
  276.  *
  277.  *  ~Window -- destructor
  278.  *
  279.  * ******************************************************************** */
  280. Window::~Window()
  281. {
  282. if (open_flag)                              // q. window still open?
  283.     Close();                                // a. yes .. close window
  284. last_window = 0;                            // clear window pointer
  285. delete old_data;                            // de-allocate screen buffer
  286. window(1, 1, 80, max_lines);                // set whole screen as window
  287. }
  288. /* ******************************************************************** *
  289.  *
  290.  *  MakeCurrent -- make this window current
  291.  *
  292.  * ******************************************************************** */
  293. void    Window::MakeCurrent(void)
  294. {
  295. if (last_window != this)                    // q. same window?
  296.     {
  297.     last_window = this;                     // a. no .. use this window
  298.     if (scroll_flag)                        // q. user want scrolling?
  299.         _wrapon(_GWRAPON);                  // a. yes .. enable scrolling
  300.      else
  301.         _wrapon(_GWRAPOFF);                 // else .. disable scrolling
  302.     if (border_flag == none)                // q. any border?
  303.         window(ul_col, ul_row,              // a. no .. set up window
  304.                 lr_col, lr_row);            // ..using entire area
  305.      else
  306.         window(ul_col + 1, ul_row + 1,      // else .. set up the window
  307.                 lr_col - 1, lr_row - 1);    // ..allowing for the border
  308.     gotoxy(cursor_col, cursor_row);         // ..and re-place cursor
  309.     textcolor(FG(cn_color));                // ..and set up foreground
  310.     textbackground(BG(cn_color));           // ..and background colors
  311.     }
  312. }
  313. /* ******************************************************************** *
  314.  *      wherex() -- determine column number                             *
  315.  * ******************************************************************** */
  316. int     wherex(void)
  317. {
  318. struct  rccoord pos;                        // row and col coordinates
  319. pos = _gettextposition();                   // get current cursor ..
  320. return(pos.col);                            // ..and return the column
  321. }
  322. /* ******************************************************************** *
  323.  *      wherey() -- determine row number                                *
  324.  * ******************************************************************** */
  325. int     wherey(void)
  326. {
  327. struct  rccoord pos;                        // row and col coordinates
  328. pos = _gettextposition();                   // get current cursor ..
  329. return(pos.row);                            // ..and return the row nbr
  330. }
  331. /* ******************************************************************** *
  332.  *      cprintf() -- handle outputing text to screen                    *
  333.  * ******************************************************************** */
  334. int     cprintf(char *msg, ...)             // message to format/display
  335. {
  336. char    buf[100];                           // string buffer
  337. va_list list;                               // variable list
  338. va_start(list, msg);                        // set up variable list
  339. vsprintf(buf, msg, list);                   // ..format buffer
  340. _outtext(buf);                              // ..then display message
  341. return(strlen(buf));                        // ..and rtn w/string length
  342. }