Problem
With the new PHP-GTK2 beta release, it's now a breeze to display html text with the use of GtkHTML as shown below:
Solution
Using GtkHTML is easy.
- We execute the built-in GtkHTML command with $gtkhtml->command($command_name);?>
- Then display the html text with $html->load_from_string($html_text)
However, if this is the first time you use GtkHtml, please 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 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 5 6 7 13 14 15 16 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 | <?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()); $html = new GtkHTML(); // note 1 $vbox->pack_start($html); $html_text = "<h1>GtkHTML Demo</h1> <p><i>hello <font color=blue>world</font></i>, <b>gtkhtml!</b></p> <table border=4> <tr><th>A</th><th>B</th></tr> <tr><td>a1.1</td><td bgcolor=#BAFFBF>b1.2</td></tr> <tr><td bgcolor=#BAFFBF>a2.1</td><td>b2.2</td></tr> </table> <ul> <li>item 1</li> <li>item 2</li> <li>item 3</li> </ul> "; $html->load_from_string($html_text); // note 2 $window->show_all(); Gtk::main(); ?> |
Output
As shown above.Explanation
- Create a new GtkHTML.
- Display the html text.
Note
Understand this one first. We will try to build a browser using GtkHTML in the next article...
Related Links
- How to display html text using gtkhtml - Part 2?
- How to create a php gtk browser using gtkhtml - Part 1?
- How to create a php gtk browser using gtkhtml - Part 2 - click on links?
- How to create a php gtk browser using gtkhtml - Part 3 - zoom in and out?
- How to create an html editor using gtkhtml - Part 1?
- How to create an html editor using gtkhtml - Part 2?
Read more...