2 // ITMultilineTextFieldCell.m
5 // Created by Joseph Spiros on Fri Mar 05 2004.
6 // Copyright (c) 2004 __MyCompanyName__. All rights reserved.
9 #import "ITMultilineTextFieldCell.h"
12 @implementation ITMultilineTextFieldCell
14 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
17 Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
19 NSArray of NSStrings - Draw first line with System Font, following lines with Small System Font
20 NSArray of NSAttributedStrings - Draw as given
21 NSArray of both - Draw each line as above!
23 The number of lines is determined by the contents of the array...
26 NSColor *defaultColor;
27 NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
28 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
29 NSPoint cellPoint = cellFrame.origin;
30 NSSize cellSize = cellFrame.size;
32 NSRect secondaryLineRect;
34 if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
35 defaultColor = [NSColor whiteColor];
37 defaultColor = [NSColor blackColor];
40 // Process the first line...
42 NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
44 NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
46 id firstString = [[self objectValue] objectAtIndex:0];
47 NSMutableAttributedString *firstAttrString;
49 if ([firstString isKindOfClass:[NSAttributedString class]]) {
50 firstAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:firstString] autorelease];
51 [firstAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[firstAttrString length])];
52 if ([defaultColor isEqual:[NSColor whiteColor]]) {
53 [firstAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[firstAttrString length])];
55 } else if ([firstString isKindOfClass:[NSString class]]) {
56 firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
58 firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
61 [controlView lockFocus];
63 [firstAttrString drawInRect:firstLineRect];
65 [controlView unlockFocus];
67 secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
70 // Process the secondary lines
72 NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
74 NSMutableArray *tMArray = [NSMutableArray arrayWithArray:[self objectValue]];
75 [tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
77 NSEnumerator *enumerator = [tMArray objectEnumerator];
80 while (secondaryString = [enumerator nextObject]) {
82 NSMutableAttributedString *secondaryAttrString;
84 if ([secondaryString isKindOfClass:[NSAttributedString class]]) {
85 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:secondaryString] autorelease];
86 [secondaryAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[secondaryAttrString length])];
87 if ([defaultColor isEqual:[NSColor whiteColor]]) {
88 [secondaryAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[secondaryAttrString length])];
90 } else if ([secondaryString isKindOfClass:[NSString class]]) {
91 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
93 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
96 [controlView lockFocus];
98 [secondaryAttrString drawInRect:secondaryLineRect];
100 [controlView unlockFocus];
102 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.