
-----------------------------------------------------------
--# GtkWebKit demo, views web pages (and runs Eu program)
-----------------------------------------------------------

include GtkEngine.e
include GtkEvents.e
include std/net/url.e
include GtkWebKit.plugin

constant docs = """

Adds a web browser 
to your EuGTK program!

This demo can display source code 
as well as running *.ex programs, 
just click on a link!
"""

object uri = 0
if networked() then
    uri = format("http://[]:8080",{get_net_address()})
else
    uri = "FILE:///"& canonical_path("~/demos/documentation/README.html")
end if

constant fmt = 
`<b>Default font</b> []
 <b>Monospace font</b> []
 <b>Serif font</b> []
 <b>Sans font</b> []
 <b>Fantasy font</b> []
 `
constant win = create(GtkWindow,"size=1000x800,position=1,border=10,background=grey90")
    connect(win,"destroy","Quit")

constant panel = create(GtkBox,VERTICAL)
    add(win,panel)

constant view = create(WebkitWebView)
    pack(panel,view,TRUE,TRUE)

constant settings = get(view,"settings")
    set(settings,{
        {"enable tabs to links",TRUE},
        {"zoom text only",TRUE},
        {"enable developer extras",TRUE},
        {"enable smooth scrolling",TRUE},
        {"enable_caret_browsing",TRUE},
        {"draw_compositing_indicators",TRUE},
    {"enable fullscreen",TRUE}, -- try these;
    {"enable_resizable_text_areas",TRUE},
    {"enable_spatial_navigation",TRUE},
    $})

constant box = create(GtkButtonBox),
    url = create(GtkEntry),
    btn1 = create(GtkButton,"gtk-about",_("About")),
    btn2 = create(GtkButton,"back#",_("Backward")),
    btn3 = create(GtkButton,"forward#",_("Forward")),
    btn4 = create(GtkButton,"gtk-quit","Quit"),
    slider1 = create(GtkScale,{
        {"orientation",HORIZONTAL},
        {"range",50,200},
        {"value",100},
        {"digits",0},
        {"tooltip text","Zoom Text"},
        {"connect","change value",_("ScaleView")}})
    pack(panel,slider1)
    add(box,{btn4,url,btn2,btn3,btn1})
    set(box,{
        {"layout",GTK_BUTTONBOX_END},
        {"child secondary",btn4,TRUE}})
    pack(panel,-box)
     
    set(url,"tooltip text","Enter URL here, hit enter.")
    set(url,"width chars",80)
    set(url,"text",uri)
    connect(url,"activate",_("LoadURI"))
    
show_all(win)

constant img2 = get(btn2,"image")
constant img3 = get(btn3,"image")

set(btn2,"tooltip markup","Go to previous page\n<small>(if available)</small>")
set(btn3,"tooltip markup","Go to next page\n<small>(if available)</small>")

connect(view,"load-changed",_("UpdateState"))
set(view,"load uri",uri)

main()

--------------------------
function About()
--------------------------
object p = floor((get(win,"size")/2) + get(win,"position")) -- keep near center
return Info(,,"Webkit Plugin",docs,,,,,,p[1],p[2])
end function

--------------------------
function Backward()
--------------------------
  set(view,"go back")
  UpdateState(view,0)
return 1
end function

-------------------------
function Forward()
-------------------------
  set(view,"go forward")
  UpdateState(view,0)
return 1
end function

------------------------
function LoadURI(atom x)
-------------------------
object uri = url:decode(get(x,"text"))
    set(view,"load uri",uri)
    UpdateState(view,WEBKIT_LOAD_FINISHED)
 return 1
end function

-----------------------------------------------------
function UpdateState(atom view, integer event)
-----------------------------------------------------
  set(img2,"visible",get(view,"can go back"))
  set(img3,"visible",get(view,"can go forward"))
  object uri
  if event = WEBKIT_LOAD_FINISHED then
    uri = get(view,"uri")
    set(win,"title",get(view,"title"))
    uri = locate_file(url:decode(uri))
    uri = uri[9..$] -- get rid of file:///
    set(url,"text",uri)
    if match("ex",fileext(uri)) then 
        system(sprintf("eui %s & ",{uri}),2) -- run in bkgnd;
    end if
  end if
return 1
end function

-----------------------------------
function ScaleView(atom ctl)
-----------------------------------
  set(view,"zoom level", get(ctl,"value") / 100)
return 0
end function


