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$
  21. */
  22. class Gtk2_IndexedComboBox extends GtkComboBox
  23. {
  24.     public function __construct($arData = null)
  25.     {
  26.         parent::__construct();
  27.         $model = new GtkListStore(Gtk::TYPE_STRINGGtk::TYPE_STRING);
  28.         $this->set_model($model);
  29.  
  30.         //show the second column only
  31.         $renderer = new GtkCellRendererText();
  32.         $this->pack_start($renderer);
  33.         $this->set_attributes($renderer'text'1);
  34.  
  35.         if ($arData !== null{
  36.             $this->set_array($arData);
  37.         }
  38.     }//public function __construct($arData = null)
  39.  
  40.  
  41.  
  42.     /**
  43.     *   Appends a single key/value pair to the list.
  44.     *
  45.     *   @param string   $strId      The id to append
  46.     *   @param string   $strValue   The value to append
  47.     */
  48.     public function append($strId$strValue)
  49.     {
  50.         $this->get_model()->append(array($strId$strValue));
  51.     }//public function append_array($strId, $strValue)
  52.  
  53.  
  54.  
  55.     /**
  56.     *   Appends an array (key and value) as data to the store.
  57.     *
  58.     *   @param array    $arData     The array to append
  59.     */
  60.     public function append_array($arData)
  61.     {
  62.         $model $this->get_model();
  63.         foreach ($arData as $strId => &$strValue{
  64.             $model->append(array($strId$strValue));
  65.         }
  66.     }//public function append_array($arData)
  67.  
  68.  
  69.  
  70.     /**
  71.     *   Returns the id of the active entry
  72.     *
  73.     *   @return string  The id/key of the selected entry
  74.     */
  75.     public function get_active_key()
  76.     {
  77.         //workaround bug in php-gtk2: get_active_iter wants a parameter instead of giving one
  78.         return $this->get_model()->get_value($this->get_model()->get_iter($this->get_active())0);
  79.         //That's the better one (that doesn't work as of 2006-03-04)
  80.         return $this->get_model()->get_value($this->get_active_iter()0);
  81.     }//public function get_active_key()
  82.  
  83.  
  84.  
  85.     /**
  86.     *   Returns the string of the active entry.
  87.     *
  88.     *   @return string  The string value of the selected entry
  89.     */
  90.     public function get_active_text()
  91.     {
  92.         //workaround bug in php-gtk2: get_active_iter wants a parameter instead of giving one
  93.         return $this->get_model()->get_value($this->get_model()->get_iter($this->get_active())1);
  94.         //That's the better one (that doesn't work as of 2006-03-04)
  95.         return $this->get_model()->get_value($this->get_active_iter()1);
  96.     }//public function get_active_text()
  97.  
  98.  
  99.  
  100.     /**
  101.     *   Inserts a single key/value pair at a certain position into the list.
  102.     *
  103.     *   @param string   $strId      The id to append
  104.     *   @param string   $strValue   The value to append
  105.     */
  106.     public function insert($nPosition$strId$strValue)
  107.     {
  108.         $this->get_model()->insert($nPositionarray($strId$strValue));
  109.     }//public function insert($nPosition, $strId, $strValue)
  110.  
  111.  
  112.  
  113.     /**
  114.     *   Inserts an array (key and value) at a certain position into the list.
  115.     *
  116.     *   @param int      $nPosition  The position to insert the array at
  117.     *   @param array    $arData     The array to append
  118.     */
  119.     public function insert_array($nPosition$arData)
  120.     {
  121.         $model $this->get_model();
  122.         foreach ($arData as $strId => &$strValue{
  123.             $model->insert($nPosition++array($strId$strValue));
  124.         }
  125.     }//public function insert_array($nPosition, $arData)
  126.  
  127.  
  128.  
  129.     /**
  130.     *   Prepends a single key/value pair to the list.
  131.     *
  132.     *   @param string   $strId      The id to append
  133.     *   @param string   $strValue   The value to append
  134.     */
  135.     public function prepend($strId$strValue)
  136.     {
  137.         $this->get_model()->prepend(array($strId$strValue));
  138.     }//public function prepend($strId, $strValue)
  139.  
  140.  
  141.  
  142.     /**
  143.     *   Prepends an array (key and value) at the beginning of the store
  144.     *
  145.     *   @param array    $arData     The array to append
  146.     */
  147.     public function prepend_array($arData)
  148.     {
  149.         $nPosition = 0;
  150.         $model $this->get_model();
  151.         foreach ($arData as $strId => &$strValue{
  152.             $model->insert($nPosition++array($strId$strValue));
  153.         }
  154.     }//public function prepend_array($arData)
  155.  
  156.  
  157.  
  158.     /**
  159.     *   Removes the first entry with the given key from the list.
  160.     *
  161.     *   @param string   $strId      The key of the entry to remove
  162.     *
  163.     *   @return boolean     True if an entry has been deleted
  164.     */
  165.     public function remove_key($strId)
  166.     {
  167.         $model $this->get_model();
  168.         $iter $model->get_iter_first();
  169.         do {
  170.             if ($model->get_value($iter0== $strId{
  171.                 break;
  172.             }
  173.         while (($iter $model->iter_next($iter)) !== null);
  174.  
  175.         if ($iter !== null{
  176.             $model->remove($iter);
  177.             return true;
  178.         }
  179.         return false;
  180.     }//public function remove_key($strId)
  181.  
  182.  
  183.  
  184.     /**
  185.     *   Sets the model row with the given key as active.
  186.     *
  187.     *   @param string   $strId      The key of the entry to be made active
  188.     *
  189.     *   @return boolean     True if an entry has been set active
  190.     */
  191.     public function set_active_key($strId)
  192.     {
  193.         $model $this->get_model();
  194.         $iter $model->get_iter_first();
  195.         do {
  196.             if ($model->get_value($iter0== $strId{
  197.                 break;
  198.             }
  199.         while (($iter $model->iter_next($iter)) !== null);
  200.  
  201.         if ($iter !== null{
  202.             $this->set_active_iter($iter);
  203.             return true;
  204.         }
  205.         return false;
  206.     }//public function set_active_key($strId)
  207.  
  208.  
  209.  
  210.     /**
  211.     *   Sets an array (key and value) as data into the store.
  212.     *   Clears any previous entries.
  213.     *
  214.     *   @param array    $arData     The array to set
  215.     */
  216.     public function set_array($arData)
  217.     {
  218.         $this->get_model()->clear();
  219.         return $this->append_array($arData);
  220.     }//public function set_array($arData)
  221.  
  222.  
  223.  
  224.     /*
  225.     *   PEAR-style camelCaseNamed method aliases
  226.     */
  227.  
  228.  
  229.  
  230.     public function appendArray($arData{
  231.         return $this->append_array($arData);
  232.     }
  233.  
  234.  
  235.  
  236.     public function getActiveKey({
  237.         return $this->get_active_key();
  238.     }
  239.  
  240.  
  241.  
  242.     public function getActiveText({
  243.         return $this->get_active_text();
  244.     }
  245.  
  246.  
  247.  
  248.     public function insertArray($nPosition$arData{
  249.         return $this->insert_array($nPosition$arData);
  250.     }
  251.  
  252.  
  253.  
  254.     public function prependArray($arData{
  255.         return $this->prepend_array($arData);
  256.     }
  257.  
  258.  
  259.  
  260.     public function removeKey($strId{
  261.         return $this->remove_key($strId);
  262.     }
  263.  
  264.  
  265.  
  266.     public function setActiveKey($strId{
  267.         return $this->set_active_key($strId);
  268.     }
  269.  
  270.  
  271.  
  272.     public function setArray($arData{
  273.         return $this->set_array($arData);
  274.     }
  275.  
  276. }//class Gtk2_IndexedComboBox extends GtkMessageDialog
  277. ?>

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