
//
//  This is just a sample pane for Pastry.  This source is in the public
//  domain.  Someone with knowledge of the SoundKit can write a much nicer
//  sound inspector pane.
//


#import "SoundSamplePane.h"

static NXAtom	types[2] = {NULL};

@implementation SoundSamplePane

- _loadNib
{
    char	buf[MAXPATHLEN+1];

    // Load a nib file from the bundle

    [[NXBundle bundleForClass: [self class]]
		      getPath: buf
		  forResource: "Sound"
		       ofType: "nib"];

    [NXApp loadNibFile: buf
		 owner: self
	     withNames: NO
	      fromZone: [self zone]];

    view = [window setContentView:[[View alloc] init]];
    [window free];
    window = nil;
    return self;
}


+ initialize
{
    if (!types[0]) {
	types[0] = NXSoundPboardType;
	types[1] = NULL;
    }
    return self;
}


+ (const NXAtom *)types
{
    return types;
}


- free
{
    [sound free];
    [view free];
    return [super free];
}


- (View *)view
{
    if (!view)
	[self _loadNib];
    return view;
}


- updateFromStream:(NXStream *)stream withType:(NXAtom)type
{
    // This seems like kind of a hacked way to get sound from a stream
    // that was written from the pasteboard data, but I don't know
    // enough about the sound kit to init a sound straight from the
    // stream.  I just use Sound's -initFromPasteboard: method.

    Pasteboard *p = [Pasteboard newUnique];

    if (!view)
        [self _loadNib];

    [p declareTypes:types num:1 owner:self];
    [p writeType:type fromStream:stream];	//! We should handle exceptions

    [sound free];
    sound = [[Sound alloc] initFromPasteboard:p];
    [p freeGlobally];

    [sound setDelegate:self];
    [durationField setDoubleValue:[sound duration]];
    [statusField setStringValue:"not playing"];
    return self;
}



- play:sender
{
    if (!playButton)
	playButton = sender;

    if (sound) {
	[playButton setEnabled:NO];
	[statusField setStringValue:"playing"];
	[sound play:self];
    }
    return self;
}


- didPlay:sender
{
    [playButton setEnabled:YES];
    [statusField setStringValue:"not playing"];
    return self;
}


@end
