Re: Custom tree models
| From: | Andrei Zmievski | Date: | Tue, 10 Oct 2006 01:25:01 +0000 |
| Subject: | Re: Custom tree models | ||
| References: | 1 | Groups: | php.gtk.dev |
| Request: | Send a blank email to php-gtk-dev+get-3467@lists.php.net to get a copy of this message | ||
No comments?
-Andrei
On Oct 5, 2006, at 9:27 PM, Andrei Zmievski wrote:
I've just committed support for custom tree models. The example script is pretty short [0]. It pretty much shows how to load up a text file and serve it up as model. Note that your iterators pretty much have to be objects, since those are returned by handle (reference) and thus represent a single value. Due to peculiarities of Gtk+ and PHP, any other type will not be freed automatically. In which case the return values will keep accumulating in memory and will be freed only on exit. This means that you can't do something like this for on_get_iter(), iter_next(), iter_parent(), iter_children(), iter_nth_child(): return 5; or even: return $a; // where $a is not an object Hope this helps, -Andrei [0] <?php error_reporting(E_ALL); class MyIter {public $idx = null;} class FileModel extends PhpGtkCustomTreeModel {private $text = array(); private $iter = null;function __construct($filename) { parent::__construct(); if (is_file($filename)) { $this->text = file($filename); $this->iter = new MyIter; } else { throw new Exception("Could not open $filename"); } }function on_get_flags() { print "on_get_flags\n"; return Gtk::TREE_MODEL_LIST_ONLY; }function on_get_n_columns() { print "on_get_n_columns\n"; return 1; }function on_get_column_type($index) { print "on_get_column_type\n"; return Gtk::TYPE_STRING; }function on_get_path($node) { exit; print "on_get_path\n"; return $node; }function on_get_iter($path) { print "on_get_iter\n"; assert(count($path) == 1); $this->iter->idx = $path[0]; return $this->iter; }function on_get_value($iter, $n) { print "on_get_value\n"; assert($n == 0); return $this->text[$this->iter->idx]; }function on_iter_next($iter) { print "on_iter_next\n"; $num = count($this->text); if ($this->iter->idx == $num-1) { return null; } else { $this->iter->idx++; return $this->iter; } }function on_iter_children($iter) { return null; }function on_iter_has_child($iter) { return false; }function on_iter_n_children($iter) { return 0; }function on_iter_nth_child($iter, $n) { return null; }function on_iter_parent($iter) { return null; }} if ($argc < 2) {die ("Please supply name of a text file\n");} $w = new GtkWindow(); $w->set_default_size(500, 400); $w->connect_simple('destroy', array('gtk', 'main_quit')); $sc = new GtkScrolledWindow(); $sc->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $w->add($sc); $m = new FileModel($argv[1]); $tree_view = new GtkTreeView($m); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("example", $cell, 'text', 0); $tree_view->append_column($col); $sc->add($tree_view); $w->show_all(); gtk::main(); --PHP-GTK Development Mailing List (http://gtk.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php