//
//  NSString-NJRExtensions.m
//  HostLauncher
//
//  Created by nicholas on Wed Oct 31 2001.
//  Copyright (c) 2001 Nicholas Riley. All rights reserved.
//

// This is a reduced version of the version I use in HostLauncher, for use in F-Script Anywhere.
// It removes methods that rely on OmniFoundation.
// Don't forget to apply changes forward!

#import <Carbon/Carbon.h>
#import "NSString-NJRExtensions.h"

#ifndef NSAppKitVersionNumber10_2
#define NSAppKitVersionNumber10_2 663
#endif

@implementation NSString (NJRExtensions)

- (NSAttributedString *)asAttributedStringTruncatedToWidth:(float)width {
    NSMutableString *text = [self mutableCopy];
    NSAttributedString *s = [NSAttributedString alloc];
    NSMutableParagraphStyle *ps = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_2) {
        [ps setLineBreakMode: NSLineBreakByTruncatingMiddle];
    } else {
        // Mac OS X versions earlier than 10.3 don't implement NSLineBreakByTruncatingMiddle; fake it
        OSStatus err;
        err = TruncateThemeText((CFMutableStringRef) text, kThemeViewsFont,
                                kThemeStateActive, width, truncMiddle, NULL);
        if (err != noErr) FSALog(@"-[NSString asAttributedStringTruncatedToWidth:]: TruncateThemeText failed with error %d", err);
        [ps setLineBreakMode: NSLineBreakByClipping];
    }
    s = [s initWithString: text attributes:
           [NSDictionary dictionaryWithObjectsAndKeys: ps, NSParagraphStyleAttributeName, nil]];
    [text release];
    [ps release];
    return [s autorelease];
}

@end
