NEW
  AUTO 3,1
*--------------------------------------
WND.Init		>PUSHW L.DEVNAME.GFX
				>PUSHBI 0
				>SYSCALL2 open
				bcs .9

				sta WND.Screen+S.OBJ.hPARENT

				>LDYA L.DCB.GFX
				>STYA IOCTL+S.IOCTL.BUFPTR
				lda #S.IOCTL.S.GETDCB
				sta IOCTL+S.IOCTL.S

				>PUSHB WND.Screen+S.OBJ.hPARENT
				>PUSHBI IOCTL.STATUS
				>PUSHW L.IOCTL
				>SYSCALL2 IOCTL

.9				rts
*--------------------------------------
WND.Setup
*				stz WND.Stack.Top
*				stz MENU.Stack.Cnt

				>LDYA DCB.GFX+S.DCB.GFX.W
				>STYA WND.Screen+S.OBJ.W
				>STYA WND.Desktop+S.OBJ.W
				>STYA WND.SysBar+S.OBJ.W
				cpy #0
				bne .1

				dec

.1				dey
				>STYA CLIP.Screen+S.RECT.X2
*--------------------------------------
				lda DCB.GFX+S.DCB.GFX.H
				sta WND.Screen+S.OBJ.H

				lda SYSFON.Hp2
				sta WND.SysBar+S.OBJ.H

				lda DCB.GFX+S.DCB.GFX.H
				sec
				sbc SYSFON.Hp2
				sta WND.Desktop+S.OBJ.H

				sta WND.SysBar+S.OBJ.Y1
				sta RECT.SysBut+S.RECT.Y1
				sta RECT.SysWNDs+S.RECT.Y1
				sta RECT.SysClk+S.RECT.Y1

				clc
				adc SYSFON.Hp1

				sta RECT.SysBut+S.RECT.Y2
				sta RECT.SysWNDs+S.RECT.Y2
				sta RECT.SysClk+S.RECT.Y2
				sta CLIP.Screen+S.RECT.Y2

				>LDYA L.WND.Screen
				>STYA WND.Desktop+S.OBJ.hPARENT
				>STYA WND.SysBar+S.OBJ.hPARENT

*				>LDYA L.WND.Screen
				jsr OBJ.SetX2Y2

				>LDYA L.WND.Desktop
				jsr OBJ.SetX2Y2

				>LDYA L.WND.SysBar
				jsr OBJ.SetX2Y2
*--------------------------------------
				>LDYA L.WND.SysBar.2osX
				ldx hSYSFON
				jsr FON.GetTextSize

				lda CB.Cache+S.CB.SrcW
				clc
				adc #25					4 + BM width(16) + 1 + "2osX" + 4
				sta RECT.SysBut+S.RECT.X2
				sta RECT.SysWNDs+S.RECT.X1
*--------------------------------------
				>LDYA L.WND.SysBar.Clk
				ldx hSYSFON
				jsr FON.GetTextSize

				lda CB.Cache+S.CB.SrcW
				clc
				adc #2					1 + "00:00" + 1
				sta CB.Cache+S.CB.SrcW

				lda DCB.GFX+S.DCB.GFX.W
				sec
				sbc CB.Cache+S.CB.SrcW
				sta RECT.SysWNDs+S.RECT.X2
				sta RECT.SysClk+S.RECT.X1

				lda DCB.GFX+S.DCB.GFX.W+1
				sbc #0
				sta RECT.SysWNDs+S.RECT.X2+1
				sta RECT.SysClk+S.RECT.X1+1

				lda DCB.GFX+S.DCB.GFX.W
				clc
				sbc #0
				sta RECT.SysClk+S.RECT.X2
				lda DCB.GFX+S.DCB.GFX.W+1
				sbc #0
				sta RECT.SysClk+S.RECT.X2+1

				jsr SYSBAR.Show

				rts
*--------------------------------------
WND.Quit		lda WND.Screen+S.OBJ.hPARENT
				beq .8

				pha
				>PUSHBI IOCTL.CLOSE
				>PUSHWZ
				pla
				>SYSCALL2 IOCTL

.8				clc
WND.Quit.RTS	rts
*--------------------------------------
* guiNewWnd(sFlags, iX1, iY1, iW, iH)
*--------------------------------------
WND.New			ldy WND.Stack.Top
				cpy #WND.MAX
				bcs .98

				lda #S.OBJ.T.WND
				jsr OBJ.New
				bcs .99

*				>STYA ZPObjPtr
				phx

				ldy #S.OBJ.H
				jsr OBJ.PullWordAtY

				ldy #S.OBJ.W
				jsr OBJ.PullWordAtY

				jsr OBJ.PullY1X1

				>PULLA
				bit #S.WND.F.RESIZE
				beq .2

				ora #S.OBJ.F.BORDER

.2				ldy #S.OBJ.F
				sta (ZPObjPtr),y

				ldy #S.PS.PID
				lda (pPs),y

				ldy #S.OBJ.hOWNER
				sta (ZPObjPtr),y

				lda L.WND.Desktop
				ldx L.WND.Desktop+1
				ldy #S.OBJ.hPARENT
				jsr OBJ.SetAXAtY

				lda L.WND.fPaint
				ldx L.WND.fPaint+1
				ldy #S.WND.fPaint
				jsr OBJ.SetAXAtY

				pla						ID

*				clc
				rts

.98				lda #E.OOH

.99				>POP 9

				rts
*--------------------------------------
WND.SetMBar		>PULLW ZPPtr1

				jsr OBJ.PullHWND

				>LDYA ZPPtr1
				jsr MBAR.New
				bcs .9

				ldy #S.WND.pMBAR
				sta (ZPWNDPtr),y

*				clc

.9				rts
*--------------------------------------
WND.ClrStatus	ldy #S.OBJ.S
				eor #$ff
				and (ZPWNDPtr),y
				sta (ZPWNDPtr),y
				rts
*--------------------------------------
WND.SetStatus	ldy #S.OBJ.S
				ora (ZPWNDPtr),y
				sta (ZPWNDPtr),y
				rts
*--------------------------------------
* ZPWNDPtr
* ZPObjPtr
*--------------------------------------
WND.AddChild.I	ldy #S.OBJ.pChilds
				lda (ZPWNDPtr),y
				sta ZPPtr1
				iny
				lda (ZPWNDPtr),y
				sta ZPPtr1+1
				ora ZPPtr1
				bne .1

				lda ZPObjPtr+1
				sta (ZPWNDPtr),y
				dey
				lda ZPObjPtr
				sta (ZPWNDPtr),y
				clc
				rts

.1				ldy #S.OBJ.pNext
				lda (ZPPtr1),y
				tax
				iny
				ora (ZPPtr1),y
				beq .2

				lda (ZPPtr1),y
				stx ZPPtr1
				sta ZPPtr1+1
				bra .1

.2				lda ZPObjPtr+1
				sta (ZPPtr1),y
				dey
				lda ZPObjPtr
				sta (ZPPtr1),y
				clc
				rts
*--------------------------------------
WND.Enter
WND.Leave
*--------------------------------------
WND.Destroy		clc
				rts
*--------------------------------------
WND.Activate	jsr OBJ.GetHWNDA


				clc
WND.Activate.RTS
				rts
*--------------------------------------
WND.Bring2Front	ldx WND.Stack.Idx
				cpx WND.Stack.Top
				beq WND.Activate.RTS

				lda WND.Stack-1,x
				pha

.1				lda WND.Stack,x
				sta WND.Stack-1,x
				inx
				cpx WND.Stack.Top
				bne .1

				dec WND.Stack.Top

.2				pla
*--------------------------------------
* guiShowWnd( hWND )
*--------------------------------------
WND.Show		sta WND.hWND

				>LDYA L.WND.Desktop
				jsr CLIP.GetRect

				ldx WND.Stack.Top
				beq .1

				lda WND.Stack-1,x
				jsr OBJ.GetHWNDA

				lda #S.WND.S.ACTIVE
				jsr WND.ClrStatus

				jsr WND.DrawTitleBar

.1				lda WND.hWND
				ldx WND.Stack.Top

				sta WND.Stack,x
				inc WND.Stack.Top
*--------------------------------------
WND.Draw		jsr OBJ.GetHWNDA		Y,A = ZPWNDPtr

				jsr OBJ.SetX2Y2

				jsr WND.ResetInnerW

				lda #S.WND.S.ACTIVE
				jsr WND.SetStatus

				>LDYA ZPWNDPtr
				ldx #1
				jsr OBJ.DrawBorders
*--------------------------------------
.1				ldy #S.OBJ.Y1
				lda (ZPWNDPtr),y
				sta Counter
				iny
				lda (ZPWNDPtr),y
				sta Counter+1

				ldy #S.WND.pTITLE+1
				lda (ZPWNDPtr),y
				bne .2

				ldy #S.OBJ.S
				lda (ZPWNDPtr),y
				and #S.WND.F.MOVE+S.WND.F.CLOSE+S.WND.F.MIN+S.WND.F.MAX
				beq .3

.2				lda SYSFON.Hp2
				jsr WND.Add2Counter

				ldy #S.WND.TBarY2
				jsr WND.SetAXAtY

				lda #S.WND.S.HASTBAR
				jsr WND.SetStatus

				jsr WND.DrawTitleBar
				bcs .9
*--------------------------------------
.3				ldy #S.WND.pMBAR
				lda (ZPWNDPtr),y
				beq .4

				ldy #S.WND.MBarY1
				jsr WND.SetCounterAtY

				lda SYSFON.Hp1
				jsr WND.Add2Counter

				ldy #S.WND.MBarY2
				jsr WND.SetAXAtY

				lda #S.WND.S.HASMBAR
				jsr WND.SetStatus

				ldy #S.WND.pMBAR
				lda (ZPWNDPtr),y
				jsr MBAR.Draw
				bcs .9
*--------------------------------------
.4				jsr CB.GetBuf
				bcs .9

				ldy #S.WND.InnerY1
				jsr WND.SetCounterAtY

				ldy #S.WND.pSTATUS+1
				lda (ZPWNDPtr),y
				beq .8

				lda SYSFON.H
				jsr WND.Add2Counter

				ldy #S.WND.InnerY2
				lda (ZPWNDPtr),y
				clc						+1
				sbc SYSFON.H
				sta (ZPWNDPtr),y
				tax
				iny
				lda (ZPWNDPtr),y
				sbc #0
				sta (ZPWNDPtr),y

				inx
				bne .5

				inc

.5				ldy	#S.WND.SBarY1+1
				sta (ZPWNDPtr),y
				dey
				txa
				sta (ZPWNDPtr),y

				lda #S.WND.S.HASSBAR
				jsr WND.SetStatus

				jsr WND.DrawStatusBar
*--------------------------------------
.8				ldy #S.WND.InnerH
				lda (ZPWNDPtr),y
				sec
				sbc Counter
				sta (ZPWNDPtr),y

				iny
				lda (ZPWNDPtr),y
				sbc Counter+1
				sta (ZPWNDPtr),y

				jsr CB.FlushBuf

				lda WND.hWND
				pha
				jmp WND.Paint1

.9				rts
*--------------------------------------
WND.ResetInnerW	ldy #S.OBJ.X1
				ldx #S.WND.InnerX1
				jsr .1

				ldy #S.OBJ.Y1
				ldx #S.WND.InnerY1

				ldy #S.OBJ.X2
				ldx #S.WND.InnerX2
				jsr .1

				ldy #S.OBJ.Y2
				ldx #S.WND.InnerY2
				jsr .1

				ldy #S.OBJ.W
				ldx #S.WND.InnerW
				jsr .1

				ldy #S.OBJ.H
				ldx #S.WND.InnerH

.1				lda (ZPWNDPtr),y
				pha
				iny
				lda (ZPWNDPtr),y
				phx
				ply
				iny
				sta (ZPWNDPtr),y
				dey
				pla
				sta (ZPWNDPtr),y
				rts
*--------------------------------------
WND.Add2Counter	clc
				adc Counter
				sta Counter
				bcc .8

				inc Counter+1

.8				ldx Counter+1
				rts
*--------------------------------------
WND.SetCounterAtY
				lda Counter
				ldx Counter+1
*--------------------------------------
WND.SetAXAtY	sta (ZPWNDPtr),y
				iny
				txa
				sta (ZPWNDPtr),y

WND.SetAXAtY.9	rts
*--------------------------------------
WND.DrawTitleBar
				jsr CB.GetBuf
				bcs WND.SetAXAtY.9
				
				lda #S.CB.CMD.FILLRECT
				sta CB.Cache+S.CB.CMD

				lda #S.CB.OP.SET
				sta CB.Cache+S.CB.OP
				lda #S.CB.M.C16
				sta CB.Cache+S.CB.M

				ldx PREFS.TBarColorI

				ldy #S.OBJ.S
				lda (ZPWNDPtr),y
				and #S.WND.S.ACTIVE
				beq .10

				ldx PREFS.TBarColorA

.10				stx CB.Cache+S.CB.COLOR

				ldy #S.OBJ.X1+5			X1,Y1,X2
				ldx #5

.11				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.X1,x
				dey
				dex
				bpl .11

				ldy #S.OBJ.Y1
				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.Y1
				sec
				adc SYSFON.H
				sta CB.Cache+S.CB.Y2

				iny

				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.Y1+1
				adc #0
				sta CB.Cache+S.CB.Y2+1

				jsr CB.Cache2CBBuf

				jsr CB.IncY1

* TOP LEFT ICON
				ldy #S.WND.ICON+1
				lda (ZPWNDPtr),y
				tax
				dey
				ora (ZPWNDPtr),y
				bne .1

				>LDYA L.BM.Apple
				bra .2

.1				lda (ZPWNDPtr),y
				tay
				txa

.2				jsr DRAW.yaBM

				jsr WND.DrawTitleBarText
				bcs .9

* TOP RIGHT ICONS

				ldy #S.OBJ.X2
				lda (ZPWNDPtr),y
				sec
				sbc #48
				sta CB.Cache+S.CB.X1

				iny
				lda (ZPWNDPtr),y
				sbc #0
				sta CB.Cache+S.CB.X1+1

				ldy #S.OBJ.F
				lda (ZPWNDPtr),y
				bit #S.WND.F.MIN
				beq .3

				ldx #BM.ID.MIN
				jsr DRAW.xBM

.3				lda #16
				jsr CB.AddA2X1

				ldy #S.OBJ.F
				lda (ZPWNDPtr),y
				bit #S.WND.F.MAX
				beq .4

				ldx #BM.ID.MAX
				jsr DRAW.xBM

.4				lda #16
				jsr CB.AddA2X1
				
				ldy #S.OBJ.F
				lda (ZPWNDPtr),y
				bit #S.WND.F.CLOSE
				beq .8

				ldx #BM.ID.CLOSE
				jsr DRAW.xBM

.8				jmp CB.FlushBuf

.9				rts
*--------------------------------------
WND.DrawTitleBarText
				ldy #S.WND.pTITLE
				lda (ZPWNDPtr),y
				pha
				iny
				lda (ZPWNDPtr),y
				ply

				ldx hSYSFON
				jsr FON.GetTextSize

				lda PREFS.XMargin
				asl
*				clc
				adc CB.Cache+S.CB.SrcW
				sta CB.Cache+S.CB.SrcW
				bcc .1
				
				inc CB.Cache+S.CB.SrcW+1

.1				ldy #S.OBJ.W
				lda (ZPWNDPtr),y
				sec
				sbc CB.Cache+S.CB.SrcW
				sta DX
				iny
				lda (ZPWNDPtr),y
				sbc CB.Cache+S.CB.SrcW+1

				lsr
				ror DX
				sta DX+1

				ldy	#S.OBJ.X1
				lda (ZPWNDPtr),y
				clc
				adc DX
*			and PREFS.BestViewedX
			and #$f8
				sta CB.Cache+S.CB.X1
				tax
				
				iny
				lda (ZPWNDPtr),y
				adc DX+1
				sta CB.Cache+S.CB.X1+1

				pha
				txa

				clc 
				adc CB.Cache+S.CB.SrcW
*			ora PREFS.nBestViewedX
			and #$f8
				sta CB.Cache+S.CB.X2
				
				pla
				adc CB.Cache+S.CB.SrcW+1
				sta CB.Cache+S.CB.X2+1
				
				jsr CB.DecY2
				
				lda #C.WHITE
				jsr CB.FillRectMonoA

				jsr CB.Cache2CBBuf
				
				lda PREFS.XMargin
				jsr CB.AddA2X1

				ldy #S.WND.pTITLE
				lda (ZPWNDPtr),y
				pha
				iny
				lda (ZPWNDPtr),y
				ply
				
				ldx hSYSFON
				jmp DRAW.YAText
*--------------------------------------
WND.DrawStatusBar
				jsr CB.GetBuf
				bcs .9
				
				jsr CB.BorderLineH

				ldy #S.OBJ.X1
				ldx #S.CB.X1
				jsr CB.GetWndYCacheX

				ldy #S.OBJ.X2
				ldx #S.CB.X2
				jsr CB.GetWndYCacheX

				ldy	#S.WND.SBarY1
				ldx #S.CB.Y1
				jsr CB.GetWndYCacheX

				jsr CB.Cache2CBBuf

				jsr CB.IncY1

				lda PREFS.SBarColor
				jsr CB.FillRectMonoA

				ldy #S.OBJ.Y2
				ldx #S.CB.Y2
				jsr CB.GetWndYCacheX

				jsr CB.Cache2CBBuf

				lda #4
				jsr CB.AddA2X1
				
				ldy #S.WND.pSTATUS
				lda (ZPWNDPtr),y
				pha
				iny
				lda (ZPWNDPtr),y
				ply

				ldx hSYSFON
				jsr DRAW.YAText
				
				jmp CB.FlushBuf
				
.9				rts				
*--------------------------------------
WND.Paint		pha						hWND

				jsr OBJ.GetHWNDA

WND.Paint1		ldy #S.WND.fPaint
				lda (ZPWNDPtr),y
				sta ZPPtr1
				iny
				lda (ZPWNDPtr),y
				sta ZPPtr1+1

				pla

				jmp (ZPPtr1)
*--------------------------------------
WND.fPaint		jsr OBJ.GetHWNDA
				jsr WND.GetInnerInClip

				jsr WND.fPaint.BG
				bcs .9

				jsr WND.GetChilds
				beq .8

.1				jsr OBJ.Paint
				bcs .9

				jsr OBJ.GetNext
				bne .1

.8				clc

.9				rts
*--------------------------------------
WND.GetInnerInClip
				ldy #S.WND.InnerX1+7
				ldx #7
				
.1				lda (ZPWNDPtr),y
				sta CLIP.Rect+S.RECT.X1,x
				dey
				dex
				bpl .1
				
				rts
*--------------------------------------
WND.fPaint.BG	jsr CB.GetBuf
				bcs .9

				lda #S.CB.CMD.FILLRECT
				sta CB.Cache+S.CB.CMD

				lda #S.CB.OP.SET+S.CB.OP.COLOR
				sta CB.Cache+S.CB.OP

				lda #S.CB.M.MONO
				sta CB.Cache+S.CB.M

				ldy #S.WND.BGCOLOR
				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.COLOR

				ldx #7

.1				lda CLIP.Rect+S.RECT.X1,x
				sta CB.Cache+S.CB.X1,x
				dex
				bpl .1
				
				jsr CB.Cache2CBBuf
*--------------------------------------
				ldy #S.WND.BGBM
				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.SrcPtr
				iny
				ora (ZPWNDPtr),y
				beq .8

				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.SrcPtr+1

				jsr CB.ResetSrcXY

				ldy #S.WND.InnerW+3
				ldx #3

.2				lda (ZPWNDPtr),y
				sta CB.Cache+S.CB.SrcW,x
				dey
				dex
				bpl .2

				lda #S.CB.CMD.BITBLT
				sta CB.Cache+S.CB.CMD

				jsr CB.Cache2CBBuf

.8				jmp CB.FlushBuf

.9				rts
*--------------------------------------
WND.GetChilds	ldy #S.OBJ.pChilds
				lda (ZPWNDPtr),y
				sta ZPObjPtr
				iny
				lda (ZPWNDPtr),y
				sta ZPObjPtr+1
				ora ZPObjPtr
				rts
*--------------------------------------
MAN
SAVE usr/src/lib/libgui.s.wnd
LOAD usr/src/lib/libgui.s
ASM
