William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | //
// AIContactInfoImageViewWithImagePicker.m
// Adium
//
// Created by Evan Schoenberg on 10/1/06.
//
#import "AIContactInfoImageViewWithImagePicker.h"
#import <AIUtilities/AIBezierPathAdditions.h>
#import <AIUtilities/AIImageAdditions.h>
@interface AIContactInfoImageViewWithImagePicker ()
- (void)resetCursorRects;
- (NSRect)_snapbackRectForFrame:(NSRect)cellFrame;
@end
@implementation AIContactInfoImageViewWithImagePicker
- (void)configureTracking
{
resetImageTrackingTag = -1;
[self resetCursorRects];
}
- (id)initWithFrame:(NSRect)inFrame
{
if ((self = [super initWithFrame:inFrame])) {
[self configureTracking];
}
return self;
}
- (void)awakeFromNib
{
if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
[super awakeFromNib];
}
[self configureTracking];
}
- (void)dealloc
{
if (resetImageTrackingTag != -1) {
[self removeTrackingRect:resetImageTrackingTag];
resetImageTrackingTag = -1;
}
[super dealloc];
}
- (void)drawRect:(NSRect)inRect
{
[NSGraphicsContext saveGraphicsState];
inRect = NSInsetRect(inRect, 1, 1);
NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:inRect radius:3];
[[NSColor windowFrameColor] set];
[clipPath setLineWidth:1];
[clipPath stroke];
//Ensure we have an even/odd winding rule in effect
[clipPath setWindingRule:NSEvenOddWindingRule];
[clipPath addClip];
[NSGraphicsContext saveGraphicsState];
[super drawRect:inRect];
[NSGraphicsContext restoreGraphicsState];
// Draw snapback image
if (showResetImageButton) {
NSImage *snapbackImage = [NSImage imageNamed:@"SRSnapback" forClass:[self class]];
NSRect snapBackRect = [self _snapbackRectForFrame:[self bounds]];
if (resetImageHovered) {
[[[NSColor blackColor] colorWithAlphaComponent:0.8] set];
} else {
[[[NSColor blackColor] colorWithAlphaComponent:0.5] set];
}
[[NSBezierPath bezierPathWithOvalInRect:snapBackRect] fill];
[snapbackImage dissolveToPoint:snapBackRect.origin fraction:1.0];
}
/*
if (hovered) {
[[[NSColor blackColor] colorWithAlphaComponent:0.40] set];
[clipPath fill];
//Draw the arrow
NSBezierPath *arrowPath = [NSBezierPath bezierPath];
NSRect frame = [self frame];
[arrowPath moveToPoint:NSMakePoint(frame.size.width - ARROW_XOFFSET - ARROW_WIDTH,
(ARROW_YOFFSET + ARROW_HEIGHT))];
[arrowPath relativeLineToPoint:NSMakePoint(ARROW_WIDTH, 0)];
[arrowPath relativeLineToPoint:NSMakePoint(-(ARROW_WIDTH/2), -(ARROW_HEIGHT))];
[[NSColor whiteColor] set];
[arrowPath fill];
}
*/
[NSGraphicsContext restoreGraphicsState];
}
#pragma mark Snapback
- (NSRect)_snapbackRectForFrame:(NSRect)cellFrame
{
if (!showResetImageButton) return NSZeroRect;
NSRect snapbackRect;
NSImage *snapbackImage = [NSImage imageNamed:@"SRSnapback" forClass:[self class]];
snapbackRect.origin = NSMakePoint(NSMaxX(cellFrame) - [snapbackImage size].width - 1, 2);
snapbackRect.size = [snapbackImage size];
return snapbackRect;
}
- (void)mouseEntered:(NSEvent *)theEvent
{
if ([[self window] isKeyWindow] || [self acceptsFirstMouse: theEvent]) {
resetImageHovered = YES;
[self display];
}
[super mouseEntered:theEvent];
}
- (void)mouseExited:(NSEvent*)theEvent
{
if ([[self window] isKeyWindow] || [self acceptsFirstMouse: theEvent]) {
resetImageHovered = NO;
[self display];
}
[super mouseEntered:theEvent];
}
- (void)mouseDown:(NSEvent *)inEvent
{
NSPoint mouseLocation = [self convertPoint:[inEvent locationInWindow] fromView:nil];
if ([self mouse:mouseLocation inRect:[self _snapbackRectForFrame:[self bounds]]]) {
if ([self.delegate respondsToSelector:@selector(deleteInImageViewWithImagePicker:)]) {
[self.delegate deleteInImageViewWithImagePicker:self];
}
} else {
[super mouseDown:inEvent];
}
}
- (void)setShowResetImageButton:(BOOL)inShowResetImageButton
{
showResetImageButton = inShowResetImageButton;
[self resetCursorRects];
}
#pragma mark Tracking rects
//Remove old tracking rects when we change superviews
- (void)viewWillMoveToSuperview:(NSView *)newSuperview
{
if (resetImageTrackingTag != -1) {
[self removeTrackingRect:resetImageTrackingTag];
resetImageTrackingTag = -1;
}
[super viewWillMoveToSuperview:newSuperview];
}
- (void)viewDidMoveToSuperview
{
[super viewDidMoveToSuperview];
[self resetCursorRects];
}
- (void)viewWillMoveToWindow:(NSWindow *)newWindow
{
if (resetImageTrackingTag != -1) {
[self removeTrackingRect:resetImageTrackingTag];
resetImageTrackingTag = -1;
}
[super viewWillMoveToWindow:newWindow];
}
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
[self resetCursorRects];
}
- (void)frameDidChange:(NSNotification *)inNotification
{
[self resetCursorRects];
}
//Reset our cursor tracking
- (void)resetCursorRects
{
//Stop any existing tracking
if (resetImageTrackingTag != -1) {
[self removeTrackingRect:resetImageTrackingTag];
resetImageTrackingTag = -1;
}
//Add a tracking rect if our superview and window are ready
if (showResetImageButton && [self superview] && [self window]) {
NSRect snapbackRect = [self _snapbackRectForFrame:[self bounds]];
NSPoint mouseLocation = [self convertPoint:[[NSApp currentEvent] locationInWindow] fromView:nil];
BOOL mouseInside = [self mouse:mouseLocation inRect:snapbackRect];
resetImageTrackingTag = [self addTrackingRect:snapbackRect owner:self userData:nil assumeInside:mouseInside];
if (mouseInside) [self mouseEntered:nil];
}
}
@end
|