Source/QuartzUtils.m
changeset 2 7b0441db81e5
parent 0 e9f7ba4718e1
child 3 40d225cf9c43
     1.1 --- a/Source/QuartzUtils.m	Fri Mar 07 11:43:02 2008 -0800
     1.2 +++ b/Source/QuartzUtils.m	Mon Mar 10 17:32:04 2008 -0700
     1.3 @@ -21,6 +21,7 @@
     1.4      THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     1.5  */
     1.6  #import "QuartzUtils.h"
     1.7 +#import <QuartzCore/QuartzCore.h>
     1.8  
     1.9  
    1.10  CGColorRef kBlackColor, kWhiteColor, 
    1.11 @@ -32,15 +33,36 @@
    1.12  __attribute__((constructor))        // Makes this function run when the app loads
    1.13  static void InitQuartzUtils()
    1.14  {
    1.15 -    kBlackColor = CGColorCreateGenericGray(0.0, 1.0);
    1.16 -    kWhiteColor = CGColorCreateGenericGray(1.0, 1.0);
    1.17 -    kTranslucentGrayColor = CGColorCreateGenericGray(0.0, 0.5);
    1.18 -    kTranslucentLightGrayColor = CGColorCreateGenericGray(0.0, 0.25);
    1.19 -    kAlmostInvisibleWhiteColor = CGColorCreateGenericGray(1, 0.05);
    1.20 -    kHighlightColor = CGColorCreateGenericRGB(1, 1, 0, 0.5);
    1.21 +    kBlackColor = CreateGray(0.0, 1.0);
    1.22 +    kWhiteColor = CreateGray(1.0, 1.0);
    1.23 +    kTranslucentGrayColor = CreateGray(0.0, 0.5);
    1.24 +    kTranslucentLightGrayColor = CreateGray(0.0, 0.25);
    1.25 +    kAlmostInvisibleWhiteColor = CreateGray(1, 0.05);
    1.26 +    kHighlightColor = CreateRGB(1, 1, 0, 0.5);
    1.27  }
    1.28  
    1.29  
    1.30 +#if TARGET_OS_ASPEN
    1.31 +CGColorRef CreateGray(CGFloat gray, CGFloat alpha)
    1.32 +{
    1.33 +    CGColorSpaceRef graySpace = CGColorSpaceCreateDeviceGray();
    1.34 +    CGFloat components[2] = {gray,alpha};
    1.35 +    CGColorRef color = CGColorCreate(graySpace, components);
    1.36 +    CGColorSpaceRelease(graySpace);
    1.37 +    return color;
    1.38 +}
    1.39 +
    1.40 +CGColorRef CreateRGB(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
    1.41 +{
    1.42 +    CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
    1.43 +    CGFloat components[4] = {red,green,blue,alpha};
    1.44 +    CGColorRef color = CGColorCreate(rgbSpace, components);
    1.45 +    CGColorSpaceRelease(rgbSpace);
    1.46 +    return color;
    1.47 +}
    1.48 +#endif
    1.49 +
    1.50 +
    1.51  void ChangeSuperlayer( CALayer *layer, CALayer *newSuperlayer, int index )
    1.52  {
    1.53      // Disable actions, else the layer will move to the wrong place and then back!
    1.54 @@ -75,49 +97,13 @@
    1.55  }    
    1.56  
    1.57  
    1.58 -CATextLayer* AddTextLayer( CALayer *superlayer,
    1.59 -                           NSString *text, NSFont* font,
    1.60 -                           enum CAAutoresizingMask align )
    1.61 -{
    1.62 -    CATextLayer *label = [[CATextLayer alloc] init];
    1.63 -    label.string = text;
    1.64 -    label.font = font;
    1.65 -    label.fontSize = font.pointSize;
    1.66 -    label.foregroundColor = kBlackColor;
    1.67 -    
    1.68 -    NSString *mode;
    1.69 -    if( align & kCALayerWidthSizable )
    1.70 -        mode = @"center";
    1.71 -    else if( align & kCALayerMinXMargin )
    1.72 -        mode = @"right";
    1.73 -    else
    1.74 -        mode = @"left";
    1.75 -    align |= kCALayerWidthSizable;
    1.76 -    label.alignmentMode = mode;
    1.77 -    
    1.78 -    CGFloat inset = superlayer.borderWidth + 3;
    1.79 -    CGRect bounds = CGRectInset(superlayer.bounds, inset, inset);
    1.80 -    CGFloat height = font.ascender;
    1.81 -    CGFloat y = bounds.origin.y;
    1.82 -    if( align & kCALayerHeightSizable )
    1.83 -        y += (bounds.size.height-height)/2.0;
    1.84 -    else if( align & kCALayerMinYMargin )
    1.85 -        y += bounds.size.height - height;
    1.86 -    align &= ~kCALayerHeightSizable;
    1.87 -    label.bounds = CGRectMake(0, font.descender,
    1.88 -                              bounds.size.width, height - font.descender);
    1.89 -    label.position = CGPointMake(bounds.origin.x,y+font.descender);
    1.90 -    label.anchorPoint = CGPointMake(0,0);
    1.91 -    
    1.92 -    label.autoresizingMask = align;
    1.93 -    [superlayer addSublayer: label];
    1.94 -    [label release];
    1.95 -    return label;
    1.96 -}
    1.97 -
    1.98 -
    1.99  CGImageRef CreateCGImageFromFile( NSString *path )
   1.100  {
   1.101 +#if TARGET_OS_ASPEN
   1.102 +    UIImage *uiImage = [UIImage imageWithContentsOfFile: path];
   1.103 +    if(!uiImage) NSLog(@"Warning: UIImage imageWithContentsOfFile failed on file %@",path);
   1.104 +    return CGImageRetain(uiImage.CGImage);
   1.105 +#else
   1.106      CGImageRef image = NULL;
   1.107      CFURLRef url = (CFURLRef) [NSURL fileURLWithPath: path];
   1.108      CGImageSourceRef src = CGImageSourceCreateWithURL(url, NULL);
   1.109 @@ -127,11 +113,18 @@
   1.110          if(!image) NSLog(@"Warning: CGImageSourceCreateImageAtIndex failed on file %@ (ptr size=%u)",path,sizeof(void*));
   1.111      }
   1.112      return image;
   1.113 +#endif
   1.114  }
   1.115  
   1.116  
   1.117  CGImageRef GetCGImageNamed( NSString *name )
   1.118  {
   1.119 +#if TARGET_OS_ASPEN
   1.120 +    name = name.lastPathComponent;
   1.121 +    UIImage *uiImage = [UIImage imageNamed: name];
   1.122 +    NSCAssert1(uiImage,@"Couldn't find bundle image resource '%@'",name);
   1.123 +    return uiImage.CGImage;
   1.124 +#else
   1.125      // For efficiency, loaded images are cached in a dictionary by name.
   1.126      static NSMutableDictionary *sMap;
   1.127      if( ! sMap )
   1.128 @@ -153,6 +146,7 @@
   1.129          CGImageRelease(image);
   1.130      }
   1.131      return image;
   1.132 +#endif
   1.133  }
   1.134  
   1.135  
   1.136 @@ -173,6 +167,7 @@
   1.137  }
   1.138  
   1.139  
   1.140 +#if ! TARGET_OS_ASPEN
   1.141  CGImageRef GetCGImageFromPasteboard( NSPasteboard *pb )
   1.142  {
   1.143      CGImageSourceRef src = NULL;
   1.144 @@ -195,11 +190,17 @@
   1.145          return image;
   1.146      } else
   1.147          return NULL;
   1.148 -}    
   1.149 +}
   1.150 +#endif
   1.151  
   1.152  
   1.153  float GetPixelAlpha( CGImageRef image, CGSize imageSize, CGPoint pt )
   1.154  {
   1.155 +#if TARGET_OS_ASPEN
   1.156 +    // iPhone uses "flipped" (i.e. normal) coords, so images are wrong-way-up
   1.157 +    pt.y = imageSize.height - pt.y;
   1.158 +#endif
   1.159 +    
   1.160      // Trivial reject:
   1.161      if( pt.x<0 || pt.x>=imageSize.width || pt.y<0 || pt.y>=imageSize.height )
   1.162          return 0.0;
   1.163 @@ -271,3 +272,24 @@
   1.164      CGPatternRelease(pattern);
   1.165      return color;
   1.166  }
   1.167 +
   1.168 +
   1.169 +#pragma mark -
   1.170 +#pragma mark PATHS:
   1.171 +
   1.172 +
   1.173 +void AddRoundRect( CGContextRef ctx, CGRect rect, CGFloat radius )
   1.174 +{
   1.175 +    radius = MIN(radius, floorf(rect.size.width/2));
   1.176 +    float x0 = CGRectGetMinX(rect), y0 = CGRectGetMinY(rect),
   1.177 +    x1 = CGRectGetMaxX(rect), y1 = CGRectGetMaxY(rect);
   1.178 +    
   1.179 +    CGContextBeginPath(ctx);
   1.180 +    CGContextMoveToPoint(ctx,x0+radius,y0);
   1.181 +    CGContextAddArcToPoint(ctx,x1,y0, x1,y1, radius);
   1.182 +    CGContextAddArcToPoint(ctx,x1,y1, x0,y1, radius);
   1.183 +    CGContextAddArcToPoint(ctx,x0,y1, x0,y0, radius);
   1.184 +    CGContextAddArcToPoint(ctx,x0,y0, x1,y0, radius);
   1.185 +    CGContextClosePath(ctx);
   1.186 +}
   1.187 +