'##############################################################################
'##############################################################################
'Begin BMPSimpleGadget.bas

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

declare function BMPSimpleGadgetHit(hitgad as Gadget ptr,junk as Gadget ptr) as integer
declare function BMPSimpleGadgetHold(hitgad as Gadget ptr,holdgad as Gadget ptr) as integer
declare function BMPSimpleGadgetRelease(hitgad as Gadget ptr,relgad as Gadget ptr) 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="",fg 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
    tmpgad->text=""


    tmpgad->ImageUnselected=imagecreate(w,h)
    bload fus,tmpgad->ImageUnSelected


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

    if fg>"" then
      tmpgad->ImageGhosted=imagecreate(w,h)
      bload fg,tmpgad->ImageGhosted
    end if

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

    tmpgad->event=event

    tmpgad->DoDraw     =@DrawBMPSimpleGadget
    tmpgad->DoHit      =@BMPSimpleGadgetHit
    tmpgad->DoHold     =@BMPSimpleGadgetHold
    tmpgad->DoRelease  =@BMPSimpleGadgetRelease
    tmpgad->DoKeyStream=0

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



function BMPSimpleGadgetHit(hitgad as Gadget ptr,junk as Gadget ptr) as integer
  SetSelect (hitgad,1)
  function=0
end function



function BMPSimpleGadgetHold (hitgad as Gadget ptr,holdgad as Gadget ptr) as integer
  function=0
end function



function BMPSimpleGadgetRelease(hitgad as Gadget ptr,relgad as Gadget ptr) as integer
  function=0
  SetSelect (hitgad,0)
  if hitgad=relgad then function=-1
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->BackGroundImage,trans

  if (gad->act=1) then
		put(x,y),gad->BackGroundImage,trans
		if (gad->sel=1) and (gad->ImageSelected>0) then
		  put(x,y),gad->ImageSelected,trans
		else
			put(x,y),gad->ImageUnSelected,trans
  	end if
	end if

	if (gad->act=2)  and (gad->ImageGhosted>0) then
		put(x,y),gad->BackGroundImage,trans
		put(x,y),gad->ImageGhosted,trans
  end if
  screenunlock
end sub

'End BMPSimpleGadget.bas
'##############################################################################
'##############################################################################
