home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2873 / window.c < prev   
Encoding:
C/C++ Source or Header  |  1991-02-28  |  10.2 KB  |  439 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. /*
  195. * Split the current window. A window
  196. * smaller than 3 lines cannot be split.
  197. * The only other error that is possible is
  198. * a "malloc" failure allocating the structure
  199. * for the new window.
  200. */
  201. bool splitwind ()
  202. {
  203.  
  204.     register    WINDOW * wp;
  205.     register int    ntru;
  206.     register int    ntrl;
  207.     register int    ntrd;
  208.     register    WINDOW * wp1;
  209.     register    WINDOW * wp2;
  210.     char    buf[80], buf1[40];
  211.  
  212.     if (curwp -> w_ntrows < 3)
  213.         {
  214.         sprintf (buf1, MSG_no_splt, R_BYTE_FMT(curwp));
  215.         sprintf (buf, buf1, curwp -> w_ntrows);
  216.         writ_echo (buf);
  217.         return (FALSE);
  218.         }
  219.  
  220.     if ((wp = (WINDOW *) malloc (sizeof (WINDOW))) == NULL)
  221.         {
  222.  
  223.         writ_echo (MSG_cnt_al_w);
  224.         return (FALSE);
  225.         }
  226.  
  227.     ++curbp -> b_nwnd;          /* Displayed twice.  */
  228.     wp -> w_bufp = curbp;
  229.     wp -> w_dotp = curwp -> w_dotp;
  230.     wp -> w_doto = curwp -> w_doto;
  231.     wp -> w_unit_offset = curwp -> w_unit_offset;
  232.     wp -> w_markp = curwp -> w_markp;
  233.     wp -> w_marko = curwp -> w_marko;
  234.     wp -> w_flag = 0;
  235.     wp -> w_disp_shift = curwp -> w_disp_shift;
  236.     wp -> w_intel_mode = curwp -> w_intel_mode;
  237.     wp -> w_fmt_ptr = curwp -> w_fmt_ptr;
  238.     ntru = (curwp -> w_ntrows - 1) / 2;/* Upper size         */
  239.     ntrl = (curwp -> w_ntrows - 1) - ntru;/* Lower size      */
  240.  
  241.     if (ntrd <= get_currow (curwp))
  242.         {
  243.     /* Old is upper window.      */
  244.         curwp -> w_ntrows = ntru;
  245.         wp -> w_wndp = curwp -> w_wndp;
  246.         curwp -> w_wndp = wp;
  247.         wp -> w_toprow = curwp -> w_toprow + ntru + 1;
  248.         wp -> w_ntrows = ntrl;
  249.         }
  250.     else
  251.         {
  252.     /* Old is lower window       */
  253.         wp1 = NULL;
  254.         wp2 = wheadp;
  255.         while (wp2 != curwp)
  256.             {
  257.             wp1 = wp2;
  258.             wp2 = wp2 -> w_wndp;
  259.             }
  260.  
  261.         if (wp1 == NULL)
  262.             wheadp = wp;
  263.         else
  264.             wp1 -> w_wndp = wp;
  265.         wp -> w_wndp = curwp;
  266.         wp -> w_toprow = curwp -> w_toprow;
  267.         wp -> w_ntrows = ntru;
  268.         ++ntru;                 /* Mode line.        */
  269.         curwp -> w_toprow += ntru;
  270.         curwp -> w_ntrows = ntrl;
  271.         }
  272.  
  273.     wind_on_dot (curwp);        /* put window on the dot */
  274.     wp -> w_loff = curwp -> w_loff;/* do the same for the new window */
  275.     wp -> w_linep = curwp -> w_linep;
  276.     curwp -> w_flag |= WFMODE | WFHARD;
  277.     wp -> w_flag |= WFMODE | WFHARD;
  278.     return (TRUE);
  279. }
  280.  
  281.  
  282. /*
  283. * Enlarge the current window.
  284. * Find the window that loses space. Make
  285. * sure it is big enough. If so, hack the window
  286. * descriptions, and ask redisplay to do all the
  287. * hard work. You don't just set "force reframe"
  288. * because dot would move.
  289. */
  290. bool enlargewind (f, n, k)
  291. {
  292.     register    WINDOW * adjwp;
  293.     register    LINE * lp;
  294.     register int    i;
  295.  
  296.     if (n < 0)
  297.         return (shrinkwind (f, -n, KRANDOM));
  298.     if (wheadp -> w_wndp == NULL)
  299.         {
  300.  
  301.         writ_echo (MSG_one_w);
  302.         return (FALSE);
  303.         }
  304.  
  305.     if ((adjwp = curwp -> w_wndp) == NULL)
  306.         {
  307.         adjwp = wheadp;
  308.         while (adjwp -> w_wndp != curwp)
  309.             adjwp = adjwp -> w_wndp;
  310.         }
  311.  
  312.     if (adjwp -> w_ntrows <= n)
  313.         {
  314.         writ_echo (MSG_imp_chg);
  315.         return (FALSE);
  316.         }
  317.  
  318.     if (curwp -> w_wndp == adjwp)
  319.         {
  320.     /* Shrink below.     */
  321.         lp = adjwp -> w_linep;
  322.         for (i = 0; i < n && lp != adjwp -> w_bufp -> b_linep; ++i)
  323.             lp = lforw (lp);
  324.         adjwp -> w_linep = lp;
  325.         adjwp -> w_toprow += n;
  326.         }
  327.     else
  328.         {
  329.     /* Shrink above.     */
  330.         lp = curwp -> w_linep;
  331.         for (i = 0; i < n && lback (lp) != curbp -> b_linep; ++i)
  332.             lp = lback (lp);
  333.         curwp -> w_linep = lp;
  334.         curwp -> w_toprow -= n;
  335.         }
  336.  
  337.     curwp -> w_ntrows += n;
  338.     adjwp -> w_ntrows -= n;
  339.     curwp -> w_flag |= WFMODE | WFHARD;
  340.     adjwp -> w_flag |= WFMODE | WFHARD;
  341.     return (TRUE);
  342. }
  343.  
  344.  
  345. /*
  346. * Shrink the current window.
  347. * Find the window that gains space. Hack at
  348. * the window descriptions. Ask the redisplay to
  349. * do all the hard work.
  350. */
  351. bool shrinkwind (f, n, k)
  352. {
  353.     register    WINDOW * adjwp;
  354.     register    LINE * lp;
  355.     register int    i;
  356.  
  357.     if (n < 0)
  358.         return (enlargewind (f, -n, KRANDOM));
  359.     if (wheadp -> w_wndp == NULL)
  360.         {
  361.         writ_echo (MSG_one_w);
  362.         return (FALSE);
  363.         }
  364.  
  365.     if ((adjwp = curwp -> w_wndp) == NULL)
  366.         {
  367.         adjwp = wheadp;
  368.         while (adjwp -> w_wndp != curwp)
  369.             adjwp = adjwp -> w_wndp;
  370.         }
  371.  
  372.     if (curwp -> w_ntrows <= n)
  373.         {
  374.         writ_echo (MSG_imp_chg);
  375.         return (FALSE);
  376.         }
  377.  
  378.     if (curwp -> w_wndp == adjwp)
  379.         {
  380.     /* Grow below.       */
  381.         lp = adjwp -> w_linep;
  382.         for (i = 0; i < n && lback (lp) != adjwp -> w_bufp -> b_linep; ++i)
  383.             lp = lback (lp);
  384.         adjwp -> w_linep = lp;
  385.         adjwp -> w_toprow -= n;
  386.         }
  387.     else
  388.         {
  389.     /* Grow above.       */
  390.         lp = curwp -> w_linep;
  391.         for (i = 0; i < n && lp != curbp -> b_linep; ++i)
  392.             lp = lforw (lp);
  393.         curwp -> w_linep = lp;
  394.         curwp -> w_toprow += n;
  395.         }
  396.  
  397.     curwp -> w_ntrows -= n;
  398.     adjwp -> w_ntrows += n;
  399.     curwp -> w_flag |= WFMODE | WFHARD;
  400.     adjwp -> w_flag |= WFMODE | WFHARD;
  401.     return (TRUE);
  402. }
  403.  
  404.  
  405. /*
  406. * Pick a window for a pop-up.
  407. * Split the screen if there is only
  408. * one window. Pick the uppermost window that
  409. * isn't the current window. An LRU algorithm
  410. * might be better. Return a pointer, or
  411. * NULL on error.
  412. */
  413. WINDOW * wpopup ()
  414. {
  415.  
  416.     register    WINDOW * wp;
  417.  
  418.     if (wheadp -> w_wndp == NULL
  419.             && splitwind () == FALSE)
  420.         return (NULL);
  421.     wp = wheadp;                /* Find window to use    */
  422.     while (wp != NULL && wp == curwp)
  423.         wp = wp -> w_wndp;
  424.     return (wp);
  425. }
  426.  
  427.  
  428. /*
  429. * Refresh the display. 
  430. * In the normal case the
  431. * call to "update" in "main.c" refreshes the screen,
  432. * and all of the windows need not be recomputed.
  433. */
  434. bool refresh ()
  435. {
  436.     sgarbf = TRUE;
  437.     return (TRUE);
  438. }
  439.