function rect = winpos( pos, scale )
%WINPOS      Position a figure window on the screen.
%   RECT = WINPOS( POS, SCALE ) repositions a figure window on the
%   screen.  POS may either be a rectangle argument [llx lly wid hgt]
%   or may be a string argument ('tr', 'tl', 'br', 'bl' ) which
%   specifies which corner of the screen to place the window.  The
%   latter form will set the size of the window to be SCALE(1) the
%   width and SCALE(2) the height of your computer screen.  SCALE
%   defaults to [1/2 1/2] if unspecified.

%----------------------------------------------------------------------
%			  Copyright (C) 1995
%		Massachussetts Institute of Technology
%	       Sea Grant Underwater Vehicles Laboratory
%----------------------------------------------------------------------
%     $Revision: 1.1 $			$State: Exp $
%     $Author: rob $		$Date: 2000/01/10 19:08:44 $
%----------------------------------------------------------------------

if ( isstr( pos ) )
	if ( nargin < 2 ), scale = [1/2 1/2]; end
				% get screen size
	sunits = get( 0, 'units' );
	screen = get( 0, 'screensize' );
				% determine actual maximum figure size
				% by querying a hypothetical figure
				% set to max screen (invisible because
				% it's a secret how i do this)
	units = get( gcf, 'units' );
	set( gcf, 'visible', 'off', 'units', sunits, 'position', ...
		screen );
				% i work in pixels
	set( gcf, 'units', 'pixels' );
	rect = get( gcf, 'position' );
				% the 8 and 4 look good on my mac
	rect(3:4) = (rect(3:4) - rect(1:2)) .* scale - 8;
	if ( any( pos == 'r') ), rect(1) = screen(3) - rect(3) + 4; end
	if ( any( pos == 't') ), rect(2) = screen(4) - rect(4) + 4; end
				% resize and make visible
	set( gcf, 'position', rect, 'visible', 'on' );
				% restore units
	set( gcf, 'units', units );
else
				% if pos was a rectangle, correct
				% units are assumed
	rect = pos;
	set( gcf, 'position', rect );
end
return;
