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

Documentation generated on Mon, 11 Mar 2019 15:19:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.