declare function AddXGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,h as integer,txt as string) as Gadget ptr
declare function XGadgetActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawXGadget (gad as Gadget ptr)

function AddXGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,h 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=0
    tmpgad->act=0
    tmpgad->posx=x
    tmpgad->posy=y
    tmpgad->gadw=w
    tmpgad->gadh=h

    tmpgad->texto=new TextObject
    tmpgad->affiliation=1

    tmpgad->caption=txt
    tmpgad->captionalt=""
   
    for i as integer=0 to ubound(tmpgad->ctrl())
      tmpgad->ctrl(i)=0
    next i

    tmpgad->subevent=0
	  for i as integer=0 to ubound(tmpgad->gad())
      tmpgad->gad(i)=0
    next i

    tmpgad->DoDraw     =@DrawXGadget
    tmpgad->DoAction   =@XGadgetActions
    tmpgad->DoUpdate   =@UpdateGadgetDummy'falls Veränderungen am TO ein Update erfordern, im Zeifel einfach den Dummy belassen oder die DoDraw Routine nehmen

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function XGadgetActions(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

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

      case GADGET_KEYBOARD   'Keyboardauswertung
      
      case GADGET_KEYBOARDOFF'Abbruch Keyboardauswertung
  end select
end function



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

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