PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 318: How to create a php gtk browser using gtkhtml - Part 2 - click on links?
Written by kksou   
Sunday, 02 September 2007
Problem

Part 2 of this series extends Part 1 to allow user to follow links in the browser as shown below:

How to create a php gtk browser using gtkhtml - Part 2 - click on links?


Solution
  • We use the signal 'link-clicked' to be notified when the user clicks on a link in the browser.
  • In the signal handler, we first put a copy of the url in GtkEntry, manually simulate a click on the 'Go' button. This will then load the content of the url in the browser.

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   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 250);
$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://gtk.php.net/'));
$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();
$scrolled_win->add($gtkhtml);

$gtkhtml->connect('url-requested', 'on_url_requested');
$gtkhtml->connect('link-clicked', 'on_link_clicked');  // note 1

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

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

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

function on_go_button($button, $url_entry, $gtkhtml) {
    $url = $url_entry->get_text();
    $html_text = file_get_contents($url);
    $gtkhtml->set_base($url);
  • 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

The above code is extended from the code in How to create a php gtk browser using gtkhtml - Part 1?

What's new here:

  1. Notify us when user clicks on links.
  2. Set the text of GtkEntry.
  3. Display the contents of the url.

Related Links

User reviews   Average user ratings:    5.0   (from 7 users)
  1. Luis Martinez Ulloa from Peru
    November 18, 2007 5:18am

    may be necessary watch the images embedded into the webpage, how can I do this? Thanks.

  2. kksou
    November 18, 2007 7:03am

    Please refer to this post. I'm still trying to figure out how to load the image in GtkHTML. Anyone has any clue...

  3. dave
    June 11, 2008 1:07pm

    Nice tutorial. Any update on the images issue?

  4. kksou
    June 11, 2008 7:16pm

    Hi Dave,

    The PHP-GTK development team didn't update the php_gtk_html2.dll when they released PHP-GTK v2.0 and v2.0.1. So the status remains the same.

    Regards,
    /kksou

  5. Luis Martinez Ulloa
    June 15, 2008 12:47am
    it's possible to watch images... I try it

    can be adressed by adding some image widgets replacing the image tag when appears; tricky? yes. Because needs to catch the image data and put it in place, all within code.

  6. dave
    June 16, 2008 7:49am
    Re: it's possible to watch images...

    Example?

  7. dave
    June 16, 2008 10:29am
    gtk2-perl

    gtk2-perl, Gtk2::Html2 does support images at the present time. Install from cvs and run the "mupzilla" example from the examples directory.

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

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved