declare function AddLabel (event as EventHandle ptr,x as integer,y as integer,txt as string,col as integer=TextColor) as Gadget ptr
declare function LabelActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawLabel (gad as Gadget ptr)
declare sub UpdateLabel (gad as Gadget ptr)

declare sub SetLabelColor (gad as Gadget ptr,col as integer)

'Ctrl(0) Farbe des Textes
'Images(2) Hintergrund von x-Position bis rechter Screenrand
function AddLabel (event as EventHandle ptr,x as integer,y as integer,txt as string,col as integer=TextColor) as Gadget ptr
  function=0
  dim as Gadget ptr tmpgad
  dim as integer scrw
  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=8*len(txt)
    tmpgad->gadh=fontheight

    tmpgad->caption=txt

    tmpgad->Ctrl(0)=col

    tmpgad->DoDraw     =@DrawLabel
    tmpgad->DoAction   =@LabelActions
    tmpgad->DoUpdate   =@UpdateLabel
    
    SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function LabelActions(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, Maus über dem Control
        function=1
      
      case GADGET_RELEASEOFF 'Control losgelassen, dabei ist Maus neben dem Control

  end select
end function



sub DrawLabel (gad as Gadget ptr)
	dim as integer x,y,w,h
  dim as string txt
  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
    draw string ( x, y),gad->caption,gad->Ctrl(0)
    if gad->act=2 then put(x,y),gad->Images(0),alpha,SleepShade
  end if
  screenunlock
end sub



sub UpdateLabel (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=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



sub SetLabelColor (gad as Gadget ptr,col as integer)
  gad->Ctrl(0)=col
  gad->DoDraw(gad)
end sub
