declare function AddBMPSimpleGadget (event as EventHandle ptr,x as integer,y as integer,fus as string,fs as string="",fm as string="") as Gadget ptr
declare function BMPSGActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawBMPSimpleGadget (gad as Gadget ptr)

function AddBMPSimpleGadget (event as EventHandle ptr,x as integer,y as integer,fus as string,fs as string="",fm as string="") as Gadget ptr

  dim as Gadget ptr tmpgad
  function=0
  dim as integer w,h,ff=freefile
  open  fus for binary as #ff
    get #ff, 19, w
    get #ff, 23, h
  close #ff

  tmpgad=new Gadget
  if tmpgad then
    tmpgad->nextGadget=0
  	tmpgad->sel=0
    tmpgad->act=0
    tmpgad->posx=x
    tmpgad->posy=y
    tmpgad->gadw=w
    tmpgad->gadh=h

    if fm>"" then
      tmpgad->Images(1)=imagecreate(w,h)
      bload fm,tmpgad->Images(1)
    end if

    tmpgad->Images(2)=imagecreate(w,h)
    bload fus,tmpgad->Images(2)

    if fs>"" then
      tmpgad->Images(3)=imagecreate(w,h)
      bload fs,tmpgad->Images(3)
    end if

    tmpgad->event=event

    tmpgad->DoDraw     =@DrawBMPSimpleGadget
    tmpgad->DoAction   =@BMPSGActions
    tmpgad->DoUpdate   =@DrawBMPSimpleGadget

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)
    function=tmpgad
  end if
end function



function BMPSGActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  select case action
      
      case GADGET_HIT        'Control grad frisch gedrückt
        SetSelect (refgad,1)
      
      case GADGET_HOLD       'Control wird gehalten, Maus über dem Control
        SetSelect (refgad,1)
      
      case GADGET_HOLDOFF    'Control wird gehalten, Maus neben dem Control
        SetSelect (refgad,0)
      
      case GADGET_RELEASE    'Control regulär losgelassen
        SetSelect (refgad,0)
        function=1           'Ping!!!! Control meldet sich: bei mir ist was passiert!!!
      
      case GADGET_RELEASEOFF 'Control losgelassen, dabei ist Maus neben dem Control
        SetSelect (refgad,0)
  
  end select
end function



sub DrawBMPSimpleGadget (gad as Gadget ptr)
	dim as integer x,y,w,h
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh
  
  screenlock
	if gad->act=0 then 
    put(x,y),gad->Images(0),trans
  else
		put(x,y),gad->Images(0),trans
		if (gad->sel=1) and (gad->Images(3)>0) then
		  put(x,y),gad->Images(3),trans
		else
			put(x,y),gad->Images(2),trans
  	end if
    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
