Sample Code 317: How to create a php gtk browser using gtkhtml - Part 1?
Written by kksou   
Saturday, 01 September 2007
Problem

With the new PHP-GTK2 beta release, it's now a breeze to display html text with the use of GtkHTML.

So let's try to build a php-gtk browser using GtkHTML as shown below:

How to create a php gtk browser using gtkhtml - Part 1?


Solution
  • In the first row, we setup a GtkEntry for users to enter the url.
  • When the user presses Enter or clicks the 'Go' button, we read the html text from the url and display it using the GtkHTML.
  • Pay particular attention to how images are loaded in GtkHTML. It's handled by the signal 'url-requested'.

If this is the first time you use GtkHtml, please take a look at the article How to display html text using gtkhtml? that gives an introduction to the use of GtkHtml. Please also take note of the following.

Important Note:

  • This only works for PHP-GTK2 compliled with the additional library GtkHTML.
  • For linux, you have to recompile php-gtk2 to include this library.
  • For windows, you may refer to the article How to install PHP-GTK2 on windows. The latest beta release from official php-gtk2 website comes complete with GtkHTML.
  • In the php.ini, don't forget to add php-gtk.extensions = php_gtk_html2.dll to turn on GtkHTML.
  • Lastly, the most "tricky" part in running GtkHTML is that to run this script, you have to use gconfd-2 | php script.php.

    If you have installed the beta release of PHP-GTK2 on windows as outlined in this article, you will find the program gconfd-2.exe in the root directory of php-gtk.
  • In the event that you cannot get this sample code to work, I would suggest that you try to do a fresh install of the beta-release of PHP-GTK2 (details here). It should work out-of-the-box (just need to add php-gtk.extensions = php_gtk_html2.dll in php.ini as explained above). Note that you can still keep your original copy of php-gtk2 while having this new version.
  • You will most likely see the warning (php.exe:5348): Gdk-WARNING **: gdkselection-win32.c:1068: OpenClipboard failed: Invalid window handle.. Not really sure how to fix this yet. The script seems to run ok, though.


Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(800, 640);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0);

$hbox->pack_start(new GtkLabel('URL: '), 0);
$hbox->pack_start($url_entry = new GtkEntry('http://www.google.com'));
$hbox->pack_start($go_button = new GtkButton('Go'), 0);
$go_button->set_size_request(32, -1);

$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
$vbox->pack_start($scrolled_win);
$gtkhtml = new GtkHTML(); // note 1
$scrolled_win->add($gtkhtml);

$gtkhtml->connect('url-requested', 'on_url_requested'); // note 2

$url_entry->connect('activate', 'on_url_entry_activate', $go_button); // note 3
$go_button->connect('clicked', 'on_go_button', $url_entry, $gtkhtml); // note 4
$go_button->clicked();

$window->show_all();
Gtk::main();

function on_url_entry_activate($entry, $go_button) {
    $go_button->clicked(); // note 5
}

function on_go_button($button, $url_entry, $gtkhtml) {
    $url = $url_entry->get_text(); // note 6
    $html_text = file_get_contents($url); // note 7
    $gtkhtml->set_base($url);
    $gtkhtml->load_from_string($html_text); // note 8
}

function on_url_requested($gtkhtml, $url, $stream) {
    echo "image url = $url\n"; // note 9
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Create a new GtkHTML.
  2. This is used to handle all the images. We are supposed to load the images in the signal handler.
  3. Allow users to press Enter in the GtkEntry.
  4. Handle button click on the Go button.
  5. Simulate a click on the Go button when the user presses Enter.
  6. Get the url from the GtkEntry.
  7. Retreive the contents from the specified url.
  8. Display it!
  9. That's all I can do now. Still trying to figure out how to display the image.

Note

Did you see that the images are not displayed? I'm still trying to figure out this part.

Do you have any clues...


Related Links

User reviews   Average user ratings:    3.5   (from 5 users)
  1. ryannining
    April 11, 2008 3:41am
    It doesnot work for me

    This is the error code:

    C:\php-gtk>php.exe contoh\gtkhtml.php

    Fatal error: Class 'GtkHTML' not found in C:\php-gtk\contoh\gtkhtml.php on line
    19

  2. kksou
    April 11, 2008 8:41am

    This means that your GtkHTML has not been loaded. You have to give a bit more information for us to help you e.g. what platform are you using? Which version of php-gtk2 are you using? (beta? v2.0?) What have you tried? Did you add the line to load the library in php.ini?

    Regards,
    /kksou

  3. gianni
    August 18, 2008 12:30pm

  4. adel
    August 20, 2008 11:49am
    It doesnt work also

    I am using
    php-gtk-2.0.1 Windows binary pack
    php-gtk-2.0.1 Windows binary extensions pack
    under windows xp sp2
    i have these errors :
    gtkhtmls-3.0.dll is missing
    Fatal error: Class 'GtkHTML' not found
    and cant not load
    this is my php.ini
    [PHP]

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; About the php.ini in PHP-GTK ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;
    ; This file introduces the php.ini settings that you will need in order to
    ; run PHP-GTK on your system. You may also need other settings from PHP's
    ; standard php.ini file, e.g. to load further extensions or otherwise control
    ; PHP's behaviour in matters such as error reporting. Please add those in
    ; the upper part of this file, in the PHP section.

    ; You should use PHP's CLI executable to run PHP-GTK. This php.ini file
    ; should be in the same directory as the PHP executable, to avoid conflict
    ; with any other copies of PHP that may be installed on your machine.

    ; The first thing you will need to do is tell PHP where you want it to look
    ; for the PHP extension libraries (php_*.dll or php_*.so files) on your system.

    extension_dir = "./ext"

    ; Make sure that php-gtk2.dll under Windows, or php-gtk2.so under Unix, is in
    ; the directory named in extension_dir alongside any other shared PHP extensions
    ; you intend to use, and tell PHP to load it.

    extension = php_gtk2.dll
    ;extension = php_gtk_html2.dll
    ;extension = php_sqlite.**
    ;extension = php_pdo_sqlite.**

    [Date]
    ; Defines the default timezone used by the date functions
    ;date.timezone =

    [PHP-GTK]

    ;;;;;;;;;;;;;;;;;;;;;;
    ; PHP-GTK extensions ;
    ;;;;;;;;;;;;;;;;;;;;;;

    ; Extensions written for PHP-GTK are in the format php_gtk_*.dll (Windows) or
    ; php_gtk_*.so (Unix), written here as a comma-separated list. The library
    ; files need to be in the same directory as the PHP-GTK library, along with
    ; any other PHP extensions you are using.

    php-gtk.extensions = php_gtk_libglade2.dll,php_gtk_sourceview2.dll,php_gtk_html2.dll

    ;;;;;;;;;;;;;
    ; Code Page ;
    ;;;;;;;;;;;;;

    ; The string variables used for titles and other text values in GTK+ are
    ; encoded in UTF-8 internally. A code page is needed so that PHP-GTK 'knows'
    ; which character set is being used, and can convert it to UTF-8 as necessary.

    ; If your environment uses UTF-8 already, you can set the codepage directive
    ; to UTF-8 to skip the conversions.

    ; The default codepage setting in PHP-GTK 2 is ISO-8859-1, but you can also
    ; use either OEM (e.g. 850) or Windows Code Pages (e.g. CP1250) here, so
    ; long as the encoding format you choose is capable of iconv conversion. See
    ; http://www.microsoft.com/globaldev/reference/cphome.mspx for a list of
    ; the code pages and character sets that are supported on Windows systems.

    php-gtk.codepage = CP1250

    why :-(

  5. kksou
    August 21, 2008 3:15am

    Hi Adel,

    Please refer to the following post:

    Problem in running GTKHtml example

    Follow the steps in there. Better still, read the entire thread so that you understand the rationale behind some of the steps.

    Let me know if it works.

    Regards,
    /kksou

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved