/*  
** Copyright (c) 1995 Friday Software & Consulting, Inc.  All Rights Reserved.
**
** Author: <bbum@friday.com>
*/

/*  This object is included in the MiscKit by permission from the author
**  and its use is governed by the MiscKit license, found in the file
**  "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
**  for a list of all applicable permissions and restrictions.
*/

#import <appkit/appkit.h>

#import "FSCInfoPanel.h"

#import "FooFaraw.h"
    #import "ScaledView.h"

@interface FSCInfoPanel (PrivateAPI)
- startTE;
- stopTE;
@end

@implementation FSCInfoPanel
+ sharedInstance 
{
    static id sharedInstance = nil;
    
    if(!sharedInstance) {
    	sharedInstance = [self allocFromZone:[self zone]];
	[sharedInstance init];
    }
    
    return sharedInstance;
}

- orderFront:sender
{
    if(!panel)
    	[NXApp loadNibSection:"FSCInfoPanel.nib" owner:self];

	[panel center];
    [panel makeKeyAndOrderFront:sender];
    [self startTE];
    return self;
}

- doIE
{
  NXEvent dummyEvent, *evnt;

  [scaledView lockFocus];
  do {
    [scaledView oneStep];
    [scaledView oneStep]; // do two steps

    evnt=[NXApp peekNextEvent:NX_ALLEVENTS into:&dummyEvent waitFor:0
	  threshold:NX_BASETHRESHOLD];
  } while(infoRun && !evnt);
    
  [scaledView unlockFocus];
  return self;
}  

void itehandler(DPSTimedEntry te, double time, void *tobj)
{ [(id)tobj doIE]; }

- startTE
{
    if(!infoRun){
	ientry=DPSAddTimedEntry(0.02, &itehandler, self, NX_BASETHRESHOLD);
	infoRun=YES;
    }
    return self;
}

- stopTE
{
    if(infoRun){
	infoRun=NO;
	DPSRemoveTimedEntry(ientry);
    }
    return self;
}

- windowWillClose:sender
{
    if (sender==panel) {
	[self stopTE];
    }
    return self;
}

- windowDidBecomeKey:sender
{
    if (sender==panel) {
	[self startTE];
    }
    return self;
}

- windowDidResignKey:sender
{
    if (sender==panel) {
	[self stopTE];
    }
    return self;
}

@end
