Search
Syndication
Sponsors

Archive for the ‘PHP GTK’ Category

PHP-GTK : Hello World (basic)

Wednesday, February 4th, 2009

Hello World (basic)

Table of Contents

Further reading
When you begin to learn a programming language, the first program you often write is a ‘hello world’ program. So, just to fit in with everyone else, the first tutorial in this manual just happens to be a ‘hello world’ tutorial!

Throughout the tutorials we expect a reasonable grasp of PHP itself. The tutorials are designed to give the user an idea of how to use PHP-GTK, and the ideas and techniques behind it.

In this tutorial we will create a simple window with the text “Hello World!” in it.

We will start by listing the program and will then explain each line of the program, giving an overview of a very basic PHP-GTK application.

Example 2.1. A simple Hello World script

<?php
if (!class_exists(’gtk’)) {
    die(”Please load the php-gtk2 module in your php.ini\r\n”);
}

$wnd = new GtkWindow();
$wnd->set_title(’Hello world’);
$wnd->connect_simple(’destroy’, array(’gtk’, ‘main_quit’));

$lblHello = new GtkLabel(”Just wanted to say\r\n’Hello world!’”);
$wnd->add($lblHello);

$wnd->show_all();
Gtk::main();
?>
If you get an error Fatal error: Call to undefined function: connect_simple(), then you are using PHP-Gtk1 and not PHP-Gtk2. This is the wrong manual then.

Now copy the code into a text editor and save it as hello.phpw. Then open a console and start it via php hello.phpw. A window with title “Hello world” should open and contain nothing but the text “Just wanted to say ‘Hello world!’”.

Example 2.2. Checking if PHP-GTK is available

if (!class_exists(’gtk’)) {
    die(”Please load the php-gtk2 module in your php.ini\r\n”);
}
Here we make sure that PHP-GTK is available by checking if the class gtk exists. Unlike PHP 4 and PHP-GTK 1, you should not try to load the PHP-GTK module via dl(). This has been deprecated in PHP 5, and should not be used in newly written code. So all we can do is print out a message, that the module is not available and should be enabled by the user.

Example 2.3. Create a window

$wnd = new GtkWindow();
Creating a new window widget is as easy as instantiating a new object: Variable $wnd gets the new object assigned. If you already programmed with PHP 4 and PHP-GTK 1, you will miss the ampersand & before new. That was required in PHP 4, but may not be used any more in PHP 5, as references are created automatically.

Example 2.4. Set the window title

$wnd->set_title(’Hello world’);
To make it easier to identify the window in the task bar, we set the title: Just a normal call to an object method.

Example 2.5. Enable a clean shutdown

$wnd->connect_simple(’destroy’, array(’gtk’, ‘main_quit’));
That is the first interesting piece of code: The “destroy” signal gets connected to the static Gtk::main_quit method. It basically tells GTK to quit the main loop when the window gets closed/destroyed. (The main loop is explained some paragraphs below.)

Example 2.6. Create a label to display text

$lblHello = new GtkLabel(”Just wanted to say\r\n’Hello world!’”);
As done before, we create a new widget. This time we want to display a small amount of text, and GtkLabel is perfect at this task.

Example 2.7. Add the label to the window

$wnd->add($lblHello);
Now we tell the window, that the label shall be added directly on it.

Example 2.8. Make the window visible

$wnd->show_all();
Until now, you will see nothing on your screen. To make the window visible, you will need to call the show() . That would make the window visible, but not the label - we would have to call show() on the label, too. That is inconvenient if you have multiple widgets like buttons, checkboxes and other on the window: show_all() takes care of making the window and all child widgets visible.

Example 2.9. Start the main loop

Gtk::main();
After everything is set up, we need to make sure that the window stays open and responds to user interactions. A normal PHP script would end and stop at this point, but we want to keep it running: For that, the GTK main loop needs to be started. It takes care of keeping the application running and waiting for user events. If events occur, it delegates them to the corresponding callbacks.

Further reading

Now that you have created your first PHP-GTK 2 application, you probably want to create more complex layouts. To lean the basics, have a look at the Packing tutorial.

GTK-PHP : Installation on Linux

Wednesday, February 4th, 2009

Installation on Linux

The best way to install PHP-GTK 2 on Linux is to compile it. No binary or source packages are available as yet for any distribution, so you can’t use your favorite package manager to install PHP-GTK 2! These instructions should be valid for most distributions of Linux, as well as other POSIX compliant systems.

The methods listed here reportedly don’t work on FreeBSD

There are two ways to go about installing PHP-GTK 2 on your Linux system. You can either choose to use your existing PHP installation, or decide to install a separate one and dedicate it to PHP-GTK 2. The latter is a simpler method and is recommended for users with little or no Linux experience. In any case, you need Gtk+ version 2.6 upwards installed.

Installing PHP-GTK 2 along with a new PHP installation

Just follow these steps, and you should be okay! First thing you need is a fresh checkout of PHP. You can choose either the 5_1 or 5_2 branches, both work fine:

$ cvs -d :pserver:cvsread@cvs.php.net:/repository login
$ cvs -d :pserver:cvsread@cvs.php.net:/repository co -r PHP_5_2 php-src

Now, you need to compile and install it:

$ cd php-src
$ ./buildconf
$ ./configure --prefix=/opt/php5_2 --disable-cgi
$ make
$ su
$ make install
$ echo extension=php_gtk2.so >> /opt/php5_2/lib/php.ini
$ /opt/php5_2/bin/php-config --extension-dir | xargs echo 'extension_dir=' >> /opt/php5_2/lib/php.ini

If the current CVS of PHP does not compile, try an official release from the php downloads page, or a snapshot.

Now, let’s checkout and install PHP-GTK 2:

$ cd ..
$ cvs -d :pserver:cvsread@cvs.php.net:/repository co php-gtk
$ cd php-gtk
$ ./buildconf --with-phpize=/opt/php5_2/bin/phpize
$ ./configure --with-php-config=/opt/php5_2/bin/php-config
$ make
$ make install

You might want to create a link to the new php executable:

$ ln -s /opt/php5_2/bin/php /usr/bin/php-gtk

That’s it, you’re done!

Using an existing PHP installation

If you decide to ‘add-on’ PHP-GTK 2 to your existing PHP installation, ensure that your version of PHP is at least 5.1 with CLI enabled. PHP-GTK 2 will NOT work with any other versions of PHP. You can check your PHP version by typing php -v on the command line. Don’t forget to check if the word cli appears in the output!

Now, checkout the latest CVS source of PHP-GTK 2, or download the Alpha source tarball, as per instructions on the PHP-GTK 2 download page. cd into the checked out / extracted directory, and execute the following:

$ ./buildconf
$ ./configure
$ make
$ make install

This should just work for most people. However, if there are multiple installations of PHP in your machine or you have installed PHP in some exotic location, you might face trouble. Most of these cases can be solved by specifying the exact locations of thephpize and php-config files:

$ ./buildconf --with-phpize=/path/to/phpize
$ ./configure --with-php-config=/path/to/php-config

Testing your installation

Before you can use PHP-GTK 2, you need to enable the extension in your php.ini. Just add a

extension=php_gtk2.so

below all the other extension= settings.

You can test whether your installation was successful by executing:

$ php -m | grep php-gtk

You should get a single line that reads php-gtk.

Next, you can try out the demo applications in the demos directory of the php-gtk sources, e.g. demos/phpgtk2-demo.php.

PHP-GTK : Installation on Windows

Wednesday, February 4th, 2009

Installation on Windows

Using Gnope

Gnope has proven to be the most easiest way of installing and maintaining PHP-GTK 2 on Windows. Gnope is a fully featured PHP-GTK 2 installer, complete with PHP 5.1, GTK 2.6 and PEAR. A few simple mouse clicks and you’re done!

Just download the installer from gnope.org and follow the on-screen instructions. Gnope also has a PEARified channel of its own, where you can download PHP-GTK 2 applications, in addition to official PEAR packages, e.g. from the Gtk2 category.

Manual Installation

Download the Alpha binary release of PHP-GTK 2 for Windows from the PHP-GTK 2 download page. Unzip the file into a directory of your choice. A directory named php-gtk will be created containing everything included in the release.

You will need a copy of php.exe (CLI Version) and php5ts.dll, both of which are available in the binary release of PHP 5 for windows, and put them in the php-gtk directory. If you wish to use any other extensions with this copy of php.exe, make sure you put their binaries here too.

After this, you must set your PATH variable to include the gtk+2.6.9 directory present in the release. You can also let the gtkpath.bat batch file set the appropriate PATH for the current command line session by executing it.

Compiling from CVS

This is the least recommended method of installing PHP-GTK 2 on Windows. If you still want to go ahead with this, please read win32/README.win32.txt in the CVS checkout.