
/* 
	MiscAbbreviatedTextCell.m

   Copyright (C) 1996 Todd Thomas
   Use is governed by the MiscKit license

   This object is included in the MiscKit by permission from the author
   and its use is governed by the MiscKit license, found in the file
   "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
   for a list of all applicable permissions and restrictions.
*/

// RCS identification information
static char *rcsID = "$Id: MiscAbbreviatedTextCell.m,v 1.2 1996/10/07 02:49:09 todd Exp $";
static void __AvoidCompilerWarning(void) {if(!rcsID)__AvoidCompilerWarning();}

#import <AppKit/AppKit.h>

#import "MiscAbbreviatedTextCell.h"

// Module variables (static variables)


@implementation MiscAbbreviatedTextCell

/*"
   MiscAbbreviatedTextCell is a fast hack of a class that'll be
   rewritten to be more flexible later on. It uses NSAttributedStrings
   just in case the cell is making use of them.  
"*/


//-------------------------------------------------------------------
//	Drawing 
//-------------------------------------------------------------------

- (void) drawInteriorWithFrame:(NSRect)frame inView:(NSView*)controlView
/*"
   Shortens our string until it'll fit into the width given by frame.
   We append a ".." to the end like the Workspace shelf does. This
   method will be rewritten sometime in the future.
"*/
{
    NSFont* ourFont = [self font];
    float maxWidth = NSWidth (frame) - 6.0;
    float stringWidth = [ourFont widthOfString:[self stringValue]];
    BOOL useCurrentString = YES;
    NSString* originalString = [self stringValue];
    NSAttributedString* originalAttributedString = [self attributedStringValue];
    
    if (originalString != nil && ![originalString isEqualToString:@""] && maxWidth < stringWidth) {	
        NSMutableAttributedString* abbrevString = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedStringValue]];
        float currentWidth = [ourFont widthOfString:[abbrevString string]];
        NSRange endOfStringRange;

        // Will have to create an -appendString: method for NSMutableAttributedStrings.
        endOfStringRange.location = [abbrevString length];
        endOfStringRange.length = 0;
        [abbrevString replaceCharactersInRange:endOfStringRange withString:@".."];

        while (maxWidth > 0 && currentWidth > 0 && currentWidth > maxWidth) {
            NSRange charRemovedRange;
            charRemovedRange.location = [abbrevString length] - 3;
            charRemovedRange.length = 1;

            if (charRemovedRange.location > 0 &&
                charRemovedRange.location < [abbrevString length]) {
                [abbrevString deleteCharactersInRange:charRemovedRange];
                currentWidth = [ourFont widthOfString:[abbrevString string]];
            }
            else {
                currentWidth = 0;
            }
        }

        useCurrentString = NO;
        [self setAttributedStringValue:abbrevString];
    }

    [super drawInteriorWithFrame:frame inView:controlView];

    if (!useCurrentString) {
        [self setAttributedStringValue:originalAttributedString];
    }
}
    
@end

