474. How to display html text using gtkhtml - Part 2?

Problem

Mario Staas emailed me this tip on running PHP-GTK apps that uses GtkHTML.

As pointed out in the article How to display html text using gtkhtml? to run scripts with GtkHTML, you have to type the following in the command window:

gconfd-2.exe | php script.php

Mario found a much easier and elegant way to run these GtkHTML scripts as shown below:

How to display html text using gtkhtml - Part 2?


Solution

  1. Just before you use GtkHTML, add this line:
  2. $handle = popen ('gconfd-2.exe', 'r');

  3. When you're done with GtkHTML, add one more line:
  4. pclose($handle);

  5. With these two lines in place, you can now run these scripts just like a normal php-gtk script without the need to type gconfd-2.exe | php script.php in the command window.

Important Note:

  • This only works for PHP-GTK2 compliled with the additional library GtkHTML.
  • You will most likely still see the warning (php.exe:5348): Gdk-WARNING **: gdkselection-win32.c:1068: OpenClipboard failed: Invalid window handle.. Not sure if this is a bug. The script seems to run ok, though.
  • On my winxp, usually the first run doesn't show anything. Press CTRK-C and run again. It should work after that.


Sample Code

1   
2   
3   
4   
5   
6   
7   
12   
13   
18   
19   
20   
21   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
46   
47   
48   
49   
50   
51   
52   
<?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());

$handle = popen ('c:\php-gtk2\gconfd-2.exe', 'r'); // note 1


$html = new GtkHTML();
$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);

pclose($handle);// note 2

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

Output

As shown above.
 

Explanation

  1. Add this line just before you start using GtkHTML. Please replace 'c:\php-gtk2\gconfd-2.exe' with the path to your gconfd-2.exe
  2. Add this line when you're done with GtkHTML.

Related Links

Add comment


Security code
Refresh