Fixed a possible crasher where an ITMultilineTextFieldCell is treated as
[ITKit.git] / ITMultilineTextFieldCell.m
index 74b4749..b804768 100755 (executable)
 
 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
 {
-    /*
-        Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
-        
-            NSArray of NSStrings - Draw first line with System Font, following lines with Small System Font
-            NSArray of NSAttributedStrings - Draw as given
-            NSArray of both - Draw each line as above!
-        
-        The number of lines is determined by the contents of the array...
-    */
-    
-    NSColor *defaultColor;
-    NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
-    [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
-    NSPoint cellPoint = cellFrame.origin;
-    NSSize cellSize = cellFrame.size;
-    
-    NSRect secondaryLineRect;
+    if ([[self objectValue] isKindOfClass:[NSArray class]]) {
     
-    if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
-        defaultColor = [NSColor whiteColor];
-    } else {
-        defaultColor = [NSColor blackColor];
-    }
+        /*
+            Okay, here's the different possibilities for the objectValue of this cell, and how they're displayed:
+            
+                NSArray of NSStrings - Draw first line with System Font, following lines with Small System Font
+                NSArray of NSAttributedStrings - Draw as given
+                NSArray of both - Draw each line as above!
+            
+            The number of lines is determined by the contents of the array...
+        */
     
-    // Process the first line...
-    {
-        NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
-        
-        NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
+        NSColor *defaultColor;
+        NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
+        [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
+        NSPoint cellPoint = cellFrame.origin;
+        NSSize cellSize = cellFrame.size;
         
-        id firstString = [[self objectValue] objectAtIndex:0];
-        NSMutableAttributedString *firstAttrString;
+        NSRect secondaryLineRect;
         
-        if ([firstString isKindOfClass:[NSAttributedString class]]) {
-            firstAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:firstString] autorelease];
-            [firstAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[firstAttrString length])];
-            if ([defaultColor isEqual:[NSColor whiteColor]]) {
-            [firstAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[firstAttrString length])];
-            }
-        } else if ([firstString isKindOfClass:[NSString class]]) {
-            firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
+        if ([self isHighlighted] && ([self highlightColorWithFrame:cellFrame inView:controlView]!=[NSColor secondarySelectedControlColor])) {
+            defaultColor = [NSColor whiteColor];
         } else {
-            firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
+            defaultColor = [NSColor blackColor];
         }
         
-        [controlView lockFocus];
-        
-        [firstAttrString drawInRect:firstLineRect];
-        
-        [controlView unlockFocus];
-        
-        secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
-    }
-    
-    // Process the secondary lines
-    {
-        NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
-        
-        NSMutableArray *tMArray = [NSMutableArray arrayWithArray:[self objectValue]];
-        [tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
-        
-        NSEnumerator *enumerator = [tMArray objectEnumerator];
-        id secondaryString;
-        
-        while (secondaryString = [enumerator nextObject]) {
+        // Process the first line...
+        {
+            NSDictionary *firstLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
             
-            NSMutableAttributedString *secondaryAttrString;
+            NSRect firstLineRect = NSMakeRect(cellPoint.x+5, cellPoint.y+1, cellSize.width-5, cellSize.height);
             
-            if ([secondaryString isKindOfClass:[NSAttributedString class]]) {
-                secondaryAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:secondaryString] autorelease];
-                [secondaryAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[secondaryAttrString length])];
+            id firstString = [[self objectValue] objectAtIndex:0];
+            NSMutableAttributedString *firstAttrString;
+            
+            if ([firstString isKindOfClass:[NSAttributedString class]]) {
+                firstAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:firstString] autorelease];
+                [firstAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[firstAttrString length])];
                 if ([defaultColor isEqual:[NSColor whiteColor]]) {
-                [secondaryAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[secondaryAttrString length])];
+                [firstAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[firstAttrString length])];
                 }
-            } else if ([secondaryString isKindOfClass:[NSString class]]) {
-                secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
+            } else if ([firstString isKindOfClass:[NSString class]]) {
+                firstAttrString = [[[NSMutableAttributedString alloc] initWithString:firstString attributes:firstLineAttributes] autorelease];
             } else {
-                secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
+                firstAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", firstString] attributes:firstLineAttributes] autorelease];
             }
             
             [controlView lockFocus];
             
-            [secondaryAttrString drawInRect:secondaryLineRect];
+            [firstAttrString drawInRect:firstLineRect];
             
             [controlView unlockFocus];
             
-            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.
+            secondaryLineRect = NSMakeRect(cellPoint.x+5, (cellPoint.y+1+[firstAttrString size].height), cellSize.width-5, cellSize.height);
+        }
         
+        // Process the secondary lines
+        {
+            NSDictionary *secondaryLineAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, defaultColor, NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
+            
+            NSMutableArray *tMArray = [NSMutableArray arrayWithArray:[self objectValue]];
+            [tMArray removeObjectAtIndex:0]; // Remove the first line string... already handled that above!
+            
+            NSEnumerator *enumerator = [tMArray objectEnumerator];
+            id secondaryString;
+            
+            while (secondaryString = [enumerator nextObject]) {
+                
+                NSMutableAttributedString *secondaryAttrString;
+                
+                if ([secondaryString isKindOfClass:[NSAttributedString class]]) {
+                    secondaryAttrString = [[[NSMutableAttributedString alloc] initWithAttributedString:secondaryString] autorelease];
+                    [secondaryAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[secondaryAttrString length])];
+                    if ([defaultColor isEqual:[NSColor whiteColor]]) {
+                    [secondaryAttrString addAttribute:NSForegroundColorAttributeName value:defaultColor range:NSMakeRange(0,[secondaryAttrString length])];
+                    }
+                } else if ([secondaryString isKindOfClass:[NSString class]]) {
+                    secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:secondaryString attributes:secondaryLineAttributes] autorelease];
+                } else {
+                    secondaryAttrString = [[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Object (%@) is not a string", secondaryString] attributes:secondaryLineAttributes] autorelease];
+                }
+                
+                [controlView lockFocus];
+                
+                [secondaryAttrString drawInRect:secondaryLineRect];
+                
+                [controlView unlockFocus];
+                
+                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.
+            
+            }
         }
+    } else {
+        [super drawInteriorWithFrame:cellFrame inView:controlView];
     }
 }