PHP: Main - A collection of PHP tutorials!
PHP-GTK: The basics
You'll need a basic understanding of php at the least, and some knowledge of classes and objects might be a good idea too.
// Foreword //
PHP-GTK is the PHP team's efforts to to package GTK functionality into PHP. GTK is a libary for creation of applications with Graphical User Interfaces (GUIs). The project is still under development, and PHP-GTK2 is still in alpha release.(As of time of writing.) This tutorial is about the basics of the PHP-GTK2 system. Go here for a reference of objects and stuff.
// Preparation //
You will need to download PHP-GTK2 from gtk.php.net, and a release of PHP that works with it from php.net. As of this time of writing, PHP 5.1 is in GA (Generally Available) release, so if you want php 5.2 head over to snaps.php.net. Stick the php release somewhere, and the php-gtk2 somewhere too. Now place the php.exe (and php-win.exe) in your php-gtk dir, along with php5ts.dll (for windows; i'm not sure what it is on linux). Next, if on windows, add (path to phpgtk2)\gtk+2.6.9 to your PATH. (control panel > system > advanced > environment variables > path > edit > add ";(path to phpgtk2)\gtk+2.6.9;" to the end > OK).
// And begin! //
// App 1 - Hello World! //
We're going to make a nice Hello World app =)
create hello.php (or whatever) in your phpgtk dir. open ye php brackets, and place this snippet.
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');
this simply checks for the existence of gtk.
next, we start our gtk stuff.
$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk', 'main_quit'));
$window->set_title('Hello World!');
the connect_simple makes the destroying of the window quit the app.
$helloLabel = new GtkLabel('Just popping up to say "Hello World!"');
$window->add($helloLabel);
this code creates a GtkLabel object, which is basically just some text, and adds the label to the window.
$window->show_all();
this simply makes everything show up..
Gtk::main();
this runs the gtk loop and makes the magic happen :D
Complete program:
<?php
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');
$window = new GtkWindow();
$window->set_title('Hello World!');
$helloLabel = new GtkLabel('Just popping up to say "Hello World!"');
$window->add($helloLabel);
$window->show_all();
Gtk::main();
?>
// App 2 - ROT13 thingy //
Here, we're going to make a small app that lets the user enter whatever into a textbox, and hit a button to rot13 it.
start with the gtk checker, and init` your window and give it a title. don't forget the connect_simple either!
next, we create the objects we need.
$textBox = new GtkEntry();
$button = new GtkButton('_Rot13');
$button->connect_simple('clicked','rot13te
xt',$textbox);
$vBox = new GtkVBox();
GtkEntry is basically a textbox. A GtkButton is what you think it is. The underscore (_) before the R doesn't actually appear in the button, instead it makes the R underlined in the button, and gives the button a hotkey of ALT-R. The connect_simple makes the clicking of the button trigger a function called rot13text (which we shall define) with the parameter $textbox.
a GtkVBox is a simple container widget that will contain our entry field and button - a GtkWindow is a bin not a container, and can only hold one widget on it's own.
$vBox->pack_start($textbox);
$vBox->pack_start($button);
this places our textbox into the vBox, followed by our button..
$window->add($vBox);
$window->show_all();
Gtk::main();
now we also need to define our function.
function rot13text($textbox)
{
$textbox->set_text(str_rot13($textbox->get
_text()));
}
a complete no-brainer =)
Complete program:
<?php
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');
function rot13text($textbox)
{
$textbox->set_text(str_rot13($textbox->get
_text()));
}
$window = new GtkWindow();
$window->connect_simple('destroy', array('gtk', 'main_quit'));
$window->set_title('Rot13');
$textbox = new GtkEntry();
$button = new GtkButton('_Rot13');
$button->connect_simple('clicked','rot13te
xt',$textbox);
$vBox = new GtkVBox();
$vBox->pack_start($textbox);
$vBox->pack_start($button);
$window->add($vBox);
$window->show_all();
Gtk::main();
?>
// App 2a - ROT13 Thingy with dialog box //
The previous app replaced the text in the textbox. We'll improve it by instead popping up a dialog box.
in the line where we connect clicking $button, add another parameter after $textbox: $window.
now go to our rot13text() functionL replace it with this.
$dialog = new GtkMessageDialog($window, Gtk::DIALOG_MODAL,Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK);
$dialog->set_markup(str_rot13($textbox->ge
t_text()));
$dialog->run();
$dialog->destroy();
GtkMessageDialog is just a convienience widget that wraps GtkDialog. Gtk:MESSAGE_INFO tells it that it's an informational dialog.
the rest, you can work out :P
Complete program:
<?php
if (!class_exists('gtk'))
die('Please load the php-gtk2 module in your php.ini.');
function rot13text($window,$textbox)
{
$dialog = new GtkMessageDialog($window, Gtk::DIALOG_MODAL,Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK);
$dialog->set_markup(str_rot13($textbox->ge
t_text()));
$dialog->run();
$dialog->destroy();
}
$window = new GtkWindow();
$window->connect_simple('destroy', array('gtk', 'main_quit'));
$window->set_title('Rot13');
$textbox = new GtkEntry();
$button = new GtkButton('_Rot13');
$button->connect_simple('clicked','rot13te
xt',$window,$textbox);
$vBox = new GtkVBox();
$vBox->pack_start($textbox);
$vBox->pack_start($button);
$window->add($vBox);
$window->show_all();
Gtk::main();
?>
That's all folks! :D
feedback appreciated.