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.1 2006/03/20 08:22:54 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.     *
  81.     *   @return string  The id/key of the selected entry
  82.     */
  83.     public function get_active_key()
  84.     {
  85.         //workaround bug in php-gtk2: get_active_iter wants a parameter instead of giving one
  86.         return $this->get_model()->get_value($this->get_model()->get_iter($this->get_active())0);
  87.         //That's the better one (that doesn't work as of 2006-03-04)
  88.         return $this->get_model()->get_value($this->get_active_iter()0);
  89.     }//public function get_active_key()
  90.  
  91.  
  92.  
  93.     /**
  94.     *   Returns the string of the active entry.
  95.     *
  96.     *   @return string  The string value of the selected entry
  97.     */
  98.     public function get_active_text()
  99.     {
  100.         //workaround bug in php-gtk2: get_active_iter wants a parameter instead of giving one
  101.         return $this->get_model()->get_value($this->get_model()->get_iter($this->get_active())1);
  102.         //That's the better one (that doesn't work as of 2006-03-04)
  103.         return $this->get_model()->get_value($this->get_active_iter()1);
  104.     }//public function get_active_text()
  105.  
  106.  
  107.  
  108.     /**
  109.     *   Returns the default cell renderer.
  110.     *
  111.     *   @return GtkCellRenderer The default cell renderer
  112.     */
  113.     public function get_cell_renderer()
  114.     {
  115.         return $this->renderer;
  116.     }//public function get_cell_renderer()
  117.  
  118.  
  119.  
  120.     /**
  121.     *   Inserts a single key/value pair at a certain position into the list.
  122.     *
  123.     *   @param string   $strId      The id to append
  124.     *   @param string   $strValue   The value to append
  125.     */
  126.     public function insert($nPosition$strId$strValue)
  127.     {
  128.         $this->get_model()->insert($nPositionarray($strId$strValue));
  129.     }//public function insert($nPosition, $strId, $strValue)
  130.  
  131.  
  132.  
  133.     /**
  134.     *   Inserts an array (key and value) at a certain position into the list.
  135.     *
  136.     *   @param int      $nPosition  The position to insert the array at
  137.     *   @param array    $arData     The array to append
  138.     */
  139.     public function insert_array($nPosition$arData)
  140.     {
  141.         $model $this->get_model();
  142.         foreach ($arData as $strId => &$strValue{
  143.             $model->insert($nPosition++array($strId$strValue));
  144.         }
  145.     }//public function insert_array($nPosition, $arData)
  146.  
  147.  
  148.  
  149.     /**
  150.     *   Prepends a single key/value pair to the list.
  151.     *
  152.     *   @param string   $strId      The id to append
  153.     *   @param string   $strValue   The value to append
  154.     */
  155.     public function prepend($strId$strValue)
  156.     {
  157.         $this->get_model()->prepend(array($strId$strValue));
  158.     }//public function prepend($strId, $strValue)
  159.  
  160.  
  161.  
  162.     /**
  163.     *   Prepends an array (key and value) at the beginning of the store
  164.     *
  165.     *   @param array    $arData     The array to append
  166.     */
  167.     public function prepend_array($arData)
  168.     {
  169.         $nPosition = 0;
  170.         $model $this->get_model();
  171.         foreach ($arData as $strId => &$strValue{
  172.             $model->insert($nPosition++array($strId$strValue));
  173.         }
  174.     }//public function prepend_array($arData)
  175.  
  176.  
  177.  
  178.     /**
  179.     *   Removes the first entry with the given key from the list.
  180.     *
  181.     *   @param string   $strId      The key of the entry to remove
  182.     *
  183.     *   @return boolean     True if an entry has been deleted
  184.     */
  185.     public function remove_key($strId)
  186.     {
  187.         $model $this->get_model();
  188.         $iter $model->get_iter_first();
  189.         do {
  190.             if ($model->get_value($iter0== $strId{
  191.                 break;
  192.             }
  193.         while (($iter $model->iter_next($iter)) !== null);
  194.  
  195.         if ($iter !== null{
  196.             $model->remove($iter);
  197.             return true;
  198.         }
  199.         return false;
  200.     }//public function remove_key($strId)
  201.  
  202.  
  203.  
  204.     /**
  205.     *   Sets the model row with the given key as active.
  206.     *
  207.     *   @param string   $strId      The key of the entry to be made active
  208.     *
  209.     *   @return boolean     True if an entry has been set active
  210.     */
  211.     public function set_active_key($strId)
  212.     {
  213.         $model $this->get_model();
  214.         $iter $model->get_iter_first();
  215.         do {
  216.             if ($model->get_value($iter0== $strId{
  217.                 break;
  218.             }
  219.         while (($iter $model->iter_next($iter)) !== null);
  220.  
  221.         if ($iter !== null{
  222.             $this->set_active_iter($iter);
  223.             return true;
  224.         }
  225.         return false;
  226.     }//public function set_active_key($strId)
  227.  
  228.  
  229.  
  230.     /**
  231.     *   Sets an array (key and value) as data into the store.
  232.     *   Clears any previous entries.
  233.     *
  234.     *   @param array    $arData     The array to set
  235.     */
  236.     public function set_array($arData)
  237.     {
  238.         $this->get_model()->clear();
  239.         return $this->append_array($arData);
  240.     }//public function set_array($arData)
  241.  
  242.  
  243.  
  244.     /*
  245.     *   PEAR-style camelCaseNamed method aliases
  246.     */
  247.  
  248.  
  249.  
  250.     public function appendArray($arData{
  251.         return $this->append_array($arData);
  252.     }
  253.  
  254.  
  255.  
  256.     public function getActiveKey({
  257.         return $this->get_active_key();
  258.     }
  259.  
  260.  
  261.  
  262.     public function getActiveText({
  263.         return $this->get_active_text();
  264.     }
  265.  
  266.  
  267.  
  268.     public function getCellRenderer({
  269.         return $this->get_cell_renderer();
  270.     }
  271.  
  272.  
  273.  
  274.     public function insertArray($nPosition$arData{
  275.         return $this->insert_array($nPosition$arData);
  276.     }
  277.  
  278.  
  279.  
  280.     public function prependArray($arData{
  281.         return $this->prepend_array($arData);
  282.     }
  283.  
  284.  
  285.  
  286.     public function removeKey($strId{
  287.         return $this->remove_key($strId);
  288.     }
  289.  
  290.  
  291.  
  292.     public function setActiveKey($strId{
  293.         return $this->set_active_key($strId);
  294.     }
  295.  
  296.  
  297.  
  298.     public function setArray($arData{
  299.         return $this->set_array($arData);
  300.     }
  301.  
  302. }//class Gtk2_IndexedComboBox extends GtkMessageDialog
  303. ?>

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