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];
36 [self setPresentPictureTakerAsSheet:NO];
39 - (id)initWithFrame:(NSRect)inFrame
41 if ((self = [super initWithFrame:inFrame])) {
42 [self configureTracking];
51 if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
55 [self configureTracking];
60 [[NSNotificationCenter defaultCenter] removeObserver:self];
62 if (trackingTag != -1) {
63 [self removeTrackingRect:trackingTag];
67 [imageMenu release]; imageMenu = nil;
74 - (void)drawRect:(NSRect)inRect
76 [NSGraphicsContext saveGraphicsState];
78 inRect = NSInsetRect(inRect, 1, 1);
80 NSBezierPath *clipPath = [NSBezierPath bezierPathWithRoundedRect:inRect radius:3];
82 [[NSColor windowFrameColor] set];
83 [clipPath setLineWidth:1];
86 //Ensure we have an even/odd winding rule in effect
87 [clipPath setWindingRule:NSEvenOddWindingRule];
90 [super drawRect:inRect];
93 [[[NSColor blackColor] colorWithAlphaComponent:0.40] set];
97 NSBezierPath *arrowPath = [NSBezierPath bezierPath];
98 NSRect frame = [self frame];
99 [arrowPath moveToPoint:NSMakePoint(frame.size.width - ARROW_XOFFSET - ARROW_WIDTH,
100 (ARROW_YOFFSET + ARROW_HEIGHT))];
101 [arrowPath relativeLineToPoint:NSMakePoint(ARROW_WIDTH, 0)];
102 [arrowPath relativeLineToPoint:NSMakePoint(-(ARROW_WIDTH/2), -(ARROW_HEIGHT))];
104 [[NSColor whiteColor] set];
108 [NSGraphicsContext restoreGraphicsState];
111 #pragma mark Mouse movement
113 - (void)setHovered:(BOOL)inHovered
117 [self setNeedsDisplay:YES];
120 - (void)mouseEntered:(NSEvent *)inEvent
122 [self setHovered:YES];
124 [super mouseEntered:inEvent];
127 - (void)mouseExited:(NSEvent *)inEvent
129 [self setHovered:NO];
131 [super mouseExited:inEvent];
135 - (void)displayPicturePopUpForEvent:(NSEvent *)theEvent
137 NSRect myFrame = [self frame];
138 NSPoint bottomRightPoint = NSMakePoint(NSMaxX(myFrame), NSMinY(myFrame));
139 bottomRightPoint = [[self window] convertBaseToScreen:[[self superview] convertPoint:bottomRightPoint toView:nil]];
141 [AIContactListRecentImagesWindowController showWindowFromPoint:bottomRightPoint
143 recentPictureSelector:@selector(didSetImage:forAccount:)];
146 //Custom mouse down tracking to display our menu and highlight
147 - (void)mouseDown:(NSEvent *)theEvent
149 [self displayPicturePopUpForEvent:theEvent];
152 #pragma mark Tracking rects
153 //Remove old tracking rects when we change superviews
154 - (void)viewWillMoveToSuperview:(NSView *)newSuperview
156 if (trackingTag != -1) {
157 [self removeTrackingRect:trackingTag];
161 [super viewWillMoveToSuperview:newSuperview];
164 - (void)viewDidMoveToSuperview
166 [super viewDidMoveToSuperview];
168 [self resetCursorRects];
171 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
173 if (trackingTag != -1) {
174 [self removeTrackingRect:trackingTag];
178 [super viewWillMoveToWindow:newWindow];
181 - (void)viewDidMoveToWindow
183 [super viewDidMoveToWindow];
185 [self resetCursorRects];
188 - (void)frameDidChange:(NSNotification *)inNotification
190 [self resetCursorRects];
193 //Reset our cursor tracking
194 - (void)resetCursorRects
196 //Stop any existing tracking
197 if (trackingTag != -1) {
198 [self removeTrackingRect:trackingTag];
202 //Add a tracking rect if our superview and window are ready
203 if ([self superview] && [self window]) {
204 NSRect myFrame = [self frame];
205 NSRect trackRect = NSMakeRect(0, 0, myFrame.size.width, myFrame.size.height);
207 if (trackRect.size.width > myFrame.size.width) {
208 trackRect.size.width = myFrame.size.width;
211 NSPoint localPoint = [self convertPoint:[[self window] convertScreenToBase:[NSEvent mouseLocation]]
213 BOOL mouseInside = NSPointInRect(localPoint, myFrame);
215 trackingTag = [self addTrackingRect:trackRect owner:self userData:nil assumeInside:mouseInside];
216 if (mouseInside) [self mouseEntered:nil];