
#Include once "crt.bi"
#Include once "inc/iup.bi"
#Include once "inc/iupcontrols.bi"
#include once "inc/iupcd.bi"
#include once "inc/cd.bi"

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

sub _exit_ destructor
  IupClose()
end sub


' IUP handles 
Dim Shared As Ihandle Ptr cnvs 

' CD canvas 
Dim Shared As cdCanvas Ptr cdcanvas

function redraw_cb cdecl(self as Ihandle Ptr) As Integer
  if (cdcanvas=0) then return IUP_DEFAULT
  ' Activates canvas cdcanvas 
  cdActivate( cdcanvas )
  cdClear()
  
  ' Draws a rectangle on the canvas 
  cdBegin(CD_FILL)
  cdVertex(50, 50)
  cdVertex(150, 50)
  cdVertex(100, 150)
  cdEnd()
  
  ' Function executed successfully 
  return IUP_DEFAULT
  
End Function

Function  cell_cb Cdecl(self as Ihandle Ptr, cell As Integer) As ZString Ptr
  Dim As UByte r, g, b
  Dim As Integer ri, gi, bi
  Dim As ZString*30 str_
  sprintf(str_, "CELL%d", cell)
  sscanf(IupGetAttribute(self, str_), "%d %d %d", @ri, @gi, @bi)
  r = ri: g = gi: b = bi
  
  if (IupGetColor(IUP_MOUSEPOS, IUP_MOUSEPOS, @r, @g, @b))Then
    sprintf(str_, "%d %d %d", r, g, b)
    cdActivate( cdcanvas )
    cdForeground(cdEncodeColor(r, g, b))
    redraw_cb(cnvs)
    return StrPtr(str_)
  End If

  return NULL
End Function

function select_cb Cdecl(self as Ihandle Ptr ,cell As integer, type_ As Integer)As Integer
  Dim As LONG cd_color
  Dim As Integer ri, gi, bi
  Dim As ZString*30 str_
  sprintf(str_, "CELL%d", cell)
  sscanf(IupGetAttribute(self, str_), "%d %d %d", @ri, @gi, @bi)
  cd_color = cdEncodeColor(ri,gi,bi)
  cdActivate( cdcanvas )
  if (type_ = IUP_PRIMARY) Then
    cdForeground(cd_color)
  Else
    cdBackground(cd_color)
  End If

  redraw_cb(cnvs)
  return IUP_DEFAULT
End Function

Function switch_cb(self as Ihandle Ptr ,primcell As Integer, seccell As Integer) As Integer
  Dim As Long fgcolor
  cdActivate(cdcanvas)
  fgcolor = cdForeground(CD_QUERY)
  cdForeground(cdBackground(CD_QUERY))
  cdBackground(fgcolor)
  redraw_cb(cnvs)
  return IUP_DEFAULT
end Function

'
' main
'
Dim as Ihandle Ptr dlg, cb
 

' Creates a canvas associated with the redraw action
cnvs = IupCanvas(NULL)
IupSetCallback(cnvs, "ACTION", cast(Icallback,@redraw_cb))
  
' Sets size, minimum and maximum values, position and size of the thumb  
' of the horizontal scrollbar of the canvas    
IupSetAttributes(cnvs, "RASTERSIZE=200x300")

cb = IupColorbar()
IupSetAttribute(cb, "RASTERSIZE", "70x")
IupSetAttribute(cb, "EXPAND", "VERTICAL")
IupSetAttribute(cb, "NUM_PARTS", "2")
IupSetAttribute(cb, "SHOW_SECONDARY", "YES")
IupSetCallback(cb, "SELECT_CB", cast(Icallback,@select_cb))
IupSetCallback(cb, "CELL_CB", cast(Icallback,@cell_cb))
IupSetCallback(cb, "SWITCH_CB", cast(Icallback,@switch_cb))
' IupSetAttribute(cb, "SQUARED", "NO")
IupSetAttribute(cb, "PREVIEW_SIZE", "60")

' Creates a dialog with a vbox containing the canvas and the colorbar. 
dlg = IupDialog(IupHbox(cnvs, cb, NULL))
  
' Sets the dialog's title, so that it is mapped properly 
IupSetAttribute(dlg, "TITLE", "IupColorbar")
  
' Maps the dialog. This must be done before the creation of the CD canvas 
IupMap(dlg)
  
' Creates a CD canvas of type CD_IUP  
cdcanvas = cdCreateCanvas(cdContextIup(), cnvs)

' Shows dialog on the center of the screen 
IupShow(dlg)

' Initializes IUP main loop 
IupMainLoop()

