#include once "Arrows.bas"
#include once "ScrollBar_Basis.bas"

declare function AddScrollBar(event as EventHandle ptr,x as integer,y as integer,l as integer,mi as integer,ma as integer,va as integer,p as integer,hv as integer) as Gadget ptr
declare function ScrollBarActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawScrollBar (gad as Gadget ptr)

declare sub SetScrollBarVal (gad as Gadget ptr,va as integer)
declare sub ModifyScrollBar overload(gad as Gadget ptr,va as integer)
declare sub ModifyScrollBar (gad as Gadget ptr,mi as integer,ma as integer)
declare sub ModifyScrollBar (gad as Gadget ptr,mi as integer,ma as integer,va as integer)
declare function GetScrollBarVal (gad as Gadget ptr) as integer
declare sub SetScrollBarLimits(gad as Gadget ptr,mi as integer,ma as integer)

'Ctrl(0) "Ablage" des Scrollwertes aus ->gad(1)(dem eigentlichen Scrollbar) im SubHandle
'Ctrl(1) Vergleichswert zu Ctrl(0) bei Unterschied gibt es nen Ping
function AddScrollBar(event as EventHandle ptr,x as integer,y as integer,l as integer,mi as integer,ma as integer,va as integer,p as integer,hv as integer) as Gadget ptr
  function=0
  dim as Gadget ptr tmpgad
  tmpgad=new Gadget
  if tmpgad then
    tmpgad->event=event
    tmpgad->nextGadget=0
  	tmpgad->sel=0
    tmpgad->act=0
    tmpgad->posx=x
    tmpgad->posy=y
    tmpgad->gadw=l
    tmpgad->gadh=15
    if hv then swap tmpgad->gadw,tmpgad->gadh'Breite und Höhe tauschen wenn vertikal


    tmpgad->Ctrl(0)=va
	
    tmpgad->subevent=CreateEventHandle
		if tmpgad->subevent then
      if hv then                'wenn vertikal
        tmpgad->gad(0)=AddArrow(tmpgad->subevent, x, y, 2)
      	tmpgad->gad(1)=AddSB(tmpgad->subevent,x,y+16,tmpgad->gadh-32,mi,ma,va,p,1)
      	tmpgad->gad(2)=AddArrow(tmpgad->subevent, x, y+tmpgad->gadh-16, 3)
      else                      'wenn horizontal
        tmpgad->gad(0)=AddArrow(tmpgad->subevent, x, y, 0)
        tmpgad->gad(1)=AddSB(tmpgad->subevent,x+16,y,tmpgad->gadw-32,mi,ma,va,p,0)
        tmpgad->gad(2)=AddArrow(tmpgad->subevent, x+tmpgad->gadw-16, y, 1)
      end if
    end if

    tmpgad->DoDraw     =@DrawScrollBar
    tmpgad->DoAction   =@ScrollBarActions
    tmpgad->DoUpdate   =@UpdateGadgetDummy'falls Veränderungen am TO ein Update erfordern,hier nur ein dummy

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function ScrollBarActions(refgad as Gadget ptr,action as integer) as integer
  function=0

  select case action

      case GADGET_HIT,GADGET_HOLD,GADGET_HOLDOFF,GADGET_RELEASE,GADGET_RELEASEOFF

      	refgad->ctrl(1)=refgad->Ctrl(0)
        refgad->subevent->xSleep(-1,0)' ein Event der als "Durchläufer" und ohne SLEEP funktioniert!!!!!!!!!!!!!!!!
        if refgad->subevent->GADGETMESSAGE then
      
        	refgad->Ctrl(0)=GetSBVal(refgad->gad(1))'egal welches der 3 Controls angeklickt wurde, Scrollbar auslesen
      
          select case refgad->subevent->GADGETMESSAGE
      			case refgad->gad(0)
              refgad->Ctrl(0) -=1
              SetSBVal(refgad->gad(1),refgad->Ctrl(0))
              refgad->Ctrl(0)=GetSBVal(refgad->gad(1))
            case refgad->gad(1)
      			  'seltsam, beim Bewegen des Scrollbars muß garnichts passieren :D
            case refgad->gad(2)
              refgad->Ctrl(0) +=1
              SetSBVal(refgad->gad(1),refgad->Ctrl(0))
              refgad->Ctrl(0)=GetSBVal(refgad->gad(1))
           end select
        end if
        if refgad->ctrl(1)<>refgad->Ctrl(0) then
          DrawGadget(refgad->gad(1))
          function=1
        end if

  end select
end function



sub DrawScrollBar (gad as Gadget ptr)
  'Da es sich hier "nur" um ein Container
  'handelt, passiert hier einfach mal nichts!!!
end sub



sub SetScrollBarVal (gad as Gadget ptr,va as integer)
  SetSBVal (gad->gad(1),va)
  DrawGadget(gad->gad(1))
end sub



sub SetScrollBarLimits(gad as Gadget ptr,mi as integer,ma as integer)
  SetSBLimits(gad->gad(1),mi,ma)
  DrawGadget(gad->gad(1))
end sub



sub ModifyScrollBar (gad as Gadget ptr,va as integer)
  SetSBVal (gad->gad(1),va)
  DrawGadget(gad->gad(1))
end sub



sub ModifyScrollBar (gad as Gadget ptr,mi as integer,ma as integer)
  SetSBLimits(gad->gad(1),mi,ma)
  DrawGadget(gad->gad(1))
end sub



sub ModifyScrollBar (gad as Gadget ptr,mi as integer,ma as integer,va as integer)
  SetSBLimits(gad->gad(1),mi,ma)
  SetSBVal (gad->gad(1),va)
  'gad->gad(1)->DoDraw(gad->gad(1))
  DrawGadget(gad->gad(1))
end sub



function GetScrollBarVal (gad as Gadget ptr) as integer
  function=GetSBVal(gad->gad(1))
end function
