// // logosxBezelView.m // LOGOSX // // Created by micah aci on Sun Nov 14 2004. // Copyright (c) 2004 __MyCompanyName__. All rights reserved. // #import "logosxBezelView.h" #define BORDER_RADIUS 20.0 #define ELIPSIS_STRING @"..." @implementation logosxBezelView - (id)initWithFrame:(NSRect)frame { if ( self = [super initWithFrame:frame] ) { _title = nil; _text = nil; _textHeight = 0; _target = nil; _action = nil; } return self; } - (void)dealloc { [_title release]; [_text release]; _title = nil; _text = nil; _target = nil; [super dealloc]; } - (void)drawRect:(NSRect)rect { NSRect bounds = [self bounds]; NSBezierPath *bezelPath = [NSBezierPath bezierPath]; NSPoint topLeft = NSMakePoint(bounds.origin.x, bounds.origin.y); NSPoint topRight = NSMakePoint(bounds.origin.x + bounds.size.width, bounds.origin.y); NSPoint bottomLeft = NSMakePoint(bounds.origin.x, bounds.origin.y + bounds.size.height); NSPoint bottomRight = NSMakePoint(bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height); [[NSColor clearColor] set]; NSRectFill( [self frame] ); [bezelPath appendBezierPathWithArcWithCenter:NSMakePoint(topLeft.x + BORDER_RADIUS, topLeft.y + BORDER_RADIUS) radius:BORDER_RADIUS startAngle:180 endAngle:270 clockwise:NO]; [bezelPath lineToPoint:NSMakePoint(topRight.x - BORDER_RADIUS, topRight.y)]; [bezelPath appendBezierPathWithArcWithCenter:NSMakePoint(topRight.x - BORDER_RADIUS, topRight.y + BORDER_RADIUS) radius:BORDER_RADIUS startAngle:270 endAngle:0 clockwise:NO]; [bezelPath lineToPoint:NSMakePoint(bottomRight.x, bottomRight.y - BORDER_RADIUS)]; [bezelPath appendBezierPathWithArcWithCenter:NSMakePoint(bottomRight.x - BORDER_RADIUS, bottomRight.y - BORDER_RADIUS) radius:BORDER_RADIUS startAngle:0 endAngle:90 clockwise:NO]; [bezelPath lineToPoint:NSMakePoint(bottomLeft.x + BORDER_RADIUS, bottomLeft.y)]; [bezelPath appendBezierPathWithArcWithCenter:NSMakePoint(bottomLeft.x + BORDER_RADIUS, bottomLeft.y - BORDER_RADIUS) radius:BORDER_RADIUS startAngle:90 endAngle:180 clockwise:NO]; [bezelPath lineToPoint:NSMakePoint(topLeft.x, topLeft.y + BORDER_RADIUS)]; int opacityPref = 20; [[NSColor colorWithCalibratedRed:0. green:0. blue:0. alpha:((float)opacityPref/100.)] set]; [bezelPath fill]; // rects NSRect titleRect, textRect; int maxRows; titleRect = NSMakeRect(8., 52., 143., 24.); textRect = NSMakeRect(8., 4., 143., 49.); maxRows = 2; // Draw the title, resize if text too big NSMutableParagraphStyle *parrafo = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] setAlignment:NSCenterTextAlignment]; NSMutableDictionary *titleAttributes = [[NSMutableDictionary dictionaryWithObjectsAndKeys: [NSColor whiteColor], NSForegroundColorAttributeName, parrafo, NSParagraphStyleAttributeName, nil] retain]; float titleFontSize = 20.0; float accumulator = 0.; BOOL minFontSize = NO; [titleAttributes setObject:[[NSFontManager sharedFontManager] convertFont:[NSFont systemFontOfSize:titleFontSize] toHaveTrait: NSBoldFontMask] forKey:NSFontAttributeName]; NSSize titleSize = [_title sizeWithAttributes:titleAttributes]; while ( titleSize.width > ( NSWidth(titleRect) - ( titleSize.height / 2. ) ) ) { minFontSize = ( titleFontSize <= 12. ); if ( minFontSize ) { [self setTitle: [_title substringToIndex:[_title length] - 1]]; } else { titleFontSize -= 1.; accumulator += 0.5; } [titleAttributes setObject:[[NSFontManager sharedFontManager] convertFont:[NSFont systemFontOfSize:titleFontSize] toHaveTrait: NSBoldFontMask] forKey:NSFontAttributeName]; titleSize = [_title sizeWithAttributes:titleAttributes]; } titleRect.origin.y += ceil(accumulator); if ( minFontSize ) { [self setTitle: [NSString stringWithFormat:@"%@%@",[_title substringToIndex:[_title length]-1], ELIPSIS_STRING]]; } titleRect.size.height = titleSize.height; [_title drawInRect:titleRect withAttributes:titleAttributes]; NSMutableDictionary *textAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSColor whiteColor], NSForegroundColorAttributeName, parrafo, NSParagraphStyleAttributeName, [NSFont systemFontOfSize:14.0], NSFontAttributeName, nil]; NSAttributedString *_textAttributed = [[[NSAttributedString alloc] initWithString:_text attributes:textAttributes] autorelease]; if ( [self descriptionRowCount:_textAttributed inRect:textRect] > maxRows ) { [textAttributes setObject:[NSFont systemFontOfSize:12.0] forKey:NSFontAttributeName]; } [_text drawInRect:textRect withAttributes:textAttributes]; } - (void)setTitle:(NSString *)title { [_title autorelease]; _title = [title copy]; [self setNeedsDisplay:YES]; } - (void)setText:(NSString *)text { [_text autorelease]; _text = [text copy]; _textHeight = 0; [self setNeedsDisplay:YES]; } - (float)descriptionHeight:(NSAttributedString *)text inRect:(NSRect)theRect { if (_textHeight == 0) { NSTextStorage* textStorage = [[NSTextStorage alloc] initWithAttributedString:text]; NSTextContainer* textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(NSWidth(theRect),NSHeight(theRect)+1000.)] autorelease]; NSLayoutManager* layoutManager = [[[NSLayoutManager alloc] init] autorelease]; [layoutManager addTextContainer:textContainer]; [textStorage addLayoutManager:layoutManager]; (void)[layoutManager glyphRangeForTextContainer:textContainer]; _textHeight = [layoutManager usedRectForTextContainer:textContainer].size.height; _textHeight = _textHeight / 13 * 14; } return MAX (_textHeight, 30); } - (int)descriptionRowCount:(NSAttributedString *)text inRect:(NSRect)theRect{ float height = [self descriptionHeight:text inRect:theRect]; float lineHeight = [text size].height; return (int) (height / lineHeight); } - (id) target { return _target; } - (void) setTarget:(id) object { _target = object; } #pragma mark - - (SEL) action { return _action; } - (void) setAction:(SEL) selector { _action = selector; } #pragma mark - - (void) mouseUp:(NSEvent *) event { if( _target && _action && [_target respondsToSelector:_action] ) [_target performSelector:_action withObject:self]; } @end