%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%             Thank you for using ClarkWare Products!!!!               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GKEYBOX Key Box automator..... -- "key" is also known as "legends"
% 	GKEYBOX(Key#1String, Key#1linetype, Key#2String, Key#2linetype,...)
%	up to ten (10) sets of key strings and linetypes can be defined.
%	(you want MORE? - what does your plot look like??) :-) 
%       linetypes are any of those accepted by the PLOT command.
%	GKEYBOX puts up a mouse pointer for you to position the top
%	left hand corner of the key box. The key will then be placed on
%       the plot and the mouse pointer will wait for you to tell it the 
% 	furthest x point away (I have no way of telling how far the 
%	text stretches on the graphics screen). A box will then be 
%       drawn around the key list. Also see GCAD GKEY GTEXT GINPUT
%       GKEYBOX works in polar and rectangular plots....
%
%       eg gkeybox('hi','-','hello','g--'); gkeybox can be tested
%       by typing "global gkeyboxtest" before running it.
%         

%	Written in a moment of insanity (02h29 29 Dec 1992) by:
%       Alan Robert Clark; Dept Elec Eng; P.O.Wits; 2050 South Africa;
%	clark@odie.ee.wits.ac.za.

function gkeybox(              key1str, key1lntyp, key2str, key2lntyp,...
                               key3str, key3lntyp, key4str, key4lntyp,...
                               key5str, key5lntyp, key6str, key6lntyp,...
	                       key7str, key7lntyp, key8str, key8lntyp,...
                               key9str, key9lntyp, key10str, key10lntyp)
% Idiot Proofing.
if nargin == 0 
  error ('gkeybox must be called with arguments! - type help gkeybox');
end;
numberofkeys=nargin/2;
if round(numberofkeys)~=numberofkeys
  error ('gkeybox arguments are to be in pairs -- keystr#1,keylinetype#1, etc');
end; 

% For test purposes -- type 'global gkeyboxtest' before running
if exist('gkeyboxtest')==1 
  clg;
  pr=0:0.1:3*pi;
  plot(sin(pr));
  clear pr;
end;

% Get the top left hand point for the key listing;
[x,y]=ginput(1);

% Get actual plot coords & calculate relative lengths
[a]=axis; 
xlength = a(2)-a(1);
ylength = a(4)-a(3);

% Establish a std length of key line -- half way up the text..
keylinex = [x,x+0.07*xlength].'; 
keyliney = [y+0.020*ylength,y+0.020*ylength].';

% Establish a text spacing in the vertical..
textheight = 0.040*ylength;

% Shove the keys on the plot
for i=1:numberofkeys
  presenty=[y+0.020*ylength-i*textheight,y+0.020*ylength-i*textheight].';
  hold on; 
  plot(keylinex,presenty,eval(['key',i+48,'lntyp'])); 
  hold off;
  text(x+0.08*xlength,y-i*textheight,eval(['key',i+48,'str']));
end; 
endy=y-i*textheight;
[endx,dummy]=ginput(1);
xstart=x-0.020*xlength;
ystart=y+0.2*textheight;
boxx=[xstart,endx,endx,xstart,xstart].';
boxy=[ystart,ystart,endy,endy,ystart].';
hold on; plot(boxx,boxy,'-'); hold off;
end;
