root/trunk/lilypad/Sources/LPSourceListCell.m @ 172

Revision 172, 3.6 KB (checked in by jppavao, 5 years ago)

Fixed some cosmetic bugs on the message center source list.

Line 
1//
2//  LPSourceListCell.m
3//  Lilypad
4//
5//      Copyright (C) 2006-2007 PT.COM,  All rights reserved.
6//      Author: Joao Pavao <jppavao@criticalsoftware.com>
7//
8//      For more information on licensing, read the README file.
9//      Para mais informações sobre o licenciamento, leia o ficheiro README.
10//
11
12#import "LPSourceListCell.h"
13
14
15@implementation LPSourceListCell
16
17- (id)copyWithZone:(NSZone *)zone
18{
19        LPSourceListCell *newObj = [super copyWithZone:zone];
20       
21        newObj->m_icon = nil;
22        [newObj setImage:[self image]];
23       
24        return newObj;
25}
26
27- (void)dealloc
28{
29        [m_icon release];
30        [super dealloc];
31}
32
33- (NSImage *)image
34{
35        return [[m_icon retain] autorelease];
36}
37
38- (void)setImage:(NSImage *)img
39{
40        if (m_icon != img) {
41                [m_icon release];
42                m_icon = [img copy];
43        }
44}
45
46- (unsigned int)newItemsCount
47{
48        return m_newItemsCount;
49}
50
51- (void)setNewItemsCount:(unsigned int)count
52{
53        m_newItemsCount = count;
54}
55
56
57#pragma mark New Items Count
58
59- (NSAttributedString *)newItemsCountAttributedString
60{
61        NSString *newItemsCountString = [NSString stringWithFormat:@"%d", [self newItemsCount]];
62       
63        NSDictionary *attribs = [NSDictionary dictionaryWithObjectsAndKeys:
64                [NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName,
65                ([self isHighlighted] ? [NSColor whiteColor] : [NSColor blackColor]), NSForegroundColorAttributeName,
66                nil];
67       
68        return [[[NSAttributedString alloc] initWithString:newItemsCountString attributes:attribs] autorelease];
69}
70
71#pragma mark NSCell overrides
72
73
74- (NSRect)imageRectForBounds:(NSRect)theRect
75{
76        NSRect imageRect, remainder;
77        NSDivideRect(theRect, &imageRect, &remainder, NSHeight(theRect), NSMinXEdge);
78        return NSInsetRect(NSOffsetRect(imageRect, 4.0, 0.0), 1.0, 1.0);
79}
80
81- (NSRect)titleRectForBounds:(NSRect)theRect
82{
83        NSSize titleSize = [[self attributedStringValue] size];
84       
85        NSRect imageRect = [self imageRectForBounds:theRect];
86        NSRect countRect = [self newItemsCountRectForBounds:theRect];
87        NSRect titleRect;
88       
89        titleRect.origin.x = NSMaxX(imageRect) + 4.0;
90        titleRect.origin.y = NSMinY(theRect) + (NSHeight(theRect) - titleSize.height) / 2.0;
91        titleRect.size.width = (NSIsEmptyRect(countRect) ? NSMaxX(theRect) : NSMinX(countRect)) - NSMaxX(imageRect) - 6.0;
92        titleRect.size.height = titleSize.height;
93       
94        return titleRect;
95}
96
97- (NSRect)newItemsCountRectForBounds:(NSRect)theRect
98{
99        if ([self newItemsCount] > 0) {
100                NSSize countStringSize = [[self newItemsCountAttributedString] size];
101               
102                NSRect newItemsCountRect, remainder;
103                NSDivideRect(theRect, &newItemsCountRect, &remainder, countStringSize.width + 2.0, NSMaxXEdge);
104               
105                newItemsCountRect.origin.y += (NSHeight(newItemsCountRect) - countStringSize.height) / 2.0;
106                newItemsCountRect.size.height = countStringSize.height;
107               
108                return newItemsCountRect;
109        }
110        else {
111                return NSZeroRect;
112        }
113}
114
115- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
116{
117        NSRect imageRect = [self imageRectForBounds:cellFrame];
118        NSRect titleRect = [self titleRectForBounds:cellFrame];
119        NSRect countRect = [self newItemsCountRectForBounds:cellFrame];
120       
121        // DEBUG
122//      [[NSColor blueColor] set];
123//      NSFrameRect(imageRect);
124//      [[NSColor greenColor] set];
125//      NSFrameRect(titleRect);
126//      [[NSColor redColor] set];
127//      NSFrameRect(countRect);
128       
129        NSImage *img = [self image];
130        NSSize imgSize = (img ? [img size] : NSZeroSize);
131       
132        [img setFlipped:[controlView isFlipped]];
133        [img drawInRect:imageRect
134                   fromRect:NSMakeRect(0.0, 0.0, imgSize.width, imgSize.height)
135                  operation:NSCompositeSourceOver
136                   fraction:1.0];
137       
138        // Use super's powers to draw our string value
139        [super drawInteriorWithFrame:titleRect inView:controlView];
140       
141        if ([self newItemsCount] > 0)
142                [[self newItemsCountAttributedString] drawAtPoint:countRect.origin];
143}
144
145@end
Note: See TracBrowser for help on using the browser.