#import "Controller.h"

/* Controller
 * by Patrick J. Flynn (flynn@eecs.wsu.edu)
 * This class is part of the Movie application, version 2.0.
 * 1/15/92
 */

@implementation Controller

- init
{
  static NXDefaultsVector MovieDefaults = {
         {"DefaultDepth","0"},
         {NULL}
         };
  NXRegisterDefaults("Movie",MovieDefaults);
  self = [super init];
  projectionists = [[List alloc] init];
  prefsPanel=nil;
  return self;
}

- (MovieView *)currentMovieView
{
  int i;
  Window *w=[NXApp mainWindow];
  
  if ((!w)||(![projectionists count])) return nil;
  for(i=0;i<[projectionists count];i++) {
    Projectionist *p=[projectionists objectAt:i];
    if ((w == [p window])||(w==[p panel])) return [p movieView];
    };
  return nil;
}

- open:sender
{
  Projectionist *newProjectionist;
  newProjectionist = [[Projectionist alloc] init];
  if (![newProjectionist open:sender])
    [newProjectionist free];
  else
    [projectionists addObject:newProjectionist];

  return self;
}

- play:sender
{
  Projectionist *newProjectionist;
  newProjectionist = [[Projectionist alloc] init];
  if (![newProjectionist play:sender])
    [newProjectionist free];
  else
    [projectionists addObject:newProjectionist];

  return self;
}

#define DOIT(msg) { [[self currentMovieView] msg:self]; return self; }

- save:sender DOIT(save)
- copy:sender DOIT(copy)
- expand2x:sender DOIT(expand2x)
- reduce50pct:sender DOIT(reduce50pct)
- restore:sender DOIT(restore)
- revStep:sender DOIT(revStep)
- rev:sender DOIT(rev)
- stop:sender DOIT(stop)
- fwd:sender DOIT(fwd)
- fwdStep:sender DOIT(fwdStep)

- print:sender DOIT(printPSCode)

- setVersion:anObject
{
  [(version = anObject) setStringValue:VERSION];
  return self;
}

// preferences handler (Yappish style)

- prefs:sender
{
  if (!prefsPanel) {
    [NXApp loadNibSection:"Prefs.nib" owner:self withNames:NO];
    [self updatePrefs:sender];
    };
  [prefsPanel makeKeyAndOrderFront:sender];
  return self;
}

- updatePrefs:sender
{
  int def;
  const char *x = NXGetDefaultValue("Movie","DefaultDepth");
  if (x) def=atoi(x);
  else def=0;
  switch(def) {
    default:
    case 0: [defDepthMatrix selectCellAt:0:0]; break;
    case 2: [defDepthMatrix selectCellAt:1:0]; break;
    case 8: [defDepthMatrix selectCellAt:2:0]; break;
    case 12: [defDepthMatrix selectCellAt:3:0]; break;
    case 24: [defDepthMatrix selectCellAt:4:0]; break;
    };
  return self;
}

- okPrefs:sender
{
  int d=[defDepthMatrix selectedRow];
  char buf[5];
  switch(d) {
    default:
    case 0: sprintf(buf,"0"); break;
    case 1: sprintf(buf,"2"); break;
    case 2: sprintf(buf,"8"); break;
    case 3: sprintf(buf,"12"); break;
    case 4: sprintf(buf,"24"); break;
    };
  NXWriteDefault("Movie","DefaultDepth",buf);
  [[sender window] orderOut:sender];
  return self;
}

- cancelPrefs:sender
{
  [[sender window] orderOut:sender];
  return self;
}

// Application's delegate methods

- (BOOL)appAcceptsAnotherFile:sender
{
  return YES;
}

- (int)app:sender openFile:(const char *)filename type:(const char *)type
{
  Projectionist *newProjectionist;
  newProjectionist = [[Projectionist alloc] init];
  if (![[newProjectionist movieView] openFile:(char *)filename]) {
    [newProjectionist free];
    return NO;
    }
  else {
    [projectionists addObject:newProjectionist];
    return YES;
    }
}

- appDidInit:sender
{
  return self;
}

// Window's delegate methods
- windowDidBecomeMain:sender
{
  return self;
}
 
@end
