NEW
  AUTO 3,1
*--------------------------------------
* Y,A = MBAR definition
*--------------------------------------
MBAR.New		>STYA ZPPtr1			MBAR definition
				>STYA ZPPtr2			for pass #2

				jsr MBAR.GetSize
				>SYSCALL2 GetMem
				bcs .9

				>STYA ZPObjPtr

				phx						hMBAR

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

				lda #0

				ldy #S.MBAR.Items-1

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

				jsr MBAR.GetData

				pla						hMBAR
				clc
.9				rts
*--------------------------------------
* ZPPtr1 = defintion
*--------------------------------------
MBAR.GetSize	lda #S.MBAR.Items+1		Ending\0
				sta Counter
				stz Counter+1

.1				lda (ZPPtr1)
				beq .8

				ldy #$ff

.2				iny
				lda (ZPPtr1),y
				bne .2

				tya

				sec						text Ending\0

				inc
				inc						skip pMENU
				jsr SkipAPtr1

				lda #S.MBITEM
				clc
				adc Counter
				sta Counter
				bcc .1

				inc Counter+1
				bra .1

.8				>LDYA Counter
				rts
*--------------------------------------
* ZPPtr2 = defintion
* ZPObjPtr = DstMBar
* ZPPtr1 = DstMBItem
*--------------------------------------
MBAR.GetData	jsr MBAR.GetMBItems

.1				lda (ZPPtr2)
				beq .8

				lda #S.MBITEM.T.SUBMENU
				jsr MBAR.AddByte

				lda #0					S.MBITEM.S
				jsr MBAR.AddByte

				>LDYA ZPPtr2			pTEXT
				ldx hSYSFON
				jsr FON.GetTextSize

				lda PREFS.XMargin
				asl						x2
				clc
				adc CB.CACHE+S.CB.SrcW
				tay

				lda #0
				adc CB.CACHE+S.CB.SrcW+1
				jsr MBAR.AddWord		S.MBITEM.W

				>LDYA ZPPtr2			pTEXT
				jsr MBAR.AddWord

				jsr SkipStrZPtr2		skip "TEXT\0"

				lda (ZPPtr2)
				jsr MBAR.AddByte		pMENU

				ldy #1
				lda (ZPPtr2),y
				jsr MBAR.AddByte		pMENU+1

				lda #2
				jsr SkipAPtr2			stkip pMENU
				bra .1

.8				sta (ZPPtr1)
				rts
*--------------------------------------
MBAR.AddWord	pha

				tya
				jsr MBAR.AddByte
				pla

MBAR.AddByte	sta (ZPPtr1)
				inc ZPPtr1
				bne .8

				inc ZPPtr1+1

.8				rts
*--------------------------------------
* ZPWNDPtr parent WND
* MSG.X1, MSG.Y1
*--------------------------------------
MBAR.Enter		ldy #S.WND.pMBAR
				lda (ZPWNDPtr),y
				>SYSCALL2 GetMemPtr
				>STYA ZPObjPtr			pMBAR

				stz ZPPtr2
				stz ZPPtr2+1

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

				jsr MBAR.GetMBItems

.1				lda (ZPPtr1)
				beq .3

				jsr MBAR.SetX2			set X2 = X1 + W - 1

				lda MSG+S.MSG.X1
				cmp CB.Cache+S.CB.X1
				lda MSG+S.MSG.X1+1
				sbc CB.Cache+S.CB.X1+1
				bcc .2

				lda CB.Cache+S.CB.X2
				cmp MSG+S.MSG.X1
				lda CB.Cache+S.CB.X2+1
				sbc MSG+S.MSG.X1+1
				bcc .2

				ldy #S.MBITEM.S			in RECT....
				lda (ZPPtr1),y
				bmi .8					Already Selected, nothing to do

				>LDYA ZPPtr1
				>STYA ZPPtr2			save MBITEM to activate

.2				jsr MBAR.NextMBItem
				bra .1

.3				lda ZPPtr2
				ora ZPPtr2+1
				bne MBAR.Set

.8				clc
.9				rts
*--------------------------------------
MBAR.Leave		clc
				rts
*--------------------------------------
* ZPWNDPtr parent WND
* ZPObjPtr = MBAR
* ZPPtr2 = Item to activate
*--------------------------------------
MBAR.Reset		stz ZPPtr2
				stz ZPPtr2+1

MBAR.Set		jsr MENU.DestroyAll

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

				jsr MBAR.GetMBItems

				jsr CB.GetBuf
				bcs .9

.4				lda (ZPPtr1)
				beq .7

				jsr MBAR.SetX2			set X2 = X1 + W - 1
				
				ldy #S.MBITEM.S
				lda (ZPPtr1),y
				bmi .5

				ldx ZPPtr1
				cpx ZPPtr2
				bne .6

				ldx ZPPtr1+1
				cpx ZPPtr2+1
				bne .6
				
				ldx CB.Cache+S.CB.X1
				stx Counter
				ldx CB.Cache+S.CB.X1+1
				stx Counter+1

.5				eor #S.MBITEM.S.SELECTED
				sta (ZPPtr1),y

				jsr MBAR.DrawMBItem
				bcs .9

.6				jsr MBAR.NextMBItem		set X1 = X2
				bra .4

.7				jsr CB.FlushBuf
				bcs .9

				>LDYA Counter
				>STYA DX

				ldy #S.WND.MBarY2
				lda (ZPWNDPtr),y
				sta DY
				iny
				lda (ZPWNDPtr),y
				sta DY+1

				ldy #S.MBITEM.pMENU
				lda (ZPPtr2),y
				pha
				iny
				lda (ZPPtr2),y
				ply

				jsr MENU.New.I
				bcs .9

				jmp MENU.Draw

.9				rts
*--------------------------------------
* ZPWNDPtr = parent WND
*--------------------------------------
MBAR.Draw		>SYSCALL2 GetMemPtr
				>STYA ZPObjPtr			pMBAR

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

				jsr CB.GetBuf
				bcs .9

				jsr MBAR.GetMBItems

.1				lda (ZPPtr1)
				beq .8

				jsr MBAR.SetX2			set X2 = X1 + W - 1

				jsr MBAR.DrawMBItem
				bcs .9

				jsr MBAR.NextMBItem
				bra .1

.8				jsr CB.X2ToX1

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

				jsr MBAR.SetY1Y2

				lda PREFS.MBarColor
				jsr CB.FillRectMonoA

				jsr CB.Cache2CBBuf
				
				jmp CB.FlushBuf

.9				rts
*--------------------------------------
MBAR.DrawMBItem	ldy #S.MBITEM.S
				lda (ZPPtr1),y
				asl

				lda PREFS.MBarColor
				bcc .1

				eor #$ff

.1				jsr CB.FillRectMonoA

				jsr MBAR.SetY1Y2

				jsr CB.Cache2CBBuf

				lda PREFS.XMargin
				jsr CB.AddA2X1

				jsr CB.IncY1

				ldy #S.MBITEM.pTEXT
				lda (ZPPtr1),y
				pha
				iny
				lda (ZPPtr1),y
				ply
				ldx hSYSFON
				jmp DRAW.YAText
*--------------------------------------
MBAR.GetMBItems	lda ZPObjPtr
				clc
				adc #S.MBAR.Items
				sta ZPPtr1

				lda ZPObjPtr+1
				adc /S.MBAR.Items
				sta ZPPtr1+1
				rts
*--------------------------------------
MBAR.NextMBItem	jsr CB.X2ToX1

				lda #S.MBITEM
				jmp SkipAPtr1
*--------------------------------------
MBAR.SetX2		ldy #S.MBITEM.W
				lda (ZPPtr1),y
				clc
				adc CB.Cache+S.CB.X1
				pha
				
				iny
				lda (ZPPtr1),y
				adc CB.Cache+S.CB.X1+1
				
				ply
				bne .1
				
				dec
				
.1				dey				

				sty CB.Cache+S.CB.X2
				sta CB.Cache+S.CB.X2+1

				rts
*--------------------------------------
MBAR.SetY1Y2	ldy #S.WND.MBarY1
				ldx #S.CB.Y1
				jsr CB.GetWndYCacheX

				ldy	#S.WND.MBarY2
				ldx #S.CB.Y2
				jmp CB.GetWndYCacheX
*--------------------------------------
MAN
SAVE usr/src/lib/libgui.s.mbar
LOAD usr/src/lib/libgui.s
ASM
