home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0636.ZIP / CCE_0636 / CURSES / CRSSRC12.ZOO / src / refresh.c < prev    next >
C/C++ Source or Header  |  1991-09-27  |  7KB  |  327 lines

  1. /*
  2.  * Copyright (c) 1981 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)refresh.c    5.3 (Berkeley) 6/30/88";
  20. #endif /* not lint */
  21.  
  22. /*
  23.  * make the current screen look like "win" over the area coverd by
  24.  * win.
  25.  */
  26.  
  27. # include    "curses.ext"
  28. # include <string.h>
  29.  
  30. # ifdef DEBUG
  31. # define    STATIC
  32. # else
  33. # define    STATIC    static
  34. # endif
  35.  
  36. STATIC int makech __PROTO((WINDOW *, short));
  37. STATIC void domvcur __PROTO((int, int, int, int));
  38.  
  39. STATIC short    ly, lx;
  40.  
  41. STATIC bool    curwin;
  42.  
  43. WINDOW    *_win = NULL;
  44.  
  45. int wrefresh(win)
  46. reg WINDOW    *win;
  47. {
  48.     reg short    wy;
  49.     reg int        retval;
  50.  
  51.     /*
  52.      * make sure were in visual state
  53.      */
  54.     if (_endwin) {
  55.         _puts(VS);
  56.         _puts(TI);
  57.         _endwin = FALSE;
  58.     }
  59.  
  60.     /*
  61.      * initialize loop parameters
  62.      */
  63.  
  64.     ly = curscr->_cury;
  65.     lx = curscr->_curx;
  66.     wy = 0;
  67.     _win = win;
  68.     curwin = (win == curscr);
  69.  
  70.     if (win->_clear || curscr->_clear || curwin) {
  71.         if ((win->_flags & _FULLWIN) || curscr->_clear) {
  72.             _puts(CL);
  73.             ly = 0;
  74.             lx = 0;
  75.             if (!curwin) {
  76.                 curscr->_clear = FALSE;
  77.                 curscr->_cury = 0;
  78.                 curscr->_curx = 0;
  79.                 werase(curscr);
  80.             }
  81.             touchwin(win);
  82.         }
  83.         win->_clear = FALSE;
  84.     }
  85.     if (!CA) {
  86.         if (win->_curx != 0)
  87.             _putchar('\n', stdout);
  88.         if (!curwin)
  89.             werase(curscr);
  90.     }
  91. # ifdef DEBUG
  92.     fprintf(outf, "REFRESH(%0.2o): curwin = %d\n", win, curwin);
  93.     fprintf(outf, "REFRESH:\n\tfirstch\tlastch\n");
  94. # endif
  95.     for (wy = 0; wy < win->_maxy; wy++) {
  96. # ifdef DEBUG
  97.         fprintf(outf, "%d\t%d\t%d\n", wy, win->_firstch[wy],
  98.             win->_lastch[wy]);
  99. # endif
  100.         if (win->_firstch[wy] != _NOCHANGE)
  101.             if (makech(win, wy) == ERR)
  102.                 return ERR;
  103.             else {
  104.                 if (win->_firstch[wy] >= win->_ch_off)
  105.                     win->_firstch[wy] = win->_maxx +
  106.                                 win->_ch_off;
  107.                 if (win->_lastch[wy] < win->_maxx +
  108.                                win->_ch_off)
  109.                     win->_lastch[wy] = win->_ch_off;
  110.                 if (win->_lastch[wy] < win->_firstch[wy])
  111.                     win->_firstch[wy] = _NOCHANGE;
  112.             }
  113. # ifdef DEBUG
  114.         fprintf(outf, "\t%d\t%d\n", win->_firstch[wy],
  115.             win->_lastch[wy]);
  116. # endif
  117.     }
  118.  
  119.     if (win == curscr)
  120.         domvcur(ly, lx, win->_cury, win->_curx);
  121.     else {
  122.         if (win->_leave) {
  123.             curscr->_cury = ly;
  124.             curscr->_curx = lx;
  125.             ly -= win->_begy;
  126.             lx -= win->_begx;
  127.             if (ly >= 0 && ly < win->_maxy && lx >= 0 &&
  128.                 lx < win->_maxx) {
  129.                 win->_cury = ly;
  130.                 win->_curx = lx;
  131.             }
  132.             else
  133.                 win->_cury = win->_curx = 0;
  134.         }
  135.         else {
  136.             domvcur(ly, lx, win->_cury + win->_begy,
  137.                 win->_curx + win->_begx);
  138.             curscr->_cury = win->_cury + win->_begy;
  139.             curscr->_curx = win->_curx + win->_begx;
  140.         }
  141.     }
  142.     retval = OK;
  143.  
  144.     _win = NULL;
  145.     fflush(stdout);
  146.     return retval;
  147. }
  148.  
  149. /*
  150.  * make a change on the screen
  151.  */
  152. STATIC
  153. #ifdef __STDC__
  154. int makech(WINDOW *win, short wy)
  155. #else
  156. makech(win, wy)
  157. reg WINDOW    *win;
  158. short        wy;
  159. #endif
  160. {
  161.     reg char    *nsp, *csp, *ce;
  162.     reg short    wx, lch, y;
  163.     reg int        nlsp, clsp;    /* last space in lines        */
  164.  
  165.     wx = win->_firstch[wy] - win->_ch_off;
  166.     if (wx >= win->_maxx)
  167.         return OK;
  168.     else if (wx < 0)
  169.         wx = 0;
  170.     lch = win->_lastch[wy] - win->_ch_off;
  171.     if (lch < 0)
  172.         return OK;
  173.     else if (lch >= win->_maxx)
  174.         lch = win->_maxx - 1;;
  175.     y = wy + win->_begy;
  176.  
  177.     if (curwin)
  178.         csp = " ";
  179.     else
  180.         csp = &curscr->_y[wy + win->_begy][wx + win->_begx];
  181.  
  182.     nsp = &win->_y[wy][wx];
  183.     if (CE && !curwin) {
  184.         for (ce = &win->_y[wy][win->_maxx - 1]; *ce == ' '; ce--)
  185.             if (ce <= win->_y[wy])
  186.                 break;
  187.         nlsp = ce - win->_y[wy];
  188.     }
  189.  
  190.     if (!curwin)
  191.         ce = CE;
  192.     else
  193.         ce = NULL;
  194.  
  195.     while (wx <= lch) {
  196.         if (*nsp != *csp) {
  197.             domvcur(ly, lx, y, wx + win->_begx);
  198. # ifdef DEBUG
  199.             fprintf(outf, "MAKECH: 1: wx = %d, lx = %d\n", wx, lx);
  200. # endif    
  201.             ly = y;
  202.             lx = wx + win->_begx;
  203.             while (*nsp != *csp && wx <= lch) {
  204.                 if (ce != NULL && wx >= nlsp && *nsp == ' ') {
  205.                     /*
  206.                      * check for clear to end-of-line
  207.                      */
  208.                     ce = &curscr->_y[ly][COLS - 1];
  209.                     while (*ce == ' ')
  210.                         if (ce-- <= csp)
  211.                             break;
  212.                     clsp = ce - curscr->_y[ly] - win->_begx;
  213. # ifdef DEBUG
  214.                     fprintf(outf, "MAKECH: clsp = %d, nlsp = %d\n", clsp, nlsp);
  215. # endif
  216.                     if (clsp - nlsp >= (int)strlen(CE)
  217.                         && clsp < win->_maxx) {
  218. # ifdef DEBUG
  219.                         fprintf(outf, "MAKECH: using CE\n");
  220. # endif
  221.                         _puts(CE);
  222.                         lx = wx + win->_begx;
  223.                         while (wx++ <= clsp)
  224.                             *csp++ = ' ';
  225.                         return OK;
  226.                     }
  227.                     ce = NULL;
  228.                 }
  229.                 /*
  230.                  * enter/exit standout mode as appropriate
  231.                  */
  232.                 if (SO && (*nsp&_STANDOUT) != (curscr->_flags&_STANDOUT)) {
  233.                     if (*nsp & _STANDOUT) {
  234.                         _puts(SO);
  235.                         curscr->_flags |= _STANDOUT;
  236.                     }
  237.                     else {
  238.                         _puts(SE);
  239.                         curscr->_flags &= ~_STANDOUT;
  240.                     }
  241.                 }
  242.                 wx++;
  243.                 if (wx >= win->_maxx && wy == win->_maxy - 1)
  244.                     if (win->_scroll) {
  245.                         if ((curscr->_flags&_STANDOUT) &&
  246.                             (win->_flags & _ENDLINE))
  247.                             if (!MS) {
  248.                             _puts(SE);
  249.                             curscr->_flags &= ~_STANDOUT;
  250.                             }
  251.                         if (!curwin)
  252.                         _putchar((*csp = *nsp) & 0177, stdout);
  253.                         else
  254.                         _putchar(*nsp & 0177, stdout);
  255.                         if (win->_flags&_FULLWIN && !curwin)
  256.                         scroll(curscr);
  257.                         ly = win->_begy+win->_cury;
  258.                         lx = win->_begx+win->_curx;
  259.                         return OK;
  260.                     }
  261.                     else if (win->_flags&_SCROLLWIN) {
  262.                         lx = --wx;
  263.                         return ERR;
  264.                     }
  265.                 if (!curwin)
  266.                     _putchar((*csp++ = *nsp) & 0177, stdout);
  267.                 else
  268.                     _putchar(*nsp & 0177, stdout);
  269. # ifdef FULLDEBUG
  270.                 fprintf(outf,
  271.                     "MAKECH:putchar(%c)\n", *nsp & 0177);
  272. # endif
  273.                 if (UC && (*nsp & _STANDOUT)) {
  274.                     _putchar('\b', stdout);
  275.                     _puts(UC);
  276.                 }
  277.                 nsp++;
  278.             }
  279. # ifdef DEBUG
  280.             fprintf(outf, "MAKECH: 2: wx = %d, lx = %d\n", wx, lx);
  281. # endif    
  282.             if (lx == wx + win->_begx)    /* if no change */
  283.                 break;
  284.             lx = wx + win->_begx;
  285.             if (lx >= COLS && AM) {
  286.                 lx = 0;
  287.                 ly++;
  288.                 /*
  289.                  * xn glitch: chomps a newline after auto-wrap.
  290.                  * we just feed it now and forget about it.
  291.                  */
  292.                 if (XN) {
  293.                     _putchar('\n', stdout);
  294.                     _putchar('\r', stdout);
  295.                 }
  296.             }
  297.         }
  298.         else if (wx <= lch)
  299.             while (*nsp == *csp && wx <= lch) {
  300.                 nsp++;
  301.                 if (!curwin)
  302.                     csp++;
  303.                 ++wx;
  304.             }
  305.         else
  306.             break;
  307. # ifdef DEBUG
  308.         fprintf(outf, "MAKECH: 3: wx = %d, lx = %d\n", wx, lx);
  309. # endif    
  310.     }
  311.     return OK;
  312. }
  313.  
  314. /*
  315.  * perform a mvcur, leaving standout mode if necessary
  316.  */
  317. STATIC
  318. void domvcur(oy, ox, ny, nx)
  319. int    oy, ox, ny, nx; {
  320.  
  321.     if (curscr->_flags & _STANDOUT && !MS) {
  322.         _puts(SE);
  323.         curscr->_flags &= ~_STANDOUT;
  324.     }
  325.     mvcur(oy, ox, ny, nx);
  326. }
  327.