home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3444 / window.c < prev   
Encoding:
C/C++ Source or Header  |  1991-06-07  |  11.8 KB  |  510 lines

  1. /*
  2. *       Window handling.
  3. */
  4. #include    "def.h"
  5.  
  6. char    mvupwind ();
  7. bool    shrinkwind ();
  8.  
  9. extern    char    MSG_no_splt[];
  10. extern    char    MSG_cnt_al_w[];
  11. extern    char    MSG_one_w[];
  12. extern    char    MSG_imp_chg[];
  13.  
  14. #include    "lintfunc.dec"
  15. /*
  16. * Reposition the window so as to center on the dot.
  17. */
  18. bool reposition ()
  19. {
  20.     long    l_val;
  21.  
  22.     l_val = DOT_POS(curwp) - (curwp -> w_ntrows * R_BYTES(curwp) / 2);
  23.     move_ptr (curwp, l_val, FALSE, TRUE, FALSE);
  24.     curwp -> w_flag |= WFHARD;
  25.     return (TRUE);
  26. }
  27.  
  28. /*
  29. * The command make the next
  30. * window (next => down the screen)
  31. * the current window. There are no real
  32. * errors, although the command does
  33. * nothing if there is only 1 window on
  34. * the screen.
  35. */
  36. bool nextwind ()
  37. {
  38.  
  39.     register    WINDOW * wp;
  40.  
  41.     if ((wp = curwp -> w_wndp) == NULL)
  42.         wp = wheadp;
  43.     curwp = wp;
  44.     curbp = wp -> w_bufp;
  45.     return (TRUE);
  46. }
  47.  
  48.  
  49. /*
  50. * This command makes the previous
  51. * window (previous => up the screen) the
  52. * current window. There arn't any errors,
  53. * although the command does not do a lot
  54. * if there is 1 window.
  55. */
  56. bool prevwind ()
  57. {
  58.  
  59.     register    WINDOW * wp1;
  60.     register    WINDOW * wp2;
  61.  
  62.     wp1 = wheadp;
  63.     wp2 = curwp;
  64.     if (wp1 == wp2)
  65.         wp2 = NULL;
  66.     while (wp1 -> w_wndp != wp2)
  67.         wp1 = wp1 -> w_wndp;
  68.     curwp = wp1;
  69.     curbp = wp1 -> w_bufp;
  70.     return (TRUE);
  71. }
  72.  
  73.  
  74. /*
  75. * This command moves the current
  76. * window down by "arg" lines. Recompute
  77. * the top line in the window. The move up and
  78. * move down code is almost completely the same;
  79. * most of the work has to do with reframing the
  80. * window, and picking a new dot. We share the
  81. * code by having "move down" just be an interface
  82. * to "move up".
  83. */
  84. char    mvdnwind (f, n, k)
  85. register int    n;
  86. {
  87.     return (mvupwind (f, -n, KRANDOM));
  88. }
  89.  
  90.  
  91. /*
  92. * Move the current window up by "arg"
  93. * lines. Recompute the new top line of the window.
  94. * Look to see if "." is still on the screen. If it is,
  95. * you win. If it isn't, then move "." to center it
  96. * in the new framing of the window (this command does
  97. * not really move "."; it moves the frame).
  98. */
  99. char    mvupwind (f, n, k)
  100.     int    n;
  101. {
  102.     A32   l_val, l_bytes;
  103.  
  104.     l_bytes = (A32)R_BYTES(curwp);     /* number of bytes in a row */
  105.     l_val = n * l_bytes;     /* number of bytes to move */
  106.     move_ptr (curwp, l_val, FALSE, TRUE, TRUE);  /* move window */
  107.  
  108.     /* check that dot is in window */
  109.     while (DOT_POS(curwp) < WIND_POS(curwp))
  110.     {
  111.         /* dot is before the first window line */
  112.         move_ptr (curwp, l_bytes, TRUE, TRUE, TRUE);
  113.     }
  114.     while (DOT_POS (curwp) >=
  115.              ((l_bytes * curwp -> w_ntrows) + WIND_POS(curwp)))
  116.     {
  117.         /* dot is after the last window line */
  118.         move_ptr (curwp, -l_bytes, TRUE, TRUE, TRUE);
  119.     }
  120.     curwp -> w_flag |= WFHARD;
  121.     return (TRUE);
  122. }
  123.  
  124.  
  125. /*
  126. * This command makes the current
  127. * window the only window on the screen.
  128. * Try to set the framing
  129. * so that "." does not have to move on
  130. * the display. Some care has to be taken
  131. * to keep the values of dot and mark
  132. * in the buffer structures right if the
  133. * distruction of a window makes a buffer
  134. * become undisplayed.
  135. */
  136. char    onlywind ()
  137. {
  138.  
  139.     register    WINDOW * wp;
  140.     register    LINE * lp;
  141.     register int    i;
  142.  
  143.     while (wheadp != curwp)
  144.         {
  145.  
  146.         wp = wheadp;
  147.         wheadp = wp -> w_wndp;
  148.         if (--wp -> w_bufp -> b_nwnd == 0)
  149.             {
  150.  
  151.             wp -> w_bufp -> b_dotp = wp -> w_dotp;
  152.             wp -> w_bufp -> b_doto = wp -> w_doto;
  153.             wp -> w_bufp -> b_markp = wp -> w_markp;
  154.             wp -> w_bufp -> b_marko = wp -> w_marko;
  155.             }
  156.  
  157.         free ((char *) wp);
  158.         }
  159.  
  160.     while (curwp -> w_wndp != NULL)
  161.         {
  162.  
  163.         wp = curwp -> w_wndp;
  164.         curwp -> w_wndp = wp -> w_wndp;
  165.         if (--wp -> w_bufp -> b_nwnd == 0)
  166.             {
  167.  
  168.             wp -> w_bufp -> b_dotp = wp -> w_dotp;
  169.             wp -> w_bufp -> b_doto = wp -> w_doto;
  170.             wp -> w_bufp -> b_markp = wp -> w_markp;
  171.             wp -> w_bufp -> b_marko = wp -> w_marko;
  172.             }
  173.  
  174.         free ((char *) wp);
  175.         }
  176.  
  177.     lp = curwp -> w_linep;
  178.     i = curwp -> w_toprow;
  179.     while (i != 0 && lback (lp) != curbp -> b_linep)
  180.         {
  181.  
  182.         --i;
  183.         lp = lback (lp);
  184.         }
  185.  
  186.     curwp -> w_toprow = 0;
  187.     curwp -> w_ntrows = nrow - 2;/* 2 = mode, echo.  */
  188.     curwp -> w_linep = lp;
  189.     curwp -> w_flag |= WFMODE | WFHARD;
  190.     return (TRUE);
  191. }
  192.  
  193. /*
  194.  * Delete the current window, placing its space in the window above,
  195.  * or, if it is the top window, the window below. Bound to C-X 0.
  196.  */
  197.  
  198. delwind()
  199.  
  200. {
  201.     register WINDOW *wp;    /* window to recieve deleted space */
  202.     register WINDOW *lwp;    /* ptr window before curwp */
  203.     register int target;    /* target line to search for */
  204.  
  205.     /* if there is only one window, don't delete it */
  206.     if (wheadp->w_wndp == NULL) {
  207.         return(FALSE);
  208.     }
  209.  
  210.     /* find window before curwp in linked list */
  211.     wp = wheadp;
  212.     lwp = NULL;
  213.     while (wp != NULL) {
  214.         if (wp == curwp)
  215.             break;
  216.         lwp = wp;
  217.         wp = wp->w_wndp;
  218.     }
  219.  
  220.     /* find recieving window and give up our space */
  221.     wp = wheadp;
  222.     if (curwp->w_toprow == 0) {
  223.         /* find the next window down */
  224.         target = curwp->w_ntrows + 1;
  225.         while (wp != NULL) {
  226.             if (wp->w_toprow == target)
  227.                 break;
  228.             wp = wp->w_wndp;
  229.         }
  230.         if (wp == NULL)
  231.             return(FALSE);
  232.         wp->w_toprow = 0;
  233.         wp->w_ntrows += target;
  234.     } else {
  235.         /* find the next window up */
  236.         target = curwp->w_toprow - 1;
  237.         while (wp != NULL) {
  238.             if ((wp->w_toprow + wp->w_ntrows) == target)
  239.                 break;
  240.             wp = wp->w_wndp;
  241.         }
  242.         if (wp == NULL)
  243.             return(FALSE);
  244.         wp->w_ntrows += 1 + curwp->w_ntrows;
  245.     }
  246.  
  247.     /* get rid of the current window */
  248.     if (--curwp->w_bufp->b_nwnd == 0) {
  249.         curwp->w_bufp->b_dotp = curwp->w_dotp;
  250.         curwp->w_bufp->b_doto = curwp->w_doto;
  251.         curwp->w_bufp->b_markp = curwp->w_markp;
  252.         curwp->w_bufp->b_marko = curwp->w_marko;
  253.     }
  254.     if (lwp == NULL)
  255.         wheadp = curwp->w_wndp;
  256.     else
  257.         lwp->w_wndp = curwp->w_wndp;
  258.     free((char *)curwp);
  259.     curwp = wp;
  260.     wp -> w_flag |= WFMODE | WFHARD;
  261.     curbp = wp->w_bufp;
  262.     return(TRUE);
  263. }
  264.  
  265. /*
  266. * Split the current window. A window
  267. * smaller than 3 lines cannot be split.
  268. * The only other error that is possible is
  269. * a "malloc" failure allocating the structure
  270. * for the new window.
  271. */
  272. bool splitwind ()
  273. {
  274.  
  275.     register    WINDOW * wp;
  276.     register int    ntru;
  277.     register int    ntrl;
  278.     register int    ntrd;
  279.     register    WINDOW * wp1;
  280.     register    WINDOW * wp2;
  281.     char    buf[80], buf1[40];
  282.  
  283.     if (curwp -> w_ntrows < 3)
  284.         {
  285.         sprintf (buf1, MSG_no_splt, R_BYTE_FMT(curwp));
  286.         sprintf (buf, buf1, curwp -> w_ntrows);
  287.         writ_echo (buf);
  288.         return (FALSE);
  289.         }
  290.  
  291.     if ((wp = (WINDOW *) malloc (sizeof (WINDOW))) == NULL)
  292.         {
  293.  
  294.         writ_echo (MSG_cnt_al_w);
  295.         return (FALSE);
  296.         }
  297.  
  298.     ++curbp -> b_nwnd;          /* Displayed twice.  */
  299.     wp -> w_bufp = curbp;
  300.     wp -> w_dotp = curwp -> w_dotp;
  301.     wp -> w_doto = curwp -> w_doto;
  302.     wp -> w_unit_offset = curwp -> w_unit_offset;
  303.     wp -> w_markp = curwp -> w_markp;
  304.     wp -> w_marko = curwp -> w_marko;
  305.     wp -> w_flag = 0;
  306.     wp -> w_disp_shift = curwp -> w_disp_shift;
  307.     wp -> w_intel_mode = curwp -> w_intel_mode;
  308.     wp -> w_fmt_ptr = curwp -> w_fmt_ptr;
  309.     ntru = (curwp -> w_ntrows - 1) / 2;/* Upper size         */
  310.     ntrl = (curwp -> w_ntrows - 1) - ntru;/* Lower size      */
  311.  
  312.     if (ntrd <= get_currow (curwp))
  313.         {
  314.     /* Old is upper window.      */
  315.         curwp -> w_ntrows = ntru;
  316.         wp -> w_wndp = curwp -> w_wndp;
  317.         curwp -> w_wndp = wp;
  318.         wp -> w_toprow = curwp -> w_toprow + ntru + 1;
  319.         wp -> w_ntrows = ntrl;
  320.         }
  321.     else
  322.         {
  323.     /* Old is lower window       */
  324.         wp1 = NULL;
  325.         wp2 = wheadp;
  326.         while (wp2 != curwp)
  327.             {
  328.             wp1 = wp2;
  329.             wp2 = wp2 -> w_wndp;
  330.             }
  331.  
  332.         if (wp1 == NULL)
  333.             wheadp = wp;
  334.         else
  335.             wp1 -> w_wndp = wp;
  336.         wp -> w_wndp = curwp;
  337.         wp -> w_toprow = curwp -> w_toprow;
  338.         wp -> w_ntrows = ntru;
  339.         ++ntru;                 /* Mode line.        */
  340.         curwp -> w_toprow += ntru;
  341.         curwp -> w_ntrows = ntrl;
  342.         }
  343.  
  344.     wind_on_dot (curwp);        /* put window on the dot */
  345.     wp -> w_loff = curwp -> w_loff;/* do the same for the new window */
  346.     wp -> w_linep = curwp -> w_linep;
  347.     curwp -> w_flag |= WFMODE | WFHARD;
  348.     wp -> w_flag |= WFMODE | WFHARD;
  349.     return (TRUE);
  350. }
  351.  
  352.  
  353. /*
  354. * Enlarge the current window.
  355. * Find the window that loses space. Make
  356. * sure it is big enough. If so, hack the window
  357. * descriptions, and ask redisplay to do all the
  358. * hard work. You don't just set "force reframe"
  359. * because dot would move.
  360. */
  361. bool enlargewind (f, n, k)
  362. {
  363.     register    WINDOW * adjwp;
  364.     register    LINE * lp;
  365.     register int    i;
  366.  
  367.     if (n < 0)
  368.         return (shrinkwind (f, -n, KRANDOM));
  369.     if (wheadp -> w_wndp == NULL)
  370.         {
  371.  
  372.         writ_echo (MSG_one_w);
  373.         return (FALSE);
  374.         }
  375.  
  376.     if ((adjwp = curwp -> w_wndp) == NULL)
  377.         {
  378.         adjwp = wheadp;
  379.         while (adjwp -> w_wndp != curwp)
  380.             adjwp = adjwp -> w_wndp;
  381.         }
  382.  
  383.     if (adjwp -> w_ntrows <= n)
  384.         {
  385.         writ_echo (MSG_imp_chg);
  386.         return (FALSE);
  387.         }
  388.  
  389.     if (curwp -> w_wndp == adjwp)
  390.         {
  391.     /* Shrink below.     */
  392.         lp = adjwp -> w_linep;
  393.         for (i = 0; i < n && lp != adjwp -> w_bufp -> b_linep; ++i)
  394.             lp = lforw (lp);
  395.         adjwp -> w_linep = lp;
  396.         adjwp -> w_toprow += n;
  397.         }
  398.     else
  399.         {
  400.     /* Shrink above.     */
  401.         lp = curwp -> w_linep;
  402.         for (i = 0; i < n && lback (lp) != curbp -> b_linep; ++i)
  403.             lp = lback (lp);
  404.         curwp -> w_linep = lp;
  405.         curwp -> w_toprow -= n;
  406.         }
  407.  
  408.     curwp -> w_ntrows += n;
  409.     adjwp -> w_ntrows -= n;
  410.     curwp -> w_flag |= WFMODE | WFHARD;
  411.     adjwp -> w_flag |= WFMODE | WFHARD;
  412.     return (TRUE);
  413. }
  414.  
  415.  
  416. /*
  417. * Shrink the current window.
  418. * Find the window that gains space. Hack at
  419. * the window descriptions. Ask the redisplay to
  420. * do all the hard work.
  421. */
  422. bool shrinkwind (f, n, k)
  423. {
  424.     register    WINDOW * adjwp;
  425.     register    LINE * lp;
  426.     register int    i;
  427.  
  428.     if (n < 0)
  429.         return (enlargewind (f, -n, KRANDOM));
  430.     if (wheadp -> w_wndp == NULL)
  431.         {
  432.         writ_echo (MSG_one_w);
  433.         return (FALSE);
  434.         }
  435.  
  436.     if ((adjwp = curwp -> w_wndp) == NULL)
  437.         {
  438.         adjwp = wheadp;
  439.         while (adjwp -> w_wndp != curwp)
  440.             adjwp = adjwp -> w_wndp;
  441.         }
  442.  
  443.     if (curwp -> w_ntrows <= n)
  444.         {
  445.         writ_echo (MSG_imp_chg);
  446.         return (FALSE);
  447.         }
  448.  
  449.     if (curwp -> w_wndp == adjwp)
  450.         {
  451.     /* Grow below.       */
  452.         lp = adjwp -> w_linep;
  453.         for (i = 0; i < n && lback (lp) != adjwp -> w_bufp -> b_linep; ++i)
  454.             lp = lback (lp);
  455.         adjwp -> w_linep = lp;
  456.         adjwp -> w_toprow -= n;
  457.         }
  458.     else
  459.         {
  460.     /* Grow above.       */
  461.         lp = curwp -> w_linep;
  462.         for (i = 0; i < n && lp != curbp -> b_linep; ++i)
  463.             lp = lforw (lp);
  464.         curwp -> w_linep = lp;
  465.         curwp -> w_toprow += n;
  466.         }
  467.  
  468.     curwp -> w_ntrows -= n;
  469.     adjwp -> w_ntrows += n;
  470.     curwp -> w_flag |= WFMODE | WFHARD;
  471.     adjwp -> w_flag |= WFMODE | WFHARD;
  472.     return (TRUE);
  473. }
  474.  
  475.  
  476. /*
  477. * Pick a window for a pop-up.
  478. * Split the screen if there is only
  479. * one window. Pick the uppermost window that
  480. * isn't the current window. An LRU algorithm
  481. * might be better. Return a pointer, or
  482. * NULL on error.
  483. */
  484. WINDOW * wpopup ()
  485. {
  486.  
  487.     register    WINDOW * wp;
  488.  
  489.     if (wheadp -> w_wndp == NULL
  490.             && splitwind () == FALSE)
  491.         return (NULL);
  492.     wp = wheadp;                /* Find window to use    */
  493.     while (wp != NULL && wp == curwp)
  494.         wp = wp -> w_wndp;
  495.     return (wp);
  496. }
  497.  
  498.  
  499. /*
  500. * Refresh the display. 
  501. * In the normal case the
  502. * call to "update" in "main.c" refreshes the screen,
  503. * and all of the windows need not be recomputed.
  504. */
  505. bool refresh ()
  506. {
  507.     sgarbf = TRUE;
  508.     return (TRUE);
  509. }
  510.