NEW
  AUTO 3,1
*--------------------------------------
* NewMenu(pMENU, x, y)
*--------------------------------------
MENU.New		jsr OBJ.PullDXDY
				>PULLYA

MENU.New.I		>STYA ZPPtr1			MENU definition
				>STYA ZPPtr2			for pass #2

				ldx MENU.Stack.Cnt
				cpx #MENU.MAX
				beq .90

				jsr MENU.GetSize
				>SYSCALL2 GetMem
				bcs .99

				>STYA ZPObjPtr
				txa						hMenu

				ldx MENU.Stack.Cnt
				sta MENU.Stack,x

				lda #S.OBJ.T.MENU
				sta (ZPObjPtr)			S.OBJ.T

				lda #0

				ldy #S.MENU.Items-1

.1				sta (ZPObjPtr),y
				dey
				bne .1

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

				jsr OBJ.SetDXDYX1Y1

				jsr MENU.GetData

				jsr MENU.SetMenuSizePos

				ldx MENU.Stack.Cnt
				lda MENU.Stack,x

				inc	MENU.Stack.Cnt

				clc
				rts

.90				lda #E.OOH
				sec
.99
MENU.New.RTS	rts
*--------------------------------------
* ZPPtr1 = defintion
*--------------------------------------
MENU.GetSize	lda #S.MENU.Items+1		Ending\0
				sta Counter
				stz Counter+1

.1				lda (ZPPtr1)
				beq .8

				lda #S.MITEM
				clc
				adc Counter
				sta Counter
				bcc .2

				inc Counter+1

.2				lda (ZPPtr1)
				cmp #S.MITEM.T.SEP
				bne .3

				inc ZPPtr1
				bne .1

				inc ZPPtr1+1
				bra .1

.3				cmp #S.MITEM.T.ITEM
				bne .4

				lda #6					T+ID+KEMOD+KEY+pICON+"TEXT"
				bra .5

.4				lda #5					T+pMENU+pICON+"TEXT"

.5				jsr SkipAPtr1
				jsr SkipStrZPtr1		skip "TEXT\0"
				bra .1

.8				>LDYA Counter
				rts
*--------------------------------------
* ZPPtr2 = defintion
* ZPObjPtr = DstMenu
* ZPPtr1 = DstMItem
*--------------------------------------
MENU.GetData	jsr MENU.GetMItems

.1				lda (ZPPtr2)
				sta (ZPPtr1)
				beq .8

				cmp #S.MITEM.T.SEP
				bne .2

				inc ZPPtr2				skip 1 byte
				bne .5

				inc ZPPtr2+1
				bra .5
*--------------------------------------
.2				cmp #S.MITEM.T.ITEM
				bne .3

				ldy #1					 T+ID+KEMOD+KEY+pICON+"text"
				lda (ZPPtr2),y
				ldy #S.MITEM.ID
				sta (ZPPtr1),y

				ldy #2
				lda (ZPPtr2),y
				tax
				iny
				lda (ZPPtr2),y

				ldy #S.MITEM.KEYMOD+1
				sta (ZPPtr1),y
				dey						KEY
				txa
				sta (ZPPtr1),y

				lda #4
				bra .4					skip 4 bytes
*--------------------------------------
.3				ldy #1					skip T+pMENU+pICON+"text"
				lda (ZPPtr2),y
				tax
				iny
				lda (ZPPtr2),y

				ldy #S.MITEM.pMENU+1
				sta (ZPPtr1),y
				dey
				txa
				sta (ZPPtr1),y
				lda #3					skip 3 bytes

.4				jsr SkipAPtr2

				ldy #1
				lda (ZPPtr2),y
				ldy #S.MITEM.pICON+1
				sta (ZPPtr1),y
				dey
				lda (ZPPtr2)
				sta (ZPPtr1),y

				lda #2					skip pICON
				jsr SkipAPtr2

				ldy #S.MITEM.pTEXT
				lda ZPPtr2
				sta (ZPPtr1),y
				iny
				lda ZPPtr2+1
				sta (ZPPtr1),y

				jsr SkipStrZPtr2		skip "TEXT\0"

				ldy #S.MITEM.S
				lda #0
				sta (ZPPtr1),y

.5				jsr MENU.NextMItem
				bra .1

.8				clc
				rts
*--------------------------------------
MENU.SetMenuSizePos
				stz IY					Icon Margin W

				stz IE					Max Text W
				stz IE+1

				stz INE					Max Mod-Key W
				stz INE+1

				lda #2					2px Borders
				sta DY					Menu H
				stz DY+1

				jsr MENU.GetMItems

.1				lda (ZPPtr1)
				beq .3

				cmp #S.MITEM.T.SEP
				beq .2

				jsr MENU.GetMItemW

.2				jsr MENU.NextMItemDY
				bra .1
*--------------------------------------
.3				lda	#8+8				1px Border+7 (Checked) +7 (SubMenu)+1px Border
				clc
				adc IY					Icon W
				sta DX
				stz DX+1

				lda DX
				clc
				adc IE					Max Text W
				sta DX

				lda DX+1
				adc IE+1
				sta DX+1

				lda DX
				clc
				adc INE					Max Mod-Key W
				sta DX

				lda DX+1
				adc INE+1
				sta DX+1

				ldy #S.OBJ.W
				lda DX
				sta (ZPObjPtr),y
				iny
				lda DX+1
				sta (ZPObjPtr),y

				iny						#S.OBJ.H

				lda DY
				sta (ZPObjPtr),y
				iny
				lda DY+1
				sta (ZPObjPtr),y

				ldy #S.MENU.iW
				lda IY
				sta (ZPObjPtr),y

				ldy #S.MENU.tW
				lda IE
				sta (ZPObjPtr),y
				iny
				lda IE+1
				sta (ZPObjPtr),y

				ldy	#S.MENU.kW
				lda INE
				sta (ZPObjPtr),y
				iny
				lda INE+1
				sta (ZPObjPtr),y
*--------------------------------------
				ldy #S.OBJ.Y1
				lda DY
				clc
				adc (ZPObjPtr),y
				tax

				iny
				lda DY+1
				adc (ZPObjPtr),y

				cpx WND.Screen+S.OBJ.H
				sbc WND.Screen+S.OBJ.H+1
				bcc .5

				ldy #S.OBJ.Y1

				lda (ZPObjPtr),y
*				sec
				sbc DY
				sta (ZPObjPtr),y

				iny

				lda (ZPObjPtr),y
				sbc DY+1
				sta (ZPObjPtr),y

.5				>LDYA ZPObjPtr
				jmp OBJ.SetX2Y2
*--------------------------------------
MENU.GetMItemW	ldy #S.MITEM.pICON
				lda (ZPPtr1),y
				iny
				ora (ZPPtr1),y
				beq .1

				lda #20					Reserve space for Icon W + 4px SEP
				sta IY

.1				jsr MENU.GetText
				ldx hSYSFON
				jsr FON.GetTextSize

				ldx CB.Cache+S.CB.SrcW
				cpx IE
				lda CB.Cache+S.CB.SrcW+1
				sbc IE+1
				bcc .2

				stx IE					Set Max text W
				lda CB.Cache+S.CB.SrcW+1
				sta IE+1

.2				jsr MENU.GetKeyString
				beq .8

				ldx hSYSFON
				jsr FON.GetTextSize

				ldx CB.Cache+S.CB.SrcW
				cpx INE
				lda CB.Cache+S.CB.SrcW+1
				sbc INE+1
				bcc .8

				stx INE					set Max Mod-key W
				lda CB.Cache+S.CB.SrcW+1
				sta INE+1

.8				rts
*--------------------------------------
MENU.FindMItemByID
				jsr MENU.GetMItems

.1				lda (ZPPtr1)
				beq .9

				cmp #S.MITEM.T.SEP
				beq .7

				txa
				ldy #S.MITEM.ID
				cmp (ZPPtr1),y
				beq .8

.7				jsr MENU.NextMItem
				bra .1

.8				clc
				rts

.9				sec
				rts
*--------------------------------------
* ZPObjPtr = Menu
*--------------------------------------
MENU.Enter		>LDYA L.WND.Screen
				jsr CLIP.GetRect
				
				jsr MENU.GetDXDY

				stz D
				stz D+1

				ldy #S.OBJ.X2
				lda DX					X1
				clc
				adc (ZPObjPtr),y
				sta IE					IE = X2

				iny
				lda DX+1
				adc (ZPObjPtr),y
				sta IE+1

				jsr MENU.GetMItems

.1				lda (ZPPtr1)
				beq .4

				cmp #S.MITEM.T.SEP
				beq .2

				lda DY					Y1
				clc
				adc SYSFON.Hp1
				sta INE					INE = Y2
				lda DY+1
				adc #0
				sta INE+1

				>LDYAI DX				X1,X2,Y1,Y2
				jsr MOU.InRectYA
				bcc .3

				ldy #S.MITEM.S
				lda (ZPPtr1),y
				bpl .2

				eor #S.MITEM.S.SELECTED
				sta (ZPPtr1),y			was selected before...

				jsr MENU.DrawMItemBuf
				bcs .9

.2				jsr MENU.NextMItemDY
				bra .1
*--------------------------------------
.3				ldy #S.MITEM.S
				lda (ZPPtr1),y
				bmi .5					already selected, nothing to do

				eor #S.MITEM.S.SELECTED
				sta (ZPPtr1),y

				jsr MENU.DrawMItemBuf
				bcs .9

.30				ldx MENU.Stack.Idx
				inx
				cpx MENU.Stack.Cnt
				beq .31

				jsr MENU.Destroy
				bra .30

.31				lda (ZPPtr1)
				cmp #S.MITEM.T.SUBMENU
				bne .2

				ldy #S.MITEM.pMENU
				lda (ZPPtr1),y
				sta D
				iny
				lda (ZPPtr1),y
				sta D+1

				lda DY
				sec
				sbc #1
				sta Counter
				lda DY+1
				sbc #0
				sta Counter+1

				bra .2
*--------------------------------------
.8				clc
.9				rts
*--------------------------------------
.4				lda D
				ora D+1
				beq .5

				>PUSHW D				pMENU

				ldy #S.OBJ.X2
				lda (ZPObjPtr),y
				clc
				adc #1
				tax
				iny
				lda (ZPObjPtr),y
				adc #0
				>PUSHA
				txa
				>PUSHA

				>PUSHW Counter			Y1

				jsr MENU.New
				bcc MENU.Draw
				
				rts
*--------------------------------------
.5				lda	MSG+S.MSG.S
				bit #S.XY.S.CLK
				beq .8

				lda #S.MSG.T.ID
				sta MSG+S.MSG.T

				ldy #S.MITEM.ID
				lda (ZPPtr1),y
				sta MSG+S.MSG.ID

				jmp MENU.DestroyAll
*--------------------------------------
MENU.Leave		clc
				rts
*--------------------------------------
* DrawMenu(hMENU)
*--------------------------------------
MENU.Draw		>SYSCALL2 GetMemPtr
				>STYA ZPObjPtr

				>LDYA L.WND.Screen
				jsr CLIP.GetRect

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

				jsr CB.GetObjX1Y1X2Y2

				jsr CB.GFXWriteClip
				bcs .9

				>SYSCALL2 NewStkObj
				bcs .9

				stx CB.Cache+S.CB.DstPtr
				stz CB.Cache+S.CB.DstPtr+1

				txa
				ldy #S.MENU.hSAVEBUF
				sta (ZPObjPtr),y

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

				lda #S.CB.OP.SAVE
				sta CB.Cache+S.CB.OP

				jsr CB.ResetSrcXY

				jsr CB.GetObjSrcWH

				jsr CB.Write

				>LDYA ZPObjPtr
				ldx #0
				jsr OBJ.DrawBorders
				bcs .9
				
				jsr MENU.GetDXDY

				jsr MENU.GetMItems

.1				lda (ZPPtr1)
				beq .8

				jsr MENU.DrawMItemBuf
				bcs .9
				
				jsr MENU.NextMItemDY
				bra .1

.8				clc

.9				rts
*--------------------------------------
MENU.DrawMItemBuf
				jsr CB.GetBuf
				bcs .9

				jsr MENU.DrawMItem
				bcs .9

				jmp CB.FlushBuf

.9				rts
*--------------------------------------
MENU.DrawMItem	jsr MENU.ResetX1Y1X2

				lda (ZPPtr1)
				cmp #S.MITEM.T.SEP
				bne .1

				jsr CB.BorderLineH
				jmp CB.Cache2CBBuf
*--------------------------------------
.1				jsr MENU.IsSelected

				lda PREFS.MenuColor
				bcc .2

				eor #$ff

.2				jsr CB.FillRectMonoA

				lda CB.Cache+S.CB.Y1
				sec
				adc SYSFON.H
				sta CB.Cache+S.CB.Y2

				lda CB.Cache+S.CB.Y1+1
				adc #0
				sta CB.Cache+S.CB.Y2+1
				jsr CB.Cache2CBBuf
*--------------------------------------
				jsr CB.IncY1			Y1:1px margin

				lda #8					TODO : Checked
				jsr CB.AddA2X1

				jsr MENU.GetIcon
				beq .3

				jsr DRAW.yaBM
*--------------------------------------
.3				ldy #S.MENU.iW
				lda (ZPObjPtr),y
				jsr CB.AddA2X1

				jsr MENU.GetText
				ldx hSYSFON
				jsr DRAW.YAText

				ldy #S.MENU.tW+1
				lda (ZPObjPtr),y
				tax
				dey
				lda (ZPObjPtr),y
				jsr CB.AddAX2X1
*--------------------------------------
				lda (ZPPtr1)
				cmp #S.MITEM.T.ITEM
				bne .4

				jsr MENU.GetKeyString		C untouched
				beq .8

				ldx hSYSFON
				jmp DRAW.YAText
*--------------------------------------
.4				cmp #S.MITEM.T.SUBMENU
				bne .8

				ldy #S.MENU.kW+1
				lda (ZPObjPtr),y
				tax
				dey
				lda (ZPObjPtr),y
				
				jsr CB.AddAX2X1

				ldx #BM.ID.RIGHT
				jmp DRAW.xBM

.8				clc
				rts
*--------------------------------------
MENU.GetIcon	ldy #S.MITEM.pICON+1
				lda (ZPPtr1),y
				bne .1

				dey
				lda (ZPPtr1),y
				beq .8

				tax
				ldy L.BMs,x
				lda L.BMs+1,x			NON ZERO
.8				rts

.1				pha
				dey
				lda (ZPPtr1),y
				tay
				pla						NON ZERO

				rts
*--------------------------------------
MENU.IsSelected	ldy #S.MITEM.S
				lda (ZPPtr1),y
				asl
				rts
*--------------------------------------
MENU.GetText	ldy #S.MITEM.pTEXT

				lda (ZPPtr1),y
				pha
				iny
				lda (ZPPtr1),y
				ply
				rts
*--------------------------------------
* ZPPtr1 = pMITEM
*--------------------------------------
MENU.GetKeyString
				ldy #S.MITEM.KEY
				lda (ZPPtr1),y
				dey 					S.MITEM.KEYMOD
				ora (ZPPtr1),y
				beq .9

				ldx #0

				lda (ZPPtr1),y
				beq .3

				bit #KEYMOD.CTRL
				beq .1

				ldy #KEYMOD.S.CTRL
				jsr .4

.1				bit #KEYMOD.OA
				beq .2

				ldy #KEYMOD.S.OA
				jsr .4

.2				bit #KEYMOD.CA
				beq .3

				ldy #KEYMOD.S.CA
				jsr .4

.3				ldy #S.MITEM.KEY
				lda (ZPPtr1),y
				sta TEXTBUF,x
				stz TEXTBUF+1,x

				>LDYA L.TEXTBUF			NZ

				rts
*--------------------------------------
.4				pha

.5				lda KEYMOD.S,y
				sta TEXTBUF,x
				beq .6

				inx
				iny
				bra .5

.6				pla

.9				rts
*--------------------------------------
MENU.GetDXDY	ldy #S.OBJ.X1
				lda (ZPObjPtr),y
				clc
				adc #1
				sta DX
				iny
				lda (ZPObjPtr),y
				adc #0
				sta DX+1

				ldy #S.OBJ.Y1
				lda (ZPObjPtr),y
				clc
				adc #1
				sta DY
				iny
				lda (ZPObjPtr),y
				adc #0
				sta DY+1

				rts
*--------------------------------------
MENU.ResetX1Y1X2
				ldy #S.OBJ.X1
				lda (ZPObjPtr),y
				clc
				adc #1
				sta CB.Cache+S.CB.X1
				iny
				lda (ZPObjPtr),y
				adc #0
				sta CB.Cache+S.CB.X1+1

				lda DY
				sta CB.Cache+S.CB.Y1
				lda DY+1
				sta CB.Cache+S.CB.Y1+1

				ldy #S.OBJ.X2
				lda (ZPObjPtr),y
				sec
				sbc #1
				sta CB.Cache+S.CB.X2
				iny
				lda (ZPObjPtr),y
				sbc #0
				sta CB.Cache+S.CB.X2+1

				rts
*--------------------------------------
MENU.GetMItems	lda ZPObjPtr
				clc
				adc #S.MENU.Items
				sta ZPPtr1

				lda ZPObjPtr+1
				adc /S.MENU.Items
				sta ZPPtr1+1
				rts
*--------------------------------------
MENU.NextMItemDY
				lda (ZPPtr1)
				sec
				eor #S.MITEM.T.SEP
				beq .1

				lda SYSFON.Hp2
				clc

.1				adc DY
				sta DY
				bcc MENU.NextMItem

				inc DY+1

MENU.NextMItem	lda #S.MITEM
				jmp SkipAPtr1
*--------------------------------------
MENU.DestroyAll	sec
				.HS 90					BCC
MENU.Destroy	clc

				ldx MENU.Stack.Cnt
				beq .8

				php

				jsr PTR.Disable

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

				lda #S.CB.OP.RESTORE
				sta CB.Cache+S.CB.OP

				plp

				lda ZPObjPtr+1
				pha
				lda ZPObjPtr
				pha

.2				php

				ldx MENU.Stack.Cnt
				lda MENU.Stack-1,x
				>SYSCALL2 GetMemPtr
				>STYA ZPObjPtr

				jsr CB.GetObjX1Y1X2Y2

				jsr CB.ResetSrcXY

				jsr CB.GetObjSrcWH

				ldy #S.MENU.hSAVEBUF
				lda (ZPObjPtr),y
				sta CB.Cache+S.CB.DstPtr
				stz CB.Cache+S.CB.DstPtr+1

				ldx #0					Select CB.Cache
				jsr CB.GFXWriteX		Direct Write (no clip)

				ldy #S.MENU.hSAVEBUF
				lda (ZPObjPtr),y
				>SYSCALL2 FreeStkObj

				ldx MENU.Stack.Cnt
				lda MENU.Stack-1,x
				>SYSCALL2 FreeMem

				plp						Destroy all ?
				dec MENU.Stack.Cnt

				bcc .7					no

				bne .2

.7				pla
				sta ZPObjPtr
				pla
				sta ZPObjPtr+1

				jmp PTR.Enable

.8				clc
				rts
*--------------------------------------
MAN
SAVE usr/src/lib/libgui.s.menu
LOAD usr/src/lib/libgui.s
ASM
