/*  FocusStack - keeps track of focus state and nesting (for View's lockFocus)

    Copyright (C) 1995, Adam Fedor
    
   This file is part of an Objective-C class library for a window system

    FocusStack.m,v 1.1 1995/04/17 22:44:48 fedor Exp
*/

#import "appkit/FocusStack.h"
#import <objc/List.h>

@interface FocusState : Object
{
  View *view;
  int   nesting;
}

- initWithView: (View *)aView;
- (View *)focusView;
- incNesting;
- decNesting;
- (int)nesting;

@end

@implementation FocusState

- initWithView: (View *)aView
{
  [super init];
  view = aView;
  return self;
}

- (View *)focusView
{
  return view;
}

- incNesting
{
  nesting++;
  return self;
}

- decNesting
{
  nesting--;
  return self;
}

- (int) nesting
{
  return nesting;
}

@end

@implementation FocusStack

- lockView: (View *)view
{
  if ([[self lastObject] focusView] == view)
      [[self lastObject] incNesting];
  else
    {
      FocusState *state = [[FocusState alloc] initWithView:view];
      [self addObject:state];
    }
  return self;
}

- relockView: (View *)view
{
  return [self lockView:view];
}

- (View *)unlockView
{
  FocusState *state = [self lastObject];
  View *view = nil;
  
  if ([state nesting])
      [state decNesting];
  else
    {
      view = [state focusView];
      [[self removeLastObject] free];
    }
  return view;
}

- (View *) currentView
{
  return [[self lastObject] focusView];
}

@end