1 #import "ITMultilineTextFieldCell.h"
3 @implementation ITMultilineTextFieldCell
5 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
6 if ([(id)[self objectValue] isKindOfClass:[NSArray class]]) {
9 Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
11 NSArray of NSStrings - Draw first line with System Font, following lines with Small System Font
12 NSArray of NSAttributedStrings - Draw as given
13 NSArray of both - Draw each line as above!
15 The number of lines is determined by the contents of the array...
18 NSColor *defaultColor;
19 NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
20 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
21 NSPoint cellPoint = cellFrame.origin;
22 NSSize cellSize = cellFrame.size;
24 NSRect secondaryLineRect;
26 if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
27 defaultColor = [NSColor whiteColor];
29 defaultColor = [NSColor blackColor];
32 // Process the first line...
34 NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
36 NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
38 id firstString = [(NSArray *)[self objectValue] objectAtIndex:0];
39 NSMutableAttributedString *firstAttrString;
41 if ([firstString isKindOfClass:[NSAttributedString class]]) {
42 firstAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:firstString] autorelease];
43 [firstAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[firstAttrString length])];
44 if ([defaultColor isEqual:[NSColor whiteColor]]) {
45 [firstAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[firstAttrString length])];
47 } else if ([firstString isKindOfClass:[NSString class]]) {
48 firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
50 firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
53 [controlView lockFocus];
55 [firstAttrString drawInRect:firstLineRect];
57 [controlView unlockFocus];
59 secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
62 // Process the secondary lines
64 NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
66 NSMutableArray *tMArray = [NSMutableArray arrayWithArray:(NSArray *)[self objectValue]];
67 [tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
69 NSEnumerator *enumerator = [tMArray objectEnumerator];
72 while ((secondaryString = [enumerator nextObject])) {
74 NSMutableAttributedString *secondaryAttrString;
76 if ([secondaryString isKindOfClass:[NSAttributedString class]]) {
77 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:secondaryString] autorelease];
78 [secondaryAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[secondaryAttrString length])];
79 if ([defaultColor isEqual:[NSColor whiteColor]]) {
80 [secondaryAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[secondaryAttrString length])];
82 } else if ([secondaryString isKindOfClass:[NSString class]]) {
83 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
85 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
88 [controlView lockFocus];
90 [secondaryAttrString drawInRect:secondaryLineRect];
92 [controlView unlockFocus];
94 secondaryLineRect.origin.y = secondaryLineRect.origin.y+[secondaryAttrString size].height; // modify the rect for the next loop, based on the size and location of the most recently processed line.
99 [super drawInteriorWithFrame:cellFrame inView:controlView];