home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctech / hlsrc / hlwin.c < prev    next >
C/C++ Source or Header  |  1988-09-09  |  6KB  |  226 lines

  1. /*+
  2.     Name:    hlwin.c
  3.     Date:    06-Jun-1988
  4.     Author:    Kent J. Quirk
  5.         (c) Copyright 1988 Ziff Communications Co.
  6.     Abstract:    Contains window open/close functions.
  7.     History:    09-Sep-88   kjq     Version 1.00
  8. -*/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <graph.h>
  15. #include "hl.h"
  16. #include "hltimes.h"
  17. #include "winmenu.h"
  18.  
  19. unsigned int scrn_seg = 0xB800;         /* color */
  20. int scrn_width = 160;                   /* bytes */
  21.  
  22. /**** o p e n _ w i n d o w ****
  23.     Abstract:    Opens a window on the screen, saving the data under it.
  24.     Parameters: The 1-based coordinates of the corners of the window,
  25.         the attribute with which to fill the window.
  26.     Returns:    A pointer to the window object created.
  27.     Comments:    Would be nice to have automatic boxes drawing.
  28. ****************************/
  29. WINDOW *open_window(short r1, short c1, short r2, short c2, char attr)
  30. {
  31.     int y, linewid;
  32.     unsigned int buf_off, buf_seg;
  33.     char far *bp;
  34.     char far *blankline;
  35.     struct videoconfig config;
  36.     WINDOW *w;
  37.  
  38.     _getvideoconfig(&config);
  39.     if ((config.mode == _TEXTMONO) || (config.mode == _HERCMONO))
  40.     {
  41.     scrn_seg = 0xB000;
  42.     if ((attr & 0xF0) == 0)
  43.         attr = 0x07;
  44.     else
  45.         attr = 0x70;
  46.     }
  47.  
  48.     if ((w = malloc(sizeof(WINDOW))) == NULL)
  49.     return(NULL);
  50.  
  51.     linewid = (c2-c1+1)*2;
  52.     w->bufsize = (r2-r1+1) * linewid;         /* how big is rectangle? */
  53.     if ((w->scrnbuf = malloc(w->bufsize)) == NULL)  /* alloc a buf for it */
  54.     {
  55.     free(w);
  56.     return(NULL);
  57.     }
  58.     if ((blankline = (char far *)malloc(linewid)) == NULL)
  59.     {
  60.     free(w->scrnbuf);
  61.     free(w);
  62.     return(NULL);
  63.     }
  64.  
  65.     for (y=0; y<linewid; y+=2)
  66.     {
  67.     blankline[y] = ' ';
  68.     blankline[y+1] = attr;
  69.     }
  70.  
  71.     w->oldcpos = _gettextposition();        /* where is cursor now? */
  72.     w->r1 = r1;
  73.     w->c1 = c1;
  74.     w->r2 = r2;
  75.     w->c2 = c2;
  76.     w->attr = attr;
  77.     w->x = 'X';             /* bug detector */
  78.     w->cpos.row = w->cpos.col = 1;
  79.     bp = (char far *)(w->scrnbuf);
  80.  
  81.     for (y=r1-1; y<=r2-1; y++)
  82.     {
  83.     movedata(scrn_seg, y*scrn_width+(c1-1)*2,
  84.         FP_SEG(bp), FP_OFF(bp), linewid);
  85.     movedata(FP_SEG(blankline), FP_OFF(blankline),
  86.         scrn_seg, y*scrn_width+(c1-1)*2, linewid);
  87.     bp += linewid;
  88.     }
  89.     free((char near *)blankline);
  90.     return(w);
  91. }
  92.  
  93. /**** c l o s e _ w i n d o w ****
  94.     Abstract:    Given a window, this deallocates the data structure and
  95.         removes it from the screen, restoring the previous screen
  96.         contents.
  97.     Parameters: The window pointer
  98.     Returns:    Zero.
  99.     Comments:    A check is made for "validity" of the window pointer. This
  100.         prevents spectacular crashes if it's not valid, since if
  101.         it isn't, close_window tries to restore random data to the
  102.         video screen area.
  103.         Unfortunately, we can't print much diagnostic info, because
  104.         we just don't have any available.
  105. ****************************/
  106. int close_window(WINDOW *w)
  107. {
  108.     int y, linewid;
  109.     char far *bp;
  110.  
  111.     if (w->x != 'X')
  112.     {
  113.     _clearscreen(_GCLEARSCREEN);
  114.     printf("Internal error in close_window(%p).\n", w);
  115.     printf("Press any key.\n");
  116.     exit(1);
  117.     }
  118.     linewid = (w->c2 - w->c1 + 1)*2;
  119.     bp = (char far *)w->scrnbuf;
  120.  
  121.     for (y=w->r1-1; y <= w->r2-1; y++)
  122.     {
  123.     movedata(FP_SEG(bp), FP_OFF(bp),
  124.         scrn_seg, y*scrn_width + (w->c1 - 1) * 2, linewid);
  125.     bp += linewid;
  126.     }
  127.     _settextposition(w->oldcpos.row, w->oldcpos.col);
  128.     free(w->scrnbuf);
  129.     free(w);
  130.     return(0);
  131. }
  132.  
  133. /**** d e a c t i v a t e _ w i n d o w ****
  134.     Abstract:    Given a window, this remembers the current attribute and
  135.         cursor position and sets those values.
  136.     Parameters: The window pointer.
  137.     Returns:    nothing
  138.     Comments:    Not useful in an overlaid environment, but tiled windows
  139.         work fine with this.
  140. ****************************/
  141. void deactivate_window(WINDOW *w)
  142. {
  143.     w->attr = 0;
  144.     w->attr |= 0x0F & _gettextcolor();
  145.     w->attr |= 0xF0 & ((int)_getbkcolor() << 4);
  146.     w->cpos = _gettextposition();
  147. }
  148.  
  149. /**** a c t i v a t e _ w i n d o w ****
  150.     Abstract:    Restores the cursor and attributes created by the
  151.         deactivate_window() function.
  152.     Parameters: The window
  153.     Returns:    Nothing
  154. ****************************/
  155. void activate_window(WINDOW *w)
  156. {
  157.     _settextwindow(w->r1, w->c1, w->r2, w->c2);
  158.     _settextcolor(w->attr & 0x0F);
  159.     _setbkcolor((long)((w->attr & 0xF0) >> 4));
  160.     _settextposition(w->cpos.row, w->cpos.col);
  161.     _wrapon(_GWRAPOFF);
  162. }
  163.  
  164. /**** f i l l _ s c r e e n ****
  165.     Abstract:    Given an open file containing a screen image, this copies
  166.         the file data to the screen.
  167.     Parameters: The file pointer
  168.     Returns:    Nothing
  169. ****************************/
  170. void fill_screen(FILE *f)
  171. {
  172.     char *buf;
  173.     char far *bp;
  174.     struct videoconfig config;
  175.     int size;
  176.     /* struct SREGS segs; */
  177.  
  178.     _getvideoconfig(&config);
  179.     size = config.numtextrows * config.numtextcols * 2;
  180.     /* segread(&segs); */
  181.     if ((buf = malloc(size)) == NULL)
  182.     return;
  183.     
  184.     fread(buf, size, 1, f);
  185.     bp = (char far *)buf;
  186.     movedata(FP_SEG(bp), FP_OFF(bp), scrn_seg, 0, size);
  187.     free(buf);
  188. }
  189.  
  190. /**** b i o s _ g e t c u r ****
  191.     Abstract:    Gets the current cursor position from bios.
  192.     Parameters: Nothing
  193.     Returns:    An rccoord struct with the cursor position set.
  194. ****************************/
  195. struct rccoord bios_getcur()
  196. {
  197.     struct rccoord cp;
  198.     union REGS regs;
  199.  
  200.     regs.h.ah = 3;
  201.     regs.h.bh = 0;
  202.     int86(0x10, ®s, ®s);
  203.     cp.row = regs.h.dh;
  204.     cp.col = regs.h.dl;
  205.     return(cp);
  206. }
  207.  
  208. /**** b i o s _ s e t c u r ****
  209.     Abstract:    Sets the cursor position using bios.
  210.     Parameters: The row and column cursor positions
  211.     Returns:    The previous cursor position as found by bios_getcur().
  212. ****************************/
  213. struct rccoord bios_setcur(short r, short c)
  214. {
  215.     struct rccoord cp;
  216.     union REGS regs;
  217.  
  218.     cp = bios_getcur();
  219.     regs.h.ah = 2;
  220.     regs.h.bh = 0;
  221.     regs.h.dh = (unsigned char)r;
  222.     regs.h.dl = (unsigned char)c;
  223.     int86(0x10, ®s, ®s);
  224.     return(cp);
  225. }
  226.