2 // AIContactListImagePicker.m
5 // Created by Evan Schoenberg on 12/16/05.
6 // Copyright 2006 The Adium Team. All rights reserved.
9 #import "AIContactListImagePicker.h"
10 #import <Adium/AIAccount.h>
12 #import <Adium/AIAccountControllerProtocol.h>
13 #import <Adium/AIContactControllerProtocol.h>
14 #import "AIContactListRecentImagesWindowController.h"
15 #import <AIUtilities/AIBezierPathAdditions.h>
16 #import <AIUtilities/AIApplicationAdditions.h>
19 #define ARROW_HEIGHT (ARROW_WIDTH/2.0)
20 #define ARROW_XOFFSET 2
21 #define ARROW_YOFFSET 3
23 @implementation AIContactListImagePicker
25 - (void)configureTracking
27 [[NSNotificationCenter defaultCenter] addObserver:self
28 selector:@selector(frameDidChange:)
29 name:NSViewFrameDidChangeNotification
31 [self setPostsFrameChangedNotifications:YES];
34 [self resetCursorRects];
37 - (id)initWithFrame:(NSRect)inFrame
39 if ((self = [super initWithFrame:inFrame])) {
40 [self configureTracking];
49 if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
53 [self configureTracking];
58 [[NSNotificationCenter defaultCenter] removeObserver:self];
60 if (trackingTag != -1) {
61 [self removeTrackingRect:trackingTag];
65 [imageMenu release]; imageMenu = nil;
72 - (void)drawRect:(NSRect)inRect
74 [NSGraphicsContext saveGraphicsState];
76 inRect = NSInsetRect(inRect, 1, 1);
78 NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:inRect radius:3];
80 [[NSColor windowFrameColor] set];
81 [clipPath setLineWidth:1];
84 //Ensure we have an even/odd winding rule in effect
85 [clipPath setWindingRule:NSEvenOddWindingRule];
88 [super drawRect:inRect];
91 [[[NSColor blackColor] colorWithAlphaComponent:0.40] set];
95 NSBezierPath *arrowPath = [NSBezierPath bezierPath];
96 NSRect frame = [self frame];
97 [arrowPath moveToPoint:NSMakePoint(frame.size.width - ARROW_XOFFSET - ARROW_WIDTH,
98 (ARROW_YOFFSET + ARROW_HEIGHT))];
99 [arrowPath relativeLineToPoint:NSMakePoint(ARROW_WIDTH, 0)];
100 [arrowPath relativeLineToPoint:NSMakePoint(-(ARROW_WIDTH/2), -(ARROW_HEIGHT))];
102 [[NSColor whiteColor] set];
106 [NSGraphicsContext restoreGraphicsState];
109 #pragma mark Mouse movement
111 - (void)setHovered:(BOOL)inHovered
115 [self setNeedsDisplay:YES];
118 - (void)mouseEntered:(NSEvent *)inEvent
120 [self setHovered:YES];
122 [super mouseEntered:inEvent];
125 - (void)mouseExited:(NSEvent *)inEvent
127 [self setHovered:NO];
129 [super mouseExited:inEvent];
133 - (void)displayPicturePopUpForEvent:(NSEvent *)theEvent
135 if(NSClassFromString(@"NSIPRecentPicture")) {
136 NSRect myFrame = [self frame];
137 NSPoint bottomRightPoint = NSMakePoint(NSMaxX(myFrame), NSMinY(myFrame));
138 bottomRightPoint = [[self window] convertBaseToScreen:[[self superview] convertPoint:bottomRightPoint toView:nil]];
139 [AIContactListRecentImagesWindowController showWindowFromPoint:bottomRightPoint
141 recentPictureSelector:@selector(didSetImage:forAccount:)];
147 //Custom mouse down tracking to display our menu and highlight
148 - (void)mouseDown:(NSEvent *)theEvent
150 [self displayPicturePopUpForEvent:theEvent];
153 #pragma mark Tracking rects
154 //Remove old tracking rects when we change superviews
155 - (void)viewWillMoveToSuperview:(NSView *)newSuperview
157 if (trackingTag != -1) {
158 [self removeTrackingRect:trackingTag];
162 [super viewWillMoveToSuperview:newSuperview];
165 - (void)viewDidMoveToSuperview
167 [super viewDidMoveToSuperview];
169 [self resetCursorRects];
172 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
174 if (trackingTag != -1) {
175 [self removeTrackingRect:trackingTag];
179 [super viewWillMoveToWindow:newWindow];
182 - (void)viewDidMoveToWindow
184 [super viewDidMoveToWindow];
186 [self resetCursorRects];
189 - (void)frameDidChange:(NSNotification *)inNotification
191 [self resetCursorRects];
194 //Reset our cursor tracking
195 - (void)resetCursorRects
197 //Stop any existing tracking
198 if (trackingTag != -1) {
199 [self removeTrackingRect:trackingTag];
203 //Add a tracking rect if our superview and window are ready
204 if ([self superview] && [self window]) {
205 NSRect myFrame = [self frame];
206 NSRect trackRect = NSMakeRect(0, 0, myFrame.size.width, myFrame.size.height);
208 if (trackRect.size.width > myFrame.size.width) {
209 trackRect.size.width = myFrame.size.width;
212 NSPoint localPoint = [self convertPoint:[[self window] convertScreenToBase:[NSEvent mouseLocation]]
214 BOOL mouseInside = NSPointInRect(localPoint, myFrame);
216 trackingTag = [self addTrackingRect:trackRect owner:self userData:nil assumeInside:mouseInside];
217 if (mouseInside) [self mouseEntered:nil];