
#Include "crt.bi"
#Include "inc/iup.bi"
#Include "inc/iupcd.bi"


Dim shared As cdCanvas Ptr cdcanvas 

/' Virtual space size: 600x400
   The canvas will be a window into that space.
   If canvas is smaller than the virtual space, scrollbars are active.
   
   The drawing is a red cross from the corners of the virtual space.
   But CD must draw in the canvas space. So the position of the scrollbar
   will define the convertion between canvas space and virtual space.
'/

function action Cdecl(ih As Ihandle ptr, posx as Single, posy as Single) As Integer
  
  Dim As Integer iposx = posx, iposy = posy

  if (cdcanvas=0) Then Return IUP_DEFAULT

  'update CD canvas size 
  cdCanvasActivate(cdcanvas)

  ' invert scroll reference (YMAX-DY - POSY)
  iposy = 399-IupGetInt(ih, "DY") - iposy
  
  cdCanvasClear(cdcanvas)
  
  cdCanvasForeground(cdcanvas, CD_RED)
  cdCanvasLine(cdcanvas, 0-iposx, 0-iposy, 599-iposx, 399-iposy)
  cdCanvasLine(cdcanvas, 0-iposx, 399-iposy, 599-iposx, 0-iposy)

  return IUP_DEFAULT
	
End Function



Function scroll_cb cdecl(ih As Ihandle ptr,op as integer ,posx as single ,posy as single )As Integer

  action(ih, posx, posy)
  
  return IUP_DEFAULT
  
End Function

Function resize_cb cdecl(ih As Ihandle Ptr,w As Integer ,h As Integer )As Integer
 
 ' update page size, it is always the client size of the canvas 
 
  IupSetfAttribute(ih, "DX", "%d", w)
  IupSetfAttribute(ih, "DY", "%d", h)
  
  ' refresh scrollbar in IUP 2.x 
  
  IupStoreAttribute(ih, "POSX", IupGetAttribute(ih, "POSX"))
  IupStoreAttribute(ih, "POSY", IupGetAttribute(ih, "POSY"))

  if (cdcanvas =0) Then Return IUP_DEFAULT

  ' update CD canvas size 
  cdCanvasActivate(cdcanvas)
  
  return IUP_DEFAULT
  
End Function


  Dim As Ihandle Ptr dialog, canvas

  IupOpen(0,0)

  canvas = IupCanvas(NULL)
  IupSetAttribute(canvas, "RASTERSIZE", "300x200") ' initial size 
  IupSetAttribute(canvas, "SCROLLBAR", "YES")
  IupSetAttribute(canvas, "XMAX", "599")
  IupSetAttribute(canvas, "YMAX", "399")

  IupSetCallback(canvas, "SCROLL_CB",  Cast(Icallback,@scroll_cb))
  IupSetCallback(canvas, "RESIZE_CB",  Cast(Icallback,@resize_cb))
  IupSetCallback(canvas, "ACTION",  Cast(Icallback,@action))
                   
  dialog = IupDialog(canvas)
  IupSetAttribute(dialog, "TITLE", "Scrollbar Test")

  IupMap(dialog)
  cdcanvas = cdCreateCanvas(cdContextIup(), canvas)
  IupSetAttribute(canvas, "RASTERSIZE", NULL)  ' release the minimum limitation
  
  IupShowXY(dialog,IUP_CENTER,IUP_CENTER)

  IupMainLoop()

  cdKillCanvas(cdcanvas)
  IupDestroy(dialog)

  IupClose()
