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

Source for file IndexedComboBox.php

Documentation is available at IndexedComboBox.php

  1. <?php
  2. /**
  3. *   Indexed Gtk2 combo box similar to the HTML select box.
  4. *   Lets you not only store values as the normal GtkComboBox,
  5. *   but associated keys as well. The active key can be easily
  6. *   received with get_active_key.
  7. *
  8. *   It imitates the convenience methods of a text-based GtkComboBox
  9. *   that is constructed with GtkComboBox::new_text().
  10. *
  11. *   Both key and values can be strings or integers.
  12. *
  13. *   Method names aren't camelCase but with underscores to be close
  14. *   the naming of the original Gtk2 methods.
  15. *
  16. *   @category   Gtk2
  17. *   @package    Gtk2_IndexedComboBox
  18. *   @author     Christian Weiske <cweiske@php.net>
  19. *   @license    LGPL
  20. *   @version    CVS: $Id: IndexedComboBox.php,v 1.3 2006/04/05 07:13:27 cweiske Exp $
  21. */
  22. class Gtk2_IndexedComboBox extends GtkComboBox
  23. {
  24.     /**
  25.     *   The cell renderer for the text
  26.     *   @var GtkCellRenderer 
  27.     */
  28.     protected $renderer = null;
  29.  
  30.  
  31.  
  32.     public function __construct($arData = null)
  33.     {
  34.         parent::__construct();
  35.         $model = new GtkListStore(Gtk::TYPE_STRINGGtk::TYPE_STRING);
  36.         $this->set_model($model);
  37.  
  38.         //show the second column only
  39.         $this->renderer = new GtkCellRendererText();
  40.         $this->pack_start($this->renderer);
  41.         $this->set_attributes($this->renderer'text'1);
  42.  
  43.         if ($arData !== null{
  44.             $this->set_array($arData);
  45.         }
  46.     }//public function __construct($arData = null)
  47.  
  48.  
  49.  
  50.     /**
  51.     *   Appends a single key/value pair to the list.
  52.     *
  53.     *   @param string   $strId      The id to append
  54.     *   @param string   $strValue   The value to append
  55.     */
  56.     public function append($strId$strValue)
  57.     {
  58.         $this->get_model()->append(array($strId$strValue));
  59.     }//public function append_array($strId, $strValue)
  60.  
  61.  
  62.  
  63.     /**
  64.     *   Appends an array (key and value) as data to the store.
  65.     *
  66.     *   @param array    $arData     The array to append
  67.     */
  68.     public function append_array($arData)
  69.     {
  70.         $model $this->get_model();
  71.         foreach ($arData as $strId => &$strValue{
  72.             $model->append(array($strId$strValue));
  73.         }
  74.     }//public function append_array($arData)
  75.  
  76.  
  77.  
  78.     /**
  79.     *   Returns the id of the active entry.
  80.     *   If there is no active key, NULL will be returned.
  81.     *
  82.     *   @return string  The id/key of the selected entry
  83.     */
  84.     public function get_active_key()
  85.     {
  86.         $nActive $this->get_active();
  87.         if ($nActive === -1{
  88.             return null;
  89.         }
  90.         //workaround bug in php-gtk2: get_active_iter wants a parameter instead of giving one
  91.         return $this->get_model()->get_value($this->get_model()->get_iter($nActive)0);
  92.         //That's the better one (that doesn't work as of 2006-03-04)
  93.         return $this->get_model()->get_value($this->get_active_iter()0);
  94.     }//public function get_active_key()
  95.  
  96.  
  97.  
  98.     /**
  99.     *   Returns the string of the active entry.
  100.     *   If there is no active entry, NULL will be returned.
  101.     *
  102.     *   @return string  The string value of the selected entry
  103.     */
  104.     public function get_active_text()
  105.     {
  106.         $nActive $this->get_active();
  107.         if ($nActive === -1{
  108.             return null;
  109.         }
  110.         //workaround bug in php-gtk2: get_active_iter wants a parameter instead of giving one
  111.         return $this->get_model()->get_value($this->get_model()->get_iter($nActive)1);
  112.         //That's the better one (that doesn't work as of 2006-03-04)
  113.         return $this->get_model()->get_value($this->get_active_iter()1);
  114.     }//public function get_active_text()
  115.  
  116.  
  117.  
  118.     /**
  119.     *   Returns an array with all key/value pairs.
  120.     *
  121.     *   @return array Array with key/value pairs in the model
  122.     */
  123.     public function get_array()
  124.     {
  125.         $ar = array();
  126.  
  127.         $model $this->get_model();
  128.         $iter $model->get_iter_first();
  129.         if ($iter !== null{
  130.             do {
  131.                 $ar[$model->get_value($iter0)$model->get_value($iter1);
  132.             while (($iter $model->iter_next($iter)) !== null);
  133.         }
  134.  
  135.         return $ar;
  136.     }//public function get_array()
  137.  
  138.  
  139.  
  140.     /**
  141.     *   Returns the default cell renderer.
  142.     *
  143.     *   @return GtkCellRenderer The default cell renderer
  144.     */
  145.     public function get_cell_renderer()
  146.     {
  147.         return $this->renderer;
  148.     }//public function get_cell_renderer()
  149.  
  150.  
  151.  
  152.     /**
  153.     *   Inserts a single key/value pair at a certain position into the list.
  154.     *
  155.     *   @param int      $nPosition  The position to insert the values at
  156.     *   @param string   $strId      The id to append
  157.     *   @param string   $strValue   The value to append
  158.     */
  159.     public function insert($nPosition$strId$strValue)
  160.     {
  161.         $this->get_model()->insert($nPositionarray($strId$strValue));
  162.     }//public function insert($nPosition, $strId, $strValue)
  163.  
  164.  
  165.  
  166.     /**
  167.     *   Inserts an array (key and value) at a certain position into the list.
  168.     *
  169.     *   @param int      $nPosition  The position to insert the array at
  170.     *   @param array    $arData     The array to append
  171.     */
  172.     public function insert_array($nPosition$arData)
  173.     {
  174.         $model $this->get_model();
  175.         foreach ($arData as $strId => &$strValue{
  176.             $model->insert($nPosition++array($strId$strValue));
  177.         }
  178.     }//public function insert_array($nPosition, $arData)
  179.  
  180.  
  181.  
  182.     /**
  183.     *   Prepends a single key/value pair to the list.
  184.     *
  185.     *   @param string   $strId      The id to append
  186.     *   @param string   $strValue   The value to append
  187.     */
  188.     public function prepend($strId$strValue)
  189.     {
  190.         $this->get_model()->prepend(array($strId$strValue));
  191.     }//public function prepend($strId, $strValue)
  192.  
  193.  
  194.  
  195.     /**
  196.     *   Prepends an array (key and value) at the beginning of the store
  197.     *
  198.     *   @param array    $arData     The array to append
  199.     */
  200.     public function prepend_array($arData)
  201.     {
  202.         $nPosition = 0;
  203.         $model $this->get_model();
  204.         foreach ($arData as $strId => &$strValue{
  205.             $model->insert($nPosition++array($strId$strValue));
  206.         }
  207.     }//public function prepend_array($arData)
  208.  
  209.  
  210.  
  211.     /**
  212.     *   Removes the first entry with the given key from the list.
  213.     *
  214.     *   @param string   $strId      The key of the entry to remove
  215.     *
  216.     *   @return boolean     True if an entry has been deleted
  217.     */
  218.     public function remove_key($strId)
  219.     {
  220.         $model $this->get_model();
  221.         $iter $model->get_iter_first();
  222.         if ($iter !== null{
  223.             do {
  224.                 if ($model->get_value($iter0== $strId{
  225.                     break;
  226.                 }
  227.             while (($iter $model->iter_next($iter)) !== null);
  228.  
  229.             if ($iter !== null{
  230.                 $model->remove($iter);
  231.                 return true;
  232.             }
  233.         }
  234.         return false;
  235.     }//public function remove_key($strId)
  236.  
  237.  
  238.  
  239.     /**
  240.     *   Sets the model row with the given key as active.
  241.     *
  242.     *   @param string   $strId      The key of the entry to be made active
  243.     *
  244.     *   @return boolean     True if an entry has been set active
  245.     */
  246.     public function set_active_key($strId)
  247.     {
  248.         if ($strId === null{
  249.             $this->set_active(-1);
  250.             return true;
  251.         }
  252.  
  253.         $model $this->get_model();
  254.         $iter  $model->get_iter_first();
  255.         if ($iter !== null{
  256.             do {
  257.                 if ($model->get_value($iter0== $strId{
  258.                     break;
  259.                 }
  260.             while (($iter $model->iter_next($iter)) !== null);
  261.  
  262.             if ($iter !== null{
  263.                 $this->set_active_iter($iter);
  264.                 return true;
  265.             }
  266.         }
  267.         return false;
  268.     }//public function set_active_key($strId)
  269.  
  270.  
  271.  
  272.     /**
  273.     *   Sets an array (key and value) as data into the store.
  274.     *   Clears any previous entries.
  275.     *
  276.     *   @param array    $arData     The array to set
  277.     */
  278.     public function set_array($arData)
  279.     {
  280.         $this->get_model()->clear();
  281.         return $this->append_array($arData);
  282.     }//public function set_array($arData)
  283.  
  284.  
  285.  
  286.     /*
  287.     *   PEAR-style camelCaseNamed method aliases
  288.     */
  289.  
  290.  
  291.  
  292.     public function appendArray($arData{
  293.         return $this->append_array($arData);
  294.     }
  295.  
  296.  
  297.  
  298.     public function getActiveKey({
  299.         return $this->get_active_key();
  300.     }
  301.  
  302.  
  303.  
  304.     public function getActiveText({
  305.         return $this->get_active_text();
  306.     }
  307.  
  308.  
  309.  
  310.     public function getArray({
  311.         return $this->get_array();
  312.     }
  313.  
  314.  
  315.  
  316.     public function getCellRenderer({
  317.         return $this->get_cell_renderer();
  318.     }
  319.  
  320.  
  321.  
  322.     public function insertArray($nPosition$arData{
  323.         return $this->insert_array($nPosition$arData);
  324.     }
  325.  
  326.  
  327.  
  328.     public function prependArray($arData{
  329.         return $this->prepend_array($arData);
  330.     }
  331.  
  332.  
  333.  
  334.     public function removeKey($strId{
  335.         return $this->remove_key($strId);
  336.     }
  337.  
  338.  
  339.  
  340.     public function setActiveKey($strId{
  341.         return $this->set_active_key($strId);
  342.     }
  343.  
  344.  
  345.  
  346.     public function setArray($arData{
  347.         return $this->set_array($arData);
  348.     }
  349.  
  350. }//class Gtk2_IndexedComboBox extends GtkMessageDialog
  351. ?>

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