Archive for the ‘php’ Category

PHP GTK 2

Last time I said I would be writing a Java application. My idea was take some of these php scripts and group them into something that is easier to use for beginners that runs on the desktop.

However… I got to thinking. I said PHP is a powerful language, and is the only language you really need to write some truly powerful applications. So I’m going to write a PHP application that runs not from a web server or the command line but starts up a proper window.

OK, so lets get to this. You need:

Glade (to design the window)
GTK+ windows runtimes (unless you run Linux)
PHP 5 with php_gtk2 extension
My PHP GTK scripts (extract them into the same directory as the php 5 install and then double click start.bat)

You got all those? OK fire up glade and create a window that is to your liking. Like mine below. It will save as something.glade which will be some XML for creating the window.

PHP GTK2 App

Now this is the PHP to create a window… I call it glade_win.php

<?php

// load php gtk2 in
if (!extension_loaded(’php_gtk’)) {
if (strtoupper(substr(PHP_OS, 0, 3)) === ‘WIN’) {
dl(’php_gtk2.dll’);
} else {
dl(’php_gtk2.so’);
}
}

// load the window from our glade .xml file
$glade =& new GladeXML( dirname( __FILE__) . “/wndMain.glade”);

// make sure the window closes when we click close
$glade->get_widget(’wndMain’)->connect_simple(’destroy’, array(’Gtk’, ‘main_quit’));

// execute the gtk main function
gtk::main();

?>

This couldn’t be any more simple. The first bit is necessary to load the DLL in because when it runs on my Linux install it destroys my web server if it has php_gtk2 turned on by default.

The next part creates an object that loads in the XML. We then connect the close button i.e. the cross at the top right hand corner to a quit routine, and then gtk::main(); runs the necessary code to view the window.

My window XML is in my scripts zip. It should give an idea of where I am going with this :D . Hopefully that all worked smoothly for you.

Wednesday, April 23rd, 2008