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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | //
// AIContactListRecentImagesWindowController.m
// Adium
//
// Created by Evan Schoenberg on 12/19/05.
//
#import "AIContactListRecentImagesWindowController.h"
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIContactControllerProtocol.h>
#import "AIStandardListWindowController.h"
#import "AIContactListImagePicker.h"
#import "AIMenuItemView.h"
#import <Adium/AIAccount.h>
#import <AIUtilities/AIApplicationAdditions.h>
#import <AIUtilities/AIBorderlessWindow.h>
#import <AIUtilities/AIColoredBoxView.h>
#import <AIUtilities/AIImageGridView.h>
#import <AIUtilities/AIImageAdditions.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import "IKRecentPicture.h" //10.5+, private
#define FADE_INCREMENT 0.3
#define FADE_TIME .3
@interface AIContactListRecentImagesWindowController ()
- (id)initWithWindowNibName:(NSString *)inWindowNibName
imagePicker:(AIContactListImagePicker *)inPicker;
- (void)fadeOutAndClose;
@end
@implementation AIContactListRecentImagesWindowController
/*!
* @brief Show the window
*
* @param inPoint The bottom-right corner of our parent view
* @param inPicker Our parent AIContactListImagePicker
*/
+ (void)showWindowFromPoint:(NSPoint)inPoint
imagePicker:(AIContactListImagePicker *)inPicker
{
AIContactListRecentImagesWindowController *controller = [[self alloc] initWithWindowNibName:@"ContactListRecentImages"
imagePicker:inPicker];
NSWindow *window = [controller window];
[controller positionFromPoint:inPoint];
[(AIBorderlessWindow *)window setMoveable:NO];
[controller showWindow:nil];
[window makeKeyAndOrderFront:nil];
}
- (id)initWithWindowNibName:(NSString *)inWindowNibName
imagePicker:(AIContactListImagePicker *)inPicker
{
if ((self = [super initWithWindowNibName:inWindowNibName])) {
picker = [inPicker retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(parentWindowWillClose:)
name:NSWindowWillCloseNotification
object:[picker window]];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[picker release];
[super dealloc];
}
- (void)windowDidLoad
{
[imageGridView setImageSize:NSMakeSize(30, 30)];
[coloredBox setColor:[NSColor windowBackgroundColor]];
[label_recentIcons setLocalizedString:AILocalizedString(@"Recent Icons:", "Label at the top of the recent icons picker shown in the contact list")];
[picker setMaxSize:NSMakeSize(256, 256)];
currentHoveredIndex = -1;
}
- (void)positionFromPoint:(NSPoint)inPoint
{
NSWindow *window = [self window];
NSRect frame = [window frame];
NSRect screenFrame = [[window screen] visibleFrame];
frame.origin.x = inPoint.x - frame.size.width;
if (frame.origin.x < screenFrame.origin.x) {
frame.origin.x = screenFrame.origin.x;
} else if (frame.origin.x + frame.size.width > screenFrame.origin.x + screenFrame.size.width) {
frame.origin.x = screenFrame.origin.x + screenFrame.size.width - frame.size.width;
}
frame.origin.y = inPoint.y - frame.size.height;
if (frame.origin.y < screenFrame.origin.y) {
frame.origin.y = screenFrame.origin.y;
}
//Ensure our window is visible above the window of the imagePicker that created us
if ([window level] < [[picker window] level]) {
[window setLevel:[[picker window] level] + 1];
}
[window setFrame:frame display:NO animate:NO];
}
- (NSUInteger)numberOfImagesInImageGridView:(AIImageGridView *)imageGridView
{
return 10;
}
- (NSArray *)recentPictures
{
return [(IKPictureTakerRecentPictureRepository *)[IKPictureTakerRecentPictureRepository recentRepository] recentPictures];
}
- (NSArray *)recentSmallIcons
{
return [[(IKPictureTakerRecentPictureRepository *)[IKPictureTakerRecentPictureRepository recentRepository] recentPictures] valueForKey:@"smallIcon"];
}
- (NSImage *)imageGridView:(AIImageGridView *)imageGridView imageAtIndex:(NSUInteger)index
{
NSImage *displayImage;
NSArray *recentSmallIcons = [self recentSmallIcons];
if (index < [recentSmallIcons count]) {
NSImage *image = [recentSmallIcons objectAtIndex:index];
NSSize size = [image size];
NSBezierPath *fullPath = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, size.width, size.height)];
displayImage = [image copy];
[displayImage setFlipped:YES];
[displayImage lockFocus];
if (index == currentHoveredIndex) {
[[[NSColor blueColor] colorWithAlphaComponent:0.30] set];
[fullPath fill];
[[NSColor blueColor] set];
[fullPath stroke];
} else {
[[NSColor whiteColor] set];
[fullPath stroke];
}
[displayImage unlockFocus];
} else {
NSSize size = NSMakeSize(32, 32);
NSBezierPath *fullPath = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, size.width, size.height)];
displayImage = [[NSImage alloc] initWithSize:size];
[displayImage lockFocus];
[[NSColor lightGrayColor] set];
[fullPath fill];
[displayImage unlockFocus];
}
return [displayImage autorelease];
}
- (void)imageGridView:(AIImageGridView *)inImageGridView cursorIsHoveringImageAtIndex:(NSUInteger)index
{
//Update our hovered index and redisplay the image
currentHoveredIndex = index;
[imageGridView setNeedsDisplayOfImageAtIndex:index];
}
- (void)imageGridViewSelectionDidChange:(NSNotification *)notification
{
NSInteger selectedIndex = [imageGridView selectedIndex];
NSArray *recentPictures = [self recentPictures];
if (selectedIndex < [recentPictures count]) {
id recentPicture = [recentPictures objectAtIndex:selectedIndex];
NSData *imageData = nil;
if ([recentPicture respondsToSelector:@selector(editedImage)])
imageData = [[recentPicture editedImage] PNGRepresentation];
else if ([recentPicture respondsToSelector:@selector(originalImagePath)])
imageData = [NSData dataWithContentsOfFile:[recentPicture originalImagePath]];
//Notify as if the image had been selected in the picker
[[picker delegate] imageViewWithImagePicker:picker
didChangeToImageData:imageData];
//Now pass on the actual recent picture for use if possible
[picker setRecentPictureAsImageInput:recentPicture];
}
[self fadeOutAndClose];
}
- (void)selectedAccount:(id)sender
{
AIAccount *activeAccount = [sender representedObject];
//Change the active account
[adium.preferenceController setPreference:(activeAccount ? activeAccount.internalObjectID : nil)
forKey:@"Active Icon Selection Account"
group:GROUP_ACCOUNT_STATUS];
[menuItemView setMenu:[self menuForMenuItemView:menuItemView]];
}
- (void)chooseIcon:(id)sender
{
[picker showImagePicker:nil];
[self fadeOutAndClose];
}
- (void)clearRecentPictures:(id)sender
{
[[IKPictureTakerRecentPictureRepository recentRepository] clearRecents:nil];
[imageGridView reloadData];
}
- (void)windowDidResignMain:(NSNotification *)aNotification
{
[self fadeOutAndClose];
}
- (void)windowDidResignKey:(NSNotification *)aNotification
{
[self fadeOutAndClose];
}
- (void)parentWindowWillClose:(NSNotification *)aNotification
{
//Close, no fade, when our parent window closes
[self close];
}
#pragma mark Fading
- (void)fadeOut:(NSTimer *)inTimer
{
CGFloat currentAlpha = [[self window] alphaValue];
currentAlpha -= 0.15;
if (currentAlpha <= 0) {
[self close];
[inTimer invalidate];
} else {
[[self window] setAlphaValue:currentAlpha];
}
}
- (void)fadeOutAndClose
{
[NSTimer scheduledTimerWithTimeInterval:.01
target:self
selector:@selector(fadeOut:)
userInfo:nil
repeats:YES];
}
#pragma mark AIMenuItemView delegate
- (NSMenu *)menuForMenuItemView:(AIMenuItemView *)inMenuItemView
{
NSMenu *menu = [[NSMenu alloc] init];
NSMutableSet *onlineAccounts = [NSMutableSet set];
NSMutableSet *ownIconAccounts = [NSMutableSet set];
AIAccount *activeAccount = nil;
NSMenuItem *menuItem;
activeAccount = [AIStandardListWindowController activeAccountForIconsGettingOnlineAccounts:onlineAccounts
ownIconAccounts:ownIconAccounts];
NSInteger ownIconAccountsCount = [ownIconAccounts count];
NSInteger onlineAccountsCount = [onlineAccounts count];
if (ownIconAccountsCount && ((ownIconAccountsCount > 1) || (onlineAccountsCount > 1))) {
//There are at least some accounts using the global preference if the counts differ
BOOL includeGlobal = (onlineAccountsCount != ownIconAccountsCount);
AIAccount *account;
menuItem = [[NSMenuItem alloc] initWithTitle:AILocalizedString(@"Change Icon For:", nil)
target:nil
action:nil
keyEquivalent:@""];
[menuItem setEnabled:NO];
[menu addItem:menuItem];
[menuItem release];
for (account in ownIconAccounts) {
//Put a check before the account if it is the active account
menuItem = [[NSMenuItem alloc] initWithTitle:account.formattedUID
target:self
action:@selector(selectedAccount:)
keyEquivalent:@""];
[menuItem setRepresentedObject:account];
if (activeAccount == account) {
[menuItem setState:NSOnState];
}
[menuItem setIndentationLevel:1];
[menu addItem:menuItem];
[menuItem release];
}
if (includeGlobal) {
menuItem = [[NSMenuItem alloc] initWithTitle:ALL_OTHER_ACCOUNTS
target:self
action:@selector(selectedAccount:)
keyEquivalent:@""];
if (!activeAccount) {
[menuItem setState:NSOnState];
}
[menuItem setIndentationLevel:1];
[menu addItem:menuItem];
[menuItem release];
}
[menu addItem:[NSMenuItem separatorItem]];
} else {
//All accounts are using the global preference
}
menuItem = [[NSMenuItem alloc] initWithTitle:[AILocalizedString(@"Choose Icon", nil) stringByAppendingEllipsis]
target:self
action:@selector(chooseIcon:)
keyEquivalent:@""];
[menu addItem:menuItem];
[menuItem release];
menuItem = [[NSMenuItem alloc] initWithTitle:AILocalizedString(@"Clear Recent Pictures", nil)
target:self
action:@selector(clearRecentPictures:)
keyEquivalent:@""];
[menu addItem:menuItem];
[menuItem release];
return [menu autorelease];
}
- (void)menuItemViewDidChangeMenu:(AIMenuItemView *)inMenuItemView
{
NSRect oldFrame = [inMenuItemView frame];
[inMenuItemView sizeToFit];
NSRect newFrame = [inMenuItemView frame];
CGFloat heightDifference = newFrame.size.height - oldFrame.size.height;
if (heightDifference != 0) {
NSRect myFrame = [[self window] frame];
myFrame.size.height += heightDifference;
myFrame.origin.y -= heightDifference;
[[self window] setFrame:myFrame display:YES animate:NO];
}
}
@end
|