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 (17 years, 1 month 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.

    1: //
    2: //  TruncatedTabViewItem.m
    3: //  Chimera
    4: //
    5: //  Created by Matt L.  Judy on Wed Mar 13 2002.
    6: //  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
    7: //
    8: 
    9: #import "TruncatedTabViewItem.h"
   10: 
   11: @interface TruncatedTabViewItem(Private)
   12: - (NSString *)labelForCurrentTabWidth;
   13: - (NSString *)prefixOfString:(NSString *)longString withFont:(NSFont *)font thatWillFitIntoWidth:(float)fitWidth;
   14: @end
   15: 
   16: @implementation TruncatedTabViewItem
   17: 
   18: -(id)initWithIdentifier:(id)identifier
   19: {
   20:     if ( (self = [super initWithIdentifier:identifier]) ) {
   21:         [self setMaxTabWidth:250.0];
   22:         [self setHasMaxTabWidth:YES];
   23:         [[self tabView] setAllowsTruncatedLabels:YES];
   24:     }
   25:     return self;
   26: }
   27: 
   28: - (void)setLabel:(NSString *)label
   29: {
   30:     [_longLabel autorelease];
   31:     _longLabel = [label copy];
   32:     [super setLabel:[self labelForCurrentTabWidth]];
   33: }
   34: 
   35: - (NSString *)label
   36: {
   37:     return _longLabel;
   38: }
   39: 
   40: 
   41: - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)labelRect
   42: {
   43:     NSLog(@"draw");
   44:     [super drawLabel:shouldTruncateLabel inRect:labelRect];
   45: }
   46: 
   47: 
   48:  - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
   49: {
   50:     NSSize foo = [super sizeOfLabel:shouldTruncateLabel];
   51:     _rawTabWidth = foo.width;
   52:     return foo;
   53: }
   54: 
   55: /*
   56:  - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
   57: {
   58:     NSSize regularSize = [super sizeOfLabel:shouldTruncateLabel];
   59:     NSSize drawSize;
   60: 
   61:     if ( [self hasMaxTabWidth] ) {
   62:         if ( regularSize.width > [self maxTabWidth] ) {
   63:             drawSize = NSMakeSize([self maxTabWidth], regularSize.height);
   64:         } else {
   65:             drawSize = regularSize;
   66:         }
   67:     }
   68:     return drawSize;
   69: }
   70: */
   71: 
   72: -(float)maxTabWidth
   73: {
   74:     return _maxTabWidth;
   75: }
   76: 
   77: -(void)setMaxTabWidth:(float)newWidth
   78: {
   79:     _maxTabWidth = newWidth;
   80: }
   81: 
   82: -(BOOL)hasMaxTabWidth
   83: {
   84:     return _hasMaxTabWidth;
   85: }
   86: 
   87: -(void)setHasMaxTabWidth:(BOOL)newSetting
   88: {
   89:     _hasMaxTabWidth = newSetting;
   90: }
   91: 
   92: - (NSString *)labelForCurrentTabWidth
   93: {
   94:     float     availableWidth   = _rawTabWidth;
   95:     float     elipsisWidth     = 0.0;
   96:     float     labelTextWidth   = 0.0;
   97:     NSString *labelTextPart    = nil;
   98:     NSString *labelElipsisPart = @"...";
   99:     NSCell   *testCell         = [[[NSCell alloc] initTextCell:labelElipsisPart] autorelease];
  100: 
  101:     NSLog(@"ava: %f", availableWidth);
  102:     NSLog(@"max: %f", [self maxTabWidth]);
  103:     
  104:     if ( availableWidth > [self maxTabWidth] ) {
  105:         NSLog(@"Adjusting.");
  106:         availableWidth = [self maxTabWidth];
  107:     }
  108:     
  109:     [testCell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
  110:     elipsisWidth = [testCell cellSize].width;
  111:     NSLog(@"eli: %f", elipsisWidth);
  112: 
  113:     labelTextWidth = availableWidth - elipsisWidth;
  114:     labelTextPart  = [self prefixOfString:_longLabel
  115:                                  withFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]
  116:                      thatWillFitIntoWidth:labelTextWidth];
  117: 
  118:     return [labelTextPart stringByAppendingString:labelElipsisPart];
  119: }
  120: 
  121: - (NSString *)prefixOfString:(NSString *)longString withFont:(NSFont *)font thatWillFitIntoWidth:(float)fitWidth
  122: {
  123:     int           longIndex  = [longString length] - 1;
  124:     NSString     *decString	 = nil;
  125:     NSRange	      decRange;
  126:     NSSize	      testSize;
  127:     NSDictionary *attributes = [[[NSDictionary alloc] initWithObjectsAndKeys:
  128:                                        font, NSFontAttributeName, nil] autorelease];
  129:     decString = [longString substringFromIndex:0];
  130: 
  131:     while ( longIndex > 0 ) {
  132:         testSize = [decString sizeWithAttributes:attributes];
  133:         if ( testSize.width <= fitWidth ) {
  134:             break;
  135:         }
  136:         decRange = [longString rangeOfComposedCharacterSequenceAtIndex:longIndex];
  137:         longIndex = decRange.location - decRange.length;
  138:         if ( longIndex == 0 ) {
  139:             break;
  140:         }
  141:         decString = [longString substringToIndex:longIndex];
  142:     }
  143:     return decString;
  144: }
  145: 
  146: /*
  147:  - (NSRange)_rangeOfPrefixFittingWidth:(float)space withAttributes:(NSDictionary *)dict {
  148:     NSString *tmpstr;
  149:     NSRange rg;
  150:     NSSize sz;
  151:     int len = [self length];
  152:     int ix = len - 1;
  153: 
  154:     tmpstr = [self substringFromIndex:0];
  155:     while (ix > 0) {
  156:         sz = [tmpstr sizeWithAttributes:dict];
  157:         if (sz.width <= space) break;
  158:         rg = [self rangeOfComposedCharacterSequenceAtIndex:ix];
  159:         ix = rg.location - rg.length;
  160:         if (ix == 0) break;
  161:         tmpstr = [self substringToIndex:ix];
  162:     }
  163:     return NSMakeRange(0, ix);
  164: }
  165: */
  166: 
  167: @end

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