1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  import org.aswing.ASColor;
     6  import org.aswing.border.Border;
     7  import org.aswing.border.DecorateBorder;
     8  import org.aswing.Component;
     9  import org.aswing.geom.Rectangle;
    10  import org.aswing.graphices.AdvancedPen;
    11  import org.aswing.graphices.Graphics;
    12  import org.aswing.Insets;
    13  
    14  /**
    15   * A class which implements a simple 2 line bevel border.
    16   * @author iiley
    17   */
    18  class org.aswing.border.BevelBorder extends DecorateBorder {
    19  	
    20      /** Raised bevel type. */
    21      public static var RAISED:Number  = 0;
    22      /** Lowered bevel type. */
    23      public static var LOWERED:Number = 1;
    24  
    25      private var bevelType:Number;
    26      private var highlightOuter:ASColor;
    27      private var highlightInner:ASColor;
    28      private var shadowInner:ASColor;
    29      private var shadowOuter:ASColor;
    30  
    31      /**
    32       * BevelBorder()<br> default bevelType to LOWERED<br>
    33       * BevelBorder(interior:Border, bevelType:Number)<br>
    34       * Creates a bevel border with the specified type and whose
    35       * colors will be derived from the background color of the
    36       * component passed into the paintBorder method.
    37       * <p>
    38       * BevelBorder(interior:Border, bevelType:Number, highlight:ASColor, shadow:ASColor)<br>
    39       * Creates a bevel border with the specified type, highlight and
    40       * shadow colors.
    41       * <p>
    42       * BevelBorder(interior:Border, bevelType:Number, highlightOuterColor:ASColor, highlightInnerColor:ASColor, shadowOuterColor:ASColor, shadowInnerColor:ASColor)<br>
    43       * Creates a bevel border with the specified type, specified four colors.
    44       * @param interior the border in this border
    45       * @param bevelType the type of bevel for the border
    46       * @param highlightOuterColor the color to use for the bevel outer highlight
    47       * @param highlightInnerColor the color to use for the bevel inner highlight
    48       * @param shadowOuterColor the color to use for the bevel outer shadow
    49       * @param shadowInnerColor the color to use for the bevel inner shadow
    50       */
    51      public function BevelBorder(interior:Border, bevelType:Number, highlightOuterColor:ASColor, 
    52                         highlightInnerColor:ASColor, shadowOuterColor:ASColor, 
    53                         shadowInnerColor:ASColor) {
    54          super(interior);
    55          this.bevelType = bevelType;
    56          if(highlightInnerColor != undefined && shadowOuterColor == undefined){
    57          	this.highlightOuter = highlightOuterColor.brighter();
    58          	this.highlightInner = highlightOuterColor;
    59          	this.shadowOuter = shadowOuterColor;
    60          	this.shadowInner = shadowOuterColor.brighter();
    61          }else{
    62          	this.highlightOuter = highlightOuterColor;
    63          	this.highlightInner = highlightInnerColor;
    64          	this.shadowOuter = shadowOuterColor;
    65          	this.shadowInner = shadowInnerColor;
    66          }
    67      }
    68  
    69  
    70      public function paintBorderImp(c:Component, g:Graphics, b:Rectangle):Void{
    71          if (bevelType == RAISED) {
    72               paintRaisedBevel(c, g, b.x, b.y, b.width, b.height);
    73          }else{
    74               paintLoweredBevel(c, g, b.x, b.y, b.width, b.height);
    75          }
    76      }
    77      
    78      public function getBorderInsetsImp(c:Component, bounds:Rectangle):Insets{
    79      	return new Insets(2, 2, 2, 2);
    80      }
    81  
    82      /**
    83       * Returns the outer highlight color of the bevel border
    84       * when rendered on the specified component.  If no highlight
    85       * color was specified at instantiation, the highlight color
    86       * is derived from the specified component's background color.
    87       * @param c the component for which the highlight may be derived
    88       */
    89      public function getHighlightOuterColorWith(c:Component):ASColor{
    90          var highlight:ASColor = getHighlightOuterColor();
    91          if(highlight == undefined){
    92          	highlight = c.getBackground().brighter().brighter();
    93          }
    94          return highlight;
    95      }
    96  
    97      /**
    98       * Returns the inner highlight color of the bevel border
    99       * when rendered on the specified component.  If no highlight
   100       * color was specified at instantiation, the highlight color
   101       * is derived from the specified component's background color.
   102       * @param c the component for which the highlight may be derived
   103       */
   104      public function getHighlightInnerColorWith(c:Component):ASColor{
   105          var highlight:ASColor = getHighlightInnerColor();
   106          if(highlight == undefined){
   107          	highlight = c.getBackground().brighter();
   108          }
   109          return highlight;
   110      }
   111  
   112      /**
   113       * Returns the inner shadow color of the bevel border
   114       * when rendered on the specified component.  If no shadow
   115       * color was specified at instantiation, the shadow color
   116       * is derived from the specified component's background color.
   117       * @param c the component for which the shadow may be derived
   118       */
   119      public function getShadowInnerColorWith(c:Component):ASColor{
   120          var shadow:ASColor = getShadowInnerColor();
   121          if(shadow == undefined){
   122          	shadow = c.getBackground().darker();
   123          }
   124          return shadow ;
   125                                      
   126      }
   127  
   128      /**
   129       * Returns the outer shadow color of the bevel border
   130       * when rendered on the specified component.  If no shadow
   131       * color was specified at instantiation, the shadow color
   132       * is derived from the specified component's background color.
   133       * @param c the component for which the shadow may be derived
   134       */
   135      public function getShadowOuterColorWith(c:Component):ASColor{
   136          var shadow:ASColor = getShadowOuterColor();
   137          if(shadow == undefined){
   138          	shadow = c.getBackground().darker().darker();
   139          }
   140          return shadow ;
   141      }
   142  
   143      /**
   144       * Returns the outer highlight color of the bevel border.
   145       * Will return null if no highlight color was specified
   146       * at instantiation.
   147       */
   148      public function getHighlightOuterColor():ASColor{
   149          return highlightOuter;
   150      }
   151  
   152      /**
   153       * Returns the inner highlight color of the bevel border.
   154       * Will return null if no highlight color was specified
   155       * at instantiation.
   156       */
   157      public function getHighlightInnerColor():ASColor{
   158          return highlightInner;
   159      }
   160  
   161      /**
   162       * Returns the inner shadow color of the bevel border.
   163       * Will return null if no shadow color was specified
   164       * at instantiation.
   165       */
   166      public function getShadowInnerColor():ASColor{
   167          return shadowInner;
   168      }
   169  
   170      /**
   171       * Returns the outer shadow color of the bevel border.
   172       * Will return null if no shadow color was specified
   173       * at instantiation.
   174       */
   175      public function getShadowOuterColor():ASColor{
   176          return shadowOuter;
   177      }
   178  
   179      /**
   180       * Returns the type of the bevel border.
   181       */
   182      public function getBevelType():Number{
   183          return bevelType;
   184      }
   185  
   186      private function paintRaisedBevel(c:Component, g:Graphics, x:Number, y:Number,
   187                                      width:Number, height:Number):Void  {
   188          var h:Number = height;
   189          var w:Number = width;
   190          x += 0.5;
   191          y += 0.5;
   192          w -= 1;
   193          h -= 1;
   194          var pen:AdvancedPen = new AdvancedPen(getHighlightOuterColorWith(c), 1, undefined, "normal", "square", "miter");
   195          g.drawLine(pen, x, y, x, y+h-2);
   196          g.drawLine(pen, x+1, y, x+w-2, y);
   197  		
   198  		pen.setASColor(getHighlightInnerColorWith(c));
   199          g.drawLine(pen, x+1, y+1, x+1, y+h-3);
   200          g.drawLine(pen, x+2, y+1, x+w-3, y+1);
   201  
   202  		pen.setASColor(getShadowOuterColorWith(c));
   203          g.drawLine(pen, x, y+h-1, x+w-1, y+h-1);
   204          g.drawLine(pen, x+w-1, y, x+w-1, y+h-2);
   205  
   206  		pen.setASColor(getShadowInnerColorWith(c));
   207          g.drawLine(pen, x+1, y+h-2, x+w-2, y+h-2);
   208          g.drawLine(pen, x+w-2, y+1, x+w-2, y+h-3);
   209  
   210      }
   211  
   212      private function paintLoweredBevel(c:Component, g:Graphics, x:Number, y:Number,
   213                                      width:Number, height:Number):Void  {
   214          var h:Number = height;
   215          var w:Number = width;
   216          x += 0.5;
   217          y += 0.5;
   218          w -= 1;
   219          h -= 1;
   220          var pen:AdvancedPen = new AdvancedPen(getShadowInnerColorWith(c), 1, undefined, "normal", "square", "miter");
   221          g.drawLine(pen, x, y, x, y+h-1);
   222          g.drawLine(pen, x+1, y, x+w-1, y);
   223  
   224         	pen.setASColor(getShadowOuterColorWith(c));
   225          g.drawLine(pen, x+1, y+1, x+1, y+h-2);
   226          g.drawLine(pen, x+2, y+1, x+w-2, y+1);
   227  
   228          pen.setASColor(getHighlightOuterColorWith(c));
   229          g.drawLine(pen, x+1, y+h-1, x+w-1, y+h-1);
   230          g.drawLine(pen, x+w-1, y+1, x+w-1, y+h-2);
   231  
   232          pen.setASColor(getHighlightInnerColorWith(c));
   233          g.drawLine(pen, x+2, y+h-2, x+w-2, y+h-2);
   234          g.drawLine(pen, x+w-2, y+2, x+w-2, y+h-3);
   235      }
   236  
   237  }
   238