%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% testpage.m                               %
% written 2/26/00 by ben olding            %
% user interface for making a test page    %
% invokes function makepage.m              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Requires: makepage.m, pagetitle.m, setfont.m, defbox.m
%           addcell.m, initps.m, al.m, generatedata.m,
%           MasterDataList.m, findtetra.m, xyz2cmy.m,
%           tetravolume.m, coplanar.m

choice = [];
fname = 'test';
ptitle= '';
crange= [0 1];
mrange= [0 1];
yrange= [0 1];
toppts= 0;
papersize=[40 5];
margin= [0.5 0.5];
boxsize= [1 1];

while choice~='0',
  clc;
  disp ('---- Choose an option ----');
  disp (' ');
  disp (['  1) PostScript filename :  ',fname]);
  disp (['  2) Page title          :  ',ptitle]);
  disp (' ');
  disp (['  3) Cyan range          :  ',num2str(crange(1)),' to ', ...
	 num2str(crange(2))]);
  disp (['  4) Magenta range       :  ',num2str(mrange(1)),' to ', ...
	 num2str(mrange(2))]);
  disp (['  5) Yellow range        :  ',num2str(yrange(1)),' to ', ...
	 num2str(yrange(2))]);
  disp ('         --- OR ---');
  disp (['  6) # automatic points  :  ',num2str(toppts)]);
  disp (' ');
  disp (['  7) Papersize           :  ',num2str(papersize(1)),' x' ...
         ' ',num2str(papersize(2))]);
  disp (['  8) Margins             :  ',num2str(margin(1)),' x' ...
         ' ',num2str(margin(2))]);
  disp (['  9) Patch size          :  ',num2str(boxsize(1)),' x' ...
	 ' ',num2str(boxsize(2))]);					
  disp (' ');
  disp ('  0) Generate PostScript test page');
  disp (' ');
  choice = input('Choice : ','s');

  if choice == '1',
    i = input(['PostScript filename [',fname,']: '],'s');
    if ~isempty(i),
      fname = i;
    end;  
  end;

  if choice == '2',
    i = input(['Page title          [',ptitle,']   : '],'s');          
    if ~isempty(i),
      ptitle=i;
    end;
  end;

  if choice == '3'
    toppts = 0;
    i = [];
    while isempty(i),
      i = input(['Cyan start    (0 to 1) [',num2str(crange(1)),']   : ']);
      if ~isempty(i) & (i<0 | i>1),
        disp('??? I said between 0 and 1. Can''t you read?');
        i = [];
      elseif isempty(i),
        i=crange(1);
      else
        crange(1)=i;
      end;
    end;

    if crange(2)<crange(1),
      crange(2)=crange(1);
    end;  
    
    i = [];
    while isempty(i),
      lim=num2str(crange(1));
      i = input(['Cyan end      (',lim,' to 1) [',num2str(crange(2)),']' ...
		    ' : ']);
      if ~isempty(i) & (i<crange(1) | i>1),
        disp(['??? I said between ',lim,' and 1. Can''t you read?']);
        i = [];
      elseif isempty(i),
	i=crange(2);
      else
 	crange(2)=i;
      end; 
    end;
  end;
  
  if choice == '4'
    toppts = 0;
    i = [];
    while isempty(i),
      i = input(['Magenta start    (0 to 1) [',num2str(mrange(1)),']   : ']);
      if ~isempty(i) & (i<0 | i>1),
        disp('??? I said between 0 and 1. Can''t you read?');
        i = [];
      elseif isempty(i),
        i=mrange(1);
      else
        mrange(1)=i;
      end;
    end;

    if mrange(2)<mrange(1),
      mrange(2)=mrange(1);
    end;  

    i = [];
    while isempty(i),
      lim=num2str(mrange(1));
      i = input(['Magenta  end   (',lim,' to 1) [',num2str(mrange(2)),']' ...
		    ' : ']);            
      if ~isempty(i) & (i<mrange(1) | i>1),
        disp(['??? I said between ',lim,' and 1. Can''t you read?']);
        i = [];
      elseif isempty(i),
	i=mrange(2);
      else
 	mrange(2)=i;
      end; 
    end;
  end;
  
  if choice == '5'
    toppts = 0;
    i = [];
    while isempty(i),
      i = input(['Yellow start    (0 to 1) [',num2str(yrange(1)),']   : ']);
      if ~isempty(i) & (i<0 | i>1),
        disp('??? I said between 0 and 1. Can''t you read?');
        i = [];
      elseif isempty(i),
        i=yrange(1);
      else
        yrange(1)=i;
      end;
    end;

    if yrange(2)<yrange(1),
      yrange(2)=yrange(1);
    end;
    
    i = [];
    while isempty(i),
      lim=num2str(yrange(1));
      i = input(['Yellow end      (',lim,' to 1) [',num2str(yrange(2)),']' ...
		    ' : ']);
      if ~isempty(i) & (i<yrange(1) | i>1),
        disp(['??? I said between ',lim,' and 1. Can''t you read?']);
        i = [];
      elseif isempty(i),
	i=yrange(2);
      else
 	yrange(2)=i;
      end; 
    end;
  end;

  if choice == '6',
    i=[];
    while isempty(i),
      i = input (['Number of test points [',num2str(toppts),'] : ']);
      if ~isempty(i) & i<0,
	disp('??? A negative number of test points?  Please.');
        i = [];
      elseif isempty(i),
	i=toppts;
      else
	toppts=i;
      end;
    end;
    if toppts>0,
      crange = [0 1];
      mrange = [0 1];
      yrange = [0 1];
    end;
  end;
  
  if choice == '7',
    i=[];
    while isempty(i),
      i = input(['Paper width (inches) [',num2str(papersize(1)),']   : ']);
      if ~isempty(i) & i<0,
        disp('??? You can''t have negative inches.  Don''t be difficult.');
        i=[];
      elseif ~isempty(i) & i==0,
        disp('??? What?  Are you printing on a line?');
        i=[];
      elseif isempty(i),
        i=papersize(1);
      else
        papersize(1)=i;
      end;  
    end;
      
    i=[];
    while isempty(i),
      i = input(['Paper height (inches) [',num2str(papersize(2)),']   : ']);
      if ~isempty(i) & i<0,
        disp('??? You can''t have negative inches.  Don''t be difficult.');
        i=[];
      elseif ~isempty(i) & i==0,
        disp('??? What?  Are you printing on a line?');
        i=[];
      elseif isempty(i),
        i=papersize(2);
      else
        papersize(2)=i;
      end;
    end;
  end;

  if choice == '8',
    i=[];
    while isempty(i),
      i = input(['Left margin (inches) [',num2str(margin(1)),']   : ']);
      if ~isempty(i) & i<0,
        disp('??? You can''t have negative inches.  Don''t be difficult.');
        i=[];
      elseif ~isempty(i) & i>=papersize(1)/2,
        disp(['??? So you want your margins bigger than your paper?  Nice' ...
	   ' try.']);
        i=[];
      elseif isempty(i),
	i=margin(1);
      else
	margin(1)=i;
      end;
    end;
  
    i=[];
    while isempty(i),
      i = input(['Top margin (inches) [',num2str(margin(2)),']   : ']);
      if ~isempty(i) & i<0,
        disp('??? You can''t have negative inches.  Don''t be difficult.');
        i=[];
      elseif ~isempty(i) & i>=papersize(2)/2,
        disp(['??? So you want your margins bigger than your paper?  Nice' ...
	   ' try.']);
        i=[];
      elseif isempty(i),
	i=margin(2);
      else
	margin(2)=i;
      end;
    end;
  end;
    
  if choice == '9',
    i=[];
    while isempty(i),
      i = input(['Box width (inches) [',num2str(boxsize(1)),']   : ']);
      if ~isempty(i) & i<0,
        disp('??? You can''t have negative inches.  Don''t be difficult.');
        i=[];
      elseif ~isempty(i) & i>=(papersize(1)-2*margin(1)),
        disp(['??? So you want your patches wider than your paper?  Nice' ...
	   ' try.']);
        i=[];
      elseif isempty(i),
	i=boxsize(1);
      else
	boxsize(1)=i;
      end;
    end;
  
    i=[];
    while isempty(i),
      i = input(['Box height (inches) [',num2str(boxsize(2)),']   : ']);
      if ~isempty(i) & i<0,
        disp('??? You can''t have negative inches.  Don''t be difficult.');
        i=[];
      elseif ~isempty(i) & i>=(papersize(2)-2*margin(2)),
        disp(['??? So you want your patches taller than your paper?  Nice' ...
	   ' try.']);
        i=[];
      elseif isempty(i),
	i=boxsize(2);
      else
	boxsize(2)=i;
      end;
    end;
  end;
end;

if toppts == 0,
  makepage (fname,papersize,margin,boxsize,ptitle,crange,mrange,yrange);
else
  colorspace = generatedata(toppts,fname);
  makepage (fname,papersize,margin,boxsize,ptitle,crange,mrange, ...
            yrange,colorspace);
end;
