//=============================================================================
//
//		Copyright (C) 1996-1997 by Paul S. McCarthy and Eric Sunshine.
//				Written by Paul S. McCarthy and Eric Sunshine.
//							All Rights Reserved.
//
//		This notice may not be removed from this source code.
//
//		This object is included in the MiscKit by permission from the authors
//		and its use is governed by the MiscKit license, found in the file
//		"License.rtf" in the MiscKit distribution.	Please refer to that file
//		for a list of all applicable permissions and restrictions.
//
//=============================================================================
//-----------------------------------------------------------------------------
// SD_PageLayout.m
//
//		Custom subclass of NeXT's appkit PageLayout panel that adds
//		user controls for:
//
//				Margins
//				Pagination
//				Centering
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// $Id: SD_PageLayout.m,v 1.1 97/01/12 15:19:08 sunshine Exp $
// $Log:		SD_PageLayout.m,v $
// Revision 1.1	 97/01/12  15:19:08	 sunshine
// v28: Page Layout
// 
//-----------------------------------------------------------------------------
#import "SD_PageLayout.h"

#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Matrix.h>
#import <appkit/PrintInfo.h>
#import <appkit/TextField.h>
#import <objc/NXBundle.h>


@implementation SD_PageLayout
//-----------------------------------------------------------------------------
// loadAccessoryView
//-----------------------------------------------------------------------------
- (void)loadAccessoryView
	{
	View* v;
	char buff[ FILENAME_MAX + 1 ];

	[[NXBundle bundleForClass:[self class]]
		getPath:buff forResource:[[self class] name] ofType:"nib"];
	[NXApp loadNibFile:buff owner:self withNames:NO fromZone:[self zone]];

	v = [accessoryWindow setContentView:0];
	[accessoryWindow close];
	[accessoryWindow free];
	[self setAccessoryView:v];

	[[leftMarginField	setTarget:ok] setAction:@selector(performClick:)];
	[[rightMarginField	setTarget:ok] setAction:@selector(performClick:)];
	[[topMarginField	setTarget:ok] setAction:@selector(performClick:)];
	[[bottomMarginField setTarget:ok] setAction:@selector(performClick:)];

	[bottomMarginField setNextText:[scale nextText]];
	[scale setNextText:leftMarginField];
	}


//-----------------------------------------------------------------------------
// +new
//-----------------------------------------------------------------------------
+ (id)new
	{
	static id p = 0;
	if (p == 0)
		{
		p = [super new];
		[p loadAccessoryView];
		}
	return p;
	}


//-----------------------------------------------------------------------------
// + launch:
//-----------------------------------------------------------------------------
+ (id)launch:(id)sender
	{
	[SD_PageLayout new];
	[NXApp runPageLayout:sender];
	return self;
	}


//-----------------------------------------------------------------------------
// pickedUnits:
//-----------------------------------------------------------------------------
- (id)pickedUnits:(id)sender
	{
	float old_factor, new_factor, scaler;

	[self convertOldFactor:&old_factor newFactor:&new_factor];
	scaler = new_factor / old_factor;

	[leftMarginField setFloatValue:	   [leftMarginField floatValue] * scaler];
	[rightMarginField setFloatValue:  [rightMarginField floatValue] * scaler];
	[topMarginField setFloatValue:		[topMarginField floatValue] * scaler];
	[bottomMarginField setFloatValue:[bottomMarginField floatValue] * scaler];

	return [super pickedUnits:sender];
	}


//-----------------------------------------------------------------------------
// pagination_to_slot
//-----------------------------------------------------------------------------
static int pagination_to_slot( int pg )
	{
	int slot = 1;
	if (pg == NX_FITPAGINATION)
		slot = 0;
	else if (pg == NX_CLIPPAGINATION)
		slot = 2;
	return slot;
	}


//-----------------------------------------------------------------------------
// slot_to_pagination
//-----------------------------------------------------------------------------
static int slot_to_pagination( int slot )
	{
	int pg = NX_AUTOPAGINATION;
	if (slot == 0)
		pg = NX_FITPAGINATION;
	else if (slot == 2)
		pg = NX_CLIPPAGINATION;
	return pg;
	}


//-----------------------------------------------------------------------------
// readPrintInfo
//-----------------------------------------------------------------------------
- (id)readPrintInfo
	{
	int pg_row, pg_col;
	float left,right,top,bottom;
	float old_factor, new_factor;
	id rc = [super readPrintInfo];
	PrintInfo* pinfo = [NXApp printInfo];
	[pinfo getMarginLeft:&left right:&right top:&top bottom:&bottom];
	[self convertOldFactor:&old_factor newFactor:&new_factor];

	[leftMarginField   setFloatValue:new_factor*left];
	[rightMarginField  setFloatValue:new_factor*right];
	[topMarginField	   setFloatValue:new_factor*top];
	[bottomMarginField setFloatValue:new_factor*bottom];

	[centerMatrix selectCellAt:(int)[pinfo isVertCentered]
							  :(int)[pinfo isHorizCentered]];

	pg_row = pagination_to_slot( [pinfo vertPagination] );
	pg_col = pagination_to_slot( [pinfo horizPagination] );
	[paginationMatrix selectCellAt:pg_row:pg_col];

	return rc;
	}


//-----------------------------------------------------------------------------
// writePrintInfo
//-----------------------------------------------------------------------------
- (id)writePrintInfo
	{
	id rc = [super writePrintInfo];
	PrintInfo* pinfo = [NXApp printInfo];

	float old_factor, new_factor;
	[self convertOldFactor:&old_factor newFactor:&new_factor];

	[pinfo setMarginLeft:[leftMarginField	floatValue] / old_factor
				   right:[rightMarginField	floatValue] / old_factor
					 top:[topMarginField	floatValue] / old_factor
				  bottom:[bottomMarginField floatValue] / old_factor];

	[pinfo setVertCentered: [centerMatrix selectedRow]];
	[pinfo setHorizCentered:[centerMatrix selectedCol]];

	[pinfo setHorizPagination:
				slot_to_pagination([paginationMatrix selectedCol])];
	[pinfo setVertPagination:
				slot_to_pagination([paginationMatrix selectedRow])];
	return rc;
	}

@end
