/* Implementation of Button class
 *
 * Copyright (C)  1993, 1994, 1995  The Board of Trustees of  
 * The Leland Stanford Junior University.  All Rights Reserved.
 *
 * Authors: Scott Francis, Paul Kunz, Tom Pavel, Imran Qureshi, and Libing Wang
 *	    Mike Kienenberger (Alaska)
 *
 * This file is part of an Objective-C class library for a window system
 *
 * Button.m,v 1.44 1995/12/13 22:32:52 fedor Exp
 */

#include "Button.h"

/* Required for implementation: */
#include "ActionCell.h"
#include "ButtonCell.h"
#include <stdlib.h>		/* for free() */
#include "stdmacros.h"

#include "PopUpList.h"		/* since Button and PopUpList are partners */

@interface View(WidgetSet)
- _init;
@end

@implementation Button:Control


- init
{
    NXRect  rect = {{0, 0}, {50, 50}};

    [self initFrame:&rect];
    return self;
}

- initFrame:(const NXRect *)frameRect
{
    if (!cell) {
	cell = [[ButtonCell alloc] init];
	[self setTitle:"Button"];
    }
    [super initFrame:frameRect];
    instancename = "Button";
    return self;
}

- initFrame:(const NXRect *)frameRect title:(const char *)aString 
        tag:(int)anInt target:anObject action:(SEL)aSelector 
        key:(unsigned short)charCode enabled:(BOOL)flag
{
  return [self notImplemented:_cmd];
}

- initFrame:(const NXRect *)frameRect icon:(const char *)aString 
        tag:(int)anInt target:anObject action:(SEL)aSelector 
        key:(unsigned short)charCode enabled:(BOOL)flag
{
  return [self notImplemented:_cmd];
}

- (const char *)title
{
  return [cell title];
}

- setTitle:(const char*)aString
{
    if ( !popup ) {
	[cell setStringValue:aString];
    }
    return self;
}

- setTitleNoCopy:(const char *)aString
{
  return [self notImplemented:_cmd];
}

- (const char *)altTitle
{
  return [cell altTitle];
}

- setAltTitle:(const char *)aString
{
  return [self notImplemented:_cmd];
}

- (const char *)icon
{
    return [cell icon];
}

- setIcon:(const char *)iconName
{
    [cell setIcon:iconName];
    return self;
}


- (const char *)altIcon
{
    return [cell altIcon];
}

- setAltIcon:(const char *)iconName
{
    [cell setAltIcon:iconName];
    return self;
}

- image
{
    return [cell image];
}

- setImage:image
{
    [cell setImage: image];
    return self;
}

- altImage
{
    return [cell altImage];
}

- setAltImage:image
{
    [cell setAltImage: image];
    return self;
}

- (int)iconPosition
{
    return [cell iconPosition];
}

- setIconPosition:(int)aPosition
{
    [cell setIconPosition: aPosition];
    return self;
}

- setIcon:(const char *)iconName position:(int)aPosition
{
  return [self notImplemented:_cmd];
}

- setType:(int)aType
{
  return [self notImplemented:_cmd];
}

- (int) state
{
    return [cell state];
}

- setState:(int)value
{
    return [cell setState:value];
}

- (BOOL)isBordered
{
  return [cell isBordered];
}

- setBordered:(BOOL)flag
{
  [cell setBordered: flag];
  return self;
}

- (BOOL)isTransparent
{
  return [cell isTransparent];
}

- setTransparent:(BOOL)flag
{
  [cell setTransparent: flag];
  return self;
}

- setPeriodicDelay:(float)delay andInterval:(float)interval
{
  return [self notImplemented:_cmd];
}

- getPeriodicDelay:(float *)delay andInterval:(float *)interval
{
  return [self notImplemented:_cmd];
}

- (unsigned short)keyEquivalent
{
  [self notImplemented:_cmd];
  return 0;
}

- setKeyEquivalent:(unsigned short)charCode
{
  return [self notImplemented:_cmd];
}

- sound
{
  return [self notImplemented:_cmd];
}

- setSound:soundObj
{
  return [self notImplemented:_cmd];
}

- display
{
  /* FIXME: Implement */
  return self;
}

- highlight:(BOOL)flag
{
  return [self notImplemented:_cmd];
}

- (BOOL)performKeyEquivalent:(NXEvent *)theEvent
{
  [self notImplemented:_cmd];
  return 0;
}

- performClick:sender
{
    [cell sendAction];
    return self;
}

- (BOOL)acceptsFirstMouse
{
  return YES;
}

+ new
{
    return [[Button alloc] init];
}

+ newFrame:(const NXRect *)frameRect
{
    return [[Button alloc] initFrame:frameRect];
}

// + newFrame:(const NXRect *)frameRect title:(const char *)aString 
//        tag:(int)anInt target:anObject action:(SEL)aSelector 
//        key:(unsigned short)charCode enabled:(BOOL)flag
// + newFrame:(const NXRect *)frameRect icon:(const char *)aString 
//        tag:(int)anInt target:anObject action:(SEL)aSelector 
//        key:(unsigned short)charCode enabled:(BOOL)flag

/* The following methods are not in NeXTSTEP */

- read:(TypedStream *)ts
{
    [super read:ts];
    objc_read_type(ts, "i", &popup);
    return self;
}

- awake
{
    if ( !popup ) {
	if (!cell) {
	    cell = [[ButtonCell alloc] init];
	    [self setTitle:"Button"];
	}
    }
    instancename = "Button";
    return self;
}

- _init
{
    ActionCell	*savedCell;

    if ( popup) {
	savedCell = cell;
	cell = [cell target];
	[super _init];
	cell = savedCell;
    } else {
	[super _init];
    }
    return self;
}

- _managedBy:parent wid:(void *)widget 
{ 

/* In the case of having a PopUpList, the widget will be handled like a
 * subclass of Cell which it isn't.  We temporarily set the cell to its
 * target so our super class will instansiate it instead of the cell.
 * Doing this trick means we don't have re-code here everything done by
 * super and it's super.
 */

    ActionCell	*savedCell;
    

    if ( popup ) {
	savedCell = cell;
        cell = [cell target];
	[super _managedBy:parent wid:widget];
	cell = savedCell;
    } else {
	[super _managedBy:parent wid:widget];	
    }
    return self;
}

@end
