  #compact
  $ filename "t.exe"
 'uses RTL64
  uses ConsoleG
 'uses SysUtil
  uses FileDialogs

  'USER CONTROLS:
  '==============
  '
  'mouse controls:
  ' left-click
  '
  'keyboard controls:
  ' arrow keys
  ' tab shift-tab
  ' PgUp PgDn to inc/dec values (SHIFT step 10)
  

  function irnd(int z1, z2) as int
  ================================
  mov    eax,z2
  sub    eax,z1
  inc    eax
  imul   edx,Seed,0x8088405
  inc    edx
  mov    Seed,edx 'store new seed
  mul    edx 'multiply eax by edx
  return edx+z1
  end Function 



  '
  redim int ytot[5,1] static
  redim string dat[5,5] static

  procedure main()
  ================
  sys a,i,p
  static string ins
  static string s
  if opening
    'INTIAL EXAMPLE DATA
    int x,y
   for y=lbound(dat,1) to ubound(dat,1)
      for x=lbound(dat,2) to ubound(dat,2)
        dat[y,x]=str irnd(0,99)
      next
    next
    '
  endif
  '
  if closing
    del ytot
    del dat
    'mbox "closed"
    exit procedure
  endif


  cls .05,.20,.00
  '
  '/*
  'FILING
  if keystate(VK_CONTROL)
    if lastkey=0x4f 'O
      string ff
      ff=GetFileName "t.txt",0
      'mbox ff
      lastkey=0
      lastchar=0
      key[VK_CONTROL]=0
    elseif lastkey=0x53 'S
      string ff
      ff=GetFileName "t.txt",1
      'mbox ff
      lastkey=0
      lastchar=0
      key[VK_CONTROL]=0
    endif
  endif
  '*/
  '
  'GRID INPUTS
  ------------
  scale 1.5
  int x,y

  pushstate
  move 4,-1
  scale 1.5
  print "GRID TITLE"
  popstate
  move 0,-3
  '
  picklabel 0
  for x=lbound(dat,2) to ubound(dat,2)
    PushState
    move x*4
    print "Col " chr(x+64)
    PopState
  next
  PushState
  move x*4
  print "Total"
  PopState
  '
    picklabel 0
  for y=lbound(dat,1) to ubound(dat,1)
    PushState
    move 1, -y*2
    print "R" str(y)
    PopState
  next
  PushState
  move 1, -y*2
  print "Total"
  PopState
  '
  int v
  int gtot=0
  for x=lbound(dat,2) to ubound(dat,2)
    ytot[x]=0
  next
  '
  'backgound underlay
  if not pick
   color 0,.3,0
   move  3,-0.5,-.015
   PutBoxArea ubound(dat,2)*4,ubound(dat,1)*-2
   move -3,0.5, .015
  endif
  '
  for y=lbound(dat,1) to ubound(dat,1)
    int xtot=0
    for x=lbound(dat,2) to ubound(dat,2)
      v=val(dat[y,x])
      xtot+=v
      ytot[x]+=v
      PushState
      move x*4,-y*2
      if pick
        'MARK PICKING AREA
        picklabel y*10+x
        move 0,0,-.01
        PutBoxArea 3,1
        move 0,0, .01
      else 'not pick
        if picked=y*10+x
          'DATA EDITING
          '
          color 0.9,0.9,0.9
          'box-around
          glBegin GL_LINE_STRIP
          glVertex2f 0,0
          glVertex2f 3,0
          glVertex2f 3,1
          glVertex2f 0,1
          glVertex2f 0,0
          glEnd
          '
          move 0.2,0.2,.01
          ----------------
          a=input dat[y,x]
          ----------------
          static int cpick,cy,cx
          if cpick<>picked
            cp=le+1 'place cursor at end of string
            cy=y
            cx=x
            cpick=picked
          endif
          '
          'stepper by digit positiom
          'int dg
          'dg=le-cp+1
          'if dg<1 then dg=1
          'dg=pow(10,dg-1)
          int dg
          if keystate(VK_SHIFT)
            dg=10
          else
            dg=1
          endif
          '          '          
          select lastkey
          case VK_PRIOR 'PGUP
            dat(y,x)=str(val(dat[y,x))+dg)
          case VK_NEXT 'PGDN
            dat(y,x)=str(val(dat[y,x))-dg)
          case VK_UP
            cy--
            if cy<1
              cy=ubound(dat,1)
            endif
          case VK_DOWN
            cy++
            if cy>ubound(dat,1)
              cy=1
            endif
          case 9 'tab
            if GetAsyncKeyState(VK_SHIFT)=0
              cx++
              if cx>ubound(dat,2)
                cx=1
              endif
            else 'left
              cx--
              if cx<1
                cx=ubound(dat,2)
              endif
            endif
          end select
          if cn>0
            cx++
            if cx>ubound(dat,2)
              cx=1
            endif
          elseif cn<0
            cx--
            if cx<1
              cx=ubound(dat,2)
            endif
          endif
          cpick=cy*10+cx  
          picked=cpick
          lastkey=0
          lastchar=0
        else 'not picked
          'DATA DISPLAY
          move 0,0,-.01
          'color .2,.6,.3
          'PutBoxArea 3,1
          move 0.2,0.2,.01
          color .8,.8,.6
          print dat[y,x]
        endif
      endif
      PopState
    next 'x
    gtot+=xtot
    PushState
    move x*4,-y*2
    color .9,.9,.6
    print xtot
    PopState
  next 'y
  'COL TOTALS
  picklabel 0
  for x=lbound(dat,2) to ubound(dat,2)
    PushState
    move x*4,-y*2
    color .9,.9,.6
    print ytot[x]
    PopState
  next 'col totals
  PushState
  move x*4,-y*2
  color .9,.9,.6
  print gtot
  PopState
  scale 1/1.5
  'printl str(picked)
  '
  end procedure

  EndScript
