//  TestManager.m
//
//
//  You may freely copy, distribute, and reuse the code in this example.
//  NeXT disclaims any warranty of any kind, expressed or  implied, as to its
//  fitness for any particular use.

#import "TestManager.h"
#import "EmbeddedGraphic.h"

@implementation TestManager
- init
{
	self = [super init];
	[EmbeddedGraphic class];//hack to insure text knows right rtf directives
	return self;
}
- addGraphic:sender
{
	id theEG;
    int successful;
    static const char *const types[] = {"eps","tiff", NULL};
    
    successful = [[OpenPanel new] runModalForTypes:types];
    if (!successful) return self;
	
	theEG = [[EmbeddedGraphic alloc] init];
	[theEG setFile:[[OpenPanel new] filename]];
	
	[theText replaceSelWithCell:theEG];
    return self;
}

- open:sender
{
    int successful;
    NXRTFDError openResult;
    static const char *const types[] = {"rtfd", NULL};
    
    successful = [[OpenPanel new] runModalForTypes:types];
    if (!successful) return self;
    
    openResult = [theText openRTFDFrom:[[OpenPanel new] filename]];
    if (openResult != NX_RTFDErrorNone) {
	NXRunAlertPanel([NXApp appName],
		"Problems opening the file %s. Error %d.",
		NULL, NULL, NULL,
		[[OpenPanel new] filename], openResult);
	return self;
    }
	
    [[theText window] setTitleAsFilename:[[OpenPanel new] filename]];
	return self;
}
- save:sender
{
    int successful;
    NXRTFDError saveResult;
    
    [[SavePanel new] setRequiredFileType:"rtfd"];
    successful = [[SavePanel new] runModal];
    if (!successful) return self;
    
    saveResult = [theText saveRTFDTo:[[SavePanel new] filename]
    	removeBackup:NO
    	errorHandler:self];
    if (saveResult != NX_RTFDErrorNone) {
	NXRunAlertPanel([NXApp appName],
		"Problems saving the file %s. Error %d.",
		NULL, NULL, NULL,
		[[SavePanel new] filename], saveResult);
	return self;
    }
	
    [[theText window] setTitleAsFilename:[[SavePanel new] filename]];
	
    return self;
}

//Finally, the error handler's method:

- (BOOL)attemptOverwrite:(const char *)filename
{
	    NXRunAlertPanel([NXApp appName], "Permissions problem: unable to save 	file %s", NULL, NULL, NULL, filename);
	    return NO;
}

- awakeFromNib
{
    [theText setSel:0 :0];
    [[theText window] makeKeyAndOrderFront:self];
    return self;
}
@end
