// Copyright 1997-2000 Omni Development, Inc.  All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.

#import "OHFormActionButton.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <OmniBase/OmniBase.h>
#import <OmniFoundation/OmniFoundation.h>
#import <OmniAppKit/OmniAppKit.h>
#import <OWF/OWF.h>

#import "OHForm.h"
#import "OWScriptEventHandlerHolder.h"

RCS_ID("$Header: /Network/Source/CVS/OmniGroup/Frameworks/OmniHTML/Forms.subproj/OHFormActionButton.m,v 1.6 2000/01/20 00:27:50 kc Exp $")

@implementation OHFormActionButton

- initWithForm:(OHForm *)aForm name:(NSString *)aName value:(NSString *)aValue action:(SEL)anAction title:(NSString *)aTitle;
{
    if (![super initWithForm:aForm name:aName value:aValue])
        return nil;

    title = [aTitle retain];
    target = self;
    action = anAction;

    return self;
}

- (void)dealloc;
{
    if (target != self)
        [target release];
    [title release];
    [super dealloc];
}

//

- (void)setTarget:(id)newTarget;
{
    if (target == newTarget)
        return;
    if (target != self)
        [target release];
    target = (newTarget == self) ? self : [newTarget retain];
    if (button)
        [button setTarget:newTarget];
}

// Actions

// These are invoked by our button or our event object and so are only called in the appkit thread.
- (void)submitForm;
{
    generatedSubmit = YES;
    [nonretainedForm submit];
}

- (void)resetForm;
{
    [nonretainedForm reset];
}

// OHFormButton subclass

- (void)setupButton;
{
    [super setupButton];
    [button setButtonType:NSMomentaryPushButton];
    [button setImagePosition:NSNoImage];
    [button setBordered:YES];
    [button setTitle:title];
    [button setTarget:target];
    if (target == self) {
        // If (and only if) target is self, we're a non-internal button, and we want to generate script events.
        [button setAction:@selector(generateClickEvent:)];
    } else {
        // Don't generate events for e.g. File element auxiliary button.
        [button setAction:action];
    }
    [self reset];
}

- (BOOL)includeFormValues;
{
    BOOL returnValue;

    returnValue = generatedSubmit;
    generatedSubmit = NO; // Reset for next time
    return returnValue;
}

// OHFormElement protocol (partial)

- (NSString *)type;
{
    if (target != self)
        return @"InternalButton";
    else if (action == @selector(submitForm))
        return @"Submit";
    else if (action == @selector(resetForm))
        return @"Reset";
    else
        return [super type];
}

- (void)generateClickEvent:(id)sender
{
    OWScriptEvent *click;

    click = [[OWScriptEvent alloc] initWithType:OWSE_Click fromEvent:[NSApp currentEvent]];
    [click setTarget:self action:action];
    [click setInstigator:self];

    // TODO: efficiency shortcuts here ?
    [click addResponder:eventHandlers];
    // TODO: event capture by parent objects

    [click deliverAsynchronously];
    [click release];
}

@end
