Results 1 to 6 of 6
  1. #1
    Ben Stones's Avatar
    Ben Stones is offline Web Developer
    Join Date
    Jan 2007
    Location
    United Kingdom
    Posts
    4,395

    Default How to install PHP-GTK on Ubuntu 12.04 / in Debian?



    These instructions may work on other Linux distributions as well. Please note this howto is provided in the hope that it is useful, but with no warranties or guarantees of any kind. Your use of the information or advice given in this howto is solely at your own risk and responsibility. We will not be liable for any losses or damages (like loss of data). We cannot guarantee the instructions will work flawlessly, will be error-free or not cause any problems to your operating system. I'd recommend you make a backup before proceeding, and you should make regular backups in general anyway. For full OS backups (including all data on it), consider looking at Clonezilla. By proceeding, you are indicating that you fully understand the risks in doing so.

    Windows users can easily run PHP-GTK applications because binaries are available for Windows users - the most recent official binary available on the PHP-GTK website is, as of writing, 2.0.1. You can download an unofficial, more up-to-date Windows binary compiled by a user often contributing to the project "auroraesrose" here. All you need to do is unzip and run. It's as simple as that. You can right-click on any PHP-GTK file and then execute it with the PHP executable from the unzipped folder.

    Installing PHP-GTK on Ubuntu is not as easy as it should be primarily because certain packages need to be installed beforehand and a few other things - but it's relatively easy. If you know how to use a Terminal window, you'll be on your way.

    There's a few things we need to install beforehand:

    • subversion (for downloading the Cairo extension using svn)
    • git-core (for downloading PHP-GTK using git)
    • php5-dev: This is for compiling additional modules. You won't be able to install PHP-GTK without having php5-dev installed.
    • libglade2-dev: See more about Glade here. Note: No need to install libglade2-0, you should already have this installed on your system. Libglade2-0 is to load external .glade files into your applications. For more information about the Glade IDE, see the link above.
    • pecl-cairo: Read more about Cairo here.


    Apart from that, we have to concatenate a few files back otherwise we'll also run into problems while trying to execute the ./buildconf and ./configure commands prior to installing PHP-GTK.

    I do want to point out that on the PHP-GTK website it states the latest PHP-GTK source is available on the SVN server, but the latest source is available via git - which is why we need to have git installed so we can download the php-gtk source over git.

    Common errors occurring with PHP-GTK installation.

    If you simply download PHP-GTK (either the latest via SVN or downloading the source via the PHP-GTK website) without installing the necessary dependencies above, when executing ./buildconf, you'll come across this error: configure: error: PHP-GTK 2.x requires GLib 2.6.0 or higher.

    Let's get started.

    1. Install the dependencies.
    Code:
    sudo apt-get install build-essential subversion php5-cli php5-dev libgtk2.0-dev libglade2-dev git-core
    2. Download pecl-cairo.
    Code:
    svn co http://svn.php.net/repository/pecl/cairo/trunk pecl-cairo
    cd pecl-cairo
    phpize
    ./configure
    make
    sudo make install
    (What is phpize?)

    The newer libtool.m4 has been split into different files. To be able to compile the PHP-GTK source prior to install (i.e. 'make' and 'sudo make install'), you'll need to concatenate these files back into the libtool.

    Note: This may only apply to Ubuntu. If you have another (non-Ubuntu derivative) Linux distribution installed, or you have Ubuntu but you compiled and installed PHP yourself, try skipping step 3 below and go right ahead to compiling and installing PHP-GTK and see if it works. If not, come back and complete step 3.

    3. Concatenate the files back into the libtool.
    Code:
    cd /usr/share/aclocal
    sudo cp libtool.m4 libtool.m4~backup
    sudo chmod 777 libtool.m4
    sudo cat lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 >>libtool.m4
    sudo chmod 644 libtool.m4
    cd ~/
    Note: "cd ~/" changes the directory back to the home directory (~/) - if you try and fetch the php-gtk source below in /usr/share/aclocal you'll be given a "Permission Denied" error.

    Now let's go ahead and download PHP-GTK. Download the latest source from git (very latest is via git, not svn, unlike what the outdated PHP-GTK Downloads page says). An issue that was later rectified in PHP-GTK which would otherwise cause error: duplicate static to be returned when running the make command prior to installing PHP-GTK - hence we need to download the latest PHP-GTK source from git.

    4. Download and install PHP-GTK.
    Code:
    git clone git://github.com/php/php-gtk-src.git
    ./buildconf
    ./configure
    make
    sudo make install
    For your information - You can find where the PHP-GTK and Cairo extensions are installed by executing php-config and looking at the extensions_dir line.

    PHP-GTK will now be installed at this point. All you have to do now is find where your php-cli php.ini configuration file is and add the extensions to the Dynamic Extensions section of the PHP configuration file (which is above the Module Settings section) so when you execute PHP-GTK applications via the Terminal the extensions can be found.

    You can find where the php.ini file is by searching for it via the File Manager on your Linux distribution, just check which php.ini corresponds to the php-cli folder. It may, however, be located in /etc/php5/cli/php.ini. You can edit the file using gedit or vim, using:

    Code:
    sudo gedit /etc/php5/cli/php.ini
    5. Add the PHP-GTK and Cairo Extension to "Dynamic Extensions".
    Code:
    extension=php_gtk2.so
    extension=cairo.so
    Once done, save the file, quit gedit or :q to exit vim (or :wq if you're in the file at the time to save the changes and quit vim). Now you can execute a PHP-GTK test application to see if everything works.

    Make sure you're in the directory where the PHP-GTK source is located. This is likely in the home directory, so in this case change directory to there:

    Code:
    cd ~/php-gtk
    6. Execute the PHP-GTK demo application.
    Code:
    php demos/phpgtk2-demo.php
    You can also create your own test PHP-GTK application and execute it the same way - a .php extension for your PHP-GTK application will do fine.

    PHP Code:
    <?php

        $window 
    = new GtkWindow();
        
    $window->set_title('My first application!');
        
    $window->connect_simple('destroy', array('Gtk''main_quit'));

        
    $window->show_all();

        
    Gtk::main();

    ?>
    In this very basic PHP-GTK application, only a simple PHP-GTK window will be displayed as shown below. The $window->connect_simple() method is to tell PHP-GTK we want to quit the entire application when the window is closed - because when a window is closed, the destroy signal is fired. The main_quit() method which is a part of the Gtk class quits the application by terminating the main loop (Gtk::main() being the main loop) that keeps the application active.

    Find us on Twitter and Facebook

    How do I contact eUKhost?
    Customer Support: Client Area - 0808 262 0455
    Customer Relations: feedback[@]eukhost.com
    Sales: sales[@]eukhost.com - 0800 862 0380

    Contact:
    Email: ben@eukhost.com
    Skype: euk_ben

    Twitter - LinkedIn - Identi.ca

    The opinions or views expressed above are not necessarily the opinions or views of eUKhost Ltd.


  2. #2
    Ben Stones's Avatar
    Ben Stones is offline Web Developer
    Join Date
    Jan 2007
    Location
    United Kingdom
    Posts
    4,395

    Default

    Here is a sample PHP-GTK application I have been developing over the weekend.

    PHP-GTK Application Screenshot


    PHP-GTK Code
    PHP Code:
    <?php
        
    // check whether php-gtk installed etc.
        
    checkGtk();
        
        class 
    TextEditor
        
    {
            
    // the initial Vbox that will be added to the GtkWindow
            // the HBox will be added to the Vbox
            
    public $init_vbox;
            public 
    $init_hbox;
            
            
    // the open, save and quit buttons that will be on the HBox
            
    public $openBtn;
            public 
    $saveBtn;
            public 
    $quitBtn;
            
            
    // the "Save" menu item under the "File" menu dropdown
            
    public $menuSaveItem;
            
            
    // the main textview
            
    public $textView;
            
            
    // the main window
            
    public $window;
            
            public 
    $active_file// the file currently opened
            
            
    public $default_window_title// default window title
                    
            
    public function __construct()
            {
                
    // instantiate objects
                
    $this->init_vbox = new GtkVBox();
                
    $this->init_hbox = new GtkHBox();
                
    $this->openBtn = new GtkButton("Open");
                
    $this->saveBtn = new GtkButton("Save As...");
                
    $this->quitBtn = new GtkButton("Quit");            
                
                
    // create the window, set the title, default size and destroy action
                
    $this->window = new GtkWindow();
                
                
    // default window title - title may have additional text appended later
                // on
                
    $this->default_window_title "PHP-GTK Text Editor - Cross Platform!";
                
    $this->window->set_title($this->default_window_title);
        
                
    $this->window->set_default_size(700,550);
        
                
    $this->window->connect_simple('delete-event',array($this,"confirmMainQuit")); // delete-event is used instead of destroy as a BOOL value can be returned from the handler method to prevent the window being destroyed
                
                // add and pack
                
    $this->window->add($this->init_vbox);
                
    $this->init_vbox->pack_start($this->init_hbox,FALSE,FALSE);
                
                
    // add the buttons and spacing
                
    $this->init_hbox->pack_start($this->openBtn,FALSE,FALSE);
                
    $this->init_hbox->pack_start($this->saveBtn,FALSE,FALSE);
                
                
    // spacing
                
    $this->init_hbox->pack_start(new GtkHBox());
                
                
    // add quit button
                
    $this->init_hbox->pack_start($this->quitBtn,FALSE,FALSE);
                
                
    // connect the signals (buttons)
                
    $this->openBtn->connect_simple('clicked',array($this,"menuButtonClicked"),"open");
                
    $this->saveBtn->connect_simple('clicked',array($this,"menuButtonClicked"),"save");
                
    $this->quitBtn->connect_simple('clicked',array($this,"menuButtonClicked"),"quit");
                    
                
    // add the textview
                
                
    $this->textView = new GtkTextView();
                
                
    $gtkScrolledWindow = new GtkScrolledWindow();
                
                
    $gtkScrolledWindow->add($this->textView);
                
                
    $gtkScrolledWindow->set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
                
                
    $this->init_vbox->pack_start($gtkScrolledWindow);
                
                
    // add menu bar
                
    $this->addMenu();
                
                
    // show all widgets, etc.
                
    $this->window->show_all();
                
                
    // set focus to the textView
                
    $this->window->set_focus($this->textView);
                            
                
    // the main loop
                
    Gtk::main();
            }
            
            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);
            }
            
            public function 
    menuOptionClicked($param)
            {
                
    // find out what action to perform
                
                
    if(is_string($param))
                {
                    switch(
    $param)
                    {
                        case 
    "quit":
                            
    $get_buffer $this->textView->get_buffer();
                            
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                            
                            if(
    strlen($get_text) == 0)
                            {
                                
    Gtk::main_quit();
                            }
                            else
                            {
                                
    // warn first
                                
    $confirmDialog $this->confirmationDialogBox("Are you sure?""You have unsaved changes. If you quit before saving, you will permanently lose any unsaved changes...\n\nAre you sure?");
                                if(
    is_int($confirmDialog)) // user wants to quit the application
                                
    {
                                    
    Gtk::main_quit();
                                }
                                else 
    // will be an object - user does not want to quit the application
                                
    {
                                    
    $confirmDialog->destroy();
                                }
                            }
                            break;
                    }
                }
            }
            
            public function 
    menuButtonClicked($param)
            {
                
    // find out what action to perform
                
                
    if(is_string($param))
                {
                    switch(
    $param)
                    {
                        case 
    "quit":
                            
    // check whether the user has anything written
                            
    $get_buffer $this->textView->get_buffer();
                            
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                            
                            if(
    strlen($get_text) == 0)
                            {
                                
    Gtk::main_quit();
                            }
                            else
                            {
                                
    // warn first
                                
    $confirmDialog $this->confirmationDialogBox("Are you sure?""You have unsaved changes. If you quit before saving, you will permanently lose any unsaved changes...\n\nAre you sure?");
                                if(
    is_int($confirmDialog)) // user wants to quit the application
                                
    {
                                    
    Gtk::main_quit();
                                }
                                else 
    // will be an object - user does not want to quit the application
                                
    {
                                    
    $confirmDialog->destroy();
                                }
                            }
                            
                            break;
                            
                        case 
    "open":
                            
    $fchooser = new GtkFileChooserDialog("Choose the file to open"NULLGtk::FILE_CHOOSER_ACTION_OPEN, array(Gtk::STOCK_OKGtk::RESPONSE_OK));
                            
    $fchooser->show_all();
                            
                            
    // check response
                            
    if($fchooser->run() == Gtk::RESPONSE_OK)
                            {
                                
    // get the file name
                                
    $filename $fchooser->get_filename();
                                
                                
    $contents file_get_contents($filename);
                                
                                
    // output to textview
                                
    $set_text = new GtkTextBuffer();
                                
    $set_text->set_text($contents);
                                
                                
    $this->textView->set_buffer($set_text);
                                
                                
    // set active_file
                                
    $this->active_file $filename;
                                
                                
    // set save button text
                                
    $this->saveBtn->set_label("Save File");
                                
    $this->menuSaveItem->set_label("Save File");
                            }
                            
    $fchooser->destroy();
                            
                            break;
                            
                        case 
    "save":
                            
    // if a file has been opened
                            // before...
                            
    if(isset($this->active_file) && $this->active_file)
                            {
                                
    // if the file is active (open), then we don't want
                                // to display a dialog box...
                                
                                
    $get_buffer $this->textView->get_buffer();
                                
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                                
                                
    $save file_put_contents($this->active_file$get_textLOCK_EX);
                                
                                
    $this->window->set_title($this->default_window_title " - saved at " date("g:i:s a"time()));
                            }
                            else
                            {    
    // maybe we do...                    
                                
    $fsave = new GtkFileChooserDialog("Where do you want to save your text file?"NULLGtk::FILE_CHOOSER_ACTION_SAVE, array(Gtk::STOCK_OKGtk::RESPONSE_OK));
                                
    $fsave->show_all();
                            
                                
    // check response
                                
    if($fsave->run() == Gtk::RESPONSE_OK)
                                {
                                    
    // if a file is open, we want to set that as the file to save to
                                    
    $filesave_name = (!isset($filename)) ? $fsave->get_filename() : $filename;
                                
                                    
    // save the contents of the textbuffer to the file
                                    
    $get_buffer $this->textView->get_buffer();
                                    
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                                
                                    
    $save file_put_contents($filesave_name$get_textLOCK_EX);
                                    
    $this->active_file $filesave_name;
                                
                                    
    $this->saveBtn->set_label("Save");
                                    
    $this->menuSaveItem->set_label("Save");
                                }
                            
                                
    // it will stay displayed otherwise after saving...
                                
    $fsave->destroy();
                            }                        
                            break;
                    }
                }
            }
                
                public function 
    confirmMainQuit()
                {        
                    
    $get_buffer $this->textView->get_buffer();
                    
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                    
                    if(
    strlen($get_text) == 0)
                    {
                        
    Gtk::main_quit();
                    }
                    else
                    {
                        
    // warn first
                        
    $confirmDialog $this->confirmationDialogBox("Are you sure?""You have unsaved changes. If you quit before saving, you will permanently lose any unsaved changes...\n\nAre you sure?");
                        if(
    is_int($confirmDialog)) // user wants to quit the application
                        
    {
                            
    // need to quit the main loop
                            // if we simply destroy the
                            // window, the dialog box will
                            // remain active, and so will the
                            // application process
                            
    Gtk::main_quit();
                        }
                        else 
    // will be an object - user does not want to quit the application
                        
    {
                            
    $confirmDialog->destroy();
                            
                            return 
    TRUE// telling php-gtk it has been handled
                        
    }
                    }
                }
            
            public function 
    confirmationDialogBox($title,$text)
            {
                
    // warn the user first
                
    $dialog = new GtkDialog($titleNULLGtk::DIALOG_MODAL, array(Gtk::STOCK_NOGtk::RESPONSE_NOGtk::STOCK_YESGtk::RESPONSE_YES));
                
    $dialog->vbox->add(new GtkLabel($text));
                
                
    // set the dialog size
                
    $dialog->set_default_size(325,85);
                
                
    // show all widgets
                
    $dialog->show_all();
                
                if(
    $dialog->run() == Gtk::RESPONSE_YES)
                {
                    return 
    1;
                }
                else
                {
                    return 
    $dialog;
                }
            }
        }
        
        
    $texteditor = new TextEditor();

        function 
    checkGtk()
        {
            if (!
    class_exists('gtk')) {
                
    $msg "You cannot run this PHP-GTK application because you do not have PHP-GTK installed. Please install the following dependencies: build-essential php5-cli php5-dev libgtk2.0-dev libglade2-dev and install pecl-cairo, and compile and install PHP-GTK. Then add the PHP-GTK and Cairo extensions to php.ini. General instructions for installation are below:
            
        Make sure you're superuser. You can do this each time using:

        sudo

        Or elevate to root:

        sudo su -

        You can get the latest and greatest version of PHP-GTK directly from git (make sure you have the git-core package installed).

        If your Linux distribution uses apt, install git-core using:

        apt-get install git-core

        If your Linux distribution uses yum, install git-core using:

        yum install git-core

        Make sure you have the necessary dependencies:

        If your Linux distribution uses apt:

        apt-get install build-essential php5-cli php5-dev libgtk2.0-dev libglade2-dev

        If your Linux distribution uses yum:

        yum install build-essential php5-cli php5-dev libgtk2.0-dev libglade2-dev

        Install pecl-cairo (another dependency)

        git clone https://github.com/gtkforphp/cairo.git
        phpize
        ./configure
        make
        make install

        If you're running Ubuntu, it appears libtool is split into separate files and when you try and compile PHP-GTK prior to concatenating the files back into the libtool, you'll get errors similar to:

        configure.in:77: warning: LTOPTIONS_VERSION is m4_require’d but not m4_defun’d
        aclocal.m4:2912: LT_INIT is expanded from…
        aclocal.m4:2947: AC_PROG_LIBTOOL is expanded from…
        configure.in:77: the top level
        configure.in:77: warning: LTSUGAR_VERSION is m4_require’d but not m4_defun’d
        configure.in:77: warning: LTVERSION_VERSION is m4_require’d but not m4_defun’d
        configure.in:77: warning: LTOBSOLETE_VERSION is m4_require’d but not

        configure.in:51: error: possibly undefined macro: AC_MSG_ERROR
        If this token and others are legitimate, please use m4_pattern_allow.
        See the Autoconf documentation.
        configure:4266: error: possibly undefined macro: AM_PATH_GLIB_2_0
        configure:4397: error: possibly undefined macro: AM_PATH_GTK_2_0

        ./configure: line 4242: syntax error near unexpected token `debug,’
        ./configure: line 4242: `PHP_GTK_ARG_ENABLE(debug, whether to include debugging symbols,’

        If the error is output at the time of compiling PHP-GTK, you need to concatenate the files back into the libtool:

        cd /usr/share/aclocal
        cp libtool.m4 libtool.m4~backup
        chmod 777 libtool.m4
        cat lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 >>libtool.m4
        chmod 644 libtool.m4
        cd ~/

        Clone the PHP-GTK source:

        git clone git://github.com/php/php-gtk-src.git

        Now it's time to configure and install PHP-GTK:

        ./buildconf
        ./configure
        make
        make install

        Once PHP-GTK is installed, you need to add the php-gtk and cairo extensions to the php.ini configuration file. Find the PHP configuration file for php-cli and modify the file - such as:

        gedit /etc/php5/cli/php.ini

        And add the following to the \"Dynamic Extensions\" section:

        extension=php_gtk2.so
        extension=cairo.so

        "
    ;
                
    file_put_contents("ERROR",$msg);
                die();
            }
        }
    ?>
    You could create a small script to execute the PHP-GTK application automatically. One example is by creating a file without an extension, right-clicking and from the Properties window, "Allow execution of this program". You can then double-click the script to automatically execute the PHP-GTK application with the PHP interpreter. Inside the script would have something like:

    Code:
    php Text-Editor.php
    Pretty simple . Disclaimer: There are no guarantees or warranties as to the reliability, stability or security of the application and the application source code, and your expectations of the application and your use of it, or any part thereof. Use at your own risk and responsibility. I disclaim all warranties and guarantees, whether express or implied, to the maximum extent permitted under British law. I'm a good PHP developer, but I can't guarantee it'll work perfectly without errors or cause any problems with your operating system (loss of data, for example), etc.
    Find us on Twitter and Facebook

    How do I contact eUKhost?
    Customer Support: Client Area - 0808 262 0455
    Customer Relations: feedback[@]eukhost.com
    Sales: sales[@]eukhost.com - 0800 862 0380

    Contact:
    Email: ben@eukhost.com
    Skype: euk_ben

    Twitter - LinkedIn - Identi.ca

    The opinions or views expressed above are not necessarily the opinions or views of eUKhost Ltd.


  3. #3
    Ben Stones's Avatar
    Ben Stones is offline Web Developer
    Join Date
    Jan 2007
    Location
    United Kingdom
    Posts
    4,395

    Default

    Here is a more up to date version of the demo PHP-GTK application above. Again, please do take note that there are no warranties or guarantees with this application and the application code. It may not work, it could crash, you could lose valuable data, etc. Again, I have coded this to a good standard but use of the application and/or the application code (or any portion thereof) is strictly at your own risk and responsibility.

    PHP Code:
    <?php
        
    // check whether php-gtk installed etc.
        
    checkGtk();
        
        class 
    TextEditor
        
    {
            
    // the initial Vbox that will be added to the GtkWindow
            // the HBox will be added to the Vbox
            
    public $init_vbox;
            public 
    $init_hbox;
            
            
    // the open, save and quit buttons that will be on the HBox
            
    public $openBtn;
            public 
    $saveBtn;
            public 
    $quitBtn;
            
            
    // the "Save" menu item under the "File" menu dropdown
            
    public $menuSaveItem;
            
            
    // the main textview
            
    public $textView;
            
            
    // the main window
            
    public $window;
            
            public 
    $active_file// the file currently opened
            
            
    public $default_window_title// default window title
                    
            
    public function __construct()
            {
                
    // instantiate objects
                
    $this->init_vbox = new GtkVBox();
                
    $this->init_hbox = new GtkHBox();
                
    $this->openBtn = new GtkButton("Open");
                
    $this->saveBtn = new GtkButton("Save As...");
                
    $this->quitBtn = new GtkButton("Quit");            
                
                
    // create the window, set the title, default size and destroy action
                
    $this->window = new GtkWindow();
                
                
    // default window title - title may have additional text appended later
                // on
                
    $this->default_window_title "PHP-GTK Text Editor - Cross Platform!";
                
    $this->window->set_title($this->default_window_title);
        
                
    $this->window->set_default_size(700,550);
        
                
    $this->window->connect_simple('delete-event',array($this,"confirmMainQuit")); // delete-event is used instead of destroy as a BOOL value can be returned from the handler method to prevent the window being destroyed
                
                // add and pack
                
    $this->window->add($this->init_vbox);
                
    $this->init_vbox->pack_start($this->init_hbox,FALSE,FALSE);
                
                
    // add the buttons and spacing
                
    $this->init_hbox->pack_start($this->openBtn,FALSE,FALSE);
                
    $this->init_hbox->pack_start($this->saveBtn,FALSE,FALSE);
                
                
    // spacing
                
    $this->init_hbox->pack_start(new GtkHBox());
                
                
    // add quit button
                
    $this->init_hbox->pack_start($this->quitBtn,FALSE,FALSE);
                
                
    // connect the signals (buttons)
                
    $this->openBtn->connect_simple('clicked',array($this,"menuButtonClicked"),"open");
                
    $this->saveBtn->connect_simple('clicked',array($this,"menuButtonClicked"),"save");
                
    $this->quitBtn->connect_simple('clicked',array($this,"menuButtonClicked"),"quit");
                    
                
    // add the textview
                
                
    $this->textView = new GtkTextView();
                
                
    $gtkScrolledWindow = new GtkScrolledWindow();
                
                
    $gtkScrolledWindow->add($this->textView);
                
                
    $gtkScrolledWindow->set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
                
                
    $this->init_vbox->pack_start($gtkScrolledWindow);
                
                
    // add menu bar
                
    $this->addMenu();
                
                
    // show all widgets, etc.
                
    $this->window->show_all();
                
                
    // set focus to the textView
                
    $this->window->set_focus($this->textView);
                            
                
    // the main loop
                
    Gtk::main();
            }
            
            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");
                
    $this->menuSaveItem->connect_simple('activate',array($this,'menuOptionClicked'),"save");
        
                
    $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);
            }
            
            public function 
    menuOptionClicked($param)
            {
                
    // find out what action to perform
                
                
    if(is_string($param))
                {
                    switch(
    $param)
                    {
                        case 
    "quit":
                            
    $get_buffer $this->textView->get_buffer();
                            
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                            
                            if(
    strlen($get_text) == 0)
                            {
                                
    Gtk::main_quit();
                            }
                            else
                            {
                                
    // warn first
                                
    $confirmDialog $this->confirmationDialogBox("Are you sure?""You have unsaved changes. If you quit before saving, you will permanently lose any unsaved changes...\n\nAre you sure?");
                                if(
    is_int($confirmDialog)) // user wants to quit the application
                                
    {
                                    
    Gtk::main_quit();
                                }
                                else 
    // will be an object - user does not want to quit the application
                                
    {
                                    
    $confirmDialog->destroy();
                                }
                            }
                            
                            break;
                        case 
    "open":
                            
    $open $this->openFileDialog();
                            
                            break;
                            
                        case 
    "save":
                            
    $this->saveFileDialog();                
                            break;
                    }
                }
            }
            
            public function 
    menuButtonClicked($param)
            {
                
    // find out what action to perform
                
                
    if(is_string($param))
                {
                    switch(
    $param)
                    {
                        case 
    "quit":
                            
    // check whether the user has anything written
                            
    $get_buffer $this->textView->get_buffer();
                            
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                            
                            if(
    strlen($get_text) == 0)
                            {
                                
    Gtk::main_quit();
                            }
                            else
                            {
                                
    // warn first
                                
    $confirmDialog $this->confirmationDialogBox("Are you sure?""You have unsaved changes. If you quit before saving, you will permanently lose any unsaved changes...\n\nAre you sure?");
                                if(
    is_int($confirmDialog)) // user wants to quit the application
                                
    {
                                    
    Gtk::main_quit();
                                }
                                else 
    // will be an object - user does not want to quit the application
                                
    {
                                    
    $confirmDialog->destroy();
                                }
                            }
                            
                            break;
                            
                        case 
    "open":
                            
    $this->openFileDialog();
                            
                            break;
                            
                        case 
    "save":
                            
    $this->saveFileDialog();                
                            break;
                    }
                }
            }
            
            public function 
    saveFileDialog()
            {
                
    // if a file has been opened
                // before...
                
    if(isset($this->active_file) && $this->active_file)
                {
                    
    // if the file is active (open), then we don't want
                    // to display a dialog box...
                    
                    
    $get_buffer $this->textView->get_buffer();
                    
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                    
                    
    $save file_put_contents($this->active_file$get_textLOCK_EX);
                    
                    
    $this->window->set_title($this->default_window_title " - saved at " date("g:i:s a"time()));
                }
                else
                {    
    // maybe we do...                    
                    
    $fsave = new GtkFileChooserDialog("Where do you want to save your text file?"NULLGtk::FILE_CHOOSER_ACTION_SAVE, array(Gtk::STOCK_OKGtk::RESPONSE_OK));
                    
    $fsave->show_all();
                
                    
    // check response
                    
    if($fsave->run() == Gtk::RESPONSE_OK)
                    {
                        
    // if a file is open, we want to set that as the file to save to
                        
    $filesave_name = (!isset($filename)) ? $fsave->get_filename() : $filename;
                    
                        
    // save the contents of the textbuffer to the file
                        
    $get_buffer $this->textView->get_buffer();
                        
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                    
                        
    $save file_put_contents($filesave_name$get_textLOCK_EX);
                        
    $this->active_file $filesave_name;
                    
                        
    $this->saveBtn->set_label("Save");
                        
    $this->menuSaveItem->set_label("Save");
                    }
                
                    
    // it will stay displayed otherwise after saving...
                    
    $fsave->destroy();
                            }
            }
            
            public function 
    openFileDialog()
            {
                
    $fchooser = new GtkFileChooserDialog("Choose the file to open"NULLGtk::FILE_CHOOSER_ACTION_OPEN, array(Gtk::STOCK_OKGtk::RESPONSE_OK));
                
    $fchooser->show_all();
            
                
    // check response
                
    if($fchooser->run() == Gtk::RESPONSE_OK)
                {
                    
    // get the file name
                    
    $filename $fchooser->get_filename();
                
                    
    $contents file_get_contents($filename);
                
                    
    // output to textview
                    
    $set_text = new GtkTextBuffer();
                    
    $set_text->set_text($contents);
                
                    
    $this->textView->set_buffer($set_text);
                
                    
    // set active_file
                    
    $this->active_file $filename;
                
                    
    // set save button text
                    
    $this->saveBtn->set_label("Save File");
                    
    $this->menuSaveItem->set_label("Save File");
                }
                
                
    $fchooser->destroy();
            }
                
            public function 
    confirmMainQuit()
            {        
                
    $get_buffer $this->textView->get_buffer();
                
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter());
                
                if(
    strlen($get_text) == 0)
                {
                    
    Gtk::main_quit();
                }
                else
                {
                    
    // warn first
                    
    $confirmDialog $this->confirmationDialogBox("Are you sure?""You have unsaved changes. If you quit before saving, you will permanently lose any unsaved changes...\n\nAre you sure?");
                    if(
    is_int($confirmDialog)) // user wants to quit the application
                    
    {
                        
    // need to quit the main loop
                        // if we simply destroy the
                        // window, the dialog box will
                        // remain active, and so will the
                        // application process
                        
    Gtk::main_quit();
                    }
                    else 
    // will be an object - user does not want to quit the application
                    
    {
                        
    $confirmDialog->destroy();
                        
                        return 
    TRUE// telling php-gtk it has been handled
                    
    }
                }
            }
            
            public function 
    confirmationDialogBox($title,$text)
            {
                
    // warn the user first
                
    $dialog = new GtkDialog($titleNULLGtk::DIALOG_MODAL, array(Gtk::STOCK_NOGtk::RESPONSE_NOGtk::STOCK_YESGtk::RESPONSE_YES));
                
    $dialog->vbox->add(new GtkLabel($text));
                
                
    // set the dialog size
                
    $dialog->set_default_size(325,85);
                
                
    // show all widgets
                
    $dialog->show_all();
                
                if(
    $dialog->run() == Gtk::RESPONSE_YES)
                {
                    return 
    1;
                }
                else
                {
                    return 
    $dialog;
                }
            }
        }
        
        
    $texteditor = new TextEditor();

        function 
    checkGtk()
        {
            if (!
    class_exists('gtk')) {
                
    $msg "You cannot run this PHP-GTK application because you do not have PHP-GTK installed. Please install the following dependencies: build-essential php5-cli php5-dev libgtk2.0-dev libglade2-dev and install pecl-cairo, and compile and install PHP-GTK. Then add the PHP-GTK and Cairo extensions to php.ini. General instructions for installation are below:
            
        Make sure you're superuser. You can do this each time using:

        sudo

        Or elevate to root:

        sudo su -

        You can get the latest and greatest version of PHP-GTK directly from git (make sure you have the git-core package installed).

        If your Linux distribution uses apt, install git-core using:

        apt-get install git-core

        If your Linux distribution uses yum, install git-core using:

        yum install git-core

        Make sure you have the necessary dependencies:

        If your Linux distribution uses apt:

        apt-get install build-essential php5-cli php5-dev libgtk2.0-dev libglade2-dev

        If your Linux distribution uses yum:

        yum install build-essential php5-cli php5-dev libgtk2.0-dev libglade2-dev

        Install pecl-cairo (another dependency)

        git clone https://github.com/gtkforphp/cairo.git
        phpize
        ./configure
        make
        make install

        If you're running Ubuntu, it appears libtool is split into separate files and when you try and compile PHP-GTK prior to concatenating the files back into the libtool, you'll get errors similar to:

        configure.in:77: warning: LTOPTIONS_VERSION is m4_require’d but not m4_defun’d
        aclocal.m4:2912: LT_INIT is expanded from…
        aclocal.m4:2947: AC_PROG_LIBTOOL is expanded from…
        configure.in:77: the top level
        configure.in:77: warning: LTSUGAR_VERSION is m4_require’d but not m4_defun’d
        configure.in:77: warning: LTVERSION_VERSION is m4_require’d but not m4_defun’d
        configure.in:77: warning: LTOBSOLETE_VERSION is m4_require’d but not

        configure.in:51: error: possibly undefined macro: AC_MSG_ERROR
        If this token and others are legitimate, please use m4_pattern_allow.
        See the Autoconf documentation.
        configure:4266: error: possibly undefined macro: AM_PATH_GLIB_2_0
        configure:4397: error: possibly undefined macro: AM_PATH_GTK_2_0

        ./configure: line 4242: syntax error near unexpected token `debug,’
        ./configure: line 4242: `PHP_GTK_ARG_ENABLE(debug, whether to include debugging symbols,’

        If the error is output at the time of compiling PHP-GTK, you need to concatenate the files back into the libtool:

        cd /usr/share/aclocal
        cp libtool.m4 libtool.m4~backup
        chmod 777 libtool.m4
        cat lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 >>libtool.m4
        chmod 644 libtool.m4
        cd ~/

        Clone the PHP-GTK source:

        git clone git://github.com/php/php-gtk-src.git

        Now it's time to configure and install PHP-GTK:

        ./buildconf
        ./configure
        make
        make install

        Once PHP-GTK is installed, you need to add the php-gtk and cairo extensions to the php.ini configuration file. Find the PHP configuration file for php-cli and modify the file - such as:

        gedit /etc/php5/cli/php.ini

        And add the following to the \"Dynamic Extensions\" section:

        extension=php_gtk2.so
        extension=cairo.so

        "
    ;
                
    file_put_contents("ERROR",$msg);
                die();
            }
        }
    ?>
    Find us on Twitter and Facebook

    How do I contact eUKhost?
    Customer Support: Client Area - 0808 262 0455
    Customer Relations: feedback[@]eukhost.com
    Sales: sales[@]eukhost.com - 0800 862 0380

    Contact:
    Email: ben@eukhost.com
    Skype: euk_ben

    Twitter - LinkedIn - Identi.ca

    The opinions or views expressed above are not necessarily the opinions or views of eUKhost Ltd.


  4. #4
    summerwinter is offline Forum Member
    Join Date
    Sep 2012
    Posts
    1

    Default

    Hi,

    Just wanted to register to thank you. I have been trying to install PHP-GTK for the last two days and couldn't figure out how the heck to do it. Very well explained and detailed tutorial. Thanks! I noticed you posted a PHP-GTK tutorial here. Are you going to post further PHP-GTK tutorials?

    It's a shame PHP-GTK is not found in the Ubuntu repositories by default but unfortunately the team need to prioritise the packaging of other things into the repository that is included on the LiveCD. Hopefully when they make the jump to DVD-only media they'll be able to include more useful packages. I don't understand why they don't do what some other Linux distributions do - some Linux distributions have both a stripped-down ISO for CDs and a more complete ISO for DVDs with the DVD media containing even more packages and software by default.

    Nonetheless, good PHP-GTK installation tutorial!

  5. #5
    Ben Stones's Avatar
    Ben Stones is offline Web Developer
    Join Date
    Jan 2007
    Location
    United Kingdom
    Posts
    4,395

    Default

    Quote Originally Posted by summerwinter View Post
    Hi,

    Just wanted to register to thank you. I have been trying to install PHP-GTK for the last two days and couldn't figure out how the heck to do it. Very well explained and detailed tutorial. Thanks! I noticed you posted a PHP-GTK tutorial here. Are you going to post further PHP-GTK tutorials?

    It's a shame PHP-GTK is not found in the Ubuntu repositories by default but unfortunately the team need to prioritise the packaging of other things into the repository that is included on the LiveCD. Hopefully when they make the jump to DVD-only media they'll be able to include more useful packages. I don't understand why they don't do what some other Linux distributions do - some Linux distributions have both a stripped-down ISO for CDs and a more complete ISO for DVDs with the DVD media containing even more packages and software by default.

    Nonetheless, good PHP-GTK installation tutorial!
    Hi,

    Thank you . You're welcome.

    And I agree, I wish PHP-GTK was found in the Ubuntu repositories by default - would save us all a lot of time and effort just to get PHP-GTK installed. The main difficulty with installing PHP-GTK on Linux is the dependencies that it requires that many Linux distributions do not have installed by default.
    Find us on Twitter and Facebook

    How do I contact eUKhost?
    Customer Support: Client Area - 0808 262 0455
    Customer Relations: feedback[@]eukhost.com
    Sales: sales[@]eukhost.com - 0800 862 0380

    Contact:
    Email: ben@eukhost.com
    Skype: euk_ben

    Twitter - LinkedIn - Identi.ca

    The opinions or views expressed above are not necessarily the opinions or views of eUKhost Ltd.


  6. #6
    sproggit is offline Forum Member
    Join Date
    May 2013
    Posts
    1

    Default Re: How to install PHP-GTK on Ubuntu 12.04 / in Debian?

    Hi Ben,

    I'd like to add to other expressions of thanks for the creation of this post. It was clear and helpful in enabling me to get php-gtk up and working...

    In my case, that was actually for a 32-bit build running under Linux Mint 14 (Cinnamon), which is of course based on Quantal... In so doing I did hit a weird snag and thought it might be worth sharing, just in case it's of any help to anyone else...

    For Step 5 (Adding the Dynamic Extensions to php.ini), the Mint implementation seems to be subtly different. Instead of going directly to the php.ini file, you should go to

    /etc/php5/mods-available/

    and edit the file

    pdo.ini

    with the cairo and php_gtk2 shared object library names. Sadly, even this is not quite enough [although I'll concede that in my case there is probably a more elegant fix than my solution. Basically you have to put your freshly-compiled shared object files in the appropriate location in the file system for php to find them...

    So you can either use a symbolic link (ln -s) or copy the files directly. In my case I chose to copy the files directly, if only because that meant that if I were to accidentally corrupt the build tree in my home folder, I would not lose the functionality... So I did this [from my home folder]:-

    cd php-gtk-src/modules
    sudo cp php_gtk2.so /usr/lib/php5/20100525+lfs/
    sudo cp cairo.so /usr/lib/php5/20100525+lfs/


    Sadly, I have no idea what the significance of the 20100525+lfs folder might be [other than maybe acknowledgement of a unique build date for part of this codebase. I was able to deduce the need for this after successfully making the change to the /etc/php5/mods-available/pdo.ini file, since once I'd made the change I attempted to run the recommended demo code, but instead I got an error message warning:-

    X35 demos # php phpgtk2-demo.php
    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20100525+lfs/php_gtk2.so' - /usr/lib/php5/20100525+lfs/php_gtk2.so: cannot open shared object file: No such file or directory in Unknown on line 0
    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20100525+lfs/php_gtk2.so' - /usr/lib/php5/20100525+lfs/php_gtk2.so: cannot open shared object file: No such file or directory in Unknown on line 0
    PHP Warning: Module 'cairo' already loaded in Unknown on line 0
    Please load the php-gtk2 module in your php.ini


    Please bear in mind that I'm not a programmer and I don't know what I'm doing. I only know that by trying to interpret the error messages and tweaking my actions in response, I have been able to make your approach work well for Mint 14.0...

    Thanks again for such a clear and comprehensive set of notes - they were invaluable.
    Last edited by sproggit; 05-05-2013 at 17:07. Reason: spotted an error

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Disclaimer of Liability

The opinions or views posted on our forum by users do not necessarily represent the opinions or views of eUKhost Limited.

Our forum may contain hyperlinks to external websites not under our control and ownership. We do not endorse the content found on any external websites and we cannot guarantee the accuracy, reliability, availability and suitability of such websites and the content of those websites. Your access and use of any external websites is at your sole risk and responsibility. Please also be aware external websites will have differing terms and conditions, privacy and cookie usage policies to our website.