Problem
Part 2 of this series extends Part 1 to allow user to follow links in the browser as shown below:
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 addphp-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 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 23 27 29 36 37 38 39 40 41 42 43 44 45 46 47 48 49 51 52 53 54 55 56 57 59 66 67 68 69 72 73 74 75 76 77 78 | <?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); $gtkhtml->load_from_string($html_text); } function on_url_requested($gtkhtml, $url, $stream) { echo "image url = $url\n"; // load the image } function on_link_clicked($gtkhtml, $url) { global $url_entry, $go_button; $url_entry->set_text($url); // note 2 $go_button->clicked(); // note 3 } ?> |
Output
As shown above.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:
- Notify us when user clicks on links.
- Set the text of GtkEntry.
- Display the contents of the url.
Read more...