declare function AddTrackBar(event as EventHandle ptr,x as integer,y as integer,l as integer,mi as integer,ma as integer,va as integer,hv as integer) as Gadget ptr
declare function TrackBarActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawTrackBar (gad as Gadget ptr)

declare sub AnalogSlideTrackBar(gad as Gadget ptr,knobpos as integer)
declare sub SetTrackBarVal (gad as Gadget ptr,va as integer)
declare function GetTrackBarVal (gad as Gadget ptr) as integer

'Steuerungsvariablen TrackBar:
'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(14) alter Regelwert um Veränderung des ->Ctrl(15) festzustellen
'Ctrl(15) Regelwert
function AddTrackBar (event as EventHandle ptr,x as integer,y as integer,l as integer,mi as integer,ma as integer,va as integer,hv as integer) as Gadget ptr
  function=0
  dim as Gadget ptr tmpgad
  dim as integer range

  if ma<mi then ma=mi

  if va>ma then va=ma
  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->gadw=w 'weiter unten
    'tmpgad->gadh=h 'weiter unten

    tmpgad->Ctrl(0)=mi
    tmpgad->Ctrl(1)=ma
    tmpgad->Ctrl(5)=hv
    tmpgad->Ctrl(15)=va

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

    tmpgad->DoDraw     =@DrawTrackBar
    tmpgad->DoAction   =@TrackBarActions
    tmpgad->DoUpdate   =@DrawTrackBar

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function TrackBarActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  dim as integer mx,my
  mx=refgad->event->MOUSEX
  my=refgad->event->MOUSEY

  select case action
      
      case GADGET_HIT        'Control grad frisch gedrückt
        if refgad->Ctrl(5) then
          'vertikal
          if my-refgad->posy>=refgad->Ctrl(2) and _           'Knob Treffer
            my-refgad->posy<refgad->Ctrl(2)+11 then
            refgad->Ctrl(3)=(my-refgad->posy)-refgad->Ctrl(2) 'Speichern des MouseOffsets überm Knob
            refgad->Ctrl(4)=1 'SliderModus bei Hold aktivieren
            SetSelect (refgad,1)
          else  'anderenfalls Knob jeweils um 1 in Richtung Mausposition verschieben, kein HOLD
            if my < refgad->posy + refgad->Ctrl(2) then
              SetTrackBarVal(refgad,GetTrackBarVal(refgad)-1)
            elseif my >= refgad->posy+ refgad->Ctrl(2) + 11 then
              SetTrackBarVal(refgad,GetTrackBarVal(refgad)+1)
            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)+11 then
            refgad->Ctrl(3)=(mx-refgad->posx)-refgad->Ctrl(2) 'Speichern des MouseOffsets überm Knob
            refgad->Ctrl(4)=1 'SliderModus bei Hold aktivieren
            SetSelect (refgad,1)
          else  'anderenfalls Knob jeweils um 1 in Richtung Mausposition verschieben, kein HOLD
            if mx < refgad->posx + refgad->Ctrl(2) then
              SetTrackBarVal(refgad,GetTrackBarVal(refgad)-1)
            elseif mx >= refgad->posx + refgad->Ctrl(2) + 11 then
              SetTrackBarVal(refgad,GetTrackBarVal(refgad)+1)
            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
        if refgad->Ctrl(4)=1 then
          refgad->Ctrl(14)=GetTrackBarVal(refgad)
          if refgad->Ctrl(5) then  'vertikal
            AnalogSlideTrackBar(refgad,my- refgad->posy-refgad->Ctrl(3))
          else                'horizontal
            AnalogSlideTrackBar(refgad,mx- refgad->posx-refgad->Ctrl(3))
          end if
          if refgad->Ctrl(14)<>GetTrackBarVal(refgad) then function=1
        end if

      'case GADGET_HOLDOFF    'Control wird gehalten, Maus neben dem Control

      case GADGET_RELEASE,GADGET_RELEASEOFF    'Control regulär losgelassen/Control losgelassen, dabei ist Maus neben dem Control
        refgad->Ctrl(4)=0
        SetSelect(refgad,0)
      
      'case GADGET_RELEASEOFF 'Control losgelassen, dabei ist Maus neben dem Control

  end select
end function



sub DrawTrackBar (gad as Gadget ptr)
	dim as integer i,x,y,w,h,p,s,range
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh
	p		=gad->Ctrl(2)
	range=gad->Ctrl(1) - gad->Ctrl(0)

  screenlock
	if gad->act=0 then
    put(x,y),gad->Images(0),pset
  else
    put(x,y),gad->Images(0),pset
    if gad->Ctrl(5) then'vertikal
      Slot x+10,y,h,1
      FillB x,y+p,22,11,GadgetColor,gad->sel'Knob
    else'horizontal
      Slot x,y+10,w,0
      FillB x+p,y,11,22,GadgetColor,gad->sel'Knob
    end if
    if gad->act=2 then put(x,y),gad->Images(0),alpha,SleepShade 'Shade (x, y,w, h,gad->Images(0))
  end if
  screenunlock
end sub



sub AnalogSlideTrackBar(gad as Gadget ptr,knobpos as integer)
	dim as integer range,l
  range=gad->Ctrl(1) - gad->Ctrl(0)

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

  if knobpos<0 then knobpos=0
  if knobpos>l-11 then knobpos=l-11

  gad->Ctrl(2)=knobpos
  if range then gad->Ctrl(15)=gad->Ctrl(0)  +  cint(range*gad->Ctrl(2)/(l-11))

  DrawGadget(gad)
end sub



sub SetTrackBarVal (gad as Gadget ptr,va as integer)
	dim as integer range,l
  range=gad->Ctrl(1) - gad->Ctrl(0)

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

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

  gad->Ctrl(15)=va
  if range then gad->Ctrl(2)=(gad->Ctrl(15)-gad->Ctrl(0))/range * (l-11)

  DrawGadget(gad)
end sub



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