home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part03 / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  5.7 KB  |  278 lines

  1. /* Screen interface package
  2.  *
  3.  * Author: Bob Baldwin  June 1983
  4.  * $Date: 86/01/15 16:04:16 $
  5.  * $Log:    screen.c,v $
  6.  * Revision 1.1  86/01/15  16:04:16  baldwin
  7.  * Initial revision
  8.  * 
  9.  * Much code moved to terminal.c  baldwin 10/86
  10.  *
  11.  * Revision 1.2  85/01/10  02:35:10  simsong
  12.  * Changed to termcap support
  13.  * 
  14.  * Revision 1.1  85/01/09  23:45:10  simsong
  15.  * Initial revision
  16.  * 
  17.  */
  18.  
  19.  
  20. /* The screen consists of 24 lines and 80 columns.  The basic screen
  21.  * operation is replacing the character that is already at a given
  22.  * position with a new character.  That is, characters are placed,
  23.  * they are not inserted.
  24.  *
  25.  * Various commands are provided for positioning the cursor, placing
  26.  * strings on the screen, and removing characters.
  27.  *
  28.  * If a routine finds something wrong or inconsistent, it will print
  29.  * an error message on the screen.
  30.  */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <strings.h>
  35. #include "window.h"
  36. #include "terminal.h"
  37. #include "specs.h"
  38.  
  39.  
  40.  
  41. /* These variables contain the current location of the cursor.
  42.  * The origin, or home, of the  cursor is in the upper lefthand
  43.  * corner of the screen.  That location is line 1, column 1.
  44.  * The lower righthand corner of the screen is the location
  45.  * identified by line 24, column 80.
  46.  */
  47.  
  48. int    cline;
  49. int    ccolumn;
  50.  
  51. /* These must be 24 and 80 since only that region of the screen
  52.  * is covered by the windows.  The wl_driver() routine will
  53.  * exit if the cursor moves out of that area.
  54.  */
  55. int    MXLINE = MAXHEIGHT;        /* Max line number */
  56. int    MXCOL = MAXWIDTH;        /* Max column number */
  57.  
  58.  
  59.  
  60. /* Clear Screen.  This should be the first screen operation called in order
  61.  * to initialize the line and column locations.
  62.  */
  63. clrscreen()
  64. {
  65.     enter_mode(SMNORMAL);
  66.     Puts(erase_scr);                /* clear the screen */
  67.     cline = 1;
  68.     ccolumn = 1;
  69. }
  70.  
  71.  
  72. /* Set Cursor Position.  The next character places on the screen
  73.  * will appear in the given line and column.
  74.  *
  75.  * Note: Bob's code is broken in that it assumes the screen goes
  76.  * from 1..24 and 1..80, rather than 0..23 and 0..79. So this
  77.  * routine subtracs one before it calls the curses routine.
  78.  */
  79. setcursor(line, column)
  80. int    line, column;        /* ranges: 1..24 and 1..80 inclusive */
  81. {
  82.     if (line < 1 || line > MXLINE  ||  column < 1 || column > MXCOL)  {
  83.         disperr("setcursor tried to move cursor off screen");
  84.         return;
  85.         }
  86.  
  87.     cline = line;
  88.     ccolumn = column;
  89.     
  90.     enter_mode(SMNORMAL);
  91.     Puts(tgoto(cm,column-1,line-1));
  92. }
  93.  
  94.  
  95. /* Return the row location of the cursor (1 is in upper-left corner).
  96.  */
  97. rowcursor()
  98. {
  99.     return(cline);
  100. }
  101.  
  102.  
  103. /* Return the row location of the cursor (1 is in upper-left corner).
  104.  */
  105. colcursor()
  106. {
  107.     return(ccolumn);
  108. }
  109.  
  110.  
  111.  
  112. /* Get Cursor Position
  113.  * The value returned equals 256*LineNumber + ColumnNumber
  114.  */
  115. int    getcursor()
  116. {
  117.     return((cline<<8)+ccolumn);
  118. }
  119.  
  120.  
  121. /* Jog the Cursor one position
  122.  * The value of dir determines the direction of travel:
  123.  * 1 = up, 2 = down, 3 = left, 4 = right.  A value other than one of those
  124.  * four will cause an error message to be printed.
  125.  */
  126. /* of course, there is a more ellegant way to implement this */
  127.  
  128. jogcursor(dir)
  129. int    dir;
  130. {
  131.     switch(dir) {
  132.         case 1:    cline = (cline<=1) ? 1 : cline-1;
  133.             break;
  134.         case 2:    cline = (cline >= MXLINE) ? MXLINE : cline+1;
  135.             break;
  136.         case 3:    ccolumn = (ccolumn <= 1) ? 1 : ccolumn-1;
  137.             break;
  138.         case 4:    ccolumn = (ccolumn >= MXCOL) ? MXCOL : ccolumn+1;
  139.             break;
  140.         default:
  141.             disperr("jogcursor arg out of range");
  142.             return;
  143.         }
  144.  
  145.     setcursor(cline, ccolumn);
  146. }
  147.  
  148.  
  149. /* Place String on the current line.  The cursor is advanced to the
  150.  * position after the last character in the string, unless we hit the
  151.  * edge of the screen in which case the cursor stays pinned to the
  152.  * edge and doesn't move beyond it.
  153.  */
  154. plstring(s)
  155. char    *s;
  156. {
  157.     for ( ; *s != NULL ; s++)  {
  158.         putsym((*s) & 0377);
  159.     }
  160.     ccolumn += strlen(s);
  161.     if (ccolumn >= MXCOL)
  162.           ccolumn = MXCOL;    /* Assumes no wrap-around. */
  163. }
  164.  
  165.  
  166. /* Place a number of Spaces
  167.  * This routine can also be used to erase characters on the screen by
  168.  * overwriting them with spaces.
  169.  */
  170. plnspaces(n)
  171. int    n;
  172. {    int    i;
  173.     if (n < 0)  {
  174.         disperr("plnspaces: negative arg");
  175.         return;
  176.         }
  177.  
  178.     for (i = 0 ; i < n ; i++)  {
  179.         putsym(' ');
  180.         }
  181.     ccolumn += n;
  182.     if (ccolumn >= MXCOL)
  183.           ccolumn = MXCOL;
  184. }
  185.  
  186.  
  187. /* Place a Number of a given Character
  188.  */
  189. plnchars(n, c)
  190. int    n;
  191. int    c;
  192. {    
  193.     int    i;
  194.     if (n < 0)  {
  195.         disperr("plnchars: negative arg");
  196.         return;
  197.         }
  198.  
  199.     for (i = 0 ; i < n ; i++)  {
  200.         putsym(c);
  201.         }
  202.     ccolumn += n;
  203.     if (ccolumn >= MXCOL)
  204.           ccolumn = MXCOL;
  205. }
  206.  
  207.  
  208. /* Vertical place a Character a Number of times.
  209.  * This routine can be used to draw vertical lines using
  210.  * a given character.  It correctly handles displaying
  211.  * in column 80.
  212.  * The cursor is moved to the position below the last character
  213.  * placed on the screen.  However the cursor will not move below
  214.  * the last line on the screen.
  215.  */
  216. vertnchars(n,c)
  217. int    n;
  218. int    c;
  219. {
  220.     int    i;
  221.     if (n < 0)  {
  222.         disperr("vertnchars: negative arg");
  223.         return;
  224.         }
  225.  
  226.     for (i = 0 ; i < n ; i++)  {
  227.         putsym(c);
  228.         /* Assume cursor motion ok even in graphic mode. */
  229.         setcursor(++cline, ccolumn);
  230.         if (cline >= MXLINE)  {
  231.             cline = MXLINE;
  232.             break;
  233.             }
  234.         }
  235. }
  236.  
  237.  
  238. /* Delete characters after the cursor up until the End Of the Line
  239.  */
  240. deleol()
  241. {
  242.     Puts(erase_eol);
  243. }
  244.  
  245.  
  246. /* Delete characters after the cursor up until the End Of the Screen
  247.  */
  248. deleos()
  249. {
  250.     Puts(erase_eos);
  251. }
  252.  
  253.  
  254. /* Display Error message.  The message is a string that does not end
  255.  * with a \n.
  256.  */
  257. disperr(s)
  258. char    *s;
  259. {    int    sline, scolumn;        /* Saved line and column numbers. */
  260.  
  261.     sline = cline;
  262.     scolumn  = ccolumn;
  263.  
  264. /*    setcursor(1, 1);     avoid bug when screen size unknown. */
  265.     printf("\n%s\n", s);
  266. /*    setcursor(sline, scolumn);  or position not set. */
  267. }
  268.  
  269.  
  270. /* Put a string to stdout without trailing newline.
  271.  */
  272. Puts(s)
  273. char *s;
  274. {
  275.     while(*s)
  276.         putchar(*s++);
  277. }
  278.