// SPLIT.SR

get "BRAVO1.DF";
get "CHAR.DF";
get "SELECT.DF";
get "DISPLAY.DF";
get "DOC.DF";
get "VM.DF";
get "FORMAT.DF"

// Incoming procedures

external
	[ formaty;
	mpDlDld
	initCnDcb;
	chaseCnDcb;
	incCnDcb;
	replaceCnDcb;
	updatewindow;
	move;
	errhlta;
	min
	freedl
	updateDlMaps
	]

// Incoming statics

external
	[ mpWwWwd;
	vww
	rgyfirst
	vYdofDl
	vdl
	macww
	rgpctb
	rgmaccp
	pbmfirstfree
	]

// Outgoing procedures

external
	[
	insertWindow
	deleteWindow
	SplitWw
	ValidateSel
	ugt
	setsel
	]

// S P L I T W W
//
let SplitWw(y,wgd,wwNext) be
[
let yold = y
y = (y+1) & (-2)
let tdyd = y - yold
formaty(y)
let wwd = nil
let wwdPrev = mpWwWwd ! vww
let tDl = nil
let tDcb = wwdPrev>>WWD.dcbFirst
let dcbofDl = (mpDlDld(vdl))>>DLD.dcb
let dcbPrev = valof [ 
	test (vdl eq -1) ifnot
		until tDcb>>DCB.next eq dcbofDl do
			tDcb = tDcb>>DCB.next 
	ifso	dcbofDl = tDcb>>DCB.next
	resultis tDcb
	] 
let cww = wgd>>WGDTABLE.cww
// subtract factor for Line Width, and make sure we're even
for i = 1 to cww do
	[
	if (wgd>>WGDTABLE.dyd↑i) << odd then wgd>>WGDTABLE.dyd↑i =
		wgd>>WGDTABLE.dyd↑i - 1
	wgd>>WGDTABLE.dyd↑i = wgd>>WGDTABLE.dyd↑i -dYdLineWidth
	wgd>>WGDTABLE.ydFirst↑i = wgd>>WGDTABLE.ydFirst↑i + tdyd
	]
let tww = vww
insertWindow(tww+1,cww)
if cww eq 1 then (mpWwWwd!(tww+1))>>WWD.fDirty =
	(mpWwWwd!tww)>>WWD.fDirty
tww = vww + 1
for i = 1 to cww do
	[
	wwd = mpWwWwd!tww
	rgyfirst!tww = wgd>>WGDTABLE.ydFirst↑i
	wwd>>WWD.doc = wgd>>WGDTABLE.doc↑i
	wwd>>WWD.cpFDispl = wgd>>WGDTABLE.cpFDispl↑i
	tDl = (mpWwWwd!(tww-1))>>WWD.dlLast
	wwd>>WWD.dlLast = tDl
	wwd>>WWD.dlFirst = tDl+1
	wwd>>WWD.bw = wgd>>WGDTABLE.bonw↑i
	wwd>>WWD.hpUsed = 0
	wwd>>WWD.fUpdate = true
	wwd>>WWD.xdUd = wgd>>WGDTABLE.xdUd↑i
// 	wwd>>WWD.mag = wgd>>WGDTABLE.mag↑i
	wwd>>WWD.fHd = wgd>>WGDTABLE.fHd↑i
	tww = tww + 1
	]

let tCnDcbOld = vec lCnDcb
let tCnDcbNew = vec lCnDcb
initCnDcb(tCnDcbOld,dcbofDl,1)
let dcbWwNext = (mpWwWwd ! (wwNext))>>WWD.dcbFirst
until (tCnDcbOld>>CNDCB.dcbLast)>>DCB.next eq dcbWwNext do
	chaseCnDcb(tCnDcbOld,1)
initCnDcb(tCnDcbNew,0,0)
unless y - vYdofDl eq 0 do
		incCnDcb(tCnDcbNew,0,y-vYdofDl,0,0,false)
for i = 1 to cww do
	[
	incCnDcb(tCnDcbNew,0,dYdLineWidth,0,0,true)
	wwd = mpWwWwd!(vww+i)
	wwd>>WWD.dcbFirst = tCnDcbNew>>CNDCB.dcbLast
	incCnDcb(tCnDcbNew,0,wgd>>WGDTABLE.dyd↑i,0,0,
		wgd>>WGDTABLE.bonw↑i)
	]
replaceCnDcb(tCnDcbOld,tCnDcbNew,dcbPrev,vYdofDl)
(mpWwWwd!vww)>>WWD.hpUsed = GetHpUsed(vww)
for tww = vww to wwNext - 1 do 
	updatewindow(tww)

vww = wwNext - 1
]


and insertWindow(wwparam,cww,wwgroup; numargs na) be
[
if macww eq maxww then errhlta(76)
let wwdTo,wwdFrom = nil,nil

let wwGroupAbove = (mpWwWwd!(wwparam-1))>>WWD.wwgroup
let wwNextGroup = wwparam-1
until (mpWwWwd!wwNextGroup)>>WWD.wwgroup ne wwGroupAbove do
	wwNextGroup = wwNextGroup + 1
for ww = macww to wwparam by -1 do
	[
	wwdTo = mpWwWwd!(ww+cww)
	wwdFrom = mpWwWwd!ww
	move(wwdFrom,wwdTo,lWwd)
	rgyfirst!(ww+cww) = rgyfirst!ww
	]
macww = macww+cww

//fix up groups
if na gr 2 then
	for ww = wwparam to wwparam+cww do
		(mpWwWwd!ww)>>WWD.wwgroup = wwgroup

let wwMax = min(wwNextGroup+cww,macww)
for ww = wwMax to macww do
	(mpWwWwd!ww)>>WWD.wwgroup = 
		(mpWwWwd!ww)>>WWD.wwgroup + cww
]


and deleteWindow(ww,cww) be
[
// delete cww windows beginning at ww
let wwGroupAbove = (mpWwWwd!(ww-1))>>WWD.wwgroup
let wwGroupBelow = (mpWwWwd!(ww+cww))>>WWD.wwgroup
let wwdTo,wwdFrom,wwStart,tww = nil,nil,nil,nil
let wwdFirst = mpWwWwd ! ww; let wwdLast = mpWwWwd ! (ww+cww-1)
let wwdNext = mpWwWwd ! (ww+cww)
let dlFirst = wwdFirst>>WWD.dlFirst
for tww = ww to ww+cww-1 do
	(mpWwWwd!tww)>>WWD.hpUsed = 0
for dl=dlFirst to wwdLast >> WWD.dlLast do
	[ let dld = mpDlDld(dl)
	pbmfirstfree = 1; freedl(dl,dld);
	]
let dlMacWw = wwdNext>>WWD.dlFirst
let dDlMac = dlFirst-dlMacWw
test  (ww+cww ls macww) ifso
	updateDlMaps(ww,dlFirst,0,dlMacWw,dlFirst)
ifnot	dDlMac = 0

for tww = ww to macww-cww+1 do
	[ (mpWwWwd!tww)>>WWD.hpUsed = 0
	wwdTo = mpWwWwd!tww
	wwdFrom = mpWwWwd!(tww+cww)
	move(wwdFrom,wwdTo,lWwd)
	rgyfirst!tww = rgyfirst!(tww+cww)
	]
macww = macww-cww

test wwGroupAbove eq wwGroupBelow
	ifso [
	tww = ww + 1
	until tww eq macww % (mpWwWwd!tww)>>WWD.wwgroup 
		ne wwGroupBelow do tww = tww+1
	wwStart = tww
	]
ifnot	wwStart = ww

for tww = wwStart to macww do
	(mpWwWwd!tww)>>WWD.wwgroup = 
		(mpWwWwd!tww)>>WWD.wwgroup-cww

for tww = ww to macww-1 do
	[
	let wwd = mpWwWwd ! tww
	wwd>>WWD.dlFirst = wwd>>WWD.dlFirst+dDlMac
	wwd>>WWD.dlLast = wwd>>WWD.dlLast+dDlMac
	] 


]



and GetHpUsed(ww) = valof
[
let wwd = mpWwWwd!ww
let dlFirst,dlLast = wwd>>WWD.dlFirst,wwd>>WWD.dlLast
if dlFirst ls dlLast resultis 0
let nwords,dld = 0,nil

for dl = dlFirst to dlLast do
	[
	dld = mpDlDld(dl)
	nwords = nwords + (dld>>DLD.nwrds * dld>>DLD.dYdBm)
	]
resultis nwords
]


and ValidateSel(sel) be
[
if sel>>SEL.ww ls macww
	& (mpWwWwd!(sel>>SEL.ww))>>WWD.doc eq sel>>SEL.doc
	& rgpctb!(sel>>SEL.doc) ne -1
	& ugt(rgmaccp!(sel>>SEL.doc), sel>>SEL.cplast)
then return
setsel(sel,0,0)
sel>>SEL.ww = macww-1
sel>>SEL.doc = (mpWwWwd ! (macww-1)) >> WWD.doc
sel>>SEL.type = schar
]