declare function AddCheckMarkGadget (event as EventHandle ptr,x as integer,y as integer,s as integer,txt as string="") as Gadget ptr
declare function CheckMarkGadgetActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawCheckMarkGadget (gad as Gadget ptr)
declare sub UpdateCheckMark (gad as Gadget ptr)

function AddCheckmarkGadget (event as EventHandle ptr,x as integer,y 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
    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

    tmpgad->DoDraw     =@DrawCheckMarkGadget
    tmpgad->DoAction   =@CheckMarkGadgetActions
    tmpgad->DoUpdate   =@UpdateCheckMark
		
    SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function


function CheckMarkGadgetActions(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 DrawCheckmarkGadget (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
    FillB x+boxv,y+boxv,12,12,GadgetColor,gad->sel
    if gad->sel then Tick (x+boxv+2,y+boxv+2,GadgetTextColor)
    draw string (x+20,y+txtv),gad->caption,TextColor
    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 UpdateCheckMark (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


