//	Lefty.c
//	Copyright 1998-2004 by Joseph J. Strout (joe@strout.net).
//
//	This file contains lefty-support code.  For more info, see 
//	<http://www.strout.net/info/pilot/leftypilot/code.html>.
//
//	This code is in the public domain.  Share and enjoy.
//
//		5/09/98	JJS		Initial release.
//		4/12/04 JJS		Updated include from <Pilot.h> to <PalmOS.h>.
//						Updated data types to new standards (UInt8, Int16, etc.).
//
//----------------------------------------------------------------------

#include <PalmOS.h>
#include "Lefty.h"

Boolean UserIsLefty(void)
{
	// read Lefty preference
	UInt8 leftyFlag = 0;
	UInt16 size = sizeof(UInt8);
	if (!PrefGetAppPreferencesV10('Lfty', 1, &leftyFlag, size )) leftyFlag = 0;
	return (leftyFlag != 0);
}

void LeftyFlip(FormPtr frm, UInt16 objIndex, Int16 YAxis)
{
	// find bounds
	RectangleType r;
	FrmGetObjectBounds(frm, objIndex, &r);
	// shift object
	r.topLeft.x += YAxis - r.topLeft.x + YAxis - r.topLeft.x - r.extent.x;
	FrmSetObjectPosition(frm, objIndex, r.topLeft.x, r.topLeft.y);
}

void LeftyFlipByID(FormPtr frm, UInt16 objID, Int16 YAxis)
{
	// convert ID numbers to index numbers;
	UInt16 objIndex = FrmGetObjectIndex( frm, objID );
	// call the other method
	LeftyFlip(frm, objIndex, YAxis);
}	

Int16 LeftyFindAxis(FormPtr frm, UInt16 objIndex1, UInt16 objIndex2)
{
	RectangleType r1, r2;
	short right1,right2, leftmost,rightmost;

	// find the extent of each object
	FrmGetObjectBounds(frm, objIndex1, &r1);
	FrmGetObjectBounds(frm, objIndex2, &r2);

	// return the point halfway between the leftmost left and the rightmost right
	right1 = r1.topLeft.x + r1.extent.x;
	right2 = r2.topLeft.x + r2.extent.x;
	
	leftmost = (r1.topLeft.x < r2.topLeft.x ? r1.topLeft.x : r2.topLeft.x);
	rightmost = (right1 > right2 ? right1 : right2);
	return (leftmost + rightmost) / 2;
}

Int16 LeftyFindAxisByID(FormPtr frm, UInt16 objID1, UInt16 objID2)
{
	// convert ID numbers to index numbers;
	UInt16 objIndex1 = FrmGetObjectIndex( frm, objID1 );
	UInt16 objIndex2 = FrmGetObjectIndex( frm, objID2 );
	// call the other method
	return LeftyFindAxis(frm, objIndex1, objIndex2);
}

void LeftySwap(FormPtr frm, UInt16 objIndex1, UInt16 objIndex2)
{
	RectangleType r1, r2;
	short delta1, delta2;

	// find the bounds of each object
	FrmGetObjectBounds(frm, objIndex1, &r1);
	FrmGetObjectBounds(frm, objIndex2, &r2);

	// shift object 1 so its right side is where obj2's right side was,
	delta1 = (r2.topLeft.x+r2.extent.x) - (r1.topLeft.x+r1.extent.x);
	// and shift object 2 so its left side is where obj1's left side was
	delta2 = r1.topLeft.x - r2.topLeft.x;
	
	FrmSetObjectPosition(frm, objIndex1, r1.topLeft.x + delta1, r1.topLeft.y);
	FrmSetObjectPosition(frm, objIndex2, r2.topLeft.x + delta2, r2.topLeft.y);
}

void LeftySwapByID(FormPtr frm, UInt16 objID1, UInt16 objID2)
{
	// convert ID numbers to index numbers;
	UInt16 objIndex1 = FrmGetObjectIndex( frm, objID1 );
	UInt16 objIndex2 = FrmGetObjectIndex( frm, objID2 );
	// call the other method
	LeftySwap(frm, objIndex1, objIndex2 );
}
