Browsing in Tk: Enticing Firefox into Tk and Tkhtml3
The next two afternoon talks (Steve Landers and then Dan Kennedy, both very talented programmers from down under) both deal with the marriage of web browsing and Tcl/Tk. I'll report on both of them together.
Entice: Embedding Firefox in Tk
Steve Landers: "I was going to write a language called S but that was already taken. Now L is too."
"Tcl meets Ajax. 100% buzzword compliant."
Embedding browsers in Tk
- IE via OpTcl or Tcom on Windows
- New genre of applications (e.g., NewzPoint)
- Handy if you want to format + print tables in Tk
- What about *nix?
Possible approaches
- Browser library wrapped as a Tcl extension (TkGecko). Pro's: complete, controllable from Tcl. Cons: can't be self-contained. Huge footprint. Difficult to build Gecko.
- Tk-based browser (TkHTML). Pro's: 80/20 approach, can be self-contained, easy to control from Tcl. Con's: some situations need the last 20% (that percentage decreasing over time as work on TkHTML3 progresses).
- Re-parent browser window into Tk container. Pro's fully featured browser. Cons: not so easy to control from Tcl. Applications aren't self contained.
Entice is built on top of [frame -container], and GPS's TkXext (search for X11 window ID by window title, get a handle, re-parent the window by ID).
Firefox talks back to the entice library on a server socket listening from Tcl. Various security features. Tell firefox to connect back to localhost on Entice's port, and get a custom chrome (.xul). Entice substitutes the listening port into the .xul and sends to Firefox.
Controlling Firefox via XMLHttpRequest. Javascript object running in the browser that waits asyncronously for a connection. Basically a GET operation that is asyncronous. It is at the heart of AJAX.
- entice.xul: 90 lines of javascript
- entice.tcl: 250 lines of Tcl
Thanks to GPS for making it possible (TkXext) and Mike Doyle for sponsoring it.
Tkhtml3
The problem with existing HTML widgets is simple: "Most documents found on the WWW are rendered incorrectly." Lightweight web browsers generally have the same problems (often because CSS is not supported well—if at all).
Although not always possible, Tkhtml3 tries to focus on CSS. Delegates most HTML functionality to the application. More of a CSS renderer than an HTML renderer.
Anticipated uses
- Hyptertext label widget
- Geometry manager (for example form layout very natural)
- Internal document viewer
- web browser component
HTML can be specified incrementally. .css (zero to many) must be fully prepared.
The document tree: constructed by parsing step, creates a tree. Not DOM compatible (but provides simillar levels of functionality). For the time being only read access to the tree is provided.
Document tree can be queried: for root node, for all nodes that match a specific selector, for whats under a given mouse coordinate.
Tkhtml does not implement hyperlinks(!). Applications are expected to:
- Use [bind] to detect mouse click
- Query widget to determine the node or nodes that were clicked on
- Figure out if a hyperlink node has been clicked on
- Do something about it if so
This approach makes for maximum flexibility.
Element handlers: parse handlers, script handlers, and node handlers.
To support images the application implements the -imagecmd callback. The callback is given a URI and is expected to return a Tk image.
Tkhtml3 uses images as backgrounds, list markers, and as replaced content.
Replacement objects. Any node in the tree can be mapped to display in another widget in place of a node's content.
Applications can specify regions of text to be tagged.
Hv3: A web browser built using Tkhtml3
Leverages existing Tcl/Tk extensions. [img] allows it to support more image types than any other web browser(!). The tls package for SSL, etc.
Project status
- Still alpha
- Rendering correctness is not a barrier to moving to beta stage
- Hv3 proves that a modern "scriptless" web browser is already viable
- Barriers: DOM support (Tkhtml3 will not supply a DOM API but enough functionality that it can be built on top of it) and problems with managed windows (issue with CSS specified replacement content being baseline aligned, CSS allows boxes to be drawn on top of replaced content, etc.)
Available at tkhtml.tcl.tk. The shared library on Linux is ~220kb.
—Michael A. Cleverly
Wednesday, October 11, 2006 at 16:29
Being able to embed something like XUL into Tcl/Tk would be great! It's hard to tell from this web page whether this is possible. (If it is, please provide some details.)
Thanks!!
Thu, 13 Aug 2009, 20:41