Menu
|
PHP GTK Tutorial #3
In the previous tutorial, I left you with a problem to
solve, namely how to get a button and a text box into a
single window. Alright, I cheated a bit as you've
probably found it isn't possible wihout adding another
bit of code. Position of widgets within a window is
controlled by providing a table of cells to contain the
widgets not unlike layout managers in Java if you are
familiar with the Java language. These can either be
stacked upon each other or side by side. First to display
widgets (in this text boxes) side by side:-
<>#! /usr/bin/php -q
<?php
# display text boxes in a horizontal container
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);
$text2 = &new GtkText();
$text2->insert_text("Text2",0);
$hbox = &new GtkHBox(false, 5);
$window->add($hbox);
$hbox->pack_start($text1, true, true, 1);
$text1->show();
$hbox->pack_start($text2, true, true, 1);
$text2->show();
$window->show_all();
Gtk::main();
?>
Most of this should be familiar to you. The new bits are:-
$hbox = &new GtkHBox(false, 5);
$window->add($hbox);
$hbox->pack_start($text1, true, true, 1);
$text1->show();
$hbox->pack_start($text2, true, true, 1);
$text2->show();
These work like this:-
$hbox = &new GtkHBox(false, 5);
This creates a new variable that contains a reference
to a new horizontal box. The parameter false if
set to true would set the size of all the widgets to the
size of the largest one. The other parameter sets a
spacing between the various widgets. The box is then
added to the window. Now to populate the box container.
$hbox->pack_start($text1, true, true, 1);
This will pack the container with widgets starting at
the top corner. The first parameter specifies the widget
to be packed. The second parameter specifies whether the
widget will be allowed to resize if the main window is
resized. The third parameter doesn't really concern us
and is always true. Finally, the number of pixels to
srround the widget is sepcified which in this case is 1.
The textbox is then displayed and the same is commands
are applied to the second text box. This then displays
two text boxes side by side. Now we move on to stacking
the widgets vertically which will look something like
this-

This is achieved using the following code which also
has some added functions:-
#! /usr/bin/php -q
<?php
# Display text boxes vertically
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($text1)
{
$text1->insert_text('Hello world!',0);
}
$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->set_title("Test App");
$window->set_usize(300, 200);
$window->set_border_width(10);
$window->connect('destroy', 'destroy');
$text1 = &new GtkText();
$text1->set_editable(true);
$text1->set_word_wrap(true);
$button = &new GtkButton('Hello World!');
$button->connect_object('clicked', 'btnClicked', $text1);
$vbox = &new GtkVBox(false, 5);
$window->add($vbox);
$vbox->pack_start($text1, true, true, 1);
$text1->show();
$vbox->pack_start($button, true, true, 1);
$button->show();
$window->show_all();
Gtk::main();
?>
Just take a moment to see if you can understand what
is going on before reading any further.
Right, done that? Need some help? OK, the window is
setup in the usual way as is the text box and a button
object is created. Now comes a slightly strange bit:-
$button->connect_object('clicked', 'btnClicked', $text1);
This is an slightly different version of the connect
command we used earlier which enables us to pass a
parameter to the event function btnClicked we have
written to respond to the user clicking on the button.
The third parameter $text1 is a reference to the
text box that is passed to the btnClicked that is
above this main code which looks like this:-
function btnClicked($text1)
{
$text1->insert_text('Hello world!',0);
}
Now, we have passed the reference to the text box that
can now be used within this function. This then inserts
the specified text (just had to squeeze a Hello World!
program in here somewhere!) into the text box (note the
zero). The packing of the VBox works in the same way as
the previous example except that widgets are stacked on
on top of the other.
And that's it for another tutorial.
|