//
//  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"

@implementation NSString (NJRExtensions)

- (NSAttributedString *)asAttributedStringTruncatedToWidth:(float)width {
    NSMutableString *text = [self mutableCopy];
    NSAttributedString *s = [NSAttributedString alloc];
    NSMutableParagraphStyle *ps = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    OSStatus err;
    /*
    NSString *fs;
    Str255 font;
    SInt16 size; // label font is 10, pushbutton font 13, views font is 12!
    err = GetThemeFont(kThemeViewsFont, smUnicodeScript, font, &size, NULL);
    fs = CFStringCreateWithPascalString(kCFAllocatorDefault, font, CFStringGetSystemEncoding());
    NSLog(@"Theme font: %@ %d", fs, size);
    */
    err = TruncateThemeText((CFMutableStringRef) text, kThemeViewsFont,
                            kThemeStateActive, width, truncMiddle, NULL);
    /* // There's a workaround for a 10.1 bug in Connections' awakeFromNib: the initial
       // column size is wrong, until you resize the window.  Here's how to demonstrate the bug:
    {   static float lastWidth = 0; // XXX debug
        if (lastWidth != width) {
            lastWidth = width;
            NSLog(@"Got width %f", width);
        }
    }
    */
    if (err != noErr) FSALog(@"-[NSString asAttributedStringTruncatedToWidth:]: TruncateThemeText failed with error %d", err);
    // XXX Should be able to skip the above by using NSLineBreakByTruncatingMiddle,
    // XXX but OS X 10.1 doesn't implement it yet.
    [ps setLineBreakMode: NSLineBreakByClipping];
    s = [s initWithString: text attributes:
           [NSDictionary dictionaryWithObjectsAndKeys: ps, NSParagraphStyleAttributeName, nil]];
    [text release];
    [ps release];
    return [s autorelease];
}

@end
