<?phperror_reporting(E_ALL);class ListWidget extends GtkTreeView{ protected $model; protected $cell_render; protected $column; protected $selection; # an alias to $this->get_selection(); function __construct(){ parent::__construct(); $this->build(); } protected function build(){ $this->model = new GtkListStore(Gtk::TYPE_STRING); # could be Gtk::TYPE_PHP_VALUE http://gtk.php.net/manual/en/gtk.enum.type.php $this->column = new GtkTreeViewColumn(); $this->cell_renderer = new GtkCellRendererText(); $this->selection = $this->get_selection(); # avoid use of $this->get_selection() // Add the columns to the view $this->append_column($this->column); // Pack the cell-renderer $this->column->pack_start($this->cell_renderer, true); // link the renderer to the model $this->column->set_attributes($this->cell_renderer, 'text', 0); $this->set_model($this->model); $this->set_headers_visible(false); $this->selection->set_mode(Gtk::SELECTION_BROWSE); # force to Gtk::SELECTION_BROWSE mode (which is default) $this->connect('row-activated', array($this,'on_row_activated'), null); } public function clear(){ $this->model->clear(); } public function append($item){ $this->model->append(array($item)); } public function set_list($list){ $this->clear(); foreach($list as $item) $this->append($item); } # callback(GtkTreeView treeview, XXX UNKNOWN, GtkTreeViewColumn UNKNOWN); public function on_row_activated($treeview, $path, $column){ $iter = $this->model->get_iter($path); $value = $this->model->get_value($iter, 0); echo "on_row_activated($value)\n"; } # simulated a query on a database wich return a large array, and append it to $model list public function update_large_query($count=5000, $disconnect=true){ $start = microtime(true); if($disconnect) # disconnect model $this->set_model(null); $this->clear(); for($i=0 ; $i<$count ; $i++) $this->append($i); if($disconnect) # re-attach model to treeview; $this->set_model($this->model); $end = microtime(true); $time = $end - $start; echo "feed in $time\n"; }}$window = new GtkWindow();$window->connect_simple('destroy', array('Gtk','main_quit'));$window->set_size_request( 200, 600 ); # set window size$window->set_position(Gtk::WIN_POS_CENTER); # place window to screen center$window->set_title("Gtk List test"); $vbox = new GtkVbox(); $window->add($vbox); $button = new GtkButton('update'); $vbox->pack_start($button, false, false); $button->connect('clicked', 'update'); $button = new GtkButton('update disconnected'); $vbox->pack_start($button, false, false); $button->connect('clicked', 'update2'); $sw = new GtkScrolledWindow(); $sw->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $vbox->pack_start($sw); $list = new ListWidget(); $list->set_list(array('initial values,', 'clic "update" button', 1,2,3,4,5,4,3,2,1,0)); $sw->add($list);//display it$window->show_all();Gtk::main();function update(){ global $list; $list->update_large_query(5000, false);}function update2(){ global $list; $list->update_large_query(5000, true);}?>