home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progc
/
ce331src.arj
/
WINDOW.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-03-10
|
18KB
|
799 lines
/*
* W I N D O W . C
*
* purpose:
* Window management. Some of the functions are internal, and some are
* attached to keys that the user actually types.
*/
#include <stdio.h> /* Turbo C++ verison 1.0 */
#include <stdlib.h>
#include <dos.h>
#include <pdefs.h> /* PforCe version 1.05 (includes jwg.h) */
#include <pf_ansi.h>
#include "struct.h"
#include "def.h"
#include "protos.h"
/**
* Reposition dot in the current window to line "n". If the argument is
* positive, it is that line. If it is negative it is that line from the
* bottom. If it is 0 the window is centered (this is what the standard
* redisplay code does). With no argument it defaults to 0.
*/
int Reposition(f, n)
int f, n;
{
if (f == FALSE) /* default to 0 to center screen */
n = 0;
curwp->w_force = (char)n;
curwp->w_flag |= WFFORCE;
return(TRUE);
}
/**
* Refresh the screen. With no argument, it just does the Refresh. With an
* argument it recenters dot in the current window.
*/
int Refresh(f, n)
int f, n;
{
if (f == FALSE)
sgarbf = TRUE;
else
{
curwp->w_force = 0; /* center dot */
curwp->w_flag |= WFFORCE;
}
return(TRUE);
}
/**
* This command make the next window the current window. There are no real
* errors, although the command does nothing if there is only 1 window on
* the screen. With an argument this command finds the <n>th window from
* the top.
*/
int NextWind(f, n)
int f;
int n;
{
WINDOW *wp;
int nwindows; /* total number of windows */
if (f)
{
/* first count the # of windows */
wp = wheadp;
nwindows = 1;
while (wp->w_wndp != NULLWPTR)
{
nwindows++;
wp = wp->w_wndp;
}
/* if argument is negative, it is nth window from bottom of screen */
if (n < 0)
n += nwindows + 1;
/* if an argument, give them that window from the top */
if (n > 0 && n <= nwindows)
{
wp = wheadp;
while (--n)
wp = wp->w_wndp;
}
else
{
MlWrite("Window number out of range");
return(FALSE);
}
}
else
if ((wp = curwp->w_wndp) == NULLWPTR)
wp = wheadp;
curwp = wp;
curbp = wp->w_bufp;
UpMode();
return(TRUE);
}
/**
* This command makes the previous window (previous => up the screen) the
* current window. There arn't any errors, although the command does not do
* a lot if there is only one window.
*/
int PrevWind(f, n)
int f, n;
{
WINDOW *wp1;
WINDOW *wp2;
/* if we have an argument, we mean the nth window from the bottom */
if (f)
return(NextWind(f, -n));
wp1 = wheadp;
wp2 = curwp;
if (wp1 == wp2)
wp2 = NULLWPTR;
while (wp1->w_wndp != wp2)
wp1 = wp1->w_wndp;
curwp = wp1;
curbp = wp1->w_bufp;
UpMode();
return(TRUE);
}
/**
* This command moves the current window down by "arg" lines. Recompute the
* top line in the window. The move up and move down code is almost completely
* the same; most of the work has to do with reframing the window, and picking
* a new dot. We share the code by having "move down" just be an interface to
* "move up". Magic.
*/
int MoveWindDn(f, n)
int f, n;
{
return(MoveWindUp(f, -n));
}
/**
* Move the current window up by "arg" lines. Recompute the new top line of
* the window. Look to see if dot is still on the screen. If it is, you win.
* If it isn't, then move dot to center it in the new framing of the window
* (this command does not really move dot; it moves the frame).
*/
int MoveWindUp(f, n)
int f;
int n;
{
LINE *lp;
int i;
lp = curwp->w_linep;
if (n < 0)
{
while (n++ && lp != curbp->b_linep)
lp = lforw(lp);
}
else
{
while (n-- && lback(lp) != curbp->b_linep)
lp = lback(lp);
}
curwp->w_linep = lp;
curwp->w_flag |= WFHARD; /* mode line is OK */
for (i = 0; i < curwp->w_ntrows; ++i)
{
if (lp == curwp->w_dotp)
return(TRUE);
if (lp == curbp->b_linep)
break;
lp = lforw(lp);
}
lp = curwp->w_linep;
i = curwp->w_ntrows / 2;
while (i-- && lp != curbp->b_linep)
lp = lforw(lp);
curwp->w_dotp = lp;
curwp->w_doto = 0;
return(TRUE);
}
/**
* This command makes the current window the only window on the screen.
* Try to set the framing so that dot does not have to move on the display.
* Some care has to be taken to keep the values of dot and mark in the buffer
* structures right if the destruction of a window makes a buffer become
* undisplayed.
*/
int OnlyWind(f, n)
int f, n;
{
WINDOW *wp;
LINE *lp;
#if BMARKS
int bmark;
#endif
int i;
while (wheadp != curwp)
{
wp = wheadp;
wheadp = wp->w_wndp;
if (--wp->w_bufp->b_nwnd == 0)
{
wp->w_bufp->b_dotp = wp->w_dotp;
wp->w_bufp->b_doto = wp->w_doto;
wp->w_bufp->b_markp = wp->w_markp;
wp->w_bufp->b_marko = wp->w_marko;
wp->w_bufp->b_anchorp = wp->w_anchorp;
wp->w_bufp->b_anchoro = wp->w_anchoro;
wp->w_bufp->b_fcol = wp->w_fcol;
#if BMARKS
for (bmark = 0; bmark < NBMARKS; bmark++)
{
wp->w_bufp->b_bmarkp[bmark] = wp->w_bmarkp[bmark];
wp->w_bufp->b_bmarko[bmark] = wp->w_bmarko[bmark];
}
#endif
}
free((char *)wp);
}
while (curwp->w_wndp != NULLWPTR)
{
wp = curwp->w_wndp;
curwp->w_wndp = wp->w_wndp;
if (--wp->w_bufp->b_nwnd == 0)
{
wp->w_bufp->b_dotp = wp->w_dotp;
wp->w_bufp->b_doto = wp->w_doto;
wp->w_bufp->b_markp = wp->w_markp;
wp->w_bufp->b_marko = wp->w_marko;
wp->w_bufp->b_anchorp = wp->w_anchorp;
wp->w_bufp->b_anchoro = wp->w_anchoro;
wp->w_bufp->b_fcol = wp->w_fcol;
#if BMARKS
for (bmark = 0; bmark < NBMARKS; bmark++)
{
wp->w_bufp->b_bmarkp[bmark] = wp->w_bmarkp[bmark];
wp->w_bufp->b_bmarko[bmark] = wp->w_bmarko[bmark];
}
#endif
}
free((char *)wp);
}
lp = curwp->w_linep;
i = curwp->w_toprow;
while (i != 0 && lback(lp) != curbp->b_linep)
{
--i;
lp = lback(lp);
}
curwp->w_toprow = 0;
curwp->w_ntrows = (char)term.t_nrow - 1;
curwp->w_linep = lp;
curwp->w_flag |= WFMODE | WFHARD;
return(TRUE);
}
/**
* Delete the current window, placing its space in the window above,
* or, if it is the top window, the window below.
*/
int DelWind(f, n)
int f, n;
{
WINDOW *wp; /* window to recieve deleted space */
WINDOW *lwp; /* ptr window before curwp */
int target; /* target line to search for */
#if BMARKS
int bmark;
#endif
/* if there is only one window, don't delete it */
if (wheadp->w_wndp == NULLWPTR)
{
MlWrite("Can't delete the only window");
return(FALSE);
}
/* find window before curwp in linked list */
wp = wheadp;
lwp = NULLWPTR;
while (wp != NULLWPTR)
{
if (wp == curwp)
break;
lwp = wp;
wp = wp->w_wndp;
}
/* find receiving window and give up our space */
wp = wheadp;
if (curwp->w_toprow == 0)
{
/* find the next window down */
target = curwp->w_ntrows + 1;
while (wp != NULLWPTR)
{
if (wp->w_toprow == (char)target)
break;
wp = wp->w_wndp;
}
if (wp == NULLWPTR)
return(FALSE);
wp->w_toprow = 0;
wp->w_ntrows += (char)target;
}
else
{
/* find the next window up */
target = curwp->w_toprow - 1;
while (wp != NULLWPTR)
{
if ((wp->w_toprow + wp->w_ntrows) == (char)target)
break;
wp = wp->w_wndp;
}
if (wp == NULLWPTR)
return(FALSE);
wp->w_ntrows += curwp->w_ntrows + 1;
}
/* get rid of the current window */
if (--curwp->w_bufp->b_nwnd == 0)
{
curwp->w_bufp->b_dotp = curwp->w_dotp;
curwp->w_bufp->b_doto = curwp->w_doto;
curwp->w_bufp->b_markp = curwp->w_markp;
curwp->w_bufp->b_marko = curwp->w_marko;
curwp->w_bufp->b_anchorp = curwp->w_anchorp;
curwp->w_bufp->b_anchoro = curwp->w_anchoro;
curwp->w_bufp->b_fcol = curwp->w_fcol;
#if BMARKS
for (bmark = 0; bmark < NBMARKS; bmark++)
{
curwp->w_bufp->b_bmarkp[bmark] = curwp->w_bmarkp[bmark];
curwp->w_bufp->b_bmarko[bmark] = curwp->w_bmarko[bmark];
}
#endif
}
if (lwp == NULLWPTR)
wheadp = curwp->w_wndp;
else
lwp->w_wndp = curwp->w_wndp;
free((char *)curwp);
curwp = wp;
wp->w_flag |= WFHARD;
curbp = wp->w_bufp;
UpMode();
return(TRUE);
}
/**
* Split the current window. A window smaller than 3 lines cannot be split.
* An argument of 1 forces the cursor into the upper window, an argument of
* two forces the cursor to the lower window. The only other error that is
* possible is a "malloc" failure allocating the structure for the new window.
*/
int SplitWind(f, n)
int f, n;
{
WINDOW *wp;
LINE *lp;
int ntru;
int ntrl;
int ntrd;
WINDOW *wp1;
WINDOW *wp2;
#if BMARKS
int bmark;
#endif
char buf[NSTRING];
if (curwp->w_ntrows < 3)
{
strfmt(buf, "Can't split a %d line window", curwp->w_ntrows);
MlWrite(buf);
return(FALSE);
}
if ((wp = (WINDOW *)malloc(sizeof(WINDOW))) == NULLWPTR)
{
OutOfMem();
return(FALSE);
}
++curbp->b_nwnd; /* displayed twice */
wp->w_bufp = curbp;
wp->w_dotp = curwp->w_dotp;
wp->w_doto = curwp->w_doto;
wp->w_markp = curwp->w_markp;
wp->w_marko = curwp->w_marko;
wp->w_anchorp = curwp->w_anchorp;
wp->w_anchoro = curwp->w_anchoro;
wp->w_fcol = curwp->w_fcol;
#if BMARKS
for (bmark = 0; bmark < NBMARKS; bmark++)
{
wp->w_bmarkp[bmark] = curwp->w_bmarkp[bmark];
wp->w_bmarko[bmark] = curwp->w_bmarko[bmark];
}
#endif
wp->w_flag = 0;
wp->w_force = 0;
/* set the colors of the new window */
wp->w_fcolor = (char)gfcolor;
wp->w_bcolor = (char)gbcolor;
ntru = (curwp->w_ntrows-1) / 2; /* upper size */
ntrl = (curwp->w_ntrows-1) - ntru; /* lower size */
lp = curwp->w_linep;
ntrd = 0;
while (lp != curwp->w_dotp)
{
++ntrd;
lp = lforw(lp);
}
lp = curwp->w_linep;
if (((f == FALSE) && (ntrd <= ntru)) || ((f == TRUE) && (n == 1)))
{
/* old is upper window */
if (ntrd == ntru) /* hit mode line */
lp = lforw(lp);
curwp->w_ntrows = (char)ntru;
wp->w_wndp = curwp->w_wndp;
curwp->w_wndp = wp;
wp->w_toprow = curwp->w_toprow + ntru + 1;
wp->w_ntrows = (char)ntrl;
}
else
{ /* old is lower window */
wp1 = NULLWPTR;
wp2 = wheadp;
while (wp2 != curwp)
{
wp1 = wp2;
wp2 = wp2->w_wndp;
}
if (wp1 == NULLWPTR)
wheadp = wp;
else
wp1->w_wndp = wp;
wp->w_wndp = curwp;
wp->w_toprow = curwp->w_toprow;
wp->w_ntrows = (char)ntru;
++ntru; /* mode line */
curwp->w_toprow += (char)ntru;
curwp->w_ntrows = (char)ntrl;
while (ntru--)
lp = lforw(lp);
}
curwp->w_linep = lp; /* adjust the top lines */
wp->w_linep = lp; /* if necessary */
curwp->w_flag |= WFMODE | WFHARD;
wp->w_flag |= WFMODE | WFHARD;
return(TRUE);
}
/**
* Enlarge the current window. Find the window that loses space. Make sure it
* is big enough. If so, hack the window descriptions, and ask redisplay to do
* all the hard work. You don't just set "force reframe" because dot would
* move.
*/
int EnlargeWind(f, n)
int f, n;
{
WINDOW *adjwp;
LINE *lp;
int i;
if (n < 0)
return(ShrinkWind(f, -n));
if (wheadp->w_wndp == NULLWPTR)
{
MlWrite("Only one window");
return(FALSE);
}
if ((adjwp = curwp->w_wndp) == NULLWPTR)
{
adjwp = wheadp;
while (adjwp->w_wndp != curwp)
adjwp = adjwp->w_wndp;
}
if (adjwp->w_ntrows <= (char)n)
{
MlWrite("Impossible change");
return(FALSE);
}
if (curwp->w_wndp == adjwp) /* shrink below */
{
lp = adjwp->w_linep;
for (i = 0; i < n && lp != adjwp->w_bufp->b_linep; ++i)
lp = lforw(lp);
adjwp->w_linep = lp;
adjwp->w_toprow += (char)n;
}
else
{ /* shrink above */
lp = curwp->w_linep;
for (i = 0; i < n && lback(lp) != curbp->b_linep; ++i)
lp = lback(lp);
curwp->w_linep = lp;
curwp->w_toprow -= (char)n;
}
curwp->w_ntrows += (char)n;
adjwp->w_ntrows -= (char)n;
curwp->w_flag |= WFMODE | WFHARD;
adjwp->w_flag |= WFMODE | WFHARD;
return(TRUE);
}
/**
* Shrink the current window. Find the window that gains space. Hack at the
* window descriptions. Ask the redisplay to do all the hard work.
*/
int ShrinkWind(f, n)
int f, n;
{
WINDOW *adjwp;
LINE *lp;
int i;
if (n < 0)
return(EnlargeWind(f, -n));
if (wheadp->w_wndp == NULLWPTR)
{
MlWrite("Only one window");
return(FALSE);
}
if ((adjwp = curwp->w_wndp) == NULLWPTR)
{
adjwp = wheadp;
while (adjwp->w_wndp != curwp)
adjwp = adjwp->w_wndp;
}
if (curwp->w_ntrows <= (char)n)
{
MlWrite("Impossible change");
return(FALSE);
}
if (curwp->w_wndp == adjwp) /* grow below */
{
lp = adjwp->w_linep;
for (i = 0; i < n && lback(lp) != adjwp->w_bufp->b_linep; ++i)
lp = lback(lp);
adjwp->w_linep = lp;
adjwp->w_toprow -= (char)n;
}
else
{ /* grow above */
lp = curwp->w_linep;
for (i = 0; i < n && lp != curbp->b_linep; ++i)
lp = lforw(lp);
curwp->w_linep = lp;
curwp->w_toprow += (char)n;
}
curwp->w_ntrows -= (char)n;
adjwp->w_ntrows += (char)n;
curwp->w_flag |= WFMODE | WFHARD;
adjwp->w_flag |= WFMODE | WFHARD;
return(TRUE);
}
/**
* Resize the current window to the requested size
*/
int Resize(f, n)
int f, n;
{
int clines; /* current # of lines in window */
/* must have a non-default argument, else ignore call */
if (f == FALSE)
return(TRUE);
/* find out what to do */
clines = curwp->w_ntrows;
/* already the right size? */
if (clines == n)
return(TRUE);
return(EnlargeWind(TRUE, n - clines));
}
/**
* Pick a window for a pop-up. Split the screen if there is only one window.
* Pick the uppermost window that isn't the current window. An LRU algorithm
* might be better. Return a pointer, or NULL on error.
*/
WINDOW * WindPopUp()
{
WINDOW *wp;
if (
wheadp->w_wndp == NULLWPTR /* only 1 window */
&&
SplitWind(FALSE, 0) == FALSE /* and it won't split */
)
return(NULLWPTR);
wp = wheadp; /* find window to use */
while (wp != NULLWPTR && wp == curwp)
wp = wp->w_wndp;
return(wp);
}
/**
* Scroll the next window up (back) a page
*/
int NextWindUp(f, n)
int f, n;
{
NextWind(FALSE, 1);
BackPage(f, n);
PrevWind(FALSE, 1);
return(TRUE);
}
/**
* Scroll the next window down (forward) a page
*/
int NextWindDn(f, n)
int f, n;
{
NextWind(FALSE, 1);
ForwPage(f, n);
PrevWind(FALSE, 1);
return(TRUE);
}
/**
* Save ptr to current window
*/
int SaveWind()
{
swindow = curwp;
return(TRUE);
}
/**
* Restore the saved screen
*/
int RestWind()
{
WINDOW *wp;
/* find the window */
wp = wheadp;
while (wp != NULLWPTR)
{
if (wp == swindow)
{
curwp = wp;
curbp = wp->w_bufp;
UpMode();
return(TRUE);
}
wp = wp->w_wndp;
}
MlWrite("No such window");
return(FALSE);
}
/**
* Resize the screen, re-writing the screen
*/
int NewSize(f, n)
int f;
int n;
{
WINDOW *wp; /* current window being examined */
WINDOW *nextwp; /* next window to scan */
WINDOW *lastwp; /* last window scanned */
int lastline; /* screen line of last line of current window */
#if BMARKS
int bmark;
#endif
/* if the command defaults, assume the largest */
if (f == FALSE)
n = term.t_mrow + 1;
/* make sure it's in range */
if (n < 3 || n > term.t_mrow + 1)
{
MlWrite("Screen size out of range");
return(FALSE);
}
if (term.t_nrow == n - 1)
return(TRUE);
else if (term.t_nrow < n - 1)
{
/* go to the last window */
wp = wheadp;
while (wp->w_wndp != NULLWPTR)
wp = wp->w_wndp;
/* and enlarge it as needed */
wp->w_ntrows = (char)n - wp->w_toprow - 2;
wp->w_flag |= WFHARD | WFMODE;
}
else
{
/* rebuild the window structure */
nextwp = wheadp;
wp = NULLWPTR;
lastwp = NULLWPTR;
while (nextwp != NULLWPTR)
{
wp = nextwp;
nextwp = wp->w_wndp;
/* get rid of it if it is too low */
if (wp->w_toprow > (char)n - 2)
{
/* save the point/mark if needed */
if (--wp->w_bufp->b_nwnd == 0)
{
wp->w_bufp->b_dotp = wp->w_dotp;
wp->w_bufp->b_doto = wp->w_doto;
wp->w_bufp->b_markp = wp->w_markp;
wp->w_bufp->b_marko = wp->w_marko;
wp->w_bufp->b_anchorp = wp->w_anchorp;
wp->w_bufp->b_anchoro = wp->w_anchoro;
wp->w_bufp->b_fcol = wp->w_fcol;
#if BMARKS
for (bmark = 0; bmark < NBMARKS; bmark++)
{
wp->w_bufp->b_bmarkp[bmark] = wp->w_bmarkp[bmark];
wp->w_bufp->b_bmarko[bmark] = wp->w_bmarko[bmark];
}
#endif
}
/* Update curwp and lastwp if needed */
if (wp == curwp)
curwp = wheadp;
curbp = curwp->w_bufp;
if (lastwp != NULLWPTR)
lastwp->w_wndp = NULLWPTR;
/* free the structure */
free((char *)wp);
wp = NULLWPTR;
}
else
{
lastline = wp->w_toprow + wp->w_ntrows - 1;
if (lastline >= n - 2)
{
wp->w_ntrows = (char)n - wp->w_toprow - 2;
wp->w_flag |= WFHARD | WFMODE;
}
}
lastwp = wp;
}
}
/* screen is garbage */
term.t_nrow = n - 1;
sgarbf = TRUE;
return(TRUE);
}
/**
* Get screen offset of current line in current window
*/
int GetWindPos()
{
int sline; /* screen line from top of window */
LINE *lp; /* scannile line pointer */
/* search down the line we want */
lp = curwp->w_linep;
sline = 1;
while (lp != curwp->w_dotp)
{
++sline;
lp = lforw(lp);
}
/* and return the value */
return(sline);
}