File:  [mozdev] / chimera / TruncatedTabViewItem.m
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Sun Mar 17 11:07:14 2002 UTC (15 years, 9 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
This represents my final planned checkin for version 0.2.0 of this project.  BrowserWindowController was modified to allow for a dynamic 'Sidebar' toolbar icon, and the requisite graphics have also been added.  Credits were modified to include a few individual contributors.  A TruncatedTabView object is being created, but is not ready for 0.2.0.  I am adding it to CVS anyway to alleviate the potential for error, but it is not linked.  The sidebar tab icons have been updated, along with the Stop and Reload toolbar icons.

//
//  TruncatedTabViewItem.m
//  Chimera
//
//  Created by Matt L.  Judy on Wed Mar 13 2002.
//  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//

#import "TruncatedTabViewItem.h"

@interface TruncatedTabViewItem(Private)
- (NSString *)labelForCurrentTabWidth;
- (NSString *)prefixOfString:(NSString *)longString withFont:(NSFont *)font thatWillFitIntoWidth:(float)fitWidth;
@end

@implementation TruncatedTabViewItem

-(id)initWithIdentifier:(id)identifier
{
    if ( (self = [super initWithIdentifier:identifier]) ) {
        [self setMaxTabWidth:250.0];
        [self setHasMaxTabWidth:YES];
        [[self tabView] setAllowsTruncatedLabels:YES];
    }
    return self;
}

- (void)setLabel:(NSString *)label
{
    [_longLabel autorelease];
    _longLabel = [label copy];
    [super setLabel:[self labelForCurrentTabWidth]];
}

- (NSString *)label
{
    return _longLabel;
}


- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)labelRect
{
    NSLog(@"draw");
    [super drawLabel:shouldTruncateLabel inRect:labelRect];
}


 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
{
    NSSize foo = [super sizeOfLabel:shouldTruncateLabel];
    _rawTabWidth = foo.width;
    return foo;
}

/*
 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
{
    NSSize regularSize = [super sizeOfLabel:shouldTruncateLabel];
    NSSize drawSize;

    if ( [self hasMaxTabWidth] ) {
        if ( regularSize.width > [self maxTabWidth] ) {
            drawSize = NSMakeSize([self maxTabWidth], regularSize.height);
        } else {
            drawSize = regularSize;
        }
    }
    return drawSize;
}
*/

-(float)maxTabWidth
{
    return _maxTabWidth;
}

-(void)setMaxTabWidth:(float)newWidth
{
    _maxTabWidth = newWidth;
}

-(BOOL)hasMaxTabWidth
{
    return _hasMaxTabWidth;
}

-(void)setHasMaxTabWidth:(BOOL)newSetting
{
    _hasMaxTabWidth = newSetting;
}

- (NSString *)labelForCurrentTabWidth
{
    float     availableWidth   = _rawTabWidth;
    float     elipsisWidth     = 0.0;
    float     labelTextWidth   = 0.0;
    NSString *labelTextPart    = nil;
    NSString *labelElipsisPart = @"...";
    NSCell   *testCell         = [[[NSCell alloc] initTextCell:labelElipsisPart] autorelease];

    NSLog(@"ava: %f", availableWidth);
    NSLog(@"max: %f", [self maxTabWidth]);
    
    if ( availableWidth > [self maxTabWidth] ) {
        NSLog(@"Adjusting.");
        availableWidth = [self maxTabWidth];
    }
    
    [testCell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
    elipsisWidth = [testCell cellSize].width;
    NSLog(@"eli: %f", elipsisWidth);

    labelTextWidth = availableWidth - elipsisWidth;
    labelTextPart  = [self prefixOfString:_longLabel
                                 withFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]
                     thatWillFitIntoWidth:labelTextWidth];

    return [labelTextPart stringByAppendingString:labelElipsisPart];
}

- (NSString *)prefixOfString:(NSString *)longString withFont:(NSFont *)font thatWillFitIntoWidth:(float)fitWidth
{
    int           longIndex  = [longString length] - 1;
    NSString     *decString	 = nil;
    NSRange	      decRange;
    NSSize	      testSize;
    NSDictionary *attributes = [[[NSDictionary alloc] initWithObjectsAndKeys:
                                       font, NSFontAttributeName, nil] autorelease];
    decString = [longString substringFromIndex:0];

    while ( longIndex > 0 ) {
        testSize = [decString sizeWithAttributes:attributes];
        if ( testSize.width <= fitWidth ) {
            break;
        }
        decRange = [longString rangeOfComposedCharacterSequenceAtIndex:longIndex];
        longIndex = decRange.location - decRange.length;
        if ( longIndex == 0 ) {
            break;
        }
        decString = [longString substringToIndex:longIndex];
    }
    return decString;
}

/*
 - (NSRange)_rangeOfPrefixFittingWidth:(float)space withAttributes:(NSDictionary *)dict {
    NSString *tmpstr;
    NSRange rg;
    NSSize sz;
    int len = [self length];
    int ix = len - 1;

    tmpstr = [self substringFromIndex:0];
    while (ix > 0) {
        sz = [tmpstr sizeWithAttributes:dict];
        if (sz.width <= space) break;
        rg = [self rangeOfComposedCharacterSequenceAtIndex:ix];
        ix = rg.location - rg.length;
        if (ix == 0) break;
        tmpstr = [self substringToIndex:ix];
    }
    return NSMakeRange(0, ix);
}
*/

@end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>