#ifdef __FB_WIN32__
# ifndef IUP_DLL
#  define IUP_DLL
# endif
#endif

#include "crt.bi"
#include once "inc/iup.bi"
#include once "inc/iupcd.bi"
#include once "inc/iupweb.bi"

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

sub _exit_ destructor
  IupClose()
end sub


#ifndef __FB_WIN32__
function history_cb cdecl(ih As Ihandle Ptr ) As Integer
  Dim As Integer i
  Dim As String*50 str_
  Dim As Integer back = IupGetInt(ih, "BACKCOUNT")
  Dim As Integer fwrd = IupGetInt(ih, "FORWARDCOUNT")

  printf("HISTORY ITEMS\n")
 
  for i = -(back) to  -1
    sprintf(str_, "ITEMHISTORY%d", i)
    printf(!"Backward %02d: %s\n", i, IupGetAttribute(ih, str_))
  Next

  sprintf(str_, "ITEMHISTORY%d", 0)
  printf(!"Current  %02d: %s\n", 0, IupGetAttribute(ih, str_))

  for i = 1 to fwrd
    sprintf(str_, "ITEMHISTORY%d", i)
    printf(!"Forward  %02d: %s\n", i, IupGetAttribute(ih, str_))
  Next

  return IUP_DEFAULT
End Function
#endif

function navigate_cb cdecl(self As Ihandle Ptr, Url As ZString Ptr) As Integer
  printf(!"NAVIGATE_CB: %s\n", url)
  if strstr(url, "download")<>NULL Then Return IUP_IGNORE
  return IUP_DEFAULT
End Function
                   
function error_cb cdecl(self As Ihandle Ptr, Url As ZString Ptr) As Integer
  printf(!"ERROR_CB: %s\n", url)
  return IUP_DEFAULT
End Function

function completed_cb cdecl(self As Ihandle Ptr, Url As ZString Ptr) As Integer
  printf(!"COMPLETED_CB: %s\n", url)
  return IUP_DEFAULT
End Function

function newwindow_cb cdecl(self As Ihandle Ptr, Url As ZString Ptr) As Integer
  printf(!"NEWWINDOW_CB: %s\n", url)
  IupSetAttribute(self, "VALUE", url)
  return IUP_DEFAULT
End Function

function back_cb cdecl(self As Ihandle Ptr) As Integer
  Dim As Ihandle ptr  web  = cptr(IHandle ptr,IupGetAttribute(self, "MY_WEB"))
  IupSetAttribute(web, "BACKFORWARD", "-1")
  return IUP_DEFAULT
End Function

function forward_cb cdecl(self As Ihandle Ptr) As integer
  Dim As Ihandle ptr  web  = cptr(IHandle ptr,IupGetAttribute(self, "MY_WEB"))
  IupSetAttribute(web, "BACKFORWARD", "1")
  return IUP_DEFAULT
End Function

function stop_cb cdecl(self As Ihandle Ptr) As Integer
  Dim As Ihandle ptr web  = cptr(IHandle ptr,IupGetAttribute(self, "MY_WEB"))
  IupSetAttribute(web, "STOP", NULL)
  return IUP_DEFAULT
End Function

function reload_cb cdecl(self As Ihandle Ptr) As integer
  Dim As Ihandle Ptr web  = cptr(IHandle ptr,IupGetAttribute(self, "MY_WEB"))
  IupSetAttribute(web, "RELOAD", NULL)
  return IUP_DEFAULT
End Function

function load_cb cdecl(self As Ihandle Ptr) As Integer
  Dim As Ihandle Ptr txt  = cptr(IHandle ptr,IupGetAttribute(self, "MY_TEXT"))
  Dim As Ihandle Ptr web  = cptr(IHandle ptr,IupGetAttribute(self, "MY_WEB"))
  IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"))
  return IUP_DEFAULT
End Function

sub WebBrowserTest()
  ' Creates an instance of the WebBrowser control
  dim as Ihandle ptr web = IupWebBrowser()
  ' Creates a dialog containing the control
  dim as Ihandle ptr btBack    = IupButton("Back", NULL)
  dim as Ihandle ptr btForward = IupButton("Forward", NULL)
  dim as Ihandle ptr txt       = IupText("")
  dim as Ihandle ptr btLoad    = IupButton("Load", NULL)
  dim as Ihandle ptr btReload  = IupButton("Reload", NULL)
  dim as Ihandle ptr btStop    = IupButton("Stop", NULL)
#ifndef __FB_WIN32__
  dim as Ihandle ptr history   = IupButton("History", NULL)
  IupSetCallback(history,  "ACTION", cast(Icallback,@history_cb))
#else
  dim as Ihandle ptr history   = NULL
#endif   
  dim as Ihandle ptr dlg = IupDialog(IupVbox(IupHbox(btBack, btForward, txt, _
                                             btLoad, btReload, btStop, _
                                             history, NULL), _
                                     web, NULL))

  IupSetAttribute(dlg, "TITLE", "IupWebBrowser")
  IupSetAttribute(dlg, "MY_TEXT", cptr(zstring ptr,txt))
  IupSetAttribute(dlg, "MY_WEB", cptr(zstring ptr,web))
  IupSetAttribute(dlg, "RASTERSIZE", "800x600")
  IupSetAttribute(dlg, "MARGIN", "10x10")
  IupSetAttribute(dlg, "GAP", "10")

  IupSetAttribute(txt, "VALUE", "http://www.tecgraf.puc-rio.br/iup")
  IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"))
  IupSetAttributeHandle(dlg, "DEFAULTENTER", btLoad)

  IupSetAttribute(txt, "EXPAND", "HORIZONTAL")
  IupSetCallback(btLoad,   "ACTION", cast(Icallback,@load_cb))
  IupSetCallback(btReload, "ACTION", cast(Icallback,@reload_cb))
  IupSetCallback(btBack,   "ACTION", cast(Icallback,@back_cb))
  IupSetCallback(btForward,"ACTION", cast(Icallback,@forward_cb))
  IupSetCallback(btStop,   "ACTION", cast(Icallback,@stop_cb))


  IupSetCallback(web, "NEWWINDOW_CB", cast(Icallback,@newwindow_cb))
  IupSetCallback(web, "NAVIGATE_CB",  cast(Icallback,@navigate_cb))
  IupSetCallback(web, "ERROR_CB",     cast(Icallback,@error_cb))
  IupSetCallback(web, "COMPLETED_CB", cast(Icallback,@completed_cb))

  ' Shows dialog
  IupShow(dlg)
End Sub


'
' main
'
WebBrowserTest()
IupMainLoop()
