  #compact
  % filename "t.exe"
  % Title "Console Demo (F1 help)"
 '% Animated
  % ScaleUp
 '% PlaceCentral
 '% AnchorCentral
 '% NoEscape
 '% WindowOpacity 244 'max 255

 int width=1000
 int height=800

  'uses RTL64
  uses ConsoleG


================
Class ScrollEdit
================
  '
  '-----------------------
  'globals of input()
  'int cp 'char position
  'int cn ' next cell
  'int le 'length of sting
  '-----------------------

  's       line array
  'a       cr search pos
  'b       cr over pos
  'e
  'en      line tally
  'n       lines to display
  'pkl     picked line index, current
  'pks     picked line index, previous
  'pke     line index, entering act
  'pickex  picked holder
  'pickey  key-picked code
  'i,j,k   iterators
  'p,q     picked char positions
  'inputs  global line buffer
  'fi      filename
  't       text buffer
  'le      length of text buffer
  'a,b


  int a,b,c,e,i,j,k,n,p,q
  int en,ev,le
  int pks,pkl,pke,pickex,pickey
  string fi,t
  string s[1000]
  '
  method constructor(int nl, string ff)
  =====================================
    'FILL DATA
    n=nl : b=1 : e=n : j=0 
    fi=ff
    t=getfile fi
    le=len t
    '
    'SPLIT INTO LINES
    en=0 : a=0 : b=1
    do
      a=instr b,t,cr
      if a=0
        a=le+1
        if a>b
          en++ : s[en]=mid t,b,a-b 'FINAL
        endif
        exit do
      endif
      en++ : s[en]=mid t,b,a-b
      b=a+2
    end do
    b=1
    pickey=0x101
    pickex=0x101
  float  a '??
  cls 0,0.2,0.2
  '
  end method 'constructor
  '
  method destructor
  =================
  end method
  '
  '
  method DelLines
  ===============
  en-=c
  k=i+c
  for j=i to en
    s[j]=s[k]
    k++
  next
  end method
  '
  method CopyLines
  ================
  k=i
  for j=1 to c
    cut[j]=s[k]
    k++
  next
  cutn=c
  end method
  '
  method CutLines
  ===============
  CopyLines
  DelLines
  end method
  '
  method InsLines
  ===============
  k=en+cutn
  for j=en to i step-1
    s[k]=s[j]
    k--
  next
  en+=cutn
  k=i
  for j=1 to cutn
    s[k]=cut[j]
    k++
  next
  end method
  '
  method SelectUsingKeys
  ======================
  '
  'A  ACTIVE STEP
  'B  START OF BLOCK
  'E  END OF BLOCK
  'EN END OF DATA
  'N  WINDOW SIZE
  '
  int c,i,j,k,p,q,r
  '
  'parts of id
  q=pickey and 0x0000ff  'character
  p=pickey >>8  'line
  r=pickex >>8  'prev line
  '
  if p>r
    i=r
    c=p-r+1
  elseif p<r
    i=p
    c=r-p+1
  else
    i=p
    c=1
  endif
  if key[VK_CONTROL]
    select lastkey
    case 0x2E 'del' 46
      DelLines  : a=1
    case 0x43 'C'
      CopyLines : a=1
    case 0x56 'V'
      InsLines  : a=1
    case 0x58 'X'
      CutLines  : a=1
    case 33
      a=en : b=1      : i=1 'ctrl-pgup
    case 34
      a=en : b=en+1-n : i=n 'ctrl-pgdn
    end select
  else
    select lastkey
    case  9  : a=9
    case 33  : a=-n : if b+a>=1  then b+=a 'pgup
    case 34  : a=n  : if b+a<=en then b+=a 'pgdn
    case 38  : ' up
      a=-1
      if i+b+a>=1
        if i>1
          i+=a
        else
          if b>1
            b+=a
          endif
        endif
      endif
    case 40  : 'down
      a=1
      if i+b+a-1<=en
        if i+a<=n
          i+=a
        else
          b+=a
        endif
      endif
    end select
  endif
  if a
    pickey=q+i*0x100
    pickex=pickey
    lastkey=0
    lastchar=0
    pks=0
    e=b+n-1
  endif
  end method 'SelectUsingKeys


  method LineEdit
  ===============
  int i,p,q
  p=pickey >>8      'line
  q=pickey and 0xff 'char
  if not pks=pkl
    inputs=s[pkl] 'another line
  endif
  cp=q 'char position

  --------------
  a=input inputs
  --------------
  q=cp 'updated char position
  '
  'SPLIT LINE / DELETE MERGE
  if a=13
    'line splitting
    s[pkl]=left inputs,cp-1
     inputs=mid inputs,cp
    q=1
    pkl++ : p++
    if p>n
      b+=1 : p=n : e=b+n-1
    endif
    '
    ''INSERT BELOW PKL (current line)
    'make gap
    for i=en to pkl step -1: s[i+1]=s[i] : next
    en++ 'incr last line
  elseif a=8 and pkl>1
    'backspace/delete
    pkl--  : p--
    if p<1 then b-=1 : p=1 : e=b+n-1
    q=len s[pkl]+1
    inputs=s[pkl]+inputs
    for i=pkl+1 to <en : s[i]=s[i+1] : next
    en-=1
  elseif a=46 and pkl<en
    'merge lines
    inputs+=s[pkl+1]
    for i=pkl+1 to <en : s[i]=s[i+1] : next
    en-=1
  endif
  pickey=p*0x100+q
  if not pick
    if picked
      pickey=picked
      'itr
      'cp=pickey and 0xff
      picked=0
    endif
  endif
  if a then pickex=pickey
  s[pkl]=inputs
  lastkey=0
  lastchar=0
  pks=pkl
  end method 'LineEdit
  '
  '
  method act
  ==========

  'ARROW KEYS ETC FOR SCROLLING LISTS
  '
  j=pickey >>8 'line
  if j>0 and j<=n
    SelectUsingKeys
    pkl=b+j-1
  endif
  '
  j=pickex >>8 'line
  pke=b+j-1
  '
  pushstate 'main
  scale 1.25,1
  printl "File: " fi
  scale .8,1
  printl
  picktext=0x0
  for i=b to e
    picktext+=0x100 'line offset
    if i>en
      printl
    else
      PushState
        color .99,.40,.00
        print i 'line numbers
      PopState
      a=0.2
      move 3 'margin
      PushState
      if i>=pkl and i<=pke then a=1
      if i>=pke and i<=pkl then a=1
      if pkl=i 'detect picked line
        '
        if not pick
          PushState
          Color .10,.10,.90 'blue
          move -.50,-0.2,-0.001
          PutBoxArea 20,1 'line bar
          PopState
        endif
        '
        color .40,.80,.80 '
        LineEdit()
        '
      else
        color .40,.80,.80 '
        print s[i]
      endif
      PopState
      move -3,-1 'cr lf
      PickLabel 0
    endif
  next 'e
  lastchar=0
  popstate 'main
  if key[0x70]
    help 'F1
  endif
  picklabel 0
  lastkey=0
  '
  end method 'act
  '
  method help
  ===========
  '
    pushstate
    move 20,0
    color .8,.5
    scale  1.5,1.0
    printl "Active Keys:"
    printl
    scale  1/1.5,1.0
    printl "Left Right Up Down"
    printl "Home End PgUp PgDn"
   'printl "Space"
   'printl "Tab"
    printl "Enter"
    printl "Esc"    tab "Exit"
    printl "Ctrl-P" tab "Take snapshot"
    printl "F1 This help panel"
    printl
    scale 1.5,1.0
    printl "Mouse:"
    scale 1/1.5,1.0
    printl "Left Click to Select"
    popstate
    '
  end method 'help
  '
  '
  end class 'ScrollEdit
  '
  '
  function main
  =============
  flat
  cls
  picksetup 'position detection phase
  if opening
    new ScrollEdit Editor(16,"ScrollEdit1.o2bas") static
  endif
  if closing
    del Editor
    exit function
  endif
  pushstate
  'usermovement m1,100
  'if pick
  '  m1.mode=0x001
  '  putboxarea 10,10
  'else
    Editor.act
  'endif
  popstate
  end function 'main

  EndScript
