
|
|
|||
Desktop Application Development with PHP-GTKBy Akash Mehta2008-03-13
Your first PHP-GTK application Load up your favourite text editor and bash out the following: <?php Save the file as "helloworld.php", either in C:\Gnope\ (Windows) or
your home directory (Linux). (This is merely for consistency throughout
this tutorial; It doesn't actually matter where you save the file.).
Open up a command prompt / terminal (Start > Run > "cmd" on
Windows) and switch to the directory where you saved the file - "
It isn't terribly attractive, but it isn't too bad for six lines of code either. (If you get a fatal error, "call to undefined function connect_simple()", you're using PHP-GTK 1. Grab the binaries for / compile from source PHP-GTK 2 and try again.) Play around with the window a little bit. Try resizing it:
So, what's going on here? Let's take a look back at the code: $wndMain = new GtkWindow(); First, this section creates a new GTK window. The GTK toolkit is comprised of a variety of different controls, from text boxes to drop down menus and just basic text labels. A window itself is (in a way) one of these controls, and handling it somewhat generically gives us a lot of flexibility. GTK works with containers, inside of which you can place controls, and a window is a special type of container. Most items have some special events, and GTK calls those events "signals". Just like you can bind to events in Javascript, you can assign callbacks for signals. In this case, the "destroy" signal - called when the user clicks the close button on the window - links to the callback gtk::main_quit(), which will handle garbage collection and allow us to end gracefully. See this documentation page on signals for more details.
Now we get to start working with controls. We'll start with a label, a simple non-editable text field. Where in HTML you can output text anywhere and control it via CSS elsewhere, desktop applications are a little more strict. To work with controls in PHP-GTK, we use classes. When we create a new object of a control class, it's stored in memory but doesn't actually appear anywhere - because GTK doesn't know where we want it to appear. We then need to go back to our window and instruct PHP-GTK to add the label to the window. PHP-GTK takes care of the rest; when we resize the window, it simply centers the label within the container, no messy Javascript or unreliable CSS hacks. $wndMain->show_all(); Finally, we tell GTK to actually display the window - otherwise it
just sits in memory. Once we're done, however, we need GTK to take over
and just wait. Unlike a web-based script, which continues processing
and finishes, to maintain the state of a desktop application window and
wait for user input we need some kind of an infinite loop or
wait-for-event system. While I won't go into details of internals here,
suffice to say a call to Tutorial Pages: » What is PHP-GTK? » Getting started with PHP-GTK » Your first PHP-GTK application » So how powerful is this PHP-GTK? » Building a real application in PHP-GTK » Further reading |
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |