| 1 |
masaki |
56 |
// |
| 2 |
|
|
// XspfMCollectionViewItem.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/10. |
| 6 |
|
|
// Copyright 2009 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfMCollectionViewItem.h" |
| 10 |
|
|
|
| 11 |
masaki |
57 |
#import "XspfMCollectionItemBox.h" |
| 12 |
masaki |
229 |
#import "XSPFMXspfObject.h" |
| 13 |
|
|
#import "XspfMLabelMenuItem.h" |
| 14 |
masaki |
56 |
|
| 15 |
masaki |
57 |
|
| 16 |
masaki |
56 |
@implementation XspfMCollectionViewItem |
| 17 |
|
|
|
| 18 |
|
|
- (id)copyWithZone:(NSZone *)zone |
| 19 |
|
|
{ |
| 20 |
masaki |
174 |
XspfMCollectionViewItem *result = [super copyWithZone:zone]; |
| 21 |
masaki |
57 |
|
| 22 |
masaki |
229 |
result->menu = [menu copy]; |
| 23 |
masaki |
56 |
[result performSelector:@selector(setupBinding:) withObject:nil afterDelay:0.0]; |
| 24 |
|
|
|
| 25 |
|
|
return result; |
| 26 |
|
|
} |
| 27 |
|
|
|
| 28 |
|
|
- (void)dealloc |
| 29 |
|
|
{ |
| 30 |
|
|
[collectionViewHolder removeObserver:self forKeyPath:@"isFirstResponder"]; |
| 31 |
masaki |
57 |
|
| 32 |
masaki |
56 |
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 33 |
|
|
[nc removeObserver:self]; |
| 34 |
|
|
|
| 35 |
masaki |
172 |
[self setBox:nil]; |
| 36 |
|
|
|
| 37 |
masaki |
56 |
[super dealloc]; |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
|
|
- (void)setSelected:(BOOL)flag |
| 41 |
|
|
{ |
| 42 |
|
|
[super setSelected:flag]; |
| 43 |
|
|
[self coodinateColors]; |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
masaki |
178 |
- (void)findAndSetBox |
| 47 |
|
|
{ |
| 48 |
|
|
id views = [[self view] subviews]; |
| 49 |
|
|
for(id view in views) { |
| 50 |
|
|
if([view isKindOfClass:[XspfMCollectionItemBox class]]) { |
| 51 |
|
|
[self setBox:view]; |
| 52 |
|
|
} |
| 53 |
|
|
} |
| 54 |
|
|
} |
| 55 |
masaki |
56 |
- (void)setupBinding:(id)obj |
| 56 |
|
|
{ |
| 57 |
|
|
collectionViewHolder = [self collectionView]; |
| 58 |
|
|
[collectionViewHolder addObserver:self |
| 59 |
|
|
forKeyPath:@"isFirstResponder" |
| 60 |
|
|
options:NSKeyValueObservingOptionNew |
| 61 |
|
|
context:NULL]; |
| 62 |
|
|
|
| 63 |
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 64 |
|
|
[nc addObserver:self selector:@selector(applicationDidBecomeOrResignActive:) |
| 65 |
|
|
name:NSApplicationDidBecomeActiveNotification |
| 66 |
|
|
object:NSApp]; |
| 67 |
|
|
[nc addObserver:self selector:@selector(applicationDidBecomeOrResignActive:) |
| 68 |
|
|
name:NSApplicationDidResignActiveNotification |
| 69 |
|
|
object:NSApp]; |
| 70 |
|
|
|
| 71 |
|
|
[self coodinateColors]; |
| 72 |
masaki |
174 |
|
| 73 |
masaki |
229 |
XspfMLabelMenuItem *item = (XspfMLabelMenuItem *)[menu itemWithTag:1000]; |
| 74 |
|
|
XSPFMXspfObject *object = [self representedObject]; |
| 75 |
|
|
[item setRepresentedObject:object]; |
| 76 |
|
|
[item setObjectValue:object.label]; |
| 77 |
|
|
|
| 78 |
masaki |
174 |
[[self view] setMenu:menu]; |
| 79 |
masaki |
178 |
[self findAndSetBox]; |
| 80 |
masaki |
56 |
} |
| 81 |
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context |
| 82 |
|
|
{ |
| 83 |
|
|
if([keyPath isEqualToString:@"isFirstResponder"]) { |
| 84 |
|
|
[self willChangeValueForKey:@"firstResponder"]; |
| 85 |
|
|
[self coodinateColors]; |
| 86 |
|
|
[self didChangeValueForKey:@"firstResponder"]; |
| 87 |
|
|
return; |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; |
| 91 |
|
|
} |
| 92 |
masaki |
172 |
- (void)setBox:(XspfMCollectionItemBox *)box |
| 93 |
|
|
{ |
| 94 |
|
|
[_box autorelease]; |
| 95 |
|
|
_box = [box retain]; |
| 96 |
|
|
[_box setCollectionViewItem:self]; |
| 97 |
masaki |
178 |
|
| 98 |
|
|
[_box setMenu:menu]; |
| 99 |
masaki |
172 |
} |
| 100 |
masaki |
57 |
- (void)setView:(NSView *)view |
| 101 |
|
|
{ |
| 102 |
|
|
[super setView:view]; |
| 103 |
|
|
|
| 104 |
masaki |
172 |
if(!view) return; |
| 105 |
|
|
|
| 106 |
masaki |
174 |
[view setMenu:menu]; |
| 107 |
masaki |
178 |
|
| 108 |
|
|
[self findAndSetBox]; |
| 109 |
masaki |
57 |
} |
| 110 |
masaki |
178 |
- (void)setMenu:(NSMenu *)aMenu |
| 111 |
|
|
{ |
| 112 |
|
|
menu = aMenu; |
| 113 |
|
|
} |
| 114 |
masaki |
172 |
|
| 115 |
masaki |
56 |
- (BOOL)isFirstResponder |
| 116 |
|
|
{ |
| 117 |
|
|
return [[self collectionView] isFirstResponder]; |
| 118 |
|
|
} |
| 119 |
|
|
|
| 120 |
masaki |
57 |
- (NSColor *)backgroundColor |
| 121 |
masaki |
56 |
{ |
| 122 |
|
|
if(![self isSelected]) { |
| 123 |
|
|
return [NSColor whiteColor]; |
| 124 |
|
|
} |
| 125 |
masaki |
57 |
if([self isFirstResponder] && [NSApp isActive]) { |
| 126 |
masaki |
56 |
return [NSColor colorWithCalibratedRed:65/255.0 |
| 127 |
|
|
green:120/255.0 |
| 128 |
|
|
blue:211/255.0 |
| 129 |
|
|
alpha:1.0]; |
| 130 |
|
|
} else { |
| 131 |
|
|
return [NSColor colorWithCalibratedRed:212/255.0 |
| 132 |
|
|
green:212/255.0 |
| 133 |
|
|
blue:212/255.0 |
| 134 |
|
|
alpha:1.0]; |
| 135 |
|
|
} |
| 136 |
|
|
} |
| 137 |
|
|
|
| 138 |
masaki |
229 |
- (NSColor *)labelTextColor |
| 139 |
|
|
{ |
| 140 |
|
|
XSPFMXspfObject *obj = [self representedObject]; |
| 141 |
|
|
HMLog(HMLogLevelDebug, @"Label is %@", obj.label); |
| 142 |
|
|
|
| 143 |
|
|
if([self isSelected] && [self isFirstResponder] && [NSApp isActive] && [obj.label integerValue] == 0) {//XspfMLabelNone) { |
| 144 |
|
|
return [NSColor whiteColor]; |
| 145 |
|
|
} |
| 146 |
|
|
return [NSColor blackColor]; |
| 147 |
|
|
} |
| 148 |
masaki |
56 |
- (NSColor *)textColor |
| 149 |
|
|
{ |
| 150 |
masaki |
57 |
if([self isSelected] && [self isFirstResponder] && [NSApp isActive]) { |
| 151 |
masaki |
56 |
return [NSColor whiteColor]; |
| 152 |
|
|
} |
| 153 |
|
|
return [NSColor blackColor]; |
| 154 |
|
|
} |
| 155 |
masaki |
171 |
- (IBAction)changeRate:(id)sender |
| 156 |
|
|
{ |
| 157 |
|
|
[self performSelector:@selector(highlightRateIfNeeded) withObject:nil afterDelay:0.0]; |
| 158 |
|
|
} |
| 159 |
|
|
- (void)highlightRateIfNeeded |
| 160 |
|
|
{ |
| 161 |
|
|
BOOL flag = [self isSelected] && [self isFirstResponder] && [NSApp isActive]; |
| 162 |
masaki |
172 |
NSLevelIndicatorCell *cell = [_box.rating cell]; |
| 163 |
masaki |
171 |
[cell setHighlighted:flag]; |
| 164 |
|
|
[cell setBackgroundStyle:flag ? NSBackgroundStyleDark : NSBackgroundStyleLight]; |
| 165 |
|
|
} |
| 166 |
masaki |
56 |
- (void)coodinateColors |
| 167 |
|
|
{ |
| 168 |
masaki |
57 |
[self willChangeValueForKey:@"backgroundColor"]; |
| 169 |
|
|
[self didChangeValueForKey:@"backgroundColor"]; |
| 170 |
masaki |
56 |
|
| 171 |
|
|
[self willChangeValueForKey:@"textColor"]; |
| 172 |
|
|
[self didChangeValueForKey:@"textColor"]; |
| 173 |
masaki |
171 |
|
| 174 |
masaki |
229 |
[self willChangeValueForKey:@"labelTextColor"]; |
| 175 |
|
|
[self didChangeValueForKey:@"labelTextColor"]; |
| 176 |
|
|
|
| 177 |
masaki |
171 |
[self highlightRateIfNeeded]; |
| 178 |
masaki |
56 |
} |
| 179 |
|
|
- (void)applicationDidBecomeOrResignActive:(id)notification |
| 180 |
|
|
{ |
| 181 |
|
|
[self coodinateColors]; |
| 182 |
|
|
} |
| 183 |
|
|
|
| 184 |
|
|
@end |