Wouldn’t it be nice if building PHP-GTK applications was as easy as writing DHTML? This proof of concept might be an eye opener... (as well as a useful XML parsing example).
Building Apps with XUL and Moz..err PHP-GTK
Last week I was looking at Rendering XUL Apps that run under Mozilla, with PHP. Although XUL is great, the obvious concern is the visitor would need a Gecko based browser to view the result.
So how about getting even more marginalized and making it possible to use XUL to build PHP-GTK apps? Take an XUL document like;
<?xml version="1.0"?>
<window
title="XUL to PHP-GTK"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<!-- Note full path to script for inclusion -->
<script type="application/x-php"
src="c:htdocsxul2gtkfunctions.php" />
<vbox>
<!-- Note id's must be valid PHP variable names! -->
<textbox
id="messageBox"
value="Messages..."
readonly="false"/>
<hbox>
<button
label="Hello World!"
oncommand="hello" />
<button
label="Goodbye World!"
oncommand="goodbye" />
</hbox>
</vbox>
</window>
And turn it into a PHP application instantly;
The inspiration here comes from Gerald Bauer’s LUXOR project, parses XUL documents and generates Java Swing apps which can be launched straight from a website using JNLP and Java Web Start.
PHP not JavaScript
What’s nice about doing this is you can use PHP instead of JavaScript for the presentation logic. This file;
<script type="application/x-php"
src="c:htdocsxul2gtkfunctions.php" />
Looks like;
<?php
function hello() {
// Use the getElementById() function to fetch the widget
$messageBox=& getElementById('messageBox');
$length=$messageBox->get_length();
$messageBox->freeze();
$messageBox->insert_text("Hello World!n",$length);
$messageBox->thaw();
}
function goodbye() {
$messageBox=& getElementById('messageBox');
$length=$messageBox->get_length();
$messageBox->freeze();
$messageBox->insert_text("Goodbye World!n",$length);
$messageBox->thaw();
}
?>
These above two functions are specified as callbacks in the XUL document;
<button
label="Hello World!"
oncommand="hello" />
<button
label="Goodbye World!"
oncommand="goodbye" />
Notice also how the functions get access to the GTKText widgets;
function hello() {
// Use the getElementById() function to fetch the widget
$messageBox=& getElementById('messageBox');
The element id is specified in the XUL document as well;
<textbox
id="messageBox"
XUL compared to Glade
In some ways this is re-inventing a wheel which glade already does for PHP-GTK. But glade bears more in common with ANT than HTML e.g.
<widget>
<class>GtkWindow</class>
<name>window</name>
<title>Some App</title>
<type>GTK_WINDOW_TOPLEVEL</type>
<position>GTK_WIN_POS_CENTER</position>
<modal>False</modal>
<allow_shrink>False</allow_shrink>
<allow_grow>True</allow_grow>
<auto_shrink>False</auto_shrink>
<widget>
<class>GtkVBox</class>
<name>vbox1</name>
<homogeneous>False</homogeneous>
<spacing>0</spacing>
</widget>
</widget>
It pretty much needs the IDE while XUL is probably easier to hand code and has the advantage of CSS, in Mozilla, to specify look at feel (no I haven’t written a CSS parser).
Any takers?
Anyway - there’s more documentation (full API docs generated with PHPDocumentor ) and discussion provided in the download.
Hopefully the approach I’ve used which build on Luis Argerich’s excellent SAX Filter Classes is extensible enough to build a complete XUL to PHP-GTK library. Given XUL and GTK have a pretty close relationship, it’s generally just “join the dots”.
As far as CSS goes, if people are limited to CSS class attributes or getting elements by their id attribute, it shouldn’t be too hard to provide a little CSS based customization of the look and feel - colors at least.
But that’s it from me for a while. Within a week there’s a very high probability that I’ll be a father so it’s time to cut down on online time. Apologies to anyone who’s been trying to get hold of me by email recently - been slacking. If anyone is interested keeping this site moving in the mean time, please drop line - I’ll be on the look out (but slowly).
Till then.
