/*
 *   This sourcecode is part of FileSpy, a logfile observing utility.
 *   Copyright (C) 1996  Felix Rauch
 *
 *   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.
 *   
 *   Notice that this program may not be possessed, used, copied,
 *   distributed or modified by people having to do with nuclear
 *   weapons. See the file CONDITIONS for details.
 *
 *   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 the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   To contact the original author, try:
 *   e-mail: Felix.Rauch@nice.ch
 *   Traditional mail:	Felix Rauch
 *			Sempacherstrasse 33
 *			8032 Zurich
 *			Switzerland
 */

// Most of this has been taken from YapDocument.m

#import "Document.h"

#define MAXSIZE 1.0e38	// Maximum size of a text object

@implementation Document

+ new:sender;
{
    id docWin;		/* Window belonging to this document. */
    id textObj;		/* The text object we put in the window. */
    NXRect textFrame;	/* The frame of the text object in our window */

    self = [[Document allocFromZone:[self newZone]] init];

    if (![NXApp loadNibSection:"Document.nib" owner:self withNames:NO fromZone:[self zone]]) {
	NXLogError ("Can't find Document.nib!");	
        [self free];
	return nil;
    }
    
    docWin = [document window];
    [[document docView] getFrame:&textFrame];
    [docWin setDelegate:sender];
    textObj = [[Text alloc] initFrame:&textFrame];
    [[document setDocView:textObj] free];
    [document setAutoresizeSubviews:YES];
    [textObj setVertResizable:YES];		// Grow down as you type
    [textObj setHorizResizable:NO];		// But not sideways 
    [textObj setAutosizing:NX_WIDTHSIZABLE];	// Size horizontally when resized
    [textObj setMonoFont:YES];
    [textObj setOpaque:YES];
    [textObj setMinSize:&textFrame.size];
    NX_WIDTH(&textFrame) = NX_HEIGHT(&textFrame) = MAXSIZE;
    [textObj setMaxSize:&textFrame.size];	// Can grow
    [textObj setSel:0:0];			// Set the selection
    [textObj setDelegate:self];
    [textObj sizeToFit];
    [textObj setEditable:NO];

    
    [self setDocument:document];
    [self setWindow:docWin];			// make the new instance familiar with the window
    return self;
}

/*
 * The next two methods allow us to cache/reuse zones.
 */
static id zoneList = nil;

+ (NXZone *)newZone
{
    if (!zoneList || ![zoneList count]) {
	return NXCreateZone(vm_page_size, vm_page_size, YES);
    } else {
	return (NXZone *)[zoneList removeLastObject];
    }
}

+ (void)reuseZone:(NXZone *)aZone
{
    if (!zoneList) zoneList = [List new];
    [zoneList addObject:(id)aZone];
}


- free
{
    NXZone *docZone = [self zone];
    [super free];
    [Document reuseZone:docZone];
    return nil;
}

- scrollView
{
	return document;
}

- setDocument:doc
{
	document = doc;
	return self;
}

- setWindow:window
{
	myWin = window;
	return self;
}

- window
{
    return myWin;
}

- moveWindowTo:(NXCoord)x:(NXCoord)y
{
	[myWin moveTo:x:y];
	return self;
}

@end
