

#include once "crt/stdlib.bi"
#include once "crt/stdio.bi"
#include once "inc/iup.bi"
#include once "inc/iupcontrols.bi"
#include once "inc/iupcd.bi"

sub _init_ constructor
  IupOpen(0,0)
  IupControlsOpen()
end sub

sub _exit_ destructor
  IupClose()
end sub

function nlines_cb cdecl (h As Ihandle Ptr )  as integer
  return 7
end function

function ncols_cb cdecl (h As Ihandle Ptr ) as integer
  return 7
end function

function height_cb cdecl (h As Ihandle Ptr ,i as Integer )  as integer
  return (30+i*1.5)
end function

function width_cb cdecl (h As Ihandle Ptr ,j As Integer )  as integer
  Return (50+j*1.5)
end function

function mouseclick_cb cdecl (h As Ihandle Ptr , b as integer,m As ZString ptr ,i As Integer ,j As Integer ,x As Integer ,y As Integer ,r As ZString ptr)  as integer
  printf(!"CLICK: %d: (%02d, %02d)\n", b, i, j)
  return IUP_DEFAULT
end function

function scrolling_cb cdecl (h As Ihandle Ptr ,i as integer ,j as Integer )  as integer
  printf(!"SCROLL: (%02d, %02d)\n", i, j)
  return IUP_DEFAULT
end function

function vspan_cb cdecl (h As Ihandle Ptr ,i as integer ,j as Integer )  as integer
  if (i = 1 And j = 1) Then Return 2
  if (i = 5 And j = 5) Then Return 2
  return 1
end function

function hspan_cb cdecl(h As Ihandle Ptr ,i as integer ,j as Integer )  as integer
  if (i = 1 And j = 1) Then Return 2
  if (i = 5 And j = 5) Then Return 2
  return 1
end function

function draw_cb cdecl (h As Ihandle Ptr,i as integer ,j as integer ,xmin as integer ,xmax as integer ,ymin as integer ,ymax as integer ,canvas As cdCanvas Ptr) As Integer
  Dim As Integer xm = (xmax + xmin) / 2
  Dim As Integer  ym = (ymax + ymin) / 2
  dim As ZString*64 buffer

  if (i = 1 And j = 2) Then Return IUP_DEFAULT
  if (i = 2 And j = 1) Then Return IUP_DEFAULT
  if (i = 2 And j = 2) Then return IUP_DEFAULT
  if (i = 5 And j = 6) Then return IUP_DEFAULT
  if (i = 6 And j = 5) Then return IUP_DEFAULT
  if (i = 6 And j = 6) Then return IUP_DEFAULT

  if (i = 1 and j = 1) Then
    cdCanvasForeground(canvas, CD_WHITE)
  else
    cdCanvasForeground(canvas, cdEncodeColor(_
          (i*20), _
          (j*100), _
          (i+100)))
  EndIf
  cdCanvasBox(canvas, xmin, xmax, ymin, ymax)
  cdCanvasTextAlignment(canvas, CD_CENTER)
  cdCanvasForeground(canvas, CD_BLACK)
  sprintf(buffer, "(%02d, %02d)", i, j)
  cdCanvasText(canvas, xm, ym, buffer)

  return IUP_DEFAULT
end function

function create() as Ihandle ptr
  dim as Ihandle ptr cells = IupCells()

  IupSetCallback(cells, "MOUSECLICK_CB", cast(Icallback,@mouseclick_cb))
  IupSetCallback(cells, "DRAW_CB"      , cast(Icallback,@draw_cb))
  IupSetCallback(cells, "WIDTH_CB"     , cast(Icallback,@width_cb))
  IupSetCallback(cells, "HEIGHT_CB"    , cast(Icallback,@height_cb))
  IupSetCallback(cells, "NLINES_CB"    , cast(Icallback,@nlines_cb))
  IupSetCallback(cells, "NCOLS_CB"     , cast(Icallback,@ncols_cb))
  IupSetCallback(cells, "HSPAN_CB"     , cast(Icallback,@hspan_cb))
  IupSetCallback(cells, "VSPAN_CB"     , cast(Icallback,@vspan_cb))
  IupSetCallback(cells, "SCROLLING_CB" , cast(Icallback,@scrolling_cb))
  IupSetAttribute(cells, "BOXED", "NO")
  IupSetAttribute(cells, "RASTERSIZE","395x255")

  IupSetCallback(cells, "MOUSECLICK_CB", cast(Icallback,@mouseclick_cb))
  IupSetCallback(cells, "DRAW_CB",   cast(Icallback,@draw_cb))
  IupSetCallback(cells, "WIDTH_CB",  cast(Icallback,@width_cb))
  IupSetCallback(cells, "HEIGHT_CB", cast(Icallback,@height_cb))
  IupSetCallback(cells, "NLINES_CB", cast(Icallback,@nlines_cb))
  IupSetCallback(cells, "NCOLS_CB",  cast(Icallback,@ncols_cb))
  IupSetCallback(cells, "HSPAN_CB",  cast(Icallback,@hspan_cb))
  IupSetCallback(cells, "VSPAN_CB",  cast(Icallback,@vspan_cb))
  IupSetCallback(cells, "SCROLLING_CB", cast(Icallback,@scrolling_cb))

  IupSetAttribute(cells, "BOXED", "NO")

  return cells
End Function

'
' main
'
dim as Ihandle ptr cells = create()
dim as Ihandle ptr box = IupVbox(cells, NULL)
IupSetAttribute(box,"MARGIN","10x10")

dim as Ihandle ptr dlg = IupDialog(box)
IupSetAttribute(dlg,"TITLE","IupCells")
IupSetAttribute(dlg,"RASTERSIZE","350x250")
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)

IupMainLoop()

  
