The article assumes that you have successfully installed PHP-GTK v2.0 or v2.0.1, with the glade library properly set up. It is also recommended that you have Glade 3 installed, so that you can load the glade file and explore the widgets and their respective settings. And lastly, it is assumed that you already have some basic knowledge of PHP-GTK. This means that I will focus on how the various GTK widgets are being used, as opposed to explaining what they are. Having said that, if you're totally new to PHP-GTK and just want to have a feel of what PHP-GTK is, this application will serve as a good showcase. You will see what a typical PHP-GTK application looks like, what it can do, and how easy things can be done in PHP-GTK. Okay. Enough said. Let's get started right away! Step 1: Lay out the widgets with Glade The first step in developing a PHP-GTK application is to lay out the widgets using Glade. For the Notepad application, we start with a GtkWindow that contains a GtkVBox. In the GtkVBox, we first pack a GtkMenu, followed by a GtkTextView. To cater to long text, we also want to stuff the GtkTextView inside a GtkScrolledWindow so that a scroll bar is automatically displayed for long text. So the widget tree for our Notepad application is as follows:
I assume that you are familiar with Glade. So I will stop the Glade portion for now. Please download the .glade file available at the end of this article, open it in the Glade application, and you will be able to explore the layout of the widgets, the settings, and the signals set up for each of the widgets. Step 2: Let's get the base up and running The very first thing I always do after designing the layout using Glade is write a base application that loads the glade file. So here's the base program that I've written (assuming you have named the glade file you've created in Step 1 "notepad.glade"). <?php $app = new NotePad(); Gtk::main(); class NotePad { function __construct() { $glade = new GladeXML(dirname(__FILE__).'/notepad.glade'); $glade->signal_autoconnect_instance($this); } public function on_menu_select($menu_item) { $item = $menu_item->child->get_label(); echo "menu selected: $itemn"; } public function on_search_entry_activate() { echo "on_search_entry_activaten"; } public function on_search_button_clicked($button=null) { echo "on_search_button_clickedn"; } public function on_wordwrap_toggled($menuitem) { $active = $menuitem->get_active(); echo "on_wordwrap_toggled ($active)n"; } } Copy the above program and save it in a file, say "notepad.php." Place this program together with the glade file in the same directory. Suppose your PHP-GTK2 is installed in the c:php-gtk2 directory. To run the program, open a command prompt, change the directory where you have stored notepad.php and notepad.glade, and type: c:php-gtk2 notepad.php You should see the following window appear:
Try typing some text and selecting some of the menu items. You will be able to see the menu item selected being echoed in the command window. The constructor loads the Glade file, while the rest of the functions are empty stubs for the signal handlers. The signal handlers are here so that you do not get any warning messages for the missing signal handlers. This base program is the simplest completely runnable version of the notepad application. It allows you to test your .glade file to see if the layout of the widgets is the same as what you have designed in the Glade application. You can also test to see if all of the menu items respond to selection. If you see no echo in the command window when you select a menu item, you will know that you did not set up the signal handler correctly for that menu item in the glade file. Once your glade file has tested properly, you can start adding each functionality incrementally to this base program. At each step, your PHP-GTK will always be a fully runnable piece of code. Step 3: Adding each functionality incrementally Below I will step through each function of the Notepad application one by one.
blog comments powered by Disqus |
|
|
|
|
|
|
|