Added `-[NSArray validateAsPropertyList]` and `-[NSDictionary validateAsPropertyList...
[adiumx.git] / Source / AIContactListImagePicker.m
blobc3e69e2c5ed83f37c582c2bc21f3f715c90c88ae
1 //
2 //  AIContactListImagePicker.m
3 //  Adium
4 //
5 //  Created by Evan Schoenberg on 12/16/05.
6 //  Copyright 2006 The Adium Team. All rights reserved.
7 //
9 #import "AIContactListImagePicker.h"
10 #import <Adium/AIAccount.h>
11 #import <AIAdium.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>
18 #define ARROW_WIDTH             8
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
30                                                                                            object:self];
31         [self setPostsFrameChangedNotifications:YES];
32         
33         trackingTag = -1;
34         [self resetCursorRects];
35         
36         [self setPresentPictureTakerAsSheet:NO];
39 - (id)initWithFrame:(NSRect)inFrame
41         if ((self = [super initWithFrame:inFrame])) {
42                 [self configureTracking];
43                 imageMenu = nil;
44         }
45         
46         return self;
49 - (void)awakeFromNib
51         if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
52         [super awakeFromNib];
53         }
55         [self configureTracking];
58 - (void)dealloc
60         [[NSNotificationCenter defaultCenter] removeObserver:self];
62         if (trackingTag != -1) {
63                 [self removeTrackingRect:trackingTag];
64                 trackingTag = -1;
65         }
66         
67         [imageMenu release]; imageMenu = nil;
68         
69         [super dealloc];
72 #pragma mark Drawing
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];
84         [clipPath stroke];
86         //Ensure we have an even/odd winding rule in effect
87         [clipPath setWindingRule:NSEvenOddWindingRule];
88         [clipPath addClip];
89         
90         [super drawRect:inRect];
91         
92         if (hovered) {
93                 [[[NSColor blackColor] colorWithAlphaComponent:0.40] set];
94                 [clipPath fill];
96                 //Draw the arrow
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))];
103                 
104                 [[NSColor whiteColor] set];
105                 [arrowPath fill];
106         }
108         [NSGraphicsContext restoreGraphicsState];
111 #pragma mark Mouse movement
113 - (void)setHovered:(BOOL)inHovered
115         hovered = inHovered;
116         
117         [self setNeedsDisplay:YES];
120 - (void)mouseEntered:(NSEvent *)inEvent
122         [self setHovered:YES];
123         
124         [super mouseEntered:inEvent];   
127 - (void)mouseExited:(NSEvent *)inEvent
129         [self setHovered:NO];
130         
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
142                                                                                                            imagePicker:self
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];
158                 trackingTag = -1;
159         }
160         
161         [super viewWillMoveToSuperview:newSuperview];
164 - (void)viewDidMoveToSuperview
166         [super viewDidMoveToSuperview];
167         
168         [self resetCursorRects];
171 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
173         if (trackingTag != -1) {
174                 [self removeTrackingRect:trackingTag];
175                 trackingTag = -1;
176         }
177         
178         [super viewWillMoveToWindow:newWindow];
181 - (void)viewDidMoveToWindow
183         [super viewDidMoveToWindow];
184         
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];
199                 trackingTag = -1;
200         }
201         
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);
206                 
207                 if (trackRect.size.width > myFrame.size.width) {
208                         trackRect.size.width = myFrame.size.width;
209                 }
210                 
211                 NSPoint localPoint = [self convertPoint:[[self window] convertScreenToBase:[NSEvent mouseLocation]]
212                                                                            fromView:nil];
213                 BOOL    mouseInside = NSPointInRect(localPoint, myFrame);
215                 trackingTag = [self addTrackingRect:trackRect owner:self userData:nil assumeInside:mouseInside];
216                 if (mouseInside) [self mouseEntered:nil];
217         }
220 @end