home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part02 / windowlib.c < prev   
Encoding:
C/C++ Source or Header  |  1987-06-16  |  4.3 KB  |  216 lines

  1. /*
  2.  * Library of window routines.
  3.  *
  4.  * Robert W. Baldwin, December 1984.
  5.  */
  6.  
  7. #include    <stdio.h>
  8. #include    "window.h"
  9. #include    "specs.h"
  10.  
  11.  
  12. /* The external topktab must be filled in by the application.
  13.  * It specifies the keystroke behavior that is the same in
  14.  * all windows.  For example, the refresh function can be handled here.
  15.  * It should be terminated by a keychar == 0, not -1, so the
  16.  * active window will get a chance to handle the key.
  17.  */
  18. extern    keyer    topktab[];
  19.  
  20.  
  21. /* This external must be a routine that gets and returns
  22.  * a keystroke (integer) without taking any arguments.
  23.  */
  24. extern    key        u_getkey();
  25.  
  26.  
  27.  
  28. /* This is the main loop that runs the window system.
  29.  * It returns if the cursor is not in any of the windows in wtab.
  30.  */
  31. wl_driver(wtab)
  32. gwindow        *wtab[];        /* Ptr to null terminated list of windows. */
  33. {
  34. gwindow        **pw, *w;
  35. int            lrow, lcol;        /* Cursor location relative to window. */
  36. key            k;
  37.  
  38. while (TRUE)  {
  39.     for (pw = wtab ; TRUE ; pw++)  {
  40.         w = *pw;
  41.         if (w == NULL)  return;
  42.         if (wl_hascur(w))  break;
  43.         }
  44.  
  45.     lrow = rowcursor() - w->worg_row + 1;
  46.     lcol = colcursor() - w->worg_col + 1;
  47.     (*(w->wfirst))(w, lrow, lcol);
  48.  
  49.     while (wl_hascur(w))  {
  50.         k = u_getkey();
  51.         if (!ddokey(w, k, topktab)) {
  52.             (*(w->wkey))(w, k);
  53.             }
  54.         }
  55.  
  56.     (*(w->wlast))(w);
  57.     }
  58. }
  59.  
  60.  
  61.  
  62. /* Refresh all windows.
  63.  * Do not move the cursor.
  64.  */
  65. wl_refresh(wtab)
  66. gwindow        *wtab[];        /* Ptr to null terminated list of windows. */
  67. {
  68.     gwindow        **pw, *w;
  69.     int            row, col;        /* Initial global cursor location. */
  70.  
  71.     row = rowcursor();
  72.     col = colcursor();
  73.  
  74.     for (pw = wtab ; TRUE ; pw++)  {
  75.         w = *pw;
  76.         if (w == NULL)  break;
  77.         wl_draw(w);
  78.         }
  79.     setcursor(row, col);
  80. }
  81.  
  82.  
  83.  
  84. /* Restore the cursor the the position saved in the window structure.
  85.  * Can also be used to set the cursor by first setting the cursor coords
  86.  * in the window data structure.
  87.  */
  88. wl_rcursor(w)
  89. gwindow    *w;            /* Pointer to basic window data. */
  90. {
  91.     int    grow,gcol;    /* Global cursor locations. */
  92.  
  93.     grow = w->wcur_row + w->worg_row - 1;
  94.     gcol = w->wcur_col + w->worg_col - 1;
  95.     setcursor(grow,gcol);
  96.     if (!wl_hascur(w))  disperr("wl_rcursor arguments out-of-bounds.");
  97. }
  98.  
  99.  
  100.  
  101. /* Set the cursor to the given coordinates within a window.
  102.  * That is, set it relative to the window's origin.
  103.  * It displays an error if the cursor leaves the window.
  104.  */
  105. wl_setcur(w, row, col)
  106. gwindow    *w;            /* Pointer to basic window data. */
  107. int        row, col;    /* Local coordinates. */
  108. {
  109.     int    grow,gcol;    /* Global cursor locations. */
  110.  
  111.     w->wcur_row = row;
  112.     w->wcur_col = col;
  113.     grow = w->worg_row + row - 1;
  114.     gcol = w->worg_col + col - 1;
  115.     setcursor(grow,gcol);
  116.     if (!wl_hascur(w))  disperr("wl_setcur arguments out-of-bounds.");
  117. }
  118.  
  119.  
  120. /* No-op window routine.
  121.  */
  122. wl_noop()
  123. {
  124. }
  125.  
  126.  
  127.  
  128. /* Return TRUE if the cursor is in the given window.
  129.  */
  130. wl_hascur(w)
  131. gwindow        *w;
  132. {
  133.     int        grow, gcol;        /* Global cursor location. */
  134.     int        lrow, lcol;        /* Cursor location relative to window. */
  135.  
  136.     grow = rowcursor();
  137.     gcol = colcursor();
  138.     lrow = grow - w->worg_row + 1;
  139.     lcol = gcol - w->worg_col + 1;
  140.  
  141.     if (lrow < 1  ||  w->wheight < lrow)  return(FALSE);
  142.     if (lcol < 1  ||  w->wwidth  < lcol)  return(FALSE);
  143.     return(TRUE);
  144. }
  145.  
  146.  
  147.  
  148. /* Generic draw routine.
  149.  */
  150. wl_draw(w)
  151. gwindow    *w;
  152. {
  153.     (*(w->wredraw))(w);
  154. }
  155.  
  156.  
  157. /* Redraw routine that can be used with any twindow.
  158.  * Leaves cursor on the last display line.
  159.  */
  160. wl_twdraw(w)
  161. twindow    *w;
  162. {
  163.     displine    **lines, *line;
  164.  
  165.     for (lines = w->dlines ; (line = *lines) != NULL ; lines++)  {
  166.         (*(line->wredraw))(line);
  167.         }
  168. }
  169.  
  170.  
  171. /* Erase the window by putting spaces of all of it.
  172.  * Leave the cursor at the window's origin.
  173.  */
  174. wl_erase(w)
  175. gwindow    *w;
  176. {
  177.     int    grow, gcol;        /* Global row and column locations. */
  178.     int i;
  179.  
  180.     grow = w->worg_row;
  181.     gcol = w->worg_col;
  182.  
  183.     for (i = 0 ; i < w->wheight ; i++)  {
  184.         setcursor(grow+i, gcol);
  185.         plnspaces(w->wwidth);
  186.         }
  187.     wl_setcur(w, 1, 1);
  188. }
  189.  
  190.  
  191. /* Outline a window without changing its inside.
  192.  * Leave cursor at the origin.
  193.  */
  194. wl_outline(w)
  195. gwindow    *w;
  196. {
  197.     int    grow, gcol;        /* Global row and column locations. */
  198.  
  199.     grow = w->worg_row;
  200.     gcol = w->worg_col;
  201.  
  202.     setcursor(grow, gcol);
  203.     plnchars(w->wwidth, '-');
  204.     setcursor(grow+w->wheight-1, gcol);
  205.     plnchars(w->wwidth, '-');
  206.  
  207.     setcursor(grow, gcol);
  208.     vertnchars(w->wheight, '|');
  209.     setcursor(grow, gcol+w->wwidth-1);
  210.     vertnchars(w->wheight, '|');
  211.  
  212.     grow = w->worg_row + w->wcur_row - 1;
  213.     gcol = w->worg_col + w->wcur_col - 1;
  214.     setcursor(grow, gcol);
  215. }
  216.