// FtpDspInit.bcpl - Display initialization code
// Copyright Xerox Corporation 1979, 1980, 1981, 1982
// Last modified October 1, 1982  8:29 PM by Boggs

get "AltoDefs.d"
get "Streams.d"

external
[
// outgoing procedures
InitDisplay
FTPShowDisplayStream; MakeBar; MakeTitle

// incoming procedures
CharWidth; GetLinePos; InvertLine
ShowDisplayStream; CreateDisplayStream
InitializeContext; Title
Wss; Allocate; Free; Zero; Enqueue

// outgoing statics
currentYPos; lastShownStream
lineWords; fontHeight; fontWidth

// incoming statics
sysZone; sysFont
otherPupQ; ctxQ
]

static
[
currentYPos; lastShownStream
lineWords; fontHeight; fontWidth
]

manifest
[
black = true
white = false
]

structure String [ length byte; char↑1,1 byte ]

//-----------------------------------------------------------------------------------------
let InitDisplay() be
//-----------------------------------------------------------------------------------------
[
// sysFont
fontWidth = sysFont!-1 & 377b
fontHeight = (sysFont!-2+1) & -2
lineWords = lDCB+38*fontHeight

// top and bottom white bars
lastShownStream = MakeBar(white, 2)
ShowDisplayStream(lastShownStream, DSalone)
ShowDisplayStream(MakeBar(white, 1), DSbelow, lastShownStream)

// title window
let titleCtx = InitializeContext(Allocate(sysZone, 275), 275, Title, 3)
titleCtx!5 = MakeBar(white, (3*fontHeight)/2)
FTPShowDisplayStream(titleCtx!5)
titleCtx!3 = CreateDisplayStream(1, Allocate(sysZone, lineWords+10),
 lineWords+5, sysFont, 0, DSnone, sysZone)
FTPShowDisplayStream(titleCtx!3)
titleCtx!4 = CreateDisplayStream(1, Allocate(sysZone, lineWords+10),
 lineWords+5, sysFont, 0, DSnone, sysZone)
otherPupQ = Allocate(sysZone, 2); otherPupQ!0 = 0
Enqueue(ctxQ, titleCtx)
FTPShowDisplayStream(MakeBar(white, 8))
]

//-----------------------------------------------------------------------------------------
and FTPShowDisplayStream(st) be
//-----------------------------------------------------------------------------------------
[
ShowDisplayStream(st, DSbelow, lastShownStream)
lastShownStream = st
let dcb = st>>DS.fdcb
   [
   currentYPos = currentYPos + dcb>>DCB.height lshift 1
   if dcb eq st>>DS.ldcb break
   dcb = dcb>>DCB.next
   ] repeat
]

//-----------------------------------------------------------------------------------------
and MakeBar(background, nLines) = valof
//-----------------------------------------------------------------------------------------
[
structure Bar: [ fdcb word; ldcb word; @DCB ]
manifest lenBar = size Bar/16
let bar = Allocate(sysZone, lenBar, false, true)
Zero(bar, lenBar)
bar>>Bar.fdcb = lv bar>>Bar.next
bar>>Bar.ldcb = lv bar>>Bar.next
bar>>Bar.background = background
bar>>Bar.width = 0
bar>>Bar.height = nLines
resultis bar
]

//-----------------------------------------------------------------------------------------
and MakeTitle(string) be
//-----------------------------------------------------------------------------------------
[
let width = StringWidth(string)
let lenDS = lDCB + width*fontHeight +1
let title = CreateDisplayStream(1, Allocate(sysZone,lenDS), lenDS,
 sysFont, width)
Wss(title, string)
InvertLine(title, GetLinePos(title))
FTPShowDisplayStream(MakeBar(black, 2))
FTPShowDisplayStream(title)
FTPShowDisplayStream(MakeBar(black, 1))
]

//-----------------------------------------------------------------------------------------
and StringWidth(string) = valof
//-----------------------------------------------------------------------------------------
//returns the number of words necessary to display the string
[
let width = 8
for i = 1 to string>>String.length do
   width = width + CharWidth(sysFont, string>>String.char↑i)
resultis ((width+31)/16) & (-2)
]