5 // Created by Sean Patrick O'Brien on 9/23/06.
6 // Copyright 2006 Sean Patrick O'Brien. All rights reserved.
9 #import "NFIButtonCell.h"
10 #import "NSImage+FrameworkImage.h"
12 @implementation NFIButtonCell
14 - (void)drawImage:(NSImage*)image withFrame:(NSRect)frame inView:(NSView*)controlView
16 if([self showsStateBy] == NSNoCellMask){
17 [super drawImage:image withFrame:frame inView:controlView];
21 NSString *state = [self isEnabled] ? ([self isHighlighted] ? @"P" : @"N") : @"D";
22 NSString *position = [self intValue] ? @"On" : @"Off";
23 NSImage *checkImage = [NSImage frameworkImageNamed:[NSString stringWithFormat:@"Checkbox%@%@.tiff", position, state]];
25 NSSize size = [checkImage size];
27 float y = NSMaxY(frame) - (frame.size.height-size.height)/2.0;
28 float x = frame.origin.x+addX;
30 [checkImage compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver];
33 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
35 if([self showsStateBy] != NSNoCellMask){
36 [super drawInteriorWithFrame:cellFrame inView:controlView];
40 NSString *state = [self isEnabled] ? ([self isHighlighted] ? @"P" : @"N") : @"D";
41 NSImage *leftImage = [NSImage frameworkImageNamed:[NSString stringWithFormat:@"ButtonLeft%@.tiff", state]];
42 NSImage *fillImage = [NSImage frameworkImageNamed:[NSString stringWithFormat:@"ButtonFill%@", state]];
43 NSImage *rightImage = [NSImage frameworkImageNamed:[NSString stringWithFormat:@"ButtonRight%@", state]];
45 NSSize size = [leftImage size];
46 float addX = size.width / 2.0;
47 float y = NSMaxY(cellFrame) - (cellFrame.size.height-size.height)/2.0;
48 float x = cellFrame.origin.x+addX;
49 float fillX = x + size.width;
50 float fillWidth = cellFrame.size.width - size.width - addX;
52 [leftImage compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver];
54 size = [rightImage size];
55 addX = size.width / 2.0;
56 x = NSMaxX(cellFrame) - size.width - addX;
57 fillWidth -= size.width+addX;
59 [rightImage compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver];
61 [fillImage setScalesWhenResized:YES];
62 [fillImage setSize:NSMakeSize(fillWidth, [fillImage size].height)];
63 [fillImage compositeToPoint:NSMakePoint(fillX, y) operation:NSCompositeSourceOver];
66 - (void)drawImageWithFrame:(NSRect)frame inView:(NSButton *)view
69 if ([self isHighlighted] && [self alternateImage])
70 image = [self alternateImage];
74 [super drawImage:image withFrame:NSOffsetRect(frame, 0, ([view isFlipped] ? -1 : 1)) inView:view];
77 - (void)drawWithFrame:(NSRect)frame inView:(NSButton *)view
79 [self drawInteriorWithFrame:frame inView:view];
81 if([self showsStateBy] != NSNoCellMask){
85 [NSGraphicsContext saveGraphicsState];
86 NSShadow* theShadow = [[NSShadow alloc] init];
87 [theShadow setShadowOffset:NSMakeSize(0, -1)];
88 [theShadow setShadowBlurRadius:0.9];
90 [theShadow setShadowColor:[[NSColor whiteColor]
91 colorWithAlphaComponent:1.0]];
95 if ([self image] && [self imagePosition] != NSNoImage)
96 [self drawImageWithFrame:frame inView:view];
98 [self drawTitle:[view attributedTitle] withFrame:frame inView:view];
100 [NSGraphicsContext restoreGraphicsState];