PageRenderTime 62ms CodeModel.GetById 11ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSBezierPath+RoundRect.m

http://macfuse.googlecode.com/
Objective C | 101 lines | 64 code | 12 blank | 25 comment | 3 complexity | b660e070ec14fbbc994a126102107998 MD5 | raw file
  1//
  2//  GTMNSBezierPath+RoundRect.h
  3//
  4//  Category for adding utility functions for creating
  5//  round rectangles.
  6//
  7//  Copyright 2006-2008 Google Inc.
  8//
  9//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 10//  use this file except in compliance with the License.  You may obtain a copy
 11//  of the License at
 12// 
 13//  http://www.apache.org/licenses/LICENSE-2.0
 14// 
 15//  Unless required by applicable law or agreed to in writing, software
 16//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 17//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 18//  License for the specific language governing permissions and limitations under
 19//  the License.
 20//
 21
 22#import "GTMNSBezierPath+RoundRect.h"
 23
 24@implementation NSBezierPath (GTMBezierPathRoundRectAdditions)
 25
 26
 27+ (NSBezierPath *)gtm_bezierPathWithRoundRect:(NSRect)rect
 28                                 cornerRadius:(CGFloat)radius {
 29  NSBezierPath *bezier = [NSBezierPath bezierPath];
 30  [bezier gtm_appendBezierPathWithRoundRect:rect cornerRadius:radius];
 31  return bezier;
 32}
 33
 34+ (NSBezierPath *)gtm_bezierPathWithRoundRect:(NSRect)rect
 35                          topLeftCornerRadius:(CGFloat)radiusTL
 36                         topRightCornerRadius:(CGFloat)radiusTR
 37                       bottomLeftCornerRadius:(CGFloat)radiusBL
 38                      bottomRightCornerRadius:(CGFloat)radiusBR {
 39  NSBezierPath *bezier = [NSBezierPath bezierPath];
 40  [bezier gtm_appendBezierPathWithRoundRect:rect
 41                        topLeftCornerRadius:radiusTL
 42                       topRightCornerRadius:radiusTR
 43                     bottomLeftCornerRadius:radiusBL
 44                    bottomRightCornerRadius:radiusBR];
 45  return bezier;
 46}
 47
 48- (void)gtm_appendBezierPathWithRoundRect:(NSRect)rect
 49                             cornerRadius:(CGFloat)radius {
 50  if (radius > 0.0) {
 51    // Clamp radius to be no larger than half the rect's width or height.
 52    radius = MIN(radius, 0.5 * MIN(rect.size.width, rect.size.height));
 53    
 54    [self gtm_appendBezierPathWithRoundRect:rect
 55                        topLeftCornerRadius:radius
 56                       topRightCornerRadius:radius
 57                     bottomLeftCornerRadius:radius
 58                    bottomRightCornerRadius:radius];
 59  } else {
 60    // When radius <= 0.0, use plain rectangle.
 61    [self appendBezierPathWithRect:rect];
 62  }
 63}
 64
 65- (void)gtm_appendBezierPathWithRoundRect:(NSRect)rect
 66                      topLeftCornerRadius:(CGFloat)radiusTL
 67                     topRightCornerRadius:(CGFloat)radiusTR
 68                   bottomLeftCornerRadius:(CGFloat)radiusBL
 69                  bottomRightCornerRadius:(CGFloat)radiusBR {
 70  // Clamp radii to be at least zero. I'd like to clamp both TL+TR and BL+BR to
 71  // be less than the width and TL+BL and TR+BR to be less than the height, but
 72  // what to do if they're not? Do we scale them both evenly?
 73  radiusTL = MAX(0, radiusTL);
 74  radiusTR = MAX(0, radiusTR);
 75  radiusBL = MAX(0, radiusBL);
 76  radiusBR = MAX(0, radiusBR);
 77  
 78  if (!NSIsEmptyRect(rect)) {
 79    NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
 80    NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
 81    NSPoint bottomRight = NSMakePoint(NSMaxX(rect), NSMinY(rect));
 82    
 83    [self moveToPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))];
 84    [self appendBezierPathWithArcFromPoint:topLeft
 85                                   toPoint:rect.origin
 86                                    radius:radiusTL];
 87    [self appendBezierPathWithArcFromPoint:rect.origin
 88                                   toPoint:bottomRight
 89                                    radius:radiusBL];
 90    [self appendBezierPathWithArcFromPoint:bottomRight
 91                                   toPoint:topRight
 92                                    radius:radiusBR];
 93    [self appendBezierPathWithArcFromPoint:topRight
 94                                   toPoint:topLeft
 95                                    radius:radiusTR];
 96    [self closePath];
 97  }
 98}
 99
100
101@end