PHP GTK Tutorial
 

Menu

PHP GTK Tutorial #2

The previous tutorial got as far as displaying a window using PHP and the GTK toolkit. Before getting on with populating the window with widgets, it might be nice to be able to control things like the size of the window, it's title, position and border. So, to set the size create a new file (perhaps called gtkSize.php) and add the following code:-

#! /usr/bin/php -q

<?php
# Display a window in a particular size
# can be expanded when run but cannot
# be made smaller than stated size

if (!class_exists('gtk'))
{
    if (strtoupper(substr(PHP_OS, 0,3) == 'WIN'))
        dl('php_gtk.dll');
      else
            dl('php_gtk.so');
}

if (!extension_loaded('gtk'))
{
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}

function destroy()
{
    Gtk::main_quit();
}

$window = &new GtkWindow();
$window->set_usize(300, 100);
$window->connect('destroy', 'destroy');
$window->show_all();

Gtk::main();
?>


The important bit is the:-

$window->set_usize(300, 100);

which will create a window 300 pixels wide and 100 high. The line:-

$window->show_all();

simply displays them all one everything has been setup. To change the title simply add:-

$window->set_title("Test App");

below the size command and to centre the window add:-

<>$window->set_position(GTK_WIN_POS_CENTER);

Finally, we can control the size of the border by specifying:-

$window->set_border_width(10);

Now we can get down to adding some widgets to make the window a little more useable. Many of the widgets you can use have entensive functionality that doesn't require intervention on the part of the programmer to make it work. One example is the text box which acts as a small text editor and can be created with only a single line of code as follows:-

#! /usr/bin/php -q

<?php
# display text box

if (!class_exists('gtk'))
{
    if (strtoupper(substr(PHP_OS, 0,3) == 'WIN'))
        dl('php_gtk.dll');
      else
            dl('php_gtk.so');
}

if (!extension_loaded('gtk'))
{
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}

function destroy()
{
    Gtk::main_quit();
}

$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->set_title("Test App");
$window->set_usize(300, 100);
$window->set_border_width(10);
$window->connect('destroy', 'destroy');

$text1 = &new GtkText();
$text1->insert_text("Text1",0);
$text1->set_editable(true);
$text1->set_word_wrap(true);
$window->add($text1);

$window->show_all();

Gtk::main();
?>


This will create a window that can be typed into as below:-

Text Box

The code to do this is in the block:-

$text1 = &new GtkText();
$text1->insert_text("Text1",0);
$text1->set_editable(true);
$text1->set_word_wrap(true);
$window->add($text1);

This first creates a new text box of type GtkText. Text can be inserted if required or left blank. The zero in the insert_text command allows for text of any length to be inserted. According to the manual, it should be -1 but this sometimes produces errors and zero seems to work best. We set it so that it can be edited and switch on word wrap. Finally, it is added to the window object. Most objects can be added and their properties set in this way. Note that if the window is maximised, the text object covers the entire window. We'll get around to how this can be changed in a later tutorial. One of the other essential widgets are butons. Here's how we do those:-

 #! /usr/bin/php -q

<?php
# Display a button

if (!class_exists('gtk'))
{
    if (strtoupper(substr(PHP_OS, 0,3) == 'WIN'))
        dl('php_gtk.dll');
      else
            dl('php_gtk.so');
}

if (!extension_loaded('gtk'))
{
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}

function destroy()
{
    Gtk::main_quit();
}

function btnClicked()
{
    print "Hello World!";
}

$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->set_title("Test App");
$window->set_usize(300, 100);
$window->connect('destroy', 'destroy');

$button = &new GtkButton('Hello World!');
$button->connect('clicked', 'btnClicked');
$window->add($button);

$window->show_all();

Gtk::main();
?>


As usual, create a new file to test it. The important parts are as follows:-

$button = &new GtkButton('Hello World!');
$button->connect('clicked', 'btnClicked');
$window->add($button);


This block of code creates a button and gives it a caption. The second line is similar to the function we wrote in the previous tutorial to destroy the window only this time it is capturing the event of the button being clicked by the user by using the connect keyword. Finally, it is added to the window object. Another useful option is the ability to display a tooltip (a small usually yellow window that pops up containing helpful text when the mouse is hovered over a widget). This can be added just below the button text as follows:-

#setup tool tip
$tt = &new GtkTooltips();
$tt->set_delay(200);
$tt->set_tip($button, 'Please click...', '');
$tt->enable();


This slightly different as this is a property of the button and so isn't added to it but merely set. So, we create a new tooltip and set a delay in milliseconds to determine how long the mouse must hover over a widget before the tooltip is displayed. The tooltip is then associated with the button widget and given some helpful text to display. The tooltip is then enabled.

That concludes this particular tutorial. You might like to try to add a button and a textbox to a slightly larger window and give them both tooltips. Have fun. Until the next time...

   
Please email: nickjc@nickjc.co.uk for help and advice

Back