declare function AddRadioButton (event as EventHandle ptr,x as integer,y as integer,s as integer,txt as string="",Head as Gadget ptr) as Gadget ptr
declare function RadioButtonActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawRadioButton (gad as Gadget ptr)
declare sub UpdateRadioButton (gad as Gadget ptr)

'Steuerungsvariablen:
'Ctrl(0) erster RB
'Ctrl(1) letzte RB (nur im Head RB definiert)
'Ctrl(2) nachfolgender RB
function AddRadioButton (event as EventHandle ptr,x as integer,y as integer,s as integer,txt as string="",Head as Gadget ptr) as Gadget ptr
  function=0
  dim as Gadget ptr tmpgad
  tmpgad=new Gadget
  if tmpgad then
    tmpgad->event=event
    tmpgad->nextGadget=0
  	tmpgad->sel=s
    tmpgad->act=0
    tmpgad->posx=x
    tmpgad->posy=y
    if txt="" then
      tmpgad->gadw=16
    else
      tmpgad->gadw=20 + len(txt)*8
    end if
    if fontheight=8 then
      tmpgad->gadh=12
    else
      tmpgad->gadh=fontheight
    end if

    tmpgad->caption=txt
    
    if Head=0 then
      tmpgad->Ctrl(0)=cast(integer,tmpgad)
      tmpgad->Ctrl(1)=cast(integer,tmpgad)
      tmpgad->Ctrl(2)=0
    else
      if cast(integer,Head)=Head->Ctrl(0) then
        dim as Gadget ptr lastgad=cast(Gadget ptr,Head->Ctrl(1))
        tmpgad->Ctrl(0)=cast(integer,Head)
        Head->Ctrl(1)=cast(integer,tmpgad)
        lastgad->Ctrl(2)=cast(integer,tmpgad)
      end if
    end if

    tmpgad->DoDraw     =@DrawRadioButton
    tmpgad->DoAction   =@RadioButtonActions
    tmpgad->DoUpdate   =@UpdateRadioButton

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function


function RadioButtonActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  select case action
      
      case GADGET_HIT        'Control grad frisch gedrückt

      case GADGET_HOLD       'Control wird gehalten, Maus über dem Control

      case GADGET_HOLDOFF    'Control wird gehalten, Maus neben dem Control

      case GADGET_RELEASE    'Control regulär losgelassen
        dim as Gadget ptr element=cast(Gadget ptr,refgad->Ctrl(0))
        do
          if element=refgad then
            SetSelect(element,1)
          else
             SetSelect(element,0)
          end if
          element=cast(Gadget ptr,element->Ctrl(2))
        loop until element=0
        function=1

      case GADGET_RELEASEOFF 'Control losgelassen, dabei ist Maus neben dem Control

  end select
end function



sub DrawRadioButton (gad as Gadget ptr)
	dim as integer x,y,w,h,txtv,boxv
	dim as string txt
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh
  txtv=(h-fontheight)\2
  boxv=(h-12)\2

  screenlock
	if gad->act=0 then
    put(x,y),gad->Images(0),pset
  else
    put(x,y),gad->Images(0),pset
    FrameC x+boxv,y+boxv
    if gad->sel then Dot x+boxv+2,y+boxv+2,GadgetColor
    draw string (x+20,y+txtv),gad->caption,TextColor
    if gad->act=2 then put(x,y),gad->Images(0),alpha,SleepShade
  end if
	screenunlock
end sub



sub UpdateRadioButton (gad as Gadget ptr)
  dim as integer ptr img
  dim as integer newgadw
  put(gad->posx,gad->posy),gad->Images(0),pset        'lösche alten Text auf Screen mit altem Hintergrund aus images(0)
  imagedestroy(gad->Images(0))                        'alten Hintergrund freigeben
  gad->gadw=20+len(gad->caption)*8                       'Breite nach Länge neuen String setzen
  img=imagecreate(gad->gadw,gad->gadh)                'neues Image mit neuer Breite erzeugen
  get (gad->posx,gad->posy)-(gad->posx+gad->gadw-1,gad->posy+gad->gadh-1),img'Hintergrund sichern
  gad->Images(0)=img                                  'neuer Hintergrund in Image(0) hinterlegen
  DrawGadget(gad)                                    'Neuzeichnen
end sub
