
	load 'macros.dump'
	include 'driver.equ'

	include 'pl.equ'
;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	D_DrawIcon
	IMPORT	D_FastMult
	IMPORT	P_ChangePage
	IMPORT	P_ContentRect
	IMPORT	P_DblPage
	IMPORT	P_Do_Ruler
	IMPORT	P_DrawAPage
	IMPORT	P_DrawContent
	IMPORT	P_FirstPOff
	IMPORT	P_H
	IMPORT	P_HRulRect
	IMPORT	P_L_ArrFilledIcon
	IMPORT	P_L_ArrHollowIcon
	IMPORT	P_Page
	IMPORT	P_PageCount
	IMPORT	P_PageStart
	IMPORT	P_Pagemode
	IMPORT	P_PortRect
	IMPORT	P_PrintHand
	IMPORT	P_R_ArrFilledIcon
	IMPORT	P_R_ArrHollowIcon
	IMPORT	P_TrackCursor
	IMPORT	P_V
	IMPORT	P_VRulRect
	IMPORT	P_VScroll
	IMPORT	P_VisPalette
	IMPORT	P_VisRuler
	IMPORT	T_ClipThread
	IMPORT	T_DotObj




;---------------------------------------------------------------------------
;
; P_ActionProc (PartCode:w,CtlHdl:l) : Result:w
;


P_ActionProc	PROC		EXPORT
			;Using P_Data
			;Using P_RulerData
			;Using T_Data

		input  PartCode:w,CtlHdl:l
		output Result:w

		local  UpdateRgn:l,UpdateRect:r,SRect:r,CtlMax:w
		local  Amount:w,NewValue:w,Width:w,Height:w
		local  PHdl:l,PPtr:l,TempPtr:l

		begin +b

		; Get rid of any possible scritch marks before possible scrolling
		moveword #-10,PPtr
		moveword #-10,PPtr+2
		call P_TrackCursor,in=(!PPtr:l)
		tool _ClipRect,in=(#P_PortRect:l)


		moverect P_ContentRect,SRect
		tool _NewRgn,out=(UpdateRgn:l)
		movelong P_PrintHand,PHdl
		movelong [PHdl],PPtr


		spacelong
		pushlong CtlHdl
		_GetCtlParams
		subword s,s,CtlMax

		cmpl CtlHdl,P_VScroll
		jeq Vertical


		subword P_ContentRect+6,P_ContentRect+2,Width

		lda PartCode
		cmp #Thumb
		beq HThumb
		cmp #5					;UpArrow
		beq HUpArrow
		cmp #6					;DownArrow
		beq HDownArrow
		cmp #PageUp
		beq HPageUp
		cmp #PageDown
		beq HPageDown
		brl Exit


HThumb
		tool _GetCtlValue,in=(CtlHdl:l),out=(a:w)
		and #$fffe
		sta NewValue

		tool _SetCtlValue,in=(a:w,CtlHdl:l)

		subword NewValue,P_H,Amount
		moveword NewValue,P_H
		lda Amount
		abs a
		cmp Width
		blt HSmall

		lda Width
		ldy Amount
		bpl HStore
		neg a
HStore	sta Amount
HSmall	brl HDoScroll


HUpArrow
		moveword #-32,Amount
		bra HCheckValue

HDownArrow
		moveword #32,Amount
		bra HCheckValue

HPageUp
		lda Width
		lsr a
		neg a
		sta Amount
		bra HCheckValue

HPageDown
		lda Width
		lsr a
		sta Amount




HCheckValue	lda Amount
		and #$fffe
		sta Amount
		addword a,P_H,NewValue
		bpl HCheckMax
		stz NewValue
		bra HSet

HCheckMax	cmp CtlMax
		bmi HSet
		moveword CtlMax,NewValue

HSet
		subword NewValue,P_H,Amount
		moveword NewValue,P_H
		pushword NewValue
		pushlong CtlHdl
		_SetCtlValue


HDoScroll
		stz SRect
		pushlong !SRect
		subword #0,Amount,s
		pushword #0
		pushlong UpdateRgn
		_ScrollRect

		movelong [UpdateRgn],TempPtr
		moverect [TempPtr]:#2,UpdateRect
		moveword P_ContentRect,UpdateRect


		lda P_VisRuler
		beq ToUpdate

		pushword [PPtr]:#6
		pushword #0
		subword P_ContentRect+2,P_H,s
		in UpdateRect+2:w,UpdateRect+6:w,#0:w
		in #P_HRulRect:l,#P_VRulRect:l,#P_PortRect:l
		in #P_ContentRect:l,P_Pagemode:w
		xcall P_Do_Ruler

ToUpdate	brl DoUpdate




Vertical
		subword P_ContentRect+4,P_ContentRect,Height

		lda PartCode
		cmp #Thumb
		beq VThumb
		cmp #5					;UpArrow
		beq VUpArrow
		cmp #6					;DownArrow
		beq VDownArrow
		cmp #PageUp
		beq VPageUp
		cmp #PageDown
		beq VPageDown
		brl Exit


VThumb
		tool _GetCtlValue,in=(CtlHdl:l),out=(a:w)
		and #$fffe
		sta NewValue

		tool _SetCtlValue,in=(a:w,CtlHdl:l)

		subword NewValue,P_V,Amount
		moveword NewValue,P_V
		lda Amount
		abs a
		cmp Height
		blt VSmall

		lda Height
		ldy Amount
		bpl VStore
		neg a
VStore	sta Amount
VSmall	brl VDoScroll


VUpArrow
		moveword #-16,Amount
		bra VCheckValue

VDownArrow
		moveword #16,Amount
		bra VCheckValue

VPageUp
		lda Height
		lsr a
		neg a
		sta Amount
		bra VCheckValue

VPageDown
		lda Height
		lsr a
		sta Amount



VCheckValue	lda Amount
		and #$fffe
		sta Amount
		addword a,P_V,NewValue
		bpl VCheckMax
		stz NewValue
		bra VSet

VCheckMax	cmp CtlMax
		bmi VSet
		moveword CtlMax,NewValue

VSet
		subword NewValue,P_V,Amount
		moveword NewValue,P_V
		tool _SetCtlValue,in=(a:w,CtlHdl:l)



VDoScroll	stz SRect+2
		lda P_VisPalette
		beq DidPal
		moveword #P_VRulLeft,SRect+2
DidPal	pushlong !SRect
		pushword #0
		subword #0,Amount,s
		pushlong UpdateRgn
		_ScrollRect

		movelong [UpdateRgn],TempPtr
		moverect [TempPtr]:#2,UpdateRect
		moveword P_ContentRect+2,UpdateRect+2

		lda P_VisRuler
		beq DoUpdate

		pushword [PPtr]:#4
		pushword #0
		subword P_ContentRect,P_V,s

		in UpdateRect:w,UpdateRect+4:w,#1:w
		in #P_HRulRect:l,#P_VRulRect:l,#P_PortRect:l
		in #P_ContentRect:l,P_Pagemode:w
		xcall P_Do_Ruler



DoUpdate
		tool _DisposeRgn,in=(UpdateRgn:l)
		tool _ClipRect,in=(!UpdateRect:l)

		call T_ClipThread,in=(T_DotObj:l)
		call P_DrawContent,in=(!UpdateRect:l,#0:w)
		tool _ClipRect,in=(#P_PortRect:l)


Exit	return
		ENDP






;---------------------------------------------------------------------------
;
; P_ScrollPages (xpos:w)
;

P_ScrollPages	PROC		EXPORT
			;Using P_Data
			;Using P_IconDATA

		input xpos:w

		local pagenum:w,xloc:w,yloc:w,count:w,xcheck:w
		local pt:l,tmprect:r,CRect:r,off:w,curpage:w

		begin


		addword P_Page,P_FirstPOff,curpage

		moveword #-1,xloc
		moveword P_ContentRect+4,yloc
		sta tmprect
		sta CRect
		addword a,#12,tmprect+4
		sta CRect+4

		moveword #P_PageWidth-1,xcheck
		; Check the template page
		cmpw xpos,xcheck
		bge Ck2nd

		lda P_DblPage
		beq DoRight
		moveword P_FirstPOff,pagenum		; pagenum=#0+P_FirstPOff;
		brl ShowPage

		; Check the second template page
Ck2nd	lda P_DblPage
		beq CheckLeft
		addword #P_PageWidth-2,xloc,xloc
		addword #P_PageWidth-2,xcheck,xcheck
		cmpw xpos,xcheck
		bge CheckLeft

DoRight	addword #1,P_FirstPOff,pagenum
		brl ShowPage

		; Check the leftarrow
CheckLeft
		; first add in the space.
		addword #P_SpaceWidth,xcheck,xcheck

		cmp xpos
		jge exit								; dead space

		addword #P_SpaceWidth+P_PageWidth,xloc,xloc

		cmpw P_PageCount,#P_MaxVisPages+1
		jlt CheckVis

		addword #P_ControlWidth-2,xloc,xloc
		addword #P_ControlWidth-1,xcheck,xcheck
		cmp xpos
		jlt CheckVis


		moveword #1,off
		moveword xloc,tmprect+2
		inc a
		inc a
		sta CRect+6
		addword a,#5*(P_PageWidth-2)-2,tmprect+6
		subword xloc,#P_ControlWidth-2,CRect+2

		lda P_DblPage
		beq LeftLoop

		inc tmprect+2
		inc tmprect+2


LeftLoop	tool _GetMouse,in=(!pt:l)
		tool _PtInRect,in=(!pt:l,!CRect:l),out=(a:w)
		bne LeftIn

		; Outside the box

		lda off
		jne LeftDown	; already off

		; turn it off
		moveword #1,off
		pushlong #P_L_ArrHollowIcon
		subword xloc,#P_ControlWidth-6,s
		lda yloc
		inc a
		pha
		pushword #modeCOPY
		jsl D_DrawIcon

		brl LeftDown

LeftIn	lda off
		beq LeftHere	; already on

		; turn it on
		stz off
		pushlong #P_L_ArrFilledIcon
		subword xloc,#P_ControlWidth-6,s
		lda yloc
		inc a
		pha
		pushword #modeCOPY
		jsl D_DrawIcon

LeftHere	addword #2,P_FirstPOff,a
		cmp P_PageStart
		beq LeftDown

		tool _ScrollRect,in=(!tmprect:l,#P_PageWidth-2:w,#0:w,#0:l)

NoExtra

		dec P_PageStart

		call P_DrawAPage,in=(xloc:w,yloc:w,P_PageStart:w,#1:w)

LeftDown	tool _StillDown,in=(#0:w),out=(a:w)
		jne LeftLoop

		pushlong #P_L_ArrHollowIcon
		subword xloc,#P_ControlWidth-6,s
		lda yloc
		inc a
		pha
		pushword #modeCOPY
		jsl D_DrawIcon

		brl Exit

		; Check pages 1-5
CheckVis	cmpw P_PageCount,#P_MaxVisPages
		blt GotCount
		lda #P_MaxVisPages
GotCount	sta count
		moveword P_PageStart,pagenum

loop	addword #P_PageWidth-2,xcheck,xcheck
		cmp xpos
		jge ShowPage

		addword #P_PageWidth-2,xloc,xloc
		inc pagenum
		dec count
		bne loop

		; Check the rightarrow
CheckRight	cmpw P_PageCount,#P_MaxVisPages+1
		jlt exit							; Not really here, I guess

		addword #4,xloc,xloc
		addword #P_ControlWidth+1,xcheck,xcheck
		cmp xpos
		jlt exit


		moveword #1,off
		subword xloc,#4,tmprect+6
		sta CRect+2
		subword a,#5*(P_PageWidth-2)-2,tmprect+2
		moveword xcheck,CRect+6

RightLoop	tool _GetMouse,in=(!pt:l)
		tool _PtInRect,in=(!pt:l,!CRect:l),out=(a:w)
		bne RightIn

		lda off
		bne RightDown

		moveword #1,off
		pushlong #P_R_ArrHollowIcon
		pushword xloc
		lda yloc
		inc a
		pha
		pushword #modeCOPY
		jsl D_DrawIcon

		bra RightDown

RightIn	lda off
		beq RightHere

		stz off
		pushlong #P_R_ArrFilledIcon
		pushword xloc
		lda yloc
		inc a
		pha
		pushword #modeCOPY
		jsl D_DrawIcon

RightHere	addword P_PageCount,P_FirstPOff,a
		subword a,#4,a
		cmp P_PageStart
		blt RightDown

		tool _ScrollRect,in=(!tmprect:l,#-P_PageWidth+2:w,#0:w,#0:l)

		inc P_PageStart

		subword xloc,#2+P_PageWidth,s
		pushword yloc
		addword P_PageStart,#P_MaxVisPages-1,s
		pushword #1
		jsl P_DrawAPage


RightDown	tool _StillDown,in=(#0:w),out=(a:w)
		jne RightLoop

		pushlong #P_R_ArrHollowIcon
		pushword xloc
		lda yloc
		inc a
		pha
		pushword #modeCOPY
		jsl D_DrawIcon

		brl Exit


ShowPage
		; don't do anything if the page is current.
		cmpw curpage,pagenum
		cmp pagenum
		jeq exit

		stz count
		; redraw the old page, if visible.

		cmpw P_Page,#2
		bge NotTemplate					 ; curpage is a template

		moveword #-1,count
		lda P_DblPage
		beq HideOldPage

		lda P_Page
		beq HideOldPage
		addword #P_PageWidth-2,count,count
		bra HideOldPage

NotTemplate	cmpw curpage,P_PageStart
		jlt DoNewPage					   ; curpage is scrolled off left

		cmpw P_PageCount,#P_MaxVisPages+1
		blt DoOldPage					   ; all pages are visible.

		addword #P_ControlWidth-2,count,count

		addword #P_MaxVisPages-1,P_PageStart,a
		cmp curpage
		blt DoNewPage					   ; curpage is scrolled off right

DoOldPage	subword curpage,P_PageStart,a

		; x now has 0-4
		rcall D_FastMult,in=(a:x,#P_PageWidth-2:y)

		addword a,#P_PageWidth-1+P_SpaceWidth,a
		addword a,count,count

		lda P_DblPage
		beq HideOldPage
		addword #P_PageWidth-2,count,count

HideOldPage	addword #2,count,tmprect+2
		addword a,#P_PageWidth-4,tmprect+6
		tool _ClipRect,in=(!tmprect:l)

		dec P_Page
		call P_DrawAPage,in=(count:w,yloc:w,curpage:w,#0:w)
		inc P_Page


DoNewPage	addword #2,xloc,tmprect+2
		addword a,#P_PageWidth-4,tmprect+6
		tool _ClipRect,in=(!tmprect:l)

		; do all this stack monkeying w/ P_Page so the page
		; icon changes, then the screen gets refreshed -- it
		; 'looks & feels' better.

		pushword P_Page							 ; ...on the stack
		subword pagenum,P_FirstPOff,off			 ; squirrel away the D_New
		sta P_Page								  ; page # for later...
		call P_DrawAPage,in=(xloc:w,yloc:w,pagenum:w,#0:w)
		pullword P_Page							 ; off the stack.
		tool _ClipRect,in=(#P_ContentRect:l)		; so that P_ChangePage
		call P_ChangePage,in=(off:w,#0:w)		   ; will be happy


Exit	return
		ENDP
		END

