#include once "MultiLineEditBox_Basis.bas"
#include once "ScrollBar.bas"

declare function AddMultiLineEditBox (event as EventHandle ptr,x as integer,y as integer,c as integer,lines as integer,mode as integer=0) as Gadget ptr
declare function MultiLineEditActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawMultiLineEdit (gad as Gadget ptr)

declare sub MultiLineEditSubHandle(gad as Gadget ptr)
declare sub UpdateMLEB(gad as Gadget ptr)

'Ctrl(4)  0=Viewmodus 1=Editormodus
function AddMultiLineEditBox(event as EventHandle ptr,x as integer,y as integer,c 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=c*8 + 6 + 15 '6 >> Rand innerhalb MLEB,  15>> Breite ScrollBar rechts
    tmpgad->gadh=fontheight*lines + 6 + 15'6 >> Rand innerhalb MLEB, 15>> Höhe der ScrollBar unten
    
    tmpgad->Ctrl(4)=mode
    
    tmpgad->subevent=CreateEventHandle
		if tmpgad->subevent then
      tmpgad->gad(0)=AddMLEBGadget(tmpgad->subevent,x,y,c,lines,mode)
    	tmpgad->gad(1)=AddScrollBar(tmpgad->subevent,x+c*8 + 6,y,lines*fontheight+6,1,1,1,lines,1)
    	tmpgad->gad(2)=AddScrollBar(tmpgad->subevent,x,y+lines*fontheight+6,c*8+6,1,1,1,c,0)
		end if

    tmpgad->texto=tmpgad->gad(0)->texto'Verbindung zum Textobjekt erzeugen das eigentlich zur MLEB in gad(0) gehört
    tmpgad->affiliation=0'das TO darf nicht bei Zugriff über dieses Control gelöscht werden
    
    tmpgad->DoDraw     =@DrawMultiLineEdit
    tmpgad->DoAction   =@MultiLineEditActions
    tmpgad->DoUpdate   =@UpdateMLEB
    
		SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



function MultiLineEditActions(refgad as Gadget ptr,action as integer) as integer
  function=0
  dim as integer enable
  select case action

      case GADGET_HIT
        MultiLineEditSubHandle(refgad)
        SetSelect(refgad->gad(0),1)
        function=-1

      case GADGET_HOLD,GADGET_HOLDOFF,GADGET_RELEASE,GADGET_RELEASEOFF
        MultiLineEditSubHandle(refgad)

      case GADGET_KEYBOARD   'Keyboardauswertung
        'ob eine Taste gedrückt wurde ist schon in GadgetControl überprüft worden
        'wenn man bis hierher gelangt ist, wurde definitiv etwas gedrückt
        if  refgad->event->EXTENDED then
          'Cursorbewegung mit Pfeiltasten
          if refgad->event->ASCCODE=75 then refgad->texto->CursorLeft
          if refgad->event->ASCCODE=77 then refgad->texto->CursorRight
          if refgad->event->ASCCODE=72 then refgad->texto->CursorUp
          if refgad->event->ASCCODE=80 then refgad->texto->CursorDown
          'added by MilkFreeze, modified by Muttonhead :D
          if refgad->event->ASCCODE=71 then refgad->texto->CursorKeyPos1
          if refgad->event->ASCCODE=79 then refgad->texto->CursorKeyEnd
          ModifyScrollBar(refgad->gad(2),TraceCursorPosition(refgad->gad(0)))
          ModifyScrollBar(refgad->gad(1),TraceCursorLine(refgad->gad(0)))
          DrawGadget(refgad->gad(0))
          'DEL Windows
          #ifdef __fb_win32__
            if refgad->event->ASCCODE=83 then
              refgad->texto->KeyDelete
              ModifyScrollBar(refgad->gad(1),1,refgad->texto->GetLines,TraceCursorLine(refgad->gad(0)))
              ModifyScrollBar(refgad->gad(2),TraceCursorPosition(refgad->gad(0)))
              DrawGadget(refgad->gad(0))
            end if
          #endif

        else

          'BACKSPACE
          if refgad->event->ASCCODE=8 then
            refgad->texto->KeyBackspace
            ModifyScrollBar(refgad->gad(1),1,refgad->texto->GetLines,TraceCursorLine(refgad->gad(0)))
            ModifyScrollBar(refgad->gad(2),TraceCursorPosition(refgad->gad(0)))
            DrawGadget(refgad->gad(0))
          end if

          'DEL Linux
          #ifdef __fb_linux__
            if refgad->event->ASCCODE=127 then
              refgad->texto->KeyDelete(refgad->gad(0))
              ModifyScrollBar(refgad->gad(1),1,refgad->texto->GetLines,TraceCursorLine(refgad->gad(0)))
              ModifyScrollBar(refgad->gad(2),TraceCursorPosition(refgad->gad(0)))
              DrawGadget(refgad->gad(0))
            end if
          #endif

          'Return
          if refgad->event->ASCCODE=13 then
            refgad->texto->KeyReturn
            ModifyScrollBar(refgad->gad(1),1,refgad->texto->GetLines,TraceCursorLine(refgad->gad(0)))
            ModifyScrollBar(refgad->gad(2),TraceCursorPosition(refgad->gad(0)))
            DrawGadget(refgad->gad(0))
          end if

          'Zeicheneingabe
          enable=0

          'Linux chr(127) (Delete) ausschließen
          #ifdef __fb_linux__
            if refgad->event->ASCCODE>=32 and refgad->event->ASCCODE<>127 then enable=1
          #endif

          'Windows
          #ifdef __fb_win32__
            if refgad->event->ASCCODE>=32 then enable=1
          #endif

          if refgad->Ctrl(4)<>1 then enable=0

          if enable then
            refgad->texto->KeyAddChar(refgad->event->KEY)
            ModifyScrollBar(refgad->gad(1),1,refgad->texto->GetLines,TraceCursorLine(refgad->gad(0)))
            ModifyScrollBar(refgad->gad(2),1,refgad->texto->GetLongestLine+1,TraceCursorPosition(refgad->gad(0)))
            DrawGadget(refgad->gad(0))
          end if
        end if

      case GADGET_KEYBOARDOFF'Abbruch Keyboardauswertung
        SetSelect(refgad->gad(0),0)
        function=-1

  end select
end function



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



sub MultiLineEditSubHandle(gad as Gadget ptr)
  gad->subevent->xSleep(-1,0)' ein Event der als "Durchläufer" und ohne SLEEP funktioniert!!!!!!!!!!!!!!!!
  if gad->subevent->GADGETMESSAGE then
    select case gad->subevent->GADGETMESSAGE
      case gad->gad(0)
        ModifyScrollBar(gad->gad(1),1,gad->texto->GetLines,TraceCursorLine(gad->gad(0)))
        ModifyScrollBar(gad->gad(2),TraceCursorPosition(gad->gad(0)))
      case gad->gad(1)
        VScrollMLEB(gad->gad(0),GetScrollBarVal(gad->gad(1)))
      case gad->gad(2)
        HScrollMLEB(gad->gad(0),GetScrollBarVal(gad->gad(2)))
    end select
    DrawGadget(gad->gad(0))
  end if
end sub



sub UpdateMLEB(gad as Gadget ptr)
  ModifyScrollBar(gad->gad(1),1,gad->texto->GetLines)
  ModifyScrollBar(gad->gad(2),1,gad->texto->GetLongestLine+1)
  DrawGadget(gad->gad(0))
end sub
