Build GUI Interfaces with GTk

Build GUI Interfaces with GTk

Use GTk to build cross-platform GUIs for your PHP code.

Why limit yourself to web markup, or even tag-based devices? Why not take over the desktop itself with your PHP code? With the GTk toolkit you can do that, and it's really easy. This hack shows how to use GTk to develop a simple regular-expression test application.

The Code

Save the code in Figure as retest.php.

A GTk regular expression tester
if( !extension_loaded('gtk')) {
	dl( 'php_gtk.'.PHP_SHLIB_SUFFIX);

$start_regex = "/name:\\s*(.*?)\\n/";
$start_text = "name: Jack\nname:Lori\nname:Megan\n";

function delete_event( ) { return false; }

function shutdown( ) { gtk::main_quit( ); }

function run( )
	global $rb_regex, $tb_text, $ft;

	$regex = $rb_regex->get_chars(0, -1);
	$text = $tb_text->get_chars(0, -1);

	preg_match_all( $regex, $text, $found );

	$ft->clear( );
	$i = 0;
	foreach( $found[1] as $f )
		$ft->insert( $i, array( $f ) );

$window = &new GtkWindow( );
$window->set_usize( 700, 400 );
$window->set_title( "Regular Expression Tester" );
$window->connect('destroy', 'shutdown');
$window->connect('delete-event', 'delete_event');

$bb = new GtkTable( );

$rb = new GtkTable( );

$rb_label = new GtkLabel( "Regex:" );
$rb->attach( $rb_label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 5, 5 );
$rb_regex = new GtkEntry( );
$rb_regex->insert_text( $start_regex, 0 );
$rb->attach( $rb_regex, 1, 2, 0, 1, GTK_FILL, GTK_SHRINK, 5, 5 );
$rb_run = new GtkButton( "Run" );
$rb_run->connect('clicked', 'run');
$rb->attach( $rb_run, 2, 3, 0, 1, GTK_SHRINK, GTK_SHRINK, 5, 5 );

$tb_label = new GtkLabel( "Text:" );
$rb->attach( $tb_label, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 5, 5 );

$tb_text = new GtkText( );
$tb_text->set_editable( true );
$tb_text->insert_text( $start_text, 0 );
$rb->attach( $tb_text, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 5, 5 );

$bb->attach( $rb,0,1,0,1,GTK_SHRINK,GTK_FILL,5,5 );

$ft = new GtkCList( 1 );

$bb->attach( $ft,1,2,0,1 );

$window->add( $bb );

$window->show_all( );

gtk::main( );

Most of the code in this hack sets up the GUI interface. That starts with the creation of a new GtkWindow. After that, the controls for the window are created and attached to the original window. The regular expression code is run when the user clicks the Run button; that button is connected to the run( ) function through the connect( ) method on the $rb_run object.

The run( ) function takes the current contents of the $rb_regex variable, as well as the text fields and runs the regular expression. It then clears out the contents of the table and adds all of the regular expression matches into the table using the insert( ) method.

Running the Hack

You run this hack on the command line using a special version of the PHP 4 interpreter that you can download from, which includes support for GTk:

	c:\retest\ > php retest.php

Even though you still run php, you need the GTk version of the interpreter. Otherwise, you're going to get some nasty errors when you try to run retest.php.

This launches the window shown in Figure.

Next, click on the Run button to run the regular expression in the top text box against the text string in the lower, larger text box. The result is shown in Figure.

The starting point of the interface

After clicking the Run button

This shows that three strings were found: Jack, Lori, and Megan. You can alter the regular expression and the text however you like. This is actually a nifty tool, and it can come in handy when you are just learning the weird world that is regular expressions, perhaps when you're working on your URL rewriting rules [Hack #60].

An in-depth explanation of how GTk functions is beyond the scope of this hack. Suffice it to say that all of the standard Windows controls are there, as well as an option for custom drawing. Even better, the toolkit runs on Windows, Mac, and Linux, giving your PHP script far more portability than other languages provide.

That being said, GTk is not perfect. In general, you have less formatting control than you do when coding directly to the native Windows API with C or C++. But for applications such as this one, or a handy widget that (for example) checks your server status, pixel-perfect positioning is probably not required.

See Also

  • "Read XML on the Cheap with Regular Expressions" [Hack #38]

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows