Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMCollectionItemView.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 592 - (hide annotations) (download)
Fri Jun 15 12:39:28 2012 UTC (11 years, 10 months ago) by masakih
File size: 13485 byte(s)
[Mod] XspfMLabelシリーズをHMLabelに改名

1 masakih 514 //
2 masakih 526 // XspfMCollectionItemView.m
3 masakih 514 // XspfManager
4     //
5     // Created by Hori,Masaki on 11/01/25.
6     //
7    
8 masakih 526 /*
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 masakih 514 #import "XspfMCollectionItemView.h"
63    
64 masakih 592 #import "HMLabelCell.h"
65 masakih 516 #import "XspfMShadowImageCell.h"
66 masakih 514
67 masakih 520
68 masakih 548 NSString *const XspfMThumbnailBinding = @"thumbnail";
69     NSString *const XspfMTitleColorBinding = @"titleColor";
70     NSString *const XspfMRatingBinding = @"rating";
71 masakih 520
72    
73 masakih 514 @implementation XspfMCollectionItemView
74    
75 masakih 541 + (void)initialize
76     {
77     static BOOL isFirst = YES;
78     if(isFirst){
79     isFirst = NO;
80    
81 masakih 548 [self exposeBinding:XspfMThumbnailBinding];
82     [self exposeBinding:NSTitleBinding];
83     [self exposeBinding:XspfMTitleColorBinding];
84     [self exposeBinding:XspfMRatingBinding];
85     [self exposeBinding:NSLabelBinding];
86 masakih 541 }
87     }
88    
89 masakih 521 - (Class)valueClassForBinding:(NSString *)binding
90     {
91 masakih 548 if([binding isEqualToString:XspfMThumbnailBinding]) {
92 masakih 521 return [NSImage class];
93     }
94 masakih 548 if([binding isEqualToString:NSTitleBinding]) {
95 masakih 521 return [NSString class];
96     }
97 masakih 548 if([binding isEqualToString:XspfMTitleColorBinding]) {
98 masakih 521 return [NSColor class];
99     }
100 masakih 548 if([binding isEqualToString:XspfMRatingBinding]) {
101 masakih 521 return [NSValue class];
102     }
103 masakih 548 if([binding isEqualToString:NSLabelBinding]) {
104 masakih 521 return [NSValue class];
105     }
106    
107     return [super valueClassForBinding:binding];
108     }
109    
110 masakih 514 - (void)setup
111     {
112 masakih 534 controlSize = -1;;
113 masakih 520
114 masakih 516 thumbnailCell = [[XspfMShadowImageCell alloc] initImageCell:nil];
115 masakih 514
116     titleCell = [[NSTextFieldCell alloc] initTextCell:@""];
117 masakih 515 [titleCell setEditable:YES];
118     [titleCell setSelectable:YES];
119     [titleCell setEnabled:YES];
120 masakih 514
121     rateCell = [[NSLevelIndicatorCell alloc] initWithLevelIndicatorStyle:NSRatingLevelIndicatorStyle];
122 masakih 515 [rateCell setEditable:YES];
123     [rateCell setEnabled:YES];
124     [rateCell setHighlighted:YES];
125 masakih 514
126 masakih 528 rateTitleCell = [[NSTextFieldCell alloc] initTextCell:NSLocalizedString(@"Rate:", @"Icon view Rate label.")];
127 masakih 514 [rateTitleCell setAlignment:NSRightTextAlignment];
128    
129 masakih 592 labelCell = [[HMLabelCell alloc] initTextCell:@""];
130     [labelCell setLabelStyle:HMSquareStyle];
131 masakih 514 [labelCell setDrawX:NO];
132 masakih 521
133 masakih 523 if([self frame].size.height < 200) {
134 masakih 521 [self setControlSize:NSSmallControlSize];
135 masakih 534 } else {
136     [self setControlSize:NSRegularControlSize];
137 masakih 521 }
138 masakih 536
139     [self calcSize];
140 masakih 514 }
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 masakih 541
166 masakih 514 [super dealloc];
167     }
168    
169 masakih 541 - (void)setValue:(id)value forKey:(NSString *)key
170 masakih 520 {
171 masakih 548 if([key isEqualToString:XspfMThumbnailBinding]) {
172 masakih 541 [thumbnailCell setImage:value];
173 masakih 545 [self setNeedsDisplayInRect:selectedBounds];
174 masakih 520 return;
175     }
176 masakih 548 if([key isEqualToString:NSTitleBinding]) {
177 masakih 541 value = value ? value : @"";
178     [titleCell setStringValue:value];
179 masakih 520 return;
180     }
181 masakih 548 if([key isEqualToString:XspfMTitleColorBinding]) {
182 masakih 541 [titleCell setTextColor:value];
183 masakih 545 [self setNeedsDisplayInRect:titleBounds];
184 masakih 520 return;
185     }
186 masakih 548 if([key isEqualToString:XspfMRatingBinding]) {
187 masakih 541 [rateCell setObjectValue:value];
188 masakih 520 return;
189     }
190 masakih 548 if([key isEqualToString:NSLabelBinding]) {
191 masakih 541 [labelCell setObjectValue:value];
192 masakih 520 return;
193     }
194 masakih 515
195 masakih 541 [super setValue:value forKey:key];
196 masakih 514 }
197 masakih 541 - (id)valueForKey:(NSString *)key
198 masakih 522 {
199 masakih 548 if([key isEqualToString:XspfMThumbnailBinding]) {
200 masakih 541 return [thumbnailCell image];
201 masakih 522 }
202 masakih 548 if([key isEqualToString:NSTitleBinding]) {
203 masakih 541 return [titleCell stringValue];
204 masakih 522 }
205 masakih 548 if([key isEqualToString:XspfMTitleColorBinding]) {
206 masakih 541 return [titleCell textColor];
207 masakih 522 }
208 masakih 548 if([key isEqualToString:XspfMRatingBinding]) {
209 masakih 541 return [rateCell objectValue];
210 masakih 522 }
211 masakih 548 if([key isEqualToString:NSLabelBinding]) {
212 masakih 541 return [labelCell objectValue];
213 masakih 522 }
214    
215 masakih 541 return [super valueForKey:key];
216 masakih 522 }
217 masakih 514
218 masakih 541
219 masakih 514 - (void)setSelected:(BOOL)flag
220     {
221     if(selected && flag) return;
222     if(!selected && !flag) return;
223    
224     selected = flag;
225    
226 masakih 545 [self setNeedsDisplayInRect:selectedBounds];
227 masakih 514 }
228    
229 masakih 520 - (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 masakih 521 titleFont = [NSFont controlContentFontOfSize:11];
241 masakih 520 break;
242     }
243    
244     [titleCell setFont:titleFont];
245     [rateTitleCell setFont:titleFont];
246 masakih 521
247     [titleCell setControlSize:size];
248     [thumbnailCell setControlSize:size];
249     [rateCell setControlSize:size];
250     [rateTitleCell setControlSize:size];
251 masakih 520 }
252 masakih 514
253 masakih 520 - (NSControlSize)controlSize
254     {
255     return controlSize;
256     }
257 masakih 536 - (void)setFrameSize:(NSSize)newSize
258     {
259     [super setFrameSize:newSize];
260     [self calcSize];
261     }
262 masakih 520
263 masakih 536 - (void)calcSize
264 masakih 514 {
265 masakih 536 if([self frame].size.height < 200) {
266     [self setControlSize:NSSmallControlSize];
267     } else {
268     [self setControlSize:NSRegularControlSize];
269 masakih 520 }
270 masakih 536
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 masakih 538 CGFloat rateTitleWidth = ceil(thumbnailWidth * 0.4);
285 masakih 536
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 masakih 514 }
295 masakih 545
296 masakih 529 - (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 masakih 514
323     - (void)drawRect:(NSRect)dirtyFrame
324     {
325 masakih 533 #if 0
326     [[NSColor redColor] set];
327     [NSBezierPath strokeRect:[self bounds]];
328     #endif
329 masakih 544
330 masakih 545 BOOL drawThumbnail = NSIntersectsRect(selectedBounds, dirtyFrame);
331     BOOL drawLabel = NSIntersectsRect(labelBounds, dirtyFrame);
332 masakih 544
333     if(selected && drawThumbnail) {
334 masakih 529 CGFloat radius = [self selectRectRadius];
335 masakih 545 NSRect frame = selectedBounds;
336 masakih 514 NSBezierPath *bezier = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:radius yRadius:radius];
337 masakih 515 [[NSColor gridColor] set];
338 masakih 514 [bezier fill];
339     }
340 masakih 529
341 masakih 544 if(drawThumbnail) {
342 masakih 545 [thumbnailCell drawWithFrame:thumbnailBounds inView:self];
343 masakih 544 }
344 masakih 514
345 masakih 544 if(drawLabel) {
346 masakih 545 NSRect frame = labelBounds;
347 masakih 544 CGFloat radius = [self selectedTitleRectRadius];
348     NSBezierPath *bezier = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:radius yRadius:radius];
349     [backgroundColor set];
350 masakih 542 [bezier fill];
351 masakih 544
352 masakih 545 [labelCell drawWithFrame:labelBounds inView:self];
353 masakih 544
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 masakih 545 [titleCell drawWithFrame:titleBounds inView:self];
361 masakih 542 }
362 masakih 545 if(NSIntersectsRect(rateBounds, dirtyFrame)) {
363     [rateCell drawWithFrame:rateBounds inView:self];
364 masakih 544 }
365 masakih 545 if(NSIntersectsRect(rateLabelBounds, dirtyFrame)) {
366     [rateTitleCell drawWithFrame:rateLabelBounds inView:self];
367 masakih 544 }
368 masakih 536
369     #if 0
370     [[NSColor redColor] set];
371 masakih 545 [NSBezierPath strokeRect:rateLabelBounds];
372 masakih 536 #endif
373 masakih 514 }
374 masakih 515
375 masakih 518 - (NSRect)imageFrame
376     {
377 masakih 545 return [(XspfMShadowImageCell *)thumbnailCell imageRectForBounds:thumbnailBounds inView:self];
378 masakih 518 }
379 masakih 515
380 masakih 537 - (void)setTarget:(id)newTarget
381     {
382     target = newTarget;
383     }
384     - (void)setAction:(SEL)newAction
385     {
386     action = newAction;
387     }
388 masakih 515 - (void)mouseDown:(NSEvent *)event
389     {
390     [self.window endEditingFor:self];
391    
392     NSPoint mouse = [self convertPoint:[event locationInWindow] fromView:nil];
393    
394 masakih 545 if([self mouse:mouse inRect:rateBounds]) {
395     [rateCell trackMouse:event inRect:rateBounds ofView:self untilMouseUp:YES];
396 masakih 548 id dict = [self infoForBinding:XspfMRatingBinding];
397 masakih 541 id obj = [dict objectForKey:NSObservedObjectKey];
398     id key = [dict objectForKey:NSObservedKeyPathKey];
399     [obj setValue:[rateCell objectValue] forKeyPath:key];
400 masakih 545 [self setNeedsDisplayInRect:rateBounds];
401 masakih 515 return;
402     }
403    
404 masakih 545 if([event clickCount] == 2 && [self mouse:mouse inRect:titleBounds]) {
405 masakih 515 NSText *fieldEditor = [self.window fieldEditor:YES forObject:self];
406     [titleCell setTextColor:[NSColor textColor]];
407     [titleCell setBezeled:YES];
408 masakih 533 [titleCell setShowsFirstResponder:YES];
409 masakih 545 [titleCell editWithFrame:titleBounds
410 masakih 515 inView:self
411     editor:fieldEditor
412     delegate:self
413     event:event];
414     [fieldEditor selectAll:nil];
415 masakih 533
416 masakih 515 return;
417     }
418 masakih 545 if([event clickCount] == 2 && [self mouse:mouse inRect:thumbnailBounds]) {
419 masakih 537 [self sendAction:action to:target];
420    
421     return;
422     }
423 masakih 515
424     return [super mouseDown:event];
425     }
426     - (void)textDidEndEditing:(NSNotification *)notification
427     {
428     NSText *fieldEditor =[notification object];
429 masakih 539 [titleCell setStringValue:[[[fieldEditor string] copy] autorelease]];
430 masakih 515 [titleCell setBezeled:NO];
431     [titleCell setDrawsBackground:NO];
432     [titleCell endEditing:fieldEditor];
433 masakih 523 [self.window makeFirstResponder:self.superview];
434 masakih 515
435 masakih 548 id dict = [self infoForBinding:NSTitleBinding];
436 masakih 541 id obj = [dict objectForKey:NSObservedObjectKey];
437     id key = [dict objectForKey:NSObservedKeyPathKey];
438     [obj setValue:[titleCell stringValue] forKeyPath:key];
439 masakih 515
440 masakih 545 [self setNeedsDisplayInRect:labelBounds];
441 masakih 515 }
442    
443 masakih 514 @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26