home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / cursesp.zip / UPDATE.C < prev    next >
C/C++ Source or Header  |  1991-12-03  |  6KB  |  178 lines

  1. /****************************************************************/
  2. /* Doupdate() routine of the PCcurses package                   */
  3. /*                                                              */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,  */
  8. /* and therefore consider myself free to make it public domain. */
  9. /*              Bjorn Larsson (...mcvax!enea!infovax!bl)        */
  10. /****************************************************************/
  11. /* 1.0: Release:                                        870515  */
  12. /* 1.2: Changed call sequence to cursesio.[c,asm], Thanks       */
  13. /*      to S. Creps. Rcsid[] string for maintenance:    881002  */
  14. /* 1.3: MSC -W3, Turbo'C' -w -w-pro checkes:            881005  */
  15. /****************************************************************/
  16.  
  17. #include <curses.h>
  18. #include <curspriv.h>
  19.  
  20. static void clrupdate(WINDOW*);         /* fwd declaration */
  21. static bool transformline(int);
  22. static void clearscreen(void);
  23. static void gotoxy(int,int);
  24. static void Putchar(int);
  25.  
  26. char _curses_update_rcsid[] = "@(#)update.c v1.3 - 881005";
  27.  
  28. static WINDOW   *twin;                  /* used by many routines */
  29.  
  30. /****************************************************************/
  31. /* Doupdate() updates the physical screen to look like _curs-   */
  32. /* var.tmpwin if curscr is not 'Clear-marked'. Otherwise it    */
  33. /* updates the screen to look like curscr.            */
  34. /****************************************************************/
  35.  
  36. void doupdate(void)
  37.   {
  38.   int     i;
  39.  
  40.   twin   = _cursvar.tmpwin;
  41.   if (curscr->_clear)
  42.     clrupdate(curscr);
  43.   else
  44.     {
  45.     if (twin->_clear)
  46.       clrupdate(twin);
  47.     else
  48.       {
  49.       for (i=0; i < LINES; i++)
  50.     if (twin->_minchng[i] != _NO_CHANGE)
  51.       if (transformline(i))
  52.         break;
  53.       } /* else */
  54.     } /* else */
  55.   curscr->_curx = twin->_curx;
  56.   curscr->_cury = twin->_cury;
  57.   gotoxy(curscr->_cury, curscr->_curx);
  58.   } /* doupdate */
  59.  
  60. /****************************************************************/
  61. /* Clrupdate(scr) updates the screen by clearing it and then    */
  62. /* redraw it in it's entirety. If _cursvar.refrbrk is TRUE, and    */
  63. /* there is pending input characters, the update will be pre-    */
  64. /* maturely terminated.                        */
  65. /****************************************************************/
  66.  
  67. static void clrupdate(WINDOW *scr)
  68.   {
  69.   int        *src;
  70.   int        *dst;
  71.   int         i;
  72.   int         j;
  73.   WINDOW    *w;
  74.  
  75.   w = curscr;
  76.  
  77.   if (scr != w)                /* copy scr to curscr */
  78.     {
  79.     for (i=0; i < LINES; i++)
  80.       {
  81.       src = scr->_line[i];
  82.       dst = w->_line[i];
  83.       for (j=0; j < COLS; j++)
  84.     *dst++ = *src++;
  85.       } /* for */
  86.     } /* if */
  87.   clearscreen();            /* clear physical screen */
  88.   scr->_clear = FALSE;
  89.   for (i=0; i < LINES; i++)        /* update physical screen */
  90.     {
  91.     src = w->_line[i];
  92.     for(j=0; j < COLS; j++)
  93.       {
  94.       if (*src != (' ' | ATR_NRM))
  95.     {
  96.     gotoxy(i,j);
  97.     Putchar(*src);
  98.     } /* if */
  99.       src++;
  100.       } /* for */
  101.     if(_cursvar.refrbrk && _cursespendch())
  102.       return;
  103.     } /* for */
  104.   } /* clrupdate */
  105.  
  106. /****************************************************************/
  107. /* Transformline() updates the given physical line to look    */
  108. /* like the corresponding line in _cursvar.tmpwin. Transform-    */
  109. /* line returns 1 if premature refresh end is allowed, and    */
  110. /* there is an input character pending.                */
  111. /****************************************************************/
  112.  
  113. static bool transformline(int lineno)
  114.   {
  115.   int        *dstp;
  116.   int        *srcp;
  117.   int         x;
  118.   int         endx;
  119.  
  120.   x    = twin->_minchng[lineno];
  121.   endx = twin->_maxchng[lineno];
  122.   dstp = curscr->_line[lineno] + x;
  123.   srcp = twin->_line[lineno] + x;
  124.   
  125.   for( ; x <= endx; x++)
  126.     {
  127.     if(*dstp != *srcp)
  128.       {
  129.       gotoxy(lineno,x);
  130.       Putchar(*srcp);
  131.       } /* if */
  132.     *dstp++ = *srcp++;
  133.     } /* for */
  134.   twin->_minchng[lineno] = _NO_CHANGE;
  135.   twin->_maxchng[lineno] = _NO_CHANGE;
  136.   return ((bool)(_cursvar.refrbrk && _cursespendch()));
  137.   } /* transformline */
  138.  
  139. /****************************************************************/
  140. /* Clearscreen() clears the physical screen and puts the cursor    */
  141. /* in the home position.                    */
  142. /****************************************************************/
  143.  
  144. static void clearscreen(void)
  145.   {
  146.   _cursesscroll(0,0,LINES-1,COLS-1,0,0x07);
  147.   _cursescursor(0,0);
  148.   _cursvar.cursrow = 0;
  149.   _cursvar.curscol = 0;
  150.   } /* clearscreen */
  151.  
  152. /****************************************************************/
  153. /* Gotoxy() moves the physical cursor to the desired address on    */
  154. /* the screen. We don't optimize here - on a PC, it takes more    */
  155. /* time to optimize than to do things directly.            */
  156. /****************************************************************/
  157.  
  158. static void gotoxy(int row, int col)
  159.   {
  160.   if((_cursvar.cursrow == row) && (_cursvar.curscol == col))
  161.     return;
  162.   _cursescursor(row,col);
  163.   _cursvar.cursrow = row;
  164.   _cursvar.curscol = col;
  165.   } /* gotoxy */
  166.  
  167. /****************************************************************/
  168. /* Putchar() writes a character, with attributes, to the physi-    */
  169. /* cal screen, but avoids writing to the lower right screen    */
  170. /* position.                            */
  171. /****************************************************************/
  172.  
  173. static void Putchar(int ch)
  174.   {
  175.   if ((_cursvar.cursrow < _LINES) || (_cursvar.curscol < _COLS))
  176.     _cursescattr(ch, (ch>>8) & 0xff);
  177.   } /* Putchar */
  178.