Adding the ITMultilineTextFieldCell work I've done so far...
[ITKit.git] / ITMultilineTextFieldCell.m
1 //
2 //  ITMultilineTextFieldCell.m
3 //  ITKit
4 //
5 //  Created by Joseph Spiros on Fri Mar 05 2004.
6 //  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "ITMultilineTextFieldCell.h"
10
11
12 @implementation ITMultilineTextFieldCell
13
14 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
15 {
16     /*
17         Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
18         
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!
22         
23         The number of lines is determined by the contents of the array...
24     */
25     
26     NSColor *defaultColor;
27     NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
28     [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
29     NSPoint cellPoint = cellFrame.origin;
30     NSSize cellSize = cellFrame.size;
31     
32     NSRect secondaryLineRect;
33     
34     if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
35         defaultColor = [NSColor whiteColor];
36     } else {
37         defaultColor = [NSColor blackColor];
38     }
39     
40     // Process the first line...
41     {
42         NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
43         
44         NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
45         
46         id firstString = [[self objectValue] objectAtIndex:0];
47         NSMutableAttributedString *firstAttrString;
48         
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])];
54             }
55         } else if ([firstString isKindOfClass:[NSString class]]) {
56             firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
57         } else {
58             firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
59         }
60         
61         [controlView lockFocus];
62         
63         [firstAttrString drawInRect:firstLineRect];
64         
65         [controlView unlockFocus];
66         
67         secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
68     }
69     
70     // Process the secondary lines
71     {
72         NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
73         
74         NSMutableArray *tMArray = [NSMutableArray arrayWithArray:[self objectValue]];
75         [tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
76         
77         NSEnumerator *enumerator = [tMArray objectEnumerator];
78         id secondaryString;
79         
80         while (secondaryString = [enumerator nextObject]) {
81             
82             NSMutableAttributedString *secondaryAttrString;
83             
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])];
89                 }
90             } else if ([secondaryString isKindOfClass:[NSString class]]) {
91                 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
92             } else {
93                 secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
94             }
95             
96             [controlView lockFocus];
97             
98             [secondaryAttrString drawInRect:secondaryLineRect];
99             
100             [controlView unlockFocus];
101             
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.
103         
104         }
105     }
106 }
107
108 @end