// October 10, 1979  10:07 AM by Taft			*** OVERLAY A ***

get "zpDefs.bcpl"
get "AltoDefs.d"


// outgoing procedures:

external [
	refreshDisplay
	itIsARectangle
	menuDisplay
	wipeDisplay
	gridDisplay
	frameDisplay
	turnDisplayOff
	]
  


// incoming procedures:

external [
	SetBlock		// SYSTEM
	Zero

	drawSpline		// ZPDRAW
	XORdot

	showText		// ZPTEXT
	complementBox
	fontAddress

	paintDot		// ZPCONVERT
	paintString

	equal			// ZPUTIL

	flushItem		// ZPITEM		(same overlay)
	]



// incoming statics:

external [
	dsp			// SYSTEM

	@bitmap			// ZPINIT1
	@switchDCB
	@bitmapSize
	@scanlineWidth
	@height
	@Xmax
	@Ymax
	@splineTable
	@maxSplineID
	@maxTextID
	@textTable
	@fontDefTable
	@fontFile
	@dspFont
	@dspFontAddress
	@font
	@brush
	@color
	@gridSpacing
	@versionX

	@showGrid		// ZPEDIT
	@colorOn
	@revSelectTable

	brushFont		// ZPCONVERT
	]


// static:

static [
	@savedDCBnext=0
	]



//*****************************************************************
// display area refresh
//*****************************************************************


let refreshDisplay(partialRefresh; numargs na) be [refreshDisplay
	if na eq 0 then partialRefresh=false
	if partialRefresh then turnDisplayOff()
	Zero(bitmap, bitmapSize)
	paintMenu()
	if splineTable!0 then for id=1 to maxSplineID do [
		let splinePointer=splineTable!id
		unless splinePointer loop
		if partialRefresh & itIsARectangle(splinePointer) loop
		drawSpline(id)
		]
	unless partialRefresh then [
		if textTable!0 then for f=0 to maxFont-1 do [
			for t=1 to maxTextID do [
				let textPointer=textTable!t
				unless textPointer loop
				unless textPointer>>TEXT.font eq f loop
				showText(t)
				]
			]
		frameDisplay(1)
		if showGrid then gridDisplay()
		turnDisplayOn()
		]
	]refreshDisplay


and itIsARectangle(splinePointer) =
	(splinePointer>>SPLINE.type ne regSpline) & (splinePointer>>SPLINE.shape ne rBrush)


and menuDisplay() be [menuDisplay
	clearMenu()
	paintMenu()
	]menuDisplay


and clearMenu() be [clearMenu
	let w=bitmap
	for k=1 to height do [
		Zero(w, 4)
		w=w+scanlineWidth
		]
	]clearMenu


and paintMenu() be [paintMenu
	let menuTable= versionX ?
		table [
			rDotFont+3; sDotFont+3; hBarFont+3; vBarFont+3; -1;
			-1; brushSymbol; makeSymbol; deleteSymbol;
			-1; selectSymbol; mTransf2Symbol; mTransf4Symbol;
			mTransf6Symbol; -1; -1; -1;
			lineFont; lineFont+1; lineFont+2; lineFont+3; -1;
			-1; dashSymbol; cyclicSymbol; -1;
			-1; -1; cTransf2Symbol; cTransf4Symbol;
			cTransf6Symbol; -1; -1; -1; ],
		table [
			rDotFont+3; sDotFont+3; hBarFont+3; vBarFont+3;
			-1; blueSymbol; redSymbol; greenSymbol; blackSymbol;
			brushSymbol; makeSymbol; selectSymbol; mTransf2Symbol;
			mTransf4Symbol; mTransf6Symbol; -1; -1;
			lineFont; lineFont+1; lineFont+2; lineFont+3;
			-1; yellowSymbol; cyanSymbol; magentaSymbol; -1;
			dashSymbol; cyclicSymbol; deleteSymbol; cTransf2Symbol;
			cTransf4Symbol; cTransf6Symbol; -1; -1 ]

	// display menu symbols
	let w0=bitmap+symbolPos*scanlineWidth
	let menuSkip=symbolHeight*scanlineWidth
	let w=w0+symbolWidth
	for k=0 to 33 do [
		if menuTable!k ne -1 then
			paintDot(w, symbolPos, brushFont+menuTable!k)
		w= (k eq 16) ? w0, w+menuSkip
		]

	// show current brush
	manifest [ c1=4*symbolHeight+8; a1=#377; a2=#177400 ]
	w0=bitmap+c1*scanlineWidth+1
	@w0= a1; @(w0+1)=a2
	w=w0+scanlineWidth
	for k=1 to 14 do [
		@w=#200; @(w+1)=#400
		w=w+scanlineWidth
		]
	@w= a1; @(w+1)=a2
	paintDot(w0+scanlineWidth, 12, brushFont+brush)
	w=bitmap+((brush<<BRUSH.thickness)*symbolHeight+6)*scanlineWidth
	complementBox(w, 6, 24, 24)
	w=bitmap+((brush<<BRUSH.shape)*symbolHeight+6)*scanlineWidth+2
	complementBox(w, 6, 24, 24)

	// color menu (not in version X)
	unless versionX then [
		manifest [ c00=8*symbolHeight+6; c0=c00+12 ]
		paintString("color", scanlineWidth,
			10, bitmap+c00*scanlineWidth, dspFontAddress)
		paintString((colorOn ? "ON", "OFF"), scanlineWidth,
			(colorOn ? 4, 7), bitmap+c0*scanlineWidth, dspFontAddress)
		let hTable= table [ 0;
			2*symbolHeight + c1 -2; 3*symbolHeight + c1 -2; symbolHeight + c1 -2;
			symbolHeight + c1 -2; 3*symbolHeight + c1 -2; 2*symbolHeight + c1 -2;
			4*symbolHeight + c1 -2 ]
		let bTable= table [ 0; 0; 0; 2; 0; 2; 2; 2 ]
		complementBox(bitmap + (hTable!color)*scanlineWidth + bTable!color, 6, 24, 24)
		]

	// font menu
	manifest [ c2=symbolHeight*15 + 8]
	let wtab=vec 4
	wtab!2=bitmap+c2*scanlineWidth
	wtab!0=wtab!2+2
	wtab!3=wtab!2+symbolHeight*scanlineWidth
	wtab!1=wtab!3+2
	for f=0 to 3 do [
		if fontFile>>FONTFILE.length↑f eq 0 & f ne dspFont loop
		let stringPointer=lv((fontDefTable!f)>>FONTDEF.dispCode)
		// don't try to understand this:
		paintString(stringPointer, scanlineWidth, 7,
			(equal(stringPointer, "Q") ? wtab!f - 14*scanlineWidth, wtab!f),
			fontAddress(f))
		]
	complementBox(wtab!font-4*scanlineWidth, 4, 24, 24)
	]paintMenu



and frameDisplay(b) be [frameDisplay
	let ptl=bitmap+margin
	let ptr=bitmap+scanlineWidth-1
	let w= b ? -1, 0
	SetBlock(ptl, w, scanlineWidth-margin)
	SetBlock(ptl+bitmapSize-scanlineWidth, w, scanlineWidth-margin)
	test b
	ifso for k=1 to height do [
		@ptl=@ptl % #100000
		@ptr=@ptr % 1
		ptl=ptl+scanlineWidth
		ptr=ptr+scanlineWidth
		]
	ifnot for k=1 to height do [
		@ptl=@ptl & #077777
		@ptr=@ptr & #177776
		ptl=ptl+scanlineWidth
		ptr=ptr+scanlineWidth
		]
	]frameDisplay



and wipeDisplay() be [wipeDisplay
	unless splineTable!0 % textTable!0 return
	for id=1 to maxSplineID do if splineTable!id then
		flushItem(id)
	for id=1 to maxTextID do if textTable!id then
		flushItem(textFlag+id)
	refreshDisplay()
	]wipeDisplay




and gridDisplay() be [gridDisplay
	let y=0
	[ let x=0
	  [ XORdot(x, y)
	    x=x+gridSpacing
	    if x gr Xmax break
	    ] repeat
	  y=y+gridSpacing
	  if y gr Ymax break
	  ] repeat
	]gridDisplay



and turnDisplayOff() be [
	if savedDCBnext return
	savedDCBnext=switchDCB>>DCB.next
	switchDCB>>DCB.next=0
	switchDCB>>DCB.background=1
	]



and turnDisplayOn() be [
	unless savedDCBnext return
	switchDCB>>DCB.background=0
	switchDCB>>DCB.next=savedDCBnext
	savedDCBnext=0
	]