PHP-GTK Tutorial: Creating menus in PHP-GTK using GTK+

This article is brought to you by Ben Stones, guest editor for DPS Computing Ltd.

What is PHP-GTK?

First and foremost, what is GTK+? GTK (short for GIMP Tool Kit) is an extensive library of methods for creating cross-platform graphical user interfaces using the GTK+ library with a language of your choice. There are extensive language bindings to GTK+ – from Python, to C++, C# and much more – including PHP-GTK.

PHP-GTK is not as widely used as one would hope, but there is a community of PHP-GTK developers and the #php-gtk channel in Freenode is a good place to get help on matters concerning PHP-GTK – although sometimes you may be waiting a short while for a response – but the channel is active daily.

So with the ease and use and flexibility of the PHP programming language – one would wish it could be used to create desktop applications. And of course, with PHP-GTK, this is possible using the GTK+ library. Installing PHP-GTK is not as easy as it could be; although for Windows users, you’re in luck – there are pre-compiled binaries for you to download and use without requiring any complicated installation procedures. However, for installing on Linux distributions, it is fairly easy. If you Google “How to install PHP-GTK on Ubuntu” you’ll find a post on another forum with instructions on how to install PHP-GTK on Linux distributions, including Ubuntu.

How do I create menus in PHP-GTK?

Creating menus are not as simple as you would first imagine. But once you understand how the methods work, it will make sense. Essentially, the GtkMenuItem is not just for menu items within a menu option. They are also used to create a top-level menu item, too. Let’s take a look at some sample PHP-GTK code.

// add menu bar
$this->addMenu();

public function addMenu()
{
/* create the menu */
$file = new GtkMenuItem(“File”);
$help = new GtkMenuItem(“Help”);

// create the menubar
$menubar = new GtkMenuBar();
$menubar->append($file);
$menubar->append($help);

// append the menu options
$menu = new GtkMenu();

$menu2 = new GtkMenu();

$open = new GtkMenuItem(“Open”);
$this->menuSaveItem = new GtkMenuItem(“Save As…”);
$quit = new GtkMenuItem(“Quit”);

$about = new GtkMenuItem(“About”);

// connect the signals

$quit->connect_simple(‘activate’,array($this,’menuOptionClicked’),”quit”);
$open->connect_simple(‘activate’,array($this,’menuOptionClicked’),”open”);

$menu->append($open);
$menu->append($this->menuSaveItem);
$menu->append($quit);

$menu2->append($about);

$file->set_submenu($menu);
$help->set_submenu($menu2);

$help->set_submenu($menu2);

$this->init_vbox->pack_start($menubar,FALSE,FALSE);
}

Original publication – August 2012.

Disclaimer: DPS Computing Ltd., its employees and the author of this article disclaims all liability with respect to the accuracy, reliability and security of any application code and routines that are provided in this article, as well as in respect to the accuracy and reliability of the article itself. We cannot guarantee that the application code in this article will work as expected or not cause any damage to your operating system, any software or not cause loss of, or damage to, data. Your use of the application code and routines provided in this article is at your sole risk and responsibility.
Ben Stones: