Great !
Em Sáb, 2006-02-18 às 12:41 -0800, Andrei Zmievski escreveu:
> Over the last couple of days I have implemented some additional
> functionality for GtkTreeModel derived stores (list and tree), which
> is not present in Gtk+. Basically, the tree model and rows can now
> function as iterators and arrays. Here is a sample script
> illustrating what can be done. There may be a bug or two there to
> chase down, and maybe a couple of small things to add, but so far it
> is pretty stable and, I think, cool.
>
> -Andrei
>
> <?php
>
> // A little demo of the new functionality
>
> // create our tree store
> $store = new GtkTreeStore(Gtk::TYPE_STRING, Gtk::TYPE_LONG);
>
> // add some rows
> $root = $store->append(null, array('root', 2));
> $child = $store->append($root, array('child1', 3));
> $child2 = $store->append($root, array('child2', 5));
> $child3 = $store->append($child, array('child1-1', 7));
> $child4 = $store->append($child, array('child1-2', 11));
> $child5 = $store->append($child, array('child1-3', 13));
> $child6 = $store->append($child4, array('child1-2-1', 17));
> print_tree($store);
>
> // get the first row in the tree model (all equivalent)
> $row = $store[0];
> $row = $store['0'];
> $row = $store[array(0)];
> $iter = $store->get_iter(0);
> $row = $store[$iter];
>
> // get the row with label child1-2
> $row = $store['0:0:1'];
> $row = $store[array(0, 0, 1)];
> $iter = $store->get_iter('0:0:1');
> $row = $store[$iter];
>
> // check if a row exists
> var_dump(isset($store['0:0'])); // yes, it does
> var_dump(isset($store['0:2'])); // no, it doesn't
>
> // how many top-level rows do we have?
> var_dump(count($store)); // 1, of course, but more if we had a list
> store
>
> // how many columns do we have?
> var_dump(count($store[0])); // 2
>
> // you can set new values in an existing row
> // here we change child1-3 row
> $store['0:0:2'] = array('CHILD1-3', 1313);
> print_tree($store);
>
> // you can also delete rows
> // let's delete child1-2-1
> unset($store['0:0:1:0']);
> print_tree($store);
>
> // you can use the store directly in foreach()
> // it will iterate over the top level rows (only 1 in tree model, of
> course)
> // each value is an object of class GtkTreeModelRow
> foreach ($store as $row)
> {
> // work on each row here
> }
>
> // GtkTreeModelRow is special too
> // you can obtain all kinds of info from it
> $row = $store['0:0:0']; // get child1-1 row
>
> // first of all, you can access it as an array
> // where index corresponds to a column
> var_dump($row[0], $row[1]); // outputs 'child1-1' and 7
>
> // you can also change column values through it
> $row[1] += 100; // here we added a 100 to its second column value
> var_dump($row[0], $row[1]); // outputs 'child1-1' and 107
>
> // you can even use list() with it
> list($label, $num) = $store['0:1'];
> var_dump($label, $num); // outputs 'child2' and 5
>
> // sometimes it's easier to access grab a specific column from a
> specific row
> var_dump($store['0:0:2'][1]); // outputs 1313
> // let's change it again
> $store['0:0:2'][1] *= 3;
> var_dump($store['0:0:2'][1]); // outputs 3939
>
> // what else can we get from GtkTreeModelRow..
> $row = $store['0:0:1']; // child1-1 row
> $model = $row->model; // well, that's obvious
> $next = $row->next; // its next sibling, in this case child1-2
> $parent = $row->parent; // its parent, in this case child1
> $iter = $row->iter; // the GtkTreeIter for the row
> $path = $row->path; // and the path for the row (as an array)
>
> // we can also get at the children of the row
> // the result is an object of type GtkTreeModelRowIter
> // which we can use in foreach()
> $row = $store['0:0'];
> foreach ($row->children() as $child) {
> // work on the child row
> $child[0] .= '!';
> }
> print_tree($store);
>
> // That's all folks! (for now)
>
>
>
> // helper function
> function print_tree($iter, $level = 0)
> {
> foreach ($iter as $row) {
> printf("%s%s (%d)\n", str_repeat(" ", $level), $row[0], $row
> [1]);
> print_tree($row->children(), $level+1);
> }
> }
>
> ?>
>
--
:: Pablo Dall'Oglio
:: www.dalloglio.net
--
PHP-GTK General Mailing List (
http://gtk.php.net/)
To unsubscribe, visit:
http://www.php.net/unsub.php