Huge audit of ITKit, mostly everything has been updated to current coding
[ITKit.git] / ITMultilineTextFieldCell.m
1 #import "ITMultilineTextFieldCell.h"
2
3 @implementation ITMultilineTextFieldCell
4
5 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
6         if ([(id)[self objectValue] isKindOfClass:[NSArray class]]) {
7         
8                 /*
9                         Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
10                         
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!
14                         
15                         The number of lines is determined by the contents of the array...
16                 */
17         
18                 NSColor *defaultColor;
19                 NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
20                 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
21                 NSPoint cellPoint = cellFrame.origin;
22                 NSSize cellSize = cellFrame.size;
23                 
24                 NSRect secondaryLineRect;
25                 
26                 if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
27                         defaultColor = [NSColor whiteColor];
28                 } else {
29                         defaultColor = [NSColor blackColor];
30                 }
31                 
32                 // Process the first line...
33                 {
34                         NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
35                         
36                         NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
37                         
38                         id firstString = [(NSArray *)[self objectValue] objectAtIndex:0];
39                         NSMutableAttributedString *firstAttrString;
40                         
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])];
46                                 }
47                         } else if ([firstString isKindOfClass:[NSString class]]) {
48                                 firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
49                         } else {
50                                 firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
51                         }
52                         
53                         [controlView lockFocus];
54                         
55                         [firstAttrString drawInRect:firstLineRect];
56                         
57                         [controlView unlockFocus];
58                         
59                         secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
60                 }
61                 
62                 // Process the secondary lines
63                 {
64                         NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
65                         
66                         NSMutableArray *tMArray = [NSMutableArray arrayWithArray:(NSArray *)[self objectValue]];
67                         [tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
68                         
69                         NSEnumerator *enumerator = [tMArray objectEnumerator];
70                         id secondaryString;
71                         
72                         while ((secondaryString = [enumerator nextObject])) {
73                                 
74                                 NSMutableAttributedString *secondaryAttrString;
75                                 
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])];
81                                         }
82                                 } else if ([secondaryString isKindOfClass:[NSString class]]) {
83                                         secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
84                                 } else {
85                                         secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
86                                 }
87                                 
88                                 [controlView lockFocus];
89                                 
90                                 [secondaryAttrString drawInRect:secondaryLineRect];
91                                 
92                                 [controlView unlockFocus];
93                                 
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.
95                         
96                         }
97                 }
98         } else {
99                 [super drawInteriorWithFrame:cellFrame inView:controlView];
100         }
101 }
102
103 @end