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

Source for file EntryDialog.php

Documentation is available at EntryDialog.php

  1. <?php
  2. /**
  3. *   Dialog message box with text entry field.
  4. *
  5. *   There are three modes to use it:
  6. *   - Normal constructor with (somewhat complicated) parameters
  7. *       as known from GtkMessageDialog.
  8. *   - new_simple() static method constructor with only one required
  9. *       parameter.
  10. *   - Static get() method that just returns the inputted text instead
  11. *       of the dialog itself. This is the most convenient and code-saving
  12. *       method to get an input.
  13. *
  14. *   Even when the dialog has an OK button only, the user can cancel it
  15. *   by closing the window.
  16. *
  17. *   @category   Gtk2
  18. *   @package    Gtk2_EntryDialog
  19. *   @author     Christian Weiske <cweiske@php.net>
  20. *   @license    LGPL
  21. *   @version    CVS: $Id: EntryDialog.php,v 1.2 2006/04/06 04:12:06 cweiske Exp $
  22. */
  23. class Gtk2_EntryDialog extends GtkMessageDialog
  24. {
  25.     /**
  26.     *   The entry box
  27.     *   @var GtkEntry 
  28.     */
  29.     protected $entry;
  30.  
  31.     /**
  32.     *   The response id that is used as default
  33.     *   @var int 
  34.     */
  35.     protected $nDefaultResponseId = null;
  36.  
  37.  
  38.  
  39.     /**
  40.     *   Normal constructor.
  41.     *   Parameters are the same as for GtkMessageDialog.
  42.     *
  43.     *   @param GtkWindow        $parent     Parent window (can be null)
  44.     *   @param GtkDialogFlags   $flags      Dialog flags (use 0 as default)
  45.     *   @param GtkMessageType   $type       Message type (e.g. Gtk::MESSAGE_QUESTION)
  46.     *   @param GtkButtonsType   $buttons    Buttons to show (e.g. Gtk::BUTTONS_OK)
  47.     *   @param string           $message    Message to display
  48.     *   @param string           $default    Default text for the entry
  49.     */
  50.     public function __construct($parent$flags$type$buttons$message$default = null)
  51.     {
  52.         parent::__construct($parent$flags$type$buttons$message);
  53.         $this->entry = new GtkEntry();
  54.         $this->entry->connect_simple('activate'array($this'onActivateEntry'));
  55.         $this->vbox->pack_end($this->entry);
  56.         if ($default !== null{
  57.             $this->entry->set_text($default);
  58.         }
  59.         switch ($buttons{
  60.             case Gtk::BUTTONS_OK:
  61.             case Gtk::BUTTONS_OK_CANCEL:
  62.                 $this->nDefaultResponseId = Gtk::RESPONSE_OK;
  63.                 break;
  64.             case Gtk::BUTTONS_YESNO:
  65.                 $this->nDefaultResponseId = Gtk::RESPONSE_YES;
  66.                 break;
  67.             case Gtk::BUTTONS_CLOSE:
  68.                 $this->nDefaultResponseId = Gtk::RESPONSE_CLOSE;
  69.                 break;
  70.         }
  71.     }//public function __construct($parent, $flags, $type, $buttons, $message, $default = null)
  72.  
  73.  
  74.  
  75.     /**
  76.     *   Simplified constructor with not so much parameters.
  77.     *   Message type is Gtk::MESSAGE_QUESTION, the flags will be
  78.     *   Gtk::DIALOG_MODAL if the parent is set. Only one OK button
  79.     *   will be visible.
  80.     *
  81.     *   @param string       $message    Message/question to display
  82.     *   @param string       $default    Default entry text
  83.     *   @param GtkWidget    $parent     Parent widget if any
  84.     *
  85.     *   @return Gtk2_EntryDialog    Entry dialog instance
  86.     */
  87.     public static function new_simple($message$default = null$parent = null)
  88.     {
  89.         $flags $parent === null ? 0 : Gtk::DIALOG_MODAL;
  90.         return new Gtk2_EntryDialog(
  91.             $parent$flagsGtk::MESSAGE_QUESTION,
  92.             Gtk::BUTTONS_OK$message$default
  93.         );
  94.     }//public static function new_simple($message, $default = null, $parent = null)
  95.  
  96.  
  97.  
  98.     /**
  99.     *   Creates a dialog with the given parameters (@see new_simple),
  100.     *   runs it, and returns the text set.
  101.     *   If the user cancelled the dialog, this method returns FALSE.
  102.     *   In any other case (even when the text is empty), the a string
  103.     *   with the text is returned.
  104.     *
  105.     *   @param string       $message    Message/question to display
  106.     *   @param string       $default    Default entry text
  107.     *   @param GtkWidget    $parent     Parent widget if any
  108.     *
  109.     *   @return string      Text input by the user
  110.     */
  111.     public static function get($message$default$parent = null)
  112.     {
  113.         $dialog = self::new_simple($message$default$parent);
  114.         $answer $dialog->run();
  115.         if ($answer == Gtk::RESPONSE_OK{
  116.             $text $dialog->get_text();
  117.         else {
  118.             //cancelled
  119.             $text = false;
  120.         }
  121.         $dialog->destroy();
  122.         return $text;
  123.     }//public static function get($message, $default, $parent = null)
  124.  
  125.  
  126.  
  127.     /**
  128.     *   Show the dialog and block until a button has been pressed.
  129.     *
  130.     *   @return int     The response id of the pressed button.
  131.     */
  132.     public function run()
  133.     {
  134.         //Make sure that the entry is visible
  135.         $this->show_all();
  136.         return parent::run();
  137.     }//public function run()
  138.  
  139.  
  140.  
  141.     /**
  142.     *   Sets the text for the entry
  143.     *
  144.     *   @param string   $text   The text to set
  145.     */
  146.     public function set_text($text)
  147.     {
  148.         $this->entry->set_text($text);
  149.     }//public function set_text($text)
  150.  
  151.  
  152.  
  153.     /**
  154.     *   Retrieves the text from the entry
  155.     *
  156.     *   @return string  The input from the user
  157.     */
  158.     public function get_text()
  159.     {
  160.         return $this->entry->get_text();
  161.     }//public function get_text()
  162.  
  163.  
  164.  
  165.     /**
  166.     *   Set the default response.
  167.     *   The button with the id will be the default one,
  168.     *   allowing you to just press return to activate it
  169.     *
  170.     *   @param int  $response_id    Response code
  171.     */
  172.     public function set_default_response($response_id)
  173.     {
  174.         parent::set_default_response($response_id);
  175.         $this->nDefaultResponseId = $response_id;
  176.     }//public function set_default_response($response_id)
  177.  
  178.  
  179.  
  180.     /**
  181.     *   Callback for the entry text.
  182.     *   Activates the default button.
  183.     */
  184.     public function onActivateEntry()
  185.     {
  186.         if ($this->nDefaultResponseId !== null{
  187.             $this->response($this->nDefaultResponseId);
  188.         }
  189.     }//public function onActivateEntry()
  190.  
  191.  
  192.  
  193.     /*
  194.     *   PEAR-style camelCase method aliases
  195.     */
  196.  
  197.  
  198.  
  199.     /**
  200.     *   Alias for set_default_response()
  201.     *
  202.     *   @param int  $response_id    Response code
  203.     */
  204.     public function setDefaultResponse($response_id)
  205.     {
  206.         $this->set_default_response($response_id);
  207.     }//public function setDefaultResponse($response_id)
  208.  
  209.  
  210.  
  211.     /**
  212.     *   Alias for set_text().
  213.     *
  214.     *   @param string   $text   The text to set
  215.     */
  216.     public function setText($text)
  217.     {
  218.         $this->set_text($text);
  219.     }//public function setText($text)
  220.  
  221.  
  222.  
  223.     /**
  224.     *   Alias for get_text().
  225.     *
  226.     *   @return string  The input from the user
  227.     */
  228.     public function getText()
  229.     {
  230.         return $this->get_text();
  231.     }//public function getText()
  232.  
  233. }//class Gtk2_EntryDialog extends GtkMessageDialog
  234. ?>

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