API 0.9.5
AppKit/CPGraphics.j
Go to the documentation of this file.
00001 /*
00002  * CPGraphics.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2010, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 function CPDrawTiledRects(
00026    /* CGRect */ boundsRect,
00027    /* CGRect */ clipRect,
00028    /* CPRectEdge[] */ sides,
00029    /* float[] */ grays)
00030 {
00031     if (sides.length != grays.length)
00032         [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and grays (length: " + grays.length + ") must have the same length."];
00033 
00034     var colors = [];
00035 
00036     for (var i = 0; i < grays.length; ++i)
00037         colors.push([CPColor colorWithCalibratedWhite:grays[i] alpha:1.0]);
00038 
00039     return CPDrawColorTiledRects(boundsRect, clipRect, sides, colors);
00040 }
00041 
00042 function CPDrawColorTiledRects(
00043    /* CGRect */ boundsRect,
00044    /* CGRect */ clipRect,
00045    /* CPRectEdge[] */ sides,
00046    /* CPColor[] */ colors)
00047 {
00048     if (sides.length != colors.length)
00049         [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and colors (length: " + colors.length + ") must have the same length."];
00050 
00051     var resultRect = _CGRectMakeCopy(boundsRect),
00052         slice = _CGRectMakeZero(),
00053         remainder = _CGRectMakeZero(),
00054         context = [[CPGraphicsContext currentContext] graphicsPort];
00055 
00056     CGContextSaveGState(context);
00057     CGContextSetLineWidth(context, 1.0);
00058 
00059     for (var sideIndex = 0; sideIndex < sides.length; ++sideIndex)
00060     {
00061         var side = sides[sideIndex];
00062 
00063         CGRectDivide(resultRect, slice, remainder, 1.0, side);
00064         resultRect = remainder;
00065         slice = CGRectIntersection(slice, clipRect);
00066 
00067         // Cocoa docs say that only slices that are within the clipRect are actually drawn
00068         if (_CGRectIsEmpty(slice))
00069             continue;
00070 
00071         var minX, maxX, minY, maxY;
00072 
00073         if (side == CPMinXEdge || side == CPMaxXEdge)
00074         {
00075             // Make sure we have at least 1 pixel to draw a line
00076             if (_CGRectGetWidth(slice) < 1.0)
00077                 continue;
00078 
00079             minX = _CGRectGetMinX(slice) + 0.5;
00080             maxX = minX;
00081             minY = _CGRectGetMinY(slice);
00082             maxY = _CGRectGetMaxY(slice);
00083         }
00084         else // CPMinYEdge || CPMaxYEdge
00085         {
00086             // Make sure we have at least 1 pixel to draw a line
00087             if (_CGRectGetHeight(slice) < 1.0)
00088                 continue;
00089 
00090             minX = _CGRectGetMinX(slice);
00091             maxX = _CGRectGetMaxX(slice);
00092             minY = _CGRectGetMinY(slice) + 0.5;
00093             maxY = minY;
00094         }
00095 
00096         CGContextBeginPath(context);
00097         CGContextMoveToPoint(context, minX, minY);
00098         CGContextAddLineToPoint(context, maxX, maxY);
00099         CGContextSetStrokeColor(context, colors[sideIndex]);
00100         CGContextStrokePath(context);
00101     }
00102 
00103     CGContextRestoreGState(context);
00104 
00105     return resultRect;
00106 }
 All Classes Files Functions Variables Defines