/*
        InputView.m

	SlideShowController : This file is part of SlideShow

	Copyright (C) 2004 Fabien VALLON
	Additional copyrights here

	Authors: Fabien VALLON <fabien.vallon@fr.alcove.com>
	Date:	24 March 2004

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License as
	published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public
	License along with this program; if not, write to:

		Free Software Foundation, Inc.
		59 Temple Place - Suite 330
		Boston, MA  02111-1307, USA
*/


// lot of code borrow From  KeyboardInput-test.m: Keyboard Input demo/test
//  Copyright (C) 1999, 2000 Free Software Foundation, Inc.
//  Author:  Nicola Pero <n.pero@mi.flashnet.it>
//   Date: 1999, 2000


#include "InputView.h"

#include <AppKit/NSButton.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSInputManager.h>
#include <AppKit/NSTextField.h>
#include <AppKit/NSWindow.h>

@implementation InputView

-(id) initWithFrame:(NSRect) frame
{
  if ( ! ( self = [super initWithFrame:NSMakeRect(0,0,250,100)] ) )
    return nil;
  
  //EntryField
  {
    entryField = [[NSTextField alloc] initWithFrame:NSMakeRect(6,50,250,22)];
    [entryField setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
    [entryField setEditable: NO];
    [entryField setBezeled: NO];
    [entryField setBackgroundColor: [NSColor controlColor]];
    [entryField setStringValue: @"Capture"];
    [entryField autorelease];

    [self addSubview: entryField];
  }

 
  enabled = YES;
  return self;
}

- (BOOL) acceptsFirstResponder
{
  return enabled;
}

- (BOOL) resignFirstResponder
{
  return (!enabled);
}

 -(BOOL) performKeyEquivalent: (NSEvent *)theEvent
 {
    if (enabled)
      {
        [self keyDown: theEvent];
       return YES;
      }
    else
     return [super performKeyEquivalent: theEvent];
 }

-(void) keyDown: (NSEvent *)theEvent
{
  NSString *entryValue = [[NSString alloc] init];

  if (enabled)
    {
      NSString *characters = [[NSString alloc] init];
      characters = [theEvent charactersIgnoringModifiers];
      if ([characters length] == 1) 
	{
	  unichar c;
	  c = [characters characterAtIndex: 0];
	  entryValue = [NSInputManager describeKeyStroke: c
				       withModifiers: [theEvent modifierFlags]];
	}
      else 
	entryValue = @"";
	
      
      [entryField setStringValue: entryValue];
    }
}


-(void) setEnabled: (BOOL) flag
{
  enabled = flag;
}



//Accessor
-(NSTextField*) entryField
{
  NSLog(@"===============entryField %@",entryField);
  return entryField;
}


@end
