Hack87.Build GUI Interfaces with GTk


Hack 87. 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.

9.3.1. The Code

Save the code in Example 9-6 as retest.php.

Example 9-6. A GTk regular expression tester
 <?php 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 ) ); $i++; } } $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.

9.3.2. 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 http://gtk.php.net/, 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 9-4.

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 9-5.

Figure 9-4. The starting point of the interface


Figure 9-5. 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.

9.3.3. See Also

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



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net