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

Source for file VarDump.php

Documentation is available at VarDump.php

  1. <?php
  2. /**
  3. *   Simple class for viewing PHP variables a var_dump() way
  4. *    in PHP-Gtk - reloaded for PHP-Gtk2.
  5. *
  6. *   It displays arrays and objects in a tree with all their
  7. *    children and subchildren and subsubchildren and ...
  8. *
  9. *   The class is memory-saving as it loads only the children
  10. *    which are currently visible. If the user expands a row,
  11. *    the next children will be loaded.
  12. *
  13. *   The tree has a small convenience feature: Left-click a row,
  14. *    and it will be expanded. Right-click it, and it collapses.
  15. *   Double-click or middle-click it, and all rows below the
  16. *    current one will be expanded. They will be expanded "all" only
  17. *    if they have been expanded before, as loading them recursively
  18. *    is very dangerous (if there are loops).
  19. *
  20. *   Note that VarDump opens its own Gtk::main()-Loop, so your
  21. *    own program will stop executing until the VarDump window
  22. *    is closed.
  23. *
  24. *   Usage:
  25. *   require_once('Gtk2/VarDump.php');
  26. *   $ar = new array(1, 2, 3, 4, 'key' => array('this','is','cool');
  27. *   new Gtk2_VarDump($ar);
  28. *
  29. *   Layout:
  30. *   +--[Window title]------------------------------------------------+
  31. *   |+--------------------------+/+---------------------------------+|
  32. *   || Node      | Type         ^\| Key    | Type      | Value      ^|
  33. *   ||                          |/|                                 ||
  34. *   || Left tree with objects   |\|  Right list with simple values  ||
  35. *   ||   and arrays             |/|     (int,float,string,...)      ||
  36. *   ||                          v\|                                 v|
  37. *   |+--------------------------+/+---------------------------------+|
  38. *   |                             [OK]                               |
  39. *   +----------------------------------------------------------------+
  40. *
  41. *   @author Christian Weiske <cweiske@php.net>
  42. */
  43. class Gtk2_VarDump extends GtkWindow
  44. {
  45.     /**
  46.     *   The tree on the left side of the window.
  47.     *   @var GtkTreeView 
  48.     */
  49.     protected $trTree    = null;
  50.  
  51.     /**
  52.     *   List on the right side of the window.
  53.     *   @var GtkTreeView 
  54.     */
  55.     protected $trValues  = null;
  56.  
  57.     /**
  58.     *   Model (data store) for the tree on the left.
  59.     *   @var GtkTreeStore 
  60.     */
  61.     protected $modTree   = null;
  62.  
  63.     /**
  64.     *   Model (data store) for the list on the right.
  65.     *   @var GtkListStore 
  66.     */
  67.     protected $modValues = null;
  68.  
  69.  
  70.  
  71.     /**
  72.     *   Create a new Gtk2_VarDump window and keep it displayed
  73.     *   in its own Gtk::main()-loop.
  74.     *   This main loop is stopped as soon the window is closed
  75.     *
  76.     *   @param mixed    $variable   The variable to inspect
  77.     *   @param string   $title      The title for the window and the variable
  78.     */
  79.     public function __construct($variable$title 'Gtk2_VarDump')
  80.     {
  81.         parent::__construct();
  82.         $this->buildDialog($title);
  83.         $this->buildTree($variable$title);
  84.         $this->trTree->expand_row('0'false);//expand first row
  85.         $this->show_all();
  86.         Gtk::main();
  87.     }//public function __construct($variable, $title = 'Gtk2_VarDump')
  88.  
  89.  
  90.  
  91.     /**
  92.     *   Creates the dialog content, loads the tree models and so
  93.     *
  94.     *   @param  string  $title  The title for the window
  95.     */
  96.     protected function buildDialog($title)
  97.     {
  98.         $this->set_title($title);
  99.         $this ->connect_simple('destroy'array($this'close'));
  100.  
  101.         $btnOk = GtkButton::new_from_stock(Gtk::STOCK_OK);
  102.         $btnOk->connect_simple('clicked'array($this'close'));
  103.  
  104.         $vboxMain = new GtkVBox();
  105.         $hpane = new GtkHPaned();
  106.         $hpane->set_position(250);
  107.  
  108.         //Node, Type, original variable, if the children have been checked for subchildren
  109.         $this->modTree   = new GtkTreeStore(Gtk::TYPE_STRINGGtk::TYPE_STRINGGtk::TYPE_PHP_VALUEGtk::TYPE_BOOLEAN);
  110.         //Keyname, Type (+ size), Value
  111.         $this->modValues = new GtkListStore(Gtk::TYPE_STRINGGtk::TYPE_STRINGGtk::TYPE_STRING);
  112.  
  113.         $this->trTree       = new GtkTreeView();
  114.         $this->trValues     = new GtkTreeView();
  115.         $this->trTree       ->set_model($this->modTree);
  116.         $this->trValues     ->set_model($this->modValues);
  117.  
  118.         $selection $this->trTree->get_selection();
  119.         $selection->connect         ('changed'              array($this'selectTreeRow'));
  120.         $this->trTree->connect      ('row-expanded'         array($this'expandTree'));
  121.         $this->trTree->connect_after('event' array($this'clickedTree'));
  122.         $this->trTree->set_events   (Gdk::_2BUTTON_PRESS | Gdk::BUTTON_RELEASE);
  123.  
  124.         $this->createColumns($this->trTree    array('Node''Type'));
  125.         $this->createColumns($this->trValues  array('Key''Type''Value'));
  126.  
  127.         $scrwndTree   = new GtkScrolledWindow();
  128.         $scrwndValues = new GtkScrolledWindow();
  129.         $scrwndTree   ->set_policy(Gtk::POLICY_AUTOMATICGtk::POLICY_AUTOMATIC);
  130.         $scrwndValues ->set_policy(Gtk::POLICY_AUTOMATICGtk::POLICY_AUTOMATIC);
  131.         $scrwndTree   ->add($this->trTree);
  132.         $scrwndValues ->add($this->trValues);
  133.  
  134.         $hpane->add1($scrwndTree);
  135.         $hpane->add2($scrwndValues);
  136.  
  137.         $vboxMain->pack_start($hpanetrue true 0);
  138.         $vboxMain->pack_end(  $btnOkfalsefalse0);
  139.  
  140.         $this->add($vboxMain);
  141.  
  142.         $btnOk->set_flags($btnOk->flags(+ Gtk::CAN_DEFAULT);
  143.         $this->set_default($btnOk);
  144.         $this->set_default_size(600400);
  145.     }//protected function buildDialog($title)
  146.  
  147.  
  148.  
  149.     /**
  150.     *   Creates GtkTreeView columns out of an string array and
  151.     *   appends them to the tree view.
  152.     *   The columns will be resizable and sortable.
  153.     *
  154.     *   @param GtkTreeView  $tree       The tree to which the columns shall be appended
  155.     *   @param array        $arColumns  Array of strings which are the titles for the columns
  156.     */
  157.     protected function createColumns($tree$arColumns)
  158.     {
  159.         $cell_renderer = new GtkCellRendererText();
  160.         foreach ($arColumns as $nId => $strTitle{
  161.             $column = new GtkTreeViewColumn($strTitle$cell_renderer"text"$nId);
  162.             $column->set_resizable(true);
  163.             $column->set_sort_column_id($nId);
  164.             $tree->append_column($column);
  165.         }
  166.     }//protected function createColumns($tree, $arColumns)
  167.  
  168.  
  169.  
  170.     /**
  171.     *   Appends the given $variable to the tree on the right.
  172.     *   $name is used as title for the node, $parent is the parent node
  173.     *   to which the new node will be appended.
  174.     *
  175.     *   @param mixed        $variable   The variable to append
  176.     *   @param string       $name       The title for the variable (e.g. array key)
  177.     *   @param GtkTreeIter  $parent     The parent node to which the new node shall be appended
  178.     *   @param int          $nStop      After how many levels appending shall be stopped
  179.     */
  180.     protected function buildTree($variable$name$parent = null$nStop = 1)
  181.     {
  182.         $type gettype($variable);
  183.  
  184.         if ($type == 'array'{
  185.             $type .= '[' count($variable']';
  186.         else if($type == 'object' && 
  187.             //FIXME: Tell me how to distinguish between Gtk::TYPE_PHP_VALUE and Gtk::TYPE_OBJECT!
  188.             ($variable instanceof GtkWidget || $variable instanceof GObject || $variable instanceof GdkRectangle)
  189.         {
  190.             $type get_class($variable);
  191.             $variable = new Gtk2_VarDump_PseudoClass($variable);
  192.         else if ($type == 'object'{
  193.             $type trim(get_class($variable));
  194.  
  195.             //FIXME: That here is a workaround until I know how to distinguish between Gtk::TYPE_PHP_VALUE and Gtk::TYPE_OBJECT
  196.             if ($type == 'StyleHelper' || substr($type03== 'Gdk' || substr($type05== 'Pango'{
  197.                 $variable = null;
  198.             }
  199.         else {
  200.             //not an array and not an object
  201.             $variable   = new Gtk2_VarDump_PseudoClass($variable);
  202.             $nStop      = 0;
  203.         }
  204.  
  205.         $node $this->modTree->append($parentarray($name$type$variablefalse));
  206.  
  207.         if ($nStop > 0{
  208.             $this->appendChildren($variable$node$nStop--);
  209.         }
  210.  
  211.         if ($parent === null{
  212.             $this->trTree->get_selection()->select_path('0');
  213.         }
  214.     }//protected function buildTree($variable, $name, $parent = null, $nStop = 1)
  215.  
  216.  
  217.  
  218.     /**
  219.     *   Appends all the children of the given variable to $node
  220.     *
  221.     *   @param mixed        $variable   The variable, whose children shall be appended
  222.     *   @param GtkTreeIter  $node       The parent node to which the new ones shall be appended
  223.     *   @param int          $nStop      After how many levels appending shall be stopped
  224.     */
  225.     protected function appendChildren($variable$node$nStop = 1)
  226.     {
  227.         $type gettype($variable);
  228.  
  229.         if ($type == 'object' && $variable instanceof Gtk2_VarDump_PseudoClass{
  230.             $variable $variable->value;
  231.             $type     gettype($variable);
  232.         }
  233.  
  234.         switch ($type{
  235.             case 'object':
  236.                 $arKeys array_keys(get_object_vars($variable));
  237.                 break;
  238.             case 'array':
  239.                 $arKeys array_keys($variable);
  240.                 break;
  241.             default:
  242.                 return;
  243.         }
  244.  
  245.         foreach ($arKeys as $key{
  246.             $value ($type == 'array'$variable[$key$variable->$key;
  247.             switch (gettype($value)) {
  248.                 case 'object':
  249.                 case 'array':
  250.                     $this->buildTree($value$key$node$nStop - 1);
  251.                     break;
  252.                 default:
  253.                     //other types aren't displayed in the tree
  254.                     break;
  255.             }
  256.         }
  257.     }//protected function appendChildren($variable, $node, $nStop = 1)
  258.  
  259.  
  260.  
  261.     /**
  262.     *   Adds all the children of the given $variable to the list
  263.     *   on the right side.
  264.     *   Arrays and objects are not added, as they appear on the
  265.     *   tree on the left.
  266.     *
  267.     *   @param mixed    $variable   The variable whose children values shall be shown
  268.     */
  269.     protected function buildValues($variable)
  270.     {
  271.         $this->modValues->clear();
  272.         switch (gettype($variable))
  273.         {
  274.             case 'object':
  275.                 $arKeys array_keys(get_object_vars($variable));
  276.                 foreach ($arKeys as $key{
  277.                     $value $variable->$key;
  278.                     $this->appendValue($key$value);
  279.                 }
  280.                 break;
  281.  
  282.             case 'array':
  283.                 $arKeys array_keys($variable);
  284.                 foreach ($arKeys as $key{
  285.                     $value $variable[$key];
  286.                     $this->appendValue($key$value);
  287.                 }
  288.                 break;
  289.  
  290.             default:
  291.                 //do nothing
  292.                 //shouldn't happen
  293.         }
  294.     }//protected function buildValues($variable)
  295.  
  296.  
  297.  
  298.     /**
  299.     *   Appends one value to the list on the right.
  300.     *   Arrays and objects will not be displayed, as they already
  301.     *   appear on the tree on the left side.
  302.     *
  303.     *   @param mixed    $key    The title for the node
  304.     *   @param mixed    $value  The value to display
  305.     */
  306.     protected function appendValue($key$value)
  307.     {
  308.         switch (gettype($value)) {
  309.             case 'object':
  310.             case 'array':
  311.                 //Don't display arrays and objects in the values list
  312.                 continue;
  313.             case 'string':
  314.                 $this->modValues->append(
  315.                     array(
  316.                         $key,
  317.                         'string[' strlen($value']',
  318.                         $value
  319.                     )
  320.                 );
  321.                 break;
  322.             default:
  323.                 $this->modValues->append(
  324.                     array(
  325.                         $key,
  326.                         gettype($value),
  327.                         $value
  328.                     )
  329.                 );
  330.                 break;
  331.         }
  332.     }//protected function appendValues($variable, $arKeys)
  333.  
  334.  
  335.  
  336.     /**
  337.     *   Called whenever a tree row is expanded.
  338.     *   It is used to load the children of the node's children
  339.     *   if they haven't been loaded yet.
  340.     *
  341.     *   @param  GtkTreeView $tree       The tree on which the signal has been emitted
  342.     *   @param  GtkTreeIter $iterator   The node which has been expanded
  343.     */
  344.     public function expandTree($tree$iterator)
  345.     {
  346.         if ($this->modTree->get_value($iterator3)) {
  347.             //already checked
  348.             return;
  349.         }
  350.  
  351.         //check if children have subchildren and load them
  352.         $child $this->modTree->iter_children($iterator);
  353.         while ($child !== null{
  354.             $this->appendChildren(
  355.                 $this->modTree->get_value($child2),
  356.                 $child
  357.             );
  358.             //get the next child
  359.             $child $this->modTree->iter_next($child);
  360.         }
  361.         $this->modTree->set($iterator3true);
  362.     }//public function expandTree($tree, $iterator)
  363.  
  364.  
  365.  
  366.     /**
  367.     *   Called whenever a row on the left tree has been selected.
  368.     *   It is used to show the children of the selected variable
  369.     *   on the right list.
  370.     *
  371.     *   @param array    $selection  Array consisting of the model and the currently selected node (GtkTreeIter)
  372.     */
  373.     public function selectTreeRow($selection)
  374.     {
  375.         list($model$iter$selection->get_selected();
  376.         if ($iter === null{
  377.             return;
  378.         }
  379.         $variable $model->get_value($iter2);
  380.         $this->buildValues($variable);
  381.     }//public function selectTreeRow($selection)
  382.  
  383.  
  384.  
  385.     /**
  386.     *   The tree has been clicked, and the currently selected row
  387.     *   will be expanded or collapsed, depending which mouse button has
  388.     *   been clicked.
  389.     *   The left mouse button will expand the node,
  390.     *   the right mouse button will collapse it.
  391.     *   Middle mouse button and a double-clicked left button will
  392.     *    expand all children but *only* if they have been expanded
  393.     *    before - it would be too dangerous to expand all children to
  394.     *    any depth recursively if there are loops.
  395.     *
  396.     *   @param GtkTreeView  $tree   The tree which has been clicked
  397.     *   @param GdkEvent     $event  The event data for the click event
  398.     */
  399.     public function clickedTree($tree$event)
  400.     {
  401.         if ($event->type !== Gdk::_2BUTTON_PRESS && $event->type !== Gdk::BUTTON_RELEASE{
  402.             return;
  403.         }
  404.  
  405.         list($model$arSelected$tree->get_selection()->get_selected_rows();
  406.         $path implode(':'$arSelected[0]);
  407.  
  408.         if ($event->button == 1{
  409.             //left mouse button
  410.             //If double-click: expand all rows down
  411.             $tree->expand_row($path$event->type == Gdk::_2BUTTON_PRESS);
  412.         else if ($event->button == 2{
  413.             //middle mouse button - expand all
  414.             $tree->expand_row($pathtrue);
  415.         else if ($event->button == 3{
  416.             //right mouse button
  417.             $tree->collapse_row($path);
  418.         }
  419.     }//public function clickedTree($tree, $event)
  420.  
  421.  
  422.  
  423.     /**
  424.     *   Called when the user clicks "OK" or tries to close the window.
  425.     *   This function quits the main loop opened in the constructor.
  426.     */
  427.     public function close()
  428.     {
  429.         //quit our own main loop
  430.         $this->destroy();
  431.         Gtk::main_quit();
  432.     }//public function close()
  433.  
  434. }//class Gtk2_VarDump extends GtkWindow
  435.  
  436.  
  437.  
  438. /**
  439. *   Pseudo class wrapper around simple data types.
  440. *   Needed to store a simple data type in a GtkTreeStore.
  441. *
  442. *   The bug has been fixed in CVS by Andrei on 2005-11-22,
  443. *   but I will leave this until there is a new w32 version
  444. *   of php-gtk2 so that all people can use Gtk2_VarDump.
  445. */
  446. class Gtk2_VarDump_PseudoClass
  447. {
  448.     public $value = null;
  449.  
  450.  
  451.     public function __construct($value)
  452.     {
  453.         $this->value $value;
  454.     }//public function __construct($value)
  455.  
  456.  
  457.  
  458.     public function __toString()
  459.     {
  460.         return $this->value;
  461.     }//public function __toString()
  462.  
  463. }//class Gtk2_VarDump_PseudoClass
  464. ?>

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