#include once "MultiLineEditBox_Basis.bas"

declare function AddStringGadget(event as EventHandle ptr,x as integer,y as integer,c as integer,txt as string="",cl as integer=0,se as integer=0) as Gadget ptr
declare function StringGadgetActions(refgad as Gadget ptr,action as integer) as integer
declare sub DrawStringGadget (gad as Gadget ptr)

declare sub StringGadgetSubHandle(gad as Gadget ptr)
declare sub UpdateStringGadgetBox(gad as Gadget ptr)

declare function GetString (gad as Gadget ptr) as string
declare sub SetString (gad as Gadget ptr,txt as string)
declare function FindDot(s as string) as integer
declare sub ScrollToAnEnd (gad as Gadget ptr)

'Ctrl(0) Zeichenbegrenzung 0=String,1=integer,2=Fließkomma,3=binär,4=hexadezimal,5=IP
'Ctrl(1) Schalter "Zeige Stringende"
function AddStringGadget(event as EventHandle ptr,x as integer,y as integer,c as integer,txt as string="",cl as integer=0,sse as integer=0) 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'6 >> Rand
    tmpgad->gadh=fontheight + 6'6 >> Rand

    tmpgad->Ctrl(0)=cl
    tmpgad->Ctrl(1)=sse

    tmpgad->subevent=CreateEventHandle
		if tmpgad->subevent then
      tmpgad->gad(0)=AddMLEBGadget(tmpgad->subevent,x,y,c,1,1)
    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->texto->SetLineContent(1,txt)
    ScrollToAnEnd(tmpgad)

    tmpgad->DoDraw     =@DrawStringGadget
    tmpgad->DoAction   =@StringGadgetActions
    tmpgad->DoUpdate   =@DrawStringGadget
		
    SaveBackGround(tmpgad)
    event->ChainGadget (tmpgad)

		function=tmpgad
  end if
end function



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

      case GADGET_HIT
        StringGadgetSubHandle(refgad)
        SetSelect(refgad->gad(0),1)
        function=-1
      
      case GADGET_HOLD,GADGET_HOLDOFF,GADGET_RELEASE,GADGET_RELEASEOFF
        StringGadgetSubHandle(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 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
          '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
          TraceCursorPosition(refgad->gad(0))
          DrawGadget(refgad->gad(0))
          'DEL Windows
          #ifdef __fb_win32__
            if refgad->event->ASCCODE=83 then
              refgad->texto->KeyDelete
              TraceCursorPosition(refgad->gad(0))
              DrawGadget(refgad->gad(0))
            end if
          #endif

        else

          'BACKSPACE
          if refgad->event->ASCCODE=8 then
            refgad->texto->KeyBackspace
            TraceCursorPosition(refgad->gad(0))
            DrawGadget(refgad->gad(0))
          end if

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

          'Return
          if refgad->event->ASCCODE=13 then
            ScrollToAnEnd(refgad)
            function=-1
          end if

          'Zeicheneingabe
          enable=0

          'Beschränkungen bei der Zeicheneingabe
          select case refgad->Ctrl(0)
            case 0'String
             '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

            case 1'Ganze Zahlen
              if refgad->event->ASCCODE>47 and refgad->event->ASCCODE<58 then enable=1

            case 2'Fließkomma
              if refgad->event->ASCCODE>47 and refgad->event->ASCCODE<58 then enable=1
              if refgad->event->ASCCODE=46 and FindDot(refgad->texto->GetLineContent(1))=0 then enable=1 'wenn Punkt gedrückt und noch kein Punkt im String ist

            case 3'binär
              if refgad->event->ASCCODE>47 and refgad->event->ASCCODE<50 then enable=1

            case 4'hexadezimal
              if refgad->event->ASCCODE>47 and refgad->event->ASCCODE<58 then enable=1
              if refgad->event->ASCCODE>64 and refgad->event->ASCCODE<71 then enable=1
              if refgad->event->ASCCODE>96 and refgad->event->ASCCODE<103 then enable=1
            
            case 5'IPAdressen  ;)
              if refgad->event->ASCCODE>47 and refgad->event->ASCCODE<58 then enable=1
              if refgad->event->ASCCODE>64 and refgad->event->ASCCODE<71 then enable=1
              if refgad->event->ASCCODE>96 and refgad->event->ASCCODE<103 then enable=1
              if refgad->event->ASCCODE=46 then enable=1
          end select

          if enable then
            refgad->texto->KeyAddChar(refgad->event->KEY)
            TraceCursorPosition(refgad->gad(0))
            DrawGadget(refgad->gad(0))
          end if
        end if

      case GADGET_KEYBOARDOFF'Abbruch Keyboardauswertung
        ScrollToAnEnd(refgad)
        SetSelect(refgad->gad(0),0)

        function=0

  end select
end function



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



sub StringGadgetSubHandle(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)
        TraceCursorPosition(gad->gad(0))
    end select
    DrawGadget(gad->gad(0))
  end if
end sub



function GetString (gad as Gadget ptr) as string
  function=gad->texto->GetLineContent(1)
end function



sub SetString (gad as Gadget ptr,txt as string)
  gad->texto->SetLineContent(1,txt)
  ScrollToAnEnd(gad)
  DrawGadget(gad->gad(0))
end sub



function FindDot(s as string) as integer
  function=0
  if len(s)>0 then
    for i as integer=len(s) to 1 step -1
      if mid(s,i,1)="." then function=len(s)-i+1
    next i
  end if
end function


sub ScrollToAnEnd (gad as Gadget ptr)
  if gad->Ctrl(1)=1 then
    HScrollMLEB(gad->gad(0),1)'erst Scrollfenster (virtuell) ganz nach links schieben
    gad->texto->CursorKeyEnd  'dann Cursor nach rechts an Textende schieben
    'dadurch ist immer das Ende des Textes im StringGadget zu sehen
    'Da nun das "Cursorverfolgen" von links(Textanfang) erfolgt
  else
    gad->texto->CursorKeyPos1
  end if
  TraceCursorPosition(gad->gad(0))
end sub
