declare function AddToggleGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,h as integer,s as integer,txt as string) as Gadget ptr
declare function ToggleGadgetActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawToggleGadget (gad as Gadget ptr)

function AddToggleGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,h as integer,s as integer,txt as string) 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
    tmpgad->gadw=w
    tmpgad->gadh=h

    tmpgad->caption=txt

    tmpgad->DoDraw     =@DrawToggleGadget
    tmpgad->DoAction   =@ToggleGadgetActions
    tmpgad->DoUpdate   =@DrawToggleGadget
		
    SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function ToggleGadgetActions(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
        if GetSelect(refgad) then SetSelect(refgad,0) else SetSelect(refgad,1)
         function=1
      case GADGET_RELEASEOFF 'Control losgelassen, dabei ist Maus neben dem Control
  end select
end function



sub DrawToggleGadget (gad as Gadget ptr)
	dim as integer x,y,w,h
  dim as string txtu,txts
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh

  screenlock
  if gad->act=0 then
    put(x,y),gad->Images(0),pset
  else
    FrameB x, y, w, h,1
    FillA x+1,y+1,w-2,h-2,GadgetColor,gad->sel
    if gad->sel then
      draw string ( x+(w-len(gad->caption)*8)/2 +1, y+(h-fontheight)/2+1 ),gad->caption,GadgetTextColor
			draw string ( x+(w-len(gad->caption)*8)/2 , y+(h-fontheight)/2 ),gad->caption,white
    else
      draw string ( x+(w-len(gad->caption)*8)/2 , y+(h-fontheight)/2 ),gad->caption,GadgetTextColor
    end if
    if gad->act=2 then put(x,y),gad->Images(0),alpha,SleepShade
  end if
  screenunlock
end sub
