|
Problem You have a form with several fields. At the press of a button, you would like a popup list to appear. Users can select from the list. On pressing the Return key, the fields will automatically be populated with data of the selected item as shown below:

Solution
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
| <?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 1"); $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') { // note 1
$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); // note 2
}
// 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(); // note 2
if (count($selection)>0) { for ($i=0; $i<4; ++$i) { $entry[$i]->set_text($selection[$i]); // note 10
} } }
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); // note 9
} } return $selection; // note 9
}
class Prompt{
var $selected_iter = null; // note 3
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(), 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')); // note 4
$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);
|
- 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
- Add the "Select from list" button after the first field.
- Popup the dialog to display the list.
- This keeps track of user's selection in the form of iter. If there's no selection, it will be null.
- Register 'key-press-event' on the dialog so that we can know when the user press 'Enter'.
- Just close the dialog box if the user presses ESC.
- If the key is not ESC and also not a Return, just return
FALSE to let php-gtk use the default handler.
- This is the case in which the user did not highlight any row but press a Return. To avoid making this program too complicated, I just output a message in the command line. In real life situation, you may want to popup an alert box using the technique as outlined in How to display a popup alert for required fields - Part 1?
- User has made a selection and press the Return key. We record the selected iter and destroy the dialog.
- We transfer the selection to an array, and return the array.
- If there's a selection, we populate the fields with the data from the selected row.
Note
Let's get this up and running first. In the next article, we will allow user to select with double-click.
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. |