Changeset 2709


Ignore:
Timestamp:
Aug 10, 2007, 5:34:52 PM (15 years ago)
Author:
livings124
Message:

layout file browser cell a bit more dynamically

Location:
trunk/macosx
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/FileBrowserCell.h

    r2149 r2709  
    2828{
    2929    float fPercent;
     30   
     31    NSMutableDictionary * fTitleAttributes, * fStatusAttributes;
    3032}
    3133
  • trunk/macosx/FileBrowserCell.m

    r2269 r2709  
    2626#import "StringAdditions.h"
    2727
    28 #define SPACE 2.0
     28#define PADDING_BEFORE_IMAGE 2.0
     29#define IMAGE_FOLDER_SIZE 16.0
     30#define IMAGE_ICON_SIZE 32.0
     31#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
     32#define PADDING_ABOVE_TITLE_REG 1.0
     33#define PADDING_BELOW_STATUS_REG 1.0
     34#define PADDING_AFTER_TITLE 1.0
    2935
    3036@implementation FileBrowserCell
     
    5056}
    5157
     58- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
     59{
     60    if (!fTitleAttributes)
     61    {
     62        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
     63        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
     64       
     65        fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     66                            [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
     67                            paragraphStyle, NSParagraphStyleAttributeName, nil];
     68       
     69        [paragraphStyle release];
     70    }
     71   
     72    if (color)
     73        [fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
     74       
     75    NSString * title = [[self objectValue] objectForKey: @"Name"];
     76    return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
     77}
     78
     79- (NSRect) titleRectForBounds: (NSRect) bounds
     80{
     81    NSAttributedString * title = [self attributedTitleWithColor: nil];
     82    NSSize titleSize = [title size];
     83   
     84    NSRect result = bounds;
     85   
     86    if (![[[self objectValue] objectForKey: @"IsFolder"] boolValue])
     87    {
     88        result.origin.x += PADDING_BEFORE_IMAGE + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
     89        result.origin.y += PADDING_ABOVE_TITLE_REG;
     90    }
     91    else
     92    {
     93        result.origin.x += PADDING_BEFORE_IMAGE + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
     94        result.origin.y += (result.size.height - titleSize.height) * 0.5;
     95    }
     96    result.size = titleSize;
     97    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_AFTER_TITLE);
     98   
     99    return result;
     100}
     101
     102- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color
     103{
     104    if (!fStatusAttributes)
     105    {
     106        NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
     107        [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
     108       
     109        fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     110                                [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
     111                                paragraphStyle, NSParagraphStyleAttributeName, nil];
     112       
     113        [paragraphStyle release];
     114    }
     115   
     116    if (color)
     117        [fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
     118   
     119    #warning fPercent?
     120    NSString * status = [NSString stringWithFormat: NSLocalizedString(@"%.2f%% of %@",
     121                            "Inspector -> Files tab -> file status string"), fPercent,
     122                            [NSString stringForFileSize: [[[self objectValue] objectForKey: @"Size"] unsignedLongLongValue]]];
     123   
     124    return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
     125}
     126
     127- (NSRect) statusRectForBounds: (NSRect) bounds
     128{
     129    if ([[[self objectValue] objectForKey: @"IsFolder"] boolValue])
     130        return NSZeroRect;
     131   
     132    NSAttributedString * status = [self attributedStatusWithColor: nil];
     133    NSSize statusSize = [status size];
     134   
     135    NSRect result = bounds;
     136   
     137    result.origin.x += PADDING_BEFORE_IMAGE + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
     138    result.origin.y += result.size.height - PADDING_BELOW_STATUS_REG - statusSize.height;
     139   
     140    result.size = statusSize;
     141    result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_AFTER_TITLE);
     142   
     143    return result;
     144}
     145
     146- (NSRect) imageRectForBounds: (NSRect) bounds
     147{
     148    NSRect result = bounds;
     149   
     150    result.origin.x += PADDING_BEFORE_IMAGE;
     151   
     152    const float IMAGE_SIZE = [[[self objectValue] objectForKey: @"IsFolder"] boolValue] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
     153    result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5;
     154    result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
     155   
     156    return result;
     157}
     158
    52159- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
    53160{
    54     NSMutableDictionary * item = [self objectValue];
     161    //icon
     162    [[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect
     163        operation: NSCompositeSourceOver fraction: 1.0];
    55164   
    56     //image
    57     float imageHeight = cellFrame.size.height - 2.0;
    58    
    59     NSImage * image = [self image];
    60     [image setSize: NSMakeSize(imageHeight, imageHeight)];
    61     NSRect imageRect = NSMakeRect(cellFrame.origin.x + 2.0 * SPACE,
    62                                     cellFrame.origin.y + (cellFrame.size.height - imageHeight) / 2.0,
    63                                     imageHeight, imageHeight);
    64    
    65     [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
    66    
    67     //text
    68     NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    69     [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
    70    
     165    //title
    71166    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
    72167                                                isEqual: [NSColor alternateSelectedControlColor]];
    73     NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
    74                     highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
    75                     [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
    76                     paragraphStyle, NSParagraphStyleAttributeName, nil];
    77168   
    78     NSString * nameString = [item objectForKey: @"Name"];
    79     NSRect nameTextRect = NSMakeRect(NSMaxX(imageRect) + SPACE, cellFrame.origin.y + 1.0,
    80             NSMaxX(cellFrame) - NSMaxX(imageRect) - 2.0 * SPACE, [nameString sizeWithAttributes: nameAttributes].height);
     169    [[self attributedTitleWithColor: highlighted ? [NSColor whiteColor] : [NSColor controlTextColor]]
     170        drawInRect: [self titleRectForBounds: cellFrame]];
    81171   
    82     [nameString drawInRect: nameTextRect withAttributes: nameAttributes];
    83     [nameAttributes release];
    84    
    85     //status text
    86     if (![[item objectForKey: @"IsFolder"] boolValue])
    87     {
    88         NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
    89                         highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
    90                         [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
    91                         paragraphStyle, NSParagraphStyleAttributeName, nil];
    92        
    93         NSString * statusString = [NSString stringWithFormat: NSLocalizedString(@"%.2f%% of %@",
    94                                     "Inspector -> Files tab -> file status string"), fPercent,
    95                                     [NSString stringForFileSize: [[item objectForKey: @"Size"] unsignedLongLongValue]]];
    96        
    97         NSRect statusTextRect = nameTextRect;
    98         statusTextRect.size.height = [statusString sizeWithAttributes: statusAttributes].height;
    99         statusTextRect.origin.y += (cellFrame.size.height + nameTextRect.size.height - statusTextRect.size.height) * 0.5 - 1.0;
    100        
    101         [statusString drawInRect: statusTextRect withAttributes: statusAttributes];
    102         [statusAttributes release];
    103     }
    104    
    105     [paragraphStyle release];
     172    //status
     173    NSRect statusRect = [self statusRectForBounds: cellFrame];
     174    if (!NSEqualRects(statusRect, NSZeroRect))
     175        [[self attributedStatusWithColor: highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor]] drawInRect: statusRect];
    106176}
    107177
  • trunk/macosx/FileOutlineView.m

    r2383 r2709  
    3232- (void) awakeFromNib
    3333{
    34     NSBrowserCell * browserCell = [[[FileBrowserCell alloc] init] autorelease];
     34    NSBrowserCell * browserCell = [[FileBrowserCell alloc] init];
    3535    [[self tableColumnWithIdentifier: @"Name"] setDataCell: browserCell];
     36    [browserCell release];
    3637   
    3738    [self setAutoresizesOutlineColumn: NO];
Note: See TracChangeset for help on using the changeset viewer.