// Window command Module QWINCOM.SR

get "BRAVO.DF"
get "CHAR.DF"
get "GINN.DF"

// Incoming Procedures

external [
	updatedisplay;
	linemark;
	updatelinemark;
	formatx;
	formaty;
	move;
	finddl;
	errhlt;
	backnlines;
	updatewindow;
	getvch;
	mapscrcp;
	prevscrvch;
	setbug;
	createdoce;
	establishww;
	mousedecode
	stripedecode
	pollmouse
	pollstripe
	pollkeyset
	initcomt
	splitwindow
	freedl
	createdisplay
	copydnww
	createdocm
	docalloc
	hidebug
	visible
	cpvisible
	getdoc
	wwmark
	updatewwmark
	buggedmenu
	menuitem
	resetmessage
	setmessage
	pollinput
	selectwholeww
	setsel
	dirflip
	elapsed
	setmenu
	marks
	locatebug
	stillselecting
	buggedsomething
	selectsel
	wipedoc
	bubblingww
	];

// Incoming Statics

external [
	vturning
	vwindows
	mdoc
	vcp;
	vchremain;
	cpscrt;
	vdlhint;
	vulmode;
	rgmaccp;
	rgcplast;
	rgupdate;
	rgcpfirst;
	rgxfirst;
	rgbs;
	rgbp;
	rgdoc;
	rgdlfirst;
	rgdllast;
	rgyfirst;
	rgylast;
	rgmaxdl;
	rgcpfdispl;
	rgxlast;
	vrgcc;
	vdoc;
	vww;
	vdl;
	vlb;
	vpw;
	vheight;
	rgpctb;
	chcom;
	vstripe;
	comt;
	macww
	abs
	rghpused
	pbmfirstfree
	heightstd
	mpdldcb
	selection;
	selaux;
	rgsdoc
	ddoc
	cursorstate ;
	vwwcurrent
	vcpfirst
	vcplast
	vxfirst
	vxlast
	rgdirty
	xbug
	ybug
	sww
	sdoc
	];

// Outgoing Procedures

external [
	modwindow
	divwindow
	delwindow
	newwindow
	];

structure DCB:
	[
	next	word;
	mode	bit 1;
	bw	bit 1;
	htab	bit 6;
	nwrds	bit 8;
	sa	word;
	slc	word;
	];

let divwindow(cmdcode) = wwcommand(cdivwindow,cmdcode,false,ssplit)

and modwindow(cmdcode) = wwcommand(cmodwindow,cmdcode,true,sbound)

and delwindow(cmdcode) = wwcommand(cdelwindow,cmdcode,false,swindow)

and newwindow(cmdcode) = wwcommand(cnewwindow,cmdcode,false,snew)

and wwcommand(proc, cmdcode, oksys, bugshape) = valof
[
unless buggedsomething(true, -1, oksys, bugshape) do resultis 0 ;
let ans = proc(cmdcode)
resultis ans
]

and cdivwindow(cmdcode) = valof
[ 
while stillselecting(-1,0, false, swindow) do
	[
	if cdelwindow(0) eq 0 then resultis 0 ;
	setbug(vstripe eq pollstripe(false)?ssplit, swindow) ;
	]
if vstripe ne pollstripe(false) % rgdoc ! vww eq docsys %
	rgdoc ! vww eq mdoc then resultis 0;
splitwindow(ybug,rgdoc ! vww);
rgcpfdispl ! vww = rgcpfirst ! vdl ;
rgsdoc!vww=rgsdoc!(vww-1)
bubblingww(vww, 1)
vwwcurrent = vww ;
vturning = true
resultis 0;
] // end of cdivwindow()

and cmodwindow(cmdcode) = valof
[ 
let wwc,n,dist = nil,nil,nil;
let closest = 10000;
for ww = 2 to macww-2 do
	if abs(ybug-rgylast ! ww) ls closest then
		[
		dist = ybug-rgylast ! ww
		closest = abs(dist);
		wwc = ww;
		];
let theight = heightstd;
let dl = vdl ;
while stillselecting(-1,0, true, swindow) do
	[
	let tdl = vdl;
	let tdoc = -1;
	let ty = rgylast ! wwc ;
	if dl eq vdl & ty ls ybug & abs(ty-ybug) ls theight then
		loop ;
	dl = vdl ;
	if (wwc+2 ne macww & ybug gr rgylast ! (wwc+1)) %
		ybug ls rgyfirst ! wwc then loop
	if ybug gr 773 then ybug = 773 ;
	rgylast ! wwc = ybug;
	vww = wwc + 1 ;
	if vww ls macww then
		[
		establishww(vww) ;
		test ybug gr ty
			ifso rgcpfdispl ! vww =
				rgcpfirst ! dl;
			ifnot	[
				n = rgdlfirst ! vww - dl;
				vcp = rgcpfdispl ! vww ;
					[
					n = n-
					  backnlines(vww,vcp,n);
					]
				    repeatuntil vcp eq 0 % n eq 0
				rgcpfdispl ! vww = vcp ;
				// rgxlast ! (rgdlfirst ! vww) =-1;
				];
		];
	createdisplay()
	vturning = true
	updatedisplay()
	];
resultis 0;
] // end of cmodwindow

and cdelwindow(cmdcode) = valof
[
if cmdcode eq 0 &
      comt ! (pollmouse()+(vstripe-sstripe) lshift 3) ne sdelwindow then
	resultis -1
unless buggedsomething(true, -1, false, swindow) then resultis waitup() ;
let w = vww ;
let d = rgdoc ! w ;
let sel = nil ;
unless 2 le w & w le macww-2 & macww gr 4 do
	[ // %% setmessage
	setmessage(" That window is permanent: it can't be deleted")
	resultis waitup() ;
	]
let copyup = w eq 2 % (rgdoc!(w-1) ne d & rgdoc!(w+1) eq d);
let elsewhere = -1 ;
for i = macww-1 to 2 by -1 do
	if i ne w & rgdoc ! i eq d &
	    (rgsdoc ! i eq rgsdoc ! w % elsewhere ls 0) then
		elsewhere = i ;
if rgdoc ! w ne ddoc & rgdirty ! d & elsewhere ls 0 then
	[
	setmessage(" Before deleting window, File or Cancel document") ;
	vwwcurrent = w
	resultis waitup()
	]
let reselect = false ;
if selection >> SEL.doc eq d %
	selaux >> SEL.doc eq d then
		reselect = elsewhere;
let okdel = true ;
for i = rgdlfirst ! w to rgmaxdl ! w - 1 do
	mpdldcb ! i >> DCB.bw = 1 ;
while stillselecting(-1,0, false, swindow) do
	if vstripe ne pollstripe(false) % vww ne w then okdel = false;
for i = rgdlfirst ! w to rgmaxdl ! w  - 1 do
	mpdldcb ! i >> DCB.bw = 0 ;
unless okdel do resultis 0 ;
let teodc = rgmaccp ! sdoc -1 ;
if reselect then for t = 0 to 1 do
	[
	sel = t eq 0? selaux, selection ;
	if sel >> SEL.doc eq d then
	test reselect ls 0
	ifso	test t eq 0
		ifso	selectsel(sel, sww, teodc, teodc-t)
		ifnot	[
			vwwcurrent = copyup? 3,2 ;
			selectsel(sel, vwwcurrent,
				rgcpfdispl ! vwwcurrent) ;
			] ;
	ifnot	[
		sel >> SEL.ww = reselect ;
		vwwcurrent = reselect ;
		]
	];
test copyup & w+1 ne macww
	ifso	rgyfirst ! (w+1) = rgyfirst ! w
	ifnot	rgylast ! (w-1) = rgylast ! w;
if d ne ddoc & elsewhere eq -1 then wipedoc(d)
rghpused ! w = 0;
for dl = rgdlfirst ! w to rgdllast ! w do
	[
	pbmfirstfree = 1; freedl(dl);
	];
for ww = w to macww-2 do
	copydnww(ww,ww+1);
bubblingww(w, -1)
macww = macww-1;
createdisplay();
resultis 0;
] // end of cdelwindow

and waitup() = valof
[
while stillselecting(-1, 0, false, swindow) do loop ;
resetmessage() ; // %%
resultis 0
]

and cnewwindow(cmdcode) = valof
[
while stillselecting(-1,0, false, swindow) do
	[
	if cdelwindow(0) eq 0 then resultis 0 ;
	setbug(vstripe eq pollstripe(false)?snew, swindow) ;
	]
let tww = vww ;
if vstripe ne pollstripe(false) % vdoc eq docsys % tww ge macww-1 %
	vdoc eq mdoc % rgdoc!tww eq rgdoc!(tww+1) then resultis 0;
let tdoc = docalloc();
if tdoc eq -1 then resultis 0;
createdocm(tdoc);
splitwindow(ybug, tdoc);
bubblingww(tww+1, 1)
getdoc(tww+1,ddoc)
vwwcurrent = tww+1
resultis 0
]