#include once "window9.bi"

Function ScrollBarGadget(ByVal gadget As Long, _
						ByVal x As Long, ByVal y As Long, _
						ByVal width_ As Long, ByVal Height_ As Long, _
						ByVal MINRange As Long, ByVal MAXRange As Long, _
						ByVal Style As Long, ByVal PageLength As Long) As HWND Export
		
	Var scroll=CreateWindowEx(0,"SCROLLBAR","",WS_CHILD Or WS_VISIBLE Or style,_
	x,y,width_,height_,win9GetCurrent,Cast(HMENU,CInt(gadget)),0,0)
	SetPageStepScrollBar(scroll, PageLength)
	SetScrollRange(Scroll, SB_CTL, MINRange, MAXRange, TRUE)
	win9AddNewGadget(gadget,scroll)
    dim as hwnd hwParent = Cast(HWND,win9GetCurrent())
    for i as Long = 0 to 1000
        dim as USTRING sz = getclassname_(hwParent)
        if instr(sz , "CLASSCLASS_") andalso getparent(hwParent) = 0 then
            if getprop(hwParent , "tabstopenable") then
                win9AddTabStop(hwParent , gadget)
            EndIf
            exit for
        else
            hwParent = getparent(hwParent)
            if hwParent = 0 then exit for
        EndIf
    Next
	win9AddColor(scroll,&hf0f0f0,0)
	Return scroll
End Function

Sub SetScrollGadgetPos OverLoad(ByVal gadget As long, ByVal POSITION As Integer) Export
	SetScrollPos(GadgetID(gadget),SB_CTL,POSITION,TRUE)
End Sub

Sub SetScrollGadgetPos OverLoad(ByVal hwnd As HWND, ByVal POSITION As Integer, ByVal style As integer) Export
	If style =0 Then
		SetScrollPos(hwnd,SB_HORZ,POSITION,TRUE)
	Else
		SetScrollPos(hwnd,SB_VERT,POSITION,TRUE)
	EndIf
End Sub

Function GetScrollGadgetPos OverLoad(ByVal gadget As Long) As Integer Export
	Return GetScrollPos(GadgetID(gadget),SB_CTL)
End Function

Function GetScrollGadgetPos OverLoad(ByVal hwnd As hwnd, ByVal style As Integer) As Integer Export
	If style =0 Then
		Return GetScrollPos(hwnd,SB_HORZ)
	Else
		Return GetScrollPos(hwnd,SB_VERT)
	EndIf
End Function

Sub SetScrollGadgetRange OverLoad(ByVal gadget As Long, ByVal MINRange As Integer, ByVal MAXRange As Integer) Export
	SetScrollRange(GadgetID(gadget), SB_CTL, MINRange, MAXRange, TRUE)
End Sub

Sub SetScrollGadgetRange OverLoad(ByVal hwnd As HWND, ByVal MINRange As Integer, ByVal MAXRange As Integer, ByVal style As Integer) Export
	If style=0 Then
		SetScrollRange(hwnd, SB_HORZ, MINRange, MAXRange, TRUE)
	Else
		SetScrollRange(hwnd, SB_VERT, MINRange, MAXRange, TRUE)
	EndIf
End Sub

Function GetScrollGadgetRange OverLoad(ByVal gadget As Long, ByVal flag As Integer) As Integer Export
	Dim As long MINRange
	Dim As long MAXRange
	GetScrollRange(GadgetID(gadget), SB_CTL, @MINRange, @MAXRange)
	Select Case flag
		Case 0
			Return   MINRange
		Case Else
			Return   MAXRange
	End Select
End Function

Function GetScrollGadgetRange OverLoad(ByVal HWND As HWND, ByVal flag As Integer, ByVal style As Integer) As Integer Export
	Dim As long MINRange
	Dim As long MAXRange
	If style=0 Then
		GetScrollRange(HWND, SB_HORZ, @MINRange, @MAXRange)
	Else
		GetScrollRange(HWND, SB_VERT, @MINRange, @MAXRange)
	EndIf
	Select Case flag
		Case 0
			Return   MINRange
		Case Else
			Return   MAXRange
	End Select
End Function

Sub SetScrollGadgetPage OverLoad(ByVal gadget As long, ByVal page As Integer)  Export
	Dim sinf As SCROLLINFO
	sinf.cbSize=SizeOf(sinf)
	sinf.fMask=SIF_RANGE Or SIF_PAGE
	GetScrollInfo(GadgetID(gadget),SB_CTL,@sinf)
	sinf.nPage=page
	SetScrollInfo(GadgetID(gadget),SB_CTL,@sinf,TRUE)
End Sub

Sub SetScrollGadgetPage OverLoad(ByVal HWND As HWND, ByVal page As Integer, ByVal style As Integer)  Export
	Dim sinf As SCROLLINFO
	sinf.cbSize=SizeOf(sinf)
	sinf.fMask=SIF_RANGE Or SIF_PAGE
	If style=0 Then
		GetScrollInfo(hwnd,SB_HORZ,@sinf)
		sinf.nPage=page
		SetScrollInfo(hwnd,SB_HORZ,@sinf,TRUE)
	Else
		GetScrollInfo(hwnd,SB_VERT,@sinf)
		sinf.nPage=page
		SetScrollInfo(hwnd,SB_VERT,@sinf,TRUE)
	EndIf
End Sub
