<?php
if( !extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
function on_click($item, $event, $i) {
echo $item->get_data($i)."\n";
flush();
}
function on_key($item, $i) {
echo $item->get_data($i)."\n";
flush();
}
$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->connect_object("destroy", array("gtk",
"main_quit"));
$combo = &new GtkCombo();
/* The GtkEntry and GtkList are accessible through the combo's properties */
$entry = $combo->entry;
$entry->set_text('Choose some fruit');
$list = $combo->list;
$list->set_selection_mode(GTK_SELECTION_SINGLE);
$fruit = array('apples', 'bananas', 'cherries', 'damsons', 'eggplants',
'figs', 'grapes');
for($i = 0; $i < count($fruit); $i++) {
$item = &new GtkListItem();
/* You can put pretty much anything into a GtkListItem */
$box = &new GtkHBox();
$arrow = &new GtkArrow(GTK_ARROW_RIGHT, GTK_SHADOW_OUT);
$box->pack_start($arrow, false, false, 5);
$label = &new GtkLabel('Item '.($i+1));
$box->pack_start($label, false, false, 10);
$item->add($box);
$combo->set_item_string($item, "You chose $fruit[$i]");
$data = $fruit[$i];
/* This data will be carried with the $item. The key here is $i. */
$item->set_data($i, $data);
/* Two separate signals to get around the 'select' problem. */
$item->connect('button-press-event', 'on_click', $i);
$item->connect('toggle', 'on_key', $i);
$list->add($item);
$item->show_all();
}
$window->add($combo);
$window->show_all();
gtk::main();
?>
|