PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 132: How to populate form with popup list - Part 2?
Written by kksou   
Tuesday, 02 January 2007
Problem

In Part 1, you have set up a form with several fields. At the press of a button, a popup list appears that allow users to select from the list. In Part 1, user must select with arrow key and press Return.

Now you would like to allow users to select with double-click as shown below:

How to populate form with popup list - Part 2?


Solution
  • Set up the form and the dialog as described in Part 1.
  • To add the support for double-click, we use the button-press-event.

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   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
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   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
132   
133   
134   
135   
136   
137   
138   
139   
140   
141   
142   
143   
144   
145   
146   
147   
148   
149   
150   
151   
152   
153   
154   
155   
156   
157   
158   
159   
160   
161   
162   
163   
164   
165   
166   
167   
168   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Populate form with popup list - Part 2\n".
"   allow selection using double click");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$fields = array('Item number', 'Item Description', 'Unit price', 'Quantity');
$field_size = array(120, 200, 80, 80);
$entry = array(); // to store the text entries

$table = new GtkTable();
display_table($table, $fields, $field_size); // display the table

$vbox->pack_start($table);

function display_table($table, $fields, $field_size) {
    global $entry;
    $row = 0;
    foreach ($fields as $field) {
        $label = new GtkLabel("  $field: ");
        $alignment = new GtkAlignment(1, .5, 0, 0);
        $alignment->add($label);
        $table->attach($alignment, 0, 1, $row, $row+1, Gtk::FILL, Gtk::SHRINK, 0, 0);
        $entry[$row] = new GtkEntry();
        $alignment = new GtkAlignment(0, .5, 0, 0);
        $alignment->add($entry[$row]);
        $entry[$row]->set_size_request($field_size[$row], -1);

        if ($field=='Item number') {
            $hbox = new GtkHBox();
            // add a button to popup list after the label
            $button = new GtkButton('Select from list');

            $hbox->pack_start($alignment, 0, 0);
            $hbox->pack_start(new GtkLabel(' '), 0, 0);
            $hbox->pack_start($button, false);
            $widget = &$hbox;
        } else {
            $widget = $alignment;
        }

        $table->attach($widget, 1, 2, $row, $row+1);
        ++$row;
    }
    $button->connect('clicked', 'popup_list', $entry);
}

// create a submit button
$button = new GtkButton('Submit');
$button->set_size_request(60, 28);
$button->connect('clicked', 'on_click');
$row = count($fields);
$alignment = new GtkAlignment(0, 0.5, 0, 0);
$alignment->add($button);
$table->attach($alignment, 1, 2, $row, $row+1);

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

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

function popup_list($button, $entry) {
    $selection = prompt();
    if (count($selection)>0) {
        for ($i=0; $i<4; ++$i) {
            $entry[$i]->set_text($selection[$i]);
        }
    }
}

function prompt() {
    $prompt = new Prompt();
    $selection = array();
    if ($prompt->selected_iter!=null) {
        $model = $prompt->model;
        for ($i=0; $i<4; ++$i) {
            $selection[] = $model->get_value($prompt->selected_iter, $i);
        }
    }
    return $selection;
}

class Prompt{

    var $selected_iter = null;

    function Prompt() {
        $dialog = new GtkDialog('Select From List', null, Gtk::DIALOG_MODAL|Gtk::DIALOG_NO_SEPARATOR);
        $dialog->vbox->pack_start(new GtkLabel(), 0, 0);
        $dialog->vbox->pack_start(new GtkLabel('Select item with arrow keys and press Return.'), 0, 0);
        $dialog->vbox->pack_start(new GtkLabel('Or select by double-click the desired row.'), 0, 0);
        $dialog->vbox->pack_start(new GtkLabel(), 0, 0);


        // Set up a scroll window
        $scrolled_win = new GtkScrolledWindow();
        $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $dialog->vbox->pack_start($scrolled_win);

        // the 2D table
        $data = array(
        array('id0', 'item 19', 2, 3.1),
        array('id1', 'item 16', 20, 6.21),
        array('id2', 'item 13', 8, 9.36),
        array('id3', 'item 10', 11, 12.4),
        array('id4', 'item 7', 5, 15.5),
        array('id5', 'item 4', 17, 18.6),
        array('id6', 'item 3', 20, 21.73));

        $this->display_table($scrolled_win, $data);

        $this->dialog = $dialog;
        $dialog->connect('key-press-event', array($this, 'on_key'));
        $dialog->show_all();
        $dialog->run();
        $dialog->destroy();
    }

    function display_table($scrolled_win, $data) {
        // Creates the list store
        if (defined("GObject::TYPE_STRING")) {
            $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING,
                GObject::TYPE_LONG, GObject::TYPE_DOUBLE);
        } else {
            $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
                Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE);
        }
        $this->model = $model;
        $field_header = array('Item Number', 'Item Description', 'Qty', 'Price');
        $field_justification = array(0.5, 0.0, 0.5, 1.0);

        // Creates the view to display the list store
        $view = new GtkTreeView($model);
        $this->view = $view;
        $scrolled_win->add($view);
        $scrolled_win->set_size_request(300, 186);

        // Creates the columns
        for ($col=0; $col<count($field_header); ++$col) {
            $cell_renderer = new GtkCellRendererText();
            $cell_renderer->set_property("xalign", $field_justification[$col]);
            $column = new GtkTreeViewColumn($field_header[$col], $cell_renderer, 'text', $col);
            $column->set_alignment($field_justification[$col]);
            $column->set_sort_column_id($col);

            // set the header font and color
            $label = new GtkLabel($field_header[$col]);
            $label->modify_font(new PangoFontDescription("Arial Bold"));
            $label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000FF"));
            $column->set_widget($label);
            $label->show();

            // setup self-defined function to display alternate row color
  • 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

We make use of the code in Part 1.

What's new here:

  1. Register button-press-event on the treeview.
  2. Check if this is a double-click.
  3. Make sure there's a selection.
  4. Record the selected iter and destroy the dialog.

Related Links

User reviews

There are no user reviews yet.

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