'##############################################################################
'##############################################################################
'Begin TextViewBox.bas

'Container***************************************
declare function AddTextViewBox (event as EventHandle ptr,x as integer,y as integer,w as integer,rows as integer,sa() as string) as Gadget ptr

declare sub InitNewTextArray(gad as Gadget ptr,sa() as string,si as integer=1)
declare sub ScrollText(gad as Gadget ptr,si as integer)

declare function TextViewSubHandle(gad as Gadget ptr,junkb as Gadget ptr) as integer
declare sub DrawTextViewBox (gad as Gadget ptr)

declare function GetMaxRowLength(sa() as string) as integer

declare function AddTextViewBoxGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,rows as integer,sa() as string,mrl as integer) as Gadget ptr
declare function TextViewHit(hitgad as Gadget ptr,junk as Gadget ptr) as integer
declare function TextViewHold(hitgad as Gadget ptr,holdgad as Gadget ptr) as integer
declare function TextViewRelease(hitgad as Gadget ptr,relgad as Gadget ptr) as integer

declare sub ScrollTextView(gad as Gadget ptr,si as integer,ci as integer)

declare sub DrawTextView(gad as Gadget ptr)

'Steuerungsvariablen:
'Container
'Ca	vertikale Scroll-Hilfsvariable
'Cb	horizontale Scroll-Hilfsvariable

'TextViewBox
'Ca Breite Container in Zeichen
'Cb Höhe Container in Zeilen
'Cc Zeiger auf externes String-Array(kleinster Index)
'Cd grösste Indexnummer
'Ce Scroll - Indexnummer (Offset beim vert. Scrollen)
'Cf grösste Zeilenlänge
'Cg Scroll Zeichen (Offset beim horiz. Scrollen)


'ContainerGadget***************************************************************
function AddTextViewBox (event as EventHandle ptr,x as integer,y as integer,w as integer,rows as integer,sa() as string) as Gadget ptr
  dim as Gadget ptr tmpgad
	dim as integer mrl=GetMaxRowLength(sa())
	function=0
  tmpgad=new Gadget
	if tmpgad then
    tmpgad->nextGadget=0
  	tmpgad->sel=0
    tmpgad->act=0
    tmpgad->posx=x
    tmpgad->posy=y
    tmpgad->gadw=w*8 + 6 + 15 '6 >> Rand innerhalb TextViewBox,  15>> Breite Arrows,Slider rechts
    tmpgad->gadh=fontheight*rows + 6 + 15'6 >> Rand innerhalb TextViewBox, 15>> Höhe der Arrows,Slider unten

    tmpgad->value=0
    tmpgad->event=event

    tmpgad->DoDraw     =@DrawTextViewBox
    tmpgad->DoHit      =@TextViewSubHandle
    tmpgad->DoHold     =@TextViewSubHandle
    tmpgad->DoRelease  =@TextViewSubHandle
    tmpgad->DoKeyStream=0

		tmpgad->Ca=1
		tmpgad->Cb=1

    tmpgad->subevent=CreateSubEventHandle
		if tmpgad->subevent then
      tmpgad->subevent->gad(0)=AddTextViewBoxGadget(tmpgad->subevent->event,x,y,w,rows,sa(),mrl)
      'Arrows,ScrollBar senkrecht
			tmpgad->subevent->gad(1)=AddArrow(tmpgad->subevent->event,x+w*8 + 6,y,2)
    	tmpgad->subevent->gad(2)=AddScrollBar(tmpgad->subevent->event,x+w*8 + 6,y+16,fontheight*rows + 6-32,1,ubound(sa),1,rows,1)
    	tmpgad->subevent->gad(3)=AddArrow(tmpgad->subevent->event,x+w*8 + 6,y+fontheight*rows + 6 - 16 ,3)
      'Arrows,ScrollBar waagerecht
			tmpgad->subevent->gad(4)=AddArrow(tmpgad->subevent->event,x, y + fontheight*rows + 6,0)
    	tmpgad->subevent->gad(5)=AddScrollBar(tmpgad->subevent->event,x+16 ,y + fontheight*rows + 6,w*8 + 6 -32,1,mrl,1,w,0)
    	tmpgad->subevent->gad(6)=AddArrow(tmpgad->subevent->event,x+w*8 + 6 - 16,y+fontheight*rows + 6 ,1)
		end if

		'SaveBackGround(tmpgad) 'da die ->DoDraw Prozedur für den Container "leer" ist, sparen wir hier mal den Speicher
    'die Controls des SubEvents erledigen das Hintergrundspeichern für sich allein 
		event->ChainGadget (tmpgad)
		function=tmpgad
	end if
end function



function TextViewSubHandle(gad as Gadget ptr,junkb as Gadget ptr) as integer
	function=0

	dim as integer VTextViewPos,HTextViewPos
	VTextViewPos=gad->Ca
	HTextViewPos=gad->Cb

	gad->subevent->event->xSleep(0,0)' ein Event der als "Durchläufer" funktioniert!!!!!!!!!!!!!!!!
	if gad->subevent->event->GADGETMESSAGE then
  	select case gad->subevent->event->GADGETMESSAGE
'			TextViewer keine Aktion beim Draufklicken
'	    case gad->subevent->gad(0)
'      	function=-1
			case gad->subevent->gad(1)
        VTextViewPos -=1
        SetScrollBarVal(gad->subevent->gad(2),VTextViewPos)
        VTextViewPos=GetScrollBarVal(gad->subevent->gad(2))
        ScrollTextView(gad->subevent->gad(0),VTextViewPos,HTextViewPos)

			case gad->subevent->gad(2)
        VTextViewPos=GetScrollBarVal(gad->subevent->gad(2))
        ScrollTextView(gad->subevent->gad(0),VTextViewPos,HTextViewPos)

			case gad->subevent->gad(3)
        VTextViewPos +=1
        SetScrollBarVal(gad->subevent->gad(2),VTextViewPos)
        VTextViewPos=GetScrollBarVal(gad->subevent->gad(2))
        ScrollTextView(gad->subevent->gad(0),VTextViewPos,HTextViewPos)
        
      case gad->subevent->gad(4)
        HTextViewPos -=1
        SetScrollBarVal(gad->subevent->gad(5),HTextViewPos)
        HTextViewPos=GetScrollBarVal(gad->subevent->gad(5))
        ScrollTextView(gad->subevent->gad(0),VTextViewPos,HTextViewPos)

			case gad->subevent->gad(5)
        HTextViewPos=GetScrollBarVal(gad->subevent->gad(5))
        ScrollTextView(gad->subevent->gad(0),VTextViewPos,HTextViewPos)

			case gad->subevent->gad(6)
        HTextViewPos +=1
        SetScrollBarVal(gad->subevent->gad(5),HTextViewPos)
        HTextViewPos=GetScrollBarVal(gad->subevent->gad(5))
        ScrollTextView(gad->subevent->gad(0),VTextViewPos,HTextViewPos)

		end select
		gad->Ca=VTextViewPos
		gad->Cb=HTextViewPos
		gad->subevent->gad(0)->DoDraw(gad->subevent->gad(0))
	end if
end function



sub InitNewTextArray(gad as Gadget ptr,sa() as string,si as integer=1)
	dim as integer VTextViewPos,HTextViewPos
	VTextViewPos=si
	HTextViewPos=1

  'ehemalig NewTextArray(gad->subevent->gad(0),sa())
  'direkt integriert da nur einmal aufgerufen
	gad->subevent->gad(0)->Cc=cast(integer,@(sa(lbound(sa))))
  gad->subevent->gad(0)->Cd=ubound(sa)
  if gad->subevent->gad(0)->Cd=0 then gad->subevent->gad(0)->Ce=0 else gad->subevent->gad(0)->Ce=1
  gad->subevent->gad(0)->value=0
  gad->subevent->gad(0)->Cf=GetMaxRowLength(sa())

  'sub NewTextArray(gad as Gadget ptr,sa() as string)
  '  gad->Cc=cast(integer,@(sa(lbound(sa))))
  '  gad->Cd=ubound(sa)
  '  if gad->Cd=0 then gad->Ce=0 else gad->Ce=1
  '  gad->value=0
  '  gad->Cf=GetMaxRowLength(sa())
  'end sub

  SetScrollBarLimits(gad->subevent->gad(2),1,ubound(sa))
  SetScrollBarLimits(gad->subevent->gad(5),1,gad->subevent->gad(0)->Cf)

  SetScrollBarVal(gad->subevent->gad(2),VTextViewPos)
  VTextViewPos=GetScrollBarVal(gad->subevent->gad(2))

  SetScrollBarVal(gad->subevent->gad(5),HTextViewPos)
  HTextViewPos=GetScrollBarVal(gad->subevent->gad(5))

  ScrollTextView(gad->subevent->gad(0),VTextViewPos, HTextViewPos)

  gad->Ca=VTextViewPos
	gad->Cb=HTextViewPos
	
  gad->subevent->gad(0)->DoDraw(gad->subevent->gad(0))
end sub



sub ScrollText(gad as Gadget ptr,si as integer)
	dim as integer VTextViewPos,HTextViewPos
	VTextViewPos=si
	HTextViewPos=1

  SetScrollBarVal(gad->subevent->gad(2),VTextViewPos)'ScrollBar verschieben
  VTextViewPos=GetScrollBarVal(gad->subevent->gad(2))'falls VTextViewPos ausserhalb der Grenzen korrigierten Werte aus dem ScrollBar holen

  SetScrollBarVal(gad->subevent->gad(5),HTextViewPos)'selbiges Procedere
  HTextViewPos=GetScrollBarVal(gad->subevent->gad(5))

  ScrollTextView(gad->subevent->gad(0),VTextViewPos, HTextViewPos)' und nun Scrollen

  gad->Ca=VTextViewPos 'Zwischenspeichern der Scrollpositionen im Container
	gad->Cb=HTextViewPos
	
  gad->subevent->gad(0)->DoDraw(gad->subevent->gad(0))' Neuzeichnen der TextViewBox
end sub



sub DrawTextViewBox (gad as Gadget ptr)
'Da es sich hier "nur" um ein Container
'handelt, passiert hier einfach mal nichts!!!
end sub


'******************************************************************************

function GetMaxRowLength(sa() as string) as integer
	function=0
	dim as integer length=0
	if ubound(sa)>0 then
		for i as integer=1 to ubound(sa)
			if len(sa(i))>length then length=len(sa(i))
		next i
		function=length
	end if
end function



function AddTextViewBoxGadget (event as EventHandle ptr,x as integer,y as integer,w as integer,rows as integer,sa() as string, mrl as integer) as Gadget ptr
  dim as Gadget ptr tmpgad
  function=0
  tmpgad=new Gadget
  if tmpgad then
    tmpgad->nextGadget=0
  	tmpgad->sel=0
    tmpgad->act=0
    tmpgad->posx=x
    tmpgad->posy=y
    tmpgad->gadw=w*8 + 6
    tmpgad->gadh=fontheight*rows + 6

    tmpgad->value=0
    tmpgad->event=event

    tmpgad->DoDraw     =@DrawTextView
    tmpgad->DoHit      =@TextViewHit
    tmpgad->DoHold     =@TextViewHold
    tmpgad->DoRelease  =@TextViewRelease
    tmpgad->DoKeyStream=0

    tmpgad->Ca=w
    tmpgad->Cb=rows
    tmpgad->Cc=cast(integer,@(sa(lbound(sa))))
    tmpgad->Cd=ubound(sa)
    if tmpgad->Cd=0 then tmpgad->Ce=0 else tmpgad->Ce=1

		tmpgad->Cf=mrl
    tmpgad->Cg=1
		
		SaveBackGround(tmpgad)		
		event->ChainGadget (tmpgad)
		function=tmpgad
  end if
end function



function TextViewHit(hitgad as Gadget ptr,junk as Gadget ptr) as integer
  function=0
end function



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



function TextViewRelease(hitgad as Gadget ptr,relgad as Gadget ptr) as integer
  function=0
end function



sub ScrollTextView(gad as Gadget ptr,si as integer, ci as integer)
  dim as integer drows,upperindex,dchars,mrl
  dchars=gad->Ca
	drows =gad->Cb
  upperindex=gad->Cd
  mrl=gad->Cf
	if upperindex then
    if si>upperindex-drows+1 then si=upperindex-drows+1'kann unter 1 sein !!
    if si<1 then si=1                                  'wird aber hiermit behoben
    gad->Ce=si
  else
    gad->Ce=0
  end if
	if mrl then
		if ci>gad->Cf-dchars+1 then ci=gad->Cf-dchars+1'kann unter 1 sein !!
  	if ci<1 then ci=1                              'wird aber hiermit behoben
    gad->Cg=ci
  else
    gad->Cg=0
	end if
end sub



sub DrawTextView (gad as Gadget ptr)
	dim as integer x,y,w,h,offset,i
	dim as integer dwidth,drows,upperindex,vscrollindex,hscrollindex,tmpindex,selectindex
  dim as string ptr stringptr
  dim as string tmpstring
  x   =gad->posx
	y   =gad->posy
	w		=gad->gadw
	h		=gad->gadh
	offset=3

  dwidth=gad->Ca
  drows =gad->Cb
  stringptr=cast(string ptr,gad->Cc)
  upperindex=gad->Cd
  vscrollindex=gad->Ce
  hscrollindex=gad->Cg
  'selectindex=gad->value

  screenlock
  if gad->act=0 then put(x,y),gad->BackGroundImage,pset

  if gad->act=1 then
    FrameB x,y,w,h,1
		ClearBox x+1,y+1,w-2,h-2,white
		for i=0 to drows-1
      tmpindex=vscrollindex+i
      if tmpindex>0 and tmpindex<= upperindex then
				tmpstring=UImport(mid(stringptr[tmpindex],hscrollindex,dwidth))
        if tmpindex=selectindex then
          ClearBox(x+offset, y+offset+i*fontheight,dwidth*8,fontheight,CursorColor)
          draw string ( x+offset, y+offset+i*fontheight ),tmpstring,white
        else
          ClearBox(x+offset, y+offset+i*fontheight,dwidth*8,fontheight,white)
          draw string ( x+offset, y+offset+i*fontheight ),tmpstring,TextColor
		    end if
      else
        ClearBox(x+offset, y+offset+i*fontheight,dwidth*8,fontheight,white)
      end if
    next i
  end if

  if gad->act=2 then
		FrameB x,y,w,h,1
    ClearBox x+1,y+1,w-2,h-2
    for i=0 to drows-1
      tmpindex=vscrollindex+i
      if tmpindex>0 and tmpindex<= upperindex then
        tmpstring=UImport(mid(stringptr[tmpindex],hscrollindex,dwidth))
        if tmpindex=selectindex then ClearBox(x+offset, y+offset+i*fontheight,dwidth*8,fontheight,ShiftRGB(BackGroundColor,-contrast\8))
        draw string ( x+offset, y+offset+i*fontheight ),tmpstring,ShiftRGB(BackGroundColor,-contrast\2)
      end if
    next i
  end if
  screenunlock
end sub
'End TextView.bas
'##############################################################################
'##############################################################################
