// 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 "OHFormTextArea.h"

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

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

#import "OHFormValue.h"

@implementation OHFormTextArea

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (NSView *)setupTextField;
{
    NSRect textFrame, scrollFrame;
    static const NSSize veryLarge = {1e+30, 1e+30};
    NSScrollView *scrollView;

    ASSERT_IN_MAIN_THREAD(@"AppKit calls must be from main thread!");
    OBASSERT(!formTextFlags.invisibleInput); // Can't have invisible text areas

    textFrame = [isa textFrameForWidth:width height:height];
    textView = [[NSTextView alloc] initWithFrame:textFrame];
    if (initialValue)
        [textView setString:initialValue];
    [textView setAlignment:textAlignment];
    [textView setVerticallyResizable:YES];
    [textView setDrawsBackground:YES];
    [textView setAutoresizingMask:NSViewWidthSizable];
    [textView setRichText:NO];
    [textView setFont:[isa textFont]];
    [textView setMaxSize:veryLarge];
    [textView sizeToFit];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:NSTextStorageDidProcessEditingNotification object:[textView textStorage]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditing:) name:NSTextDidEndEditingNotification object:textView];

    // Add space for the border
    textFrame.size.width += 6.0;

    scrollFrame.origin = NSMakePoint(0.0, 0.0);
    scrollFrame.size = [NSScrollView frameSizeForContentSize:textFrame.size hasHorizontalScroller:NO hasVerticalScroller:YES borderType:NSBezelBorder];

    scrollView = [[NSScrollView alloc] initWithFrame:scrollFrame];
    [scrollView setDocumentView:textView];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setHasHorizontalScroller:NO];
    [scrollView setBorderType:NSBezelBorder];
    [scrollView setBackgroundColor:[NSColor whiteColor]];
    [scrollView freeGStates];
    [textView release];
    textField = nil;
    return [scrollView autorelease];
}

- (void)setAutoSubmit;
{
    // Text areas can't auto-submit
}

- (void)textDidChange:(NSNotification *)aNotification
{
    [elementStateLock lock];
    [currentValue release];
    currentValue = [[textView string] copy];
    [elementStateLock unlock];
}

- (void)textDidEndEditing:(NSNotification *)aNotification
{
    [self userDidChangeValue];
}

- (void)updateUI
{
    ASSERT_MAIN_THREAD_OPS_OK(@"AppKit calls must be from main thread!");
    [elementStateLock lock];
    [textView setString:currentValue];
    [elementStateLock unlock];
    [textView sizeToFit];
}

// OAOptionalSearchableCell informal protocol

- (NSView <OASearchableContent> *)searchableContentView;
{
    return textView;
}

// OHFormElement protocol (partial)

- (NSString *)type;
{
    return @"TextArea";
}

@end
