/* -------------------------------------------------------------------
#import <appkit/PageLayout.h>
	RDR, Inc. 

	Objective-C source file for the class PageMargins

	COPYRIGHT (C), 1991, RDR, Inc.
	ALL RIGHTS RESERVED.

	Responsible:			Approved:
	RDR:Ernest Prabhakar		

	Date:				Rev:
	1991-Jun-28			___

                             PageMargins                              

	1. Introduction
 * PageLayout is overridden so that the user can set the margins of
 * the page.  This is important in a Drawing program where the user
 * typically wants to maximize the drawable area on the page.
 *
 * The accessory view is used to add the additional fields, and
 * pickedUnits: is overridden so that the margin is displayed in the
 * currently selected units.  Note that the accesory is 
 * |built in AppWithDoc|
 *
 * This can be used as an example of how to override Application Kit panels.

Stolen almost in entirety from the Draw Example

	2. Revision History

	___	The starting point.	Ernest Prabhakar/1991-Jun-28


	3. Source Code

------------------------------------------------------------------- */

/* ------------------------- Import files ------------------------- */

/* ------------------------  Classes used  ------------------------ */
#import <appkit/Application.h>
#import <appkit/Matrix.h>
#import <appkit/PrintInfo.h>

/* ----------------------  Class variables  ----------------------- */

/*====================================================================
                 Implementation of class PageMargins                  
====================================================================*/
#import "PageMargins.h"

@implementation PageMargins : PageLayout
{
    id leftMargin;
    id rightMargin;
    id topMargin;
    id bottomMargin;
}

/*====================================================================
                           State Variables                            
====================================================================*/

/*--------------------------------------------------------------------
|-setTopBotForm:anObject| 	      Set Top and Bottom Margin Fields
Returns self.
                                          Ernest Prabhakar/1991-Jun-28
--------------------------------------------------------------------*/
-setTopBotForm:anObject
{
    [anObject setTarget:ok];
    [anObject setAction:@selector(performClick:)];
    [anObject setNextText:width];
    topMargin = [anObject findCellWithTag:5];
    bottomMargin = [anObject findCellWithTag:6];
    return self;
}

/*--------------------------------------------------------------------
|-setSideForm:anObject| 			Set Side Margin Fields
Returns self.
                                          Ernest Prabhakar/1991-Jun-28
--------------------------------------------------------------------*/
-setSideForm:anObject
{
    [scale setNextText:anObject];
    [anObject setTarget:ok];
    [anObject setAction:@selector(performClick:)];
    leftMargin = [anObject findCellWithTag:3];
    rightMargin = [anObject findCellWithTag:4];
    return self;
}

/*====================================================================
                            Action Methods                            
====================================================================*/

/*--------------------------------------------------------------------
|-pickedUnits:sender| 
 * Called when the user selects different units (e.g. cm or inches).
 * Must update the margin fields.
Returns self.
                                          Ernest Prabhakar/1991-Jun-28
--------------------------------------------------------------------*/
-pickedUnits:sender
{
    float old, new;

    [self convertOldFactor:&old newFactor:&new];
    [leftMargin setFloatValue:new * [leftMargin floatValue] / old];
    [rightMargin setFloatValue:new * [rightMargin floatValue] / old];
    [topMargin setFloatValue:new * [topMargin floatValue] / old];
    [bottomMargin setFloatValue:new * [bottomMargin floatValue] / old];

    return [super pickedUnits:sender];
}

/*====================================================================
                        Exchange Margins Data                         
====================================================================*/

/*--------------------------------------------------------------------
|-readPrintInfo| 		Set the fields from the current Doc.
 * Sets the margin fields from the Application-wide PrintInfo.
Returns self.
                                          Ernest Prabhakar/1991-Jun-28
--------------------------------------------------------------------*/
-readPrintInfo
{
    id pi;
    float conversion, dummy;
    NXCoord left, right, top, bottom;

    [super readPrintInfo];
    pi = [NXApp printInfo];
    [self convertOldFactor:&conversion newFactor:&dummy];
    [pi getMarginLeft:&left right:&right top:&top bottom:&bottom];
    [leftMargin setFloatValue:left * conversion];
    [rightMargin setFloatValue:right * conversion];
    [topMargin setFloatValue:top * conversion];
    [bottomMargin setFloatValue:bottom * conversion];

    return self;
}

/*--------------------------------------------------------------------
|-writePrintInfo| 			    Write to current document.
 * Sets the margin values in the Application-wide PrintInfo from
 * the margin fields in the panel.
Returns self.
                                          Ernest Prabhakar/1991-Jun-28
--------------------------------------------------------------------*/
-writePrintInfo
{
    id pi;
    float conversion, dummy;

    [super writePrintInfo];
    pi = [NXApp printInfo];
    [self convertOldFactor:&conversion newFactor:&dummy];
    if (conversion) {
	[pi setMarginLeft:[leftMargin floatValue] / conversion
		    right:[rightMargin floatValue] / conversion
		      top:[topMargin floatValue] / conversion
		   bottom:[bottomMargin floatValue] / conversion];
    }

    return self;
}

@end
