home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / SCBOX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  5.9 KB  |  183 lines

  1. /**
  2. *
  3. * Name        scbox -- Draw a box on the current display page
  4. *             using character graphics
  5. *
  6. * Synopsis    ercode = scbox(u_row,u_col,l_row,l_col,
  7. *                 boxtype,boxchar,attrib);
  8. *        int  ercode      Error code:  0 if okay, 1 if box
  9. *                  dimensions are invalid.
  10. *        int  u_row,u_col  Upper left corner of window.
  11. *        int  l_row,l_col  Lower right corner of window.
  12. *        int  boxtype      Nature of characters to be used for
  13. *                  the box:  -1 if boxchar to be used,
  14. *                  0 to 15 if IBM box characters to be
  15. *                  used (see below).
  16. *        char boxchar      Character to be used for drawing the
  17. *                  box if boxtype is -1.
  18. *        int  attrib      The attribute to be used on the vacant
  19. *                  lines.  It has the background attribute
  20. *                  in the high order four bits of the low
  21. *                  order byte, and the foreground in the
  22. *                  low order nybble.
  23. *
  24. * Description    This function draws a box on the current display page.
  25. *
  26. *        If boxtype is -1, then the box will be drawn using the
  27. *        character specified as boxchar.  If boxtype is in the
  28. *        range 0 to 15, then the box will be drawn using the
  29. *        box-drawing characters in the IBM PC character set.  The
  30. *        actual value of boxtype specifies whether the sides of
  31. *        the box are drawn with single or double lines, according
  32. *        to the following table:
  33. *
  34. *               Box Type       Bottom  Right   Top    Left
  35. *               ------------------------------------------
  36. *            0   (0000)     S    S      S      S
  37. *            1   (0001)     S    S      S      D
  38. *            2   (0010)     S    S      D      S
  39. *            3   (0011)     S    S      D      D
  40. *            4   (0100)     S    D      S      S
  41. *            5   (0101)     S    D      S      D
  42. *            6   (0110)     S    D      D      S
  43. *            7   (0111)     S    D      D      D
  44. *            8   (1000)     D    S      S      S
  45. *            9   (1001)     D    S      S      D
  46. *               10   (1010)     D    S      D      S
  47. *               11   (1011)     D    S      D      D
  48. *               12   (1100)     D    D      S      S
  49. *               13   (1101)     D    D      S      D
  50. *               14   (1110)     D    D      D      S
  51. *               15   (1111)     D    D      D      D
  52. *
  53. *        If a box type is chosen from this table, corner
  54. *        characters will be automatically chosen to match the
  55. *        sides.
  56. *
  57. *        The box dimensions are considered invalid if any corner
  58. *        lies beyond the screen; or if the height or width of the
  59. *        box is less than two characters; or if the upper left
  60. *        corner (u_row,u_col) is below or to the right of the
  61. *        lower right corner (l_row,l_col).  If this occurs then
  62. *        1 is returned as the value of the function.
  63. *
  64. * Returns    ercode        Error code:  0 if okay,
  65. *                         1 if box dimensions are invalid.
  66. *
  67. * Version    6.00 (C)Copyright Blaise Computing Inc.  1985,1987,1989
  68. *
  69. **/
  70.  
  71. #include <dos.h>
  72.  
  73. #include <bscreens.h>
  74.  
  75.     /* Macro to write a single character at a given point on the      */
  76.     /* screen                                  */
  77.  
  78. #define  write_one(row,col,ch,fore,back)      \
  79.      {                      \
  80.         sccurset(row,col);              \
  81.         scattrib(fore,back,ch,1);          \
  82.      }
  83.  
  84.     /* Macro to write more than one character at a given point on the */
  85.     /* screen                                  */
  86.  
  87. #define  write_some(row,col,ch,fore,back,number)  \
  88.      {                      \
  89.         if (number)               \
  90.         {                      \
  91.         sccurset(row,col);          \
  92.         scattrib(fore,back,ch,number);      \
  93.         }                      \
  94.      }
  95.  
  96. int scbox(u_row,u_col,l_row,l_col,boxtype,boxchar,attrib)
  97. int  u_row,u_col,l_row,l_col,boxtype;
  98. char boxchar;
  99. int  attrib;
  100. {
  101.     int  mode,act_page,columns;       /* Returned by SCMODE          */
  102.     int  save_row,save_col,high,low;
  103.     int  row,width,fore,back;
  104.     char topleft,topright,botleft,botright,
  105.      top,bottom,left,right;       /* Characters to write          */
  106.  
  107.     /* Tables of box characters                       */
  108.  
  109.     static char upleft[]          /* Top left corners          */
  110.                = {'\332','\326','\325','\311'},
  111.         upright[]          /* Top right corners          */
  112.                = {'\277','\270','\267','\273'},
  113.         lowleft[]          /* Bottom left corners          */
  114.                = {'\300','\323','\324','\310'},
  115.         lowright[]          /* Bottom right corners          */
  116.                = {'\331','\275','\276','\274'};
  117.  
  118.     static char horiz[]           /* Horizontal lines          */
  119.                = {'\304','\315'},
  120.         vert[]              /* Vertical lines           */
  121.                = {'\263','\272'};
  122.  
  123.     /* Validate the box position and dimensions               */
  124.  
  125.     scmode(&mode,&columns,&act_page);
  126.     if (     0 >  u_row
  127.       || u_row >= l_row
  128.       || l_row >= scrows()
  129.       ||     0 >  u_col
  130.       || u_col >= l_col
  131.       || l_col >= columns     )
  132.     return 1;
  133.  
  134.     width = l_col - u_col - 1;          /* Distance between the sides   */
  135.  
  136.     if ((mode > 3) && (mode != 7))    /* Force attribute for graphics */
  137.     attrib = min(3,max(0,attrib));
  138.     fore = utlonyb(attrib);
  139.     back = uthinyb(attrib);
  140.  
  141.     /* Look up the box characters from the tables              */
  142.  
  143.     if (boxtype < 0)
  144.     topleft  = topright = botleft = botright
  145.          = left     = right   = top     = bottom   = boxchar;
  146.     else
  147.     {
  148.     topleft  = upleft  [  boxtype        & 3];
  149.     topright = upright [ (boxtype >> 1) & 3];
  150.     botleft  = lowleft [((boxtype >> 2) & 2) | (boxtype & 1)];
  151.     botright = lowright[ (boxtype >> 2) & 3];
  152.  
  153.     top     = horiz[(boxtype >> 1) & 1];
  154.     bottom     = horiz[(boxtype >> 3) & 1];
  155.     left     = vert [ boxtype    & 1];
  156.     right     = vert [(boxtype >> 2) & 1];
  157.     }
  158.  
  159.     /* Draw the box, starting from upper left corner, working          */
  160.     /* horizontally.                              */
  161.  
  162.                       /* Save cursor location          */
  163.     sccurst(&save_row,&save_col,&high,&low);
  164.  
  165.     write_one (u_row,u_col,     topleft, fore,back);
  166.     write_some(u_row,(u_col + 1),top,      fore,back,width);
  167.     write_one (u_row,l_col,     topright,fore,back);
  168.  
  169.     for (row = u_row + 1; row < l_row; row++)
  170.     {
  171.     write_one(row,u_col,left, fore,back);        /* Left side      */
  172.     write_one(row,l_col,right,fore,back);        /* Right side     */
  173.     }
  174.  
  175.     write_one (l_row,u_col,     botleft, fore,back);
  176.     write_some(l_row,(u_col + 1),bottom,  fore,back,width);
  177.     write_one (l_row,l_col,     botright,fore,back);
  178.  
  179.     sccurset(save_row,save_col);      /* Restore cursor location      */
  180.  
  181.     return 0;
  182. }
  183.