#include once "ListBox_Basis.bas"
#include once "ScrollBar.bas"

declare function AddListBox (event as EventHandle ptr,x as integer,y as integer,c as integer,lines as integer) as Gadget ptr
declare function ListActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawList (gad as Gadget ptr)
declare sub UpdateLB(gad as Gadget ptr)

declare sub ListSubHandle(gad as Gadget ptr)

declare function GetListBoxVal(gad as Gadget ptr) as integer
declare sub SetListBoxVal(gad as Gadget ptr,i as integer)


'Ctrl(15) wenn 1 wird ein Ping gegeben
function AddListBox(event as EventHandle ptr,x as integer,y as integer,c as integer,lines as integer) 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=c*8 + 6 + 15 '6 >> Rand innerhalb MLEB,  15>> Breite ScrollBar rechts
    tmpgad->gadh=fontheight*lines + 6'6 >> Rand innerhalb MLEB
    
    tmpgad->subevent=CreateEventHandle
		if tmpgad->subevent then
      tmpgad->gad(0)=AddLBGadget(tmpgad->subevent,x,y,c,lines)
    	tmpgad->gad(1)=AddScrollBar(tmpgad->subevent,x+c*8 + 6,y,lines*fontheight+6+1,1,1,1,lines,1)
		end if

    tmpgad->texto=tmpgad->gad(0)->texto'Verbindung zum Textobjekt erzeugen das eigentlich zur LB in gad(0) gehört
    tmpgad->affiliation=0'das TO darf nicht bei Zugriff über dieses Control gelöscht werden

    tmpgad->DoDraw     =@DrawList
    tmpgad->DoAction   =@ListActions
    tmpgad->DoUpdate   =@UpdateLB

		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function ListActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  dim as integer enable
  select case action
      case GADGET_HIT,GADGET_HOLD,GADGET_HOLDOFF,GADGET_RELEASE,GADGET_RELEASEOFF
        ListSubHandle(refgad)
        if refgad->Ctrl(15) then function=1
  end select
end function



sub DrawList (gad as Gadget ptr)
  DrawGadget(gad->gad(0))
end sub



sub UpdateLB(gad as Gadget ptr)
  gad->gad(0)->Ctrl(15)=0'Selektion in LB aufheben
  ModifyScrollBar(gad->gad(1),1,TO_GetLines(gad),1)
  VScrollLB(gad->gad(0),1)
  DrawGadget(gad->gad(0))
end sub



sub ListSubHandle(gad as Gadget ptr)
  gad->subevent->xSleep(-1,0)'"Durchläufer" und ohne SLEEP
  gad->Ctrl(15)=0
  if gad->subevent->GADGETMESSAGE then
    select case gad->subevent->GADGETMESSAGE
      case gad->gad(0)
        gad->Ctrl(15)=1
      case gad->gad(1)
        VScrollLB(gad->gad(0),GetScrollBarVal(gad->gad(1)))
        DrawGadget(gad->gad(0))
    end select
  end if
end sub



function GetListBoxVal(gad as Gadget ptr) as integer
  function=gad->gad(0)->Ctrl(15)
end function



sub SetListBoxVal(gad as Gadget ptr,i as integer)
  dim as integer l=TO_GetLines(gad)
  if i<0 then i=0
  if i>l then i=l
  gad->gad(0)->Ctrl(15)=i
  ModifyScrollBar(gad->gad(1),i)
  VScrollLB(gad->gad(0),GetScrollBarVal(gad->gad(1)))
  DrawGadget(gad)
end sub
