Source for file Tree.php
Documentation is available at Tree.php
require_once 'Gtk2/VarDump/ColTreeView.php';
* Treeview for Gtk2_VarDump
* @author Christian Weiske <cweiske@php.net>
class Gtk2_VarDump_Tree extends Gtk2_VarDump_ColTreeView
* List view for plain values
public function __construct ()
}//public function __construct()
* Create the GUI and set up all the things
protected function build ()
//Node, Type, original variable, if the children have been checked for subchildren
$this->model = new GtkTreeStore (
Gobject ::TYPE_STRING , Gobject ::TYPE_STRING ,
Gobject ::TYPE_PHP_VALUE , Gobject ::TYPE_BOOLEAN
$this->set_model ($this->model);
$selection = $this->get_selection ();
$selection->connect ('changed' , array ($this, 'selectTreeRow'));
$this->connect ('row-expanded' , array ($this, 'expandTree'));
$this->connect_after ('event' , array ($this, 'clickedTree'));
$this->set_events (Gdk ::_2BUTTON_PRESS | Gdk ::BUTTON_RELEASE );
$this->createColumns (array ('Node', 'Type'));
}//protected function build()
* Set the list that displays the simple values
* @param Gtk2_VarDump_List $list List object
public function setList ($list)
}//public function setList($list)
* Set the variable (and their name) to display.
* @param mixed $variable Variable to display
* @param string $name Name of the variable
public function setVariable ($variable, $name = '')
$this->buildTree ($variable, $name);
$this->expand_row ('0', false ); //expand first row
}//public function setVariable($variable, $name = '')
* Appends the given $variable to the tree on the right.
* $name is used as title for the node, $parent is the parent node
* to which the new node will be appended.
* @param mixed $variable The variable to append
* @param string $name The title for the variable (e.g. array key)
* @param GtkTreeIter $parent The parent node to which the new node shall be appended
* @param int $nStop After how many levels appending shall be stopped
protected function buildTree ($variable, $name, $parent = null , $nStop = 1 )
$type .= '[' . count($variable) . ']';
} else if ($type == 'object') {
//not an array and not an object
if ($this->list !== null ) {
$this->list->setVariable ($variable, $name);
$node = $this->model->append ($parent, array ($name, $type, $variable, false ));
$this->appendChildren ($variable, $node, $nStop-- );
$this->get_selection ()->select_path ('0');
}//protected function buildTree($variable, $name, $parent = null, $nStop = 1)
* Appends all the children of the given variable to $node
* @param mixed $variable The variable, whose children shall be appended
* @param GtkTreeIter $node The parent node to which the new ones shall be appended
* @param int $nStop After how many levels appending shall be stopped
protected function appendChildren ($variable, $node, $nStop = 1 )
//Class not available in PHP -> no keys
foreach ($arKeys as $key) {
$value = ($type == 'array') ? $variable[$key] : $variable->$key;
$this->buildTree ($value, $key, $node, $nStop - 1 );
//other types aren't displayed in the tree
}//protected function appendChildren($variable, $node, $nStop = 1)
* Called whenever a tree row is expanded.
* It is used to load the children of the node's children
* if they haven't been loaded yet.
* @param GtkTreeView $tree The tree on which the signal has been emitted
* @param GtkTreeIter $iterator The node which has been expanded
public function expandTree ($tree, $iterator)
if ($this->model->get_value ($iterator, 3 )) {
//check if children have subchildren and load them
$child = $this->model->iter_children ($iterator);
while ($child !== null ) {
$this->model->get_value ($child, 2 ),
$child = $this->model->iter_next ($child);
$this->model->set ($iterator, 3 , true );
}//public function expandTree($tree, $iterator)
* Called whenever a row on the left tree has been selected.
* It is used to show the children of the selected variable
* @param array $selection Array consisting of the model and the currently selected node (GtkTreeIter)
public function selectTreeRow ($selection)
list ($model, $iter) = $selection->get_selected ();
$variable = $model->get_value ($iter, 2 );
if ($this->list !== null ) {
$this->list->setVariable ($variable);
}//public function selectTreeRow($selection)
* The tree has been clicked, and the currently selected row
* will be expanded or collapsed, depending which mouse button has
* The left mouse button will expand the node,
* the right mouse button will collapse it.
* Middle mouse button and a double-clicked left button will
* expand all children but *only* if they have been expanded
* before - it would be too dangerous to expand all children to
* any depth recursively if there are loops.
* @param GtkTreeView $tree The tree which has been clicked
* @param GdkEvent $event The event data for the click event
public function clickedTree ($tree, $event)
if ($event->type !== Gdk ::_2BUTTON_PRESS && $event->type !== Gdk ::BUTTON_RELEASE ) {
list ($model, $arSelected) = $tree->get_selection ()->get_selected_rows ();
if ($arSelected === null ) {
$path = implode(':', $arSelected[0 ]);
if ($event->button == 1 ) {
//If double-click: expand all rows down
$tree->expand_row ($path, $event->type == Gdk ::_2BUTTON_PRESS );
} else if ($event->button == 2 ) {
//middle mouse button - expand all
$tree->expand_row ($path, true );
} else if ($event->button == 3 ) {
$tree->collapse_row ($path);
}//public function clickedTree($tree, $event)
}//class Gtk2_VarDump_Tree extends Gtk2_VarDump_ColTreeView
Documentation generated on Mon, 11 Mar 2019 15:19:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|