#include once "fltk-c.bi"

'test of:
' Fl_Table                  http://www.fltk.org/doc-1.3/classFl__Table.html
' Fl_TableExNew
' Fl_TableExSetDrawCellCB
' Fl_TableSetRows
' Fl_TableSetRowHeader
' ...
' DrawPushClip
' DrawPopClip
' DrawBox
' DrawRect
' DrawRectFill
' DrawSetColor
' DrawStrBox

const MAX_ROWS = 32
const MAX_COLS = 26

dim shared as integer datas(MAX_ROWS-1,MAX_COLS-1)

sub DrawHeader(tbl as Fl_Table ptr,s as string, _
               x as integer, y as integer, w as integer, h as integer)
  dim as zstring ptr z = strptr(s)
  DrawPushClip(x,y,w,h)
    DrawBox(FL_THIN_UP_BOX, x,y,w,h, Fl_TableGetRowHeaderColor(tbl))
    DrawSetColor(FL_BLACK)
    DrawStrBox(z, x,y,w,h, FL_ALIGN_CENTER)
  DrawPopClip()
end sub

sub DrawData(tbl as Fl_Table ptr,s as string, _
             x as integer, y as integer, w as integer, h as integer)
  dim as zstring ptr z = strptr(s)
  DrawPushClip(x,y,w,h)
    DrawSetColor(FL_WHITE): DrawRectFill(x,y,w,h)
    DrawSetColor(FL_GRAY0): DrawStrBox(z, x,y,w,h, FL_ALIGN_CENTER)
    DrawSetColor(Fl_BLACK): DrawRect(x,y,w,h)
  DrawPopClip()
end sub

sub DrawCellCB cdecl (self as any ptr, context as Fl_TableContext, _
                      r as integer, c as integer, _
                      x as integer, y as integer, w as integer, h as integer)
  '? r & "," & c & " " & x,y,w,h
  select case as const context
  case FL_CONTEXT_STARTPAGE
    DrawSetFont(FL_HELVETICA, 16)
  case FL_CONTEXT_COL_HEADER
    DrawHeader(self,chr(asc("A")+c),x,y,w,h)
  case FL_CONTEXT_ROW_HEADER
    DrawHeader(self,str(r),x,y,w,h)
  case FL_CONTEXT_CELL
    DrawData(self,str(datas(r,c)),x,y,w,h)
  end select
end sub

'
' main
'
for r as integer=0 to MAX_ROWS-1
  for c as integer=0 to MAX_COLS-1
    datas(r,c)=rnd*1000
  next
next

dim as Fl_Window  ptr win = Fl_WindowNew(320,200, "Resize me ...")
dim as Fl_TableEx ptr tbl = Fl_TableExNew(20,20,280,160,"Fl_TableEX")
Fl_TableExSetDrawCellCB tbl,@DrawCellCB

Fl_GroupBegin(tbl)              ' open child list

' Rows
Fl_TableSetRows(tbl,MAX_ROWS)   ' how many rows
Fl_TableSetRowHeader(tbl,1)     ' enable row headers (along left)
Fl_TableRowHeightAll(tbl,20)    ' default height of rows
Fl_TableSetRowResize(tbl,0)     ' disable row resizing
'  Cols
Fl_TableSetCols(tbl,MAX_COLS)   ' how many columns
Fl_TableSetColHeader(tbl,1)     ' enable column headers (along top)
Fl_TableColWidthAll(tbl,80)     ' default width of columns
Fl_TableSetColResize(tbl,1)     ' enable column resizing

Fl_GroupEnd(tbl)                ' close child list

Fl_GroupSetResizable(win,win)

Fl_WindowShow(win)
Fl_Run
