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

Source for file Adjustment.php

Documentation is available at Adjustment.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2005 Scott Mattocks                                    |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Scott Mattocks <scottmattocks@php.net>                       |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Adjustment.php,v 1.2 2005/01/11 13:55:04 scottmattocks Exp $
  20.  
  21. // Define the error code if it isn't already.
  22. if (!defined('GTK_STYLED_ERROR')) {
  23.     define('GTK_STYLED_ERROR'1);
  24. }
  25.  
  26. /**
  27.  * A styleable adjustment pseudo widget.
  28.  *
  29.  * The interface for this should mimic a GtkAdjustment.
  30.  *
  31.  * @abstract
  32.  * @author     Scott Mattocks <scottmattocks@php.net>
  33.  * @version    @VER@
  34.  * @category   Gtk
  35.  * @package    Gtk_Styled
  36.  * @subpackage Adjustment
  37.  * @license    PHP version 3.0
  38.  * @copyright  Copyright &copy; 2005 Scott Mattocks
  39.  */
  40.  
  41.     /**
  42.      * The useable widget
  43.      * @var object 
  44.      */
  45.     var $widget;
  46.     /**
  47.      * The track that the bar sits in.
  48.      * @var object 
  49.      */
  50.     var $track;
  51.     /**
  52.      * The portion of the track before the bar.
  53.      * @var object 
  54.      */
  55.     var $preTrack;
  56.     /**
  57.      * The visual representation of the adjustment.
  58.      * @var object 
  59.      */
  60.     var $bar;
  61.     /**
  62.      * The portion of the track after the bar.
  63.      * @var object 
  64.      */
  65.     var $postTrack;
  66.  
  67.     /**
  68.      * The value that the adjustment represents.
  69.      * @var double 
  70.      */
  71.     var $value;
  72.     /**
  73.      * The lower limit of the value.
  74.      * @var double 
  75.      */
  76.     var $lower;
  77.     /**
  78.      * The upper limit of the value.
  79.      * @var double 
  80.      */
  81.     var $upper;
  82.     /**
  83.      * The amount that the value should change with one step.
  84.      * @var double 
  85.      */
  86.     var $stepIncrement;
  87.     /**
  88.      * The amount that the value should change with one page.
  89.      * @var double 
  90.      */
  91.     var $pageIncrement;
  92.     /**
  93.      * The size of one page's worth of data.
  94.      * @var double 
  95.      */
  96.     var $pageSize;
  97.     /**
  98.      * The widget width/height (depending on the orientation)
  99.      * @var integer 
  100.      */
  101.     var $width = 15;
  102.  
  103.     /**
  104.      * Constructor.
  105.      *
  106.      * The constructor should set the basic properties of the pseudo
  107.      * widget and then create the more advanced properties. The advanced
  108.      * properties include the bar and the track that it moves in.
  109.      *
  110.      * @access public
  111.      * @param  double $value 
  112.      * @param  double $lower 
  113.      * @param  double $upper 
  114.      * @param  double $step_inc 
  115.      * @param  double $page_inc 
  116.      * @param  double $page_size 
  117.      * @return void 
  118.      */
  119.     function Gtk_Styled_Adjustment($value$lower$upper
  120.                                    $step_inc$page_inc$page_size)
  121.     {
  122.         // Check the passed values.
  123.         if (!is_numeric($value)) {
  124.             return $this->_handleError('Gtk_Styled_Adjustment::value is expected to be a number.');
  125.         else {
  126.             $this->value = $value;
  127.         }
  128.         if (!is_numeric($lower)) {
  129.             return $this->_handleError('Gtk_Styled_Adjustment::lower is expected to be a number.');
  130.         else {
  131.             $this->lower = $lower;
  132.         }
  133.         if (!is_numeric($upper)) {
  134.             return $this->_handleError('Gtk_Styled_Adjustment::upper is expected to be a number.');
  135.         else {
  136.             $this->upper = $upper;
  137.         }
  138.         if (!is_numeric($step_inc)) {
  139.             return $this->_handleError('Gtk_Styled_Adjustment::stepIncrement is expected to be a number.');
  140.         else {
  141.             $this->stepIncrement = $step_inc;
  142.         }
  143.         if (!is_numeric($page_inc)) {
  144.             return $this->_handleError('Gtk_Styled_Adjustment::page increment is expected to be a number.');
  145.         else {
  146.             $this->pageIncrement = $page_inc;
  147.         }
  148.         if (!is_numeric($page_size)) {
  149.             return $this->_handleError('Gtk_Styled_Adjustment::pageSize is expected to be a number.');
  150.         else {
  151.             $this->pageSize = $page_size;
  152.         }
  153.         
  154.         // Create the track and the bar.
  155.         $this->_createTrack();
  156.     }
  157.     
  158.     /**
  159.      * Get the final product that the user can use.
  160.      *
  161.      * To make things easier for the user, and to keep some
  162.      * consistency with other PEAR Gtk classes, the final usable
  163.      * object is called widget and is returned from the getWidget
  164.      * method.
  165.      * 
  166.      * @access public
  167.      * @param  none 
  168.      * @return &object 
  169.      */
  170.     function &getWidget()
  171.     {
  172.         return $this->widget;
  173.     }
  174.     
  175.     /**
  176.      * Get the container that will hold the adjustment track.
  177.      *
  178.      * The properties of the track container determin the direction
  179.      * that the adjustment will be shown. The possible options are
  180.      * GtkVBox or GtkHBox producing a vertical or horizontal adjustment
  181.      * respectively.
  182.      *
  183.      * @abstract
  184.      * @access   private
  185.      * @param    none 
  186.      * @return   widget  The container may not be a child of GtkBin.
  187.      */
  188.     function &_getTrackContainer()
  189.     {
  190.         // Abstract.
  191.         $this->_handleError('Gtk_Styled_Adjustment is an abstract class and ' .
  192.                              'should not be instantiated directly. Use ' .
  193.                              'Gtk_HStyleAdjustment or Gtk_VStyleAdjustment ' .
  194.                              'instead.');
  195.     }
  196.     
  197.     /**
  198.      * Create the track that the bar will live in.
  199.      *
  200.      * The track acts as the boundry for the bar. It contains the
  201.      * widget and sets a limit on the values that the bar may
  202.      * represent. The properties of the bar may be influenced by
  203.      * the properties of the track.
  204.      *
  205.      * Depending on the type of widget that uses the adjustment, the
  206.      * bar may not have to move with in the track but just grow and
  207.      * shrink within it. (ProgressBar) The track acts as a boundry for
  208.      * the bar no matter how it is used.
  209.      *
  210.      * @access private
  211.      * @param  none 
  212.      * @return void 
  213.      */
  214.     function _createTrack()
  215.     {
  216.         //  ALL THIS MOVEMENT BELONGS IN THE SCROLL BAR!!!
  217.         //  It is left here for demonstration purposes only.
  218.         
  219.         // Create the track that the bar will live in.
  220.         $this->track =$this->_getTrackContainer();
  221.         $this->_setTrackSize();
  222.  
  223.         // Create an event box to represent the empty space to the left.
  224.         $this->preTrack =new GtkEventBox;
  225.         $this->preTrack->set_usize($this->value-1);
  226.  
  227.         // Create an event box to represnet the empty space to the right.
  228.         $this->postTrack =new GtkEventBox;
  229.         
  230.         // Add everything to the track.
  231.         $this->track->pack_start($this->preTrackfalsefalse0);
  232.         $this->track->pack_start($this->_createBar()falsefalse0);
  233.         $this->track->pack_start($this->postTracktruetrue0);
  234.  
  235.         // Add the track to the widget.
  236.         $this->widget =new GtkHBox();
  237.         $this->widget->pack_start($this->tracktruetrue0);
  238.  
  239.         // Set the sizes.
  240.         $this->_setBarSize();
  241.         $this->_setTrackSize();
  242.         $this->_setBarPosition();
  243.     }
  244.     
  245.     /**
  246.      * Create the bar that visually represents the adjustment.
  247.      *
  248.      * The bar that lives within the track is capable of chaning size
  249.      * and position based on the values that the track represents.
  250.      *
  251.      * @access private
  252.      * @param  none 
  253.      * @return &$object 
  254.      */
  255.     function &_createBar()
  256.     {
  257.         $this->bar =new GtkButton(NULL);
  258.         $this->_setBarSize();
  259.         
  260.         return $this->bar;
  261.     }
  262.  
  263.     /**
  264.      * Set the size of the bar.
  265.      *
  266.      * The bar size is configurable. Depending on how the adjustment is
  267.      * used, the bar size may depend entirely on its value or it may
  268.      * change with respect to its environment. If the adjustment is used
  269.      * as a progress bar, the size of the bar is directly related to the
  270.      * percentage of the task that is complete. If the adjustment is used
  271.      * as a scroll bar, the size of the bar is related to the amount of
  272.      * the scrolling widget that is currently shown compared to its total
  273.      * size.
  274.      *
  275.      * @abstract
  276.      * @access   private
  277.      * @param    none 
  278.      * @return   void 
  279.      */
  280.     function _setBarSize()
  281.     {
  282.         // Abstract.
  283.         $this->_handleError('Gtk_Styled_Adjustment is an abstract class and ' .
  284.                              'should not be instantiated directly. Use ' .
  285.                              'Gtk_HStyleAdjustment or Gtk_VStyleAdjustment ' .
  286.                              'instead.');
  287.     }
  288.     
  289.     /**
  290.      * Set the bars position within the track.
  291.      *
  292.      * Set the starting position of the bar with in the track. The bar
  293.      * cannot start (length of track) - (length of bar). The position
  294.      * of the bar is mostly only relavent when the adjustment is being
  295.      * used as a scroll bar or when being used as an activity progress
  296.      * indicator.
  297.      *
  298.      * The bar position is determined by the width/height of the event
  299.      * box that preceeds the bar.
  300.      *
  301.      * @abstract
  302.      * @access   private
  303.      * @param    none 
  304.      * @return   void 
  305.      */
  306.     function _setBarPosition()
  307.     {
  308.         // Abstract
  309.         $this->_handleError('Gtk_Styled_Adjustment is an abstract class and ' .
  310.                              'should not be instantiated directly. Use ' .
  311.                              'Gtk_HStyleAdjustment or Gtk_VStyleAdjustment ' .
  312.                              'instead.');
  313.     }
  314.  
  315.     /**
  316.      * Set the size of the entire track.
  317.      *
  318.      * Sets the size of the entire track. The track contains the preTrack
  319.      * the bar and the postTrack.
  320.      *
  321.      * This method is abstract and should be overwritten in the child
  322.      * classes
  323.      *
  324.      * @abstract
  325.      * @access   private
  326.      * @param    none 
  327.      * @return   void 
  328.      */
  329.     function _setTrackSize()
  330.     {
  331.         // Abstract.
  332.         $this->_handleError('Gtk_Styled_Adjustment is an abstract class and ' .
  333.                              'should not be instantiated directly. Use ' .
  334.                              'Gtk_HStyleAdjustment or Gtk_VStyleAdjustment ' .
  335.                              'instead.');
  336.     }
  337.  
  338.     /**
  339.      * Set the current adjustment value.
  340.      *
  341.      * Sets the value that the adjustment represents. The value has an
  342.      * impact on the bar position and size.
  343.      *
  344.      * @access public
  345.      * @param  double  $value The new adjustment value.
  346.      * @return void 
  347.      */
  348.     function setValue($value)
  349.     {
  350.         // Check to see if the widget has been realized first.
  351.         $this->_checkRealized();
  352.         
  353.         // Set the new value
  354.         $this->value = $value;
  355.         
  356.         // Don't let the value exceed the bounds.
  357.         if ($this->value > $this->upper - $this->pageSize{
  358.             $this->value = $this->upper - $this->pageSize;
  359.         }
  360.         if ($this->value < $this->lower{
  361.             $this->value = $this->lower;
  362.         }
  363.         
  364.         // Set the bar position.
  365.         $this->_setBarPosition();
  366.  
  367.         return $this->value;
  368.     }
  369.     
  370.     /**
  371.      * Set the lower limit of the adjustment.
  372.      *
  373.      * Sets the lower limit of the adjustment widget. The lower limit
  374.      * affects the bar size and position.
  375.      *
  376.      * @access public
  377.      * @param  double $lower The new lower limit value.
  378.      * @return void 
  379.      */
  380.     function setLower($lower)
  381.     {
  382.         // Check to see if the widget has been realized first.
  383.         $this->_checkRealized();
  384.  
  385.         if (!is_numeric($lower)) {
  386.             return $this->_handleError('Lower expects a numeric value.');
  387.         }
  388.         
  389.         $this->lower = $lower;
  390.         $this->_setBarSize();
  391.         $this->_setBarPosition();
  392.     }
  393.     
  394.     /**
  395.      * Set the upper limit of the adjustment.
  396.      *
  397.      * Sets the upper limit of the adjustment widget. The upper limit
  398.      * affects the bar size and position.
  399.      *
  400.      * @access public
  401.      * @param  double $upper The new upper limit value.
  402.      * @return void 
  403.      */
  404.     function setUpper($upper)
  405.     {
  406.         // Check to see if the widget has been realized first.
  407.         $this->_checkRealized();
  408.  
  409.         if (!is_numeric($upper)) {
  410.             return $this->_handleError('Upper expects a numeric value.');
  411.         }
  412.         
  413.         $this->upper = $upper;
  414.  
  415.         $this->_setBarSize();
  416.         $this->_setBarPosition();
  417.     }
  418.     
  419.     /**
  420.      * Set the size of one step.
  421.      *
  422.      * Sets the size of one step. One step is the amount a scroll bar
  423.      * will move when the arrow at the begining or end of the scroll
  424.      * bar is pressed.
  425.      *
  426.      * @access public
  427.      * @param  double $increment The new size of one step
  428.      * @return void 
  429.      */
  430.     function setStepIncrement($increment)
  431.     {
  432.         // Check to see if the widget has been realized first.
  433.         $this->_checkRealized();
  434.  
  435.         if (!is_numeric($increment)) {
  436.             return $this->_handleError('Step increment expects a numeric value.');
  437.         }
  438.         
  439.         $this->stepIncrement = $increment;
  440.     }
  441.     
  442.     /**
  443.      * Set the size of one page movement.
  444.      *
  445.      * Sets the size of one page. One page is the amount a scroll bar
  446.      * will move when the space around the bar of a scroll bar is
  447.      * pressed.
  448.      *
  449.      * @access public
  450.      * @param  double $increment The new size of one page
  451.      * @return void 
  452.      */
  453.     function setPageIncrement($increment)
  454.     {
  455.         // Check to see if the widget has been realized first.
  456.         $this->_checkRealized();
  457.  
  458.         if (!is_numeric($increment)) {
  459.             return $this->_handleError('Page increment expects a numeric value.');
  460.         }
  461.         
  462.         $this->pageIncrement = $increment;
  463.     }
  464.     
  465.     /**
  466.      * Set the size of one page.
  467.      *
  468.      * Sets the size of one page. One page is the size of the display
  469.      * area. Changing the page size has an affect on the bar size and
  470.      * position.
  471.      *
  472.      * @access public
  473.      * @param  double $increment The new size of one page
  474.      * @return void 
  475.      */
  476.     function setPageSize($pageSize)
  477.     {
  478.         // Check to see if the widget has been realized first.
  479.         $this->_checkRealized();
  480.  
  481.         if (!is_numeric($pageSize)) {
  482.             return $this->_handleError('Page size expects a numeric value.');
  483.         }
  484.         
  485.         $this->pageSize = $pageSize;
  486.         $this->_setBarSize();
  487.         $this->_setBarPosition();
  488.     }
  489.     
  490.     /**
  491.      * Notify the system that a change has been made.
  492.      *
  493.      * @access public
  494.      * @param  none 
  495.      * @return void 
  496.      */
  497.     function changed()
  498.     {
  499.         // Empty
  500.     }
  501.     
  502.     /**
  503.      * Notify the system that a value has changed.
  504.      *
  505.      * @access public
  506.      * @param  none 
  507.      * @return void 
  508.      */
  509.     function value_changed()
  510.     {
  511.         // Empty.
  512.     }
  513.     
  514.     /**
  515.      * Prevent the bound of the track from being exceeded by the bar.
  516.      *
  517.      * @access public
  518.      * @param  double $lower The lower boundry.
  519.      * @param  double $upper The upper boundry.
  520.      * @return void 
  521.      */
  522.     function clamp_page($lower$upper)
  523.     {
  524.         // Empty.
  525.     }
  526.     
  527.     /**
  528.      * Alias of setValue. Added for API consistency.
  529.      *
  530.      * @see setValue
  531.      */
  532.     function set_value($value)
  533.     {
  534.         $this->setValue($value);
  535.     }
  536.     
  537.     /**
  538.      * Set the style for a portion of the adjustment.
  539.      *
  540.      * Each portion of the adjustment is styleable. The style that
  541.      * is passed will be applied to the portion of the adjustment.
  542.      *
  543.      * @access public
  544.      * @param  string $portion The portion to style.
  545.      * @param  widget &$style  The style to apply.
  546.      * @return void 
  547.      */
  548.     function setStyle($portion$style)
  549.     {
  550.         // Check for the portion.
  551.         if (!isset($this->$portion)) {
  552.             $this->_handleError('Undefined portion: ' $portion ' Cannot apply style.');
  553.         }
  554.         $this->$portion->set_style($style);
  555.     }
  556.  
  557.     /**
  558.      * Set the pix mask for the bar.
  559.      *
  560.      * Make the bar appear to be something other than a rectangle.
  561.      * This lets you create images for the bar so that it can be
  562.      * anything you want. This is the ultimate in adjustment
  563.      * customizatioin.
  564.      *
  565.      * NOTE: I don't know what will happen if you change the size
  566.      * of the bar when a mask is applied. Changing the page size
  567.      * and upper and lower values could have undesired effects.
  568.      *
  569.      * @access public
  570.      * @param  object &$mask The image to make the bar appear as.
  571.      * @return void 
  572.      */
  573.     function setBarMask(&$mask$x = 0$y = 0)
  574.     {
  575.         $this->bar->shape_combine_mask($mask$x$y);
  576.     }
  577.  
  578.     /**
  579.      * Set the contents of the bar to the given widget.
  580.      *
  581.      * This method makes it possible to put a label, pixmap, or
  582.      * any other widget into the bar in order to alter the bar's
  583.      * appearance. When combined with setBarMask(), this method
  584.      * makes the style an shape of the adjustment bar completely
  585.      * controllable by the programmer.
  586.      *
  587.      * The previous contents of the bar are returned after the
  588.      * new contents have been added.
  589.      *
  590.      * @access public
  591.      * @param  object &$widget The new widget to put in the bar.
  592.      * @return widget          The previous contents of the bar.
  593.      */
  594.     function &setBarContents(&$widget)
  595.     {
  596.         $prevChild $this->bar->child;
  597.         $this->bar->remove($prevChild);
  598.         $this->bar->add($widget);
  599.  
  600.         return $prevChild;
  601.     }
  602.     
  603.     /**
  604.      * Get the adjustments value.
  605.      *
  606.      * @access public
  607.      * @param  none 
  608.      * @return double 
  609.      */
  610.     function getValue()
  611.     {
  612.         return $this->value;
  613.     }
  614.  
  615.     /**
  616.      * Get the adjustments lower limit.
  617.      *
  618.      * @access public
  619.      * @param  none 
  620.      * @return double 
  621.      */
  622.     function getLower()
  623.     {
  624.         return $this->lower;
  625.     }
  626.  
  627.     /**
  628.      * Get the adjustments upper limit.
  629.      *
  630.      * @access public
  631.      * @param  none 
  632.      * @return double 
  633.      */
  634.     function getUpper()
  635.     {
  636.         return $this->upper;
  637.     }
  638.  
  639.     /**
  640.      * Get the adjustments step increment.
  641.      *
  642.      * @access public
  643.      * @param  none 
  644.      * @return double 
  645.      */
  646.     function getStepIncrement()
  647.     {
  648.         return $this->stepIncrement;
  649.     }
  650.  
  651.     /**
  652.      * Get the adjustments page increment.
  653.      *
  654.      * @access public
  655.      * @param  none 
  656.      * @return double 
  657.      */
  658.     function getPageIncrement()
  659.     {
  660.         return $this->pageIncrement;
  661.     }
  662.  
  663.     /**
  664.      * Get the adjustments page size.
  665.      *
  666.      * @access public
  667.      * @param  none 
  668.      * @return double 
  669.      */
  670.     function getPageSize()
  671.     {
  672.         return $this->pageSize;
  673.     }
  674.  
  675.     /**
  676.      * Check to see that the mouse is within the boundries of the
  677.      * bar.
  678.      *
  679.      * Check the current mouse position to see if it is within
  680.      * the bounds of the adjustment bar. It is only necessary to
  681.      * check if the mouse is with in one direction of the bar's
  682.      * alloted space. For example, we only care if the mouse is
  683.      * within the y-range of a vertical adjustment.
  684.      *
  685.      * @abstract
  686.      * @access   public
  687.      * @param    none 
  688.      * @return   boolean 
  689.      */
  690.     function checkMouseBounds()
  691.     {
  692.         // Abstract.
  693.         $this->_handleError('Gtk_Styled_Adjustment is an abstract class and ' .
  694.                             'should not be instantiated directly. Use ' .
  695.                             'Gtk_HStyleAdjustment or Gtk_VStyleAdjustment ' .
  696.                             'instead.');
  697.     }
  698.  
  699.     /**
  700.      * Checks to make sure that the widgets have been realized.
  701.      *
  702.      * Many of the classes that use Gtk_Styled_Adjustment objects
  703.      * expect that the widgets have been realized. This is
  704.      * becuase they use properties which are only set after
  705.      * object realization.
  706.      *
  707.      * @access private
  708.      * @param  none 
  709.      * @return boolean true if realized, false if not
  710.      */
  711.     function _checkRealized()
  712.     {
  713.         if (!isset($this->widget->window)) {
  714.             $this->_handleError('Setting values on a Gtk_Styled_Adjustment ' 
  715.                                 'that has not yet been realized can have ' .
  716.                                 'unexpected results.');
  717.             return false;
  718.         else {
  719.             return true;
  720.         }
  721.     }
  722.  
  723.     /**
  724.      * Error handling method.
  725.      *
  726.      * Errors should be handled with PEAR::Error_Stack
  727.      *
  728.      * @access private
  729.      * @param  string  $message 
  730.      * @param  integer $level 
  731.      * @return mixed 
  732.      */
  733.     function _handleError($msg$code = GTK_STYLED_ERROR$pearMode = PEAR_ERROR_PRINT)
  734.     {
  735.         // Require the pear class so that we can use its error functionality.
  736.         require_once ('PEAR.php');
  737.         
  738.         // Check whether or not we should print the error.
  739.         PEAR::raiseError($msg$code$pearMode);
  740.     }
  741. }
  742. /*
  743.  * Local variables:
  744.  * tab-width: 4
  745.  * c-basic-offset: 4
  746.  * End:
  747.  */
  748. ?>

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