Gtk2_VarDump
[ class tree: Gtk2_VarDump ] [ index: Gtk2_VarDump ] [ all elements ]

Source for file Tree.php

Documentation is available at Tree.php

  1. <?php
  2. require_once 'Gtk2/VarDump/ColTreeView.php';
  3.  
  4. /**
  5. *   Treeview for Gtk2_VarDump
  6. *
  7. *   @author Christian Weiske <cweiske@php.net>
  8. */
  9. class Gtk2_VarDump_Tree extends Gtk2_VarDump_ColTreeView
  10. {
  11.     /**
  12.     *   List view for plain values
  13.     *   @var Gtk2_VarDump_List 
  14.     */
  15.     protected $list = null;
  16.  
  17.  
  18.  
  19.     public function __construct()
  20.     {
  21.         parent::__construct();
  22.         $this->build();
  23.     }//public function __construct()
  24.  
  25.  
  26.  
  27.     /**
  28.     *   Create the GUI and set up all the things
  29.     */
  30.     protected function build()
  31.     {
  32.         //Node, Type, original variable, if the children have been checked for subchildren
  33.         $this->model = new GtkTreeStore(Gtk::TYPE_STRINGGtk::TYPE_STRINGGtk::TYPE_PHP_VALUEGtk::TYPE_BOOLEAN);
  34.         $this->set_model($this->model);
  35.         $selection $this->get_selection();
  36.         $selection->connect ('changed'              array($this'selectTreeRow'));
  37.         $this->connect      ('row-expanded'         array($this'expandTree'));
  38.         $this->connect_after('event'                array($this'clickedTree'));
  39.         $this->set_events   (Gdk::_2BUTTON_PRESS | Gdk::BUTTON_RELEASE);
  40.  
  41.         $this->createColumns(array('Node''Type'));
  42.     }//protected function build()
  43.  
  44.  
  45.  
  46.     /**
  47.     *   Set the list that displays the simple values
  48.     *
  49.     *   @param Gtk2_VarDump_List $list  List object
  50.     */
  51.     public function setList($list)
  52.     {
  53.         $this->list $list;
  54.     }//public function setList($list)
  55.  
  56.  
  57.  
  58.     /**
  59.     *   Set the variable (and their name) to display.
  60.     *
  61.     *   @param mixed  $variable   Variable to display
  62.     *   @param string $name       Name of the variable
  63.     */
  64.     public function setVariable($variable$name '')
  65.     {
  66.         $this->buildTree($variable$name);
  67.         $this->expand_row('0'false);//expand first row
  68.     }//public function setVariable($variable, $name = '')
  69.  
  70.  
  71.  
  72.     /**
  73.     *   Appends the given $variable to the tree on the right.
  74.     *   $name is used as title for the node, $parent is the parent node
  75.     *   to which the new node will be appended.
  76.     *
  77.     *   @param mixed        $variable   The variable to append
  78.     *   @param string       $name       The title for the variable (e.g. array key)
  79.     *   @param GtkTreeIter  $parent     The parent node to which the new node shall be appended
  80.     *   @param int          $nStop      After how many levels appending shall be stopped
  81.     */
  82.     protected function buildTree($variable$name$parent = null$nStop = 1)
  83.     {
  84.         $type gettype($variable);
  85.  
  86.         if ($type == 'array'{
  87.             $type .= '[' count($variable']';
  88.         else if ($type == 'object'{
  89.             $type trim(get_class($variable));
  90.         else {
  91.             //not an array and not an object
  92.             if ($this->list !== null{
  93.                 $this->list->setVariable($variable$name);
  94.             }
  95.             return;
  96.         }
  97.  
  98.         $node $this->model->append($parentarray($name$type$variablefalse));
  99.  
  100.         if ($nStop > 0{
  101.             $this->appendChildren($variable$node$nStop--);
  102.         }
  103.  
  104.         if ($parent === null{
  105.             $this->get_selection()->select_path('0');
  106.         }
  107.     }//protected function buildTree($variable, $name, $parent = null, $nStop = 1)
  108.  
  109.  
  110.  
  111.     /**
  112.     *   Appends all the children of the given variable to $node
  113.     *
  114.     *   @param mixed        $variable   The variable, whose children shall be appended
  115.     *   @param GtkTreeIter  $node       The parent node to which the new ones shall be appended
  116.     *   @param int          $nStop      After how many levels appending shall be stopped
  117.     */
  118.     protected function appendChildren($variable$node$nStop = 1)
  119.     {
  120.         $type gettype($variable);
  121.         switch ($type{
  122.             case 'object':
  123.                 if (class_exists(get_class($variable))) {
  124.                     $arKeys array_keys(get_object_vars($variable));
  125.                 else {
  126.                     //Class not available in PHP -> no keys
  127.                     $arKeys = array();
  128.                 }
  129.                 break;
  130.             case 'array':
  131.                 $arKeys array_keys($variable);
  132.                 break;
  133.             default:
  134.                 return;
  135.         }
  136.  
  137.         foreach ($arKeys as $key{
  138.             $value ($type == 'array'$variable[$key$variable->$key;
  139.             switch (gettype($value)) {
  140.                 case 'object':
  141.                 case 'array':
  142.                     $this->buildTree($value$key$node$nStop - 1);
  143.                     break;
  144.                 default:
  145.                     //other types aren't displayed in the tree
  146.                     break;
  147.             }
  148.         }
  149.     }//protected function appendChildren($variable, $node, $nStop = 1)
  150.  
  151.  
  152.  
  153.     /**
  154.     *   Called whenever a tree row is expanded.
  155.     *   It is used to load the children of the node's children
  156.     *   if they haven't been loaded yet.
  157.     *
  158.     *   @param  GtkTreeView $tree       The tree on which the signal has been emitted
  159.     *   @param  GtkTreeIter $iterator   The node which has been expanded
  160.     */
  161.     public function expandTree($tree$iterator)
  162.     {
  163.         if ($this->model->get_value($iterator3)) {
  164.             //already checked
  165.             return;
  166.         }
  167.  
  168.         //check if children have subchildren and load them
  169.         $child $this->model->iter_children($iterator);
  170.         while ($child !== null{
  171.             $this->appendChildren(
  172.                 $this->model->get_value($child2),
  173.                 $child
  174.             );
  175.             //get the next child
  176.             $child $this->model->iter_next($child);
  177.         }
  178.         $this->model->set($iterator3true);
  179.     }//public function expandTree($tree, $iterator)
  180.  
  181.  
  182.  
  183.     /**
  184.     *   Called whenever a row on the left tree has been selected.
  185.     *   It is used to show the children of the selected variable
  186.     *   on the right list.
  187.     *
  188.     *   @param array    $selection  Array consisting of the model and the currently selected node (GtkTreeIter)
  189.     */
  190.     public function selectTreeRow($selection)
  191.     {
  192.         list($model$iter$selection->get_selected();
  193.         if ($iter === null{
  194.             return;
  195.         }
  196.         $variable $model->get_value($iter2);
  197.         if ($this->list !== null{
  198.             $this->list->setVariable($variable);
  199.         }
  200.     }//public function selectTreeRow($selection)
  201.  
  202.  
  203.  
  204.     /**
  205.     *   The tree has been clicked, and the currently selected row
  206.     *   will be expanded or collapsed, depending which mouse button has
  207.     *   been clicked.
  208.     *   The left mouse button will expand the node,
  209.     *   the right mouse button will collapse it.
  210.     *   Middle mouse button and a double-clicked left button will
  211.     *    expand all children but *only* if they have been expanded
  212.     *    before - it would be too dangerous to expand all children to
  213.     *    any depth recursively if there are loops.
  214.     *
  215.     *   @param GtkTreeView  $tree   The tree which has been clicked
  216.     *   @param GdkEvent     $event  The event data for the click event
  217.     */
  218.     public function clickedTree($tree$event)
  219.     {
  220.         if ($event->type !== Gdk::_2BUTTON_PRESS && $event->type !== Gdk::BUTTON_RELEASE{
  221.             return;
  222.         }
  223.  
  224.         list($model$arSelected$tree->get_selection()->get_selected_rows();
  225.         if ($arSelected === null{
  226.             return;
  227.         }
  228.  
  229.         $path implode(':'$arSelected[0]);
  230.  
  231.         if ($event->button == 1{
  232.             //left mouse button
  233.             //If double-click: expand all rows down
  234.             $tree->expand_row($path$event->type == Gdk::_2BUTTON_PRESS);
  235.         else if ($event->button == 2{
  236.             //middle mouse button - expand all
  237.             $tree->expand_row($pathtrue);
  238.         else if ($event->button == 3{
  239.             //right mouse button
  240.             $tree->collapse_row($path);
  241.         }
  242.     }//public function clickedTree($tree, $event)
  243.  
  244. }//class Gtk2_VarDump_Tree extends Gtk2_VarDump_ColTreeView
  245. ?>

Documentation generated on Mon, 11 Mar 2019 14:42:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.