'##############################################################################
'##############################################################################
'Begin RadioButton.bas
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 RBHit(hitgad as Gadget ptr,junk as Gadget ptr) as integer
declare function RBHold(hitgad as Gadget ptr,holdgad as Gadget ptr) as integer
declare function RBRelease(hitgad as Gadget ptr,relgad as Gadget ptr) as integer

declare sub DrawRadioButton (gad as Gadget ptr)

'Steuerungsvariablen:
'Ca erster RB
'Cb letzte RB (nur im Head RB definiert)
'Cc 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
  dim as Gadget ptr tmpgad,lastgad

  function=0
  tmpgad=new Gadget
  if tmpgad then
    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->text=txt

    tmpgad->event=event

    tmpgad->DoDraw     =@DrawRadioButton
    tmpgad->DoHit      =@RBHit
    tmpgad->DoHold     =@RBHold
    tmpgad->DoRelease  =@RBRelease
    tmpgad->DoKeyStream=0

    if Head=0 then
      tmpgad->Ca=cast(integer,tmpgad)
      tmpgad->Cb=cast(integer,tmpgad)
      tmpgad->Cc=0
    else
      if cast(integer,Head)=Head->Ca then
        dim as Gadget ptr lastgad=cast(Gadget ptr,Head->Cb)
        tmpgad->Ca=cast(integer,Head)
        Head->Cb=cast(integer,tmpgad)
        lastgad->Cc=cast(integer,tmpgad)
      end if
    end if

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

    function=tmpgad
  end if
end function



function RBHit(hitgad as Gadget ptr,junk as Gadget ptr) as integer
  function=0
end function



function RBHold(hitgad as Gadget ptr,holdgad as Gadget ptr) as integer
  function=0
end function



function RBRelease(hitgad as Gadget ptr,relgad as Gadget ptr) as integer
  if hitgad=relgad then
    dim as Gadget ptr element=cast(Gadget ptr,hitgad->Ca)
    do
      if element=hitgad then
        SetSelect(element,1)
      else
         SetSelect(element,0)
      end if
      if element->Cc=0 then exit do
       element=cast(Gadget ptr,element->Cc)
    loop
    function=-1
  else
    function=0
  end if
end function



sub DrawRadioButton (gad as Gadget ptr)
	dim as integer x,y,w,h,txtv,boxv
  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->BackGroundImage,pset

  if gad->act=1 then
    put(x,y),gad->BackGroundImage,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->text,TextColor
  end if

  if gad->act=2 then
    put(x,y),gad->BackGroundImage,pset
    FrameC x+boxv,y+boxv
    if gad->sel then Dot x+boxv+2,y+boxv+2,BackGroundColor
    draw string (x+20,y+txtv),gad->text,ShiftRGB(BackGroundColor,-contrast\2)
  end if









/'
	if gad->act=1 then
	  FrameC x,y
		if gad->sel then Dot x+2,y+2,ShiftRGB(GadgetColor,-contrast\2)
  end if

  if gad->act=2 then
		FrameC x,y
		if gad->sel then Dot x+2,y+2,BackGroundColor
	end if
'/
	screenunlock
end sub

'End RadioButton.bas
'##############################################################################
'##############################################################################
