325. How to align GtkEntry fields - Part 3 - using gtkhtml?

Problem

Hope you have seen the following articles:

Combining the techniques from the above two articles, did you realize that we can actually layout and align PHP-GTK widgets using the good old HTML which a lot of us are so familiar with?

I will come out with a couple of examples to illustrate this.

In this first example, I use the example from the article How to align GtkEntry fields - Part 2?

Please first run the example from that article so that you know what we're trying to achieve. We will achieve the same thing in this example, except that we will use GtkHTML to do the layout and alignment as shown below:

How to align GtkEntry fields - Part 3 - using gtkhtml?


Solution

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   
44   
45   
46   
47   
48   
49   
51   
52   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
77   
78   
79   
80   
82   
83   
84   
85   
86   
88   
90   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

$window->realize();
$org_bg = $window->get_style()->bg[Gtk::STATE_NORMAL];

// init entry fields
$fields = array();
$object_count = 0;
$field_size = array('itemnum'=>120, 'desc'=>200,
    'price'=>80, 'qty'=>80);

// setup GtkHTML
$gtkhtml = new GtkHTML();
$vbox->pack_start($gtkhtml, 0);

$html_text = "<p align=center><b><font size=+1 color=blue>
Align GtkEntry Fields<br>Part 3 - using GtkHTML</font></b></p>
<table border=0>
<tr>
    <td align=right>Item number:</td>
    <td><OBJECT classid=entry_itemnum></OBJECT></td>
</tr>
<tr>
    <td align=right>Item description:</td>
    <td><OBJECT classid=entry_desc></OBJECT></td>
</tr>
<tr>
    <td align=right>Unit price:</td>
    <td><OBJECT classid=entry_price></OBJECT></td>
</tr>
<tr>
    <td align=right>Quantity:</td>
    <td><OBJECT classid=entry_qty></OBJECT></td>
</tr>
<tr>
    <td align=right></td>
    <td><OBJECT classid=button_Submit></OBJECT></td>
</tr>
</table>
";

$gtkhtml->connect('object-requested', 'on_object_requested');

$gtkhtml->load_from_string($html_text);
$gtkhtml->modify_base(Gtk::STATE_NORMAL, $org_bg);

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

function on_object_requested($gtkhtml, $gtkhtmlembedded) {
    // the following is supposed to return the classid of the object
    // but it doesn't seem to work...
    $object_id = $gtkhtmlembedded->classid;

    // until we find out how to get the object_id
    // we will just do a hardcode mapping of the the object_id
    global $object_count;
    $object_id_mapping = array('entry_itemnum', 'entry_desc', 
    'entry_price', 'entry_qty', 'button_Submit'); // note 1
    $object_id = $object_id_mapping[$object_count];
    ++$object_count;

    global $fields, $field_size;
    if (!preg_match('/([a-zA-Z]+)_(.+)/', $object_id, $matches)) return;
    $widget_type = $matches[1]; // note 2
    $widget_id = $matches[2]; // note 3

    switch($widget_type) { // note 4

        case 'entry':
            $entry = $fields[$widget_id] = new GtkEntry();
            $width = $field_size[$widget_id];
            $entry->set_size_request($width, -1);
            $gtkhtmlembedded->add($entry);
            $entry->show();
            break;

        case 'button':
            $button = new GtkButton($widget_id);
            $button->connect('clicked', 'on_submit');
            $gtkhtmlembedded->add($button);
            $button->show();
            break;
    }

    return true;
}

function on_submit($button) {
    global $fields;
    $i = 0;
    foreach($fields as $field) {
        echo "field[$i]: ".$field->get_text()."\n";
        ++$i;
    }
}

?>

Output

As shown above.
 

Explanation

The above code is based on How to embed gtk widgets within html text using gtkhtml?

It achieves the same result as How to align GtkEntry fields - Part 2?

What's new here:

  1. As explained in How to embed gtk widgets within html text using gtkhtml?, I still don't know how to retrieve the object ID in the signal handler. So in the meantime, I just do a hardcode mapping to get the object ID. If you know how to get the object ID, you can comment out these few lines. (Yes, please drop me a note to let me know how to do that too...!)
  2. Get the widget type.
  3. Get the widget ID.
  4. Set up the widgets respectively.

Related Links

Add comment


Security code
Refresh