  '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 



  '

  ==========
  class grid
  ==========
  '
  indexbase 1
  '
  int    *xtot
  int    *ytot
  bstring*dat
  bstring fn,fd
  int ex,ey
  int cpick,cy,cx,dg
  int a
  int ct
  bstring bz
  bstring s
  '
  method constructor(int y,x)
  ===========================
  ex=x : ey=y
  @xtot=getmemory ex*sizeof int
  @ytot=getmemory ey*sizeof int
  @dat =getmemory ex*ey*sizeof sys
  remap bstring da[ey,ex]
  @da=@dat
  init
  end method
  '
  method destructor
  =================
  remap bstring da[ey,ex]
  @da=@dat
  int x,y
    for y=1 to ey
      for x=1 to ex
        'frees da[y,x]
      next
    next
    freememory @dat
    freememory @ytot
    freememory @xtot
    frees s
    frees bz
    frees fn
    frees fd
    'mbox "frame count " ct
  end method
  '
  method clear
  ============
  remap bstring da[ey,ex]
  @da=@dat
  int x,y
    'INTIAL EXAMPLE DATA
    for y=1 to ey
      for x=1 to ex
        da[y,x]=""
      next
    next
  end method
  '
  method init
  ===========
  remap bstring da[ey,ex]
  @da=@dat
  int x,y
    'INTIAL EXAMPLE DATA
    for y=1 to ey
      for x=1 to ex
        da[y,x]=str irnd(0,99)
      next
    next
  end method
  '
  method load
  ===========
  remap bstring da[ey,ex]
  @da=@dat
  fd=space ex*ey*16
  mid fd,1,getfile fn
  int i,x,y
    for y=1 to ey
      for x=1 to ex
        da[y,x]=rtrim mid fd,i+1, 14
        i+=16
      next
    next
  end method
  '
  method save
  ===========
  remap bstring da[ey,ex]
    @da=@dat
    int i,x,y
    bstring ss,t
    fd=space ex*ey*16
    for y=1 to ey
      for x=1 to ex
        ss=space 16
        mid ss,1,da[y,x]
        mid fd,i+1,ss
        mid fd,i+15," ,"
        i+=16
      next
      mid fd,i-2,cr 'next row
    next
    putfile fn,fd
    frees ss
    frees t
  end method
  '
  method EditCell(int x,y)
  ========================
  '
  remap bstring da[ey,ex]
  @da=@dat
   if cpick<>picked
    'le=1+len da[y,x]
    cp=1 'place cursor at start of string
    'cp=le+1 'place cursor at end of string
    'place cursor at start of string
    cy=y
    cx=x
    bz=da(y,x) 
    cpick=picked
  endif
  '
  ===================================
  a=input da[y,x], 1 'overtype mode=1
  ===================================
  '
  int dg
  if keystate(VK_SHIFT)
    dg=10
  else
    dg=1
  endif
  '
  if lastchar=26 'CTRL-Z UNDO
    da[y,x]=bz
    cp=1
  endif
  '          
  select lastkey
    case VK_PRIOR 'PGUP
      da[y,x]=str(val(da[y,x])+dg)
    case VK_NEXT 'PGDN
      da[y,x]=str(val(da[y,x])-dg)
    case VK_UP
      cy--
      if cy<1
           cy=ey
      endif
    case VK_DOWN
      cy++
      if cy>ey
        cy=1
      endif
    case 9 'TAB
      if GetAsyncKeyState(VK_SHIFT)=0
        cx++
        if cx>ex
          cx=1
        endif
      else 'go left
        cx--
        if cx<1
          cx=ex
        endif
      endif
    case 13 'CR down
      if GetAsyncKeyState(VK_SHIFT)=0
        cy++
        if cy>ey
          cy=1
        endif
      else 'go up
        cy--
        if cy<1
          cy=ey
        endif
      endif
    end select
    '
    picked=cy*10+cx  
    lastkey=0
    lastchar=0
  end method
  '
  method act
  ==========
  '
  ct++
  remap bstring da[ey,ex]
  @da=@dat
  int i,x,y
  '
  'FILING
  if keystate(VK_CONTROL)
    if lastkey=0x4f 'O
      string ff
      ff=GetFileName "t.txt",0
      if ff
        fn=ff : load
      endif
      'mbox ff
      lastkey=0
      lastchar=0
      key[VK_CONTROL]=0
    elseif lastkey=0x53 'S
      string ff
      ff=GetFileName "t.txt",1
      if ff
        fn=ff : save
      endif
      '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=1 to ex
    PushState
    move x*4
    print "Col " chr(x+64)
    PopState
  next
  PushState
  move x*4
  print "Total"
  PopState
  '
  picklabel 0
  for y=1 to ey
    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=1 to ex
    ytot[x]=0
  next
  '
  'backgound underlay
  if not pick
   color 0,.3,0
   move  3,-0.5,-.015
   PutBoxArea ex*4,ey*-2
   move -3,0.5, .015
  endif
  '
  '
  for y=1 to ey
    int xtot=0
    for x=1 to ex
      v=val(da[y,x])
      xtot+=v
      ytot[x]+=v
      PushState
      move x*4,-y*2
      if pick 'detecting posiyion
        'MARK PICKING AREA
        picklabel y*10+x
        move 0,0,-.01
        PutBoxArea 3,1
        move 0,0, .01
      else 'not pick 'displaying
        if picked=y*10+x
          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
          '
          EditCell(x,y) 'DATA EDITING
          '
        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 da[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=1 to ex
    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 method
  '
  end class 'grid
