<?phperror_reporting(E_ALL);/** * * Marc Quinton - 2006 december. * * PhpGtkCustomTreeModel sample examples ; * * PhpGtkCustomTreeModel implements a customisable GtkTreeView model. * * a good tutorial about this in python : * http://www.moeraki.com/pygtktutorial/pygtk2tutorial/sec-GenericTreeModel.html * * * here is a list of available methods for PhpGtkCustomTreeModel : * * on_get_flags(); * on_get_n_columns(); * on_get_column_type($index); * on_get_path($node); * on_get_iter($path); * on_iter_children($iter); * on_iter_has_child($iter); * on_iter_n_children($iter); * on_iter_nth_child($iter, $n); * on_iter_parent($iter); * on_get_value($iter, $colum); * on_iter_next($iter) * * * * this file contains : * * - MyIter - a basic iterator class * - FlatTextModel - implements all methods needed for a flat list textual model (based on Andrei example) * - FlatTextRandomListModel - generate a list of random numbers * - FlatTextArrayModel - give an array list in contructor to build model * - CrazyFlatTextRandomListModel - a really random list model * - OrderedListModel - an ordered list * - ReverseOrderedListModel - some model in reverse ordered * * class hierarchy tree : * * PhpGtkCustomTreeModel * FlatTextModel * FlatTextRandomListModel * FlatTextRandomListModel * FlatTextArrayModel * CrazyFlatTextRandomListModel * OrderedListModel * ReverseOrderedListModel * * TODO : work on non-flats models. * */class MyIter { public $idx = null;}/** * an abtract class that implements basic methods for a flat (text) list. * */abstract class FlatTextModel extends PhpGtkCustomTreeModel{ protected $iter = null; protected $debug; function __construct() { parent::__construct(); $this->debug=false; } abstract public function on_get_value($iter, $n); abstract public function on_iter_next($iter); # can handle only List feature. public function on_get_flags() { return Gtk::TREE_MODEL_LIST_ONLY; } # number of colums. public function on_get_n_columns() { return 1; } # only one column of type string. public function on_get_column_type($index) { return Gtk::TYPE_STRING; } public function on_get_path($node) { exit; $this->debug("on_get_path\n"); return $node; } # flat list public function on_get_iter($path) { $this->debug("on_get_iter\n"); assert(count($path) == 1); $this->iter->idx = $path[0]; return $this->iter; } # flat list. public function on_iter_children($iter) { return null; } ## flat list. public function on_iter_has_child($iter) { return false; } # flat list. public function on_iter_n_children($iter) { return 0; } # flat list. public function on_iter_nth_child($iter, $n) { return null; } # flat list. public function on_iter_parent($iter) { return null; } protected function debug($txt){ if($this->debug) echo $txt; }}/** * FlatTextRandomListModel * * Construct a flat list build internaly ; constructor receive array size. * */class FlatTextRandomListModel extends FlatTextModel { protected $data; protected $count; function __construct($count, $min=0, $max=100) { parent::__construct(); $this->build_array($count, $min, $max); } protected function build_array($count, $min, $max){ $this->data = array(); for($i=0 ; $i<$count ; $i++) $this->data[] = rand($min, $max); } public function on_get_value($iter, $n) { $this->debug("on_get_value\n"); assert($n == 0); return $this->data[$this->iter->idx]; } public function on_iter_next($iter) { $this->debug("on_iter_next\n"); $num = count($this->data); if ($this->iter->idx == $num-1) { return null; } else { $this->iter->idx++; return $this->iter; } }}/** * FlatTextArrayModel * * Construct a flat list given by an array (in constructor) * */class FlatTextArrayModel extends FlatTextModel { protected $data; function __construct(&$array) { parent::__construct(); if (!is_array($array)) { throw new Exception("construction error : given array is not an array"); } $this->data = &$array; } function on_get_value($iter, $n) { $this->debug("on_get_value\n"); assert($n == 0); return $this->data[$this->iter->idx]; } function on_iter_next($iter) { $this->debug("on_iter_next\n"); $num = count($this->data); if ($this->iter->idx == $num-1) { return null; } else { $this->iter->idx++; return $this->iter; } }}/** * CrazyFlatTextRandomListModel * * return a fully random generator iterator ; * with user interactions, cell content change for ever in a silly and * random way. * */class CrazyFlatTextRandomListModel extends FlatTextModel { # protected $data; do not store data ; return a random number each time. protected $min; protected $max; protected $count; function __construct($count, $min=0, $max=100) { parent::__construct(); $this->count = $count; $this->min = $min; $this->max = $max; } public function on_get_value($iter, $n) { $this->debug("on_get_value\n"); assert($n == 0); # our nice random generator. return rand($this->min, $this->max); } public function on_iter_next($iter) { $this->debug("on_iter_next\n"); if ($this->iter->idx == ($this->count-1)) { return null; } else { $this->iter->idx++; return $this->iter; } }}/** * a simple Ordered list model. * */class OrderedListModel extends FlatTextModel { protected $count; function __construct($count) { parent::__construct(); $this->count = $count; } public function on_get_value($iter, $n) { $this->debug("on_get_value\n"); assert($n == 0); return $iter->idx; } public function on_iter_next($iter) { $this->debug("on_iter_next\n"); if ($this->iter->idx == ($this->count-1)) { return null; } else { $this->iter->idx++; return $this->iter; } }}/** * a simple Ordered list model in reverse order. * */class ReverseOrderedListModel extends OrderedListModel { public function on_get_value($iter, $n) { $this->debug("on_get_value\n"); assert($n == 0); return $this->count - $iter->idx; }}if ($argc < 2) { die ("Please supply a test number\n");}switch($argv[1]){case 1: # flat list. $array_spec = array(1,2,3,4,5,6,7,8,9); $model = new FlatTextArrayModel($array_spec); break;case 2: # flat list - random numbers $count = 2000; $array_spec = array(); for($i=0 ; $i<$count ; $i++) $array_spec[] = rand(0,100); $model = new FlatTextArrayModel($array_spec); break;case 3: $model = new FlatTextRandomListModel(200); break;case 4: $model = new CrazyFlatTextRandomListModel(200); break;case 5: $model = new OrderedListModel(200); break;case 6: $model = new ReverseOrderedListModel(200); break;default: die ("test numbers are from 1 to 6\n");}$w = new GtkWindow();$w->set_default_size(100, 250);$w->connect_simple('destroy', array('gtk', 'main_quit')); $sc = new GtkScrolledWindow(); $sc->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $w->add($sc); $tree_view = new GtkTreeView($model); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("example", $cell, 'text', 0); $tree_view->append_column($col); $sc->add($tree_view); $w->show_all();gtk::main();?>