2 // AIContactInfoImageViewWithImagePicker.m
5 // Created by Evan Schoenberg on 10/1/06.
8 #import "AIContactInfoImageViewWithImagePicker.h"
9 #import <AIUtilities/AIBezierPathAdditions.h>
10 #import <AIUtilities/AIImageAdditions.h>
12 @interface AIContactInfoImageViewWithImagePicker (PRIVATE)
13 - (void)resetCursorRects;
14 - (NSRect)_snapbackRectForFrame:(NSRect)cellFrame;
17 @implementation AIContactInfoImageViewWithImagePicker
19 - (void)configureTracking
21 resetImageTrackingTag = -1;
22 [self resetCursorRects];
25 - (id)initWithFrame:(NSRect)inFrame
27 if ((self = [super initWithFrame:inFrame])) {
28 [self configureTracking];
36 if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
40 [self configureTracking];
45 if (resetImageTrackingTag != -1) {
46 [self removeTrackingRect:resetImageTrackingTag];
47 resetImageTrackingTag = -1;
54 - (void)drawRect:(NSRect)inRect
56 [NSGraphicsContext saveGraphicsState];
58 inRect = NSInsetRect(inRect, 1, 1);
60 NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:inRect radius:3];
62 [[NSColor windowFrameColor] set];
63 [clipPath setLineWidth:1];
66 //Ensure we have an even/odd winding rule in effect
67 [clipPath setWindingRule:NSEvenOddWindingRule];
70 [NSGraphicsContext saveGraphicsState];
71 [super drawRect:inRect];
72 [NSGraphicsContext restoreGraphicsState];
74 // Draw snapback image
75 if (showResetImageButton) {
76 NSImage *snapbackImage = [NSImage imageNamed:@"SRSnapback" forClass:[self class]];
77 NSRect snapBackRect = [self _snapbackRectForFrame:[self bounds]];
78 if (resetImageHovered) {
79 [[[NSColor blackColor] colorWithAlphaComponent:0.8] set];
81 [[[NSColor blackColor] colorWithAlphaComponent:0.5] set];
84 [[NSBezierPath bezierPathWithOvalInRect:snapBackRect] fill];
85 [snapbackImage dissolveToPoint:snapBackRect.origin fraction:1.0];
90 [[[NSColor blackColor] colorWithAlphaComponent:0.40] set];
94 NSBezierPath *arrowPath = [NSBezierPath bezierPath];
95 NSRect frame = [self frame];
96 [arrowPath moveToPoint:NSMakePoint(frame.size.width - ARROW_XOFFSET - ARROW_WIDTH,
97 (ARROW_YOFFSET + ARROW_HEIGHT))];
98 [arrowPath relativeLineToPoint:NSMakePoint(ARROW_WIDTH, 0)];
99 [arrowPath relativeLineToPoint:NSMakePoint(-(ARROW_WIDTH/2), -(ARROW_HEIGHT))];
101 [[NSColor whiteColor] set];
106 [NSGraphicsContext restoreGraphicsState];
109 #pragma mark Snapback
110 - (NSRect)_snapbackRectForFrame:(NSRect)cellFrame
112 if (!showResetImageButton) return NSZeroRect;
115 NSImage *snapbackImage = [NSImage imageNamed:@"SRSnapback" forClass:[self class]];
117 snapbackRect.origin = NSMakePoint(NSMaxX(cellFrame) - [snapbackImage size].width - 1, 2);
118 snapbackRect.size = [snapbackImage size];
123 - (void)mouseEntered:(NSEvent *)theEvent
125 if ([[self window] isKeyWindow] || [self acceptsFirstMouse: theEvent]) {
126 resetImageHovered = YES;
130 [super mouseEntered:theEvent];
133 - (void)mouseExited:(NSEvent*)theEvent
135 if ([[self window] isKeyWindow] || [self acceptsFirstMouse: theEvent]) {
136 resetImageHovered = NO;
140 [super mouseEntered:theEvent];
143 - (void)mouseDown:(NSEvent *)inEvent
145 NSPoint mouseLocation = [self convertPoint:[inEvent locationInWindow] fromView:nil];
146 if ([self mouse:mouseLocation inRect:[self _snapbackRectForFrame:[self bounds]]]) {
147 if ([[self delegate] respondsToSelector:@selector(deleteInImageViewWithImagePicker:)]) {
148 [[self delegate] deleteInImageViewWithImagePicker:self];
152 [super mouseDown:inEvent];
156 - (void)setShowResetImageButton:(BOOL)inShowResetImageButton
158 showResetImageButton = inShowResetImageButton;
160 [self resetCursorRects];
164 #pragma mark Tracking rects
165 //Remove old tracking rects when we change superviews
166 - (void)viewWillMoveToSuperview:(NSView *)newSuperview
168 if (resetImageTrackingTag != -1) {
169 [self removeTrackingRect:resetImageTrackingTag];
170 resetImageTrackingTag = -1;
173 [super viewWillMoveToSuperview:newSuperview];
176 - (void)viewDidMoveToSuperview
178 [super viewDidMoveToSuperview];
180 [self resetCursorRects];
183 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
185 if (resetImageTrackingTag != -1) {
186 [self removeTrackingRect:resetImageTrackingTag];
187 resetImageTrackingTag = -1;
190 [super viewWillMoveToWindow:newWindow];
193 - (void)viewDidMoveToWindow
195 [super viewDidMoveToWindow];
197 [self resetCursorRects];
200 - (void)frameDidChange:(NSNotification *)inNotification
202 [self resetCursorRects];
205 //Reset our cursor tracking
206 - (void)resetCursorRects
208 //Stop any existing tracking
209 if (resetImageTrackingTag != -1) {
210 [self removeTrackingRect:resetImageTrackingTag];
211 resetImageTrackingTag = -1;
214 //Add a tracking rect if our superview and window are ready
215 if (showResetImageButton && [self superview] && [self window]) {
216 NSRect snapbackRect = [self _snapbackRectForFrame:[self bounds]];
217 NSPoint mouseLocation = [self convertPoint:[[NSApp currentEvent] locationInWindow] fromView:nil];
218 BOOL mouseInside = [self mouse:mouseLocation inRect:snapbackRect];
220 resetImageTrackingTag = [self addTrackingRect:snapbackRect owner:self userData:nil assumeInside:mouseInside];
221 if (mouseInside) [self mouseEntered:nil];