GTK example class

By sunslayer on Mar 18, 2011

creates a gui in php with a couple example controls

I couldn't find very many helpful tutorials for GTK, so i hope this helps some people

<?php

    class GUI extends GtkWindow {

        /*
         * Class constructor
         * 
         *  @param int $width       window width
         *  @param int $height      window height
         *  @param int $title       title bar text
         * 
         */

        public function __construct($width=400, $height=200, $title="PHP-GTK GUI") {
            // Call parent class constructor
            parent::__construct();

            // Set basic window properties
            $this->set_default_size($width,$height);
            $this->set_title($title);
            $this->connect_simple("destroy",array($this,"__destruct"));

            // Creates widgets
            $this->setControls();

            // Updates GUI
            $this->show_all();
        }

        /*
         * Class destructor
         * 
         * Breaks main GTK loop and destroys window
         */

        public function __destruct() {
            gtk::main_quit();
        }

        /*
         * Creates controls used in window
         */

        private function setControls() {

            // Creates new buttons and text label
            $lbl =& new GtkLabel("Hello World!");
            $bOk =& new GtkButton("_Ok");
            $bClose =& new GtkButton("_Close");

            // Bind events to widgets
            $bOk->connect_simple("clicked",array($this,"createDialog"));
            $bClose->connect_simple("clicked",array($this,"destroy"));

            // Creates a grid and attaches widgets to it
            $tbl =& new GtkTable(4,2);
            $tbl->attach($lbl,0,1,0,2);
            $tbl->attach($bOk,1,2,0,1);
            $tbl->attach($bClose,1,2,1,2);

            // Adds grid to the window
            $this->add($tbl);
        }

        /*
         * Creates Modal dialog
         */

        public function createDialog() {
            $dialog =& new GtkMessageDialog($this, Gtk::DIALOG_MODAL, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, "PHP-GTK Modal Dialog");
            $dialog->set_markup("<span foreground='blue'>Modal Dialog example</span>");
            $dialog->run();
            $dialog->destroy();
        }
    }

    if (class_exists("gtk")) { // Checks if GTK module is loaded

        // Creates new instance of class GUI
        new GUI(300,75,"Example Window");
        Gtk::main();

    }   else die("GTK Module not loaded."); // Exit

?>

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.