| 1 |
// |
| 2 |
// XspfMCollectionItemView.m |
| 3 |
// XspfManager |
| 4 |
// |
| 5 |
// Created by Hori,Masaki on 11/01/25. |
| 6 |
// |
| 7 |
|
| 8 |
/* |
| 9 |
This source code is release under the New BSD License. |
| 10 |
Copyright (c) 2011, masakih |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に |
| 14 |
限り、再頒布および使用が許可されます。 |
| 15 |
|
| 16 |
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含 |
| 17 |
めること。 |
| 18 |
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表 |
| 19 |
示、本条件一覧、および下記免責条項を含めること。 |
| 20 |
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、 |
| 21 |
コントリビューターの名前を使用してはならない。 |
| 22 |
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、 |
| 23 |
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証 |
| 24 |
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター |
| 25 |
も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか |
| 26 |
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する |
| 27 |
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用 |
| 28 |
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定 |
| 29 |
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい |
| 30 |
て、一切責任を負わないものとします。 |
| 31 |
------------------------------------------------------------------- |
| 32 |
Copyright (c) 2011, masakih |
| 33 |
All rights reserved. |
| 34 |
|
| 35 |
Redistribution and use in source and binary forms, with or without |
| 36 |
modification, are permitted provided that the following conditions |
| 37 |
are met: |
| 38 |
|
| 39 |
1, Redistributions of source code must retain the above copyright |
| 40 |
notice, this list of conditions and the following disclaimer. |
| 41 |
2, Redistributions in binary form must reproduce the above copyright |
| 42 |
notice, this list of conditions and the following disclaimer in |
| 43 |
the documentation and/or other materials provided with the |
| 44 |
distribution. |
| 45 |
3, The names of its contributors may be used to endorse or promote |
| 46 |
products derived from this software without specific prior |
| 47 |
written permission. |
| 48 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 49 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 50 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 51 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 52 |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 53 |
INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 54 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 55 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 56 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 58 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 59 |
POSSIBILITY OF SUCH DAMAGE. |
| 60 |
*/ |
| 61 |
|
| 62 |
#import "XspfMCollectionItemView.h" |
| 63 |
|
| 64 |
#import "HMLabelCell.h" |
| 65 |
#import "XspfMShadowImageCell.h" |
| 66 |
|
| 67 |
|
| 68 |
NSString *const XspfMThumbnailBinding = @"thumbnail"; |
| 69 |
NSString *const XspfMTitleColorBinding = @"titleColor"; |
| 70 |
NSString *const XspfMRatingBinding = @"rating"; |
| 71 |
|
| 72 |
|
| 73 |
@implementation XspfMCollectionItemView |
| 74 |
|
| 75 |
+ (void)initialize |
| 76 |
{ |
| 77 |
static BOOL isFirst = YES; |
| 78 |
if(isFirst){ |
| 79 |
isFirst = NO; |
| 80 |
|
| 81 |
[self exposeBinding:XspfMThumbnailBinding]; |
| 82 |
[self exposeBinding:NSTitleBinding]; |
| 83 |
[self exposeBinding:XspfMTitleColorBinding]; |
| 84 |
[self exposeBinding:XspfMRatingBinding]; |
| 85 |
[self exposeBinding:NSLabelBinding]; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
- (Class)valueClassForBinding:(NSString *)binding |
| 90 |
{ |
| 91 |
if([binding isEqualToString:XspfMThumbnailBinding]) { |
| 92 |
return [NSImage class]; |
| 93 |
} |
| 94 |
if([binding isEqualToString:NSTitleBinding]) { |
| 95 |
return [NSString class]; |
| 96 |
} |
| 97 |
if([binding isEqualToString:XspfMTitleColorBinding]) { |
| 98 |
return [NSColor class]; |
| 99 |
} |
| 100 |
if([binding isEqualToString:XspfMRatingBinding]) { |
| 101 |
return [NSValue class]; |
| 102 |
} |
| 103 |
if([binding isEqualToString:NSLabelBinding]) { |
| 104 |
return [NSValue class]; |
| 105 |
} |
| 106 |
|
| 107 |
return [super valueClassForBinding:binding]; |
| 108 |
} |
| 109 |
|
| 110 |
- (void)setup |
| 111 |
{ |
| 112 |
controlSize = -1;; |
| 113 |
|
| 114 |
thumbnailCell = [[XspfMShadowImageCell alloc] initImageCell:nil]; |
| 115 |
|
| 116 |
titleCell = [[NSTextFieldCell alloc] initTextCell:@""]; |
| 117 |
[titleCell setEditable:YES]; |
| 118 |
[titleCell setSelectable:YES]; |
| 119 |
[titleCell setEnabled:YES]; |
| 120 |
|
| 121 |
rateCell = [[NSLevelIndicatorCell alloc] initWithLevelIndicatorStyle:NSRatingLevelIndicatorStyle]; |
| 122 |
[rateCell setEditable:YES]; |
| 123 |
[rateCell setEnabled:YES]; |
| 124 |
[rateCell setHighlighted:YES]; |
| 125 |
|
| 126 |
rateTitleCell = [[NSTextFieldCell alloc] initTextCell:NSLocalizedString(@"Rate:", @"Icon view Rate label.")]; |
| 127 |
[rateTitleCell setAlignment:NSRightTextAlignment]; |
| 128 |
|
| 129 |
labelCell = [[HMLabelCell alloc] initTextCell:@""]; |
| 130 |
[labelCell setLabelStyle:HMSquareStyle]; |
| 131 |
[labelCell setDrawX:NO]; |
| 132 |
|
| 133 |
if([self frame].size.height < 200) { |
| 134 |
[self setControlSize:NSSmallControlSize]; |
| 135 |
} else { |
| 136 |
[self setControlSize:NSRegularControlSize]; |
| 137 |
} |
| 138 |
|
| 139 |
[self calcSize]; |
| 140 |
} |
| 141 |
- (id)initWithCoder:(NSCoder *)decoder |
| 142 |
{ |
| 143 |
self = [super initWithCoder:decoder]; |
| 144 |
|
| 145 |
[self setup]; |
| 146 |
|
| 147 |
return self; |
| 148 |
} |
| 149 |
- (id)initWithFrame:(NSRect)frameRect |
| 150 |
{ |
| 151 |
self = [super initWithFrame:frameRect]; |
| 152 |
if(self) { |
| 153 |
[self setup]; |
| 154 |
} |
| 155 |
return self; |
| 156 |
} |
| 157 |
- (void)dealloc |
| 158 |
{ |
| 159 |
[backgroundColor release]; |
| 160 |
|
| 161 |
[thumbnailCell release]; |
| 162 |
[titleCell release]; |
| 163 |
[rateTitleCell release]; |
| 164 |
[labelCell release]; |
| 165 |
|
| 166 |
[super dealloc]; |
| 167 |
} |
| 168 |
|
| 169 |
- (void)setValue:(id)value forKey:(NSString *)key |
| 170 |
{ |
| 171 |
if([key isEqualToString:XspfMThumbnailBinding]) { |
| 172 |
[thumbnailCell setImage:value]; |
| 173 |
[self setNeedsDisplayInRect:selectedBounds]; |
| 174 |
return; |
| 175 |
} |
| 176 |
if([key isEqualToString:NSTitleBinding]) { |
| 177 |
value = value ? value : @""; |
| 178 |
[titleCell setStringValue:value]; |
| 179 |
return; |
| 180 |
} |
| 181 |
if([key isEqualToString:XspfMTitleColorBinding]) { |
| 182 |
[titleCell setTextColor:value]; |
| 183 |
[self setNeedsDisplayInRect:titleBounds]; |
| 184 |
return; |
| 185 |
} |
| 186 |
if([key isEqualToString:XspfMRatingBinding]) { |
| 187 |
[rateCell setObjectValue:value]; |
| 188 |
return; |
| 189 |
} |
| 190 |
if([key isEqualToString:NSLabelBinding]) { |
| 191 |
[labelCell setObjectValue:value]; |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
[super setValue:value forKey:key]; |
| 196 |
} |
| 197 |
- (id)valueForKey:(NSString *)key |
| 198 |
{ |
| 199 |
if([key isEqualToString:XspfMThumbnailBinding]) { |
| 200 |
return [thumbnailCell image]; |
| 201 |
} |
| 202 |
if([key isEqualToString:NSTitleBinding]) { |
| 203 |
return [titleCell stringValue]; |
| 204 |
} |
| 205 |
if([key isEqualToString:XspfMTitleColorBinding]) { |
| 206 |
return [titleCell textColor]; |
| 207 |
} |
| 208 |
if([key isEqualToString:XspfMRatingBinding]) { |
| 209 |
return [rateCell objectValue]; |
| 210 |
} |
| 211 |
if([key isEqualToString:NSLabelBinding]) { |
| 212 |
return [labelCell objectValue]; |
| 213 |
} |
| 214 |
|
| 215 |
return [super valueForKey:key]; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
- (void)setSelected:(BOOL)flag |
| 220 |
{ |
| 221 |
if(selected && flag) return; |
| 222 |
if(!selected && !flag) return; |
| 223 |
|
| 224 |
selected = flag; |
| 225 |
|
| 226 |
[self setNeedsDisplayInRect:selectedBounds]; |
| 227 |
} |
| 228 |
|
| 229 |
- (void)setControlSize:(NSControlSize)size |
| 230 |
{ |
| 231 |
if(size == controlSize) return; |
| 232 |
controlSize = size; |
| 233 |
|
| 234 |
NSFont *titleFont = nil; |
| 235 |
switch(controlSize) { |
| 236 |
case NSRegularControlSize: |
| 237 |
titleFont = [NSFont controlContentFontOfSize:13]; |
| 238 |
break; |
| 239 |
case NSSmallControlSize: |
| 240 |
titleFont = [NSFont controlContentFontOfSize:11]; |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
[titleCell setFont:titleFont]; |
| 245 |
[rateTitleCell setFont:titleFont]; |
| 246 |
|
| 247 |
[titleCell setControlSize:size]; |
| 248 |
[thumbnailCell setControlSize:size]; |
| 249 |
[rateCell setControlSize:size]; |
| 250 |
[rateTitleCell setControlSize:size]; |
| 251 |
} |
| 252 |
|
| 253 |
- (NSControlSize)controlSize |
| 254 |
{ |
| 255 |
return controlSize; |
| 256 |
} |
| 257 |
- (void)setFrameSize:(NSSize)newSize |
| 258 |
{ |
| 259 |
[super setFrameSize:newSize]; |
| 260 |
[self calcSize]; |
| 261 |
} |
| 262 |
|
| 263 |
- (void)calcSize |
| 264 |
{ |
| 265 |
if([self frame].size.height < 200) { |
| 266 |
[self setControlSize:NSSmallControlSize]; |
| 267 |
} else { |
| 268 |
[self setControlSize:NSRegularControlSize]; |
| 269 |
} |
| 270 |
|
| 271 |
CGFloat margin = 10; |
| 272 |
CGFloat titleTopMargin = 3; |
| 273 |
CGFloat padding = (controlSize == NSSmallControlSize) ? 5 : 10; |
| 274 |
NSSize labelInsetSize = (controlSize == NSSmallControlSize) ? NSMakeSize(-2, -1) : NSMakeSize(-4, -2); |
| 275 |
NSSize slectInsetSize = (controlSize == NSSmallControlSize) ? NSMakeSize(-5, -5) : NSMakeSize(-10, -10); |
| 276 |
|
| 277 |
NSRect myBounds = [self bounds]; |
| 278 |
CGFloat fontHeight = [[titleCell font] pointSize]; |
| 279 |
NSSize rateSize = NSMakeSize(65, 13); |
| 280 |
|
| 281 |
CGFloat thumbnailWidth = NSWidth(myBounds) - 2 * (margin + padding); |
| 282 |
CGFloat thumbnailHeight = ceil(thumbnailWidth * 3 / 4); |
| 283 |
CGFloat titleHeight = ceil(fontHeight * 2 * 1.3); |
| 284 |
CGFloat rateTitleWidth = ceil(thumbnailWidth * 0.4); |
| 285 |
|
| 286 |
thumbnailBounds = NSMakeRect(margin + padding, NSHeight(myBounds) - margin - padding - thumbnailHeight, |
| 287 |
thumbnailWidth, thumbnailHeight); |
| 288 |
titleBounds = NSMakeRect(NSMinX(thumbnailBounds), NSMinY(thumbnailBounds) - padding - titleTopMargin - titleHeight + labelInsetSize.height, |
| 289 |
thumbnailWidth, titleHeight); |
| 290 |
rateLabelBounds = NSMakeRect(NSMinX(thumbnailBounds), margin, rateTitleWidth, fontHeight + 4); |
| 291 |
rateBounds = NSMakeRect(NSMaxX(rateLabelBounds), margin, rateSize.width, rateSize.height); |
| 292 |
labelBounds = NSInsetRect(titleBounds, labelInsetSize.width, labelInsetSize.height); |
| 293 |
selectedBounds = NSInsetRect(thumbnailBounds, slectInsetSize.width, slectInsetSize.height); |
| 294 |
} |
| 295 |
|
| 296 |
- (CGFloat)selectRectRadius |
| 297 |
{ |
| 298 |
CGFloat radius = 0.0; |
| 299 |
switch(controlSize) { |
| 300 |
case NSRegularControlSize: |
| 301 |
radius = 8; |
| 302 |
break; |
| 303 |
case NSSmallControlSize: |
| 304 |
radius = 5; |
| 305 |
break; |
| 306 |
} |
| 307 |
return radius; |
| 308 |
} |
| 309 |
- (CGFloat)selectedTitleRectRadius |
| 310 |
{ |
| 311 |
CGFloat radius = 0.0; |
| 312 |
switch(controlSize) { |
| 313 |
case NSRegularControlSize: |
| 314 |
radius = 5; |
| 315 |
break; |
| 316 |
case NSSmallControlSize: |
| 317 |
radius = 3; |
| 318 |
break; |
| 319 |
} |
| 320 |
return radius; |
| 321 |
} |
| 322 |
|
| 323 |
- (void)drawRect:(NSRect)dirtyFrame |
| 324 |
{ |
| 325 |
#if 0 |
| 326 |
[[NSColor redColor] set]; |
| 327 |
[NSBezierPath strokeRect:[self bounds]]; |
| 328 |
#endif |
| 329 |
|
| 330 |
BOOL drawThumbnail = NSIntersectsRect(selectedBounds, dirtyFrame); |
| 331 |
BOOL drawLabel = NSIntersectsRect(labelBounds, dirtyFrame); |
| 332 |
|
| 333 |
if(selected && drawThumbnail) { |
| 334 |
CGFloat radius = [self selectRectRadius]; |
| 335 |
NSRect frame = selectedBounds; |
| 336 |
NSBezierPath *bezier = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:radius yRadius:radius]; |
| 337 |
[[NSColor gridColor] set]; |
| 338 |
[bezier fill]; |
| 339 |
} |
| 340 |
|
| 341 |
if(drawThumbnail) { |
| 342 |
[thumbnailCell drawWithFrame:thumbnailBounds inView:self]; |
| 343 |
} |
| 344 |
|
| 345 |
if(drawLabel) { |
| 346 |
NSRect frame = labelBounds; |
| 347 |
CGFloat radius = [self selectedTitleRectRadius]; |
| 348 |
NSBezierPath *bezier = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:radius yRadius:radius]; |
| 349 |
[backgroundColor set]; |
| 350 |
[bezier fill]; |
| 351 |
|
| 352 |
[labelCell drawWithFrame:labelBounds inView:self]; |
| 353 |
|
| 354 |
if(selected && [labelCell integerValue] != 0) { |
| 355 |
frame = NSInsetRect(frame, 2, 2); |
| 356 |
radius = radius - 2; |
| 357 |
bezier = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:radius yRadius:radius]; |
| 358 |
[bezier fill]; |
| 359 |
} |
| 360 |
[titleCell drawWithFrame:titleBounds inView:self]; |
| 361 |
} |
| 362 |
if(NSIntersectsRect(rateBounds, dirtyFrame)) { |
| 363 |
[rateCell drawWithFrame:rateBounds inView:self]; |
| 364 |
} |
| 365 |
if(NSIntersectsRect(rateLabelBounds, dirtyFrame)) { |
| 366 |
[rateTitleCell drawWithFrame:rateLabelBounds inView:self]; |
| 367 |
} |
| 368 |
|
| 369 |
#if 0 |
| 370 |
[[NSColor redColor] set]; |
| 371 |
[NSBezierPath strokeRect:rateLabelBounds]; |
| 372 |
#endif |
| 373 |
} |
| 374 |
|
| 375 |
- (NSRect)imageFrame |
| 376 |
{ |
| 377 |
return [(XspfMShadowImageCell *)thumbnailCell imageRectForBounds:thumbnailBounds inView:self]; |
| 378 |
} |
| 379 |
|
| 380 |
- (void)setTarget:(id)newTarget |
| 381 |
{ |
| 382 |
target = newTarget; |
| 383 |
} |
| 384 |
- (void)setAction:(SEL)newAction |
| 385 |
{ |
| 386 |
action = newAction; |
| 387 |
} |
| 388 |
- (void)mouseDown:(NSEvent *)event |
| 389 |
{ |
| 390 |
[self.window endEditingFor:self]; |
| 391 |
|
| 392 |
NSPoint mouse = [self convertPoint:[event locationInWindow] fromView:nil]; |
| 393 |
|
| 394 |
if([self mouse:mouse inRect:rateBounds]) { |
| 395 |
[rateCell trackMouse:event inRect:rateBounds ofView:self untilMouseUp:YES]; |
| 396 |
id dict = [self infoForBinding:XspfMRatingBinding]; |
| 397 |
id obj = [dict objectForKey:NSObservedObjectKey]; |
| 398 |
id key = [dict objectForKey:NSObservedKeyPathKey]; |
| 399 |
[obj setValue:[rateCell objectValue] forKeyPath:key]; |
| 400 |
[self setNeedsDisplayInRect:rateBounds]; |
| 401 |
return; |
| 402 |
} |
| 403 |
|
| 404 |
if([event clickCount] == 2 && [self mouse:mouse inRect:titleBounds]) { |
| 405 |
NSText *fieldEditor = [self.window fieldEditor:YES forObject:self]; |
| 406 |
[titleCell setTextColor:[NSColor textColor]]; |
| 407 |
[titleCell setBezeled:YES]; |
| 408 |
[titleCell setShowsFirstResponder:YES]; |
| 409 |
[titleCell editWithFrame:titleBounds |
| 410 |
inView:self |
| 411 |
editor:fieldEditor |
| 412 |
delegate:self |
| 413 |
event:event]; |
| 414 |
[fieldEditor selectAll:nil]; |
| 415 |
|
| 416 |
return; |
| 417 |
} |
| 418 |
if([event clickCount] == 2 && [self mouse:mouse inRect:thumbnailBounds]) { |
| 419 |
[self sendAction:action to:target]; |
| 420 |
|
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
return [super mouseDown:event]; |
| 425 |
} |
| 426 |
- (void)textDidEndEditing:(NSNotification *)notification |
| 427 |
{ |
| 428 |
NSText *fieldEditor =[notification object]; |
| 429 |
[titleCell setStringValue:[[[fieldEditor string] copy] autorelease]]; |
| 430 |
[titleCell setBezeled:NO]; |
| 431 |
[titleCell setDrawsBackground:NO]; |
| 432 |
[titleCell endEditing:fieldEditor]; |
| 433 |
[self.window makeFirstResponder:self.superview]; |
| 434 |
|
| 435 |
id dict = [self infoForBinding:NSTitleBinding]; |
| 436 |
id obj = [dict objectForKey:NSObservedObjectKey]; |
| 437 |
id key = [dict objectForKey:NSObservedKeyPathKey]; |
| 438 |
[obj setValue:[titleCell stringValue] forKeyPath:key]; |
| 439 |
|
| 440 |
[self setNeedsDisplayInRect:labelBounds]; |
| 441 |
} |
| 442 |
|
| 443 |
@end |