declare function AddMLEBGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,lines as integer,mode as integer=1) as Gadget ptr
declare function MLEBActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawMLEB(gad as Gadget ptr)

declare sub HScrollMLEB(gad as Gadget ptr,c as integer)
declare sub VScrollMLEB(gad as Gadget ptr,l as integer)
declare function TraceCursorPosition(gad as Gadget ptr) as integer
declare function TraceCursorLine(gad as Gadget ptr) as integer


'Ctrl(0)=Breite der Box in Zeichen
'Ctrl(1)=Höhe der Box in Zeilen
'Ctrl(2)=angescrolltes Zeichen, für Horizontalscrolling, hat nichts mit der Cursoposition im TO der Box
'Ctrl(3)=angescrollte Zeile, für Vertikalscrolling, hat nichts mit der Cursorzeile im TO der Box
'Ctrl(4)= 0=Viewmodus 1=Editormodus
function AddMLEBGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,lines as integer,mode as integer=1) 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*8 + 6
    tmpgad->gadh=fontheight*lines + 6

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

    tmpgad->Ctrl(0)=w
    tmpgad->Ctrl(1)=lines
    tmpgad->Ctrl(2)=1
    tmpgad->Ctrl(3)=1
    tmpgad->Ctrl(4)=mode

    tmpgad->DoDraw     =@DrawMLEB
    tmpgad->DoAction   =@MLEBActions
    tmpgad->DoUpdate   =@UpdateGadgetDummy'falls Veränderungen am TO ein Update erfordern,hier nur ein dummy
		
    SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function MLEBActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  dim as integer mx,my,l,p,ll,lcl,offset
  dim as string lc
  
  select case action

      case GADGET_HIT        'Control grad frisch gedrückt
        mx=refgad->event->MOUSEX
        my=refgad->event->MOUSEY
        ll=refgad->texto->GetLines
        l=-1
        p=-1
        offset=3
        if my>=refgad->posy+offset and my<refgad->posy + refgad->gadh-offset then
          l=refgad->Ctrl(3) + int((my-refgad->posy-offset)/fontheight)
          if l>ll then l=ll
          lc=refgad->texto->GetLineContent(l)
          lcl=len(lc)
        end if

        if mx>=refgad->posx+offset and mx<refgad->posx + refgad->gadw-offset then
          if (refgad->Ctrl(4)=0) or (refgad->Ctrl(4)=1) then
            p=refgad->Ctrl(2) + int((mx-refgad->posx-offset)/8)
            if p>lcl then p=lcl+1
          end if
        end if

        if (l<>-1) and (p<>-1) then
          refgad->texto->SetCursor(l,p)
          function=1
        end if
      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

  end select
end function



sub DrawMLEB (gad as Gadget ptr)
	dim as integer x,y,w,h,offset,lines
  dim as integer boxc,boxl,scrollpos,scrollline,lcount,cursorline,cursorpos,cursorvisible,mode
	dim as uinteger tcolor
  dim as TextLine ptr l
  dim as string tmpstring,cursorchar
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh
	offset=3
  lines=gad->texto->GetLines
  boxc=gad->Ctrl(0)
  boxl=gad->Ctrl(1)
  scrollpos=gad->Ctrl(2)
  scrollline=gad->Ctrl(3)
  l=gad->texto->GetLineAddr(scrollline)
  cursorline=gad->texto->GetCursorLine
  cursorpos=gad->texto->GetCursorPosition
  cursorchar=""
  cursorvisible=0
  tmpstring=gad->texto->GetLineContent(cursorline)
  if cursorline>=scrollline and cursorline<scrollline+boxl and cursorpos>=scrollpos and cursorpos<scrollpos+boxc then
    cursorvisible=1
    if len(tmpstring)>=cursorpos then cursorchar=mid(tmpstring,cursorpos,1)
  end if
  mode=gad->Ctrl(4)

  tmpstring=""
  screenlock
  if gad->act=0 then
    put(x,y),gad->Images(0),pset
  else
    FrameB x,y,w,h,1
		ClearBox x+1,y+1,w-2,h-2,white
    lcount=0
    do
      tmpstring=gad->texto->GetLineContent (l)
      if scrollpos>len(tmpstring) then'Wenn der Zeichenoffset größer ist als Zeilenlänge(Horizontalscrolling)
        tmpstring=""
      else
        tmpstring=mid(tmpstring,scrollpos,boxc)
      end if

      select case mode
        case 0'Textview
          if (gad->sel=1) and (cursorvisible=1) and (scrollline+lcount=cursorline) then _
            ClearBox x+offset+(cursorpos-scrollpos)*8,y+offset+lcount*fontheight+(fontheight-2),8,2,&HEEEEEE
          tcolor=TextColor

        case 1'Cursor
          if (gad->sel=1) and (cursorvisible=1) and (scrollline+lcount=cursorline) then _
            ClearBox x+offset+(cursorpos-scrollpos)*8-1,y+offset+lcount*fontheight,2,fontheight,CursorColor
          tcolor=TextColor

      end select
      
      draw string ( x+offset, y+offset+lcount*fontheight ),tmpstring,tcolor


      l=l->nextline
		  lcount +=1
    loop until (l=0) or (lcount=boxl)'keine Zeile im TO oder der Zeilenzähler=Anzahl der zu zeigenden Zeilen(lcount startet bei 0!)
    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



sub HScrollMLEB(gad as Gadget ptr,c as integer)
  gad->Ctrl(2)=c
end sub



sub VScrollMLEB(gad as Gadget ptr,l as integer)
  gad->Ctrl(3)=l
end sub



function TraceCursorPosition(gad as Gadget ptr) as integer
  dim as integer cp=gad->texto->GetCursorPosition
  if cp < gad->Ctrl(2) then gad->Ctrl(2)= cp
  if cp > gad->Ctrl(2) + gad->Ctrl(0)-1 then gad->Ctrl(2)= cp - gad->Ctrl(0)+1
  function=gad->Ctrl(2)
end function



function TraceCursorLine(gad as Gadget ptr) as integer
  dim as integer cl,ll,sl
  cl=gad->texto->GetCursorLine
  ll=gad->texto->GetLines
  sl=gad->Ctrl(3)

  if cl>=gad->Ctrl(3) and  cl<gad->Ctrl(3) + gad->Ctrl(1) then 'Cursor sichtbar

    if ll>=gad->Ctrl(3) and  ll<gad->Ctrl(3) + gad->Ctrl(1) then'wenn letzte Zeile sichtbar dann versuchen diese auf unterster Zeile im Scrollfenster zu halten
      sl= ll - gad->Ctrl(1)+1
      if sl<1 then sl=1
    end if

  else  'Cursor außerhalb des Scrollfensters dann Cursor zurückholen

    if cl < gad->Ctrl(3) then sl= cl 'vor(über) Scrollfenster
    if cl >= gad->Ctrl(3) + gad->Ctrl(1) then sl= cl - gad->Ctrl(1)+1'hinter(unter) Scrollfenster
  
  end if

  gad->Ctrl(3)=sl
  function=gad->Ctrl(3)
end function
