// Copyright 1997-1999 Omni Development, Inc.  All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.

#import "NSText-OAExtensions.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <OmniBase/OmniBase.h>
#import <OmniFoundation/OmniFoundation.h>

RCS_ID("$Header: /Network/Developer/Source/CVS/OmniGroup/OmniAppKit/OpenStepExtensions.subproj/NSText-OAExtensions.m,v 1.7 1999/03/30 21:26:02 kc Exp $")

@implementation NSText (OAExtensions)

- (IBAction)jumpToSelection:(id)sender;
{
    [self scrollRangeToVisible:[self selectedRange]];
}

- (unsigned int)textLength;
{
    return [[self string] length];
}

- (void)appendTextString:(NSString *)string;
{
    NSRange endRange;

    if (!string || [string length] == 0)
	return;
    endRange = NSMakeRange([self textLength], 0);
    [self replaceCharactersInRange:endRange withString:string];
}

- (void)appendRTFData:(NSData *)data;
{
    NSRange endRange;

    if (!data || [data length] == 0)
	return;
    endRange = NSMakeRange([[self string] length], 0);
    [self replaceCharactersInRange:endRange withRTF:data];
}

- (void)appendRTFDData:(NSData *)data;
{
    NSRange endRange;

    if (!data || [data length] == 0)
	return;
    endRange = NSMakeRange([[self string] length], 0);
    [self replaceCharactersInRange:endRange withRTFD:data];
}

- (void)appendRTFString:(NSString *)string;
{
    NSData *rtfData;
    NSRange endRange;

    if (!string || [string length] == 0)
	return;
    rtfData = [string dataUsingEncoding:[NSString defaultCStringEncoding]
	       allowLossyConversion:YES];
    endRange = NSMakeRange([[self string] length], 0);
    [self replaceCharactersInRange:endRange withRTF:rtfData];
}

- (NSData *)textData;
{
    return [[self string] dataUsingEncoding:[NSString defaultCStringEncoding]
                       allowLossyConversion:YES];
}

- (NSData *)rtfData;
{
    return [self RTFFromRange:NSMakeRange(0, [self textLength])];
}

- (NSData *)rtfdData;
{
    return [self RTFDFromRange:NSMakeRange(0, [self textLength])];
}

- (void)setRTFData:(NSData *)rtfData;
{
    [self replaceCharactersInRange:NSMakeRange(0, [self textLength])
                           withRTF:rtfData];
}

- (void)setRTFDData:(NSData *)rtfdData;
{
    [self replaceCharactersInRange:NSMakeRange(0, [self textLength])
                          withRTFD:rtfdData];
}

- (void)setRTFString:(NSString *)string;
{
    NSData                     *rtfData;
    NSRange                     fullRange;

    rtfData = [string dataUsingEncoding:[NSString defaultCStringEncoding]
	       allowLossyConversion:YES];
    fullRange = NSMakeRange(0, [self textLength]);
    [self replaceCharactersInRange:fullRange withRTF:rtfData];
}

- (void)setTextFromString:(NSString *)aString;
{
    [self setString:aString ? aString : @""];
}

- (NSString *)substringWithRange:(NSRange)aRange;
{
    NS_DURING {
	NSString *substring;

	substring = [[self string] substringWithRange: aRange];
	NS_VALUERETURN(substring, NSString *);
    } NS_HANDLER {
	return @"";
    } NS_ENDHANDLER;
}


// OAFindControllerTarget

- (BOOL)findString:(NSString *)textPattern ignoreCase:(BOOL)ignoreCase backwards:(BOOL)backwards wrap:(BOOL)wrap;
{
    if ([self findString:textPattern ignoreCase:ignoreCase backwards:backwards ignoreSelection:NO])
        return YES;

    if (!wrap)
        return NO;

    // Try again, ignoring the selection and searching from one end or the other.
    return [self findString:textPattern ignoreCase:ignoreCase backwards:backwards ignoreSelection:YES];
}


// OASearchableContent protocol

- (BOOL)findString:(NSString *)textPattern ignoreCase:(BOOL)ignoreCase backwards:(BOOL)backwards ignoreSelection:(BOOL)ignoreSelection;
{
    NSString *string;
    unsigned int stringLength;
    NSRange selectedRange, range;

    string = [self string];
    if (!string || (stringLength = [string length]) == 0)
        return NO;

    if (ignoreSelection)
        selectedRange = NSMakeRange((backwards ? stringLength : 0), 0);
    else
        selectedRange = [self selectedRange];
    
    range = [string findString:textPattern selectedRange:selectedRange options:(backwards ? NSBackwardsSearch : 0) | (ignoreCase ? NSCaseInsensitiveSearch : 0) wrap:NO];
    if (range.length) {
        [self setSelectedRange:range];
        [self scrollRangeToVisible:range];
        [[self window] makeFirstResponder:self];
        return YES;
    }
    return NO;
}

@end

@implementation NSTextView (OAExtensions)

- (unsigned int)textLength;
{
    return [[self textStorage] length];
}

@end
