/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include "appletdefs.h"
#include "appletquickdraw.h"
#include "appletzoom.h"

#define zoomfixer 65536L

#define zoomsteps 16

Fixed zoomfract;




static short zoomblend (short i1, short i2) {

	Fixed smallFix,bigFix,tempFix;

	smallFix = zoomfixer * i1;
	
	bigFix = zoomfixer * i2;
	
	tempFix = FixMul (zoomfract, bigFix) + FixMul (zoomfixer-zoomfract, smallFix);
	
	return (FixRound (tempFix));
	} /*zoomblend*/
	

static void zoomrect (Rect *smallrect, Rect *bigrect, Boolean zoomup) {

	Fixed factor;
	Rect rect1,rect2,rect3,rect4;
	GrafPtr savePort,deskPort;
	short i;
	
	GetPort (&savePort);
	
	OpenPort (deskPort = (GrafPtr) NewPtr (sizeof (GrafPort)));
	
	InitPort (deskPort);
	
	SetPort (deskPort);
	
	PenPat (&quickdrawglobal (gray));
	
	PenMode (notPatXor);
	
	if (zoomup) {
	
		rect1 = *smallrect;
		
		factor = FixRatio(6,5);
		
		zoomfract = FixRatio(541,10000);
		}
	else {
		rect1 = *bigrect;
		
		factor = FixRatio(5,6);
		
		zoomfract = zoomfixer;
		}
		
	rect2 = rect1;
	
	rect3 = rect1;
	
	FrameRect (&rect1);
	
	for (i = 1; i<= zoomsteps; i++) {
	
		rect4.left = zoomblend (smallrect->left, bigrect->left);
		
		rect4.right = zoomblend (smallrect->right, bigrect->right);
		
		rect4.top = zoomblend (smallrect->top, bigrect->top);
		
		rect4.bottom = zoomblend (smallrect->bottom, bigrect->bottom);
		
		FrameRect (&rect4);
		
		FrameRect (&rect1);
		
		rect1 = rect2;
		
		rect2 = rect3;
		
		rect3 = rect4;
		 
		zoomfract = FixMul (zoomfract,factor);
		} /*for*/
		
	FrameRect (&rect1);
	
	FrameRect (&rect2);
	
	FrameRect (&rect3);
	
	ClosePort (deskPort);
	
	DisposePtr ((Ptr)deskPort);
	
	PenNormal ();
	
	SetPort (savePort);
	} /*zoomrect*/
	
	
void zoomport (WindowPtr w, boolean flup) {

	/*
	Zooms the window referenced by "w" either from an inivisible
	state to a visible state, or vice versa.  Pass true in the "flup"
	boolean parameter to zoom a window to open, an false to zoom
	it close.  The WindowPtr must have already been created elsewhere,
	and zooming the window invisible only hides the window, it does
	not destroy the WindowPtr data.
	*/
	
	Rect r1, r2, r3;

	SetPort (w);
	
	SetRect (&r1, 0, 20, 0, 20);
	
	r3 = (*w).portRect;
	
	r2 = r3;
	
	InsetRect (&r2, (r3.right - r3.left + 20) / 2, (r3.bottom - r3.top + 20) / 2);

	localtoglobalrect (&r2);
	
	localtoglobalrect (&r3);

	if (flup) {
	
		zoomrect (&r1, &r2, true);
		
		zoomrect (&r2, &r3, true);
		
		ShowWindow (w);
		
		SetPort (w);
		}
	else {
		HideWindow (w);
		
		zoomrect (&r2, &r3, false);
		
		zoomrect (&r1, &r2, false);
		}
	} /*zoomport*/