declare function AddSB(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 SBActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawSB (gad as Gadget ptr)

declare sub AnalogSlideSB(gad as Gadget ptr,knobpos as integer)
declare sub SetSBVal (gad as Gadget ptr,va as integer)
declare function GetSBVal (gad as Gadget ptr) as integer
declare sub SetSBLimits(gad as Gadget ptr,mi as integer,ma as integer)

'Steuerungsvariablen SB:
'Ctrl(0)  Minimalwert den .value als Rückgabewert annehmen kann
'Ctrl(1)  Maximalwert den .value als Rückgabewert annehmen kann
'Ctrl(2)  Position des Knobs in px, ergibt sich aus dem Wert in .value
'Ctrl(3)  Mausoffset beim Klicken auf den Knob
'Ctrl(4)  Schalter für Sliding-Modus
'Ctrl(5)  Schalter vertikal(1)/horizontal(0)
'Ctrl(6)  KnobSize
'Ctrl(7)  PageSize
'Ctrl(14) alter Regelwert
'Ctrl(15) Regelwert

function AddSB(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
  dim as integer range,ks,full

  if ma<mi then ma=mi
  range=ma-(p-1)-mi
  if va>mi+range then va=mi+range  'sollte range kleiner 1 sein so ergibt das für va einen Fehler (va ist unter mi)!!!
  if va<mi then va=mi

  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->Ctrl(15)=va
    
    tmpgad->Ctrl(0)=mi
    tmpgad->Ctrl(1)=ma
    '.Cc siehe weiter unten
    tmpgad->Ctrl(5)=hv

    ks=(l-2) * p/(ma-mi+1)
    if ks<6 then ks=8
    if ks>l-2 then ks=l-2

    tmpgad->Ctrl(6)=ks
    tmpgad->Ctrl(7)=p
    tmpgad->Ctrl(6)=ks
    tmpgad->Ctrl(7)=p

    if tmpgad->Ctrl(5) then
     'vertikal
      tmpgad->gadw=15
      tmpgad->gadh=l
      if range>=1 then
        tmpgad->Ctrl(2)=1 + (tmpgad->Ctrl(15)-tmpgad->Ctrl(0))/range * (tmpgad->gadh - 2 - tmpgad->Ctrl(6))
      else
        tmpgad->Ctrl(2)=1
      end if
    else
      'horizontal
      tmpgad->gadw=l
      tmpgad->gadh=15
      if range>=1 then
        tmpgad->Ctrl(2)=1 + (tmpgad->Ctrl(15)-tmpgad->Ctrl(0))/range * (tmpgad->gadw - 2 - tmpgad->Ctrl(6))
      else
        tmpgad->Ctrl(2)=1
      end if
    end if

    tmpgad->DoDraw     =@DrawSB
    tmpgad->DoAction   =@SBActions
    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 SBActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  dim as integer mx,my

  select case action

      case GADGET_HIT        'Control grad frisch gedrückt

        mx=refgad->event->MOUSEX
        my=refgad->event->MOUSEY

        if refgad->Ctrl(5) then
          'vertikal
          if my-refgad->posy>=refgad->Ctrl(2) and _           'Knob Treffer
            my-refgad->posy<refgad->Ctrl(2)+refgad->Ctrl(6) then
            refgad->Ctrl(3)=my - refgad->posy  - refgad->Ctrl(2) 'Speichern des MouseOffsets überm Knob
            refgad->Ctrl(4)=1 'SliderModus für Hold aktivieren
            SetSelect (refgad,1)
          else 'anderenfalls Knob jeweils um page in Richtung Mausposition verschieben, kein HOLD
            if my < refgad->posy + refgad->Ctrl(2) then
              SetSBVal(refgad,GetSBVal(refgad)-refgad->Ctrl(7))
              DrawGadget(refgad)
            elseif my >= refgad->posy+ refgad->Ctrl(2) + refgad->Ctrl(6) then
              SetSBVal(refgad,GetSBVal(refgad)+refgad->Ctrl(7))
              DrawGadget(refgad)
            end if
            refgad->Ctrl(4)=0
          end if
        else
          'horizontal
          if mx-refgad->posx>=refgad->Ctrl(2) and _           'Knob Treffer
            mx-refgad->posx<refgad->Ctrl(2)+refgad->Ctrl(6) then
            refgad->Ctrl(3)=mx - refgad->posx  - refgad->Ctrl(2) 'Speichern des MouseOffsets überm Knob
            refgad->Ctrl(4)=1 'SliderModus für Hold aktivieren
            SetSelect (refgad,1)
            DrawGadget(refgad)
          else 'anderenfalls Knob jeweils um page in Richtung Mausposition verschieben, kein HOLD
            if mx < refgad->posx + refgad->Ctrl(2) then
              SetSBVal(refgad,GetSBVal(refgad)-refgad->Ctrl(7))
              refgad->DoDraw(refgad)
            elseif mx >= refgad->posx + refgad->Ctrl(2) + refgad->Ctrl(6) then
              SetSBVal(refgad,GetSBVal(refgad)+refgad->Ctrl(7))
              DrawGadget(refgad)
            end if
            refgad->Ctrl(4)=0
          end if
        end if
        function=1

      case GADGET_HOLD,GADGET_HOLDOFF       'Control wird gehalten, Maus über/neben dem Control
        mx=refgad->event->MOUSEX
        my=refgad->event->MOUSEY
        if refgad->Ctrl(4)=1 then
          refgad->Ctrl(14)=GetSBVal(refgad)
          if refgad->Ctrl(5) then 'vertikal
            AnalogSlideSB(refgad,my- refgad->posy-refgad->Ctrl(3))
          else               'horizontal
            AnalogSlideSB(refgad,mx- refgad->posx-refgad->Ctrl(3))
          end if
      
          if refgad->Ctrl(14)<>GetSBVal(refgad) then
            SetSelect (refgad,1)
            function=1
          end if
        end if

      case GADGET_RELEASE,GADGET_RELEASEOFF    'Control losgelassen
        refgad->Ctrl(4)=0
        SetSelect(refgad,0)

  end select
end function



sub DrawSB (gad as Gadget ptr)
	dim as integer i,x,y,w,h,p,k
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh
	p		=gad->Ctrl(2)
	k   =gad->Ctrl(6)

  screenlock
	if gad->act=0 then 
    put(x,y),gad->Images(0),pset
  else
    FrameB x,y,w,h,1
		ClearBox x+1,y+1,w-2,h-2,ShiftRGB(BackGroundColor,-contrast\6)
    if gad->Ctrl(5) then'vertikal
      FillB x+1,y+p,w-2,k,GadgetColor,gad->sel
    else'horizontal
      FillB x+p,y+1,k,h-2,GadgetColor,gad->sel
    end if
    if gad->act=2 then put(x,y),gad->Images(0),alpha,SleepShade
  end if
  screenunlock
end sub



sub AnalogSlideSB(gad as Gadget ptr,knobpos as integer)
	dim as integer range,l

  if gad->Ctrl(5) then l=gad->gadh else l=gad->gadw ' vert.-->l=gadh  /  horiz.-->l=gadw
  range=gad->Ctrl(1)- (gad->Ctrl(7)-1) - gad->Ctrl(0)

  'knob Position festlegen
  if knobpos<1 then knobpos=1
  if knobpos>l-2-gad->Ctrl(6) then knobpos=l-1-gad->Ctrl(6)
  gad->Ctrl(2)=knobpos

  'Regelgröße festlegen
  if range>=1 then
    gad->Ctrl(15)=gad->Ctrl(0)  +  cint(range*(gad->Ctrl(2)-1)/(l-2-gad->Ctrl(6)))
  else
    gad->Ctrl(15)=gad->Ctrl(0)
  end if

  DrawGadget(gad)
end sub



sub SetSBVal (gad as Gadget ptr,va as integer)
	dim as integer range,l
  if gad->Ctrl(5) then l=gad->gadh else l=gad->gadw ' vert.-->l=gadh  /  horiz.-->l=gadw

  range=gad->Ctrl(1)- (gad->Ctrl(7)-1) - gad->Ctrl(0)

  if va>gad->Ctrl(0)+range then va=gad->Ctrl(0)+range
  if va<gad->Ctrl(0) then va=gad->Ctrl(0)

  gad->Ctrl(15)=va

  if range>=1 then
    gad->Ctrl(2)=1 + (gad->Ctrl(15)-gad->Ctrl(0))/range * (l - 2 - gad->Ctrl(6))
  else
    gad->Ctrl(2)=1
  end if
end sub



function GetSBVal (gad as Gadget ptr) as integer
	function=gad->Ctrl(15)
end function



sub SetSBLimits(gad as Gadget ptr,mi as integer,ma as integer)
  dim as integer range,l,va,p,ks

  'Werte holen
  if gad->Ctrl(5) then l=gad->gadh else l=gad->gadw ' vert.-->l=gadh  /  horiz.-->l=gadw
  va=gad->Ctrl(15)
  p =gad->Ctrl(7)

  'Limiter für ma
  if ma<mi then ma=mi

  range=ma-(p-1)-mi

  if va>mi+range then va=mi+range  'sollte range kleiner 1 sein so ergibt das für va einen Fehler (va ist unter mi)!!!
  if va<mi then va=mi              'hiermit korrigiert

  'Knobsize berechen
  ks=(l-2) * p/(ma-mi+1)
  if ks<6 then ks=8
  if ks>l-2 then ks=l-2

  'Werte zurückschreiben
  gad->Ctrl(15)=va
  gad->Ctrl(0)=mi
  gad->Ctrl(1)=ma
  gad->Ctrl(6)=ks
  if range>=1 then
    gad->Ctrl(2)=1 + (gad->Ctrl(15)-gad->Ctrl(0))/range * (l - 2 - gad->Ctrl(6))
  else
    gad->Ctrl(2)=1
  end if
end sub
